pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Gabriel Furstenheim Milerud <furstenheim@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Check that numeric is zero
Date: Sat, 06 May 2023 23:07:31 +0100
Message-ID: <878re1b1i3.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAJN3DWquY5P6X_mZEr4xCx7exJceJv6JxLCCgTX__imnxafuWg@mail.gmail.com>
References: <CAJN3DWquY5P6X_mZEr4xCx7exJceJv6JxLCCgTX__imnxafuWg@mail.gmail.com>

>>>>> "Gabriel" == Gabriel Furstenheim Milerud <furstenheim@gmail.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)





view thread (2+ messages)

Message-ID: <878re1b1i3.fsf@news-spur.riddles.org.uk>
Permalink:  ../878re1b1i3.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/878re1b1i3.fsf@news-spur.riddles.org.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, furstenheim@gmail.com
  Subject: Re: Check that numeric is zero
  In-Reply-To: <878re1b1i3.fsf@news-spur.riddles.org.uk>
  References: <CAJN3DWquY5P6X_mZEr4xCx7exJceJv6JxLCCgTX__imnxafuWg@mail.gmail.com> <878re1b1i3.fsf@news-spur.riddles.org.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox