This answer shows how to get the DEFAULT value stored in the information_schema.
The problem is that the default value is stored as text, so you see e.g. a string 'hello'::text
or expression (1 + 1)
which is difficult to compare with the column value.
Anyway you may use a trick using a helper table illustrated below.
Create table
create table mytable (
id int ,
col text default 'hello',
col2 int default 1,
col3 int
);
and put some data in it
insert into mytable(id) values(1);
insert into mytable(id,col,col2,col3) values(2,'x',3,10);
now create a clone table (like
) and insert one row with DEFAULT values
drop table get_default;
create table get_default ( like mytable including all);
insert into get_default DEFAULT VALUES;
query is trivial only you must hanle the case of null
default value in extra OR
predicate
select * from mytable
where col3 = (select col3 from get_default) or
col3 is null and (select col3 from get_default) is null;
id|col |col2|col3|
--+-----+----+----+
1|hello| 1| |
But note that your approach will fail badly if you use non deterministic DEFAULT values such as now()
or nextval
.