Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres return a default value when a column doesn't exist

I have a query where I essentially want a fallback value if a certain column is missing. I was wondering if I can handle this purely in my query (rather than probing first and sending a seperate query. In essence i'm looking for an equivalent to COALESCE that handles the case of a missing column.

Imagine the following 2 tables.

T1
id | title | extra
1    A     | value

- and -

T2
id | title
1    A

I'd like to be able to SELECT from either of these tables WITH THE SAME QUERY.

eg, if t2 actually had an 'extra' column I could use

 SELECT id,title, COALESCE(extra, 'default') as extra

But that only works if the column value is NULL, not when the column is missing entirely.

I would prefer an SQL version but I can accept a PLPGSQL function (with a behaviour similiar to COALLESCE) too.

NOTE to SQL purists: I don't really feel like debating why I want to do this in SQL and not in application logic (or why I won't just add the column permanently to the schema) so please restrict your comments/answers to the specific request and not your opinion on database 'correctness' or whatever else might offend you about this question.

like image 855
SpliFF Avatar asked Sep 23 '13 02:09

SpliFF


2 Answers

Why does Rowan's hack work (mostly)?

SELECT id, title
     , CASE WHEN extra_exists THEN extra::text ELSE 'default' END AS extra
FROM   tbl
CROSS  JOIN (
   SELECT EXISTS (
      SELECT FROM information_schema.columns 
      WHERE  table_name = 'tbl'
      AND    column_name = 'extra')
   ) AS extra(extra_exists)

Normally, it would not work at all. Postgres parses the SQL statement and throws an exception if any of the involved columns does not exist.

The trick is to introduce a table name (or alias) with the same name as the column name in question. extra in this case. Every table name can be referenced as a whole, which results in the whole row being returned as type record. And since every type can be cast to text, we can cast this whole record to text. This way, Postgres accepts the query as valid.

Since column names take precedence over table names, extra::text is interpreted to be the column tbl.extra if the column exists. Otherwise, it would default to returning the whole row of the table extra - which never happens.

Try to pick a different table alias for extra to see for yourself.

This is an undocumented hack and might break if Postgres decides to change the way SQL strings are parsed and planned in future versions - even though unlikely.

Unambiguous

If you decide to use this, at least make it unambiguous.

A table name alone is not unique. A table named "tbl" can exist any number of times in multiple schemas of the same database, which could lead to very confusing and completely false results. You need to supply the schema name additionally:

SELECT id, title
     , CASE WHEN col_exists THEN extra::text ELSE 'default' END AS extra
FROM   tbl
CROSS  JOIN (
   SELECT EXISTS (
      SELECT FROM information_schema.columns 
      WHERE  table_schema = 'public'
      AND    table_name = 'tbl'
      AND    column_name = 'extra'
      ) AS col_exists
   ) extra;

Faster

Since this query is hardly portable to other RDBMS, I suggest to use the catalog table pg_attribute instead of the information schema view information_schema.columns. About 10 times faster.

SELECT id, title
     , CASE WHEN col_exists THEN extra::text ELSE 'default' END AS extra
FROM   tbl
CROSS  JOIN (
   SELECT EXISTS (
      SELECT FROM pg_catalog.pg_attribute
      WHERE  attrelid = 'myschema.tbl'::regclass  -- schema-qualified!
      AND    attname  = 'extra'
      AND    NOT attisdropped    -- no dropped (dead) columns
      AND    attnum   > 0        -- no system columns
      )
   ) extra(col_exists);

Also using the more convenient and secure cast to regclass. See:

  • What does regclass mean in Postgresql

You can attach the needed alias to fool Postgres to any table, including the primary table itself. You don't need to join to another relation at all, which should be fastest:

SELECT id, title
     , CASE WHEN EXISTS (SELECT FROM pg_catalog.pg_attribute
                         WHERE  attrelid = 'tbl'::regclass
                         AND    attname  = 'extra'
                         AND    NOT attisdropped
                         AND    attnum   > 0)
            THEN extra::text
            ELSE 'default' END AS extra
FROM tbl AS extra;

Convenience

You could encapsulate the test for existence in a simple SQL function (once), arriving (almost) at the function you have been asking for:

CREATE OR REPLACE FUNCTION col_exists(_tbl regclass, _col text)
  RETURNS bool
  LANGUAGE sql STABLE AS
$func$
SELECT EXISTS (
   SELECT FROM pg_catalog.pg_attribute
   WHERE  attrelid = $1
   AND    attname  = $2
   AND    NOT attisdropped
   AND    attnum   > 0
   )
$func$;

COMMENT ON FUNCTION col_exists(regclass, text) IS
'Test for existence of a column. Returns TRUE / FALSE.
$1 .. exact table name (case sensitive!), optionally schema-qualified
$2 .. exact column name (case sensitive!)';

Simplifies the query to:

SELECT id, title
     , CASE WHEN col_exists THEN extra::text ELSE 'default' END AS extra
FROM   tbl
CROSS  JOIN col_exists('tbl', 'extra') AS extra(col_exists);

Using the form with additional relation here, since it turned out to be faster with the function.

Still, you only get the text representation of the column with any of these queries. It's not as simple to get the actual type.

Benchmark

I ran a quick benchmark with 100k rows on pg 9.1 and 9.2 to find these to be fastest:

Fastest:

SELECT id, title
     , CASE WHEN EXISTS (SELECT FROM pg_catalog.pg_attribute
                         WHERE  attrelid = 'tbl'::regclass
                         AND    attname  = 'extra'
                         AND    NOT attisdropped
                         AND    attnum   > 0)
            THEN extra::text
            ELSE 'default' END AS extra
FROM   tbl AS extra;

2nd fastest:

SELECT id, title
     , CASE WHEN col_exists THEN extra::text ELSE 'default' END AS extra
FROM   tbl
CROSS  JOIN col_exists('tbl', 'extra') AS extra(col_exists);

db<>fiddle here
Old sqlfiddle

like image 96
Erwin Brandstetter Avatar answered Oct 04 '22 19:10

Erwin Brandstetter


One way is to look up the information schema table and do a little magic with it.

Something like:

SELECT id, title, CASE WHEN extra_exists THEN extra ELSE 'default' END AS extra
FROM mytable
CROSS JOIN (
SELECT EXISTS (SELECT 1 
FROM information_schema.columns 
WHERE table_name='mytable' AND column_name='extra') AS extra_exists) extra

Edit: Where 'mytable' needs to be passed in for the table you want to query.

like image 45
Rowan Avatar answered Oct 04 '22 19:10

Rowan