Skip to content

Commit be677bb

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 9bf3458 commit be677bb

File tree

9 files changed

+114
-13
lines changed

9 files changed

+114
-13
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "utils/array.h"
5454
#include "utils/builtins.h"
5555
#include "utils/fmgroids.h"
56+
#include "utils/guc.h"
5657
#include "utils/hsearch.h"
5758
#include "utils/lsyscache.h"
5859
#include "utils/rel.h"
@@ -2021,11 +2022,15 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
20212022
quote_identifier(configitem));
20222023

20232024
/*
2024-
* Some GUC variable names are 'LIST' type and hence must not
2025-
* be quoted.
2025+
* Variables that are marked GUC_LIST_QUOTE were already fully
2026+
* quoted by flatten_set_variable_args() before they were put
2027+
* into the proconfig array; we mustn't re-quote them or we'll
2028+
* make a mess. Variables that are not so marked should just
2029+
* be emitted as simple string literals. If the variable is
2030+
* not known to guc.c, we'll do the latter; this makes it
2031+
* unsafe to use GUC_LIST_QUOTE for extension variables.
20262032
*/
2027-
if (pg_strcasecmp(configitem, "DateStyle") == 0
2028-
|| pg_strcasecmp(configitem, "search_path") == 0)
2033+
if (GetConfigOptionFlags(configitem, true) & GUC_LIST_QUOTE)
20292034
appendStringInfoString(&buf, pos);
20302035
else
20312036
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
@@ -648,8 +648,8 @@ const char *const config_type_names[] =
648648
*
649649
* 6. Don't forget to document the option (at least in config.sgml).
650650
*
651-
* 7. If it's a new GUC_LIST option you must edit pg_dumpall.c to ensure
652-
* it is not single quoted at dump time.
651+
* 7. If it's a new GUC_LIST_QUOTE option, you must add it to
652+
* variable_is_guc_list_quote() in src/bin/pg_dump/dumputils.c.
653653
*/
654654

655655

@@ -6108,6 +6108,30 @@ GetConfigOptionResetString(const char *name)
61086108
return NULL;
61096109
}
61106110

6111+
/*
6112+
* Get the GUC flags associated with the given option.
6113+
*
6114+
* If the option doesn't exist, return 0 if missing_ok is true,
6115+
* otherwise throw an ereport and don't return.
6116+
*/
6117+
int
6118+
GetConfigOptionFlags(const char *name, bool missing_ok)
6119+
{
6120+
struct config_generic *record;
6121+
6122+
record = find_option(name, false, WARNING);
6123+
if (record == NULL)
6124+
{
6125+
if (missing_ok)
6126+
return 0;
6127+
ereport(ERROR,
6128+
(errcode(ERRCODE_UNDEFINED_OBJECT),
6129+
errmsg("unrecognized configuration parameter \"%s\"",
6130+
name)));
6131+
}
6132+
return record->flags;
6133+
}
6134+
61116135

61126136
/*
61136137
* flatten_set_variable_args

src/bin/pg_dump/dumputils.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,3 +1522,26 @@ simple_string_list_member(SimpleStringList *list, const char *val)
15221522
}
15231523
return false;
15241524
}
1525+
1526+
/*
1527+
* Detect whether the given GUC variable is of GUC_LIST_QUOTE type.
1528+
*
1529+
* It'd be better if we could inquire this directly from the backend; but even
1530+
* if there were a function for that, it could only tell us about variables
1531+
* currently known to guc.c, so that it'd be unsafe for extensions to declare
1532+
* GUC_LIST_QUOTE variables anyway. Lacking a solution for that, it doesn't
1533+
* seem worth the work to do more than have this list, which must be kept in
1534+
* sync with the variables actually marked GUC_LIST_QUOTE in guc.c.
1535+
*/
1536+
bool
1537+
variable_is_guc_list_quote(const char *name)
1538+
{
1539+
if (pg_strcasecmp(name, "temp_tablespaces") == 0 ||
1540+
pg_strcasecmp(name, "session_preload_libraries") == 0 ||
1541+
pg_strcasecmp(name, "shared_preload_libraries") == 0 ||
1542+
pg_strcasecmp(name, "local_preload_libraries") == 0 ||
1543+
pg_strcasecmp(name, "search_path") == 0)
1544+
return true;
1545+
else
1546+
return false;
1547+
}

src/bin/pg_dump/dumputils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,6 @@ extern void set_dump_section(const char *arg, int *dumpSections);
7575
extern void simple_string_list_append(SimpleStringList *list, const char *val);
7676
extern bool simple_string_list_member(SimpleStringList *list, const char *val);
7777

78+
extern bool variable_is_guc_list_quote(const char *name);
79+
7880
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_dump.c

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

1002110021
/*
10022-
* Some GUC variable names are 'LIST' type and hence must not be
10023-
* quoted.
10022+
* Variables that are marked GUC_LIST_QUOTE were already fully quoted
10023+
* by flatten_set_variable_args() before they were put into the
10024+
* proconfig array; we mustn't re-quote them or we'll make a mess.
10025+
* Variables that are not so marked should just be emitted as simple
10026+
* string literals. If the variable is not known to
10027+
* variable_is_guc_list_quote(), we'll do the latter; this makes it
10028+
* unsafe to use GUC_LIST_QUOTE for extension variables.
1002410029
*/
10025-
if (pg_strcasecmp(configitem, "DateStyle") == 0
10026-
|| pg_strcasecmp(configitem, "search_path") == 0)
10030+
if (variable_is_guc_list_quote(configitem))
1002710031
appendPQExpBuffer(q, "%s", pos);
1002810032
else
1002910033
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
@@ -1596,10 +1596,15 @@ makeAlterConfigCommand(PGconn *conn, const char *arrayitem,
15961596
appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
15971597

15981598
/*
1599-
* Some GUC variable names are 'LIST' type and hence must not be quoted.
1599+
* Variables that are marked GUC_LIST_QUOTE were already fully quoted by
1600+
* flatten_set_variable_args() before they were put into the setconfig
1601+
* array; we mustn't re-quote them or we'll make a mess. Variables that
1602+
* are not so marked should just be emitted as simple string literals. If
1603+
* the variable is not known to variable_is_guc_list_quote(), we'll do the
1604+
* latter; this makes it unsafe to use GUC_LIST_QUOTE for extension
1605+
* variables.
16001606
*/
1601-
if (pg_strcasecmp(mine, "DateStyle") == 0
1602-
|| pg_strcasecmp(mine, "search_path") == 0)
1607+
if (variable_is_guc_list_quote(mine))
16031608
appendPQExpBuffer(buf, "%s", pos + 1);
16041609
else
16051610
appendStringLiteralConn(buf, pos + 1, conn);

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ extern void EmitWarningsOnPlaceholders(const char *className);
307307
extern const char *GetConfigOption(const char *name, bool missing_ok,
308308
bool restrict_superuser);
309309
extern const char *GetConfigOptionResetString(const char *name);
310+
extern int GetConfigOptionFlags(const char *name, bool missing_ok);
310311
extern void ProcessConfigFile(GucContext context);
311312
extern void InitializeGUCOptions(void);
312313
extern bool SelectConfigFiles(const char *userDoption, const char *progname);

src/test/regress/expected/rules.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,3 +2686,28 @@ View definition:
26862686
FROM ( VALUES (1,2)) v(q, w);
26872687

26882688
drop view rule_v1;
2689+
-- test for pg_get_functiondef properly regurgitating SET parameters
2690+
-- Note that the function is kept around to stress pg_dump.
2691+
CREATE FUNCTION func_with_set_params() RETURNS integer
2692+
AS 'select 1;'
2693+
LANGUAGE SQL
2694+
SET extra_float_digits TO 2
2695+
SET work_mem TO '4MB'
2696+
SET datestyle to iso, mdy
2697+
SET search_path TO PG_CATALOG, "Mixed/Case", 'c:/"a"/path'
2698+
IMMUTABLE STRICT;
2699+
SELECT pg_get_functiondef('func_with_set_params()'::regprocedure);
2700+
pg_get_functiondef
2701+
---------------------------------------------------------------
2702+
CREATE OR REPLACE FUNCTION public.func_with_set_params() +
2703+
RETURNS integer +
2704+
LANGUAGE sql +
2705+
IMMUTABLE STRICT +
2706+
SET extra_float_digits TO '2' +
2707+
SET work_mem TO '4MB' +
2708+
SET "DateStyle" TO 'iso, mdy' +
2709+
SET search_path TO pg_catalog, "Mixed/Case", "c:/""a""/path"+
2710+
AS $function$select 1;$function$ +
2711+
2712+
(1 row)
2713+

src/test/regress/sql/rules.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,3 +1016,15 @@ drop view rule_v1;
10161016
create view rule_v1(x) as select * from (values(1,2)) v(q,w);
10171017
\d+ rule_v1
10181018
drop view rule_v1;
1019+
1020+
-- test for pg_get_functiondef properly regurgitating SET parameters
1021+
-- Note that the function is kept around to stress pg_dump.
1022+
CREATE FUNCTION func_with_set_params() RETURNS integer
1023+
AS 'select 1;'
1024+
LANGUAGE SQL
1025+
SET extra_float_digits TO 2
1026+
SET work_mem TO '4MB'
1027+
SET datestyle to iso, mdy
1028+
SET search_path TO PG_CATALOG, "Mixed/Case", 'c:/"a"/path'
1029+
IMMUTABLE STRICT;
1030+
SELECT pg_get_functiondef('func_with_set_params()'::regprocedure);

0 commit comments

Comments
 (0)