From 0fe16500d3ae68b8928a2b94dcab434e358480d5 Mon Sep 17 00:00:00 2001 From: Jan Wieck Date: Mon, 19 Mar 2007 23:38:32 +0000 Subject: Changes pg_trigger and extend pg_rewrite in order to allow triggers and rules to be defined with different, per session controllable, behaviors for replication purposes. This will allow replication systems like Slony-I and, as has been stated on pgsql-hackers, other products to control the firing mechanism of triggers and rewrite rules without modifying the system catalog directly. The firing mechanisms are controlled by a new superuser-only GUC variable, session_replication_role, together with a change to pg_trigger.tgenabled and a new column pg_rewrite.ev_enabled. Both columns are a single char data type now (tgenabled was a bool before). The possible values in these attributes are: 'O' - Trigger/Rule fires when session_replication_role is "origin" (default) or "local". This is the default behavior. 'D' - Trigger/Rule is disabled and fires never 'A' - Trigger/Rule fires always regardless of the setting of session_replication_role 'R' - Trigger/Rule fires when session_replication_role is "replica" The GUC variable can only be changed as long as the system does not have any cached query plans. This will prevent changing the session role and accidentally executing stored procedures or functions that have plans cached that expand to the wrong query set due to differences in the rule firing semantics. The SQL syntax for changing a triggers/rules firing semantics is ALTER TABLE TRIGGER|RULE ; ::= ENABLE | ENABLE ALWAYS | ENABLE REPLICA | DISABLE psql's \d command as well as pg_dump are extended in a backward compatible fashion. Jan --- doc/src/sgml/catalogs.sgml | 27 ++++++++++++++++++++++++--- doc/src/sgml/config.sgml | 19 ++++++++++++++++++- doc/src/sgml/ref/alter_table.sgml | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 7 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index 9a9f9b55bef..31db6521c5e 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -1,4 +1,4 @@ - + @@ -3639,6 +3639,20 @@ + + ev_enabled + char + + + Controls in which modes + the rule fires. + O = rule fires in origin and local modes, + D = rule is disabled, + R = rule fires in replica mode, + A = rule fires always. + + + is_instead bool @@ -4178,9 +4192,16 @@ tgenabled - bool + char - True if trigger is enabled + + Controls in which modes + the trigger fires. + O = trigger fires in origin and local modes, + D = trigger is disabled, + R = trigger fires in replica mode, + A = trigger fires always. + diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 17504b3d5e8..1fe39db2af7 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1,4 +1,4 @@ - + Server Configuration @@ -3523,6 +3523,23 @@ SELECT * FROM parent WHERE key = 2400; + + session_replication_role (string) + + session_replication_role configuration parameter + + + + Controls the trigger and rule firing for the current session. + See for the different options to + enable or disable triggers and rules. Setting the variable requires + superuser privilege and can only be done before any query plans have + been cached. Possible values are origin, + replica and local. + + + + vacuum_freeze_min_age (integer) diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index 6ff289b21a4..9b34c4fb993 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -1,5 +1,5 @@ @@ -43,6 +43,12 @@ where action is one of: DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ] DISABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE TRIGGER [ trigger_name | ALL | USER ] + ENABLE REPLICA TRIGGER trigger_name + ENABLE ALWAYS TRIGGER trigger_name + DISABLE RULE rewrite_rule_name + ENABLE RULE rewrite_rule_name + ENABLE REPLICA RULE rewrite_rule_name + ENABLE ALWAYS RULE rewrite_rule_name CLUSTER ON index_name SET WITHOUT CLUSTER SET WITHOUT OIDS @@ -193,10 +199,10 @@ where action is one of: - DISABLE/ENABLE TRIGGER + DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER - These forms disable or enable trigger(s) belonging to the table. + These forms configure the firing of trigger(s) belonging to the table. A disabled trigger is still known to the system, but is not executed when its triggering event occurs. For a deferred trigger, the enable status is checked when the event occurs, not when the trigger function @@ -207,6 +213,27 @@ where action is one of: requires superuser privileges; it should be done with caution since of course the integrity of the constraint cannot be guaranteed if the triggers are not executed. + The trigger firing mechanism is also affected by the configuration + variable . Simply ENABLEd + triggers will fire when the replication role is origin + (the default) or local. Triggers configured ENABLE REPLICA + will only fire if the session is in replica mode and triggers + configured ENABLE ALWAYS will fire regardless of the current replication + mode. + + + + + + DISABLE/ENABLE [ REPLICA | ALWAYS ] RULE + + + These forms configure the firing of rewrite rules belonging to the table. + A disabled rule is still known to the system, but is not applied + during query rewriting. The semantics are as for disabled/enabled + triggers. This configuration is ignored for ON SELECT rules, which + are always applied in order to keep views working even if the current + session is in a non-default replication role. -- cgit v1.2.3