Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit10fb48d

Browse files
committed
Add an optional missing_ok argument to SQL function current_setting().
This allows convenient checking for existence of a GUC from SQL, which isparticularly useful when dealing with custom variables.David Christensen, reviewed by Jeevan Chalke
1 parent7261172 commit10fb48d

File tree

9 files changed

+101
-17
lines changed

9 files changed

+101
-17
lines changed

‎contrib/tsearch2/tsearch2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ tsa_tsearch2(PG_FUNCTION_ARGS)
363363
tgargs[i+1]=trigger->tgargs[i];
364364

365365
tgargs[1]=pstrdup(GetConfigOptionByName("default_text_search_config",
366-
NULL));
366+
NULL, false));
367367
tgargs_old=trigger->tgargs;
368368
trigger->tgargs=tgargs;
369369
trigger->tgnargs++;

‎doc/src/sgml/func.sgml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16444,7 +16444,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
1644416444
<indexterm>
1644516445
<primary>current_setting</primary>
1644616446
</indexterm>
16447-
<literal><function>current_setting(<parameter>setting_name</parameter>)</function></literal>
16447+
<literal><function>current_setting(<parameter>setting_name</parameter> [, <parameter>missing_ok</parameter> ])</function></literal>
1644816448
</entry>
1644916449
<entry><type>text</type></entry>
1645016450
<entry>get current value of setting</entry>
@@ -16492,6 +16492,11 @@ SELECT current_setting('datestyle');
1649216492
ISO, MDY
1649316493
(1 row)
1649416494
</programlisting>
16495+
16496+
If there is no setting named <parameter>setting_name</parameter>,
16497+
<function>current_setting</function> throws an error
16498+
unless <parameter>missing_ok</parameter> is supplied and is
16499+
<literal>true</literal>.
1649516500
</para>
1649616501

1649716502
<para>

‎src/backend/utils/misc/guc.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7131,7 +7131,7 @@ ExtractSetVariableArgs(VariableSetStmt *stmt)
71317131
caseVAR_SET_VALUE:
71327132
returnflatten_set_variable_args(stmt->name,stmt->args);
71337133
caseVAR_SET_CURRENT:
7134-
returnGetConfigOptionByName(stmt->name,NULL);
7134+
returnGetConfigOptionByName(stmt->name,NULL, false);
71357135
default:
71367136
returnNULL;
71377137
}
@@ -7200,7 +7200,7 @@ set_config_by_name(PG_FUNCTION_ARGS)
72007200
true,0, false);
72017201

72027202
/* get the new current value */
7203-
new_value=GetConfigOptionByName(name,NULL);
7203+
new_value=GetConfigOptionByName(name,NULL, false);
72047204

72057205
/* Convert return string to text */
72067206
PG_RETURN_TEXT_P(cstring_to_text(new_value));
@@ -7627,7 +7627,7 @@ GetPGVariableResultDesc(const char *name)
76277627
constchar*varname;
76287628

76297629
/* Get the canonical spelling of name */
7630-
(void)GetConfigOptionByName(name,&varname);
7630+
(void)GetConfigOptionByName(name,&varname, false);
76317631

76327632
/* need a tuple descriptor representing a single TEXT column */
76337633
tupdesc=CreateTemplateTupleDesc(1, false);
@@ -7650,7 +7650,7 @@ ShowGUCConfigOption(const char *name, DestReceiver *dest)
76507650
char*value;
76517651

76527652
/* Get the value and canonical spelling of name */
7653-
value=GetConfigOptionByName(name,&varname);
7653+
value=GetConfigOptionByName(name,&varname, false);
76547654

76557655
/* need a tuple descriptor representing a single TEXT column */
76567656
tupdesc=CreateTemplateTupleDesc(1, false);
@@ -7734,19 +7734,30 @@ ShowAllGUCConfig(DestReceiver *dest)
77347734
}
77357735

77367736
/*
7737-
* Return GUC variable value by name; optionally return canonical
7738-
* form of name. Return value is palloc'd.
7737+
* Return GUC variable value by name; optionally return canonical form of
7738+
* name. If the GUC is unset, then throw an error unless missing_ok is true,
7739+
* in which case return NULL. Return value is palloc'd (but *varname isn't).
77397740
*/
77407741
char*
7741-
GetConfigOptionByName(constchar*name,constchar**varname)
7742+
GetConfigOptionByName(constchar*name,constchar**varname,boolmissing_ok)
77427743
{
77437744
structconfig_generic*record;
77447745

77457746
record=find_option(name, false,ERROR);
77467747
if (record==NULL)
7748+
{
7749+
if (missing_ok)
7750+
{
7751+
if (varname)
7752+
*varname=NULL;
7753+
returnNULL;
7754+
}
7755+
77477756
ereport(ERROR,
77487757
(errcode(ERRCODE_UNDEFINED_OBJECT),
77497758
errmsg("unrecognized configuration parameter \"%s\"",name)));
7759+
}
7760+
77507761
if ((record->flags&GUC_SUPERUSER_ONLY)&& !superuser())
77517762
ereport(ERROR,
77527763
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
@@ -8033,14 +8044,34 @@ GetNumConfigOptions(void)
80338044
Datum
80348045
show_config_by_name(PG_FUNCTION_ARGS)
80358046
{
8036-
char*varname;
8047+
char*varname=TextDatumGetCString(PG_GETARG_DATUM(0));
80378048
char*varval;
80388049

8039-
/* Get the GUC variable name */
8040-
varname=TextDatumGetCString(PG_GETARG_DATUM(0));
8050+
/* Get the value */
8051+
varval=GetConfigOptionByName(varname,NULL, false);
8052+
8053+
/* Convert to text */
8054+
PG_RETURN_TEXT_P(cstring_to_text(varval));
8055+
}
8056+
8057+
/*
8058+
* show_config_by_name_missing_ok - equiv to SHOW X command but implemented as
8059+
* a function. If X does not exist, suppress the error and just return NULL
8060+
* if missing_ok is TRUE.
8061+
*/
8062+
Datum
8063+
show_config_by_name_missing_ok(PG_FUNCTION_ARGS)
8064+
{
8065+
char*varname=TextDatumGetCString(PG_GETARG_DATUM(0));
8066+
boolmissing_ok=PG_GETARG_BOOL(1);
8067+
char*varval;
80418068

80428069
/* Get the value */
8043-
varval=GetConfigOptionByName(varname,NULL);
8070+
varval=GetConfigOptionByName(varname,NULL,missing_ok);
8071+
8072+
/* return NULL if no such variable */
8073+
if (varval==NULL)
8074+
PG_RETURN_NULL();
80448075

80458076
/* Convert to text */
80468077
PG_RETURN_TEXT_P(cstring_to_text(varval));

‎src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO201506282
56+
#defineCATALOG_VERSION_NO201507021
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,6 +3067,8 @@ DESCR("convert bitstring to int8");
30673067

30683068
DATA(insert OID = 2077 ( current_settingPGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 25 "25" _null_ _null_ _null_ _null_ _null_ show_config_by_name _null_ _null_ _null_ ));
30693069
DESCR("SHOW X as a function");
3070+
DATA(insert OID = 3294 ( current_settingPGNSP PGUID 12 1 0 0 0 f f f f t f s 2 0 25 "25 16" _null_ _null_ _null_ _null_ _null_ show_config_by_name_missing_ok _null_ _null_ _null_ ));
3071+
DESCR("SHOW X as a function, optionally no error for missing variable");
30703072
DATA(insert OID = 2078 ( set_configPGNSP PGUID 12 1 0 0 0 f f f f f f v 3 0 25 "25 25 16" _null_ _null_ _null_ _null_ _null_ set_config_by_name _null_ _null_ _null_ ));
30713073
DESCR("SET X as a function");
30723074
DATA(insert OID = 2084 ( pg_show_all_settingsPGNSP PGUID 12 1 1000 0 0 f f f f t t s 0 0 2249 "" "{25,25,25,25,25,25,25,25,25,25,25,1009,25,25,25,23,16}" "{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}" "{name,setting,unit,category,short_desc,extra_desc,context,vartype,source,min_val,max_val,enumvals,boot_val,reset_val,sourcefile,sourceline,pending_restart}" _null_ _null_ show_all_settings _null_ _null_ _null_ ));
@@ -4866,8 +4868,8 @@ DESCR("GIN support");
48664868
DATA(insert OID = 3301 ( jsonb_concat PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 3802 "3802 3802" _null_ _null_ _null_ _null_ _null_ jsonb_concat _null_ _null_ _null_ ));
48674869
DATA(insert OID = 3302 ( jsonb_delete PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 3802 "3802 25" _null_ _null_ _null_ _null_ _null_ jsonb_delete _null_ _null_ _null_ ));
48684870
DATA(insert OID = 3303 ( jsonb_delete PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 3802 "3802 23" _null_ _null_ _null_ _null_ _null_ jsonb_delete_idx _null_ _null_ _null_ ));
4869-
DATA(insert OID = 3304 ( jsonb_delete_path PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 3802 "3802 1009" _null_ _null_ _null_ _null_ _null_ jsonb_delete_path _null_ _null_ _null_ ));
4870-
DATA(insert OID = 3305 ( jsonb_set PGNSP PGUID 12 1 0 0 0 f f f f t f i 4 0 3802 "3802 1009 3802 16" _null_ _null_ _null_ _null_ _null_ jsonb_set _null_ _null_ _null_ ));
4871+
DATA(insert OID = 3304 ( jsonb_delete_path PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 3802 "3802 1009" _null_ _null_ _null_ _null_ _null_ jsonb_delete_path _null_ _null_ _null_ ));
4872+
DATA(insert OID = 3305 ( jsonb_set PGNSP PGUID 12 1 0 0 0 f f f f t f i 4 0 3802 "3802 1009 3802 16" _null_ _null_ _null_ _null_ _null_ jsonb_set _null_ _null_ _null_ ));
48714873
DESCR("Set part of a jsonb");
48724874
DATA(insert OID = 3306 ( jsonb_pretty PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "3802" _null_ _null_ _null_ _null_ _null_ jsonb_pretty _null_ _null_ _null_ ));
48734875
DESCR("Indented text from jsonb");

‎src/include/utils/builtins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ extern Datum quote_nullable(PG_FUNCTION_ARGS);
11141114

11151115
/* guc.c */
11161116
externDatumshow_config_by_name(PG_FUNCTION_ARGS);
1117+
externDatumshow_config_by_name_missing_ok(PG_FUNCTION_ARGS);
11171118
externDatumset_config_by_name(PG_FUNCTION_ARGS);
11181119
externDatumshow_all_settings(PG_FUNCTION_ARGS);
11191120
externDatumshow_all_file_settings(PG_FUNCTION_ARGS);

‎src/include/utils/guc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ extern int set_config_option(const char *name, const char *value,
365365
GucActionaction,boolchangeVal,intelevel,
366366
boolis_reload);
367367
externvoidAlterSystemSetConfigFile(AlterSystemStmt*setstmt);
368-
externchar*GetConfigOptionByName(constchar*name,constchar**varname);
368+
externchar*GetConfigOptionByName(constchar*name,constchar**varname,
369+
boolmissing_ok);
369370
externvoidGetConfigOptionByNum(intvarnum,constchar**values,bool*noshow);
370371
externintGetNumConfigOptions(void);
371372

‎src/test/regress/expected/guc.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,37 @@ select myfunc(1), current_setting('work_mem');
720720
2MB | 2MB
721721
(1 row)
722722

723+
-- check current_setting()'s behavior with invalid setting name
724+
select current_setting('nosuch.setting'); -- FAIL
725+
ERROR: unrecognized configuration parameter "nosuch.setting"
726+
select current_setting('nosuch.setting', false); -- FAIL
727+
ERROR: unrecognized configuration parameter "nosuch.setting"
728+
select current_setting('nosuch.setting', true) is null;
729+
?column?
730+
----------
731+
t
732+
(1 row)
733+
734+
-- after this, all three cases should yield 'nada'
735+
set nosuch.setting = 'nada';
736+
select current_setting('nosuch.setting');
737+
current_setting
738+
-----------------
739+
nada
740+
(1 row)
741+
742+
select current_setting('nosuch.setting', false);
743+
current_setting
744+
-----------------
745+
nada
746+
(1 row)
747+
748+
select current_setting('nosuch.setting', true);
749+
current_setting
750+
-----------------
751+
nada
752+
(1 row)
753+
723754
-- Normally, CREATE FUNCTION should complain about invalid values in
724755
-- function SET options; but not if check_function_bodies is off,
725756
-- because that creates ordering hazards for pg_dump

‎src/test/regress/sql/guc.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ select myfunc(0);
258258
select current_setting('work_mem');
259259
select myfunc(1), current_setting('work_mem');
260260

261+
-- check current_setting()'s behavior with invalid setting name
262+
263+
select current_setting('nosuch.setting');-- FAIL
264+
select current_setting('nosuch.setting', false);-- FAIL
265+
select current_setting('nosuch.setting', true) isnull;
266+
267+
-- after this, all three cases should yield 'nada'
268+
setnosuch.setting='nada';
269+
270+
select current_setting('nosuch.setting');
271+
select current_setting('nosuch.setting', false);
272+
select current_setting('nosuch.setting', true);
273+
261274
-- Normally, CREATE FUNCTION should complain about invalid values in
262275
-- function SET options; but not if check_function_bodies is off,
263276
-- because that creates ordering hazards for pg_dump

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp