X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id C5261D1B209 for ; Mon, 17 May 2004 07:26:49 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 32201-03 for ; Mon, 17 May 2004 07:26:32 -0300 (ADT) Received: from lamb.mcmillan.net.nz (218-101-14-65.paradise.net.nz [218.101.14.65]) by svr1.postgresql.org (Postfix) with ESMTP id 2F903D1B1BB for ; Mon, 17 May 2004 07:26:28 -0300 (ADT) Received: from lamb.mcmillan.net.nz (lamb.mcmillan.net.nz [127.0.0.1]) by lamb.mcmillan.net.nz (Postfix) with ESMTP id 143D3AD98585; Mon, 17 May 2004 22:26:23 +1200 (NZST) Subject: Re: table column information From: Andrew McMillan To: "Scot L. Harris" Cc: pgsql-php@postgresql.org In-Reply-To: <1084763775.2058.173.camel@lathe> References: <1084749506.2056.148.camel@lathe> <1084762532.4822.36.camel@lamb.mcmillan.net.nz> <1084763775.2058.173.camel@lathe> Content-Type: text/plain Message-Id: <1084789582.27913.7.camel@lamb.mcmillan.net.nz> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.5.7 Date: Mon, 17 May 2004 22:26:22 +1200 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.2 tagged_above=0.0 required=5.0 tests=HTML_MESSAGE, HTML_TAG_BALANCE_TABLE X-Spam-Level: X-Archive-Number: 200405/30 X-Sequence-Number: 2749 On Sun, 2004-05-16 at 23:16 -0400, Scot L. Harris wrote: > > > > You can also select the column names from the database metadata > > directly: > > > > SELECT attname > > FROM pg_class c join pg_attribute a on c.oid = a.attrelid > > WHERE c.relname = '' > > AND a.attnum >= 0; > > > > This approach won't get killed by the efficiency problems above. > > > > Cheers, > > Andrew. > > Thanks. Most of the tables I have are fairly small (for now) but at > least one of them has many thousands of rows and I did not want to have > to scan all of them for this information. I understand why the 0=1 > trick will scan every row. I like the idea of getting the meta data > directly. > > None of the books I have seem to discuss this kind of thing. Is the > pg_class and pg_attribute tables hidden? I see pga_layout and some > others but not the first two when I do a \d. I do get a column listing > when I do a \d pg_class so they are there. > > And this worked great on my test database/tables. When I want to figure out something like this I tend to use "psql -E" so that all queries are echoed before being sent to the backend. Then I do something like "\d " and see what SQL psql generates internally. Also, dig here for detailed information on the postgresql data dictionary tables: http://www.postgresql.org/docs/7.4/interactive/catalogs.html the most useful ones are pg_class and pg_attribute usually (for obvious reasons :-). With 7.4 I also find myself looking at the pg_stat_activity view from time to time as well. Cheers, Andrew. ------------------------------------------------------------------------- Andrew @ Catalyst .Net .NZ Ltd, PO Box 11-053, Manners St, Wellington WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St DDI: +64(4)916-7201 MOB: +64(21)635-694 OFFICE: +64(4)499-2267 You possess a mind not merely twisted, but actually sprained. -------------------------------------------------------------------------