Skip to content

Commit f74a024

Browse files
committed
Don't fail for bad GUCs in CREATE FUNCTION with check_function_bodies off.
The previous coding attempted to activate all the GUC settings specified in SET clauses, so that the function validator could operate in the GUC environment expected by the function body. However, this is problematic when restoring a dump, since the SET clauses might refer to database objects that don't exist yet. We already have the parameter check_function_bodies that's meant to prevent forward references in function definitions from breaking dumps, so let's change CREATE FUNCTION to not install the SET values if check_function_bodies is off. Authors of function validators were already advised not to make any "context sensitive" checks when check_function_bodies is off, if indeed they're checking anything at all in that mode. But extend the documentation to point out the GUC issue in particular. (Note that we still check the SET clauses to some extent; the behavior with !check_function_bodies is now approximately equivalent to what ALTER DATABASE/ROLE have been doing for awhile with context-dependent GUCs.) This problem can be demonstrated in all active branches, so back-patch all the way.
1 parent 8bfca99 commit f74a024

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

src/backend/catalog/pg_proc.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -561,24 +561,34 @@ ProcedureCreate(const char *procedureName,
561561
/* Verify function body */
562562
if (OidIsValid(languageValidator))
563563
{
564-
ArrayType *set_items;
565-
int save_nestlevel;
564+
ArrayType *set_items = NULL;
565+
int save_nestlevel = 0;
566566

567567
/* Advance command counter so new tuple can be seen by validator */
568568
CommandCounterIncrement();
569569

570-
/* Set per-function configuration parameters */
571-
set_items = (ArrayType *) DatumGetPointer(proconfig);
572-
if (set_items) /* Need a new GUC nesting level */
570+
/*
571+
* Set per-function configuration parameters so that the validation is
572+
* done with the environment the function expects. However, if
573+
* check_function_bodies is off, we don't do this, because that would
574+
* create dump ordering hazards that pg_dump doesn't know how to deal
575+
* with. (For example, a SET clause might refer to a not-yet-created
576+
* text search configuration.) This means that the validator
577+
* shouldn't complain about anything that might depend on a GUC
578+
* parameter when check_function_bodies is off.
579+
*/
580+
if (check_function_bodies)
573581
{
574-
save_nestlevel = NewGUCNestLevel();
575-
ProcessGUCArray(set_items,
576-
(superuser() ? PGC_SUSET : PGC_USERSET),
577-
PGC_S_SESSION,
578-
GUC_ACTION_SAVE);
582+
set_items = (ArrayType *) DatumGetPointer(proconfig);
583+
if (set_items) /* Need a new GUC nesting level */
584+
{
585+
save_nestlevel = NewGUCNestLevel();
586+
ProcessGUCArray(set_items,
587+
(superuser() ? PGC_SUSET : PGC_USERSET),
588+
PGC_S_SESSION,
589+
GUC_ACTION_SAVE);
590+
}
579591
}
580-
else
581-
save_nestlevel = 0; /* keep compiler quiet */
582592

583593
OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
584594

src/test/regress/expected/guc.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,19 @@ select myfunc(1), current_setting('regex_flavor');
699699
extended | extended
700700
(1 row)
701701

702+
-- Normally, CREATE FUNCTION should complain about invalid values in
703+
-- function SET options; but not if check_function_bodies is off,
704+
-- because that creates ordering hazards for pg_dump
705+
create function func_with_bad_set() returns int as $$ select 1 $$
706+
language sql
707+
set default_text_search_config = no_such_config;
708+
NOTICE: text search configuration "no_such_config" does not exist
709+
ERROR: invalid value for parameter "default_text_search_config": "no_such_config"
710+
set check_function_bodies = off;
711+
create function func_with_bad_set() returns int as $$ select 1 $$
712+
language sql
713+
set default_text_search_config = no_such_config;
714+
NOTICE: text search configuration "no_such_config" does not exist
715+
select func_with_bad_set();
716+
ERROR: invalid value for parameter "default_text_search_config": "no_such_config"
717+
reset check_function_bodies;

src/test/regress/sql/guc.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,21 @@ set regex_flavor = basic;
251251
select myfunc(0);
252252
select current_setting('regex_flavor');
253253
select myfunc(1), current_setting('regex_flavor');
254+
255+
-- Normally, CREATE FUNCTION should complain about invalid values in
256+
-- function SET options; but not if check_function_bodies is off,
257+
-- because that creates ordering hazards for pg_dump
258+
259+
create function func_with_bad_set() returns int as $$ select 1 $$
260+
language sql
261+
set default_text_search_config = no_such_config;
262+
263+
set check_function_bodies = off;
264+
265+
create function func_with_bad_set() returns int as $$ select 1 $$
266+
language sql
267+
set default_text_search_config = no_such_config;
268+
269+
select func_with_bad_set();
270+
271+
reset check_function_bodies;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy