Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d10e41d

Browse files
committed
Introduce pg_settings_get_flags() to find flags associated to a GUC
The most meaningful flags are shown, which are the ones useful for the user and for automating and extending the set of tests supported currently by check_guc. This script may actually be removed in the future, but we are not completely sure yet if and how we want to support the remaining sanity checks performed there, that are now integrated in the main regression test suite as of this commit. Thanks also to Peter Eisentraut and Kyotaro Horiguchi for the discussion. Bump catalog version. Author: Justin Pryzby Discussion: https://postgr.es/m/20211129030833.GJ17618@telsasoft.com
1 parent 02b8048 commit d10e41d

File tree

6 files changed

+208
-1
lines changed

6 files changed

+208
-1
lines changed

doc/src/sgml/func.sgml

+37
Original file line numberDiff line numberDiff line change
@@ -23859,6 +23859,43 @@ SELECT currval(pg_get_serial_sequence('sometable', 'id'));
2385923859
</para></entry>
2386023860
</row>
2386123861

23862+
<row>
23863+
<entry role="func_table_entry"><para role="func_signature">
23864+
<indexterm>
23865+
<primary>pg_settings_get_flags</primary>
23866+
</indexterm>
23867+
<function>pg_settings_get_flags</function> ( <parameter>guc</parameter> <type>text</type> )
23868+
<returnvalue>text[]</returnvalue>
23869+
</para>
23870+
<para>
23871+
Returns an array of the flags associated with the given GUC, or
23872+
<literal>NULL</literal> if it does not exist. The result is
23873+
an empty array if the GUC exists but there are no flags to show.
23874+
Only the most useful flags are exposed, as of the following:
23875+
<simplelist>
23876+
<member>
23877+
<literal>EXPLAIN</literal>: parameters included in
23878+
<command>EXPLAIN (SETTINGS)</command> commands.
23879+
</member>
23880+
<member>
23881+
<literal>NO_SHOW_ALL</literal>: parameters excluded from
23882+
<command>SHOW ALL</command> commands.
23883+
</member>
23884+
<member>
23885+
<literal>NO_RESET_ALL</literal>: parameters excluded from
23886+
<command>RESET ALL</command> commands.
23887+
</member>
23888+
<member>
23889+
<literal>NOT_IN_SAMPLE</literal>: parameters not included in
23890+
<filename>postgresql.conf</filename> by default.
23891+
</member>
23892+
<member>
23893+
<literal>RUNTIME_COMPUTED</literal>: runtime-computed parameters.
23894+
</member>
23895+
</simplelist>
23896+
</para></entry>
23897+
</row>
23898+
2386223899
<row>
2386323900
<entry role="func_table_entry"><para role="func_signature">
2386423901
<indexterm>

src/backend/utils/misc/guc.c

+39
Original file line numberDiff line numberDiff line change
@@ -9634,6 +9634,45 @@ GetConfigOptionByName(const char *name, const char **varname, bool missing_ok)
96349634
return _ShowOption(record, true);
96359635
}
96369636

9637+
/*
9638+
* Return some of the flags associated to the specified GUC in the shape of
9639+
* a text array, and NULL if it does not exist. An empty array is returned
9640+
* if the GUC exists without any meaningful flags to show.
9641+
*/
9642+
Datum
9643+
pg_settings_get_flags(PG_FUNCTION_ARGS)
9644+
{
9645+
#define MAX_GUC_FLAGS 5
9646+
char *varname = TextDatumGetCString(PG_GETARG_DATUM(0));
9647+
struct config_generic *record;
9648+
int cnt = 0;
9649+
Datum flags[MAX_GUC_FLAGS];
9650+
ArrayType *a;
9651+
9652+
record = find_option(varname, false, true, ERROR);
9653+
9654+
/* return NULL if no such variable */
9655+
if (record == NULL)
9656+
PG_RETURN_NULL();
9657+
9658+
if (record->flags & GUC_EXPLAIN)
9659+
flags[cnt++] = CStringGetTextDatum("EXPLAIN");
9660+
if (record->flags & GUC_NO_RESET_ALL)
9661+
flags[cnt++] = CStringGetTextDatum("NO_RESET_ALL");
9662+
if (record->flags & GUC_NO_SHOW_ALL)
9663+
flags[cnt++] = CStringGetTextDatum("NO_SHOW_ALL");
9664+
if (record->flags & GUC_NOT_IN_SAMPLE)
9665+
flags[cnt++] = CStringGetTextDatum("NOT_IN_SAMPLE");
9666+
if (record->flags & GUC_RUNTIME_COMPUTED)
9667+
flags[cnt++] = CStringGetTextDatum("RUNTIME_COMPUTED");
9668+
9669+
Assert(cnt <= MAX_GUC_FLAGS);
9670+
9671+
/* Returns the record as Datum */
9672+
a = construct_array(flags, cnt, TEXTOID, -1, false, TYPALIGN_INT);
9673+
PG_RETURN_ARRAYTYPE_P(a);
9674+
}
9675+
96379676
/*
96389677
* Return GUC variable value by variable number; optionally return canonical
96399678
* form of name. Return value is palloc'd.

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 202201271
56+
#define CATALOG_VERSION_NO 202201311
5757

5858
#endif

src/include/catalog/pg_proc.dat

+5
Original file line numberDiff line numberDiff line change
@@ -6096,6 +6096,11 @@
60966096
proargmodes => '{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',
60976097
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}',
60986098
prosrc => 'show_all_settings' },
6099+
6100+
{ oid => '8921', descr => 'return flags for specified GUC',
6101+
proname => 'pg_settings_get_flags', provolatile => 's', prorettype => '_text',
6102+
proargtypes => 'text', prosrc => 'pg_settings_get_flags' },
6103+
60996104
{ oid => '3329', descr => 'show config file settings',
61006105
proname => 'pg_show_all_file_settings', prorows => '1000', proretset => 't',
61016106
provolatile => 'v', prorettype => 'record', proargtypes => '',

src/test/regress/expected/guc.out

+83
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,86 @@ set default_with_oids to f;
813813
-- Should not allow to set it to true.
814814
set default_with_oids to t;
815815
ERROR: tables declared WITH OIDS are not supported
816+
-- Test GUC categories and flag patterns
817+
SELECT pg_settings_get_flags(NULL);
818+
pg_settings_get_flags
819+
-----------------------
820+
821+
(1 row)
822+
823+
SELECT pg_settings_get_flags('does_not_exist');
824+
pg_settings_get_flags
825+
-----------------------
826+
827+
(1 row)
828+
829+
CREATE TABLE tab_settings_flags AS SELECT name, category,
830+
'EXPLAIN' = ANY(flags) AS explain,
831+
'NO_RESET_ALL' = ANY(flags) AS no_reset_all,
832+
'NO_SHOW_ALL' = ANY(flags) AS no_show_all,
833+
'NOT_IN_SAMPLE' = ANY(flags) AS not_in_sample,
834+
'RUNTIME_COMPUTED' = ANY(flags) AS runtime_computed
835+
FROM pg_show_all_settings() AS psas,
836+
pg_settings_get_flags(psas.name) AS flags;
837+
-- Developer GUCs should be flagged with GUC_NOT_IN_SAMPLE:
838+
SELECT name FROM tab_settings_flags
839+
WHERE category = 'Developer Options' AND NOT not_in_sample
840+
ORDER BY 1;
841+
name
842+
------
843+
(0 rows)
844+
845+
-- Most query-tuning GUCs are flagged as valid for EXPLAIN.
846+
-- default_statistics_target is an exception.
847+
SELECT name FROM tab_settings_flags
848+
WHERE category ~ '^Query Tuning' AND NOT explain
849+
ORDER BY 1;
850+
name
851+
---------------------------
852+
default_statistics_target
853+
(1 row)
854+
855+
-- Runtime-computed GUCs should be part of the preset category.
856+
SELECT name FROM tab_settings_flags
857+
WHERE NOT category = 'Preset Options' AND runtime_computed
858+
ORDER BY 1;
859+
name
860+
------
861+
(0 rows)
862+
863+
-- Preset GUCs are flagged as NOT_IN_SAMPLE.
864+
SELECT name FROM tab_settings_flags
865+
WHERE category = 'Preset Options' AND NOT not_in_sample
866+
ORDER BY 1;
867+
name
868+
------
869+
(0 rows)
870+
871+
-- NO_SHOW_ALL implies NO_RESET_ALL, and vice-versa.
872+
SELECT name FROM tab_settings_flags
873+
WHERE no_show_all AND NOT no_reset_all
874+
ORDER BY 1;
875+
name
876+
------
877+
(0 rows)
878+
879+
-- Exceptions are transaction_*.
880+
SELECT name FROM tab_settings_flags
881+
WHERE NOT no_show_all AND no_reset_all
882+
ORDER BY 1;
883+
name
884+
------------------------
885+
transaction_deferrable
886+
transaction_isolation
887+
transaction_read_only
888+
(3 rows)
889+
890+
-- NO_SHOW_ALL implies NOT_IN_SAMPLE.
891+
SELECT name FROM tab_settings_flags
892+
WHERE no_show_all AND NOT not_in_sample
893+
ORDER BY 1;
894+
name
895+
------
896+
(0 rows)
897+
898+
DROP TABLE tab_settings_flags;

src/test/regress/sql/guc.sql

+43
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,46 @@ reset check_function_bodies;
311311
set default_with_oids to f;
312312
-- Should not allow to set it to true.
313313
set default_with_oids to t;
314+
315+
-- Test GUC categories and flag patterns
316+
SELECT pg_settings_get_flags(NULL);
317+
SELECT pg_settings_get_flags('does_not_exist');
318+
CREATE TABLE tab_settings_flags AS SELECT name, category,
319+
'EXPLAIN' = ANY(flags) AS explain,
320+
'NO_RESET_ALL' = ANY(flags) AS no_reset_all,
321+
'NO_SHOW_ALL' = ANY(flags) AS no_show_all,
322+
'NOT_IN_SAMPLE' = ANY(flags) AS not_in_sample,
323+
'RUNTIME_COMPUTED' = ANY(flags) AS runtime_computed
324+
FROM pg_show_all_settings() AS psas,
325+
pg_settings_get_flags(psas.name) AS flags;
326+
327+
-- Developer GUCs should be flagged with GUC_NOT_IN_SAMPLE:
328+
SELECT name FROM tab_settings_flags
329+
WHERE category = 'Developer Options' AND NOT not_in_sample
330+
ORDER BY 1;
331+
-- Most query-tuning GUCs are flagged as valid for EXPLAIN.
332+
-- default_statistics_target is an exception.
333+
SELECT name FROM tab_settings_flags
334+
WHERE category ~ '^Query Tuning' AND NOT explain
335+
ORDER BY 1;
336+
-- Runtime-computed GUCs should be part of the preset category.
337+
SELECT name FROM tab_settings_flags
338+
WHERE NOT category = 'Preset Options' AND runtime_computed
339+
ORDER BY 1;
340+
-- Preset GUCs are flagged as NOT_IN_SAMPLE.
341+
SELECT name FROM tab_settings_flags
342+
WHERE category = 'Preset Options' AND NOT not_in_sample
343+
ORDER BY 1;
344+
-- NO_SHOW_ALL implies NO_RESET_ALL, and vice-versa.
345+
SELECT name FROM tab_settings_flags
346+
WHERE no_show_all AND NOT no_reset_all
347+
ORDER BY 1;
348+
-- Exceptions are transaction_*.
349+
SELECT name FROM tab_settings_flags
350+
WHERE NOT no_show_all AND no_reset_all
351+
ORDER BY 1;
352+
-- NO_SHOW_ALL implies NOT_IN_SAMPLE.
353+
SELECT name FROM tab_settings_flags
354+
WHERE no_show_all AND NOT not_in_sample
355+
ORDER BY 1;
356+
DROP TABLE tab_settings_flags;

0 commit comments

Comments
 (0)