pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Charles Clavadetscher <clavadetscher@swisspug.org>
To: 'Kristjan Tammekivi' <kristjantammekivi@gmail.com>
To: pgsql-general@postgresql.org
Subject: RE: Potentially undocumented behaviour change in Postgres 11 concerning OLD record in an after insert trigger
Date: Fri, 4 Jan 2019 11:56:22 +0100
Message-ID: <03ad01d4a41c$27b240e0$7716c2a0$@swisspug.org> (raw)
In-Reply-To: <CAABK7uL-uC9ZxKBXzo_68pKt7cECfNRv+c35CXZpjq6jCAzYYA@mail.gmail.com>
References: <CAABK7uL-uC9ZxKBXzo_68pKt7cECfNRv+c35CXZpjq6jCAzYYA@mail.gmail.com>

Hello

 

From: Kristjan Tammekivi <kristjantammekivi@gmail.com> 
Sent: Freitag, 4. Januar 2019 11:46
To: pgsql-general@postgresql.org
Subject: Potentially undocumented behaviour change in Postgres 11 concerning OLD record in an after insert trigger

 

Hi,

 

I've noticed a change in the behaviour in triggers / hstores in Postgres 11.1 when compared to Postgres 10.5.

The following won't work on Postgres 10.5 but in Postgres 11.1 it works just fine:

 

CREATE EXTENSION hstore;

CREATE TABLE _tmp_test1 (id serial PRIMARY KEY, val INTEGER);
CREATE TABLE _tmp_test1_changes (id INTEGER, changes HSTORE);

CREATE FUNCTION test1_trigger ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$BODY$
BEGIN
INSERT INTO _tmp_test1_changes (id, changes) VALUES (NEW.id, hstore(OLD) - hstore(NEW));
RETURN NEW;
END
$BODY$;

CREATE TRIGGER table_update AFTER INSERT OR UPDATE ON _tmp_test1
FOR EACH ROW EXECUTE PROCEDURE test1_trigger();

 

INSERT INTO _tmp_test1 (val) VALUES (5);

ERROR:  record "old" is not assigned yet

DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.

CONTEXT:  SQL statement "INSERT INTO _tmp_test1_changes (id, changes) VALUES (NEW.id, hstore(OLD) - hstore(NEW))"

PL/pgSQL function test1_trigger() line 3 at SQL statement

 

I couldn't find anything about this in the release notes (https://www.postgresql.org/docs/11/release-11.html), but maybe I just didn't know what to look for.

 

I doubt that this works on any PG version for INSERT.

 

According to the documentation:

 

https://www.postgresql.org/docs/10/plpgsql-trigger.html and https://www.postgresql.org/docs/11/plpgsql-trigger.html

 

OLD: Data type RECORD; variable holding the old database row for UPDATE/DELETE operations in row-level triggers. This variable is unassigned in statement-level triggers and for INSERT operations.

 

Regards

Charles

view thread (6+ messages)  latest in thread

Message-ID: <03ad01d4a41c$27b240e0$7716c2a0$@swisspug.org>
Permalink:  ../03ad01d4a41c$27b240e0$7716c2a0$@swisspug.org/
Also on:    postgresql.org/message-id/03ad01d4a41c$27b240e0$7716c2a0$@swisspug.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-general@postgresql.org
  Cc: clavadetscher@swisspug.org, kristjantammekivi@gmail.com
  Subject: Re: Potentially undocumented behaviour change in Postgres 11 concerning OLD record in an after insert trigger
  In-Reply-To: <03ad01d4a41c$27b240e0$7716c2a0$@swisspug.org>
  References: <CAABK7uL-uC9ZxKBXzo_68pKt7cECfNRv+c35CXZpjq6jCAzYYA@mail.gmail.com> <03ad01d4a41c$27b240e0$7716c2a0$@swisspug.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