PLPGSQL函数返回错误:整数超出范围,即使值正确

发布于 2025-01-26 20:03:22 字数 731 浏览 4 评论 0原文

我在两个Postgres函数上遇到奇怪的行为,返回单个BigInt值。第一个返回正确的值时,第二个返回错误:

错误:整数超出范围

唯一的区别是,第一个返回NS中的恒定时间值,而第二个则根据恒定值计算出来,为方便起见,在S:中给出:

此操作正常:

CREATE OR REPLACE FUNCTION c_getTime1()
RETURNS bigint AS $$
BEGIN
    RETURN 12000000000; --12s in ns
END; $$
LANGUAGE plpgsql IMMUTABLE parallel safe;

这是一个错误。

CREATE OR REPLACE FUNCTION c_getTime2()
RETURNS bigint AS $$
BEGIN
    RETURN 12*1000*1000*1000; --12s in ns
END; $$
LANGUAGE plpgsql IMMUTABLE parallel safe;

只是想知道,有什么很大的区别吗?我应该以某种方式播放第二个功能的返回吗? 这两个函数均以直接方式调用:

select c_getTime1();
select c_getTime2();

PostgreSQL数据库版本为13.3。

事先感谢您提出的任何建议。

I'm experiencing strange behaviour on two Postgres functions returning a single bigint value. The first one returns the correct value while the second one throws an error:

ERROR: integer out of range

The only difference is that the first one returns a constant time value in ns, while the second one calculates it based on constant values, for convenience, given in s:

This one works fine:

CREATE OR REPLACE FUNCTION c_getTime1()
RETURNS bigint AS $
BEGIN
    RETURN 12000000000; --12s in ns
END; $
LANGUAGE plpgsql IMMUTABLE parallel safe;

This one throws an error.

CREATE OR REPLACE FUNCTION c_getTime2()
RETURNS bigint AS $
BEGIN
    RETURN 12*1000*1000*1000; --12s in ns
END; $
LANGUAGE plpgsql IMMUTABLE parallel safe;

Just wondering, is there any big difference? Should I somehow type-cast the second function's return?
Both functions are called using direct way:

select c_getTime1();
select c_getTime2();

The Postgresql database version is 13.3.

Thanks in advance for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2025-02-02 20:03:22

选择pg_typeof(100);
返回整数
返回12*1000*1000*1000;将首先计算为整数然后施放到bigint。
https://wwwww.postgresql.org.org.org.org.org.org/docs/current/current/current/datatate-numeric-imeric-imeric-.orec-.orec-numeric-ericeric.-numeric-. html
首先计算然后找到错误:22003:整数超出范围
因此,您应该更改为

返回12 :: bigint * 1000 :: bigint * 1000 :: bigint * 1000 :: bigint;

select pg_typeof(100);
return integer.
RETURN 12*1000*1000*1000; will first compute as integer then cast to bigint.
https://www.postgresql.org/docs/current/datatype-numeric.html
first compute then found out ERROR: 22003: integer out of range
so you should change to

RETURN 12 ::bigint * 1000 ::bigint * 1000::bigint * 1000::bigint;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文