在不同 PG 服务器中获取函数的不同输出
我已将数据库从 postgres 版本 9.6 迁移到 postgres 版本 13,因此我面临一个问题,即函数在两个版本中返回不同的输出。我正在用虚拟数据解释问题,因为我的实际函数和类型太大且复杂。
例如:
假设公共模式中有一个用户表,其中包含以下数据:
id | name | address_line1 | address_line2 |
---|---|---|---|
1 | Carlos | 4145 Francis Mine, Westwood | CA-96137 |
2 | Nathalie | 3852 Goldie Lane, Cincinnati | OH-45202 |
我的函数返回用户定义的类型。类型定义如下:
CREATE TYPE user AS
(
id bigint,
name character varying(255),
address_line1 character varying(255),
address_line2 character varying(255)
);
函数如下:
CREATE OR REPLACE FUNCTION get_user(user_id bigint, OUT retval public.user)
RETURNS public.user
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
BEGIN
SELECT
*
INTO
retval
FROM
public.users U
WHERE
U.id = user_id
;
END;
$BODY$;
现在,当我运行查询 SELECT get_user(5);
时,根据函数定义 retval 的值将为 SQL NULL (即 NULL>
),所以在 PG v9.6 中我得到的输出为“(,,,)”,它指示具有空值的列。但在 PG v13 中,我得到的输出为
。我希望 PG v13 中的函数输出类似于 PG v9.6。因此,当我进一步将函数 get_user 的结果传递到 row_to_json() 时,它会给出不同的输出。
PG v9.6 中 row_to_json(SELECT get_user(5));
的输出如下:
{
"id" : null,
"name" : null,
"address_line1" : null,
"address_line2" : null
}
但在 PG v13 中,row_to_json(SELECT get_user(5));
输出 如下: code> 将是
。
I have migrated my database from postgres version 9.6 to postgres version 13, So I am facing a issue where a function returns different output in both the versions. I am explaining the problem with dummy data because my actual function and type is to much big and complicated.
For example:
Consider there is a users table in public schema, which contains following data:
id | name | address_line1 | address_line2 |
---|---|---|---|
1 | Carlos | 4145 Francis Mine, Westwood | CA-96137 |
2 | Nathalie | 3852 Goldie Lane, Cincinnati | OH-45202 |
My function returns a user defined type. Type definition is as below:
CREATE TYPE user AS
(
id bigint,
name character varying(255),
address_line1 character varying(255),
address_line2 character varying(255)
);
The function is as below:
CREATE OR REPLACE FUNCTION get_user(user_id bigint, OUT retval public.user)
RETURNS public.user
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
BEGIN
SELECT
*
INTO
retval
FROM
public.users U
WHERE
U.id = user_id
;
END;
$BODY$;
Now when I run the query SELECT get_user(5);
so according to function definition value of retval will be SQL NULL (i.e. <NULL>
), so in PG v9.6 I got output as "(,,,)" which indicates column with null values. But in PG v13 I got output as <NULL>
. I want output of my function in PG v13 similar to the PG v9.6. Because of this when I further pass result of my function get_user into row_to_json()
it gives different output.
Output of row_to_json(SELECT get_user(5));
in PG v9.6 will be as below:
{
"id" : null,
"name" : null,
"address_line1" : null,
"address_line2" : null
}
But in case of PG v13 output of row_to_json(SELECT get_user(5));
will be <NULL>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论