Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1txJOa-006Tl0-Hw for pgsql-general@arkaria.postgresql.org; Wed, 26 Mar 2025 05:33:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1txJOY-00FJbS-1f for pgsql-general@arkaria.postgresql.org; Wed, 26 Mar 2025 05:33:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1txJOX-00FJbK-GP for pgsql-general@lists.postgresql.org; Wed, 26 Mar 2025 05:33:41 +0000 Received: from mail-qv1-xf2a.google.com ([2607:f8b0:4864:20::f2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1txJOU-001B03-3B for pgsql-general@postgresql.org; Wed, 26 Mar 2025 05:33:40 +0000 Received: by mail-qv1-xf2a.google.com with SMTP id 6a1803df08f44-6ecf0e07954so45980206d6.1 for ; Tue, 25 Mar 2025 22:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742967218; x=1743572018; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JX7iJlARDdnXsmrf7lsrD6Y17y0imuAZFKnSDZZsJLg=; b=ZzEYkj+5nnGh3OZ4JrN8nd5/C0yrmDFyoYWU3yry1ziMEu56aKG8NnXIxjKzr9uLlb Y+63coqqXvaRCswYwsYcrX3veDAxfCbSmDas0ELNU3x0BRWX6yctxf1i6ipIhUMUg+OP 0rP9fVjk9vgBwcFkTfrNsq+XfLoHfCrj29H8b7JksSQGOGAMaGqML+SGc3NwtbGTuLxH 8ixdyF8TnDiYFWU72xzCpWR85eJ6qBieNnt+C3W1GxNR/tQhqhgC+mjGU0R17q9bKbH2 OfD9yFsWPhLeDtL44Ta+huKq/RRbV85MIa+0ezMb2ASwg0l0B5IUukvXiFVZTr0sxxkW y0tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742967218; x=1743572018; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JX7iJlARDdnXsmrf7lsrD6Y17y0imuAZFKnSDZZsJLg=; b=fqVRRZwVDSjZ3RFSE1RkgXJx8qVNtxppyyXkZty0axLInLrnpTD2tMFXp+Y4p8ri4k FgcvT5KezNI7dVtV1d0OD/RDov2hGdeE0+Q8+5QU3iEoOfDDVqjTyxDPqfxVZGlglI5e HIAiy456DWPl+AsEntQuBx26nBcG4q2Ol+b3XiSvCSvimvqCDgEY5LLnmspr0uSqO16V 8BoI1JVIlM9sf0hrSMwQHbE9UoQVRQC/VfHJd77OyZthhk1nDqc2BX0WvkeKyQs4/SPW FMJg85snrQacttJMvaqHT39STaPawuvxrwDUAw19/QVGrl8v+MjURxVJiuJ5eyrl2wGh AJzQ== X-Gm-Message-State: AOJu0YxdgCPM30zB1ijn3zsON+u5ggC1ogJARRnpZlGP6RmhbrsXflqV 0chkxblPEKqcL9K/39F5dbQJw5YPZdWf3D6i5SKXFc+5X09rVvdru70WI2vsUU9LeRe6O7Nqx6+ 7CTnqd6ngcf11OgxL2QaGuQlF9N0= X-Gm-Gg: ASbGncuQfIDeSTban4YxgeduIyAlUH11T4dh2WXqBQa80k+aYNVhTgXgbLiC1XIpdcI Xbg9ut0weUxDJNnE7H3cxJMIAcD++4gvujMOxRdduc1lTQD4q2OUIRKpZwA5vCEmR+tT16qJSps DwSI4hxhtuGNURJVwt6hVv4BWqNtFI X-Google-Smtp-Source: AGHT+IHCmn+yPB7WbQH5LtzUqSQqUkIMNioLGksvG+nCsu48Udt3OPmPzkZbPrFlFiRICETL3OmBSf4l85RGqO99mmo= X-Received: by 2002:a05:6214:2a87:b0:6d4:e0a:230e with SMTP id 6a1803df08f44-6eb3f2e76a5mr300923136d6.16.1742967217737; Tue, 25 Mar 2025 22:33:37 -0700 (PDT) MIME-Version: 1.0 References: <1284143.1742922449@sss.pgh.pa.us> In-Reply-To: <1284143.1742922449@sss.pgh.pa.us> From: Tony Shelver Date: Wed, 26 Mar 2025 07:33:26 +0200 X-Gm-Features: AQ5f1JrluWk8244rquysCtvFoweJOKu-vclkYfCPG0aNAH-VlDSfiKniQHa_fQw Message-ID: Subject: Re: How to pass a list of locations (longitude, latitude) to a PostgreSQL/PostGIS stored function? To: alexander.farber@gmail.com Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000de54150631382d7a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000de54150631382d7a Content-Type: text/plain; charset="UTF-8" On Tue, 25 Mar 2025 at 19:07, Tom Lane wrote: > Alexander Farber writes: > > Then I am trying to add a function, which would receive a series of > > locations (longitude and latitude pairs in microdegrees) and return a > list > > of lowercase 2-letter country codes, like "de", "pl", "lv": > > > CREATE OR REPLACE FUNCTION find_countries(locations BIGINT[][]) > > RETURNS TABLE (country TEXT) AS $$ > > Postgres isn't too friendly to representing a list of locations as > a 2-D array, because we generally don't treat arrays as being > arrays-of-arrays, so unnest produces a set of bigints not a set > of smaller arrays. You might be best advised to create a composite > type like "location (long bigint, lat bigint)" and use an array of > that. If you're really hot to use a 2-D array, the only construct > I can think of that's on board with unnesting that the way you need > is plpgsql's FOREACH SLICE syntax: > > > https://www.postgresql.org/docs/devel/plpgsql-control-structures.html#PLPGSQL-FOREACH-ARRAY > > You could probably make a custom version of unnest that uses that > and then keep your query about the same. > > regards, tom lane > > > Another approach I use is to string everything into a JSON object, and de-serialize it in the function. --000000000000de54150631382d7a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, 25 Mar 2025 at 19:07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Alexander Farber <alexander.far= ber@gmail.com> writes:
> Then I am trying to add a function, which would receive a series of > locations (longitude and latitude pairs in microdegrees) and return a = list
> of lowercase 2-letter country codes, like "de", "pl&quo= t;, "lv":

>=C2=A0 =C2=A0 =C2=A0CREATE OR REPLACE FUNCTION find_countries(locations= BIGINT[][])
>=C2=A0 =C2=A0 =C2=A0RETURNS TABLE (country TEXT) AS $$

Postgres isn't too friendly to representing a list of locations as
a 2-D array, because we generally don't treat arrays as being
arrays-of-arrays, so unnest produces a set of bigints not a set
of smaller arrays.=C2=A0 You might be best advised to create a composite type like "location (long bigint, lat bigint)" and use an array o= f
that.=C2=A0 If you're really hot to use a 2-D array, the only construct=
I can think of that's on board with unnesting that the way you need
is plpgsql's FOREACH SLICE syntax:

https://w= ww.postgresql.org/docs/devel/plpgsql-control-structures.html#PLPGSQL-FOREAC= H-ARRAY

You could probably make a custom version of unnest that uses that
and then keep your query about the same.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane



Another approach I us= e is to string everything into a JSON object, and de-serialize it in the fu= nction.


=C2=A0
--000000000000de54150631382d7a--