pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: paul.coyne@diridium.com
To: pgsql-bugs@postgresql.org
Subject: BUG #14609: ON CONSTRAINT (aka UPSERT) code fails when excluded.<columname> used in calculation.
Date: Fri, 31 Mar 2017 21:10:11 +0000
Message-ID: <20170331211011.2690.61348@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14609
Logged by:          Paul Coyne
Email address:      paul.coyne@diridium.com
PostgreSQL version: 9.5.6
Operating system:   Ubuntu 14.04 (and later)
Description:        

Table:


CREATE TABLE public.test
(
  id bigint NOT NULL DEFAULT nextval('test_id_seq'::regclass),
  patient_id bigint,
  site_code character varying,
  mrn character varying,
  sendingfacility character varying(50),
  CONSTRAINT test_pkey PRIMARY KEY (id),
  CONSTRAINT test_site_code_mrn_key UNIQUE (site_code, mrn)
)
WITH (
  OIDS=FALSE
);


CREATE INDEX index_test_on_patient_id
  ON public.test
  USING btree
  (patient_id);

CREATE UNIQUE INDEX index_test_on_site_code_and_mrn
  ON public.test
  USING btree
  (site_code COLLATE pg_catalog."default", mrn COLLATE
pg_catalog."default");




SP:


CREATE OR REPLACE FUNCTION public.sp_test_upsert_with_coalesce(
    p_sendingfacility character varying DEFAULT NULL::character varying,
    p_site_code character varying DEFAULT NULL::character varying,
    p_mrn character varying DEFAULT NULL::character varying)
  RETURNS bigint AS
$BODY$
 /*  BEGIN  */
		 INSERT INTO test(
		             sendingfacility,
		             site_code,
			     mrn
							   )
		 VALUES                (
		                                          NULLIF(p_sendingfacility,''),
		                                          p_site_code,
							   p_mrn
		                        )
	   ON CONFLICT ON CONSTRAINT test_site_code_mrn_key
           DO UPDATE
            SET sendingfacility =
NULLIF(COALESCE(NULLIF(p_sendingfacility,''),excluded.sendingfacility),'""')
            WHERE excluded.mrn =  p_mrn and excluded.site_code = p_site_code
RETURNING id;
 /*  END;  */
   $BODY$
  LANGUAGE sql VOLATILE
  COST 100;

SQL to Run:

select sp_test_upsert_with_coalesce('ABC','1','DEF')    
select * from test   -- returns 1,,1,DEF,ABC
select sp_test_upsert_with_coalesce('DEF','1','DEF')
select * from test  -- returns 1,,1,DEF,DEF
select sp_test_upsert_with_coalesce('','1','DEF')   
select * from test  -- returns 1,,1,DEF,,    -- ERROR: blank parameter
should not be written to DB based on SP
                                                                   -- if
'excluded.sendingfacility' is changed to test.sendingfacility, the code
works

Can excluded.variablename be used in a calculation?   It appears to have the
value NULL in the formula.
 
The actual code applies to healthcare HL7 messages where by most fields
follow this logic:

If I sent you a value for variable A, store it.
If I send you an empty string for variable A, keep what I sent you before
If I send you "", clear out the variable A
If I send you a new value for for variable A, store it. 







-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


view thread (4+ messages)  latest in thread

Message-ID: <20170331211011.2690.61348@wrigleys.postgresql.org>
Permalink:  ../20170331211011.2690.61348@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170331211011.2690.61348@wrigleys.postgresql.org

 · 

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-bugs@postgresql.org
  Cc: paul.coyne@diridium.com
  Subject: Re: BUG #14609: ON CONSTRAINT (aka UPSERT) code fails when excluded.<columname> used in calculation.
  In-Reply-To: <20170331211011.2690.61348@wrigleys.postgresql.org>
  References: <20170331211011.2690.61348@wrigleys.postgresql.org>

* 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