From a4847fc3ef139ba9a8ffebb6ffa06ee72078ffa2 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 25 Mar 2015 19:05:20 -0400 Subject: Add an ASSERT statement in plpgsql. This is meant to make it easier to insert simple debugging cross-checks in plpgsql functions. Pavel Stehule, reviewed by Jim Nasby --- doc/src/sgml/plpgsql.sgml | 77 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 4 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 9fc2a2f498b..d36acf6d996 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -2562,8 +2562,9 @@ END; those shown in . A category name matches any error within its category. The special condition name OTHERS matches every error type except - QUERY_CANCELED. (It is possible, but often unwise, - to trap QUERY_CANCELED by name.) Condition names are + QUERY_CANCELED and ASSERT_FAILURE. + (It is possible, but often unwise, to trap those two error types + by name.) Condition names are not case-sensitive. Also, an error condition can be specified by SQLSTATE code; for example these are equivalent: @@ -3387,8 +3388,12 @@ END LOOP label ; Errors and Messages + + Reporting Errors and Messages + RAISE + in PL/pgSQL @@ -3580,6 +3585,67 @@ RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id; + + + + Checking Assertions + + + ASSERT + in PL/pgSQL + + + + assertions + in PL/pgSQL + + + + plpgsql.check_asserts configuration parameter + + + + The ASSERT statement is a convenient shorthand for + inserting debugging checks into PL/pgSQL + functions. + + +ASSERT condition , message ; + + + The condition is a boolean + expression that is expected to always evaluate to TRUE; if it does, + the ASSERT statement does nothing further. If the + result is FALSE or NULL, then an ASSERT_FAILURE exception + is raised. (If an error occurs while evaluating + the condition, it is + reported as a normal error.) + + + + If the optional message is + provided, it is an expression whose result (if not null) replaces the + default error message text assertion failed, should + the condition fail. + The message expression is + not evaluated in the normal case where the assertion succeeds. + + + + Testing of assertions can be enabled or disabled via the configuration + parameter plpgsql.check_asserts, which takes a boolean + value; the default is on. If this parameter + is off then ASSERT statements do nothing. + + + + Note that ASSERT is meant for detecting program + bugs, not for reporting ordinary error conditions. Use + the RAISE statement, described above, for that. + + + + @@ -5075,8 +5141,7 @@ $func$ LANGUAGE plpgsql; PostgreSQL does not have a built-in instr function, but you can create one using a combination of other - functions.instr In there is a + functions. In there is a PL/pgSQL implementation of instr that you can use to make your porting easier. @@ -5409,6 +5474,10 @@ $$ LANGUAGE plpgsql STRICT IMMUTABLE; your porting efforts. + + instr function + + -- -- instr functions that mimic Oracle's counterpart -- cgit v1.2.3