PLPGSQL函数返回错误:整数超出范围,即使值正确
我在两个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选择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;