Skip to content

Commit 66e9287

Browse files
committed
Fix mishandling of quoted-list GUC values in pg_dump and ruleutils.c.
Code that prints out the contents of setconfig or proconfig arrays in SQL format needs to handle GUC_LIST_QUOTE variables differently from other ones, because for those variables, flatten_set_variable_args() already applied a layer of quoting. The value can therefore safely be printed as-is, and indeed must be, or flatten_set_variable_args() will muck it up completely on reload. For all other GUC variables, it's necessary and sufficient to quote the value as a SQL literal. We'd recognized the need for this long ago, but mis-analyzed the need slightly, thinking that all GUC_LIST_INPUT variables needed the special treatment. That's actually wrong, since a valid value of a LIST variable might include characters that need quoting, although no existing variables accept such values. More to the point, we hadn't made any particular effort to keep the various places that deal with this up-to-date with the set of variables that actually need special treatment, meaning that we'd do the wrong thing with, for example, temp_tablespaces values. This affects dumping of SET clauses attached to functions, as well as ALTER DATABASE/ROLE SET commands. In ruleutils.c we can fix it reasonably honestly by exporting a guc.c function that allows discovering the flags for a given GUC variable. But pg_dump doesn't have easy access to that, so continue the old method of having a hard-wired list of affected variable names. At least we can fix it to have just one list not two, and update the list to match current reality. A remaining problem with this is that it only works for built-in GUC variables. pg_dump's list obvious knows nothing of third-party extensions, and even the "ask guc.c" method isn't bulletproof since the relevant extension might not be loaded. There's no obvious solution to that, so for now, we'll just have to discourage extension authors from inventing custom GUCs that need GUC_LIST_QUOTE. This has been busted for a long time, so back-patch to all supported branches. Michael Paquier and Tom Lane, reviewed by Kyotaro Horiguchi and Pavel Stehule Discussion: https://postgr.es/m/20180111064900.GA51030@paquier.xyz
1 parent 31c869e commit 66e9287

File tree

9 files changed

+117
-13
lines changed

9 files changed

+117
-13
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "utils/array.h"
6262
#include "utils/builtins.h"
6363
#include "utils/fmgroids.h"
64+
#include "utils/guc.h"
6465
#include "utils/hsearch.h"
6566
#include "utils/lsyscache.h"
6667
#include "utils/rel.h"
@@ -2568,11 +2569,15 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
25682569
quote_identifier(configitem));
25692570

25702571
/*
2571-
* Some GUC variable names are 'LIST' type and hence must not
2572-
* be quoted.
2572+
* Variables that are marked GUC_LIST_QUOTE were already fully
2573+
* quoted by flatten_set_variable_args() before they were put
2574+
* into the proconfig array; we mustn't re-quote them or we'll
2575+
* make a mess. Variables that are not so marked should just
2576+
* be emitted as simple string literals. If the variable is
2577+
* not known to guc.c, we'll do the latter; this makes it
2578+
* unsafe to use GUC_LIST_QUOTE for extension variables.
25732579
*/
2574-
if (pg_strcasecmp(configitem, "DateStyle") == 0
2575-
|| pg_strcasecmp(configitem, "search_path") == 0)
2580+
if (GetConfigOptionFlags(configitem, true) & GUC_LIST_QUOTE)
25762581
appendStringInfoString(&buf, pos);
25772582
else
25782583
simple_quote_literal(&buf, pos);

src/backend/utils/misc/guc.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ static const unit_conversion time_unit_conversion_table[] =
793793
*
794794
* 6. Don't forget to document the option (at least in config.sgml).
795795
*
796-
* 7. If it's a new GUC_LIST option you must edit pg_dumpall.c to ensure
797-
* it is not single quoted at dump time.
796+
* 7. If it's a new GUC_LIST_QUOTE option, you must add it to
797+
* variable_is_guc_list_quote() in src/bin/pg_dump/dumputils.c.
798798
*/
799799

800800

@@ -6802,6 +6802,30 @@ GetConfigOptionResetString(const char *name)
68026802
return NULL;
68036803
}
68046804

6805+
/*
6806+
* Get the GUC flags associated with the given option.
6807+
*
6808+
* If the option doesn't exist, return 0 if missing_ok is true,
6809+
* otherwise throw an ereport and don't return.
6810+
*/
6811+
int
6812+
GetConfigOptionFlags(const char *name, bool missing_ok)
6813+
{
6814+
struct config_generic *record;
6815+
6816+
record = find_option(name, false, WARNING);
6817+
if (record == NULL)
6818+
{
6819+
if (missing_ok)
6820+
return 0;
6821+
ereport(ERROR,
6822+
(errcode(ERRCODE_UNDEFINED_OBJECT),
6823+
errmsg("unrecognized configuration parameter \"%s\"",
6824+
name)));
6825+
}
6826+
return record->flags;
6827+
}
6828+
68056829

68066830
/*
68076831
* flatten_set_variable_args

src/bin/pg_dump/dumputils.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,3 +846,26 @@ buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
846846
printfPQExpBuffer(init_racl_subquery, "NULL");
847847
}
848848
}
849+
850+
/*
851+
* Detect whether the given GUC variable is of GUC_LIST_QUOTE type.
852+
*
853+
* It'd be better if we could inquire this directly from the backend; but even
854+
* if there were a function for that, it could only tell us about variables
855+
* currently known to guc.c, so that it'd be unsafe for extensions to declare
856+
* GUC_LIST_QUOTE variables anyway. Lacking a solution for that, it doesn't
857+
* seem worth the work to do more than have this list, which must be kept in
858+
* sync with the variables actually marked GUC_LIST_QUOTE in guc.c.
859+
*/
860+
bool
861+
variable_is_guc_list_quote(const char *name)
862+
{
863+
if (pg_strcasecmp(name, "temp_tablespaces") == 0 ||
864+
pg_strcasecmp(name, "session_preload_libraries") == 0 ||
865+
pg_strcasecmp(name, "shared_preload_libraries") == 0 ||
866+
pg_strcasecmp(name, "local_preload_libraries") == 0 ||
867+
pg_strcasecmp(name, "search_path") == 0)
868+
return true;
869+
else
870+
return false;
871+
}

src/bin/pg_dump/dumputils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ extern void buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
5656
const char *acl_column, const char *acl_owner,
5757
const char *obj_kind, bool binary_upgrade);
5858

59+
extern bool variable_is_guc_list_quote(const char *name);
60+
5961
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_dump.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11480,11 +11480,15 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
1148011480
appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem));
1148111481

1148211482
/*
11483-
* Some GUC variable names are 'LIST' type and hence must not be
11484-
* quoted.
11483+
* Variables that are marked GUC_LIST_QUOTE were already fully quoted
11484+
* by flatten_set_variable_args() before they were put into the
11485+
* proconfig array; we mustn't re-quote them or we'll make a mess.
11486+
* Variables that are not so marked should just be emitted as simple
11487+
* string literals. If the variable is not known to
11488+
* variable_is_guc_list_quote(), we'll do the latter; this makes it
11489+
* unsafe to use GUC_LIST_QUOTE for extension variables.
1148511490
*/
11486-
if (pg_strcasecmp(configitem, "DateStyle") == 0
11487-
|| pg_strcasecmp(configitem, "search_path") == 0)
11491+
if (variable_is_guc_list_quote(configitem))
1148811492
appendPQExpBufferStr(q, pos);
1148911493
else
1149011494
appendStringLiteralAH(q, pos, fout);

src/bin/pg_dump/pg_dumpall.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,10 +1706,15 @@ makeAlterConfigCommand(PGconn *conn, const char *arrayitem,
17061706
appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
17071707

17081708
/*
1709-
* Some GUC variable names are 'LIST' type and hence must not be quoted.
1709+
* Variables that are marked GUC_LIST_QUOTE were already fully quoted by
1710+
* flatten_set_variable_args() before they were put into the setconfig
1711+
* array; we mustn't re-quote them or we'll make a mess. Variables that
1712+
* are not so marked should just be emitted as simple string literals. If
1713+
* the variable is not known to variable_is_guc_list_quote(), we'll do the
1714+
* latter; this makes it unsafe to use GUC_LIST_QUOTE for extension
1715+
* variables.
17101716
*/
1711-
if (pg_strcasecmp(mine, "DateStyle") == 0
1712-
|| pg_strcasecmp(mine, "search_path") == 0)
1717+
if (variable_is_guc_list_quote(mine))
17131718
appendPQExpBufferStr(buf, pos + 1);
17141719
else
17151720
appendStringLiteralConn(buf, pos + 1, conn);

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ extern void EmitWarningsOnPlaceholders(const char *className);
348348
extern const char *GetConfigOption(const char *name, bool missing_ok,
349349
bool restrict_superuser);
350350
extern const char *GetConfigOptionResetString(const char *name);
351+
extern int GetConfigOptionFlags(const char *name, bool missing_ok);
351352
extern void ProcessConfigFile(GucContext context);
352353
extern void InitializeGUCOptions(void);
353354
extern bool SelectConfigFiles(const char *userDoption, const char *progname);

src/test/regress/expected/rules.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,33 @@ SELECT * FROM hat_data WHERE hat_name IN ('h8', 'h9', 'h7') ORDER BY hat_name;
31473147
DROP RULE hat_upsert ON hats;
31483148
drop table hats;
31493149
drop table hat_data;
3150+
-- test for pg_get_functiondef properly regurgitating SET parameters
3151+
-- Note that the function is kept around to stress pg_dump.
3152+
CREATE FUNCTION func_with_set_params() RETURNS integer
3153+
AS 'select 1;'
3154+
LANGUAGE SQL
3155+
SET search_path TO PG_CATALOG
3156+
SET extra_float_digits TO 2
3157+
SET work_mem TO '4MB'
3158+
SET datestyle to iso, mdy
3159+
SET local_preload_libraries TO "Mixed/Case", 'c:/"a"/path'
3160+
IMMUTABLE STRICT;
3161+
SELECT pg_get_functiondef('func_with_set_params()'::regprocedure);
3162+
pg_get_functiondef
3163+
---------------------------------------------------------------
3164+
CREATE OR REPLACE FUNCTION public.func_with_set_params() +
3165+
RETURNS integer +
3166+
LANGUAGE sql +
3167+
IMMUTABLE STRICT +
3168+
SET search_path TO pg_catalog +
3169+
SET extra_float_digits TO '2' +
3170+
SET work_mem TO '4MB' +
3171+
SET "DateStyle" TO 'iso, mdy' +
3172+
SET local_preload_libraries TO "Mixed/Case", "c:/""a""/path"+
3173+
AS $function$select 1;$function$ +
3174+
3175+
(1 row)
3176+
31503177
-- tests for pg_get_*def with invalid objects
31513178
SELECT pg_get_constraintdef(0);
31523179
pg_get_constraintdef

src/test/regress/sql/rules.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,19 @@ DROP RULE hat_upsert ON hats;
11551155
drop table hats;
11561156
drop table hat_data;
11571157

1158+
-- test for pg_get_functiondef properly regurgitating SET parameters
1159+
-- Note that the function is kept around to stress pg_dump.
1160+
CREATE FUNCTION func_with_set_params() RETURNS integer
1161+
AS 'select 1;'
1162+
LANGUAGE SQL
1163+
SET search_path TO PG_CATALOG
1164+
SET extra_float_digits TO 2
1165+
SET work_mem TO '4MB'
1166+
SET datestyle to iso, mdy
1167+
SET local_preload_libraries TO "Mixed/Case", 'c:/"a"/path'
1168+
IMMUTABLE STRICT;
1169+
SELECT pg_get_functiondef('func_with_set_params()'::regprocedure);
1170+
11581171
-- tests for pg_get_*def with invalid objects
11591172
SELECT pg_get_constraintdef(0);
11601173
SELECT pg_get_functiondef(0);

0 commit comments

Comments
 (0)