X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 28D999DC9E4 for ; Wed, 14 Dec 2005 13:53:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 38788-06 for ; Wed, 14 Dec 2005 13:53:13 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 621719DC9D2 for ; Wed, 14 Dec 2005 13:53:12 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id jBEHr8D3087162 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 14 Dec 2005 10:53:10 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id jBEHr7em083662; Wed, 14 Dec 2005 10:53:07 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id jBEHr7LD083661; Wed, 14 Dec 2005 10:53:07 -0700 (MST) (envelope-from mfuhr) Date: Wed, 14 Dec 2005 10:53:07 -0700 From: Michael Fuhr To: Jost Degenhardt Cc: pgsql-sql@postgresql.org Subject: Re: Finding out to which table a specific row belongs Message-ID: <20051214175306.GA82006@winnie.fuhr.org> References: <43A055BF.1030907@web.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <43A055BF.1030907@web.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.016 required=5 tests=[AWL=0.016] X-Spam-Score: 0.016 X-Spam-Level: X-Archive-Number: 200512/120 X-Sequence-Number: 23662 On Wed, Dec 14, 2005 at 06:26:23PM +0100, Jost Degenhardt wrote: > I have the following problem: My database consists of several tables > that are inherited from each other with one single supertable on top of > that hierarchy. Now I would like to select a single row in that > supertable and want to find out to which of the tables in the hierarchy > it belongs. http://www.postgresql.org/docs/8.1/interactive/ddl-inherit.html In some cases you may wish to know which table a particular row originated from. There is a system column called tableoid in each table which can tell you the originating table: http://www.postgresql.org/docs/8.1/interactive/ddl-system-columns.html tableoid The OID of the table containing this row. This column is particularly handy for queries that select from inheritance hierarchies (see Section 5.8), since without it, it's difficult to tell which individual table a row came from. The tableoid can be joined against the oid column of pg_class to obtain the table name. Here's an example; instead of getting the table name via a join with pg_class it uses a cast to regclass: CREATE TABLE parent (t text); CREATE TABLE child1 () INHERITS (parent); CREATE TABLE child2 () INHERITS (parent); INSERT INTO child1 VALUES ('one'); INSERT INTO child2 VALUES ('two'); SELECT tableoid::regclass, * FROM parent; tableoid | t ----------+----- child1 | one child2 | two (2 rows) -- Michael Fuhr