table-write trigger can bypass ATPrepChangePersistence

Поиск
Список
Период
Сортировка
Искать

table-write trigger can bypass ATPrepChangePersistence

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
Hi hackers,

While working on other projects, I found an issue $SUBJECT. Below describes the
exact problem, reproducer, and fix idea.

Problem
======
Unlogged tables cannot be included and excluded in a publication. When SET UNLOGGED
command is executed, validations are done in ATPrepChangePersistence() and the
backend raises an ERROR. However, rewrite-table event trigger can be fired after
the validation, and publication commands can be run at that time. Such commands
would bypass the validation thus unlogged tables could be in the pg_publication_rel.

Reproducer
=======
Below SQL commands could reproduce the inconsistency, unlogged table could be
excluded in the publication. Same thing can be said for the inclusion case.

```
postgres=# CREATE TABLE t (a int);
CREATE TABLE
postgres=# CREATE FUNCTION add_exclusion_during_rewrite()
RETURNS event_trigger
LANGUAGE plpgsql AS $$
BEGIN
    IF pg_event_trigger_table_rewrite_oid() = 'public.t'::regclass THEN
        EXECUTE
            'CREATE PUBLICATION p FOR ALL TABLES EXCEPT (TABLE public.t)';
    END IF;
END;
$$;
CREATE FUNCTION
postgres=# CREATE EVENT TRIGGER add_exclusion
    ON table_rewrite
    EXECUTE FUNCTION add_exclusion_during_rewrite();
CREATE EVENT TRIGGER
postgres=# ALTER TABLE t SET UNLOGGED;
ALTER TABLE
postgres=# SELECT c.relpersistence, pr.prexcept
FROM pg_class AS c
JOIN pg_publication_rel AS pr ON pr.prrelid = c.oid
WHERE c.oid = 'public.t'::regclass;
 relpersistence | prexcept
----------------+----------
 u              | t
(1 row)
```

Fix idea
=====
My primitive idea is to re-validate just after an event trigger is fired. Attached
Patch implemented the idea accordingly. How do you feel?

How do you feel?
Best regards,
Hayato Kuroda
FUJITSU LIMITED

Re: table-write trigger can bypass ATPrepChangePersistence

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Thu, Sep 17, 2026 at 2:49 PM Hayato Kuroda (Fujitsu)
 wrote:
>
> Hi hackers,
>
> While working on other projects, I found an issue $SUBJECT. Below describes the
> exact problem, reproducer, and fix idea.
>
> Problem
> ======
> Unlogged tables cannot be included and excluded in a publication. When SET UNLOGGED
> command is executed, validations are done in ATPrepChangePersistence() and the
> backend raises an ERROR. However, rewrite-table event trigger can be fired after
> the validation, and publication commands can be run at that time. Such commands
> would bypass the validation thus unlogged tables could be in the pg_publication_rel.
>

Hello Kuroda-san,

Thanks for the patch!
While testing the patch, I found a similar issue in the related area
which this patch can fix with a small change.

Here is the problem. While ATRewriteTables() rewrites a table, it
evaluates DEFAULT expressions for each row. A DEFAULT expression can
call a function. That function can run DDL. This DDL can run after
your recheck. So it can still bypass the check.

For example:

CREATE TABLE pub_rewrite (a int);
INSERT INTO pub_rewrite VALUES (1);

CREATE PUBLICATION pub;

CREATE FUNCTION add_to_pub_during_rewrite() RETURNS int
LANGUAGE plpgsql VOLATILE AS $$
BEGIN
  EXECUTE 'ALTER PUBLICATION pub ADD TABLE pub_rewrite';
  RETURN 1;
END;
$$;

-- Should fail, but succeeds
ALTER TABLE pub_rewrite
  SET UNLOGGED,
  ADD COLUMN b int DEFAULT add_to_pub_during_rewrite();

SELECT c.relpersistence, pr.prexcept
  FROM pg_class c JOIN pg_publication_rel pr ON pr.prrelid = c.oid
  WHERE c.oid = 'pub_rewrite'::regclass;
--  relpersistence | prexcept
-- ----------------+----------
--  u              | f
-- (1 row)

Now for a possible fix. Your patch puts the recheck right after the
event trigger fires. That is inside the per-table loop in
ATRewriteTables(). The DEFAULT expression runs later in that same
loop, during the actual rewrite of that table in ATRewriteTable(). So
the recheck happens too early for this case.

A better place is after the whole loop finishes. This means every
table has already been rewritten. All DEFAULT and CHECK functions have
already run. Only then do we check persistence again, for every table
in the list.

regards,
Ajin Cherian
Fujitsu Australia


Re: table-write trigger can bypass ATPrepChangePersistence

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
Вложения:
On Fri, Sep 18, 2026 at 4:43 PM Ajin Cherian  wrote:
>
> Now for a possible fix. Your patch puts the recheck right after the
> event trigger fires. That is inside the per-table loop in
> ATRewriteTables(). The DEFAULT expression runs later in that same
> loop, during the actual rewrite of that table in ATRewriteTable(). So
> the recheck happens too early for this case.
>
> A better place is after the whole loop finishes. This means every
> table has already been rewritten. All DEFAULT and CHECK functions have
> already run. Only then do we check persistence again, for every table
> in the list.
>

Here's a patch with this change incorporated.

regards,
Ajin Cherian
Fujitsu Australia

RE: table-write trigger can bypass ATPrepChangePersistence

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
> Problem
> ======

One pointed I did not clarify is that FK can also be affected the issue. For now
normal tables cannot refer the UNLOGGED tables, but the table-rewrite event trigger
can break the rule. In below a normal table rewrite_to_logged referred a unlogged
table rewrite_referenced, which should not be accepted.
The patch proposed here could fix the issue.

```
postgres=# CREATE TABLE rewrite_referenced (a int PRIMARY KEY);
CREATE TABLE
postgres=# CREATE UNLOGGED TABLE rewrite_to_logged (a int REFERENCES rewrite_referenced);
CREATE TABLE
postgres=# CREATE FUNCTION test_evtrig_set_unlogged() RETURNS event_trigger
LANGUAGE plpgsql AS $$
BEGIN
  IF pg_event_trigger_table_rewrite_oid() = 'rewrite_to_logged'::regclass THEN
    EXECUTE 'ALTER TABLE rewrite_referenced SET UNLOGGED';
  END IF;
END;
$$;
CREATE FUNCTION
postgres=# CREATE EVENT TRIGGER set_unlogged_during_rewrite ON table_rewrite
  WHEN TAG IN ('ALTER TABLE')
  EXECUTE FUNCTION test_evtrig_set_unlogged();
CREATE EVENT TRIGGER
postgres=# ALTER TABLE rewrite_to_logged SET LOGGED;
ALTER TABLE
postgres=# \d rewrite_to_logged
         Table "public.rewrite_to_logged"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 
Foreign-key constraints:
    "rewrite_to_logged_a_fkey" FOREIGN KEY (a) REFERENCES rewrite_referenced(a)

postgres=# \d rewrite_referenced
    Unlogged table "public.rewrite_referenced"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           | not null | 
Indexes:
    "rewrite_referenced_pkey" PRIMARY KEY, btree (a)
Referenced by:
    TABLE "rewrite_to_logged" CONSTRAINT "rewrite_to_logged_a_fkey" FOREIGN KEY (a) REFERENCES rewrite_referenced(a)
```

Best regards,
Hayato Kuroda
FUJITSU LIMITED



Re: table-write trigger can bypass ATPrepChangePersistence

От:
Shlok Kyal <shlok.kyal.oss@gmail.com>
Дата:
On Fri, 18 Sept 2026 at 13:02, Ajin Cherian  wrote:
>
> On Fri, Sep 18, 2026 at 4:43 PM Ajin Cherian  wrote:
> >
> > Now for a possible fix. Your patch puts the recheck right after the
> > event trigger fires. That is inside the per-table loop in
> > ATRewriteTables(). The DEFAULT expression runs later in that same
> > loop, during the actual rewrite of that table in ATRewriteTable(). So
> > the recheck happens too early for this case.
> >
> > A better place is after the whole loop finishes. This means every
> > table has already been rewritten. All DEFAULT and CHECK functions have
> > already run. Only then do we check persistence again, for every table
> > in the list.
> >
>
> Here's a patch with this change incorporated.
>
Hi Kuroda-san, Ajin,

I reviewed the v2 patch. Here are some comments:
1. There is a trailing whitespace error:
Applying: Recheck table persistence after table_rewrite triggers
.git/rebase-apply/patch:28: trailing whitespace.
         */
warning: 1 line adds whitespace errors.

2. Do we need to acquire an AccessShareLock here?
+ATRewriteChangePersistence(AlteredTableInfo *tab)
+{
+       Relation        rel;
+       bool            toLogged;
+
+       rel = table_open(tab->relid, AccessShareLock);
+       toLogged = (tab->newrelpersistence == RELPERSISTENCE_PERMANENT);

ALTER TABLE ... SET LOGGED/UNLOGGED already acquires and retains an
AccessExclusiveLock on the target relation:
lockmode = AlterTableGetLockLevel(atstmt->cmds);
relid = AlterTableLookupRelation(atstmt, lockmode);
Here, lockmode is AccessExclusiveLock.

Should we use NoLock instead of AccessShareLock?  This would also be
consistent with the other phase 3 relation opens in ATRewriteTables().

3. Should we also add tests for DDL executed by DEFAULT/CHECK expressions to
event_trigger.sql?

Thanks,
Shlok Kyal


Re: table-write trigger can bypass ATPrepChangePersistence

От:
Ajin Cherian <itsajin@gmail.com>
Дата:
Вложения:
On Mon, Sep 21, 2026 at 8:46 PM Shlok Kyal  wrote:
>
> > Here's a patch with this change incorporated.
> >
> Hi Kuroda-san, Ajin,
>
> I reviewed the v2 patch. Here are some comments:

Thanks for your comments, Shlok. Here's an updated patch with these
comments addressed as well as some minor changes.

regards,
Ajin Cherian
Fujitsu Australia
FAQ