public inbox for [email protected]
help / color / mirror / Atom feedFrom: Tony Shelver <[email protected]>
To: [email protected]
Cc: pgsql-general <[email protected]>
Subject: Re: How to pass a list of locations (longitude, latitude) to a PostgreSQL/PostGIS stored function?
Date: Wed, 26 Mar 2025 07:33:26 +0200
Message-ID: <CAG0dhZDtaeGxpOM+F744kZG63sOaPPh3Hd8C=gMta+xEfi_v5g@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CAADeyWiTu2P1Y_6dPn2E+1AwSvNkwNHmT6UcFStubz0kFh9gtg@mail.gmail.com>
<[email protected]>
On Tue, 25 Mar 2025 at 19:07, Tom Lane <[email protected]> wrote:
> Alexander Farber <[email protected]> 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.
view thread (4+ messages) latest in thread
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 using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected]
Subject: Re: How to pass a list of locations (longitude, latitude) to a PostgreSQL/PostGIS stored function?
In-Reply-To: <CAG0dhZDtaeGxpOM+F744kZG63sOaPPh3Hd8C=gMta+xEfi_v5g@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox