Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eoLUc-0000aJ-W6 for pgsql-general@arkaria.postgresql.org; Wed, 21 Feb 2018 03:54:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eoLUb-00073Y-A4 for pgsql-general@arkaria.postgresql.org; Wed, 21 Feb 2018 03:54:53 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eoLUa-00073P-V4 for pgsql-general@lists.postgresql.org; Wed, 21 Feb 2018 03:54:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eoLUY-0007BH-Bg for pgsql-general@lists.postgresql.org; Wed, 21 Feb 2018 03:54:51 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w1L3sfvO021872; Tue, 20 Feb 2018 22:54:41 -0500 From: Tom Lane To: Demitri Muna cc: pgsql-general@lists.postgresql.org Subject: Re: Getting a primitive numeric value from "DatumGetNumeric"? In-reply-to: References: Comments: In-reply-to Demitri Muna message dated "Tue, 20 Feb 2018 19:11:49 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <21870.1519185281.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 20 Feb 2018 22:54:41 -0500 Message-ID: <21871.1519185281@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Demitri Muna writes: > I=E2=80=99m writing a C extension for PostgreSQL. One possible input dat= atype for my function is a numeric array, e.g. ARRAY[[1.5,2.5],[3.5,4.5]].= I can use =E2=80=9CDatumGetNumeric=E2=80=9D to extract a =E2=80=9CNumeric= =E2=80=9D data type from the data, but at some point I need to convert thi= s to a number (e.g. double) so that I can do mathy things with it. How doe= s one convert a =E2=80=9CNumeric=E2=80=9D to, say, a double? If you want to work with doubles, why don't you declare the function as taking doubles? > I have a workaround in that I can pass this to my function: > ARRAY[[1.5,2.5],[3.5,4.5]]::float8[] > but I=E2=80=99d rather have the code do that instead of bothering the us= er to remember that. Well, the implicit coercions work in your favor in this particular case. You can just do, eg, regression=3D# create function foo(float8[]) returns float8 as regression-# 'select $1[1]' language sql; CREATE FUNCTION regression=3D# select foo(array[1.1,1.2]); foo = ----- 1.1 (1 row) or to emphasize that it is doing a conversion: regression=3D# select foo(array[1.1,1.2]::numeric[]); foo = ----- 1.1 (1 row) regards, tom lane