There are two commands available that allow you to return data from a function:
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 and
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1.
43.6.1.1.
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0
RETURN expression;
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 with an expression terminates the function and returns the value of
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
4 to the caller. This form is used for PL/pgSQL functions that do not return a set.
In a function that returns a scalar type, the expression's result will automatically be cast into the function's return type as described for assignments. But to return a composite (row) value, you must write an expression delivering exactly the requested column set. This may require use of explicit casting.
If you declared the function with output parameters, write just
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 with no expression. The current values of the output parameter variables will be returned.
If you declared the function to return
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
6, a
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement can be used to exit the function early; but do not write an expression following
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0.
The return value of a function cannot be left undefined. If control reaches the end of the top-level block of the function without hitting a
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement, a run-time error will occur. This restriction does not apply to functions with output parameters and functions returning
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
6, however. In those cases a
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement is automatically executed if the top-level block finishes.
Some examples:
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
43.6.1.2.
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 and
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
When a PL/pgSQL function is declared to return
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
4, the procedure to follow is slightly different. In that case, the individual items to return are specified by a sequence of
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 or
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 commands, and then a final
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 command with no argument is used to indicate that the function has finished executing.
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 can be used with both scalar and composite data types; with a composite result type, an entire “table” of results will be returned.
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 appends the results of executing a query to the function's result set.
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 and
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 can be freely intermixed in a single set-returning function, in which case their results will be concatenated.
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 and
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 do not actually return from the function — they simply append zero or more rows to the function's result set. Execution then continues with the next statement in the PL/pgSQL function. As successive
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 or
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 commands are executed, the result set is built up. A final
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0, which should have no argument, causes control to exit the function (or you can just let control reach the end of the function).
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 has a variant
IF boolean-expression THEN_`statements`_ END IF;
8, which specifies the query to be executed dynamically. Parameter expressions can be inserted into the computed query string via
IF boolean-expression THEN_`statements`_ END IF;
9, in just the same way as in the
IF v_user_id <> 0 THENUPDATE users SET email = v_email WHERE user_id = v_user_id; END IF;
0 command.
If you declared the function with output parameters, write just
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 with no expression. On each execution, the current values of the output parameter variable(s) will be saved for eventual return as a row of the result. Note that you must declare the function as returning
IF v_user_id <> 0 THENUPDATE users SET email = v_email WHERE user_id = v_user_id; END IF;
2 when there are multiple output parameters, or
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
4 when there is just one output parameter of type
IF v_user_id <> 0 THENUPDATE users SET email = v_email WHERE user_id = v_user_id; END IF;
4, in order to create a set-returning function with output parameters.
Here is an example of a function using
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
Here is an example of a function using
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3:
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
Note
The current implementation of
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
1 and
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
3 stores the entire result set before returning from the function, as discussed above. That means that if a PL/pgSQL function produces a very large result set, performance might be poor: data will be written to disk to avoid memory exhaustion, but the function itself will not return until the entire result set has been generated. A future version of PL/pgSQL might allow users to define set-returning functions that do not have this limitation. Currently, the point at which data begins being written to disk is controlled by the configuration variable. Administrators who have sufficient memory to store larger result sets in memory should consider increasing this parameter.
43.6.2. Returning from a Procedure
A procedure does not have a return value. A procedure can therefore end without a
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement. If you wish to use a
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement to exit the code early, write just
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 with no expression.
If the procedure has output parameters, the final values of the output parameter variables will be returned to the caller.
43.6.3. Calling a Procedure
A PL/pgSQL function, procedure, or
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
2 block can call a procedure using
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
3. Output parameters are handled differently from the way that
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
3 works in plain SQL. Each
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
5 or
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
6 parameter of the procedure must correspond to a variable in the
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
3 statement, and whatever the procedure returns is assigned back to that variable after it returns. For example:
CREATE PROCEDURE triple(INOUT x int) LANGUAGE plpgsql AS $$ BEGINx := x * 3; END; $$; DO $$ DECLARE myvar int := 5; BEGIN CALL triple(myvar); RAISE NOTICE 'myvar = %', myvar; -- prints 15 END; $$;
The variable corresponding to an output parameter can be a simple variable or a field of a composite-type variable. Currently, it cannot be an element of an array.
43.6.4. Conditionals
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8 and
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9 statements let you execute alternative commands based on certain conditions. PL/pgSQL has three forms of
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8:
- IF parentid IS NULL OR parentid = '' THEN RETURN fullname; ELSE RETURN hp_true_filename(parentid) || '/' || fullname; END IF; 1
- IF parentid IS NULL OR parentid = '' THEN RETURN fullname; ELSE RETURN hp_true_filename(parentid) || '/' || fullname; END IF; 2
- IF parentid IS NULL OR parentid = '' THEN RETURN fullname; ELSE RETURN hp_true_filename(parentid) || '/' || fullname; END IF; 3
and two forms of
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9:
- IF parentid IS NULL OR parentid = '' THEN RETURN fullname; ELSE RETURN hp_true_filename(parentid) || '/' || fullname; END IF; 5
- IF parentid IS NULL OR parentid = '' THEN RETURN fullname; ELSE RETURN hp_true_filename(parentid) || '/' || fullname; END IF; 6
43.6.4.1.
IF parentid IS NULL OR parentid = '' THENRETURN fullname; ELSERETURN hp_true_filename(parentid) || '/' || fullname; END IF;
7
IF boolean-expression THEN_`statements`_ END IF;
IF parentid IS NULL OR parentid = '' THENRETURN fullname; ELSERETURN hp_true_filename(parentid) || '/' || fullname; END IF;
7 statements are the simplest form of
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8. The statements between
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
00 and
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
01 will be executed if the condition is true. Otherwise, they are skipped.
Example:
IF v_user_id <> 0 THENUPDATE users SET email = v_email WHERE user_id = v_user_id; END IF;
43.6.4.2.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
02
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
02 statements add to
IF parentid IS NULL OR parentid = '' THENRETURN fullname; ELSERETURN hp_true_filename(parentid) || '/' || fullname; END IF;
7 by letting you specify an alternative set of statements that should be executed if the condition is not true. (Note this includes the case where the condition evaluates to NULL.)
Examples:
IF parentid IS NULL OR parentid = '' THENRETURN fullname; ELSERETURN hp_true_filename(parentid) || '/' || fullname; END IF;
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
0
43.6.4.3.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
05
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
1
Sometimes there are more than just two alternatives.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
05 provides a convenient method of checking several alternatives in turn. The
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8 conditions are tested successively until the first one that is true is found. Then the associated statement(s) are executed, after which control passes to the next statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
01. (Any subsequent
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8 conditions are not tested.) If none of the
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8 conditions is true, then the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
11 block (if any) is executed.
Here is an example:
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
2
The key word
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
12 can also be spelled
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
13.
An alternative way of accomplishing the same task is to nest
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
02 statements, as in the following example:
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
3
However, this method requires writing a matching
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
01 for each
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
8, so it is much more cumbersome than using
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
12 when there are many alternatives.
43.6.4.4. Simple
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
4
The simple form of
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9 provides conditional execution based on equality of operands. The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
20 is evaluated (once) and successively compared to each
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
4 in the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
22 clauses. If a match is found, then the corresponding
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23 are executed, and then control passes to the next statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
24. (Subsequent
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
22 expressions are not evaluated.) If no match is found, the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
11
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23 are executed; but if
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
11 is not present, then a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
29 exception is raised.
Here is a simple example:
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
5
43.6.4.5. Searched
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
6
The searched form of
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9 provides conditional execution based on truth of Boolean expressions. Each
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
22 clause's
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
33 is evaluated in turn, until one is found that yields
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
34. Then the corresponding
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23 are executed, and then control passes to the next statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
24. (Subsequent
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
22 expressions are not evaluated.) If no true result is found, the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
11
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23 are executed; but if
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
11 is not present, then a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
29 exception is raised.
Here is an example:
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
7
This form of
IF boolean-expression THEN_`statements`_ ELSE_`statements`_ END IF;
9 is entirely equivalent to
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
05, except for the rule that reaching an omitted
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
11 clause results in an error rather than doing nothing.
43.6.5. Simple Loops
With the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
45,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
47,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
48,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49, and
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
50 statements, you can arrange for your PL/pgSQL function to repeat a series of commands.
43.6.5.1.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
45
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
8
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
45 defines an unconditional loop that is repeated indefinitely until terminated by an
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 or
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement. The optional
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55 can be used by
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 and
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
47 statements within nested loops to specify which loop those statements refer to.
43.6.5.2.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
9
If no
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55 is given, the innermost loop is terminated and the statement following
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
60 is executed next. If
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55 is given, it must be the label of the current or some outer level of nested loop or block. Then the named loop or block is terminated and control continues with the statement after the loop's/block's corresponding
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
62.
If
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
22 is specified, the loop exit occurs only if
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
33 is true. Otherwise, control passes to the statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 can be used with all types of loops; it is not limited to use with unconditional loops.
When used with a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
67 block,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 passes control to the next statement after the end of the block. Note that a label must be used for this purpose; an unlabeled
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 is never considered to match a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
67 block. (This is a change from pre-8.4 releases of PostgreSQL, which would allow an unlabeled
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 to match a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
67 block.)
Examples:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
0
43.6.5.3.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
47
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
1
If no
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55 is given, the next iteration of the innermost loop is begun. That is, all statements remaining in the loop body are skipped, and control returns to the loop control expression (if any) to determine whether another loop iteration is needed. If
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55 is present, it specifies the label of the loop whose execution will be continued.
If
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
22 is specified, the next iteration of the loop is begun only if
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
33 is true. Otherwise, control passes to the statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
47.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
47 can be used with all types of loops; it is not limited to use with unconditional loops.
Examples:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
2
43.6.5.4.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
48
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
3
The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
48 statement repeats a sequence of statements so long as the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
33 evaluates to true. The expression is checked just before each entry to the loop body.
For example:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
4
43.6.5.5.
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 (Integer Variant)
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
5
This form of
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 creates a loop that iterates over a range of integer values. The variable
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
85 is automatically defined as type
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
86 and exists only inside the loop (any existing definition of the variable name is ignored within the loop). The two expressions giving the lower and upper bound of the range are evaluated once when entering the loop. If the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
87 clause isn't specified the iteration step is 1, otherwise it's the value specified in the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
87 clause, which again is evaluated once on loop entry. If
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
89 is specified then the step value is subtracted, rather than added, after each iteration.
Some examples of integer
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 loops:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
6
If the lower bound is greater than the upper bound (or less than, in the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
89 case), the loop body is not executed at all. No error is raised.
If a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55 is attached to the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 loop then the integer loop variable can be referenced with a qualified name, using that
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
55.
43.6.6. Looping through Query Results
Using a different type of
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 loop, you can iterate through the results of a query and manipulate that data accordingly. The syntax is:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
7
The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
96 is a record variable, row variable, or comma-separated list of scalar variables. The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
96 is successively assigned each row resulting from the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
98 and the loop body is executed for each row. Here is an example:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
8
If the loop is terminated by an
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
46 statement, the last assigned row value is still accessible after the loop.
The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
98 used in this type of
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 statement can be any SQL command that returns rows to the caller:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
02 is the most common case, but you can also use
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03,
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
04, or
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
05 with a
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
06 clause. Some utility commands such as
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
07 will work too.
PL/pgSQL variables are replaced by query parameters, and the query plan is cached for possible re-use, as discussed in detail in and .
The
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
08 statement is another way to iterate over rows:
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
9
This is like the previous form, except that the source query is specified as a string expression, which is evaluated and replanned on each entry to the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 loop. This allows the programmer to choose the speed of a preplanned query or the flexibility of a dynamic query, just as with a plain
IF v_user_id <> 0 THENUPDATE users SET email = v_email WHERE user_id = v_user_id; END IF;
0 statement. As with
IF v_user_id <> 0 THENUPDATE users SET email = v_email WHERE user_id = v_user_id; END IF;
0, parameter values can be inserted into the dynamic command via
IF boolean-expression THEN_`statements`_ END IF;
9.
Another way to specify the query whose results should be iterated through is to declare it as a cursor. This is described in .
43.6.7. Looping through Arrays
The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
50 loop is much like a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
49 loop, but instead of iterating through the rows returned by an SQL query, it iterates through the elements of an array value. (In general,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
50 is meant for looping through components of a composite-valued expression; variants for looping through composites besides arrays may be added in future.) The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
50 statement to loop over an array is:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
0
Without
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
17, or if
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
18 is specified, the loop iterates through individual elements of the array produced by evaluating the
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
4. The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
96 variable is assigned each element value in sequence, and the loop body is executed for each element. Here is an example of looping through the elements of an integer array:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
1
The elements are visited in storage order, regardless of the number of array dimensions. Although the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
96 is usually just a single variable, it can be a list of variables when looping through an array of composite values (records). In that case, for each array element, the variables are assigned from successive columns of the composite value.
With a positive
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
17 value,
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
50 iterates through slices of the array rather than single elements. The
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
17 value must be an integer constant not larger than the number of dimensions of the array. The
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
96 variable must be an array, and it receives successive slices of the array value, where each slice is of the number of dimensions specified by
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
17. Here is an example of iterating through one-dimensional slices:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
2
43.6.8. Trapping Errors
By default, any error occurring in a PL/pgSQL function aborts execution of the function and the surrounding transaction. You can trap errors and recover from them by using a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
67 block with an
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause. The syntax is an extension of the normal syntax for a
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
67 block:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
3
If no error occurs, this form of block simply executes all the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23, and then control passes to the next statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
62. But if an error occurs within the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23, further processing of the
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
23 is abandoned, and control passes to the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 list. The list is searched for the first
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
35 matching the error that occurred. If a match is found, the corresponding
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
36 are executed, and then control passes to the next statement after
functions returning a scalar type RETURN 1 + 2; RETURN scalar_var; functions returning a composite type RETURN composite_type_var; RETURN (1, 2, 'three'::text); -- must cast columns to correct types
62. If no match is found, the error propagates out as though the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause were not there at all: the error can be caught by an enclosing block with
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28, or if there is none it aborts processing of the function.
The
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
35 names can be any of those shown in Appendix A. A category name matches any error within its category. The special condition name
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
41 matches every error type except
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
42 and
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
43. (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
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
44 code; for example these are equivalent:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
4
If a new error occurs within the selected
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
36, it cannot be caught by this
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause, but is propagated out. A surrounding
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause could catch it.
When an error is caught by an
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause, the local variables of the PL/pgSQL function remain as they were when the error occurred, but all changes to persistent database state within the block are rolled back. As an example, consider this fragment:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
5
When control reaches the assignment to
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
49, it will fail with a
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
50 error. This will be caught by the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause. The value returned in the
CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS $BODY$ BEGINRETURN QUERY SELECT flightid FROM flight WHERE flightdate >= $1 AND flightdate < ($1 + 1); -- Since execution is not finished, we can check whether rows were returned -- and raise exception if not. IF NOT FOUND THEN RAISE EXCEPTION 'No flight at %.', $1; END IF; RETURN; END; $BODY$ LANGUAGE plpgsql; Returns available flights or raises exception if there are no available flights. SELECT * FROM get_available_flightid(CURRENT_DATE);
0 statement will be the incremented value of
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
53, but the effects of the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
04 command will have been rolled back. The
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03 command preceding the block is not rolled back, however, so the end result is that the database contains
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
56 not
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
57.
Tip
A block containing an
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 clause is significantly more expensive to enter and exit than a block without one. Therefore, don't use
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
28 without need.
Example 43.2. Exceptions with
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
04/
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03
This example uses exception handling to perform either
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
04 or
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03, as appropriate. It is recommended that applications use
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03 with
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
65 rather than actually using this pattern. This example serves primarily to illustrate use of PL/pgSQL control flow structures:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
6
This coding assumes the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
66 error is caused by the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03, and not by, say, an
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
03 in a trigger function on the table. It might also misbehave if there is more than one unique index on the table, since it will retry the operation regardless of which index caused the error. More safety could be had by using the features discussed next to check that the trapped error was the one expected.
43.6.8.1. Obtaining Information about an Error
Exception handlers frequently need to identify the specific error that occurred. There are two ways to get information about the current exception in PL/pgSQL: special variables and the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
69 command.
Within an exception handler, the special variable
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
44 contains the error code that corresponds to the exception that was raised (refer to for a list of possible error codes). The special variable
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
71 contains the error message associated with the exception. These variables are undefined outside exception handlers.
Within an exception handler, one may also retrieve information about the current exception by using the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
69 command, which has the form:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
7
Each
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
73 is a key word identifying a status value to be assigned to the specified
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
74 (which should be of the right data type to receive it). The currently available status items are shown in .
Table 43.2. Error Diagnostics Items
Name Type Description
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
75
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the SQLSTATE error code of the exception
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
77
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the name of the column related to exception
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
79
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the name of the constraint related to exception
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
81
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the name of the data type related to exception
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
83
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the text of the exception's primary message
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
85
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the name of the table related to exception
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
87
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the name of the schema related to exception
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
89
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the text of the exception's detail message, if any
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
91
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 the text of the exception's hint message, if any
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
93
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
76 line(s) of text describing the call stack at the time of the exception (see )
If the exception did not set a value for an item, an empty string will be returned.
Here is an example:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
8
43.6.9. Obtaining Execution Location Information
The
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
95 command, previously described in , retrieves information about current execution state (whereas the
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
69 command discussed above reports information about the execution state as of a previous error). Its
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
97 status item is useful for identifying the current execution location.
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
97 returns a text string with line(s) of text describing the call stack. The first line refers to the current function and currently executing
RETURN NEXT expression; RETURN QUERY query; RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];
95 command. The second and any subsequent lines refer to calling functions further up the call stack. For example:
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
9
CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); INSERT INTO foo VALUES (1, 2, 'three'); INSERT INTO foo VALUES (4, 5, 'six'); CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS $BODY$ DECLAREr foo%rowtype; BEGINFOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP -- can do some processing here RETURN NEXT r; -- return current row of SELECT END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql; SELECT * FROM get_all_foo();
00 returns the same sort of stack trace, but describing the location at which an error was detected, rather than the current location.