From: Alexander Lakhin <exclusion@gmail.com>
To: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17798: Incorrect memory access occurs when using BEFORE ROW UPDATE trigger
Date: Sat, 1 Apr 2023 15:00:00 +0300
Message-ID: <950f4f1a-fb71-3e45-bb65-6a57da9f9b9e@gmail.com> (raw)
In-Reply-To: <87fe6c28-80b2-f709-a8db-d105e5240e39@gmail.com>
References: <17798-0907404928dcf0dd@postgresql.org>
<87fe6c28-80b2-f709-a8db-d105e5240e39@gmail.com>
07.03.2023 09:00, Alexander Lakhin wrote:
> I've tried to materialize newslot before the oldslot materialization
> (in ExecFetchSlotHeapTuple(), where their common memory is released),
> and it looks like it fixes the issue.
I've made a simple isolation test to illustrate the bug, which I'd consider
as serious. On master it shows (under valgrind):
# using temp instance on port 61696 with PID 614130
not ok 1 - bru-trigger 2147 ms
# (test process exited with exit code 1)
src/test/isolation/output_iso/log/postmaster.log contains:
...
==00:00:00:05.840 615284== Invalid read of size 1
==00:00:00:05.840 615284== at 0x1E376C: heap_compute_data_size (heaptuple.c:147)
==00:00:00:05.840 615284== by 0x1E4458: heap_form_tuple (heaptuple.c:1061)
==00:00:00:05.840 615284== by 0x3DB74A: tts_buffer_heap_materialize (execTuples.c:749)
==00:00:00:05.840 615284== by 0x3DC5EB: ExecFetchSlotHeapTuple (execTuples.c:1655)
==00:00:00:05.840 615284== by 0x3A6BA7: ExecBRUpdateTriggers (trigger.c:3032)
==00:00:00:05.840 615284== by 0x3FE207: ExecUpdatePrologue (nodeModifyTable.c:1916)
==00:00:00:05.840 615284== by 0x3FF838: ExecUpdate (nodeModifyTable.c:2289)
==00:00:00:05.840 615284== by 0x401BD4: ExecModifyTable (nodeModifyTable.c:3795)
==00:00:00:05.840 615284== by 0x3D65FF: ExecProcNodeFirst (execProcnode.c:464)
==00:00:00:05.840 615284== by 0x3CE4F5: ExecProcNode (executor.h:272)
==00:00:00:05.840 615284== by 0x3CE585: ExecutePlan (execMain.c:1633)
==00:00:00:05.840 615284== by 0x3CF220: standard_ExecutorRun (execMain.c:364)
...
2023-04-01 14:26:31.543 MSK postmaster[615243] LOG: server process (PID 615284) exited with exit code 1
2023-04-01 14:26:31.543 MSK postmaster[615243] DETAIL: Failed process was running: UPDATE bruttest SET cnt = cnt + 1;
Maybe the test could supplement a fix (I'm still unsure how to fix the issue
right way).
Best regards,
Alexander
Attachments:BRU-trigger-bug-demo.patch (text/x-patch, 1.5K) downloaddiff --git a/src/test/isolation/expected/bru-trigger.out b/src/test/isolation/expected/bru-trigger.outnew file mode 100644index 0000000000..edae1c5832--- /dev/null+++ b/src/test/isolation/expected/bru-trigger.out@@ -0,0 +1,8 @@+Parsed test spec with 2 sessions++starting permutation: s1b s1u s2u s1c+step s1b: BEGIN;+step s1u: UPDATE bruttest SET cnt = cnt + 1;+step s2u: UPDATE bruttest SET cnt = cnt + 1; <waiting ...>+step s1c: COMMIT;+step s2u: <... completed>diff --git a/src/test/isolation/specs/bru-trigger.spec b/src/test/isolation/specs/bru-trigger.specnew file mode 100644index 0000000000..9d49b25174--- /dev/null+++ b/src/test/isolation/specs/bru-trigger.spec@@ -0,0 +1,32 @@+# Before Row Update Trigger test+#+# Test concurrent access to a row in a new buffer from within BEFORE UPDATE trigger++setup+{+ CREATE TABLE bruttest(cnt INTEGER DEFAULT 0, t TEXT);+ ALTER TABLE bruttest ALTER COLUMN t SET STORAGE plain;+ CREATE FUNCTION noop_tfunc() RETURNS TRIGGER LANGUAGE plpgsql AS $body$+ BEGIN+ RETURN NEW;+ END+ $body$;+ CREATE TRIGGER brupdate_trigger BEFORE UPDATE ON bruttest FOR EACH ROW EXECUTE FUNCTION noop_tfunc();+ INSERT INTO bruttest(t) VALUES (repeat('x', 5000));+}++teardown+{+ DROP TABLE bruttest;+ DROP FUNCTION noop_tfunc();+}++session s1+step s1b { BEGIN; }+step s1u { UPDATE bruttest SET cnt = cnt + 1; }+step s1c { COMMIT; }++session s2+step s2u { UPDATE bruttest SET cnt = cnt + 1; }++permutation s1b s1u s2u s1c
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 using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17798: Incorrect memory access occurs when using BEFORE ROW UPDATE trigger
In-Reply-To: <950f4f1a-fb71-3e45-bb65-6a57da9f9b9e@gmail.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