Return default value

From PostgreSQL wiki

Jump to: navigation, search

Snippets

return_default_value function

Works with PostgreSQL

Any version

Written in

plpgsql

Depends on

Nothing

Author: Emanuel


This function return the default value of a column:

CREATE FUNCTION ret_def(text,text,text) RETURNS text AS $$
SELECT 
  COLUMNS.column_default::text
FROM 
  information_schema.COLUMNS
  WHERE table_name = $2
  AND table_schema = $1
  AND column_name = $3
$$ LANGUAGE sql IMUTABLE;

Just call it at this way:

SELECT ret_def('schema','table','column');
Personal tools