Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j5pKh-0002cr-NA for pgsql-general@arkaria.postgresql.org; Sun, 23 Feb 2020 11:21:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j5pKg-0003JY-EK for pgsql-general@arkaria.postgresql.org; Sun, 23 Feb 2020 11:21:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j5pKf-0003GW-Vt for pgsql-general@lists.postgresql.org; Sun, 23 Feb 2020 11:21:58 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j5pKa-0004fP-4j for pgsql-general@lists.postgresql.org; Sun, 23 Feb 2020 11:21:55 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 1A11C254E; Sun, 23 Feb 2020 12:21:48 +0100 (CET) Date: Sun, 23 Feb 2020 12:21:48 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Can I trigger an action from a coalesce ? Message-ID: <20200223112148.GF12464@hjp.at> References: <20200222210206.GA17445@panix.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="eNMatiwYGLtwo1cJ" Content-Disposition: inline In-Reply-To: <20200222210206.GA17445@panix.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --eNMatiwYGLtwo1cJ Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 defaul= t, > 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 fir= es. Might I suggest a different approach? Instead of raising a notice, add an additional column. Something like this: wds=3D> create table mytable (id serial, value int); CREATE TABLE Time: 127.124 ms wds=3D> insert into mytable (value) values (2), (23), (null), (42), (78); INSERT 0 5 Time: 48.223 ms wds=3D> select * from mytable;=20 =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=97 =E2=95=91 id =E2=94=82 value =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=95=A2 =E2=95=91 1 =E2=94=82 2 =E2=95=91 =E2=95=91 2 =E2=94=82 23 =E2=95=91 =E2=95=91 3 =E2=94=82 (=E2=88=85) =E2=95=91 =E2=95=91 4 =E2=94=82 42 =E2=95=91 =E2=95=91 5 =E2=94=82 78 =E2=95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=9D (5 rows) Time: 0.657 ms wds=3D> select id, coalesce(value, 42) as value, value is null as value_was= _null wds-> from mytable; =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=97 =E2=95=91 id =E2=94=82 value =E2=94=82 value_was_null =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=95=A2 =E2=95=91 1 =E2=94=82 2 =E2=94=82 f =E2=95=91 =E2=95=91 2 =E2=94=82 23 =E2=94=82 f =E2=95=91 =E2=95=91 3 =E2=94=82 42 =E2=94=82 t =E2=95=91 =E2=95=91 4 =E2=94=82 42 =E2=94=82 f =E2=95=91 =E2=95=91 5 =E2=94=82 78 =E2=94=82 f =E2=95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=9D (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 --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --eNMatiwYGLtwo1cJ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl5SYEcACgkQ8g5IURL+ KF0/Rw//bMWpIIPac2cEGijmxox3nV3D93/xJJem0m3dfaeKi8Za+XpVYv/r0FOO +LOCHNYsWdBfIX9RFadUGIcK91RMmx5NsDT4iyBU1mHtjewXL96Cs/x9YWiFtzxK XeSZuRS247cUSSPh3lcaSt25EOoiHbfUtmgEhPmyYP4coHpGa2sYkVy0Jf8CXvOk jW25DhteU5o7KLDnhaG06Ut5YTi6mEU4fjt/phkoEBxI5tfRDTs/X11YLi96Dxj9 bnCmt23qKDc5bIKLBfKKhaQy2K+yUu8/eplwXIU1WFtx3Xg5VJ48Dmxnl83w3mWO 9BWPaqPRRjWMrJLRmjOJgNLpZHsppXyYGEi3TZ5meOaid3LYzoKqQxJtfuSG4hZ+ bA1ycPcOYtI38UpggcUuv6DO5bQIRDLrMeHOZ9npnAm/hNBFLJGtGUfVcCxVZjDS rRStPlwZwAn/zk/Ix6aJBsmX6RyVX/CRzmpcvDFj/AXbBT9UGzOJdLIYo96WtIZT RSYy8abqq+Rk90sSd502VFylRijoQ20S+Es4qoV4tCothKBBb3g29tGakqD/kEnR E/NQiEX83ETrOZTWaZsUSFeXMOa878p67SS4eJX4Z46aiLEMlUT09d1VK3vrMez+ 4eBeqiIqyfqtHM/hUaJ+p2KzP2NFpQH6ObjjdUxzY0Mc97lgN4Q= =S1i9 -----END PGP SIGNATURE----- --eNMatiwYGLtwo1cJ--