Re: SELECT <all fields except "bad_field"> from mytbl;

From: Erwin Brandstetter <brsaweda(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SELECT <all fields except "bad_field"> from mytbl;
Date: 2007-05-30 04:35:13
Message-ID: 1180499713.631199.215610@k79g2000hse.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

To conclude (to the best of my current knowledge), here is a plpgsql
code sample based on what was said here:

EXECUTE
'SELECT '
|| (SELECT array_to_string( ARRAY(
SELECT column_name::text
FROM information_schema.columns
WHERE table_schema = 'my_schema'
AND table_name = 'my_relation'
AND column_name <> 'bad_field'), ', '))
|| ' FROM my_schema.my_relation';

Comments:
- Don't forget to additionally specify the schema - a relation of the
same name may be present in multiple schemas!
- The function array_to_string is the easiest way to form the string
we need.

Actually this is awkwardly slow, as information_schema.columns queries
a ton of other information from various system relations (just have a
look at "explain analyze <query>"!).
So, if you have the necessary privileges to access pg_catalog and
speed is important, this generates the code multiple times faster:

EXECUTE
'SELECT '
|| (SELECT array_to_string(ARRAY(
SELECT a.attname
FROM pg_class c, pg_namespace nc, pg_attribute a
WHERE c.relname = 'v_event'
AND c.relnamespace = nc.oid
AND nc.nspname = 'stdat'
AND a.attrelid = c.oid
AND a.attname <> 'log_up'), ', '))
|| ' FROM stdat.v_event';

Regards
Erwin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rodrigo De León 2007-05-30 04:48:08 Re: SELECT <all fields except "bad_field"> from mytbl;
Previous Message Erwin Brandstetter 2007-05-30 03:24:57 Re: SELECT <all fields except "bad_field"> from mytbl;