postgresql_16:6.6.5.5 Obtaining the Result Status
PostgreSQL 16 > Server Programming > PL/pgSQL — SQL Procedural Language > Basic Statements > Obtaining the Result Status
There are several ways to determine the effect of a command. The first method is to use the GET DIAGNOSTICS command, which has the form: GET CURRENT DIAGNOSTICS *variable* { = | := } *item* , ... ; This command allows retrieval of system status indicators. CURRENT is a noise word (but see also GET STACKED DIAGNOSTICS in (see postgresql_16:6.6.6.8)). Each *item* is a key word identifying a status value to be assigned to the specified *variable* (which should be of the right data type to receive it). The currently available status items are shown in (see plpgsql-current-diagnostics-values). Colon-equal (:=) can be used instead of the SQL-standard = token. An example: GET DIAGNOSTICS integer_var = ROW_COUNT;
| Name | Type | Description | | --- | --- | --- | | ROW_COUNT | bigint | the number of rows processed by the most recent SQL command | | PG_CONTEXT | text | line(s) of text describing the current call stack (see (see postgresql_16:6.6.6.9)) | | PG_ROUTINE_OID | oid | OID of the current function |
The second method to determine the effects of a command is to check the special variable named FOUND, which is of type boolean. FOUND starts out false within each PL/pgSQL function call. It is set by each of the following types of statements: A SELECT INTO statement sets FOUND true if a row is assigned, false if no row is returned. A PERFORM statement sets FOUND true if it produces (and discards) one or more rows, false if no row is produced. UPDATE, INSERT, DELETE, and MERGE statements set FOUND true if at least one row is affected, false if no row is affected. A FETCH statement sets FOUND true if it returns a row, false if no row is returned. A MOVE statement sets FOUND true if it successfully repositions the cursor, false otherwise. A FOR or FOREACH statement sets FOUND true if it iterates one or more times, else false. FOUND is set this way when the loop exits; inside the execution of the loop, FOUND is not modified by the loop statement, although it might be changed by the execution of other statements within the loop body. RETURN QUERY and RETURN QUERY EXECUTE statements set FOUND true if the query returns at least one row, false if no row is returned. Other PL/pgSQL statements do not change the state of FOUND. Note in particular that EXECUTE changes the output of GET DIAGNOSTICS, but does not change FOUND.
FOUND is a local variable within each PL/pgSQL function; any changes to it affect only the current function.