Question about integer out of range in function

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Condor <condor(at)stz-bg(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Question about integer out of range in function
Date: 2021-05-16 18:09:38
Message-ID: CAKFQuwZ+WPMGPF5c4evn+q83P8mLe4UhZnJepSG5AS9=b8ciyg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sunday, May 16, 2021, Condor <condor(at)stz-bg(dot)com> wrote:

>
> new_time = fromtime * 1000; -- here is line 19
>
>

An integer times an integer results in an integer. Period. Neither
fromtime nor new_time have been assigned to yet, the in-memory result of
the computation is only allocated integer bits and if you overflow that you
get an error. If there is no error the result of that computation is
stored in new_time. Since new_time is a bigint during assignment the
in-memory integer is implicitly converted to bigint to match the assignment
type.

Either fromtime or the 1000 need to be declared as bigint if you want the
computation type to be bigint as well, and thus avoid the overflow. The
implicit cast to store into new_time goes away. And whichever, if any, of
the two values you do not change to bigint gets implicitly cast to bigint
in order for the multiplication operator to match “*(bigint,bigint)”.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Loles 2021-05-16 18:47:20
Previous Message Condor 2021-05-16 17:54:20 Re: Question about integer out of range in function