Supplying a default on read, if a column does not exist in DB - Mailing list pgsql-sql

From Bryce Nesbitt
Subject Supplying a default on read, if a column does not exist in DB
Date
Msg-id 4831F5B2.7010807@obviously.com
Whole thread Raw
List pgsql-sql
Is there a clean way in Postgres to specify a default return value, if a 
column does not exist in a database?  In pseudocode:

select p_email,
CASE WHEN EXISTS("p_email_alt") THEN p_email_alt ELSE 'none' END
from eg_application;

I can kind of almost get there with:

select p_email,
CASE WHEN EXISTS(SELECT * FROM information_schema.columns
WHERE table_schema='public' and table_catalog='stage' and 
table_name='eg_application' and column_name='p_email_alt')
THEN p_email_alt ELSE 'none' END
from eg_application;

Except that Postgres evaluates the non-existent column name in the 
"THEN", and errors out, even though the test will be false.

Note that use of stored procedures won't work for my particular use case.
            -Bryce

Google Keywords: "if column exists", "if column defined", test for 
existence of column, default column value, defaults, information schema, 
existence, definition, missing column.


pgsql-sql by date:

Previous
From: "Dave Page"
Date:
Subject: Re: dblinks
Next
From: Karl Denninger
Date:
Subject: SQL question....