Stream triggers v5
PGD introduces new types of triggers that you can use for additional data processing on the downstream/target node:
- Conflict triggers
- Transform triggers
Together, these types of triggers are known as stream triggers.
Stream triggers are designed to be trigger-like in syntax. They leverage the PostgreSQL BEFORE trigger architecture and are likely to have similar performance characteristics as PostgreSQL BEFORE Triggers.
Multiple trigger definitions can use one trigger function, just as with
normal PostgreSQL triggers.
A trigger function is a program defined in this form:
CREATE FUNCTION ... RETURNS TRIGGER
. Creating the trigger doesn't
require use of the CREATE TRIGGER
command. Instead, create stream triggers
using the special PGD functions
bdr.create_conflict_trigger()
and bdr.create_transform_trigger()
.
Once created, the trigger is visible in the catalog table pg_trigger
.
The stream triggers are marked as tgisinternal = true
and
tgenabled = 'D'
and have the name suffix '_bdrc' or '_bdrt'. The view
bdr.triggers
provides information on the triggers in relation to the table,
the name of the procedure that's being executed, the event that triggers it,
and the trigger type.
Stream triggers aren't enabled for normal SQL processing.
Because of this, the ALTER TABLE ... ENABLE TRIGGER
is blocked for stream
triggers in both its specific name variant and the ALL variant. This mechanism prevents
the trigger from executing as a normal SQL trigger.
These triggers execute on the downstream or target node. There's no
option for them to execute on the origin node. However, you might want to consider
the use of row_filter
expressions on the origin.
Also, any DML that's applied while executing a stream trigger isn't replicated to other PGD nodes and doesn't trigger the execution of standard local triggers. This is intentional. You can use it, for example, to log changes or conflicts captured by a stream trigger into a table that is crash-safe and specific to that node. See Stream triggers examples for a working example.
Trigger execution during Apply
Transform triggers execute first