# PostgreSQL plpgsql 存储过程、函数 - 状态、异常变量打印、异常捕获... - GET [STACKED] DIAGNOSTICS

+关注继续查看

### 标签

PostgreSQL , GET , STACKED , DIAGNOSTICS

## GET DIAGNOSTICS捕获运行过程中的状态值

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 [ , ... ];

GET DIAGNOSTICS integer_var = ROW_COUNT;

Name Type Description
ROW_COUNT bigint the number of rows processed by the most recent SQL command
RESULT_OID oid the OID of the last row inserted by the most recent SQL command (only useful after an INSERT command into a table having OIDs)
PG_CONTEXT text line(s) of text describing the current call stack (see Section 43.6.8)

The GET DIAGNOSTICS command, previously described in Section 43.5.5, retrieves information about current execution state (whereas the GET STACKED DIAGNOSTICS command discussed above reports information about the execution state as of a previous error).

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 DIAGNOSTICS
PL/pgSQL function outer_func() line 3 at RETURN
CONTEXT:  PL/pgSQL function outer_func() line 3 at RETURN
outer_func
------------
1
(1 row)


## GET STACKED DIAGNOSTICS捕获异常时的STACK内容

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.

Name Type Description
RETURNED_SQLSTATE text the SQLSTATE error code of the exception
COLUMN_NAME text the name of the column related to exception
CONSTRAINT_NAME text the name of the constraint related to exception
PG_DATATYPE_NAME text the name of the data type related to exception
MESSAGE_TEXT text the text of the exception's primary message
TABLE_NAME text the name of the table related to exception
SCHEMA_NAME text the name of the schema related to exception
PG_EXCEPTION_DETAIL text the text of the exception's detail message, if any
PG_EXCEPTION_HINT text the text of the exception's hint message, if any
PG_EXCEPTION_CONTEXT text line(s) of text describing the call stack at the time of the exception (see Section 43.6.8)

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;


## 参考

https://www.postgresql.org/docs/11/static/plpgsql-control-structures.html#PLPGSQL-CALL-STACK

https://www.postgresql.org/docs/11/static/plpgsql-statements.html

《Using "GET DIAGNOSTICS integer_var = ROW_COUNT;" capture rows effect by the last SQL》

RDS数据库与自建库的gtid主从同步

1805 0
RDS MySQL 5.7三节点企业版重磅发布 企业级业务云上数据库首选

0 0

265 0
【云栖号案例 | 物联网&人工智能】RDS为慧联无限数据库运维减负

1484 0
【云栖号案例 | 新零售】数据库RDS为跨境电商管理平台支撑亿级流水

1637 0
ecs与数据库（rds，redis，mongodb，memcached）连通性判断流程图

0 0
【阿里云新品发布·周刊】第11期：云数据库 MySQL 8.0 重磅发布，更适合企业使用场景的RDS数据库

432 0

1119 0
+关注

+ 订阅

Lambda Processing

PolarDB for PostgreSQL三节点功能介绍