Search code examples
sqlpostgresqlinheritancesingle-table-inheritanceinformation-schema

PostgreSQL inheritance: get the record class name


I have a base table value_list with columns code, value, active. I have some tables vl_a, vl_b, vl_c, etc. inheriting from value_list.

Is there a way, when doing a SELECT * FROM base to know from which class the child comes from.

In other words, I would like to have:

code | value |  active | class
-----+-------+---------+--------
 1   |   c   |  true   | vl_a
 3   |   g   |  false  | vl_b
 5   |   d   |  true   | vl_a
 7   |   f   |  false  | vl_c
 2   |   u   |  false  | vl_c
 2   |   q   |  true   | vl_b
 8   |   a   |  false  | vl_a

Is this possible ?

For more details, here would be the tables:

CREATE TABLE value_list(
  code integer NOT NULL,
  value character varying(50),
  active boolean,
  CONSTRAINT pkey PRIMARY KEY (code)
)

CREATE TABLE vl_a() INHERITS (value_list);
CREATE TABLE vl_b() INHERITS (value_list);
CREATE TABLE vl_c() INHERITS (value_list);

Solution

  • I finally found the solution on the Postgres doc.

    SELECT p.relname, vl.* 
    FROM qgep.is_value_list_base vl, pg_class p 
    WHERE vl.tableoid = p.oid;