pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: sivapostgres@yahoo.com <sivapostgres@yahoo.com>
To: pgsql-general@lists.postgresql.org
To: Peter J. Holzer <hjp-pgsql@hjp.at>
Subject: Re: Can I trigger an action from a coalesce ?
Date: Mon, 24 Feb 2020 05:20:49 +0000 (UTC)
Message-ID: <510218283.7242058.1582521649818@mail.yahoo.com> (raw)
In-Reply-To: <20200223112148.GF12464@hjp.at>
References: <20200222210206.GA17445@panix.com>
	<20200223112148.GF12464@hjp.at>

 It could also be done by putting those values in square bracket, if substituted with default values.eg. [0]       4       45        [100]Values within square brackets are default values.


    On Sunday, 23 February, 2020, 04:52:11 pm IST, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:  
 
 On 2020-02-22 16:02:06 -0500, stan wrote:
> I have a case where if a value does not exist, I am going to use a default,
> which is easy with coalesce. But I would like to warn the user that a
> default has been supplied. The default value is reasonable, and could
> actually come from the source table, so I can't just check the value.
> I'd like to do a raise NOTICE, if the default portion of the coalesce fires.

Might I suggest a different approach?

Instead of raising a notice, add an additional column. Something like
this:

wds=> create table mytable (id serial, value int);
CREATE TABLE
Time: 127.124 ms
wds=> insert into mytable (value) values (2), (23), (null), (42), (78);
INSERT 0 5
Time: 48.223 ms
wds=> select * from mytable; 
╔════╤═══════╗
║ id │ value ║
╟────┼───────╢
║  1 │    2 ║
║  2 │    23 ║
║  3 │  (∅) ║
║  4 │    42 ║
║  5 │    78 ║
╚════╧═══════╝
(5 rows)

Time: 0.657 ms
wds=> select id, coalesce(value, 42) as value, value is null as value_was_null
wds-> from mytable;
╔════╤═══════╤════════════════╗
║ id │ value │ value_was_null ║
╟────┼───────┼────────────────╢
║  1 │    2 │ f              ║
║  2 │    23 │ f              ║
║  3 │    42 │ t              ║
║  4 │    42 │ f              ║
║  5 │    78 │ f              ║
╚════╧═══════╧════════════════╝
(5 rows)

Time: 0.247 ms

This lets the user distinguish the real 42 with id 4 from the
substituted 42 with id 3. I think this would be awkward with a notice.
At most you could tell the user "some values were substituted", but not
which ones (at least not if the query can return a large number of
rows).

        hp


-- 
  _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |  | hjp@hjp.at        |    -- Charles Stross, "Creative writing
__/  | http://www.hjp.at/ |      challenge!"  

view thread (12+ messages)  latest in thread

Message-ID: <510218283.7242058.1582521649818@mail.yahoo.com>
Permalink:  ../510218283.7242058.1582521649818@mail.yahoo.com/
Also on:    postgresql.org/message-id/510218283.7242058.1582521649818@mail.yahoo.com

 · 

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: sivapostgres@yahoo.com, hjp-pgsql@hjp.at
  Subject: Re: Can I trigger an action from a coalesce ?
  In-Reply-To: <510218283.7242058.1582521649818@mail.yahoo.com>
  References: <20200222210206.GA17445@panix.com> <20200223112148.GF12464@hjp.at> <510218283.7242058.1582521649818@mail.yahoo.com>

* 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