Re: query multiple schemas - Mailing list pgsql-general

From Dominique Devienne
Subject Re: query multiple schemas
Date
Msg-id CAFCRh-_vhppeWG2VQ+A9kb3U6+q+tyiwc2BdMKF-r13+a2bFXg@mail.gmail.com
Whole thread Raw
In response to query multiple schemas  (Norbert Sándor <sandor.norbert@erinors.com>)
List pgsql-general
On Tue, Apr 23, 2024 at 11:08 AM Norbert Sándor <sandor.norbert@erinors.com> wrote:
> And if/when I get back to this issue myself, I'll do the same.
My current solution is not much different than the one I posted in my original question.

CREATE OR REPLACE FUNCTION tenant_query_json(tbl anyelement)

RETURNS setof json AS $func$ [...]


CREATE OR REPLACE FUNCTION tenant_query(tbl anyelement)

RETURNS table(tenantId text, record anyelement) AS $func$ [...]

Thanks for sharing Norbi. I'm not well versed in PG/PLsql, so using
`anyelement` and `returns setof / table` is interesting to see.

Regarding the type system, I don't know if PostgreSQL supports "structural" typing,
i.e. types from different schemas, despite having the same "shape", are not interoperable.
Thus your need to go through JSON to "anonymize" the types in your inner function, then
de-anonymize them (to a schema's specific type) as a record, in your outer function.

One solution in your case of homogenous tables types, is to centralize your types in
a single data-less schema, and then create each tenants tables based on those types,
i.e. all tables (across tenant schemas) share the same type. But that's speculation mostly.

--DD

CREATE [...] TABLE [...] table_name OF type_name

pgsql-general by date:

Previous
From: Dominique Devienne
Date:
Subject: Re: query multiple schemas
Next
From: Arbol One
Date:
Subject: Password forgotten