summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorJan Wieck2007-03-19 23:38:32 +0000
committerJan Wieck2007-03-19 23:38:32 +0000
commit0fe16500d3ae68b8928a2b94dcab434e358480d5 (patch)
treef1d9726d5922f2d486d4217cd4b02c4efbb63e5f /src/include
parente927f8f14ee4059e8f582453984d96cbf9490a13 (diff)
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
Diffstat (limited to 'src/include')
-rw-r--r--src/include/catalog/pg_rewrite.h12
-rw-r--r--src/include/catalog/pg_trigger.h5
-rw-r--r--src/include/commands/trigger.h16
-rw-r--r--src/include/nodes/parsenodes.h8
-rw-r--r--src/include/rewrite/prs2lock.h3
-rw-r--r--src/include/rewrite/rewriteDefine.h10
-rw-r--r--src/include/utils/plancache.h3
-rw-r--r--src/include/utils/rel.h4
8 files changed, 46 insertions, 15 deletions
diff --git a/src/include/catalog/pg_rewrite.h b/src/include/catalog/pg_rewrite.h
index 91afb141990..9996a67e407 100644
--- a/src/include/catalog/pg_rewrite.h
+++ b/src/include/catalog/pg_rewrite.h
@@ -11,7 +11,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/catalog/pg_rewrite.h,v 1.27 2007/01/05 22:19:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_rewrite.h,v 1.28 2007/03/19 23:38:31 wieck Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
@@ -42,6 +42,7 @@ CATALOG(pg_rewrite,2618)
Oid ev_class;
int2 ev_attr;
char ev_type;
+ char ev_enabled;
bool is_instead;
/* NB: remaining fields must be accessed via heap_getattr */
@@ -60,13 +61,14 @@ typedef FormData_pg_rewrite *Form_pg_rewrite;
* compiler constants for pg_rewrite
* ----------------
*/
-#define Natts_pg_rewrite 7
+#define Natts_pg_rewrite 8
#define Anum_pg_rewrite_rulename 1
#define Anum_pg_rewrite_ev_class 2
#define Anum_pg_rewrite_ev_attr 3
#define Anum_pg_rewrite_ev_type 4
-#define Anum_pg_rewrite_is_instead 5
-#define Anum_pg_rewrite_ev_qual 6
-#define Anum_pg_rewrite_ev_action 7
+#define Anum_pg_rewrite_ev_enabled 5
+#define Anum_pg_rewrite_is_instead 6
+#define Anum_pg_rewrite_ev_qual 7
+#define Anum_pg_rewrite_ev_action 8
#endif /* PG_REWRITE_H */
diff --git a/src/include/catalog/pg_trigger.h b/src/include/catalog/pg_trigger.h
index df22089e310..b7a71b2f070 100644
--- a/src/include/catalog/pg_trigger.h
+++ b/src/include/catalog/pg_trigger.h
@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/catalog/pg_trigger.h,v 1.27 2007/02/14 01:58:58 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_trigger.h,v 1.28 2007/03/19 23:38:31 wieck Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
@@ -46,7 +46,8 @@ CATALOG(pg_trigger,2620)
Oid tgfoid; /* OID of function to be called */
int2 tgtype; /* BEFORE/AFTER UPDATE/DELETE/INSERT
* ROW/STATEMENT; see below */
- bool tgenabled; /* trigger is enabled/disabled */
+ char tgenabled; /* trigger's firing configuration
+ * WRT session_replication_role */
bool tgisconstraint; /* trigger is a constraint trigger */
NameData tgconstrname; /* constraint name */
Oid tgconstrrelid; /* constraint's FROM table, if any */
diff --git a/src/include/commands/trigger.h b/src/include/commands/trigger.h
index ac9eb7296d1..8fcd0bb6aa7 100644
--- a/src/include/commands/trigger.h
+++ b/src/include/commands/trigger.h
@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/commands/trigger.h,v 1.61 2007/02/14 01:58:58 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/commands/trigger.h,v 1.62 2007/03/19 23:38:31 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -78,6 +78,18 @@ typedef struct TriggerData
#define TRIGGER_FIRED_AFTER(event) \
(!TRIGGER_FIRED_BEFORE (event))
+/*
+ * Definitions for the replication role based firing.
+ */
+#define SESSION_REPLICATION_ROLE_ORIGIN 0
+#define SESSION_REPLICATION_ROLE_REPLICA 1
+#define SESSION_REPLICATION_ROLE_LOCAL 2
+extern int SessionReplicationRole;
+
+#define TRIGGER_FIRES_ON_ORIGIN 'O'
+#define TRIGGER_FIRES_ALWAYS 'A'
+#define TRIGGER_FIRES_ON_REPLICA 'R'
+#define TRIGGER_DISABLED 'D'
extern Oid CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid);
@@ -88,7 +100,7 @@ extern void RemoveTriggerById(Oid trigOid);
extern void renametrig(Oid relid, const char *oldname, const char *newname);
extern void EnableDisableTrigger(Relation rel, const char *tgname,
- bool enable, bool skip_system);
+ char fires_when, bool skip_system);
extern void RelationBuildTriggers(Relation relation);
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index e24b57e8a23..c3a2bebca68 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.342 2007/03/13 00:33:43 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.343 2007/03/19 23:38:32 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -897,11 +897,17 @@ typedef enum AlterTableType
AT_SetRelOptions, /* SET (...) -- AM specific parameters */
AT_ResetRelOptions, /* RESET (...) -- AM specific parameters */
AT_EnableTrig, /* ENABLE TRIGGER name */
+ AT_EnableAlwaysTrig, /* ENABLE ALWAYS TRIGGER name */
+ AT_EnableReplicaTrig, /* ENABLE REPLICA TRIGGER name */
AT_DisableTrig, /* DISABLE TRIGGER name */
AT_EnableTrigAll, /* ENABLE TRIGGER ALL */
AT_DisableTrigAll, /* DISABLE TRIGGER ALL */
AT_EnableTrigUser, /* ENABLE TRIGGER USER */
AT_DisableTrigUser, /* DISABLE TRIGGER USER */
+ AT_EnableRule, /* ENABLE RULE name */
+ AT_EnableAlwaysRule, /* ENABLE ALWAYS RULE name */
+ AT_EnableReplicaRule, /* ENABLE REPLICA RULE name */
+ AT_DisableRule, /* DISABLE RULE name */
AT_AddInherit, /* INHERIT parent */
AT_DropInherit /* NO INHERIT parent */
} AlterTableType;
diff --git a/src/include/rewrite/prs2lock.h b/src/include/rewrite/prs2lock.h
index d7ebbbe6bc1..17d045053e6 100644
--- a/src/include/rewrite/prs2lock.h
+++ b/src/include/rewrite/prs2lock.h
@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/rewrite/prs2lock.h,v 1.22 2007/01/05 22:19:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/rewrite/prs2lock.h,v 1.23 2007/03/19 23:38:32 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -28,6 +28,7 @@ typedef struct RewriteRule
AttrNumber attrno;
Node *qual;
List *actions;
+ char enabled;
bool isInstead;
} RewriteRule;
diff --git a/src/include/rewrite/rewriteDefine.h b/src/include/rewrite/rewriteDefine.h
index d4673f82a6c..5b21cf8e797 100644
--- a/src/include/rewrite/rewriteDefine.h
+++ b/src/include/rewrite/rewriteDefine.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/rewrite/rewriteDefine.h,v 1.24 2007/03/13 00:33:43 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/rewrite/rewriteDefine.h,v 1.25 2007/03/19 23:38:32 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,6 +16,11 @@
#include "nodes/parsenodes.h"
+#define RULE_FIRES_ON_ORIGIN 'O'
+#define RULE_FIRES_ALWAYS 'A'
+#define RULE_FIRES_ON_REPLICA 'R'
+#define RULE_DISABLED 'D'
+
extern void DefineRule(RuleStmt *stmt, const char *queryString);
extern void DefineQueryRewrite(char *rulename,
@@ -31,4 +36,7 @@ extern void RenameRewriteRule(Oid owningRel, const char *oldName,
extern void setRuleCheckAsUser(Node *node, Oid userid);
+extern void EnableDisableRule(Relation rel, const char *rulename,
+ char fires_when);
+
#endif /* REWRITEDEFINE_H */
diff --git a/src/include/utils/plancache.h b/src/include/utils/plancache.h
index 4f03cd9e0a2..91101a318d7 100644
--- a/src/include/utils/plancache.h
+++ b/src/include/utils/plancache.h
@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/utils/plancache.h,v 1.2 2007/03/15 23:12:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/plancache.h,v 1.3 2007/03/19 23:38:32 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -102,5 +102,6 @@ extern CachedPlan *RevalidateCachedPlan(CachedPlanSource *plansource,
bool useResOwner);
extern void ReleaseCachedPlan(CachedPlan *plan, bool useResOwner);
extern TupleDesc PlanCacheComputeResultDesc(List *stmt_list);
+extern bool HaveCachedPlans(void);
#endif /* PLANCACHE_H */
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index 4065eced821..2963cc6616a 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.98 2007/02/27 23:48:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.99 2007/03/19 23:38:32 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -53,7 +53,7 @@ typedef struct Trigger
char *tgname;
Oid tgfoid;
int16 tgtype;
- bool tgenabled;
+ char tgenabled;
bool tgisconstraint;
Oid tgconstrrelid;
Oid tgconstraint;