Skip to content
Advertisement

Postgres SQL query across different schemas

We have multiple schemas, I would like to run a simple count query across schemas such as:

I saw that I’m able to get all the schemas with:

So by using:

I was able to run the query for schema1

The question is – is it possible to run in a loop and use the schema name as a parameter for search_path and run the query across all schemas? or any other efficient way to do so?

Advertisement

Answer

You will need some plpgsql and dynamic SQL for this. Here is an anonymous block for illustration:

And btw WHERE col_x is not NULL is redundant.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement