pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Christophe Pettus <xof@thebuild.com>
Cc: stan <stanb@panix.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Can I trigger an action from a coalesce ?
Date: Sat, 22 Feb 2020 17:02:33 -0500
Message-ID: <7426.1582408953@sss.pgh.pa.us> (raw)
In-Reply-To: <982511F6-2957-43EE-8A28-3A5541B56ADF@thebuild.com>
References: <20200222210206.GA17445@panix.com>
	<35c60adc-3f02-13ad-a4a6-48642a1185d8@aklaver.com>
	<BF12101F-CC34-4C75-B770-78B544AEEB70@thebuild.com>
	<20200222213325.GA22785@panix.com>
	<982511F6-2957-43EE-8A28-3A5541B56ADF@thebuild.com>

Christophe Pettus <xof@thebuild.com> writes:
> Something like this:

> create function supply_default() returns int as $$
> begin
>    raise notice 'Supplied default';
>    return 1;
> end;
> $$ immutable language plpgsql;

It's a really bad idea to mark a function that has side-effects
(i.e., emitting a NOTICE) as immutable, especially if the occurrence
of the side-effect at well-defined times is exactly what you're
desirous of.

> xof=# create table t ( i integer default supply_default(), t text );
> CREATE TABLE
> xof=# insert into t(i, t) values (2, 'text');
> INSERT 0 1
> xof=# insert into t(t) values ('text');
> NOTICE:  Supplied default
> INSERT 0 1

Other than the mislabeled volatility, I think this will mostly work.

Another possibility is to use a before-row-insert trigger
that does something like

	if new.i is null then
        begin
          new.i := whatever;
          raise notice 'Supplied default';
        end if;

This seems cleaner in principle, but a problem is that it can't tell
an inserted-by-default NULL from one that was intentionally supplied.
That might be OK if you never want the field to be null anyway.

			regards, tom lane





view thread (12+ messages)  latest in thread

Message-ID: <7426.1582408953@sss.pgh.pa.us>
Permalink:  ../7426.1582408953@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/7426.1582408953@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, xof@thebuild.com, stanb@panix.com
  Subject: Re: Can I trigger an action from a coalesce ?
  In-Reply-To: <7426.1582408953@sss.pgh.pa.us>
  References: <20200222210206.GA17445@panix.com> <35c60adc-3f02-13ad-a4a6-48642a1185d8@aklaver.com> <BF12101F-CC34-4C75-B770-78B544AEEB70@thebuild.com> <20200222213325.GA22785@panix.com> <982511F6-2957-43EE-8A28-3A5541B56ADF@thebuild.com> <7426.1582408953@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox