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 1tx7kV-003EuB-2D for pgsql-general@arkaria.postgresql.org; Tue, 25 Mar 2025 17:07:35 +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 1tx7kT-0078o3-LT for pgsql-general@arkaria.postgresql.org; Tue, 25 Mar 2025 17:07:33 +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 1tx7kT-0078nu-9n for pgsql-general@lists.postgresql.org; Tue, 25 Mar 2025 17:07:33 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tx7kR-00155x-27 for pgsql-general@postgresql.org; Tue, 25 Mar 2025 17:07:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 52PH7Tsv1284144; Tue, 25 Mar 2025 13:07:29 -0400 From: Tom Lane To: Alexander Farber cc: pgsql-general Subject: Re: How to pass a list of locations (longitude, latitude) to a PostgreSQL/PostGIS stored function? In-reply-to: References: Comments: In-reply-to Alexander Farber message dated "Tue, 25 Mar 2025 11:39:03 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1284142.1742922449.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 25 Mar 2025 13:07:29 -0400 Message-ID: <1284143.1742922449@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 li= st > 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#PLPG= SQL-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