pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
set/reset issues in create function and set_config
2+ messages / 2 participants
[nested] [flat]

* set/reset issues in create function and set_config
@ 2018-04-24 15:32  Alexey Bashtanov <bashtanov@imap.cc>
  0 siblings, 1 reply; 2+ messages in thread

From: Alexey Bashtanov @ 2018-04-24 15:32 UTC (permalink / raw)
  To: pgsql-bugs

Hello,

It looks like set_config does set-to-default if it's called with second 
argument being null, but this is undocumented.
Please see the docs patch to fix it.

Also, SET to DEFAULT or RESET in CREATE FUNCTION is accepted but does 
nothing.
It wouldn't be a problem, but documentation to SET command says:

 > If SET LOCAL is used within a function that has a SET option for the 
same variable (see CREATE FUNCTION 
<https://www.postgresql.org/docs/9.4/static/sql-createfunction.html;), 
the effects of the SET LOCAL command disappear at function exit

This doesn't work with CREATE FUNCTION ... SET ... DEFAULT ....
What do you think of just disallowing it? Patch attached as well.

Best regards,
   Alexey

Attachments: download all (2 files, .tar.gz)

disallow-create-function-set-default.patch (text/x-patch, 2.4K) download diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c new file mode 100644 index 3c74873..e8c2a34 *** a/src/backend/commands/functioncmds.c --- b/src/backend/commands/functioncmds.c *************** interpret_func_parallel(DefElem *defel) *** 610,616 **** * The input and result may be NULL to signify a null entry. */ static ArrayType * ! update_proconfig_value(ArrayType *a, List *set_items) { ListCell *l; --- 610,616 ---- * The input and result may be NULL to signify a null entry. */ static ArrayType * ! update_proconfig_value(ArrayType *a, List *set_items, bool allow_reset) { ListCell *l; *************** update_proconfig_value(ArrayType *a, Lis *** 626,633 **** if (valuestr) a = GUCArrayAdd(a, sstmt->name, valuestr); ! else /* RESET */ ! a = GUCArrayDelete(a, sstmt->name); } } --- 626,640 ---- if (valuestr) a = GUCArrayAdd(a, sstmt->name, valuestr); ! else ! if (allow_reset) /* RESET */ ! a = GUCArrayDelete(a, sstmt->name); ! else { ! ereport(ERROR, ! (errcode(ERRCODE_SYNTAX_ERROR), ! errmsg("Cannot RESET or SET to DEFAULT when creating a function or a procedure"))); ! return NULL; /* keep compiler quiet */ ! } } } *************** compute_function_attributes(ParseState * *** 770,776 **** if (leakproof_item) *leakproof_p = intVal(leakproof_item->arg); if (set_items) ! *proconfig = update_proconfig_value(NULL, set_items); if (cost_item) { *procost = defGetNumeric(cost_item); --- 777,783 ---- if (leakproof_item) *leakproof_p = intVal(leakproof_item->arg); if (set_items) ! *proconfig = update_proconfig_value(NULL, set_items, false); if (cost_item) { *procost = defGetNumeric(cost_item); *************** AlterFunction(ParseState *pstate, AlterF *** 1279,1285 **** a = isnull ? NULL : DatumGetArrayTypeP(datum); /* update according to each SET or RESET item, left to right */ ! a = update_proconfig_value(a, set_items); /* update the tuple */ memset(repl_repl, false, sizeof(repl_repl)); --- 1286,1292 ---- a = isnull ? NULL : DatumGetArrayTypeP(datum); /* update according to each SET or RESET item, left to right */ ! a = update_proconfig_value(a, set_items, true); /* update the tuple */ memset(repl_repl, false, sizeof(repl_repl));
document-set_config-2nd-null.patch (text/x-patch, 1.0K) download diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml new file mode 100644 index 53a40dd..e79d21a *** a/doc/src/sgml/func.sgml --- b/doc/src/sgml/func.sgml *************** SELECT current_setting('datestyle'); *** 18480,18486 **** <para> <function>set_config</function> sets the parameter <parameter>setting_name</parameter> to ! <parameter>new_value</parameter>. If <parameter>is_local</parameter> is <literal>true</literal>, the new value will only apply to the current transaction. If you want the new value to apply for the current session, use --- 18480,18487 ---- <para> <function>set_config</function> sets the parameter <parameter>setting_name</parameter> to ! <parameter>new_value</parameter>. If <parameter>new_value</parameter> ! is null the value is reset to default. If <parameter>is_local</parameter> is <literal>true</literal>, the new value will only apply to the current transaction. If you want the new value to apply for the current session, use
^ permalink raw reply [nested|flat] 2+ messages in thread

* Re: set/reset issues in create function and set_config
@ 2018-04-25 08:31  Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
  parent: Alexey Bashtanov <bashtanov@imap.cc>
  0 siblings, 0 replies; 2+ messages in thread

From: Kyotaro HORIGUCHI @ 2018-04-25 08:31 UTC (permalink / raw)
  To: bashtanov@imap.cc; +Cc: pgsql-bugs

At Tue, 24 Apr 2018 16:32:08 +0100, Alexey Bashtanov <bashtanov@imap.cc> wrote in <a7c1f0cb-1031-abf8-e4cc-13cf781d6563@imap.cc>
> Hello,
> 
> It looks like set_config does set-to-default if it's called with
> second argument being null, but this is undocumented.
> Please see the docs patch to fix it.

Agreed.

> Also, SET to DEFAULT or RESET in CREATE FUNCTION is accepted but does
> nothing.
> It wouldn't be a problem, but documentation to SET command says:
> 
> > If SET LOCAL is used within a function that has a SET option for the
> > same variable (see CREATE FUNCTION
> > <https://www.postgresql.org/docs/9.4/static/sql-createfunction.html;),
> > the effects of the SET LOCAL command disappear at function exit
> 
> This doesn't work with CREATE FUNCTION ... SET ... DEFAULT ....
> What do you think of just disallowing it? Patch attached as well.

SET .. DEFAULT and RESET are defined as follows.

https://www.postgresql.org/docs/10/static/sql-reset.html

> The default value is defined as the value that the parameter
> would have had, if no SET had ever been issued for it in the
> current session.

Precisely following this statement, SET ... TO DEFAULT changes
the variable to the session default temporarily. For example, the
attached patch (of course, just a PoC) does that.

=# create or replace function f() returns record as $$ select name, setting, reset_val from pg_settings where name = 'log_min_messages'; $$ set log_min_messages to default language sql;
=# set log_min_messages = debug5;
=# show log_min_messages;
 log_min_messages 
------------------
 debug5
(1 row)
=# set log_min_messages = debug5;
SET
postgres=# select f();
                 f                  
------------------------------------
 (log_min_messages,warning,warning)
(1 row)
=# show log_min_messages;
 log_min_messages 
------------------
 debug5
(1 row)

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

allow_set_default_as_func_opt.patch (text/x-patch, 1.7K) download diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c index 3c74873eeb..f04304c244 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -624,10 +624,7 @@ update_proconfig_value(ArrayType *a, List *set_items) { char *valuestr = ExtractSetVariableArgs(sstmt); - if (valuestr) - a = GUCArrayAdd(a, sstmt->name, valuestr); - else /* RESET */ - a = GUCArrayDelete(a, sstmt->name); + a = GUCArrayAdd(a, sstmt->name, valuestr); } } diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 44dfa92722..200acd694b 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -9709,7 +9709,13 @@ ProcessGUCArray(ArrayType *array, continue; } - (void) set_config_option(name, value, + if (value && strcmp(value, "default") == 0) + (void) set_config_option(name, NULL, + context, source, + action, true, 0, false); + + else + (void) set_config_option(name, value, context, source, action, true, 0, false); @@ -9734,7 +9740,6 @@ GUCArrayAdd(ArrayType *array, const char *name, const char *value) ArrayType *a; Assert(name); - Assert(value); /* test if the option is valid and we're allowed to set it */ (void) validate_option_array_item(name, value, false); @@ -9745,7 +9750,11 @@ GUCArrayAdd(ArrayType *array, const char *name, const char *value) name = record->name; /* build new item for array */ - newval = psprintf("%s=%s", name, value); + if (value) + newval = psprintf("%s=%s", name, value); + else + newval = psprintf("%s=default", name); + datum = CStringGetTextDatum(newval); if (array)
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-04-25 08:31 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-24 15:32 set/reset issues in create function and set_config Alexey Bashtanov <bashtanov@imap.cc>
2018-04-25 08:31 ` Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox