Re: Check that numeric is zero

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Gabriel Furstenheim Milerud <furstenheim(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Check that numeric is zero
Date: 2023-05-06 22:07:31
Message-ID: 878re1b1i3.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>>>>> "Gabriel" == Gabriel Furstenheim Milerud <furstenheim(at)gmail(dot)com> writes:

Gabriel> Hi,

Gabriel> I'm writing a Postgres native extension and I would like to
Gabriel> check that a numeric is zero.

Gabriel> My problem is that all exported methods like numeric_eq or
Gabriel> numeric_sign require me to have a numeric to start with, and
Gabriel> const_zero is not exported in numeric.c.

Currently the easiest and most portable way to get a numeric constant is
to call int4_numeric or int8_numeric via DirectFunctionCall; if you
don't care about versions older than pg14 there's also int64_to_numeric
which can be called directly from C.

Datum zero_num = DirectFunctionCall1(int4_numeric, Int32GetDatum(0));

(remember that this will be allocated in the current memory context; if
you want to keep a copy long-term, you'd want to datumCopy it somewhere
else.)

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeffrey Walton 2023-05-06 22:22:06 Re: "PANIC: could not open critical system index 2662" - twice
Previous Message Thomas Munro 2023-05-06 21:13:20 Re: "PANIC: could not open critical system index 2662" - twice