I have a table with inheritance with 4 child tables.
How can I know the source table of a specific row?
Asked
Active
Viewed 150 times
0
Erwin Brandstetter
- 605,456
- 145
- 1,078
- 1,228
Santi
- 1
- 1
2 Answers
2
From PostgreSQL documentation on inheritance:
By doing a join with
pg_classyou can see the actual table names:SELECT p.relname, c.name, c.altitude FROM cities c, pg_class p WHERE c.altitude > 500 AND c.tableoid = p.oid;
In the example above, p.relname would return the specific child table the row was fetched from.
sstan
- 35,425
- 6
- 48
- 66
1
Use the system column tableoid, it's available for any table.
But you don't need to join to pg_class. Just cast to regclass (and then to text, optionally).
SELECT tbl_id, tableoid::regclass::text AS source
FROM tbl
WHERE <some condition>;
Related:
Community
- 1
- 1
Erwin Brandstetter
- 605,456
- 145
- 1,078
- 1,228