1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

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 <tabname> <when> TRIGGER|RULE <name>;

     <when> ::= ENABLE | ENABLE ALWAYS | ENABLE REPLICA | DISABLE

psql's \d command as well as pg_dump are extended in a backward
compatible fashion.

Jan
This commit is contained in:
Jan Wieck
2007-03-19 23:38:32 +00:00
parent e927f8f14e
commit 0fe16500d3
24 changed files with 700 additions and 148 deletions

View File

@ -1,4 +1,4 @@
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.145 2007/02/14 01:58:55 tgl Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.146 2007/03/19 23:38:28 wieck Exp $ -->
<!--
Documentation of the system catalogs, directed toward PostgreSQL developers
-->
@ -3639,6 +3639,20 @@
</entry>
</row>
<row>
<entry><structfield>ev_enabled</structfield></entry>
<entry><type>char</type></entry>
<entry></entry>
<entry>
Controls in which <xref linkend="guc-session-replication-role"> modes
the rule fires.
<literal>O</> = rule fires in <quote>origin</> and <quote>local</> modes,
<literal>D</> = rule is disabled,
<literal>R</> = rule fires in <quote>replica</> mode,
<literal>A</> = rule fires always.
</entry>
</row>
<row>
<entry><structfield>is_instead</structfield></entry>
<entry><type>bool</type></entry>
@ -4178,9 +4192,16 @@
<row>
<entry><structfield>tgenabled</structfield></entry>
<entry><type>bool</type></entry>
<entry><type>char</type></entry>
<entry></entry>
<entry>True if trigger is enabled</entry>
<entry>
Controls in which <xref linkend="guc-session-replication-role"> modes
the trigger fires.
<literal>O</> = trigger fires in <quote>origin</> and <quote>local</> modes,
<literal>D</> = trigger is disabled,
<literal>R</> = trigger fires in <quote>replica</> mode,
<literal>A</> = trigger fires always.
</entry>
</row>
<row>

View File

@ -1,4 +1,4 @@
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.115 2007/03/06 02:06:12 momjian Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.116 2007/03/19 23:38:28 wieck Exp $ -->
<chapter Id="runtime-config">
<title>Server Configuration</title>
@ -3523,6 +3523,23 @@ SELECT * FROM parent WHERE key = 2400;
</listitem>
</varlistentry>
<varlistentry id="guc-session-replication-role" xreflabel="session_replication_role">
<term><varname>session_replication_role</varname> (<type>string</type>)</term>
<indexterm>
<primary><varname>session_replication_role</> configuration parameter</primary>
</indexterm>
<listitem>
<para>
Controls the trigger and rule firing for the current session.
See <xref linkend="sql-altertable"> 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 <literal>origin</>,
<literal>replica</> and <literal>local</>.
</para>
</listitem>
</varlistentry>
<varlistentry id="guc-vacuum-freeze-min-age" xreflabel="vacuum_freeze_min_age">
<term><varname>vacuum_freeze_min_age</varname> (<type>integer</type>)</term>
<indexterm>

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.94 2007/02/01 00:28:18 momjian Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.95 2007/03/19 23:38:29 wieck Exp $
PostgreSQL documentation
-->
@ -43,6 +43,12 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
ENABLE REPLICA TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
ENABLE ALWAYS TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
DISABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
ENABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
ENABLE REPLICA RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
ENABLE ALWAYS RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
SET WITHOUT CLUSTER
SET WITHOUT OIDS
@ -193,10 +199,10 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
</varlistentry>
<varlistentry>
<term><literal>DISABLE</literal>/<literal>ENABLE TRIGGER</literal></term>
<term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] TRIGGER</literal></term>
<listitem>
<para>
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 <replaceable class="PARAMETER">action</replaceable> 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 <xref linkend="guc-session-replication-role">. Simply ENABLEd
triggers will fire when the replication role is <quote>origin</>
(the default) or <quote>local</>. Triggers configured ENABLE REPLICA
will only fire if the session is in <quote>replica</> mode and triggers
configured ENABLE ALWAYS will fire regardless of the current replication
mode.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] RULE</literal></term>
<listitem>
<para>
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.
</para>
</listitem>
</varlistentry>