Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pvQ49-0004VC-92 for pgsql-general@arkaria.postgresql.org; Sat, 06 May 2023 22:07:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pvQ48-00036A-2m for pgsql-general@arkaria.postgresql.org; Sat, 06 May 2023 22:07:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pvQ47-000361-On for pgsql-general@lists.postgresql.org; Sat, 06 May 2023 22:07:43 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pvQ40-000vIE-Iu for pgsql-general@lists.postgresql.org; Sat, 06 May 2023 22:07:42 +0000 Received: from [192.168.127.4] (port=38145 helo=erlenstar.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.3 (FreeBSD)) (envelope-from ) id 1pvQ3w-000HY6-IU; Sat, 06 May 2023 22:07:32 +0000 Received: from localhost ([127.0.0.1]:19557 helo=erlenstar.riddles.org.uk) by erlenstar.riddles.org.uk with esmtp (Exim 4.94 (FreeBSD)) (envelope-from ) id 1pvQ3v-000N8b-Ff; Sat, 06 May 2023 22:07:31 +0000 From: Andrew Gierth To: Gabriel Furstenheim Milerud Cc: pgsql-general@lists.postgresql.org Subject: Re: Check that numeric is zero In-Reply-To: (Gabriel Furstenheim Milerud's message of "Sat, 6 May 2023 20:46:19 +0200") Message-ID: <878re1b1i3.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (berkeley-unix) Date: Sat, 06 May 2023 23:07:31 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk >>>>> "Gabriel" == Gabriel Furstenheim Milerud 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)