pgsql: Fix event triggers for partitioned tables

Started by Alvaro Herreraalmost 8 years ago6 messageshackerscomitters
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

never appliedsuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t39461
psql -h localhost -U postgres

Built from patchset v2 (message #2), July 28, 2026 at 01:51 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t39461_2 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t39461_2 && git checkout t39461_2

Patchset v2 (message #2) is on t39461_2

Jump to latest
#1Alvaro Herrera
alvherre@2ndquadrant.com
hackerscomitters

Fix event triggers for partitioned tables

Index DDL cascading on partitioned tables introduced a way for ALTER
TABLE to be called reentrantly. This caused an an important deficiency
in event trigger support to be exposed: on exiting the reentrant call,
the alter table state object was clobbered, causing a crash when the
outer alter table tries to finalize its processing. Fix the crash by
creating a stack of event trigger state objects. There are still ways
to cause things to misbehave (and probably other crashers) with more
elaborate tricks, but at least it now doesn't crash in the obvious
scenario.

Backpatch to 9.5, where DDL deparsing of event triggers was introduced.

Reported-by: Marco Slot
Authors: Michaël Paquier, Álvaro Herrera
Discussion: /messages/by-id/CANNhMLCpi+HQ7M36uPfGbJZEQLyTy7XvX=5EFkpR-b1bo0uJew@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/ad08006ba03797fed431af87de6e66c6c0985b7a

Modified Files
--------------
src/backend/catalog/index.c | 8 +++++++-
src/backend/commands/event_trigger.c | 13 +++++++------
src/backend/commands/indexcmds.c | 3 ++-
src/backend/commands/tablecmds.c | 2 +-
src/backend/commands/view.c | 4 ++++
src/include/catalog/index.h | 3 ++-
src/include/tcop/deparse_utility.h | 3 +++
.../test_ddl_deparse/expected/alter_table.out | 12 ++++++++++++
.../modules/test_ddl_deparse/sql/alter_table.sql | 8 ++++++++
src/test/regress/expected/event_trigger.out | 20 +++++++++++++++++++-
src/test/regress/sql/event_trigger.sql | 13 +++++++++++++
11 files changed, 78 insertions(+), 11 deletions(-)

#2Michael Paquier
michael@paquier.xyz
In reply to: Alvaro Herrera (#1)
hackerscomitters
Re: pgsql: Fix event triggers for partitioned tables

Hi Alvaro,

On Sat, Oct 06, 2018 at 10:18:46PM +0000, Alvaro Herrera wrote:

Fix event triggers for partitioned tables

Index DDL cascading on partitioned tables introduced a way for ALTER
TABLE to be called reentrantly. This caused an an important deficiency
in event trigger support to be exposed: on exiting the reentrant call,
the alter table state object was clobbered, causing a crash when the
outer alter table tries to finalize its processing. Fix the crash by
creating a stack of event trigger state objects. There are still ways
to cause things to misbehave (and probably other crashers) with more
elaborate tricks, but at least it now doesn't crash in the obvious
scenario.

This commit is producing a warning with my compiler:
event_trigger.c:1764:9: note: in expansion of macro ‘OidIsValid’
Assert(OidIsValid(currentEventTriggerState->currentCommand));

The fix is obvious because currentCommand is a pointer and not an Oid.
Please see attached. Should I fix it myself?
--
Michael

Attachments:

t39461_2
event-trigger-warning.patchtext/x-diff; charset=us-asciiDownload+1-1
#3Peter Eisentraut
peter_e@gmx.net
In reply to: Alvaro Herrera (#1)
hackerscomitters
Re: pgsql: Fix event triggers for partitioned tables

On 07/10/2018 00:18, Alvaro Herrera wrote:

Fix event triggers for partitioned tables

It looks like this is causing compiler warnings:

event_trigger.c: In function 'EventTriggerCollectAlterTableSubcmd':
../../../src/include/c.h:605:51: error: comparison between pointer and
zero character constant [-Werror=pointer-compare]
#define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#4Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Michael Paquier (#2)
hackerscomitters
Re: pgsql: Fix event triggers for partitioned tables

On 2018-Oct-08, Michael Paquier wrote:

Hi Alvaro,

On Sat, Oct 06, 2018 at 10:18:46PM +0000, Alvaro Herrera wrote:

Fix event triggers for partitioned tables

Index DDL cascading on partitioned tables introduced a way for ALTER
TABLE to be called reentrantly. This caused an an important deficiency
in event trigger support to be exposed: on exiting the reentrant call,
the alter table state object was clobbered, causing a crash when the
outer alter table tries to finalize its processing. Fix the crash by
creating a stack of event trigger state objects. There are still ways
to cause things to misbehave (and probably other crashers) with more
elaborate tricks, but at least it now doesn't crash in the obvious
scenario.

This commit is producing a warning with my compiler:
event_trigger.c:1764:9: note: in expansion of macro ‘OidIsValid’
Assert(OidIsValid(currentEventTriggerState->currentCommand));

The fix is obvious because currentCommand is a pointer and not an Oid.
Please see attached. Should I fix it myself?

If you have a commit ready, please do, thanks.

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#5Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Alvaro Herrera (#4)
hackerscomitters
Re: pgsql: Fix event triggers for partitioned tables

On 2018-Oct-08, Alvaro Herrera wrote:

On 2018-Oct-08, Michael Paquier wrote:

The fix is obvious because currentCommand is a pointer and not an Oid.
Please see attached. Should I fix it myself?

Pushed now, thanks.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#6Michael Paquier
michael@paquier.xyz
In reply to: Alvaro Herrera (#5)
hackerscomitters
Re: pgsql: Fix event triggers for partitioned tables

On Mon, Oct 08, 2018 at 10:39:23AM -0300, Alvaro Herrera wrote:

Pushed now, thanks.

Thanks Alvaro for addressing the issue and back-patching.
--
Michael