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 1gfN9q-0007Sg-6s for pgsql-general@arkaria.postgresql.org; Fri, 04 Jan 2019 10:56:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gfN9o-0000Uo-Td for pgsql-general@arkaria.postgresql.org; Fri, 04 Jan 2019 10:56:52 +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 1gfN9o-0000Uh-DP for pgsql-general@lists.postgresql.org; Fri, 04 Jan 2019 10:56:52 +0000 Received: from aragorn.net-wizard.org ([138.201.127.29] helo=gandalf.net-wizard.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfN9k-0005mC-L9 for pgsql-general@postgresql.org; Fri, 04 Jan 2019 10:56:51 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id AB1A880E51; Fri, 4 Jan 2019 11:56:43 +0100 (CET) X-Virus-Scanned: Gentoo amavisd-new at aragorn Received: from aragorn.net-wizard.org ([127.0.0.1]) by localhost (aragorn.net-wizard.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CS9SXztWgDui; Fri, 4 Jan 2019 11:56:33 +0100 (CET) Received: from MTECKOF043 (mtec-lee-dock-3-233.ethz.ch [82.130.89.233]) (Authenticated sender: clavadetscher@swisspug.org) by gandalf.net-wizard.org (Postfix) with ESMTPA id 791FB80E07; Fri, 4 Jan 2019 11:56:29 +0100 (CET) From: "Charles Clavadetscher" To: "'Kristjan Tammekivi'" , References: In-Reply-To: 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> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_03AE_01D4A424.897919E0" X-Mailer: Microsoft Outlook 16.0 Content-Language: de-ch Thread-Index: AQH1AtFwvGb7yPHZAulpkO1LZlPE6aVeMYNg List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_03AE_01D4A424.897919E0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello =20 From: Kristjan Tammekivi =20 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 =20 Hi, =20 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: =20 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(); =20 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 =20 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. =20 I doubt that this works on any PG version for INSERT. =20 According to the documentation: =20 https://www.postgresql.org/docs/10/plpgsql-trigger.html and = https://www.postgresql.org/docs/11/plpgsql-trigger.html =20 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. =20 Regards Charles ------=_NextPart_000_03AE_01D4A424.897919E0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Hello

 

From:<= /b> 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.p= ostgresql.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

------=_NextPart_000_03AE_01D4A424.897919E0--