Subject: [PATCH 2/2] Expose GUC flags in SQL function; retire ./check_guc

From: Justin Pryzby <[email-masked]>
Date: Wed, 8 Dec 2021 12:06:35 -0600

---
 src/backend/catalog/system_views.sql |  19 ++++-
 src/backend/utils/misc/check_guc     |  29 -------
 src/backend/utils/misc/guc.c         |  37 +++++++-
 src/include/catalog/pg_proc.dat      |   6 +-
 src/test/regress/expected/guc.out    | 122 +++++++++++++++++++++++++++
 src/test/regress/expected/rules.out  |   2 +-
 src/test/regress/sql/guc.sql         |  72 ++++++++++++++++
 7 files changed, 252 insertions(+), 35 deletions(-)
 delete mode 100755 src/backend/utils/misc/check_guc

diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 3cb69b1f87b..cdae0616bdd 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -582,7 +582,24 @@ FROM
     JOIN pg_authid rol ON l.classoid = rol.tableoid AND l.objoid = rol.oid;
 
 CREATE VIEW pg_settings AS
-    SELECT * FROM pg_show_all_settings() AS A;
+    SELECT a.name,
+    a.setting,
+    a.unit,
+    a.category,
+    a.short_desc,
+    a.extra_desc,
+    a.context,
+    a.vartype,
+    a.source,
+    a.min_val,
+    a.max_val,
+    a.enumvals,
+    a.boot_val,
+    a.reset_val,
+    a.sourcefile,
+    a.sourceline,
+    a.pending_restart
+    FROM pg_show_all_settings() AS A;
 
 CREATE RULE pg_settings_u AS
     ON UPDATE TO pg_settings
diff --git a/src/backend/utils/misc/check_guc b/src/backend/utils/misc/check_guc
deleted file mode 100755
index 323ca13191b..00000000000
--- a/src/backend/utils/misc/check_guc
+++ /dev/null
@@ -1,29 +0,0 @@
-#! /bin/sh
-set -e
-
-## this script makes some assumptions about the formatting of files it parses.
-## in postgresql.conf.sample:
-##   1) the valid config settings may be preceded by a '#', but NOT '# '
-##      (we use this to skip comments)
-
-### What options are listed in postgresql.conf.sample, but don't appear
-### in guc.c?
-
-# grab everything that looks like a setting
-SETTINGS=`sed '/^#[[:alnum:]]/!d; s/^#//; s/ =.*//; /^include/d' postgresql.conf.sample`
-
-for i in $SETTINGS ; do
-  ## it sure would be nice to replace this with an sql "not in" statement
-  grep -i "\"$i\"" guc.c >/dev/null ||
-    echo "$i seems to be missing from guc.c";
-done
-
-### What options are listed in guc.c, but don't appear
-### in postgresql.conf.sample?
-
-# grab everything that looks like a setting and convert it to lower case
-SETTINGS=`gawk -F '[",]' 'BEGIN{RS="\n\t\\\\{\n"} /",[[:space:]]*PGC_.*.*gettext_noop/ && !/NOT_IN_SAMPLE/{print tolower($2)}' guc.c`
-for i in $SETTINGS ; do
-  grep "#$i " postgresql.conf.sample >/dev/null ||
-    echo "$i seems to be missing from postgresql.conf.sample";
-done
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 4c94f09c645..2cb84d68610 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -9634,6 +9634,36 @@ GetConfigOptionByName(const char *name, const char **varname, bool missing_ok)
 	return _ShowOption(record, true);
 }
 
+/*
+ * Return an text[] array of the given flags (or a useful subset?) XXX
+ */
+static char *
+get_flags_array_text(int flags)
+{
+	StringInfoData ret;
+
+	initStringInfo(&ret);
+	appendStringInfoChar(&ret, '{');
+
+	if (flags & GUC_NO_SHOW_ALL)
+		appendStringInfo(&ret, "NO_SHOW_ALL,");
+	if (flags & GUC_NO_RESET_ALL)
+		appendStringInfo(&ret, "NO_RESET_ALL,");
+	if (flags & GUC_NOT_IN_SAMPLE)
+		appendStringInfo(&ret, "NOT_IN_SAMPLE,");
+	if (flags & GUC_EXPLAIN)
+		appendStringInfo(&ret, "EXPLAIN,");
+	if (flags & GUC_RUNTIME_COMPUTED)
+		appendStringInfo(&ret, "RUNTIME_COMPUTED,");
+
+	/* Remove trailing comma, if any */
+	if (ret.len > 1)
+		ret.data[--ret.len] = '\0';
+
+	appendStringInfoChar(&ret, '}');
+	return ret.data;
+}
+
 /*
  * Return GUC variable value by variable number; optionally return canonical
  * form of name.  Return value is palloc'd.
@@ -9861,6 +9891,9 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
 	}
 
 	values[16] = (conf->status & GUC_PENDING_RESTART) ? "t" : "f";
+
+	/* flags */
+	values[17] = get_flags_array_text(conf->flags);
 }
 
 /*
@@ -9916,7 +9949,7 @@ show_config_by_name_missing_ok(PG_FUNCTION_ARGS)
  * show_all_settings - equiv to SHOW ALL command but implemented as
  * a Table Function.
  */
-#define NUM_PG_SETTINGS_ATTS	17
+#define NUM_PG_SETTINGS_ATTS	18
 
 Datum
 show_all_settings(PG_FUNCTION_ARGS)
@@ -9978,6 +10011,8 @@ show_all_settings(PG_FUNCTION_ARGS)
 						   INT4OID, -1, 0);
 		TupleDescInitEntry(tupdesc, (AttrNumber) 17, "pending_restart",
 						   BOOLOID, -1, 0);
+		TupleDescInitEntry(tupdesc, (AttrNumber) 18, "flags",
+						   TEXTARRAYOID, -1, 0);
 
 		/*
 		 * Generate attribute metadata needed later to produce tuples from raw
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 0859dc81cac..1d7f6b4d376 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6092,9 +6092,9 @@
 { oid => '2084', descr => 'SHOW ALL as a function',
   proname => 'pg_show_all_settings', prorows => '1000', proretset => 't',
   provolatile => 's', prorettype => 'record', proargtypes => '',
-  proallargtypes => '{text,text,text,text,text,text,text,text,text,text,text,_text,text,text,text,int4,bool}',
-  proargmodes => '{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',
-  proargnames => '{name,setting,unit,category,short_desc,extra_desc,context,vartype,source,min_val,max_val,enumvals,boot_val,reset_val,sourcefile,sourceline,pending_restart}',
+  proallargtypes => '{text,text,text,text,text,text,text,text,text,text,text,_text,text,text,text,int4,bool,_text}',
+  proargmodes => '{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',
+  proargnames => '{name,setting,unit,category,short_desc,extra_desc,context,vartype,source,min_val,max_val,enumvals,boot_val,reset_val,sourcefile,sourceline,pending_restart,flags}',
   prosrc => 'show_all_settings' },
 { oid => '3329', descr => 'show config file settings',
   proname => 'pg_show_all_file_settings', prorows => '1000', proretset => 't',
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index 59da91ff04d..5f15b5680be 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -813,3 +813,125 @@ set default_with_oids to f;
 -- Should not allow to set it to true.
 set default_with_oids to t;
 ERROR:  tables declared WITH OIDS are not supported
+--
+-- Test GUC categories and flags.
+--
+CREATE TABLE pg_settings_flags AS SELECT name, category,
+	'NO_SHOW_ALL'	=ANY(flags) AS no_show_all,
+	'NO_RESET_ALL'	=ANY(flags) AS no_reset_all,
+	'NOT_IN_SAMPLE'	=ANY(flags) AS not_in_sample,
+	'EXPLAIN'	=ANY(flags) AS guc_explain,
+	'COMPUTED'	=ANY(flags) AS guc_computed
+	FROM pg_show_all_settings();
+-- test that GUCS are in postgresql.conf
+SELECT lower(name) FROM pg_settings_flags WHERE NOT not_in_sample EXCEPT
+SELECT regexp_replace(ln, '^#?([_[:alpha:]]+) (= .*|[^ ]*$)', '\1') AS guc
+FROM (SELECT regexp_split_to_table(pg_read_file('postgresql.conf'), '\n') AS ln) conf
+WHERE ln ~ '^#?[[:alpha:]]'
+ORDER BY 1;
+            lower            
+-----------------------------
+ config_file
+ plpgsql.check_asserts
+ plpgsql.extra_errors
+ plpgsql.extra_warnings
+ plpgsql.print_strict_params
+ plpgsql.variable_conflict
+(6 rows)
+
+-- test that lines in postgresql.conf that look like GUCs are GUCs
+SELECT regexp_replace(ln, '^#?([_[:alpha:]]+) (= .*|[^ ]*$)', '\1') AS guc
+FROM (SELECT regexp_split_to_table(pg_read_file('postgresql.conf'), '\n') AS ln) conf
+WHERE ln ~ '^#?[[:alpha:]]'
+EXCEPT SELECT lower(name) FROM pg_settings_flags WHERE NOT not_in_sample
+ORDER BY 1;
+        guc        
+-------------------
+ include
+ include_dir
+ include_if_exists
+(3 rows)
+
+-- test that section:DEVELOPER GUCs are flagged GUC_NOT_IN_SAMPLE:
+SELECT * FROM pg_settings_flags
+WHERE category='Developer Options' AND NOT not_in_sample
+ORDER BY 1;
+ name | category | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------+----------+-------------+--------------+---------------+-------------+--------------
+(0 rows)
+
+-- Maybe the converse:
+SELECT * FROM pg_settings_flags
+WHERE category NOT IN ('Developer Options', 'Preset Options') AND not_in_sample
+ORDER BY 1;
+          name          |                    category                     | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------------------------+-------------------------------------------------+-------------+--------------+---------------+-------------+--------------
+ application_name       | Reporting and Logging / What to Log             | f           | f            | t             | f           | f
+ transaction_deferrable | Client Connection Defaults / Statement Behavior | f           | t            | t             | f           | f
+ transaction_isolation  | Client Connection Defaults / Statement Behavior | f           | t            | t             | f           | f
+ transaction_read_only  | Client Connection Defaults / Statement Behavior | f           | t            | t             | f           | f
+(4 rows)
+
+-- Most Query Tuning GUCs are flagged as EXPLAIN:
+SELECT * FROM pg_settings_flags
+WHERE category ~ '^Query Tuning' AND NOT guc_explain
+ORDER BY 1;
+           name            |               category               | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+---------------------------+--------------------------------------+-------------+--------------+---------------+-------------+--------------
+ default_statistics_target | Query Tuning / Other Planner Options | f           | f            | f             | f           | f
+(1 row)
+
+-- Maybe the converse:
+SELECT * FROM pg_settings_flags
+WHERE guc_explain AND NOT category ~ '^Query Tuning|^Resource Usage'
+ORDER BY 1;
+        name         |                    category                     | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+---------------------+-------------------------------------------------+-------------+--------------+---------------+-------------+--------------
+ force_parallel_mode | Developer Options                               | f           | f            | t             | t           | f
+ search_path         | Client Connection Defaults / Statement Behavior | f           | f            | f             | t           | f
+(2 rows)
+
+-- GUCs flagged RUNTIME are Preset
+SELECT * FROM pg_settings_flags
+WHERE guc_computed AND NOT category='Preset Options'
+ORDER BY 1;
+ name | category | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------+----------+-------------+--------------+---------------+-------------+--------------
+(0 rows)
+
+-- PRESET GUCs are flagged NOT_IN_SAMPLE
+SELECT * FROM pg_settings_flags
+WHERE category='Preset Options' AND NOT not_in_sample
+ORDER BY 1;
+ name | category | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------+----------+-------------+--------------+---------------+-------------+--------------
+(0 rows)
+
+-- NO_SHOW_ALL implies NO_RESET_ALL:
+SELECT * FROM pg_settings_flags
+WHERE no_show_all AND NOT no_reset_all
+ORDER BY 1;
+ name | category | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------+----------+-------------+--------------+---------------+-------------+--------------
+(0 rows)
+
+-- Usually the converse:
+SELECT * FROM pg_settings_flags
+WHERE NOT no_show_all AND no_reset_all
+ORDER BY 1;
+          name          |                    category                     | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------------------------+-------------------------------------------------+-------------+--------------+---------------+-------------+--------------
+ transaction_deferrable | Client Connection Defaults / Statement Behavior | f           | t            | t             | f           | f
+ transaction_isolation  | Client Connection Defaults / Statement Behavior | f           | t            | t             | f           | f
+ transaction_read_only  | Client Connection Defaults / Statement Behavior | f           | t            | t             | f           | f
+(3 rows)
+
+-- NO_SHOW_ALL implies NOT_IN_SAMPLE:
+SELECT * FROM pg_settings_flags
+WHERE no_show_all AND NOT not_in_sample
+ORDER BY 1;
+ name | category | no_show_all | no_reset_all | not_in_sample | guc_explain | guc_computed 
+------+----------+-------------+--------------+---------------+-------------+--------------
+(0 rows)
+
+DROP TABLE pg_settings_flags;
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index d652f7b5fb4..833c1fab6c7 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1725,7 +1725,7 @@ pg_settings| SELECT a.name,
     a.sourcefile,
     a.sourceline,
     a.pending_restart
-   FROM pg_show_all_settings() a(name, setting, unit, category, short_desc, extra_desc, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, sourcefile, sourceline, pending_restart);
+   FROM pg_show_all_settings() a(name, setting, unit, category, short_desc, extra_desc, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, sourcefile, sourceline, pending_restart, flags);
 pg_shadow| SELECT pg_authid.rolname AS usename,
     pg_authid.oid AS usesysid,
     pg_authid.rolcreatedb AS usecreatedb,
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index c39c11388d5..c3898ae690f 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -311,3 +311,75 @@ reset check_function_bodies;
 set default_with_oids to f;
 -- Should not allow to set it to true.
 set default_with_oids to t;
+
+--
+-- Test GUC categories and flags.
+--
+CREATE TABLE pg_settings_flags AS SELECT name, category,
+	'NO_SHOW_ALL'	=ANY(flags) AS no_show_all,
+	'NO_RESET_ALL'	=ANY(flags) AS no_reset_all,
+	'NOT_IN_SAMPLE'	=ANY(flags) AS not_in_sample,
+	'EXPLAIN'	=ANY(flags) AS guc_explain,
+	'COMPUTED'	=ANY(flags) AS guc_computed
+	FROM pg_show_all_settings();
+
+-- test that GUCS are in postgresql.conf
+SELECT lower(name) FROM pg_settings_flags WHERE NOT not_in_sample EXCEPT
+SELECT regexp_replace(ln, '^#?([_[:alpha:]]+) (= .*|[^ ]*$)', '\1') AS guc
+FROM (SELECT regexp_split_to_table(pg_read_file('postgresql.conf'), '\n') AS ln) conf
+WHERE ln ~ '^#?[[:alpha:]]'
+ORDER BY 1;
+
+-- test that lines in postgresql.conf that look like GUCs are GUCs
+SELECT regexp_replace(ln, '^#?([_[:alpha:]]+) (= .*|[^ ]*$)', '\1') AS guc
+FROM (SELECT regexp_split_to_table(pg_read_file('postgresql.conf'), '\n') AS ln) conf
+WHERE ln ~ '^#?[[:alpha:]]'
+EXCEPT SELECT lower(name) FROM pg_settings_flags WHERE NOT not_in_sample
+ORDER BY 1;
+
+-- test that section:DEVELOPER GUCs are flagged GUC_NOT_IN_SAMPLE:
+SELECT * FROM pg_settings_flags
+WHERE category='Developer Options' AND NOT not_in_sample
+ORDER BY 1;
+
+-- Maybe the converse:
+SELECT * FROM pg_settings_flags
+WHERE category NOT IN ('Developer Options', 'Preset Options') AND not_in_sample
+ORDER BY 1;
+
+-- Most Query Tuning GUCs are flagged as EXPLAIN:
+SELECT * FROM pg_settings_flags
+WHERE category ~ '^Query Tuning' AND NOT guc_explain
+ORDER BY 1;
+
+-- Maybe the converse:
+SELECT * FROM pg_settings_flags
+WHERE guc_explain AND NOT category ~ '^Query Tuning|^Resource Usage'
+ORDER BY 1;
+
+-- GUCs flagged RUNTIME are Preset
+SELECT * FROM pg_settings_flags
+WHERE guc_computed AND NOT category='Preset Options'
+ORDER BY 1;
+
+-- PRESET GUCs are flagged NOT_IN_SAMPLE
+SELECT * FROM pg_settings_flags
+WHERE category='Preset Options' AND NOT not_in_sample
+ORDER BY 1;
+
+-- NO_SHOW_ALL implies NO_RESET_ALL:
+SELECT * FROM pg_settings_flags
+WHERE no_show_all AND NOT no_reset_all
+ORDER BY 1;
+
+-- Usually the converse:
+SELECT * FROM pg_settings_flags
+WHERE NOT no_show_all AND no_reset_all
+ORDER BY 1;
+
+-- NO_SHOW_ALL implies NOT_IN_SAMPLE:
+SELECT * FROM pg_settings_flags
+WHERE no_show_all AND NOT not_in_sample
+ORDER BY 1;
+
+DROP TABLE pg_settings_flags;
-- 
2.17.1

--EsfvRFssnM00t552--