pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joshua Drake <jd@commandprompt.com>
Cc: kevin kempter <kevin@kevinkempterllc.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Select all table column names for a specified tablename (per the system catalogs)
Date: Mon, 22 Sep 2008 23:37:10 -0400
Message-ID: <16826.1222141030@sss.pgh.pa.us> (raw)
In-Reply-To: <20080922152950.294284b4@jd-laptop>
References: <E9AEE096-D4FD-4A3E-9FD5-FF134747B040@kevinkempterllc.com>
	<20080922152950.294284b4@jd-laptop>

Joshua Drake <jd@commandprompt.com> writes:
> kevin kempter <kevin@kevinkempterllc.com> 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



view thread (3+ messages)

Message-ID: <16826.1222141030@sss.pgh.pa.us>
Permalink:  ../16826.1222141030@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16826.1222141030@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jd@commandprompt.com, kevin@kevinkempterllc.com
  Subject: Re: Select all table column names for a specified tablename (per the system catalogs)
  In-Reply-To: <16826.1222141030@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox