Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E699164FC20 for ; Tue, 23 Sep 2008 00:37:16 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14279-09 for ; Tue, 23 Sep 2008 00:37:14 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 19DD464FC21 for ; Tue, 23 Sep 2008 00:37:13 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id m8N3bAHx016827; Mon, 22 Sep 2008 23:37:10 -0400 (EDT) To: Joshua Drake cc: kevin kempter , pgsql-admin@postgresql.org Subject: Re: Select all table column names for a specified tablename (per the system catalogs) In-reply-to: <20080922152950.294284b4@jd-laptop> References: <20080922152950.294284b4@jd-laptop> Comments: In-reply-to Joshua Drake message dated "Mon, 22 Sep 2008 15:29:50 -0700" Date: Mon, 22 Sep 2008 23:37:10 -0400 Message-ID: <16826.1222141030@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200809/241 X-Sequence-Number: 30334 Joshua Drake writes: > kevin kempter wrote: >> ... I got this far (see >> below) however this query produces additional rows with attname's >> like tableoid, cmax, xmax ctid, etc. >> >> select attname from pg_attribute where attrelid = (select oid from >> pg_class where relname = 'my_tablename'); > SELECT column_name FROM information_schema.columns WHERE table_name = > 'table'; If you don't want to use information_schema (which you might well not want to, because it's a bit slow), the bits you're missing are that you want to exclude columns with attnum <= 0 (system columns) as well as those with attisdropped (dropped columns). I don't care for the subselect part of this either, mainly because it is not schema-aware. Consider select attname from pg_attribute where attrelid = 'my_tablename'::regclass and attnum > 0 and not attisdropped; which generalizes to select attname from pg_attribute where attrelid = 'my_schema.my_tablename'::regclass and attnum > 0 and not attisdropped; whereas your original will not handle that easily. regards, tom lane