Re: query multiple schemas

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Steve Baldwin <steve(dot)baldwin(at)gmail(dot)com>
Cc: Norbert Sándor <sandor(dot)norbert(at)erinors(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: query multiple schemas
Date: 2024-04-21 21:11:56
Message-ID: 2857958.1713733916@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Steve Baldwin <steve(dot)baldwin(at)gmail(dot)com> writes:
> If the number of tenant schemas is reasonably static, you could write a
> plpgsql function to create a set of UNION ALL views with one view for each
> table in all tenant schemas. You could re-run the function each time a
> tenant schema is added. Having the set of views would allow you to query
> them as you would any of the underlying tables, and the query planner could
> likely optimise the query better. With your current function, if you needed
> to add a WHERE clause and the underlying tables were large, it would likely
> not perform as well as the UNION ALL view.

Another idea is to build a partitioned table comprising all the
individual tables. This might require the tables to have all the
same owner (not sure about that) and it'd require you to store the
partition key, ie tenant, explicitly in every table which might
seem a bit wasteful. But it'll likely outperform any other solution
for the union queries. Notably, adding or deleting a partition would
be much less painful than redefining a UNION ALL view.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2024-04-21 21:21:20 Re: error in trigger creation
Previous Message Adrian Klaver 2024-04-21 20:53:05 Re: error in trigger creation