Re: Return results of join with polymorphically-defined table in pl/pgsql

From: Peter Devoy <peter(at)3xe(dot)co(dot)uk>
To: John R Pierce <pierce(at)hogranch(dot)com>
Cc: "Psql_General (E-mail)" <pgsql-general(at)postgresql(dot)org>, david(dot)g(dot)johnston(at)gmail(dot)com
Subject: Re: Return results of join with polymorphically-defined table in pl/pgsql
Date: 2016-07-25 10:14:11
Message-ID: CABoFc_jabuUOmYd5c9O22-eUMgTmy7CgojDh2PZ0WYP+SFf-DQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

@David, thanks for the tip.

>Providing a concrete example might help.
My use case is a database with a large number of spatial tables. I
have written a spatial search function which, given an arbitrary table
extended with PostGIS, will search for records in that table whose
geometries are within a given distance. The return value is a SETOF
values 'geometry ID', 'distance from input geometry' and 'centroid'
with corresponding types (int, double precision, geometry).

The final desired output is a resultset consisting of all the input
tables columns as well as these two new columns showing distance and
centroid. Obviously having an ID field this can be achieved with
INNER JOIN. The ideal scenario would be to have a function which also
performs this join... something like:

BEGIN
RETURN QUERY
EXECUTE
format(
'
SELECT
%1$I.*,
dist_query.distance AS appended_distance,
dist_query.centroid AS appended_centroid
FROM %1$I
INNER JOIN distance_search(%1$L, $1, $2, %2$L) AS dist_query
ON %1$I.%2$I=dist_query.%2$I;
',
pg_typeof(table_name),
id_column_name
)
USING search_area, buffer_size;
END;

@John
>SQL tables are /not/ polymorphic.
Yes, you are quite right. I merely meant the table who's row compound
type is been passed as a polymorphic parameter.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2016-07-25 13:11:35 Re: Return results of join with polymorphically-defined table in pl/pgsql
Previous Message 德哥 2016-07-25 06:44:36 postgres_fdw how to pushdown parent table's JOIN