Re: query multiple schemas - Mailing list pgsql-general

From Tom Lane
Subject Re: query multiple schemas
Date
Msg-id 2857958.1713733916@sss.pgh.pa.us
Whole thread Raw
In response to Re: query multiple schemas  (Steve Baldwin <steve.baldwin@gmail.com>)
Responses Re: query multiple schemas
List pgsql-general
Steve Baldwin <steve.baldwin@gmail.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



pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: error in trigger creation
Next
From: Tom Lane
Date:
Subject: Re: error in trigger creation