From c84c614c7ac841cf61e4ffadd28f7631ffbd0ebd Mon Sep 17 00:00:00 2001 From: jnasbyupgrade Date: Sun, 12 Jul 2026 18:29:38 -0500 Subject: [PATCH] Don't clobber caller's client_min_messages on install Replace the capture/restore with a transaction-local guard that only raises the level and reverts automatically. Co-Authored-By: Claude Opus 4.8 (1M context) --- sql/extension_drop--1.0.0.sql | 40 +++++++++++++++++++++++++++++------ sql/extension_drop.sql | 40 +++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/sql/extension_drop--1.0.0.sql b/sql/extension_drop--1.0.0.sql index 558d6a4..fea7e82 100644 --- a/sql/extension_drop--1.0.0.sql +++ b/sql/extension_drop--1.0.0.sql @@ -7,8 +7,33 @@ */ CREATE SCHEMA __extension_drop; -CREATE TABLE __extension_drop.messages AS SELECT pg_catalog.current_setting('client_min_messages'); -SET client_min_messages = WARNING; +/* + * Quiet the NOTICEs this install script emits without overriding a caller who + * asked for even less output, and without leaking the change past install -- + * CREATE EXTENSION may run inside a larger transaction. Save the caller's + * client_min_messages, raise it to WARNING only when the caller's level is more + * verbose than WARNING, and restore it at the end of this script. A plain SET + * (not SET LOCAL) is used because we restore explicitly; it is rolled back + * automatically if the install fails. + */ +DO $$ +DECLARE + -- client_min_messages levels ordered least-to-most severe (LOG sorts between + -- DEBUG1 and NOTICE for this GUC). + c_levels CONSTANT text[] := ARRAY[ + 'debug5', 'debug4', 'debug3', 'debug2', 'debug1' + , 'log', 'notice', 'warning', 'error' + ]; + v_current CONSTANT text := pg_catalog.current_setting('client_min_messages'); +BEGIN + PERFORM pg_catalog.set_config('extension_drop.client_min_messages', v_current, true); + IF pg_catalog.array_position(c_levels, pg_catalog.lower(v_current)) + < pg_catalog.array_position(c_levels, 'warning') + THEN + SET client_min_messages = warning; + END IF; +END +$$; CREATE FUNCTION __extension_drop.exec( sql text @@ -302,10 +327,6 @@ CREATE EVENT TRIGGER extension_drop /* * Drop "temporary" objects */ -SELECT __extension_drop.exec('SET client_min_messages = ' || current_setting) - FROM __extension_drop.messages -; -DROP TABLE __extension_drop.messages; DROP FUNCTION __extension_drop.create_function( function_name text , args text @@ -323,4 +344,11 @@ DROP FUNCTION __extension_drop.exec( ); DROP SCHEMA __extension_drop; +/* Restore the client_min_messages the caller had before this script (saved at the top). */ +SELECT pg_catalog.set_config( + 'client_min_messages' + , pg_catalog.current_setting('extension_drop.client_min_messages') + , false +); + -- vim: sw=2 ts=2 expandtab diff --git a/sql/extension_drop.sql b/sql/extension_drop.sql index d3b549d..63455e4 100644 --- a/sql/extension_drop.sql +++ b/sql/extension_drop.sql @@ -6,8 +6,33 @@ */ CREATE SCHEMA __extension_drop; -CREATE TABLE __extension_drop.messages AS SELECT pg_catalog.current_setting('client_min_messages'); -SET client_min_messages = WARNING; +/* + * Quiet the NOTICEs this install script emits without overriding a caller who + * asked for even less output, and without leaking the change past install -- + * CREATE EXTENSION may run inside a larger transaction. Save the caller's + * client_min_messages, raise it to WARNING only when the caller's level is more + * verbose than WARNING, and restore it at the end of this script. A plain SET + * (not SET LOCAL) is used because we restore explicitly; it is rolled back + * automatically if the install fails. + */ +DO $$ +DECLARE + -- client_min_messages levels ordered least-to-most severe (LOG sorts between + -- DEBUG1 and NOTICE for this GUC). + c_levels CONSTANT text[] := ARRAY[ + 'debug5', 'debug4', 'debug3', 'debug2', 'debug1' + , 'log', 'notice', 'warning', 'error' + ]; + v_current CONSTANT text := pg_catalog.current_setting('client_min_messages'); +BEGIN + PERFORM pg_catalog.set_config('extension_drop.client_min_messages', v_current, true); + IF pg_catalog.array_position(c_levels, pg_catalog.lower(v_current)) + < pg_catalog.array_position(c_levels, 'warning') + THEN + SET client_min_messages = warning; + END IF; +END +$$; CREATE FUNCTION __extension_drop.exec( sql text @@ -301,10 +326,6 @@ CREATE EVENT TRIGGER extension_drop /* * Drop "temporary" objects */ -SELECT __extension_drop.exec('SET client_min_messages = ' || current_setting) - FROM __extension_drop.messages -; -DROP TABLE __extension_drop.messages; DROP FUNCTION __extension_drop.create_function( function_name text , args text @@ -322,4 +343,11 @@ DROP FUNCTION __extension_drop.exec( ); DROP SCHEMA __extension_drop; +/* Restore the client_min_messages the caller had before this script (saved at the top). */ +SELECT pg_catalog.set_config( + 'client_min_messages' + , pg_catalog.current_setting('extension_drop.client_min_messages') + , false +); + -- vim: sw=2 ts=2 expandtab