sql - 具有重复参数的 PostgreSQL 函数

标签 sql postgresql stored-procedures plpgsql function-parameter

我在 pg_catalog.pg_stat_get_activity 中偶然发现了一个奇怪的函数签名:

CREATE OR REPLACE FUNCTION pg_stat_get_activity(
    IN pid integer, 
    OUT datid oid,
    OUT pid integer, 
    -- more parameters...)
  RETURNS SETOF record AS 'pg_stat_get_activity'
  LANGUAGE internal STABLE
  COST 1
  ROWS 100;

这个函数声明了两次相同的参数名称,这也是从information_schema中报告的。

select 
  parameter_mode,
  parameter_name
from information_schema.parameters
where specific_schema = 'pg_catalog'
and specific_name like 'pg_stat_get_activity%'
order by ordinal_position

以上产量(另见 SQLFiddle ):

+--------------+----------------+
|parameter_mode|parameter_name  |
+--------------+----------------+
|IN            |pid             |
|OUT           |datid           |
|OUT           |pid             |
|...           |...             |
+--------------+----------------+

天真地,我尝试创建一个类似的函数,但无济于事:

CREATE FUNCTION f_2647(p1 IN int, p1 OUT int)
AS $$
BEGIN
    p1 := p1;
END;
$$ LANGUAGE plpgsql;

我的问题:

  1. 为什么内部 pg_stat_get_activity 函数会两次重新声明相同的参数名称?这样做的目的是什么?例如。为什么不直接使用 INOUT 参数?
  2. 内部 pg_stat_get_activity 函数和我的有什么不同?为什么我不能使用这种语法?

我知道这些是相当学术的问题,但我需要正确理解它才能修复 issuejOOQ代码生成器。

最佳答案

我注意到它出现在 9.2 中。在 9.1 版本中,out 字段被命名为 procpid:

 parameter_mode |  parameter_name  
----------------+------------------
 IN             | pid
 OUT            | datid
 OUT            | procpid
 OUT            | usesysid
 ...

在 postgres git 历史记录中寻找更改会导致此提交:

commit 4f42b546fd87a80be30c53a0f2c897acb826ad52
Author: Magnus Hagander 
Date:   Thu Jan 19 14:19:20 2012 +0100

    Separate state from query string in pg_stat_activity

    This separates the state (running/idle/idleintransaction etc) into
    it's own field ("state"), and leaves the query field containing just
    query text.

    The query text will now mean "current query" when a query is running
    and "last query" in other states. Accordingly,the field has been
    renamed from current_query to query.

    Since backwards compatibility was broken anyway to make that, the procpid
    field has also been renamed to pid - along with the same field in
    pg_stat_replication for consistency.

    Scott Mead and Magnus Hagander, review work from Greg Smith

在更改的行中,这里是感兴趣的行:

-DATA(insert OID = 2022 (  pg_stat_get_activity         PGNSP PGUID 12 1 100 0 0 f f f f t s 1 0 2249 "23" "{23,26,23,26,25,25,16,1184,1184,1184,869,25,23}" "{i,o,o,o,o,o,o,o,o,o,o,o,o}" "{pid,datid,procpid,usesysid,application_name,current_query,waiting,xact_start,query_start,backend_start,client_addr,client_hostname,client_port}" _null_ pg_stat_get_activity _null_ _null_ _null_ ));
+DATA(insert OID = 2022 (  pg_stat_get_activity         PGNSP PGUID 12 1 100 0 0 f f f f t s 1 0 2249 "23" "{23,26,23,26,25,25,25,16,1184,1184,1184,1184,869,25,23}" "{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o}" "{pid,datid,pid,usesysid,application_name,state,query,waiting,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port}" _null_ pg_stat_get_activity _null_ _null_ _null_ ));
 

在这种预先消化的形式中,作者可能没有注意到 pid 的双重使用,或者他们不在乎,因为它在实践中是无害的。

允许通过是因为这些内部函数是由 initdb 在跳过普通用户函数的创建检查的快速路径中创建的。

关于sql - 具有重复参数的 PostgreSQL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921510/

相关文章:

mysql - 更改 mysql 字段会引发 1064 错误?

sql - 使用 CTE 计算内部查询中的行数

python - 在 testing.postgresql 中,在 Docker 中找不到 initdb 命令

mysql - 重复条目 Mysql 主数据库

php - 使用 jQuery 将变量传递给 DB 并返回 codeigniter

sql - 在单个查询中对记录进行分层检索

sql - 检索前 12 条记录,同时在给定字段中保持相同的值

django - 通过 psycopg2 连接执行的查询数

mysql - 结果包含多行错误 1172 mysql

sql - 如何从 T-SQL 存储过程返回表