Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0fe1650

Browse files
Jan WieckJan Wieck
Jan Wieck
authored and
Jan Wieck
committed
Changes pg_trigger and extend pg_rewrite in order to allow triggers and
rules to be defined with different, per session controllable, behaviorsfor replication purposes.This will allow replication systems like Slony-I and, as has been statedon pgsql-hackers, other products to control the firing mechanism oftriggers and rewrite rules without modifying the system catalog directly.The firing mechanisms are controlled by a new superuser-only GUCvariable, session_replication_role, together with a change topg_trigger.tgenabled and a new column pg_rewrite.ev_enabled. Bothcolumns 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 haveany cached query plans. This will prevent changing the session role andaccidentally executing stored procedures or functions that have planscached that expand to the wrong query set due to differences in the rulefiring 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 | DISABLEpsql's \d command as well as pg_dump are extended in a backwardcompatible fashion.Jan
1 parente927f8f commit0fe1650

File tree

24 files changed

+706
-154
lines changed

24 files changed

+706
-154
lines changed

‎doc/src/sgml/catalogs.sgml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.145 2007/02/14 01:58:55 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.146 2007/03/19 23:38:28 wieck Exp $ -->
22
<!--
33
Documentation of the system catalogs, directed toward PostgreSQL developers
44
-->
@@ -3639,6 +3639,20 @@
36393639
</entry>
36403640
</row>
36413641

3642+
<row>
3643+
<entry><structfield>ev_enabled</structfield></entry>
3644+
<entry><type>char</type></entry>
3645+
<entry></entry>
3646+
<entry>
3647+
Controls in which <xref linkend="guc-session-replication-role"> modes
3648+
the rule fires.
3649+
<literal>O</> = rule fires in <quote>origin</> and <quote>local</> modes,
3650+
<literal>D</> = rule is disabled,
3651+
<literal>R</> = rule fires in <quote>replica</> mode,
3652+
<literal>A</> = rule fires always.
3653+
</entry>
3654+
</row>
3655+
36423656
<row>
36433657
<entry><structfield>is_instead</structfield></entry>
36443658
<entry><type>bool</type></entry>
@@ -4178,9 +4192,16 @@
41784192

41794193
<row>
41804194
<entry><structfield>tgenabled</structfield></entry>
4181-
<entry><type>bool</type></entry>
4195+
<entry><type>char</type></entry>
41824196
<entry></entry>
4183-
<entry>True if trigger is enabled</entry>
4197+
<entry>
4198+
Controls in which <xref linkend="guc-session-replication-role"> modes
4199+
the trigger fires.
4200+
<literal>O</> = trigger fires in <quote>origin</> and <quote>local</> modes,
4201+
<literal>D</> = trigger is disabled,
4202+
<literal>R</> = trigger fires in <quote>replica</> mode,
4203+
<literal>A</> = trigger fires always.
4204+
</entry>
41844205
</row>
41854206

41864207
<row>

‎doc/src/sgml/config.sgml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.115 2007/03/06 02:06:12 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.116 2007/03/19 23:38:28 wieck Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -3523,6 +3523,23 @@ SELECT * FROM parent WHERE key = 2400;
35233523
</listitem>
35243524
</varlistentry>
35253525

3526+
<varlistentry id="guc-session-replication-role" xreflabel="session_replication_role">
3527+
<term><varname>session_replication_role</varname> (<type>string</type>)</term>
3528+
<indexterm>
3529+
<primary><varname>session_replication_role</> configuration parameter</primary>
3530+
</indexterm>
3531+
<listitem>
3532+
<para>
3533+
Controls the trigger and rule firing for the current session.
3534+
See <xref linkend="sql-altertable"> for the different options to
3535+
enable or disable triggers and rules. Setting the variable requires
3536+
superuser privilege and can only be done before any query plans have
3537+
been cached. Possible values are <literal>origin</>,
3538+
<literal>replica</> and <literal>local</>.
3539+
</para>
3540+
</listitem>
3541+
</varlistentry>
3542+
35263543
<varlistentry id="guc-vacuum-freeze-min-age" xreflabel="vacuum_freeze_min_age">
35273544
<term><varname>vacuum_freeze_min_age</varname> (<type>integer</type>)</term>
35283545
<indexterm>

‎doc/src/sgml/ref/alter_table.sgml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.94 2007/02/01 00:28:18 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.95 2007/03/19 23:38:29 wieck Exp $
33
PostgreSQL documentation
44
-->
55

@@ -43,6 +43,12 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
4343
DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
4444
DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
4545
ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
46+
ENABLE REPLICA TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
47+
ENABLE ALWAYS TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
48+
DISABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
49+
ENABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
50+
ENABLE REPLICA RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
51+
ENABLE ALWAYS RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
4652
CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
4753
SET WITHOUT CLUSTER
4854
SET WITHOUT OIDS
@@ -193,10 +199,10 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
193199
</varlistentry>
194200

195201
<varlistentry>
196-
<term><literal>DISABLE</literal>/<literal>ENABLE TRIGGER</literal></term>
202+
<term><literal>DISABLE</literal>/<literal>ENABLE[ REPLICA | ALWAYS ]TRIGGER</literal></term>
197203
<listitem>
198204
<para>
199-
These formsdisable or enable trigger(s) belonging to the table.
205+
These formsconfigure the firing of trigger(s) belonging to the table.
200206
A disabled trigger is still known to the system, but is not executed
201207
when its triggering event occurs. For a deferred trigger, the enable
202208
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:
207213
requires superuser privileges; it should be done with caution since
208214
of course the integrity of the constraint cannot be guaranteed if the
209215
triggers are not executed.
216+
The trigger firing mechanism is also affected by the configuration
217+
variable <xref linkend="guc-session-replication-role">. Simply ENABLEd
218+
triggers will fire when the replication role is <quote>origin</>
219+
(the default) or <quote>local</>. Triggers configured ENABLE REPLICA
220+
will only fire if the session is in <quote>replica</> mode and triggers
221+
configured ENABLE ALWAYS will fire regardless of the current replication
222+
mode.
223+
</para>
224+
</listitem>
225+
</varlistentry>
226+
227+
<varlistentry>
228+
<term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] RULE</literal></term>
229+
<listitem>
230+
<para>
231+
These forms configure the firing of rewrite rules belonging to the table.
232+
A disabled rule is still known to the system, but is not applied
233+
during query rewriting. The semantics are as for disabled/enabled
234+
triggers. This configuration is ignored for ON SELECT rules, which
235+
are always applied in order to keep views working even if the current
236+
session is in a non-default replication role.
210237
</para>
211238
</listitem>
212239
</varlistentry>

‎src/backend/commands/tablecmds.c

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.217 2007/03/13 00:33:39 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.218 2007/03/19 23:38:29 wieck Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -53,6 +53,7 @@
5353
#include"parser/parse_relation.h"
5454
#include"parser/parse_type.h"
5555
#include"parser/parser.h"
56+
#include"rewrite/rewriteDefine.h"
5657
#include"rewrite/rewriteHandler.h"
5758
#include"storage/smgr.h"
5859
#include"utils/acl.h"
@@ -253,7 +254,9 @@ static void ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel,
253254
staticvoidATExecSetTableSpace(OidtableOid,OidnewTableSpace);
254255
staticvoidATExecSetRelOptions(Relationrel,List*defList,boolisReset);
255256
staticvoidATExecEnableDisableTrigger(Relationrel,char*trigname,
256-
boolenable,boolskip_system);
257+
charfires_when,boolskip_system);
258+
staticvoidATExecEnableDisableRule(Relationrel,char*rulename,
259+
charfires_when);
257260
staticvoidATExecAddInherit(Relationrel,RangeVar*parent);
258261
staticvoidATExecDropInherit(Relationrel,RangeVar*parent);
259262
staticvoidcopy_relation_data(Relationrel,SMgrRelationdst);
@@ -1955,11 +1958,17 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
19551958
pass=AT_PASS_MISC;
19561959
break;
19571960
caseAT_EnableTrig:/* ENABLE TRIGGER variants */
1961+
caseAT_EnableAlwaysTrig:
1962+
caseAT_EnableReplicaTrig:
19581963
caseAT_EnableTrigAll:
19591964
caseAT_EnableTrigUser:
19601965
caseAT_DisableTrig:/* DISABLE TRIGGER variants */
19611966
caseAT_DisableTrigAll:
19621967
caseAT_DisableTrigUser:
1968+
caseAT_EnableRule:/* ENABLE/DISABLE RULE variants */
1969+
caseAT_EnableAlwaysRule:
1970+
caseAT_EnableReplicaRule:
1971+
caseAT_DisableRule:
19631972
caseAT_AddInherit:/* INHERIT / NO INHERIT */
19641973
caseAT_DropInherit:
19651974
ATSimplePermissions(rel, false);
@@ -2127,24 +2136,57 @@ ATExecCmd(AlteredTableInfo *tab, Relation rel, AlterTableCmd *cmd)
21272136
caseAT_ResetRelOptions:/* RESET (...) */
21282137
ATExecSetRelOptions(rel, (List*)cmd->def, true);
21292138
break;
2130-
caseAT_EnableTrig:/* ENABLE TRIGGER name */
2131-
ATExecEnableDisableTrigger(rel,cmd->name, true, false);
2139+
2140+
caseAT_EnableTrig:/* ENABLE TRIGGER name */
2141+
ATExecEnableDisableTrigger(rel,cmd->name,
2142+
TRIGGER_FIRES_ON_ORIGIN, false);
2143+
break;
2144+
caseAT_EnableAlwaysTrig:/* ENABLE ALWAYS TRIGGER name */
2145+
ATExecEnableDisableTrigger(rel,cmd->name,
2146+
TRIGGER_FIRES_ALWAYS, false);
2147+
break;
2148+
caseAT_EnableReplicaTrig:/* ENABLE REPLICA TRIGGER name */
2149+
ATExecEnableDisableTrigger(rel,cmd->name,
2150+
TRIGGER_FIRES_ON_REPLICA, false);
21322151
break;
21332152
caseAT_DisableTrig:/* DISABLE TRIGGER name */
2134-
ATExecEnableDisableTrigger(rel,cmd->name, false, false);
2153+
ATExecEnableDisableTrigger(rel,cmd->name,
2154+
TRIGGER_DISABLED, false);
21352155
break;
21362156
caseAT_EnableTrigAll:/* ENABLE TRIGGER ALL */
2137-
ATExecEnableDisableTrigger(rel,NULL, true, false);
2157+
ATExecEnableDisableTrigger(rel,NULL,
2158+
TRIGGER_FIRES_ON_ORIGIN, false);
21382159
break;
21392160
caseAT_DisableTrigAll:/* DISABLE TRIGGER ALL */
2140-
ATExecEnableDisableTrigger(rel,NULL, false, false);
2161+
ATExecEnableDisableTrigger(rel,NULL,
2162+
TRIGGER_DISABLED, false);
21412163
break;
21422164
caseAT_EnableTrigUser:/* ENABLE TRIGGER USER */
2143-
ATExecEnableDisableTrigger(rel,NULL, true, true);
2165+
ATExecEnableDisableTrigger(rel,NULL,
2166+
TRIGGER_FIRES_ON_ORIGIN, true);
21442167
break;
21452168
caseAT_DisableTrigUser:/* DISABLE TRIGGER USER */
2146-
ATExecEnableDisableTrigger(rel,NULL, false, true);
2169+
ATExecEnableDisableTrigger(rel,NULL,
2170+
TRIGGER_DISABLED, true);
2171+
break;
2172+
2173+
caseAT_EnableRule:/* ENABLE RULE name */
2174+
ATExecEnableDisableRule(rel,cmd->name,
2175+
RULE_FIRES_ON_ORIGIN);
2176+
break;
2177+
caseAT_EnableAlwaysRule:/* ENABLE ALWAYS RULE name */
2178+
ATExecEnableDisableRule(rel,cmd->name,
2179+
RULE_FIRES_ALWAYS);
2180+
break;
2181+
caseAT_EnableReplicaRule:/* ENABLE REPLICA RULE name */
2182+
ATExecEnableDisableRule(rel,cmd->name,
2183+
RULE_FIRES_ON_REPLICA);
2184+
break;
2185+
caseAT_DisableRule:/* DISABLE RULE name */
2186+
ATExecEnableDisableRule(rel,cmd->name,
2187+
RULE_DISABLED);
21472188
break;
2189+
21482190
caseAT_AddInherit:
21492191
ATExecAddInherit(rel, (RangeVar*)cmd->def);
21502192
break;
@@ -4380,7 +4422,7 @@ validateForeignKeyConstraint(FkConstraint *fkconstraint,
43804422
MemSet(&trig,0,sizeof(trig));
43814423
trig.tgoid=InvalidOid;
43824424
trig.tgname=fkconstraint->constr_name;
4383-
trig.tgenabled=TRUE;
4425+
trig.tgenabled=TRIGGER_FIRES_ON_ORIGIN;
43844426
trig.tgisconstraint= TRUE;
43854427
trig.tgconstrrelid=RelationGetRelid(pkrel);
43864428
trig.tgconstraint=constraintOid;
@@ -5877,9 +5919,21 @@ copy_relation_data(Relation rel, SMgrRelation dst)
58775919
*/
58785920
staticvoid
58795921
ATExecEnableDisableTrigger(Relationrel,char*trigname,
5880-
boolenable,boolskip_system)
5922+
charfires_when,boolskip_system)
5923+
{
5924+
EnableDisableTrigger(rel,trigname,fires_when,skip_system);
5925+
}
5926+
5927+
/*
5928+
* ALTER TABLE ENABLE/DISABLE RULE
5929+
*
5930+
* We just pass this off to rewriteDefine.c.
5931+
*/
5932+
staticvoid
5933+
ATExecEnableDisableRule(Relationrel,char*trigname,
5934+
charfires_when)
58815935
{
5882-
EnableDisableTrigger(rel,trigname,enable,skip_system);
5936+
EnableDisableRule(rel,trigname,fires_when);
58835937
}
58845938

58855939
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp