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 3B528D1B182 for ; Mon, 17 May 2004 00:27:31 -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 15347-06 for ; Mon, 17 May 2004 00:26:01 -0300 (ADT) Received: from svr4.postgresql.org (unknown [66.98.251.159]) by svr1.postgresql.org (Postfix) with ESMTP id C368CD1B1DB for ; Mon, 17 May 2004 00:24:19 -0300 (ADT) Received: from ms-smtp-01.tampabay.rr.com (ms-smtp-01-smtplb.tampabay.rr.com [65.32.5.131]) by svr4.postgresql.org (Postfix) with ESMTP id D017A5AF312 for ; Mon, 17 May 2004 03:21:01 +0000 (GMT) Received: from joiner (155.14.33.65.cfl.rr.com [65.33.14.155]) by ms-smtp-01.tampabay.rr.com (8.12.10/8.12.7) with ESMTP id i4H3GFDJ024016 for ; Sun, 16 May 2004 23:16:15 -0400 (EDT) Subject: Re: table column information From: "Scot L. Harris" To: pgsql-php@postgresql.org In-Reply-To: <1084762532.4822.36.camel@lamb.mcmillan.net.nz> References: <1084749506.2056.148.camel@lathe> <1084762532.4822.36.camel@lamb.mcmillan.net.nz> Content-Type: text/plain Message-Id: <1084763775.2058.173.camel@lathe> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5 Date: Sun, 16 May 2004 23:16:15 -0400 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Symantec AntiVirus Scan Engine X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200405/29 X-Sequence-Number: 2748 On Sun, 2004-05-16 at 22:55, Andrew McMillan wrote: > On Mon, 2004-05-17 at 00:19 +0000, ljb wrote: > > > But I don't think I want to select the entire contents of the table > > > every time I want to get the names of the columns. I know this will > > > work but I think performance will be very poor. > > >... > > > > You almost got it - just do "select * from tablename where 0=1", which returns > > no rows but will give you the fieldnames. A portable and (I think) > > efficient way to get table column names. > > It can be a cute trick, and I use it myself from time to time > (especially for "CREATE TABLE AS SELECT ..." where I want an empty table > with the same structure, pre v 7.4 which can do this anyway). You > should be aware however that as written above it will almost invariably > force a full-table scan! > > 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. Thanks! -- Scot L. Harris