Re: SELECT from mytbl; - Mailing list pgsql-general

From Erwin Brandstetter
Subject Re: SELECT from mytbl;
Date
Msg-id 1180499713.631199.215610@k79g2000hse.googlegroups.com
Whole thread Raw
In response to Re: SELECT from mytbl;  (Rodrigo De León <rdeleonp@gmail.com>)
Responses Re: SELECT from mytbl;
List 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


pgsql-general by date:

Previous
From: Erwin Brandstetter
Date:
Subject: Re: SELECT from mytbl;
Next
From: Rodrigo De León
Date:
Subject: Re: SELECT from mytbl;