Re: Fetching column names for a table

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: Steve Manes <smanes(at)magpie(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Fetching column names for a table
Date: 2005-09-21 19:54:54
Message-ID: 4331BA8E.1040005@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Steve Manes wrote:

> I need to extract a SETOF column names for a table in plpgsql. How is
> this done?

A query such as this:

select * from information_schema.columns where table_name = 'table_name';

Will give you a bunch of information. For SET OF functions in general
take a look at:

http://techdocs.postgresql.org/guides/SetReturningFunctions

You can also use something like this:

CREATE TYPE column_type_set AS (column_name text, column_type text);
CREATE OR REPLACE FUNCTION describe_table (text, text) RETURNS SETOF
column_type_set AS '
SELECT attname::text, typname::text
FROM pg_namespace, pg_attribute, pg_type, pg_class
WHERE pg_type.oid = atttypid
AND pg_class.oid = attrelid
AND relname = $2 AND attnum >= 1
AND relnamespace = pg_namespace.oid
AND pg_namespace.nspname = $1;
' LANGUAGE 'SQL';

Sincerely,

Joshua D. Drake

>
> ---------------------------------------------=o&o>---------
> Steve Manes http://www.magpie.com
> Brooklyn, NY
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly

--
Your PostgreSQL solutions company - Command Prompt, Inc. 1.800.492.2240
PostgreSQL Replication, Consulting, Custom Programming, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message CSN 2005-09-21 20:02:34 index row size exceeds btree maximum
Previous Message Scott Marlowe 2005-09-21 19:53:45 Re: Postgres locks table schema?