Re: Using Insert - Default in a condition expression ?? - Mailing list pgsql-general

From brent_despain@selinc.com
Subject Re: Using Insert - Default in a condition expression ??
Date
Msg-id OF1F917042.CC263F52-ON88257640.004DA473-87257640.004E48DF@selinc.com
Whole thread Raw
In response to Using Insert - Default in a condition expression ??  (Postgres User <postgres.developer@gmail.com>)
List pgsql-general
Don't know if this will get attached to the conversation, but here is how I have dealt with this.

insert into names (name, created_date) values ($1, default);
update names n set created_date = coalesce($2, n.craeted_date) where name = $1;

So basically insert all of your non-defaulted columns with the primary key.  This will put the defaults into the table.  Then update with coalesce the values passed into the proc.  The coalesce will use the parameter unless it is NULL.  If it is NULL it will use the default from the table.  This is inefficient since it will version the row in the table for each call to the proc.

Brent DeSpain

pgsql-general by date:

Previous
From: Andy Colson
Date:
Subject: Re: query is taking longer time after a while
Next
From: Dave Huber
Date:
Subject: Re: bulk inserts