Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
41.6. Control Structures
Prev UpChapter 41. PL/pgSQLSQL Procedural LanguageHome Next

41.6. Control Structures#

Control structures are probably the most useful (and important) part ofPL/pgSQL. WithPL/pgSQL's control structures, you can manipulatePostgreSQL data in a very flexible and powerful way.

41.6.1. Returning from a Function#

There are two commands available that allow you to return data from a function:RETURN andRETURN NEXT.

41.6.1.1. RETURN#

RETURNexpression;

RETURN with an expression terminates the function and returns the value ofexpression to the caller. This form is used forPL/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 justRETURN with no expression. The current values of the output parameter variables will be returned.

If you declared the function to returnvoid, aRETURN statement can be used to exit the function early; but do not write an expression followingRETURN.

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 aRETURN statement, a run-time error will occur. This restriction does not apply to functions with output parameters and functions returningvoid, however. In those cases aRETURN statement is automatically executed if the top-level block finishes.

Some examples:

-- functions returning a scalar typeRETURN 1 + 2;RETURN scalar_var;-- functions returning a composite typeRETURN composite_type_var;RETURN (1, 2, 'three'::text);  -- must cast columns to correct types

41.6.1.2. RETURN NEXT andRETURN QUERY#

RETURN NEXTexpression;RETURN QUERYquery;RETURN QUERY EXECUTEcommand-string [ USINGexpression [, ...]];

When aPL/pgSQL function is declared to returnSETOFsometype, the procedure to follow is slightly different. In that case, the individual items to return are specified by a sequence ofRETURN NEXT orRETURN QUERY commands, and then a finalRETURN command with no argument is used to indicate that the function has finished executing.RETURN NEXT can be used with both scalar and composite data types; with a composite result type, an entiretable of results will be returned.RETURN QUERY appends the results of executing a query to the function's result set.RETURN NEXT andRETURN QUERY can be freely intermixed in a single set-returning function, in which case their results will be concatenated.

RETURN NEXT andRETURN QUERY 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 thePL/pgSQL function. As successiveRETURN NEXT orRETURN QUERY commands are executed, the result set is built up. A finalRETURN, which should have no argument, causes control to exit the function (or you can just let control reach the end of the function).

RETURN QUERY has a variantRETURN QUERY EXECUTE, which specifies the query to be executed dynamically. Parameter expressions can be inserted into the computed query string viaUSING, in just the same way as in theEXECUTE command.

If you declared the function with output parameters, write justRETURN NEXT 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 returningSETOF record when there are multiple output parameters, orSETOFsometype when there is just one output parameter of typesometype, in order to create a set-returning function with output parameters.

Here is an example of a function usingRETURN NEXT:

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$DECLARE    r foo%rowtype;BEGIN    FOR 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 usingRETURN QUERY:

CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS$BODY$BEGIN    RETURN 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 ofRETURN NEXT andRETURN QUERY stores the entire result set before returning from the function, as discussed above. That means that if aPL/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 ofPL/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 thework_mem configuration variable. Administrators who have sufficient memory to store larger result sets in memory should consider increasing this parameter.

41.6.2. Returning from a Procedure#

A procedure does not have a return value. A procedure can therefore end without aRETURN statement. If you wish to use aRETURN statement to exit the code early, write justRETURN with no expression.

If the procedure has output parameters, the final values of the output parameter variables will be returned to the caller.

41.6.3. Calling a Procedure#

APL/pgSQL function, procedure, orDO block can call a procedure usingCALL. Output parameters are handled differently from the way thatCALL works in plain SQL. EachOUT orINOUT parameter of the procedure must correspond to a variable in theCALL statement, and whatever the procedure returns is assigned back to that variable after it returns. For example:

CREATE PROCEDURE triple(INOUT x int)LANGUAGE plpgsqlAS $$BEGIN    x := x * 3;END;$$;DO $$DECLARE myvar int := 5;BEGIN  CALL triple(myvar);  RAISE NOTICE 'myvar = %', myvar;  -- prints 15END;$$;

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.

41.6.4. Conditionals#

IF andCASE statements let you execute alternative commands based on certain conditions.PL/pgSQL has three forms ofIF:

  • IF ... THEN ... END IF

  • IF ... THEN ... ELSE ... END IF

  • IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF

and two forms ofCASE:

  • CASE ... WHEN ... THEN ... ELSE ... END CASE

  • CASE WHEN ... THEN ... ELSE ... END CASE

41.6.4.1. IF-THEN#

IFboolean-expression THENstatementsEND IF;

IF-THEN statements are the simplest form ofIF. The statements betweenTHEN andEND IF will be executed if the condition is true. Otherwise, they are skipped.

Example:

IF v_user_id <> 0 THEN    UPDATE users SET email = v_email WHERE user_id = v_user_id;END IF;

41.6.4.2. IF-THEN-ELSE#

IFboolean-expression THENstatementsELSEstatementsEND IF;

IF-THEN-ELSE statements add toIF-THEN 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 = ''THEN    RETURN fullname;ELSE    RETURN hp_true_filename(parentid) || '/' || fullname;END IF;

IF v_count > 0 THEN    INSERT INTO users_count (count) VALUES (v_count);    RETURN 't';ELSE    RETURN 'f';END IF;

41.6.4.3. IF-THEN-ELSIF#

IFboolean-expression THENstatements[ ELSIFboolean-expression THENstatements[ ELSIFboolean-expression THENstatements    ...]][ ELSEstatements]END IF;

Sometimes there are more than just two alternatives.IF-THEN-ELSIF provides a convenient method of checking several alternatives in turn. TheIF 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 afterEND IF. (Any subsequentIF conditions arenot tested.) If none of theIF conditions is true, then theELSE block (if any) is executed.

Here is an example:

IF number = 0 THEN    result := 'zero';ELSIF number > 0 THEN    result := 'positive';ELSIF number < 0 THEN    result := 'negative';ELSE    -- hmm, the only other possibility is that number is null    result := 'NULL';END IF;

The key wordELSIF can also be spelledELSEIF.

An alternative way of accomplishing the same task is to nestIF-THEN-ELSE statements, as in the following example:

IF demo_row.sex = 'm' THEN    pretty_sex := 'man';ELSE    IF demo_row.sex = 'f' THEN        pretty_sex := 'woman';    END IF;END IF;

However, this method requires writing a matchingEND IF for eachIF, so it is much more cumbersome than usingELSIF when there are many alternatives.

41.6.4.4. SimpleCASE#

CASEsearch-expression    WHENexpression [,expression [ ...]] THENstatements  [ WHENexpression [,expression [ ...]] THENstatements    ...]  [ ELSEstatements]END CASE;

The simple form ofCASE provides conditional execution based on equality of operands. Thesearch-expression is evaluated (once) and successively compared to eachexpression in theWHEN clauses. If a match is found, then the correspondingstatements are executed, and then control passes to the next statement afterEND CASE. (SubsequentWHEN expressions are not evaluated.) If no match is found, theELSEstatements are executed; but ifELSE is not present, then aCASE_NOT_FOUND exception is raised.

Here is a simple example:

CASE x    WHEN 1, 2 THEN        msg := 'one or two';    ELSE        msg := 'other value than one or two';END CASE;

41.6.4.5. SearchedCASE#

CASE    WHENboolean-expression THENstatements  [ WHENboolean-expression THENstatements    ...]  [ ELSEstatements]END CASE;

The searched form ofCASE provides conditional execution based on truth of Boolean expressions. EachWHEN clause'sboolean-expression is evaluated in turn, until one is found that yieldstrue. Then the correspondingstatements are executed, and then control passes to the next statement afterEND CASE. (SubsequentWHEN expressions are not evaluated.) If no true result is found, theELSEstatements are executed; but ifELSE is not present, then aCASE_NOT_FOUND exception is raised.

Here is an example:

CASE    WHEN x BETWEEN 0 AND 10 THEN        msg := 'value is between zero and ten';    WHEN x BETWEEN 11 AND 20 THEN        msg := 'value is between eleven and twenty';END CASE;

This form ofCASE is entirely equivalent toIF-THEN-ELSIF, except for the rule that reaching an omittedELSE clause results in an error rather than doing nothing.

41.6.5. Simple Loops#

With theLOOP,EXIT,CONTINUE,WHILE,FOR, andFOREACH statements, you can arrange for yourPL/pgSQL function to repeat a series of commands.

41.6.5.1. LOOP#

[ <<label>>]LOOPstatementsEND LOOP [label];

LOOP defines an unconditional loop that is repeated indefinitely until terminated by anEXIT orRETURN statement. The optionallabel can be used byEXIT andCONTINUE statements within nested loops to specify which loop those statements refer to.

41.6.5.2. EXIT#

EXIT [label] [ WHENboolean-expression];

If nolabel is given, the innermost loop is terminated and the statement followingEND LOOP is executed next. Iflabel 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 correspondingEND.

IfWHEN is specified, the loop exit occurs only ifboolean-expression is true. Otherwise, control passes to the statement afterEXIT.

EXIT can be used with all types of loops; it is not limited to use with unconditional loops.

When used with aBEGIN block,EXIT passes control to the next statement after the end of the block. Note that a label must be used for this purpose; an unlabeledEXIT is never considered to match aBEGIN block. (This is a change from pre-8.4 releases ofPostgreSQL, which would allow an unlabeledEXIT to match aBEGIN block.)

Examples:

LOOP    -- some computations    IF count > 0 THEN        EXIT;  -- exit loop    END IF;END LOOP;LOOP    -- some computations    EXIT WHEN count > 0;  -- same result as previous exampleEND LOOP;<<ablock>>BEGIN    -- some computations    IF stocks > 100000 THEN        EXIT ablock;  -- causes exit from the BEGIN block    END IF;    -- computations here will be skipped when stocks > 100000END;

41.6.5.3. CONTINUE#

CONTINUE [label] [ WHENboolean-expression];

If nolabel 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. Iflabel is present, it specifies the label of the loop whose execution will be continued.

IfWHEN is specified, the next iteration of the loop is begun only ifboolean-expression is true. Otherwise, control passes to the statement afterCONTINUE.

CONTINUE can be used with all types of loops; it is not limited to use with unconditional loops.

Examples:

LOOP    -- some computations    EXIT WHEN count > 100;    CONTINUE WHEN count < 50;    -- some computations for count IN [50 .. 100]END LOOP;

41.6.5.4. WHILE#

[ <<label>>]WHILEboolean-expression LOOPstatementsEND LOOP [label];

TheWHILE statement repeats a sequence of statements so long as theboolean-expression evaluates to true. The expression is checked just before each entry to the loop body.

For example:

WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP    -- some computations hereEND LOOP;WHILE NOT done LOOP    -- some computations hereEND LOOP;

41.6.5.5. FOR (Integer Variant)#

[ <<label>>]FORname IN [ REVERSE]expression ..expression [ BYexpression] LOOPstatementsEND LOOP [label];

This form ofFOR creates a loop that iterates over a range of integer values. The variablename is automatically defined as typeinteger 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 theBY clause isn't specified the iteration step is 1, otherwise it's the value specified in theBY clause, which again is evaluated once on loop entry. IfREVERSE is specified then the step value is subtracted, rather than added, after each iteration.

Some examples of integerFOR loops:

FOR i IN 1..10 LOOP    -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loopEND LOOP;FOR i IN REVERSE 10..1 LOOP    -- i will take on the values 10,9,8,7,6,5,4,3,2,1 within the loopEND LOOP;FOR i IN REVERSE 10..1 BY 2 LOOP    -- i will take on the values 10,8,6,4,2 within the loopEND LOOP;

If the lower bound is greater than the upper bound (or less than, in theREVERSE case), the loop body is not executed at all. No error is raised.

If alabel is attached to theFOR loop then the integer loop variable can be referenced with a qualified name, using thatlabel.

41.6.6. Looping through Query Results#

Using a different type ofFOR loop, you can iterate through the results of a query and manipulate that data accordingly. The syntax is:

[ <<label>>]FORtarget INquery LOOPstatementsEND LOOP [label];

Thetarget is a record variable, row variable, or comma-separated list of scalar variables. Thetarget is successively assigned each row resulting from thequery and the loop body is executed for each row. Here is an example:

CREATE FUNCTION refresh_mviews() RETURNS integer AS $$DECLARE    mviews RECORD;BEGIN    RAISE NOTICE 'Refreshing all materialized views...';    FOR mviews IN       SELECT n.nspname AS mv_schema,              c.relname AS mv_name,              pg_catalog.pg_get_userbyid(c.relowner) AS owner         FROM pg_catalog.pg_class c    LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)        WHERE c.relkind = 'm'     ORDER BY 1    LOOP        -- Now "mviews" has one record with information about the materialized view        RAISE NOTICE 'Refreshing materialized view %.% (owner: %)...',                     quote_ident(mviews.mv_schema),                     quote_ident(mviews.mv_name),                     quote_ident(mviews.owner);        EXECUTE format('REFRESH MATERIALIZED VIEW %I.%I', mviews.mv_schema, mviews.mv_name);    END LOOP;    RAISE NOTICE 'Done refreshing materialized views.';    RETURN 1;END;$$ LANGUAGE plpgsql;

If the loop is terminated by anEXIT statement, the last assigned row value is still accessible after the loop.

Thequery used in this type ofFOR statement can be any SQL command that returns rows to the caller:SELECT is the most common case, but you can also useINSERT,UPDATE,DELETE, orMERGE with aRETURNING clause. Some utility commands such asEXPLAIN 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 inSection 41.11.1 andSection 41.11.2.

TheFOR-IN-EXECUTE statement is another way to iterate over rows:

[ <<label>>]FORtarget IN EXECUTEtext_expression [ USINGexpression [, ...]] LOOPstatementsEND LOOP [label];

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 theFOR loop. This allows the programmer to choose the speed of a preplanned query or the flexibility of a dynamic query, just as with a plainEXECUTE statement. As withEXECUTE, parameter values can be inserted into the dynamic command viaUSING.

Another way to specify the query whose results should be iterated through is to declare it as a cursor. This is described inSection 41.7.4.

41.6.7. Looping through Arrays#

TheFOREACH loop is much like aFOR loop, but instead of iterating through the rows returned by an SQL query, it iterates through the elements of an array value. (In general,FOREACH is meant for looping through components of a composite-valued expression; variants for looping through composites besides arrays may be added in future.) TheFOREACH statement to loop over an array is:

[ <<label>>]FOREACHtarget [ SLICEnumber] IN ARRAYexpression LOOPstatementsEND LOOP [label];

WithoutSLICE, or ifSLICE 0 is specified, the loop iterates through individual elements of the array produced by evaluating theexpression. Thetarget 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 FUNCTION sum(int[]) RETURNS int8 AS $$DECLARE  s int8 := 0;  x int;BEGIN  FOREACH x IN ARRAY $1  LOOP    s := s + x;  END LOOP;  RETURN s;END;$$ LANGUAGE plpgsql;

The elements are visited in storage order, regardless of the number of array dimensions. Although thetarget 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 positiveSLICE value,FOREACH iterates through slices of the array rather than single elements. TheSLICE value must be an integer constant not larger than the number of dimensions of the array. Thetarget variable must be an array, and it receives successive slices of the array value, where each slice is of the number of dimensions specified bySLICE. Here is an example of iterating through one-dimensional slices:

CREATE FUNCTION scan_rows(int[]) RETURNS void AS $$DECLARE  x int[];BEGIN  FOREACH x SLICE 1 IN ARRAY $1  LOOP    RAISE NOTICE 'row = %', x;  END LOOP;END;$$ LANGUAGE plpgsql;SELECT scan_rows(ARRAY[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]);NOTICE:  row = {1,2,3}NOTICE:  row = {4,5,6}NOTICE:  row = {7,8,9}NOTICE:  row = {10,11,12}

41.6.8. Trapping Errors#

By default, any error occurring in aPL/pgSQL function aborts execution of the function and the surrounding transaction. You can trap errors and recover from them by using aBEGIN block with anEXCEPTION clause. The syntax is an extension of the normal syntax for aBEGIN block:

[ <<label>>][ DECLAREdeclarations]BEGINstatementsEXCEPTION    WHENcondition [ ORcondition ...] THENhandler_statements    [ WHENcondition [ ORcondition ...] THENhandler_statements      ...]END;

If no error occurs, this form of block simply executes all thestatements, and then control passes to the next statement afterEND. But if an error occurs within thestatements, further processing of thestatements is abandoned, and control passes to theEXCEPTION list. The list is searched for the firstcondition matching the error that occurred. If a match is found, the correspondinghandler_statements are executed, and then control passes to the next statement afterEND. If no match is found, the error propagates out as though theEXCEPTION clause were not there at all: the error can be caught by an enclosing block withEXCEPTION, or if there is none it aborts processing of the function.

Thecondition names can be any of those shown inAppendix A. A category name matches any error within its category. The special condition nameOTHERS matches every error type exceptQUERY_CANCELED andASSERT_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 bySQLSTATE code; for example these are equivalent:

WHEN division_by_zero THEN ...WHEN SQLSTATE '22012' THEN ...

If a new error occurs within the selectedhandler_statements, it cannot be caught by thisEXCEPTION clause, but is propagated out. A surroundingEXCEPTION clause could catch it.

When an error is caught by anEXCEPTION clause, the local variables of thePL/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:

INSERT INTO mytab(firstname, lastname) VALUES('Tom', 'Jones');BEGIN    UPDATE mytab SET firstname = 'Joe' WHERE lastname = 'Jones';    x := x + 1;    y := x / 0;EXCEPTION    WHEN division_by_zero THEN        RAISE NOTICE 'caught division_by_zero';        RETURN x;END;

When control reaches the assignment toy, it will fail with adivision_by_zero error. This will be caught by theEXCEPTION clause. The value returned in theRETURN statement will be the incremented value ofx, but the effects of theUPDATE command will have been rolled back. TheINSERT command preceding the block is not rolled back, however, so the end result is that the database containsTom Jones notJoe Jones.

Tip

A block containing anEXCEPTION clause is significantly more expensive to enter and exit than a block without one. Therefore, don't useEXCEPTION without need.

Example 41.2. Exceptions withUPDATE/INSERT

This example uses exception handling to perform eitherUPDATE orINSERT, as appropriate. It is recommended that applications useINSERT withON CONFLICT DO UPDATE rather than actually using this pattern. This example serves primarily to illustrate use ofPL/pgSQL control flow structures:

CREATE TABLE db (a INT PRIMARY KEY, b TEXT);CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS$$BEGIN    LOOP        -- first try to update the key        UPDATE db SET b = data WHERE a = key;        IF found THEN            RETURN;        END IF;        -- not there, so try to insert the key        -- if someone else inserts the same key concurrently,        -- we could get a unique-key failure        BEGIN            INSERT INTO db(a,b) VALUES (key, data);            RETURN;        EXCEPTION WHEN unique_violation THEN            -- Do nothing, and loop to try the UPDATE again.        END;    END LOOP;END;$$LANGUAGE plpgsql;SELECT merge_db(1, 'david');SELECT merge_db(1, 'dennis');

This coding assumes theunique_violation error is caused by theINSERT, and not by, say, anINSERT 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.


41.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 inPL/pgSQL: special variables and theGET STACKED DIAGNOSTICS command.

Within an exception handler, the special variableSQLSTATE contains the error code that corresponds to the exception that was raised (refer toTable A.1 for a list of possible error codes). The special variableSQLERRM 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 theGET STACKED DIAGNOSTICS command, which has the form:

GET STACKED DIAGNOSTICSvariable { = | := }item [ , ...];

Eachitem is a key word identifying a status value to be assigned to the specifiedvariable (which should be of the right data type to receive it). The currently available status items are shown inTable 41.2.

Table 41.2. Error Diagnostics Items

NameTypeDescription
RETURNED_SQLSTATEtextthe SQLSTATE error code of the exception
COLUMN_NAMEtextthe name of the column related to exception
CONSTRAINT_NAMEtextthe name of the constraint related to exception
PG_DATATYPE_NAMEtextthe name of the data type related to exception
MESSAGE_TEXTtextthe text of the exception's primary message
TABLE_NAMEtextthe name of the table related to exception
SCHEMA_NAMEtextthe name of the schema related to exception
PG_EXCEPTION_DETAILtextthe text of the exception's detail message, if any
PG_EXCEPTION_HINTtextthe text of the exception's hint message, if any
PG_EXCEPTION_CONTEXTtextline(s) of text describing the call stack at the time of the exception (seeSection 41.6.9)

If the exception did not set a value for an item, an empty string will be returned.

Here is an example:

DECLARE  text_var1 text;  text_var2 text;  text_var3 text;BEGIN  -- some processing which might cause an exception  ...EXCEPTION WHEN OTHERS THEN  GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,                          text_var2 = PG_EXCEPTION_DETAIL,                          text_var3 = PG_EXCEPTION_HINT;END;

41.6.9. Obtaining Execution Location Information#

TheGET DIAGNOSTICS command, previously described inSection 41.5.5, retrieves information about current execution state (whereas theGET STACKED DIAGNOSTICS command discussed above reports information about the execution state as of a previous error). ItsPG_CONTEXT status item is useful for identifying the current execution location.PG_CONTEXT returns a text string with line(s) of text describing the call stack. The first line refers to the current function and currently executingGET DIAGNOSTICS command. The second and any subsequent lines refer to calling functions further up the call stack. For example:

CREATE OR REPLACE FUNCTION outer_func() RETURNS integer AS $$BEGIN  RETURN inner_func();END;$$ LANGUAGE plpgsql;CREATE OR REPLACE FUNCTION inner_func() RETURNS integer AS $$DECLARE  stack text;BEGIN  GET DIAGNOSTICS stack = PG_CONTEXT;  RAISE NOTICE E'--- Call Stack ---\n%', stack;  RETURN 1;END;$$ LANGUAGE plpgsql;SELECT outer_func();NOTICE:  --- Call Stack ---PL/pgSQL function inner_func() line 5 at GET DIAGNOSTICSPL/pgSQL function outer_func() line 3 at RETURNCONTEXT:  PL/pgSQL function outer_func() line 3 at RETURN outer_func ------------           1(1 row)

GET STACKED DIAGNOSTICS ... PG_EXCEPTION_CONTEXT returns the same sort of stack trace, but describing the location at which an error was detected, rather than the current location.


Prev Up Next
41.5. Basic Statements Home 41.7. Cursors
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp