Re: Support TRUNCATE triggers on foreign tables - Mailing list pgsql-hackers

From Ian Lawrence Barwick
Subject Re: Support TRUNCATE triggers on foreign tables
Date
Msg-id CAB8KJ=jprX71GsRy3ypcMU27muifEWe9LzoeK=Ua6Td0cQb2cg@mail.gmail.com
Whole thread Raw
In response to Re: Support TRUNCATE triggers on foreign tables  (Fujii Masao <masao.fujii@oss.nttdata.com>)
Responses Re: Support TRUNCATE triggers on foreign tables
List pgsql-hackers
2022年7月8日(金) 14:06 Fujii Masao <masao.fujii@oss.nttdata.com>:
> On 2022/07/08 11:19, Yugo NAGATA wrote:
> >> You added "foreign tables" for BEFORE statement-level trigger as the above, but ISTM that you also needs to do
thatfor AFTER statement-level trigger. No? 
> >
> > Oops, I forgot it. I attached the updated patch.
>
> Thanks for updating the patch! LGTM.
> Barring any objection, I will commit the patch.

An observation: as-is the patch would make it possible to create a truncate
trigger for a foreign table whose FDW doesn't support truncation, which seems
somewhat pointless, possible source of confusion etc.:

    postgres=# CREATE TRIGGER ft_trigger
      AFTER TRUNCATE ON fb_foo
      EXECUTE FUNCTION fb_foo_trg();
    CREATE TRIGGER

    postgres=# TRUNCATE fb_foo;
    ERROR:  cannot truncate foreign table "fb_foo"

It would be easy enough to check for this, e.g.:

    else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
    {
        FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);

        if (!fdwroutine->ExecForeignTruncate)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("foreign data wrapper does not support
table truncation")));
        ...

which results in:

    postgres=# CREATE TRIGGER ft_trigger
      AFTER TRUNCATE ON fb_foo
      EXECUTE FUNCTION fb_foo_trg();
    ERROR:  foreign data wrapper does not support table truncation

which IMO is preferable to silently accepting DDL which will never
actually do anything.


Regards

Ian Barwick



pgsql-hackers by date:

Previous
From: Masahiko Sawada
Date:
Subject: Re: [BUG] Logical replication failure "ERROR: could not map filenode "base/13237/442428" to relation OID" with catalog modifying txns
Next
From: Etsuro Fujita
Date:
Subject: Re: Add a test for "cannot truncate foreign table"