v31-0001-Provide-a-GUC-for-temporarily-ignoring-event-tri.patch
application/octet-stream
Filename: v31-0001-Provide-a-GUC-for-temporarily-ignoring-event-tri.patch
Type: application/octet-stream
Part: 1
Message:
Re: On login trigger: take three
Patch
Format: format-patch
Series: patch v31-0001
Subject: Provide a GUC for temporarily ignoring event triggers
| File | + | − |
|---|---|---|
| doc/src/sgml/config.sgml | 15 | 0 |
| doc/src/sgml/ref/create_event_trigger.sgml | 3 | 1 |
| src/backend/commands/event_trigger.c | 33 | 7 |
| src/backend/utils/misc/guc.c | 17 | 0 |
| src/include/commands/event_trigger.h | 8 | 0 |
From fdf34f4485f1c1944ce77351f72124cb83cf06d6 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Fri, 2 Sep 2022 14:34:10 +0200
Subject: [PATCH v31 1/2] Provide a GUC for temporarily ignoring event triggers
In order to allow troubleshooting and repair without the need for
restarting in single-user mode, allow to ignore event triggers per
session.
---
doc/src/sgml/config.sgml | 15 ++++++++
doc/src/sgml/ref/create_event_trigger.sgml | 4 ++-
src/backend/commands/event_trigger.c | 40 ++++++++++++++++++----
src/backend/utils/misc/guc.c | 17 +++++++++
src/include/commands/event_trigger.h | 8 +++++
5 files changed, 76 insertions(+), 8 deletions(-)
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index a5cd4e44c7..58818d8e66 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -9383,6 +9383,21 @@ SET XML OPTION { DOCUMENT | CONTENT };
</listitem>
</varlistentry>
+ <varlistentry id="guc-ignore-event-trigger" xreflabel="ignore_event_trigger">
+ <term><varname>ignore_event_trigger</varname> (<type>enum</type>)
+ <indexterm>
+ <primary><varname>ignore_event_trigger</varname></primary>
+ <secondary>configuration parameter</secondary>
+ </indexterm>
+ </term>
+ <listitem>
+ <para>
+ Allows to temporarily disable event triggers from executing in order
+ to troubleshoot and repair faulty event triggers.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
</sect2>
<sect2 id="runtime-config-client-format">
diff --git a/doc/src/sgml/ref/create_event_trigger.sgml b/doc/src/sgml/ref/create_event_trigger.sgml
index 22c8119198..f6922c3de3 100644
--- a/doc/src/sgml/ref/create_event_trigger.sgml
+++ b/doc/src/sgml/ref/create_event_trigger.sgml
@@ -123,7 +123,9 @@ CREATE EVENT TRIGGER <replaceable class="parameter">name</replaceable>
Event triggers are disabled in single-user mode (see <xref
linkend="app-postgres"/>). If an erroneous event trigger disables the
database so much that you can't even drop the trigger, restart in
- single-user mode and you'll be able to do that.
+ single-user mode and you'll be able to do that. Event triggers can also be
+ temporarily disabled for such troubleshooting, see
+ <xref linkend="guc-ignore-event-trigger"/>.
</para>
</refsect1>
diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c
index 635d05405e..85d01c559e 100644
--- a/src/backend/commands/event_trigger.c
+++ b/src/backend/commands/event_trigger.c
@@ -72,6 +72,9 @@ typedef struct EventTriggerQueryState
static EventTriggerQueryState *currentEventTriggerState = NULL;
+/* GUC parameter */
+int ignore_event_trigger = IGNORE_EVENT_TRIGGER_NONE;
+
/* Support for dropped objects */
typedef struct SQLDropObject
{
@@ -100,6 +103,7 @@ static void validate_table_rewrite_tags(const char *filtervar, List *taglist);
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata);
static const char *stringify_grant_objtype(ObjectType objtype);
static const char *stringify_adefprivs_objtype(ObjectType objtype);
+static bool ignore_event_trigger_check(EventTriggerEvent event);
/*
* Create an event trigger.
@@ -657,8 +661,11 @@ EventTriggerDDLCommandStart(Node *parsetree)
* wherein event triggers are disabled. (Or we could implement
* heapscan-and-sort logic for that case, but having disaster recovery
* scenarios depend on code that's otherwise untested isn't appetizing.)
+ *
+ * Additionally, event triggers can be disabled with a superuser only GUC
+ * to make fixing database easier as per 1 above.
*/
- if (!IsUnderPostmaster)
+ if (!IsUnderPostmaster || ignore_event_trigger_check(EVT_DDLCommandStart))
return;
runlist = EventTriggerCommonSetup(parsetree,
@@ -692,9 +699,9 @@ EventTriggerDDLCommandEnd(Node *parsetree)
/*
* See EventTriggerDDLCommandStart for a discussion about why event
- * triggers are disabled in single user mode.
+ * triggers are disabled in single user mode or via GUC.
*/
- if (!IsUnderPostmaster)
+ if (!IsUnderPostmaster || ignore_event_trigger_check(EVT_DDLCommandEnd))
return;
/*
@@ -740,9 +747,9 @@ EventTriggerSQLDrop(Node *parsetree)
/*
* See EventTriggerDDLCommandStart for a discussion about why event
- * triggers are disabled in single user mode.
+ * triggers are disabled in single user mode or via a GUC.
*/
- if (!IsUnderPostmaster)
+ if (!IsUnderPostmaster || ignore_event_trigger_check(EVT_SQLDrop))
return;
/*
@@ -811,9 +818,9 @@ EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason)
/*
* See EventTriggerDDLCommandStart for a discussion about why event
- * triggers are disabled in single user mode.
+ * triggers are disabled in single user mode or via a GUC.
*/
- if (!IsUnderPostmaster)
+ if (!IsUnderPostmaster || ignore_event_trigger_check(EVT_TableRewrite))
return;
/*
@@ -2175,3 +2182,22 @@ stringify_adefprivs_objtype(ObjectType objtype)
return "???"; /* keep compiler quiet */
}
+
+
+/*
+ * Checks whether the specified event is ignored by the ignore_event_trigger
+ * GUC or not. Currently, the GUC only supports ignoreing all or nothing but
+ * that will most likely change so the function takes an event to aid that.
+ */
+static bool
+ignore_event_trigger_check(EventTriggerEvent event)
+{
+ (void) event; /* unused parameter */
+
+ if (ignore_event_trigger == IGNORE_EVENT_TRIGGER_ALL)
+ return true;
+
+ /* IGNORE_EVENT_TRIGGER_NONE */
+ return false;
+
+}
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 9fbbfb1be5..cb688f6789 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -49,6 +49,7 @@
#include "catalog/pg_parameter_acl.h"
#include "catalog/storage.h"
#include "commands/async.h"
+#include "commands/event_trigger.h"
#include "commands/prepare.h"
#include "commands/tablespace.h"
#include "commands/trigger.h"
@@ -596,6 +597,12 @@ static const struct config_enum_entry wal_compression_options[] = {
{NULL, 0, false}
};
+static const struct config_enum_entry ignore_event_trigger_options[] = {
+ {"none", IGNORE_EVENT_TRIGGER_NONE, false},
+ {"all", IGNORE_EVENT_TRIGGER_ALL, false},
+ {NULL, 0, false}
+};
+
/*
* Options for enum values stored in other modules
*/
@@ -4969,6 +4976,16 @@ static struct config_enum ConfigureNamesEnum[] =
NULL, NULL, NULL
},
+ {
+ {"ignore_event_trigger", PGC_SUSET, CLIENT_CONN_STATEMENT,
+ gettext_noop("Disable event triggers during the session."),
+ NULL
+ },
+ &ignore_event_trigger,
+ IGNORE_EVENT_TRIGGER_NONE, ignore_event_trigger_options,
+ NULL, NULL, NULL
+ },
+
{
{"wal_level", PGC_POSTMASTER, WAL_SETTINGS,
gettext_noop("Sets the level of information written to the WAL."),
diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h
index 10091c3aaf..9e7671e63f 100644
--- a/src/include/commands/event_trigger.h
+++ b/src/include/commands/event_trigger.h
@@ -29,6 +29,14 @@ typedef struct EventTriggerData
CommandTag tag;
} EventTriggerData;
+typedef enum ignore_event_trigger_events
+{
+ IGNORE_EVENT_TRIGGER_NONE,
+ IGNORE_EVENT_TRIGGER_ALL
+} IgnoreEventTriggersEvents;
+
+extern int ignore_event_trigger;
+
#define AT_REWRITE_ALTER_PERSISTENCE 0x01
#define AT_REWRITE_DEFAULT_VAL 0x02
#define AT_REWRITE_COLUMN_REWRITE 0x04
--
2.32.1 (Apple Git-133)