mirror of
https://github.com/postgres/postgres.git
synced 2025-07-17 06:41:09 +03:00
Fix SQL-spec incompatibilities in new transition table feature.
The standard says that all changes of the same kind (insert, update, or
delete) caused in one table by a single SQL statement should be reported
in a single transition table; and by that, they mean to include foreign key
enforcement actions cascading from the statement's direct effects. It's
also reasonable to conclude that if the standard had wCTEs, they would say
that effects of wCTEs applying to the same table as each other or the outer
statement should be merged into one transition table. We weren't doing it
like that.
Hence, arrange to merge tuples from multiple update actions into a single
transition table as much as we can. There is a problem, which is that if
the firing of FK enforcement triggers and after-row triggers with
transition tables is interspersed, we might need to report more tuples
after some triggers have already seen the transition table. It seems like
a bad idea for the transition table to be mutable between trigger calls.
There's no good way around this without a major redesign of the FK logic,
so for now, resolve it by opening a new transition table each time this
happens.
Also, ensure that AFTER STATEMENT triggers fire just once per statement,
or once per transition table when we're forced to make more than one.
Previous versions of Postgres have allowed each FK enforcement query
to cause an additional firing of the AFTER STATEMENT triggers for the
referencing table, but that's certainly not per spec. (We're still
doing multiple firings of BEFORE STATEMENT triggers, though; is that
something worth changing?)
Also, forbid using transition tables with column-specific UPDATE triggers.
The spec requires such transition tables to show only the tuples for which
the UPDATE trigger would have fired, which means maintaining multiple
transition tables or else somehow filtering the contents at readout.
Maybe someday we'll bother to support that option, but it looks like a
lot of trouble for a marginal feature.
The transition tables are now managed by the AfterTriggers data structures,
rather than being directly the responsibility of ModifyTable nodes. This
removes a subtransaction-lifespan memory leak introduced by my previous
band-aid patch 3c4359521
.
In passing, refactor the AfterTriggers data structures to reduce the
management overhead for them, by using arrays of structs rather than
several parallel arrays for per-query-level and per-subtransaction state.
I failed to resist the temptation to do some copy-editing on the SGML
docs about triggers, above and beyond merely documenting the effects
of this patch.
Back-patch to v10, because we don't want the semantics of transition
tables to change post-release.
Patch by me, with help and review from Thomas Munro.
Discussion: https://postgr.es/m/20170909064853.25630.12825@wrigleys.postgresql.org
This commit is contained in:
@ -43,13 +43,21 @@ typedef struct TriggerData
|
||||
|
||||
/*
|
||||
* The state for capturing old and new tuples into transition tables for a
|
||||
* single ModifyTable node.
|
||||
* single ModifyTable node (or other operation source, e.g. copy.c).
|
||||
*
|
||||
* This is per-caller to avoid conflicts in setting tcs_map or
|
||||
* tcs_original_insert_tuple. Note, however, that the pointed-to
|
||||
* private data may be shared across multiple callers.
|
||||
*/
|
||||
struct AfterTriggersTableData; /* private in trigger.c */
|
||||
|
||||
typedef struct TransitionCaptureState
|
||||
{
|
||||
/*
|
||||
* Is there at least one trigger specifying each transition relation on
|
||||
* the relation explicitly named in the DML statement or COPY command?
|
||||
* Note: in current usage, these flags could be part of the private state,
|
||||
* but it seems possibly useful to let callers see them.
|
||||
*/
|
||||
bool tcs_delete_old_table;
|
||||
bool tcs_update_old_table;
|
||||
@ -60,7 +68,7 @@ typedef struct TransitionCaptureState
|
||||
* For UPDATE and DELETE, AfterTriggerSaveEvent may need to convert the
|
||||
* new and old tuples from a child table's format to the format of the
|
||||
* relation named in a query so that it is compatible with the transition
|
||||
* tuplestores.
|
||||
* tuplestores. The caller must store the conversion map here if so.
|
||||
*/
|
||||
TupleConversionMap *tcs_map;
|
||||
|
||||
@ -74,17 +82,9 @@ typedef struct TransitionCaptureState
|
||||
HeapTuple tcs_original_insert_tuple;
|
||||
|
||||
/*
|
||||
* The tuplestores backing the transition tables. We use separate
|
||||
* tuplestores for INSERT and UPDATE, because INSERT ... ON CONFLICT ...
|
||||
* DO UPDATE causes INSERT and UPDATE triggers to fire and needs a way to
|
||||
* keep track of the new tuple images resulting from the two cases
|
||||
* separately. We only need a single old image tuplestore, because there
|
||||
* is no statement that can both update and delete at the same time.
|
||||
* Private data including the tuplestore(s) into which to insert tuples.
|
||||
*/
|
||||
Tuplestorestate *tcs_old_tuplestore; /* for DELETE and UPDATE old
|
||||
* images */
|
||||
Tuplestorestate *tcs_insert_tuplestore; /* for INSERT new images */
|
||||
Tuplestorestate *tcs_update_tuplestore; /* for UPDATE new images */
|
||||
struct AfterTriggersTableData *tcs_private;
|
||||
} TransitionCaptureState;
|
||||
|
||||
/*
|
||||
@ -174,8 +174,9 @@ extern void RelationBuildTriggers(Relation relation);
|
||||
extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
|
||||
|
||||
extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
|
||||
extern TransitionCaptureState *MakeTransitionCaptureState(TriggerDesc *trigdesc);
|
||||
extern void DestroyTransitionCaptureState(TransitionCaptureState *tcs);
|
||||
|
||||
extern TransitionCaptureState *MakeTransitionCaptureState(TriggerDesc *trigdesc,
|
||||
Oid relid, CmdType cmdType);
|
||||
|
||||
extern void FreeTriggerDesc(TriggerDesc *trigdesc);
|
||||
|
||||
|
Reference in New Issue
Block a user