I have a PostgreSQL 8.3 database where table inheritance is being used. I would like to get a list of all tables along with its schema name which is inherited from a base table using query. Is there any way we can get this using PGSQL?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you’re on such an old version of PostgreSQL you’ll probably have to use a PL/PgSQL function to handle inheritance depths of > 1. On modern PostgreSQL (or even 8.4) you’d use a recursive common table expression (
WITH RECURSIVE).The
pg_catalog.pg_inheritstable is the key. Given:A correct result will find
cc,dd, andccdd, but not findnotppornotshown.A single-depth query is:
… but this will only find
cc.For multi-depth inheritance (ie
tableCinheritstableBinheritstableA) you have to extend that via a recursive CTE or a loop in PL/PgSQL, using the children of the last loop as parents in the next.Update: Here’s an 8.3 compatible version that should recursively find all tables that inherit directly or indirectly from a given parent. If multiple inheritance is used, it should find any table that has the target table as one of its parents at any point along the tree.
Usage:
Here’s the recursive CTE version, which will work if you update Pg, but won’t work on your current version. It’s much cleaner IMO.