current_query()

一个返回当前查询文本的函数

current_query() 是一个系统函数,返回当前执行的查询文本。

current_query() 是在 PostgreSQL 8.4 中添加的。

用法

current_query () → text

current_query() 旨在用于函数和存储过程,以检查调用查询。

变更历史

示例

current_query() 的基本用法示例

postgres=# SELECT current_query();
      current_query      
-------------------------
 SELECT current_query();
(1 row)

如上所述,current_query() 本身几乎没有实际用途。但它在函数或过程(例如触发器函数)的上下文中很有用,可以检查调用该函数的查询。

postgres=# CREATE OR REPLACE FUNCTION query_to_text()
             RETURNS TEXT
             LANGUAGE 'plpgsql'
           AS $$
           DECLARE
             calling_query TEXT;
           BEGIN
             SELECT current_query() INTO calling_query;
           
             RAISE NOTICE 'The calling query is: "%"', calling_query;
           
             RETURN calling_query;
           END;
           $$;
CREATE FUNCTION

postgres=# SELECT query_to_text();
NOTICE:  The calling query is: "SELECT query_to_text();"
      query_to_text      
-------------------------
 SELECT query_to_text();
(1 row)

分类

系统函数

另请参阅

pg_stat_activity

反馈

请在此处提交有关“current_query()”的任何评论、建议或更正。这里