1

I'm trying to query two information_schema tables in PostgreSQL - tables and columns in order to get the following result:

table_name - columns_as_json_array

Sort of converting this one to many relation into a json array column. I tried a lot of different methods and came up with somethings like this:

SELECT t.table_name, c.json_columns
FROM information_schema.TABLES t
LEFT JOIN LATERAL(
  SELECT table_name, json_agg(row_to_json(tbc)) AS json_columns
  FROM information_schema.COLUMNS tbc
  WHERE t.table_name = tbc.table_name
  GROUP  BY table_name
  ) as c ON TRUE;

This results a list of table_names but the json_columns always contains all of the columns available instead of the columns of that certain table.

Any ideas?

1 Answer 1

3

I don't really see the point for a lateral join here. As far as concerns, you can get the expected results by aggregating information_schema.columns:

select table_name, json_agg(row_to_json(c)) json_columns
from information_schema.columns c
group by table_name
order by table_name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.