]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/guc.c
a05b3be3937c2f1e67cc78403520beb15d1faa38
[postgresql] / src / backend / utils / misc / guc.c
1 /*--------------------------------------------------------------------
2  * guc.c
3  *
4  * Support for grand unified configuration scheme, including SET
5  * command, configuration file, and command line options.
6  * See src/backend/utils/misc/README for more information.
7  *
8  *
9  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
10  * Written by Peter Eisentraut <peter_e@gmx.net>.
11  *
12  * IDENTIFICATION
13  *        $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.413 2007/08/19 01:41:25 adunstan Exp $
14  *
15  *--------------------------------------------------------------------
16  */
17 #include "postgres.h"
18
19 #include <ctype.h>
20 #include <float.h>
21 #include <limits.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #ifdef HAVE_SYSLOG
25 #include <syslog.h>
26 #endif
27
28
29 #include "access/gin.h"
30 #include "access/transam.h"
31 #include "access/twophase.h"
32 #include "access/xact.h"
33 #include "catalog/namespace.h"
34 #include "commands/async.h"
35 #include "commands/prepare.h"
36 #include "commands/vacuum.h"
37 #include "commands/variable.h"
38 #include "commands/trigger.h"
39 #include "funcapi.h"
40 #include "libpq/auth.h"
41 #include "libpq/pqformat.h"
42 #include "miscadmin.h"
43 #include "optimizer/cost.h"
44 #include "optimizer/geqo.h"
45 #include "optimizer/paths.h"
46 #include "optimizer/planmain.h"
47 #include "parser/gramparse.h"
48 #include "parser/parse_expr.h"
49 #include "parser/parse_relation.h"
50 #include "parser/parse_type.h"
51 #include "parser/scansup.h"
52 #include "pgstat.h"
53 #include "postmaster/autovacuum.h"
54 #include "postmaster/bgwriter.h"
55 #include "postmaster/postmaster.h"
56 #include "postmaster/syslogger.h"
57 #include "postmaster/walwriter.h"
58 #include "storage/fd.h"
59 #include "storage/freespace.h"
60 #include "tcop/tcopprot.h"
61 #include "utils/builtins.h"
62 #include "utils/guc_tables.h"
63 #include "utils/memutils.h"
64 #include "utils/pg_locale.h"
65 #include "utils/plancache.h"
66 #include "utils/portal.h"
67 #include "utils/ps_status.h"
68 #include "utils/tzparser.h"
69 #include "utils/xml.h"
70
71 #ifndef PG_KRB_SRVTAB
72 #define PG_KRB_SRVTAB ""
73 #endif
74 #ifndef PG_KRB_SRVNAM
75 #define PG_KRB_SRVNAM ""
76 #endif
77
78 #define CONFIG_FILENAME "postgresql.conf"
79 #define HBA_FILENAME    "pg_hba.conf"
80 #define IDENT_FILENAME  "pg_ident.conf"
81
82 #ifdef EXEC_BACKEND
83 #define CONFIG_EXEC_PARAMS "global/config_exec_params"
84 #define CONFIG_EXEC_PARAMS_NEW "global/config_exec_params.new"
85 #endif
86
87 /* upper limit for GUC variables measured in kilobytes of memory */
88 #if SIZEOF_SIZE_T > 4
89 #define MAX_KILOBYTES   INT_MAX
90 #else
91 #define MAX_KILOBYTES   (INT_MAX / 1024)
92 #endif
93
94 #define KB_PER_MB (1024)
95 #define KB_PER_GB (1024*1024)
96
97 #define MS_PER_S 1000
98 #define S_PER_MIN 60
99 #define MS_PER_MIN (1000 * 60)
100 #define MIN_PER_H 60
101 #define S_PER_H (60 * 60)
102 #define MS_PER_H (1000 * 60 * 60)
103 #define MIN_PER_D (60 * 24)
104 #define S_PER_D (60 * 60 * 24)
105 #define MS_PER_D (1000 * 60 * 60 * 24)
106
107 /* XXX these should appear in other modules' header files */
108 extern bool Log_disconnections;
109 extern int      CommitDelay;
110 extern int      CommitSiblings;
111 extern char *default_tablespace;
112 extern char *temp_tablespaces;
113 extern bool fullPageWrites;
114
115 #ifdef TRACE_SORT
116 extern bool trace_sort;
117 #endif
118 #ifdef TRACE_SYNCSCAN
119 extern bool trace_syncscan;
120 #endif
121 #ifdef DEBUG_BOUNDED_SORT
122 extern bool optimize_bounded_sort;
123 #endif
124
125 #ifdef USE_SSL
126 extern char *SSLCipherSuites;
127 #endif
128
129
130 static const char *assign_log_destination(const char *value,
131                                            bool doit, GucSource source);
132
133 #ifdef HAVE_SYSLOG
134 static int      syslog_facility = LOG_LOCAL0;
135
136 static const char *assign_syslog_facility(const char *facility,
137                                            bool doit, GucSource source);
138 static const char *assign_syslog_ident(const char *ident,
139                                         bool doit, GucSource source);
140 #endif
141
142 static const char *assign_defaultxactisolevel(const char *newval, bool doit,
143                                                    GucSource source);
144 static const char *assign_session_replication_role(const char *newval, bool doit,
145                                                    GucSource source);
146 static const char *assign_log_min_messages(const char *newval, bool doit,
147                                                 GucSource source);
148 static const char *assign_client_min_messages(const char *newval,
149                                                    bool doit, GucSource source);
150 static const char *assign_min_error_statement(const char *newval, bool doit,
151                                                    GucSource source);
152 static const char *assign_msglvl(int *var, const char *newval, bool doit,
153                           GucSource source);
154 static const char *assign_log_error_verbosity(const char *newval, bool doit,
155                                                    GucSource source);
156 static const char *assign_log_statement(const char *newval, bool doit,
157                                          GucSource source);
158 static const char *show_num_temp_buffers(void);
159 static bool assign_phony_autocommit(bool newval, bool doit, GucSource source);
160 static const char *assign_custom_variable_classes(const char *newval, bool doit,
161                                                            GucSource source);
162 static bool assign_debug_assertions(bool newval, bool doit, GucSource source);
163 static bool assign_ssl(bool newval, bool doit, GucSource source);
164 static bool assign_stage_log_stats(bool newval, bool doit, GucSource source);
165 static bool assign_log_stats(bool newval, bool doit, GucSource source);
166 static bool assign_transaction_read_only(bool newval, bool doit, GucSource source);
167 static const char *assign_canonical_path(const char *newval, bool doit, GucSource source);
168 static const char *assign_backslash_quote(const char *newval, bool doit, GucSource source);
169 static const char *assign_timezone_abbreviations(const char *newval, bool doit, GucSource source);
170 static const char *assign_xmlbinary(const char *newval, bool doit, GucSource source);
171 static const char *assign_xmloption(const char *newval, bool doit, GucSource source);
172
173 static bool assign_tcp_keepalives_idle(int newval, bool doit, GucSource source);
174 static bool assign_tcp_keepalives_interval(int newval, bool doit, GucSource source);
175 static bool assign_tcp_keepalives_count(int newval, bool doit, GucSource source);
176 static const char *show_tcp_keepalives_idle(void);
177 static const char *show_tcp_keepalives_interval(void);
178 static const char *show_tcp_keepalives_count(void);
179 static bool assign_autovacuum_max_workers(int newval, bool doit, GucSource source);
180 static bool assign_maxconnections(int newval, bool doit, GucSource source);
181
182 /*
183  * GUC option variables that are exported from this module
184  */
185 #ifdef USE_ASSERT_CHECKING
186 bool            assert_enabled = true;
187 #else
188 bool            assert_enabled = false;
189 #endif
190 bool            log_duration = false;
191 bool            Debug_print_plan = false;
192 bool            Debug_print_parse = false;
193 bool            Debug_print_rewritten = false;
194 bool            Debug_pretty_print = false;
195 bool            Explain_pretty_print = true;
196
197 bool            log_parser_stats = false;
198 bool            log_planner_stats = false;
199 bool            log_executor_stats = false;
200 bool            log_statement_stats = false;            /* this is sort of all three
201                                                                                                  * above together */
202 bool            log_btree_build_stats = false;
203
204 bool            check_function_bodies = true;
205 bool            default_with_oids = false;
206 bool            SQL_inheritance = true;
207
208 bool            Password_encryption = true;
209
210 int                     log_min_error_statement = ERROR;
211 int                     log_min_messages = NOTICE;
212 int                     client_min_messages = NOTICE;
213 int                     log_min_duration_statement = -1;
214 int                     log_temp_files = -1;
215
216 int                     num_temp_buffers = 1000;
217
218 char       *ConfigFileName;
219 char       *HbaFileName;
220 char       *IdentFileName;
221 char       *external_pid_file;
222
223 int                     tcp_keepalives_idle;
224 int                     tcp_keepalives_interval;
225 int                     tcp_keepalives_count;
226
227 /*
228  * These variables are all dummies that don't do anything, except in some
229  * cases provide the value for SHOW to display.  The real state is elsewhere
230  * and is kept in sync by assign_hooks.
231  */
232 static char *client_min_messages_str;
233 static char *log_min_messages_str;
234 static char *log_error_verbosity_str;
235 static char *log_statement_str;
236 static char *log_min_error_statement_str;
237 static char *log_destination_string;
238
239 #ifdef HAVE_SYSLOG
240 static char *syslog_facility_str;
241 static char *syslog_ident_str;
242 #endif
243 static bool phony_autocommit;
244 static bool session_auth_is_superuser;
245 static double phony_random_seed;
246 static char *backslash_quote_string;
247 static char *client_encoding_string;
248 static char *datestyle_string;
249 static char *default_iso_level_string;
250 static char *session_replication_role_string;
251 static char *locale_collate;
252 static char *locale_ctype;
253 static char *regex_flavor_string;
254 static char *server_encoding_string;
255 static char *server_version_string;
256 static int      server_version_num;
257 static char *timezone_string;
258 static char *log_timezone_string;
259 static char *timezone_abbreviations_string;
260 static char *XactIsoLevel_string;
261 static char *data_directory;
262 static char *custom_variable_classes;
263 static char *xmlbinary_string;
264 static char *xmloption_string;
265 static int      max_function_args;
266 static int      max_index_keys;
267 static int      max_identifier_length;
268 static int      block_size;
269 static bool integer_datetimes;
270
271 /* should be static, but commands/variable.c needs to get at these */
272 char       *role_string;
273 char       *session_authorization_string;
274
275
276 /*
277  * Displayable names for context types (enum GucContext)
278  *
279  * Note: these strings are deliberately not localized.
280  */
281 const char *const GucContext_Names[] =
282 {
283          /* PGC_INTERNAL */ "internal",
284          /* PGC_POSTMASTER */ "postmaster",
285          /* PGC_SIGHUP */ "sighup",
286          /* PGC_BACKEND */ "backend",
287          /* PGC_SUSET */ "superuser",
288          /* PGC_USERSET */ "user"
289 };
290
291 /*
292  * Displayable names for source types (enum GucSource)
293  *
294  * Note: these strings are deliberately not localized.
295  */
296 const char *const GucSource_Names[] =
297 {
298          /* PGC_S_DEFAULT */ "default",
299          /* PGC_S_ENV_VAR */ "environment variable",
300          /* PGC_S_FILE */ "configuration file",
301          /* PGC_S_ARGV */ "command line",
302          /* PGC_S_DATABASE */ "database",
303          /* PGC_S_USER */ "user",
304          /* PGC_S_CLIENT */ "client",
305          /* PGC_S_OVERRIDE */ "override",
306          /* PGC_S_INTERACTIVE */ "interactive",
307          /* PGC_S_TEST */ "test",
308          /* PGC_S_SESSION */ "session"
309 };
310
311 /*
312  * Displayable names for the groupings defined in enum config_group
313  */
314 const char *const config_group_names[] =
315 {
316         /* UNGROUPED */
317         gettext_noop("Ungrouped"),
318         /* FILE_LOCATIONS */
319         gettext_noop("File Locations"),
320         /* CONN_AUTH */
321         gettext_noop("Connections and Authentication"),
322         /* CONN_AUTH_SETTINGS */
323         gettext_noop("Connections and Authentication / Connection Settings"),
324         /* CONN_AUTH_SECURITY */
325         gettext_noop("Connections and Authentication / Security and Authentication"),
326         /* RESOURCES */
327         gettext_noop("Resource Usage"),
328         /* RESOURCES_MEM */
329         gettext_noop("Resource Usage / Memory"),
330         /* RESOURCES_FSM */
331         gettext_noop("Resource Usage / Free Space Map"),
332         /* RESOURCES_KERNEL */
333         gettext_noop("Resource Usage / Kernel Resources"),
334         /* WAL */
335         gettext_noop("Write-Ahead Log"),
336         /* WAL_SETTINGS */
337         gettext_noop("Write-Ahead Log / Settings"),
338         /* WAL_CHECKPOINTS */
339         gettext_noop("Write-Ahead Log / Checkpoints"),
340         /* QUERY_TUNING */
341         gettext_noop("Query Tuning"),
342         /* QUERY_TUNING_METHOD */
343         gettext_noop("Query Tuning / Planner Method Configuration"),
344         /* QUERY_TUNING_COST */
345         gettext_noop("Query Tuning / Planner Cost Constants"),
346         /* QUERY_TUNING_GEQO */
347         gettext_noop("Query Tuning / Genetic Query Optimizer"),
348         /* QUERY_TUNING_OTHER */
349         gettext_noop("Query Tuning / Other Planner Options"),
350         /* LOGGING */
351         gettext_noop("Reporting and Logging"),
352         /* LOGGING_WHERE */
353         gettext_noop("Reporting and Logging / Where to Log"),
354         /* LOGGING_WHEN */
355         gettext_noop("Reporting and Logging / When to Log"),
356         /* LOGGING_WHAT */
357         gettext_noop("Reporting and Logging / What to Log"),
358         /* STATS */
359         gettext_noop("Statistics"),
360         /* STATS_MONITORING */
361         gettext_noop("Statistics / Monitoring"),
362         /* STATS_COLLECTOR */
363         gettext_noop("Statistics / Query and Index Statistics Collector"),
364         /* AUTOVACUUM */
365         gettext_noop("Autovacuum"),
366         /* CLIENT_CONN */
367         gettext_noop("Client Connection Defaults"),
368         /* CLIENT_CONN_STATEMENT */
369         gettext_noop("Client Connection Defaults / Statement Behavior"),
370         /* CLIENT_CONN_LOCALE */
371         gettext_noop("Client Connection Defaults / Locale and Formatting"),
372         /* CLIENT_CONN_OTHER */
373         gettext_noop("Client Connection Defaults / Other Defaults"),
374         /* LOCK_MANAGEMENT */
375         gettext_noop("Lock Management"),
376         /* COMPAT_OPTIONS */
377         gettext_noop("Version and Platform Compatibility"),
378         /* COMPAT_OPTIONS_PREVIOUS */
379         gettext_noop("Version and Platform Compatibility / Previous PostgreSQL Versions"),
380         /* COMPAT_OPTIONS_CLIENT */
381         gettext_noop("Version and Platform Compatibility / Other Platforms and Clients"),
382         /* PRESET_OPTIONS */
383         gettext_noop("Preset Options"),
384         /* CUSTOM_OPTIONS */
385         gettext_noop("Customized Options"),
386         /* DEVELOPER_OPTIONS */
387         gettext_noop("Developer Options"),
388         /* help_config wants this array to be null-terminated */
389         NULL
390 };
391
392 /*
393  * Displayable names for GUC variable types (enum config_type)
394  *
395  * Note: these strings are deliberately not localized.
396  */
397 const char *const config_type_names[] =
398 {
399          /* PGC_BOOL */ "bool",
400          /* PGC_INT */ "integer",
401          /* PGC_REAL */ "real",
402          /* PGC_STRING */ "string"
403 };
404
405
406 /*
407  * Contents of GUC tables
408  *
409  * See src/backend/utils/misc/README for design notes.
410  *
411  * TO ADD AN OPTION:
412  *
413  * 1. Declare a global variable of type bool, int, double, or char*
414  *        and make use of it.
415  *
416  * 2. Decide at what times it's safe to set the option. See guc.h for
417  *        details.
418  *
419  * 3. Decide on a name, a default value, upper and lower bounds (if
420  *        applicable), etc.
421  *
422  * 4. Add a record below.
423  *
424  * 5. Add it to src/backend/utils/misc/postgresql.conf.sample, if
425  *        appropriate
426  *
427  * 6. Don't forget to document the option.
428  *
429  * 7. If it's a new GUC_LIST option you must edit pg_dumpall.c to ensure
430  *        it is not single quoted at dump time.
431  */
432
433
434 /******** option records follow ********/
435
436 static struct config_bool ConfigureNamesBool[] =
437 {
438         {
439                 {"enable_seqscan", PGC_USERSET, QUERY_TUNING_METHOD,
440                         gettext_noop("Enables the planner's use of sequential-scan plans."),
441                         NULL
442                 },
443                 &enable_seqscan,
444                 true, NULL, NULL
445         },
446         {
447                 {"enable_indexscan", PGC_USERSET, QUERY_TUNING_METHOD,
448                         gettext_noop("Enables the planner's use of index-scan plans."),
449                         NULL
450                 },
451                 &enable_indexscan,
452                 true, NULL, NULL
453         },
454         {
455                 {"enable_bitmapscan", PGC_USERSET, QUERY_TUNING_METHOD,
456                         gettext_noop("Enables the planner's use of bitmap-scan plans."),
457                         NULL
458                 },
459                 &enable_bitmapscan,
460                 true, NULL, NULL
461         },
462         {
463                 {"enable_tidscan", PGC_USERSET, QUERY_TUNING_METHOD,
464                         gettext_noop("Enables the planner's use of TID scan plans."),
465                         NULL
466                 },
467                 &enable_tidscan,
468                 true, NULL, NULL
469         },
470         {
471                 {"enable_sort", PGC_USERSET, QUERY_TUNING_METHOD,
472                         gettext_noop("Enables the planner's use of explicit sort steps."),
473                         NULL
474                 },
475                 &enable_sort,
476                 true, NULL, NULL
477         },
478         {
479                 {"enable_hashagg", PGC_USERSET, QUERY_TUNING_METHOD,
480                         gettext_noop("Enables the planner's use of hashed aggregation plans."),
481                         NULL
482                 },
483                 &enable_hashagg,
484                 true, NULL, NULL
485         },
486         {
487                 {"enable_nestloop", PGC_USERSET, QUERY_TUNING_METHOD,
488                         gettext_noop("Enables the planner's use of nested-loop join plans."),
489                         NULL
490                 },
491                 &enable_nestloop,
492                 true, NULL, NULL
493         },
494         {
495                 {"enable_mergejoin", PGC_USERSET, QUERY_TUNING_METHOD,
496                         gettext_noop("Enables the planner's use of merge join plans."),
497                         NULL
498                 },
499                 &enable_mergejoin,
500                 true, NULL, NULL
501         },
502         {
503                 {"enable_hashjoin", PGC_USERSET, QUERY_TUNING_METHOD,
504                         gettext_noop("Enables the planner's use of hash join plans."),
505                         NULL
506                 },
507                 &enable_hashjoin,
508                 true, NULL, NULL
509         },
510         {
511                 {"constraint_exclusion", PGC_USERSET, QUERY_TUNING_OTHER,
512                         gettext_noop("Enables the planner to use constraints to optimize queries."),
513                         gettext_noop("Child table scans will be skipped if their "
514                                            "constraints guarantee that no rows match the query.")
515                 },
516                 &constraint_exclusion,
517                 false, NULL, NULL
518         },
519         {
520                 {"geqo", PGC_USERSET, QUERY_TUNING_GEQO,
521                         gettext_noop("Enables genetic query optimization."),
522                         gettext_noop("This algorithm attempts to do planning without "
523                                                  "exhaustive searching.")
524                 },
525                 &enable_geqo,
526                 true, NULL, NULL
527         },
528         {
529                 /* Not for general use --- used by SET SESSION AUTHORIZATION */
530                 {"is_superuser", PGC_INTERNAL, UNGROUPED,
531                         gettext_noop("Shows whether the current user is a superuser."),
532                         NULL,
533                         GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
534                 },
535                 &session_auth_is_superuser,
536                 false, NULL, NULL
537         },
538         {
539                 {"ssl", PGC_POSTMASTER, CONN_AUTH_SECURITY,
540                         gettext_noop("Enables SSL connections."),
541                         NULL
542                 },
543                 &EnableSSL,
544                 false, assign_ssl, NULL
545         },
546         {
547                 {"fsync", PGC_SIGHUP, WAL_SETTINGS,
548                         gettext_noop("Forces synchronization of updates to disk."),
549                         gettext_noop("The server will use the fsync() system call in several places to make "
550                         "sure that updates are physically written to disk. This insures "
551                                                  "that a database cluster will recover to a consistent state after "
552                                                  "an operating system or hardware crash.")
553                 },
554                 &enableFsync,
555                 true, NULL, NULL
556         },
557         {
558                 {"synchronous_commit", PGC_USERSET, WAL_SETTINGS,
559                         gettext_noop("Sets immediate fsync at commit."),
560                         NULL
561                 },
562                 &XactSyncCommit,
563                 /* XXX TEMPORARY FOR TESTING: sync commit should default to TRUE! */
564                 false, NULL, NULL
565         },
566         {
567                 {"zero_damaged_pages", PGC_SUSET, DEVELOPER_OPTIONS,
568                         gettext_noop("Continues processing past damaged page headers."),
569                         gettext_noop("Detection of a damaged page header normally causes PostgreSQL to "
570                                 "report an error, aborting the current transaction. Setting "
571                                                  "zero_damaged_pages to true causes the system to instead report a "
572                                                  "warning, zero out the damaged page, and continue processing. This "
573                                                  "behavior will destroy data, namely all the rows on the damaged page."),
574                         GUC_NOT_IN_SAMPLE
575                 },
576                 &zero_damaged_pages,
577                 false, NULL, NULL
578         },
579         {
580                 {"full_page_writes", PGC_SIGHUP, WAL_SETTINGS,
581                         gettext_noop("Writes full pages to WAL when first modified after a checkpoint."),
582                         gettext_noop("A page write in process during an operating system crash might be "
583                                                  "only partially written to disk.  During recovery, the row changes "
584                           "stored in WAL are not enough to recover.  This option writes "
585                                                  "pages when first modified after a checkpoint to WAL so full recovery "
586                                                  "is possible.")
587                 },
588                 &fullPageWrites,
589                 true, NULL, NULL
590         },
591         {
592                 {"silent_mode", PGC_POSTMASTER, LOGGING_WHEN,
593                         gettext_noop("Runs the server silently."),
594                         gettext_noop("If this parameter is set, the server will automatically run in the "
595                                  "background and any controlling terminals are dissociated.")
596                 },
597                 &SilentMode,
598                 false, NULL, NULL
599         },
600         {
601                 {"log_checkpoints", PGC_SIGHUP, LOGGING_WHAT,
602                         gettext_noop("Logs each checkpoint."),
603                         NULL
604                 },
605                 &log_checkpoints,
606                 false, NULL, NULL
607         },
608         {
609                 {"log_connections", PGC_BACKEND, LOGGING_WHAT,
610                         gettext_noop("Logs each successful connection."),
611                         NULL
612                 },
613                 &Log_connections,
614                 false, NULL, NULL
615         },
616         {
617                 {"log_disconnections", PGC_BACKEND, LOGGING_WHAT,
618                         gettext_noop("Logs end of a session, including duration."),
619                         NULL
620                 },
621                 &Log_disconnections,
622                 false, NULL, NULL
623         },
624         {
625                 {"debug_assertions", PGC_USERSET, DEVELOPER_OPTIONS,
626                         gettext_noop("Turns on various assertion checks."),
627                         gettext_noop("This is a debugging aid."),
628                         GUC_NOT_IN_SAMPLE
629                 },
630                 &assert_enabled,
631 #ifdef USE_ASSERT_CHECKING
632                 true,
633 #else
634                 false,
635 #endif
636                 assign_debug_assertions, NULL
637         },
638         {
639                 /* currently undocumented, so don't show in SHOW ALL */
640                 {"exit_on_error", PGC_USERSET, UNGROUPED,
641                         gettext_noop("No description available."),
642                         NULL,
643                         GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE
644                 },
645                 &ExitOnAnyError,
646                 false, NULL, NULL
647         },
648         {
649                 {"log_duration", PGC_SUSET, LOGGING_WHAT,
650                         gettext_noop("Logs the duration of each completed SQL statement."),
651                         NULL
652                 },
653                 &log_duration,
654                 false, NULL, NULL
655         },
656         {
657                 {"debug_print_parse", PGC_USERSET, LOGGING_WHAT,
658                         gettext_noop("Prints the parse tree to the server log."),
659                         NULL
660                 },
661                 &Debug_print_parse,
662                 false, NULL, NULL
663         },
664         {
665                 {"debug_print_rewritten", PGC_USERSET, LOGGING_WHAT,
666                         gettext_noop("Prints the parse tree after rewriting to server log."),
667                         NULL
668                 },
669                 &Debug_print_rewritten,
670                 false, NULL, NULL
671         },
672         {
673                 {"debug_print_plan", PGC_USERSET, LOGGING_WHAT,
674                         gettext_noop("Prints the execution plan to server log."),
675                         NULL
676                 },
677                 &Debug_print_plan,
678                 false, NULL, NULL
679         },
680         {
681                 {"debug_pretty_print", PGC_USERSET, LOGGING_WHAT,
682                         gettext_noop("Indents parse and plan tree displays."),
683                         NULL
684                 },
685                 &Debug_pretty_print,
686                 false, NULL, NULL
687         },
688         {
689                 {"log_parser_stats", PGC_SUSET, STATS_MONITORING,
690                         gettext_noop("Writes parser performance statistics to the server log."),
691                         NULL
692                 },
693                 &log_parser_stats,
694                 false, assign_stage_log_stats, NULL
695         },
696         {
697                 {"log_planner_stats", PGC_SUSET, STATS_MONITORING,
698                         gettext_noop("Writes planner performance statistics to the server log."),
699                         NULL
700                 },
701                 &log_planner_stats,
702                 false, assign_stage_log_stats, NULL
703         },
704         {
705                 {"log_executor_stats", PGC_SUSET, STATS_MONITORING,
706                         gettext_noop("Writes executor performance statistics to the server log."),
707                         NULL
708                 },
709                 &log_executor_stats,
710                 false, assign_stage_log_stats, NULL
711         },
712         {
713                 {"log_statement_stats", PGC_SUSET, STATS_MONITORING,
714                         gettext_noop("Writes cumulative performance statistics to the server log."),
715                         NULL
716                 },
717                 &log_statement_stats,
718                 false, assign_log_stats, NULL
719         },
720 #ifdef BTREE_BUILD_STATS
721         {
722                 {"log_btree_build_stats", PGC_SUSET, DEVELOPER_OPTIONS,
723                         gettext_noop("No description available."),
724                         NULL,
725                         GUC_NOT_IN_SAMPLE
726                 },
727                 &log_btree_build_stats,
728                 false, NULL, NULL
729         },
730 #endif
731
732         {
733                 {"explain_pretty_print", PGC_USERSET, CLIENT_CONN_OTHER,
734                         gettext_noop("Uses the indented output format for EXPLAIN VERBOSE."),
735                         NULL
736                 },
737                 &Explain_pretty_print,
738                 true, NULL, NULL
739         },
740         {
741                 {"stats_start_collector", PGC_POSTMASTER, STATS_COLLECTOR,
742                         gettext_noop("Starts the server statistics-collection subprocess."),
743                         NULL
744                 },
745                 &pgstat_collect_startcollector,
746                 true, NULL, NULL
747         },
748         {
749                 {"stats_reset_on_server_start", PGC_POSTMASTER, STATS_COLLECTOR,
750                         gettext_noop("Zeroes collected statistics on server restart."),
751                         NULL
752                 },
753                 &pgstat_collect_resetonpmstart,
754                 false, NULL, NULL
755         },
756         {
757                 {"stats_row_level", PGC_SUSET, STATS_COLLECTOR,
758                         gettext_noop("Collects row-level statistics on database activity."),
759                         NULL
760                 },
761                 &pgstat_collect_tuplelevel,
762                 true, NULL, NULL
763         },
764         {
765                 {"stats_block_level", PGC_SUSET, STATS_COLLECTOR,
766                         gettext_noop("Collects block-level statistics on database activity."),
767                         NULL
768                 },
769                 &pgstat_collect_blocklevel,
770                 false, NULL, NULL
771         },
772
773         {
774                 {"stats_command_string", PGC_SUSET, STATS_COLLECTOR,
775                         gettext_noop("Collects information about executing commands."),
776                         gettext_noop("Enables the collection of information on the currently "
777                                         "executing command of each session, along with the time "
778                                                  "at which that command began execution.")
779                 },
780                 &pgstat_collect_querystring,
781                 true, NULL, NULL
782         },
783
784         {
785                 {"update_process_title", PGC_SUSET, STATS_COLLECTOR,
786                         gettext_noop("Updates the process title to show the active SQL command."),
787                         gettext_noop("Enables updating of the process title every time a new SQL command is received by the server.")
788                 },
789                 &update_process_title,
790                 true, NULL, NULL
791         },
792
793         {
794                 {"autovacuum", PGC_SIGHUP, AUTOVACUUM,
795                         gettext_noop("Starts the autovacuum subprocess."),
796                         NULL
797                 },
798                 &autovacuum_start_daemon,
799                 true, NULL, NULL
800         },
801
802         {
803                 {"trace_notify", PGC_USERSET, DEVELOPER_OPTIONS,
804                         gettext_noop("Generates debugging output for LISTEN and NOTIFY."),
805                         NULL,
806                         GUC_NOT_IN_SAMPLE
807                 },
808                 &Trace_notify,
809                 false, NULL, NULL
810         },
811
812 #ifdef LOCK_DEBUG
813         {
814                 {"trace_locks", PGC_SUSET, DEVELOPER_OPTIONS,
815                         gettext_noop("No description available."),
816                         NULL,
817                         GUC_NOT_IN_SAMPLE
818                 },
819                 &Trace_locks,
820                 false, NULL, NULL
821         },
822         {
823                 {"trace_userlocks", PGC_SUSET, DEVELOPER_OPTIONS,
824                         gettext_noop("No description available."),
825                         NULL,
826                         GUC_NOT_IN_SAMPLE
827                 },
828                 &Trace_userlocks,
829                 false, NULL, NULL
830         },
831         {
832                 {"trace_lwlocks", PGC_SUSET, DEVELOPER_OPTIONS,
833                         gettext_noop("No description available."),
834                         NULL,
835                         GUC_NOT_IN_SAMPLE
836                 },
837                 &Trace_lwlocks,
838                 false, NULL, NULL
839         },
840         {
841                 {"debug_deadlocks", PGC_SUSET, DEVELOPER_OPTIONS,
842                         gettext_noop("No description available."),
843                         NULL,
844                         GUC_NOT_IN_SAMPLE
845                 },
846                 &Debug_deadlocks,
847                 false, NULL, NULL
848         },
849 #endif
850
851         {
852                 {"log_lock_waits", PGC_SUSET, LOGGING_WHAT,
853                         gettext_noop("Logs long lock waits."),
854                         NULL
855                 },
856                 &log_lock_waits,
857                 false, NULL, NULL
858         },
859
860         {
861                 {"log_hostname", PGC_SIGHUP, LOGGING_WHAT,
862                         gettext_noop("Logs the host name in the connection logs."),
863                         gettext_noop("By default, connection logs only show the IP address "
864                                                  "of the connecting host. If you want them to show the host name you "
865                           "can turn this on, but depending on your host name resolution "
866                            "setup it might impose a non-negligible performance penalty.")
867                 },
868                 &log_hostname,
869                 false, NULL, NULL
870         },
871         {
872                 {"sql_inheritance", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
873                         gettext_noop("Causes subtables to be included by default in various commands."),
874                         NULL
875                 },
876                 &SQL_inheritance,
877                 true, NULL, NULL
878         },
879         {
880                 {"password_encryption", PGC_USERSET, CONN_AUTH_SECURITY,
881                         gettext_noop("Encrypt passwords."),
882                         gettext_noop("When a password is specified in CREATE USER or "
883                            "ALTER USER without writing either ENCRYPTED or UNENCRYPTED, "
884                                                  "this parameter determines whether the password is to be encrypted.")
885                 },
886                 &Password_encryption,
887                 true, NULL, NULL
888         },
889         {
890                 {"transform_null_equals", PGC_USERSET, COMPAT_OPTIONS_CLIENT,
891                         gettext_noop("Treats \"expr=NULL\" as \"expr IS NULL\"."),
892                         gettext_noop("When turned on, expressions of the form expr = NULL "
893                            "(or NULL = expr) are treated as expr IS NULL, that is, they "
894                                 "return true if expr evaluates to the null value, and false "
895                            "otherwise. The correct behavior of expr = NULL is to always "
896                                                  "return null (unknown).")
897                 },
898                 &Transform_null_equals,
899                 false, NULL, NULL
900         },
901         {
902                 {"db_user_namespace", PGC_SIGHUP, CONN_AUTH_SECURITY,
903                         gettext_noop("Enables per-database user names."),
904                         NULL
905                 },
906                 &Db_user_namespace,
907                 false, NULL, NULL
908         },
909         {
910                 /* only here for backwards compatibility */
911                 {"autocommit", PGC_USERSET, CLIENT_CONN_STATEMENT,
912                         gettext_noop("This parameter doesn't do anything."),
913                         gettext_noop("It's just here so that we won't choke on SET AUTOCOMMIT TO ON from 7.3-vintage clients."),
914                         GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE
915                 },
916                 &phony_autocommit,
917                 true, assign_phony_autocommit, NULL
918         },
919         {
920                 {"default_transaction_read_only", PGC_USERSET, CLIENT_CONN_STATEMENT,
921                         gettext_noop("Sets the default read-only status of new transactions."),
922                         NULL
923                 },
924                 &DefaultXactReadOnly,
925                 false, NULL, NULL
926         },
927         {
928                 {"transaction_read_only", PGC_USERSET, CLIENT_CONN_STATEMENT,
929                         gettext_noop("Sets the current transaction's read-only status."),
930                         NULL,
931                         GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
932                 },
933                 &XactReadOnly,
934                 false, assign_transaction_read_only, NULL
935         },
936         {
937                 {"add_missing_from", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
938                         gettext_noop("Automatically adds missing table references to FROM clauses."),
939                         NULL
940                 },
941                 &add_missing_from,
942                 false, NULL, NULL
943         },
944         {
945                 {"check_function_bodies", PGC_USERSET, CLIENT_CONN_STATEMENT,
946                         gettext_noop("Check function bodies during CREATE FUNCTION."),
947                         NULL
948                 },
949                 &check_function_bodies,
950                 true, NULL, NULL
951         },
952         {
953                 {"array_nulls", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
954                         gettext_noop("Enable input of NULL elements in arrays."),
955                         gettext_noop("When turned on, unquoted NULL in an array input "
956                                                  "value means a null value; "
957                                                  "otherwise it is taken literally.")
958                 },
959                 &Array_nulls,
960                 true, NULL, NULL
961         },
962         {
963                 {"default_with_oids", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
964                         gettext_noop("Create new tables with OIDs by default."),
965                         NULL
966                 },
967                 &default_with_oids,
968                 false, NULL, NULL
969         },
970         {
971                 {"logging_collector", PGC_POSTMASTER, LOGGING_WHERE,
972                         gettext_noop("Start a subprocess to capture stderr output and/or csvlogs into log files."),
973                         NULL
974                 },
975                 &Logging_collector,
976                 false, NULL, NULL
977         },
978         {
979                 {"log_truncate_on_rotation", PGC_SIGHUP, LOGGING_WHERE,
980                         gettext_noop("Truncate existing log files of same name during log rotation."),
981                         NULL
982                 },
983                 &Log_truncate_on_rotation,
984                 false, NULL, NULL
985         },
986
987 #ifdef TRACE_SORT
988         {
989                 {"trace_sort", PGC_USERSET, DEVELOPER_OPTIONS,
990                         gettext_noop("Emit information about resource usage in sorting."),
991                         NULL,
992                         GUC_NOT_IN_SAMPLE
993                 },
994                 &trace_sort,
995                 false, NULL, NULL
996         },
997 #endif
998
999 #ifdef TRACE_SYNCSCAN
1000         /* this is undocumented because not exposed in a standard build */
1001         {
1002                 {"trace_syncscan", PGC_USERSET, DEVELOPER_OPTIONS,
1003                         gettext_noop("Generate debugging output for synchronized scanning."),
1004                         NULL,
1005                         GUC_NOT_IN_SAMPLE
1006                 },
1007                 &trace_syncscan,
1008                 false, NULL, NULL
1009         },
1010 #endif
1011
1012 #ifdef DEBUG_BOUNDED_SORT
1013         /* this is undocumented because not exposed in a standard build */
1014         {
1015                 {
1016                         "optimize_bounded_sort", PGC_USERSET, QUERY_TUNING_METHOD,
1017                         gettext_noop("Enable bounded sorting using heap sort."),
1018                         NULL,
1019                         GUC_NOT_IN_SAMPLE
1020                 },
1021                 &optimize_bounded_sort,
1022                 true, NULL, NULL
1023         },
1024 #endif
1025
1026 #ifdef WAL_DEBUG
1027         {
1028                 {"wal_debug", PGC_SUSET, DEVELOPER_OPTIONS,
1029                         gettext_noop("Emit WAL-related debugging output."),
1030                         NULL,
1031                         GUC_NOT_IN_SAMPLE
1032                 },
1033                 &XLOG_DEBUG,
1034                 false, NULL, NULL
1035         },
1036 #endif
1037
1038         {
1039                 {"integer_datetimes", PGC_INTERNAL, PRESET_OPTIONS,
1040                         gettext_noop("Datetimes are integer based."),
1041                         NULL,
1042                         GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1043                 },
1044                 &integer_datetimes,
1045 #ifdef HAVE_INT64_TIMESTAMP
1046                 true, NULL, NULL
1047 #else
1048                 false, NULL, NULL
1049 #endif
1050         },
1051
1052         {
1053                 {"krb_caseins_users", PGC_POSTMASTER, CONN_AUTH_SECURITY,
1054                         gettext_noop("Sets whether Kerberos and GSSAPI user names should be treated as case-insensitive."),
1055                         NULL
1056                 },
1057                 &pg_krb_caseins_users,
1058                 false, NULL, NULL
1059         },
1060
1061         {
1062                 {"escape_string_warning", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1063                         gettext_noop("Warn about backslash escapes in ordinary string literals."),
1064                         NULL
1065                 },
1066                 &escape_string_warning,
1067                 true, NULL, NULL
1068         },
1069
1070         {
1071                 {"standard_conforming_strings", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1072                         gettext_noop("Causes '...' strings to treat backslashes literally."),
1073                         NULL,
1074                         GUC_REPORT
1075                 },
1076                 &standard_conforming_strings,
1077                 false, NULL, NULL
1078         },
1079
1080         {
1081                 {"allow_system_table_mods", PGC_POSTMASTER, DEVELOPER_OPTIONS,
1082                         gettext_noop("Allows modifications of the structure of system tables."),
1083                         NULL,
1084                         GUC_NOT_IN_SAMPLE
1085                 },
1086                 &allowSystemTableMods,
1087                 false, NULL, NULL
1088         },
1089
1090         {
1091                 {"ignore_system_indexes", PGC_BACKEND, DEVELOPER_OPTIONS,
1092                         gettext_noop("Disables reading from system indexes."),
1093                         gettext_noop("It does not prevent updating the indexes, so it is safe "
1094                                                  "to use.  The worst consequence is slowness."),
1095                         GUC_NOT_IN_SAMPLE
1096                 },
1097                 &IgnoreSystemIndexes,
1098                 false, NULL, NULL
1099         },
1100
1101         /* End-of-list marker */
1102         {
1103                 {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL
1104         }
1105 };
1106
1107
1108 static struct config_int ConfigureNamesInt[] =
1109 {
1110         {
1111                 {"archive_timeout", PGC_SIGHUP, WAL_SETTINGS,
1112                         gettext_noop("Forces a switch to the next xlog file if a "
1113                                                  "new file has not been started within N seconds."),
1114                         NULL,
1115                         GUC_UNIT_S
1116                 },
1117                 &XLogArchiveTimeout,
1118                 0, 0, INT_MAX, NULL, NULL
1119         },
1120         {
1121                 {"post_auth_delay", PGC_BACKEND, DEVELOPER_OPTIONS,
1122                         gettext_noop("Waits N seconds on connection startup after authentication."),
1123                         gettext_noop("This allows attaching a debugger to the process."),
1124                         GUC_NOT_IN_SAMPLE | GUC_UNIT_S
1125                 },
1126                 &PostAuthDelay,
1127                 0, 0, INT_MAX, NULL, NULL
1128         },
1129         {
1130                 {"default_statistics_target", PGC_USERSET, QUERY_TUNING_OTHER,
1131                         gettext_noop("Sets the default statistics target."),
1132                         gettext_noop("This applies to table columns that have not had a "
1133                                 "column-specific target set via ALTER TABLE SET STATISTICS.")
1134                 },
1135                 &default_statistics_target,
1136                 10, 1, 1000, NULL, NULL
1137         },
1138         {
1139                 {"from_collapse_limit", PGC_USERSET, QUERY_TUNING_OTHER,
1140                         gettext_noop("Sets the FROM-list size beyond which subqueries "
1141                                                  "are not collapsed."),
1142                         gettext_noop("The planner will merge subqueries into upper "
1143                                 "queries if the resulting FROM list would have no more than "
1144                                                  "this many items.")
1145                 },
1146                 &from_collapse_limit,
1147                 8, 1, INT_MAX, NULL, NULL
1148         },
1149         {
1150                 {"join_collapse_limit", PGC_USERSET, QUERY_TUNING_OTHER,
1151                         gettext_noop("Sets the FROM-list size beyond which JOIN "
1152                                                  "constructs are not flattened."),
1153                         gettext_noop("The planner will flatten explicit JOIN "
1154                                                  "constructs into lists of FROM items whenever a "
1155                                                  "list of no more than this many items would result.")
1156                 },
1157                 &join_collapse_limit,
1158                 8, 1, INT_MAX, NULL, NULL
1159         },
1160         {
1161                 {"geqo_threshold", PGC_USERSET, QUERY_TUNING_GEQO,
1162                         gettext_noop("Sets the threshold of FROM items beyond which GEQO is used."),
1163                         NULL
1164                 },
1165                 &geqo_threshold,
1166                 12, 2, INT_MAX, NULL, NULL
1167         },
1168         {
1169                 {"geqo_effort", PGC_USERSET, QUERY_TUNING_GEQO,
1170                         gettext_noop("GEQO: effort is used to set the default for other GEQO parameters."),
1171                         NULL
1172                 },
1173                 &Geqo_effort,
1174                 DEFAULT_GEQO_EFFORT, MIN_GEQO_EFFORT, MAX_GEQO_EFFORT, NULL, NULL
1175         },
1176         {
1177                 {"geqo_pool_size", PGC_USERSET, QUERY_TUNING_GEQO,
1178                         gettext_noop("GEQO: number of individuals in the population."),
1179                         gettext_noop("Zero selects a suitable default value.")
1180                 },
1181                 &Geqo_pool_size,
1182                 0, 0, INT_MAX, NULL, NULL
1183         },
1184         {
1185                 {"geqo_generations", PGC_USERSET, QUERY_TUNING_GEQO,
1186                         gettext_noop("GEQO: number of iterations of the algorithm."),
1187                         gettext_noop("Zero selects a suitable default value.")
1188                 },
1189                 &Geqo_generations,
1190                 0, 0, INT_MAX, NULL, NULL
1191         },
1192
1193         {
1194                 {"deadlock_timeout", PGC_SIGHUP, LOCK_MANAGEMENT,
1195                         gettext_noop("Sets the time to wait on a lock before checking for deadlock."),
1196                         NULL,
1197                         GUC_UNIT_MS
1198                 },
1199                 &DeadlockTimeout,
1200                 1000, 1, INT_MAX/1000, NULL, NULL
1201         },
1202
1203         /*
1204          * Note: There is some postprocessing done in PostmasterMain() to make
1205          * sure the buffers are at least twice the number of backends, so the
1206          * constraints here are partially unused. Similarly, the superuser
1207          * reserved number is checked to ensure it is less than the max backends
1208          * number.
1209          *
1210          * MaxBackends is limited to INT_MAX/4 because some places compute
1211          * 4*MaxBackends without any overflow check.  This check is made on
1212          * assign_maxconnections, since MaxBackends is computed as MaxConnections +
1213          * autovacuum_max_workers.
1214          *
1215          * Likewise we have to limit NBuffers to INT_MAX/2.
1216          */
1217         {
1218                 {"max_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1219                         gettext_noop("Sets the maximum number of concurrent connections."),
1220                         NULL
1221                 },
1222                 &MaxConnections,
1223                 100, 1, INT_MAX / 4, assign_maxconnections, NULL
1224         },
1225
1226         {
1227                 {"superuser_reserved_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1228                         gettext_noop("Sets the number of connection slots reserved for superusers."),
1229                         NULL
1230                 },
1231                 &ReservedBackends,
1232                 3, 0, INT_MAX / 4, NULL, NULL
1233         },
1234
1235         {
1236                 {"shared_buffers", PGC_POSTMASTER, RESOURCES_MEM,
1237                         gettext_noop("Sets the number of shared memory buffers used by the server."),
1238                         NULL,
1239                         GUC_UNIT_BLOCKS
1240                 },
1241                 &NBuffers,
1242                 1024, 16, INT_MAX / 2, NULL, NULL
1243         },
1244
1245         {
1246                 {"temp_buffers", PGC_USERSET, RESOURCES_MEM,
1247                         gettext_noop("Sets the maximum number of temporary buffers used by each session."),
1248                         NULL,
1249                         GUC_UNIT_BLOCKS
1250                 },
1251                 &num_temp_buffers,
1252                 1024, 100, INT_MAX / 2, NULL, show_num_temp_buffers
1253         },
1254
1255         {
1256                 {"port", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1257                         gettext_noop("Sets the TCP port the server listens on."),
1258                         NULL
1259                 },
1260                 &PostPortNumber,
1261                 DEF_PGPORT, 1, 65535, NULL, NULL
1262         },
1263
1264         {
1265                 {"unix_socket_permissions", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1266                         gettext_noop("Sets the access permissions of the Unix-domain socket."),
1267                         gettext_noop("Unix-domain sockets use the usual Unix file system "
1268                                                  "permission set. The parameter value is expected to be an numeric mode "
1269                                                  "specification in the form accepted by the chmod and umask system "
1270                                                  "calls. (To use the customary octal format the number must start with "
1271                                                  "a 0 (zero).)")
1272                 },
1273                 &Unix_socket_permissions,
1274                 0777, 0000, 0777, NULL, NULL
1275         },
1276
1277         {
1278                 {"work_mem", PGC_USERSET, RESOURCES_MEM,
1279                         gettext_noop("Sets the maximum memory to be used for query workspaces."),
1280                         gettext_noop("This much memory can be used by each internal "
1281                                                  "sort operation and hash table before switching to "
1282                                                  "temporary disk files."),
1283                         GUC_UNIT_KB
1284                 },
1285                 &work_mem,
1286                 1024, 8 * BLCKSZ / 1024, MAX_KILOBYTES, NULL, NULL
1287         },
1288
1289         {
1290                 {"maintenance_work_mem", PGC_USERSET, RESOURCES_MEM,
1291                         gettext_noop("Sets the maximum memory to be used for maintenance operations."),
1292                         gettext_noop("This includes operations such as VACUUM and CREATE INDEX."),
1293                         GUC_UNIT_KB
1294                 },
1295                 &maintenance_work_mem,
1296                 16384, 1024, MAX_KILOBYTES, NULL, NULL
1297         },
1298
1299         {
1300                 {"max_stack_depth", PGC_SUSET, RESOURCES_MEM,
1301                         gettext_noop("Sets the maximum stack depth, in kilobytes."),
1302                         NULL,
1303                         GUC_UNIT_KB
1304                 },
1305                 &max_stack_depth,
1306                 100, 100, MAX_KILOBYTES, assign_max_stack_depth, NULL
1307         },
1308
1309         {
1310                 {"vacuum_cost_page_hit", PGC_USERSET, RESOURCES,
1311                         gettext_noop("Vacuum cost for a page found in the buffer cache."),
1312                         NULL
1313                 },
1314                 &VacuumCostPageHit,
1315                 1, 0, 10000, NULL, NULL
1316         },
1317
1318         {
1319                 {"vacuum_cost_page_miss", PGC_USERSET, RESOURCES,
1320                         gettext_noop("Vacuum cost for a page not found in the buffer cache."),
1321                         NULL
1322                 },
1323                 &VacuumCostPageMiss,
1324                 10, 0, 10000, NULL, NULL
1325         },
1326
1327         {
1328                 {"vacuum_cost_page_dirty", PGC_USERSET, RESOURCES,
1329                         gettext_noop("Vacuum cost for a page dirtied by vacuum."),
1330                         NULL
1331                 },
1332                 &VacuumCostPageDirty,
1333                 20, 0, 10000, NULL, NULL
1334         },
1335
1336         {
1337                 {"vacuum_cost_limit", PGC_USERSET, RESOURCES,
1338                         gettext_noop("Vacuum cost amount available before napping."),
1339                         NULL
1340                 },
1341                 &VacuumCostLimit,
1342                 200, 1, 10000, NULL, NULL
1343         },
1344
1345         {
1346                 {"vacuum_cost_delay", PGC_USERSET, RESOURCES,
1347                         gettext_noop("Vacuum cost delay in milliseconds."),
1348                         NULL,
1349                         GUC_UNIT_MS
1350                 },
1351                 &VacuumCostDelay,
1352                 0, 0, 1000, NULL, NULL
1353         },
1354
1355         {
1356                 {"autovacuum_vacuum_cost_delay", PGC_SIGHUP, AUTOVACUUM,
1357                         gettext_noop("Vacuum cost delay in milliseconds, for autovacuum."),
1358                         NULL,
1359                         GUC_UNIT_MS
1360                 },
1361                 &autovacuum_vac_cost_delay,
1362                 20, -1, 1000, NULL, NULL
1363         },
1364
1365         {
1366                 {"autovacuum_vacuum_cost_limit", PGC_SIGHUP, AUTOVACUUM,
1367                         gettext_noop("Vacuum cost amount available before napping, for autovacuum."),
1368                         NULL
1369                 },
1370                 &autovacuum_vac_cost_limit,
1371                 -1, -1, 10000, NULL, NULL
1372         },
1373
1374         {
1375                 {"max_files_per_process", PGC_POSTMASTER, RESOURCES_KERNEL,
1376                         gettext_noop("Sets the maximum number of simultaneously open files for each server process."),
1377                         NULL
1378                 },
1379                 &max_files_per_process,
1380                 1000, 25, INT_MAX, NULL, NULL
1381         },
1382
1383         {
1384                 {"max_prepared_transactions", PGC_POSTMASTER, RESOURCES,
1385                         gettext_noop("Sets the maximum number of simultaneously prepared transactions."),
1386                         NULL
1387                 },
1388                 &max_prepared_xacts,
1389                 5, 0, INT_MAX, NULL, NULL
1390         },
1391
1392 #ifdef LOCK_DEBUG
1393         {
1394                 {"trace_lock_oidmin", PGC_SUSET, DEVELOPER_OPTIONS,
1395                         gettext_noop("No description available."),
1396                         NULL,
1397                         GUC_NOT_IN_SAMPLE
1398                 },
1399                 &Trace_lock_oidmin,
1400                 FirstNormalObjectId, 0, INT_MAX, NULL, NULL
1401         },
1402         {
1403                 {"trace_lock_table", PGC_SUSET, DEVELOPER_OPTIONS,
1404                         gettext_noop("No description available."),
1405                         NULL,
1406                         GUC_NOT_IN_SAMPLE
1407                 },
1408                 &Trace_lock_table,
1409                 0, 0, INT_MAX, NULL, NULL
1410         },
1411 #endif
1412
1413         {
1414                 {"statement_timeout", PGC_USERSET, CLIENT_CONN_STATEMENT,
1415                         gettext_noop("Sets the maximum allowed duration of any statement."),
1416                         gettext_noop("A value of 0 turns off the timeout."),
1417                         GUC_UNIT_MS
1418                 },
1419                 &StatementTimeout,
1420                 0, 0, INT_MAX, NULL, NULL
1421         },
1422
1423         {
1424                 {"vacuum_freeze_min_age", PGC_USERSET, CLIENT_CONN_STATEMENT,
1425                         gettext_noop("Minimum age at which VACUUM should freeze a table row."),
1426                         NULL
1427                 },
1428                 &vacuum_freeze_min_age,
1429                 100000000, 0, 1000000000, NULL, NULL
1430         },
1431
1432         {
1433                 {"max_fsm_relations", PGC_POSTMASTER, RESOURCES_FSM,
1434                         gettext_noop("Sets the maximum number of tables and indexes for which free space is tracked."),
1435                         NULL
1436                 },
1437                 &MaxFSMRelations,
1438                 1000, 100, INT_MAX, NULL, NULL
1439         },
1440         {
1441                 {"max_fsm_pages", PGC_POSTMASTER, RESOURCES_FSM,
1442                         gettext_noop("Sets the maximum number of disk pages for which free space is tracked."),
1443                         NULL
1444                 },
1445                 &MaxFSMPages,
1446                 20000, 1000, INT_MAX, NULL, NULL
1447         },
1448
1449         {
1450                 {"max_locks_per_transaction", PGC_POSTMASTER, LOCK_MANAGEMENT,
1451                         gettext_noop("Sets the maximum number of locks per transaction."),
1452                         gettext_noop("The shared lock table is sized on the assumption that "
1453                           "at most max_locks_per_transaction * max_connections distinct "
1454                                                  "objects will need to be locked at any one time.")
1455                 },
1456                 &max_locks_per_xact,
1457                 64, 10, INT_MAX, NULL, NULL
1458         },
1459
1460         {
1461                 {"authentication_timeout", PGC_SIGHUP, CONN_AUTH_SECURITY,
1462                         gettext_noop("Sets the maximum allowed time to complete client authentication."),
1463                         NULL,
1464                         GUC_UNIT_S
1465                 },
1466                 &AuthenticationTimeout,
1467                 60, 1, 600, NULL, NULL
1468         },
1469
1470         {
1471                 /* Not for general use */
1472                 {"pre_auth_delay", PGC_SIGHUP, DEVELOPER_OPTIONS,
1473                         gettext_noop("Waits N seconds on connection startup before authentication."),
1474                         gettext_noop("This allows attaching a debugger to the process."),
1475                         GUC_NOT_IN_SAMPLE | GUC_UNIT_S
1476                 },
1477                 &PreAuthDelay,
1478                 0, 0, 60, NULL, NULL
1479         },
1480
1481         {
1482                 {"checkpoint_segments", PGC_SIGHUP, WAL_CHECKPOINTS,
1483                         gettext_noop("Sets the maximum distance in log segments between automatic WAL checkpoints."),
1484                         NULL
1485                 },
1486                 &CheckPointSegments,
1487                 3, 1, INT_MAX, NULL, NULL
1488         },
1489
1490         {
1491                 {"checkpoint_timeout", PGC_SIGHUP, WAL_CHECKPOINTS,
1492                         gettext_noop("Sets the maximum time between automatic WAL checkpoints."),
1493                         NULL,
1494                         GUC_UNIT_S
1495                 },
1496                 &CheckPointTimeout,
1497                 300, 30, 3600, NULL, NULL
1498         },
1499
1500         {
1501                 {"checkpoint_warning", PGC_SIGHUP, WAL_CHECKPOINTS,
1502                         gettext_noop("Enables warnings if checkpoint segments are filled more "
1503                                                  "frequently than this."),
1504                         gettext_noop("Write a message to the server log if checkpoints "
1505                         "caused by the filling of checkpoint segment files happens more "
1506                                                  "frequently than this number of seconds. Zero turns off the warning."),
1507                         GUC_UNIT_S
1508                 },
1509                 &CheckPointWarning,
1510                 30, 0, INT_MAX, NULL, NULL
1511         },
1512
1513         {
1514                 {"wal_buffers", PGC_POSTMASTER, WAL_SETTINGS,
1515                         gettext_noop("Sets the number of disk-page buffers in shared memory for WAL."),
1516                         NULL,
1517                         GUC_UNIT_XBLOCKS
1518                 },
1519                 &XLOGbuffers,
1520                 8, 4, INT_MAX, NULL, NULL
1521         },
1522
1523         {
1524                 {"wal_writer_delay", PGC_SIGHUP, WAL_SETTINGS,
1525                         gettext_noop("WAL writer sleep time between WAL flushes."),
1526                         NULL,
1527                         GUC_UNIT_MS
1528                 },
1529                 &WalWriterDelay,
1530                 200, 1, 10000, NULL, NULL
1531         },
1532
1533         {
1534                 {"commit_delay", PGC_USERSET, WAL_SETTINGS,
1535                         gettext_noop("Sets the delay in microseconds between transaction commit and "
1536                                                  "flushing WAL to disk."),
1537                         NULL
1538                 },
1539                 &CommitDelay,
1540                 0, 0, 100000, NULL, NULL
1541         },
1542
1543         {
1544                 {"commit_siblings", PGC_USERSET, WAL_SETTINGS,
1545                         gettext_noop("Sets the minimum concurrent open transactions before performing "
1546                                                  "commit_delay."),
1547                         NULL
1548                 },
1549                 &CommitSiblings,
1550                 5, 1, 1000, NULL, NULL
1551         },
1552
1553         {
1554                 {"extra_float_digits", PGC_USERSET, CLIENT_CONN_LOCALE,
1555                         gettext_noop("Sets the number of digits displayed for floating-point values."),
1556                         gettext_noop("This affects real, double precision, and geometric data types. "
1557                          "The parameter value is added to the standard number of digits "
1558                                                  "(FLT_DIG or DBL_DIG as appropriate).")
1559                 },
1560                 &extra_float_digits,
1561                 0, -15, 2, NULL, NULL
1562         },
1563
1564         {
1565                 {"log_min_duration_statement", PGC_SUSET, LOGGING_WHEN,
1566                         gettext_noop("Sets the minimum execution time above which statements will "
1567                                                  "be logged."),
1568                         gettext_noop("Zero prints all queries. The default is -1 (turning this feature off)."),
1569                         GUC_UNIT_MS
1570                 },
1571                 &log_min_duration_statement,
1572                 -1, -1, INT_MAX / 1000, NULL, NULL
1573         },
1574
1575         {
1576                 {"log_autovacuum", PGC_BACKEND, LOGGING_WHAT,
1577                         gettext_noop("Sets the minimum execution time above which autovacuum actions "
1578                                                  "will be logged."),
1579                         gettext_noop("Zero prints all actions.  The default is -1 (turning this feature off)."),
1580                         GUC_UNIT_MS
1581                 },
1582                 &Log_autovacuum,
1583                 -1, -1, INT_MAX / 1000, NULL, NULL
1584         },
1585
1586         {
1587                 {"bgwriter_delay", PGC_SIGHUP, RESOURCES,
1588                         gettext_noop("Background writer sleep time between rounds."),
1589                         NULL,
1590                         GUC_UNIT_MS
1591                 },
1592                 &BgWriterDelay,
1593                 200, 10, 10000, NULL, NULL
1594         },
1595
1596         {
1597                 {"bgwriter_lru_maxpages", PGC_SIGHUP, RESOURCES,
1598                         gettext_noop("Background writer maximum number of LRU pages to flush per round."),
1599                         NULL
1600                 },
1601                 &bgwriter_lru_maxpages,
1602                 5, 0, 1000, NULL, NULL
1603         },
1604
1605         {
1606                 {"log_rotation_age", PGC_SIGHUP, LOGGING_WHERE,
1607                         gettext_noop("Automatic log file rotation will occur after N minutes."),
1608                         NULL,
1609                         GUC_UNIT_MIN
1610                 },
1611                 &Log_RotationAge,
1612                 HOURS_PER_DAY * MINS_PER_HOUR, 0, INT_MAX / MINS_PER_HOUR, NULL, NULL
1613         },
1614
1615         {
1616                 {"log_rotation_size", PGC_SIGHUP, LOGGING_WHERE,
1617                         gettext_noop("Automatic log file rotation will occur after N kilobytes."),
1618                         NULL,
1619                         GUC_UNIT_KB
1620                 },
1621                 &Log_RotationSize,
1622                 10 * 1024, 0, INT_MAX / 1024, NULL, NULL
1623         },
1624
1625         {
1626                 {"max_function_args", PGC_INTERNAL, PRESET_OPTIONS,
1627                         gettext_noop("Shows the maximum number of function arguments."),
1628                         NULL,
1629                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1630                 },
1631                 &max_function_args,
1632                 FUNC_MAX_ARGS, FUNC_MAX_ARGS, FUNC_MAX_ARGS, NULL, NULL
1633         },
1634
1635         {
1636                 {"max_index_keys", PGC_INTERNAL, PRESET_OPTIONS,
1637                         gettext_noop("Shows the maximum number of index keys."),
1638                         NULL,
1639                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1640                 },
1641                 &max_index_keys,
1642                 INDEX_MAX_KEYS, INDEX_MAX_KEYS, INDEX_MAX_KEYS, NULL, NULL
1643         },
1644
1645         {
1646                 {"max_identifier_length", PGC_INTERNAL, PRESET_OPTIONS,
1647                         gettext_noop("Shows the maximum identifier length."),
1648                         NULL,
1649                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1650                 },
1651                 &max_identifier_length,
1652                 NAMEDATALEN - 1, NAMEDATALEN - 1, NAMEDATALEN - 1, NULL, NULL
1653         },
1654
1655         {
1656                 {"block_size", PGC_INTERNAL, PRESET_OPTIONS,
1657                         gettext_noop("Shows the size of a disk block."),
1658                         NULL,
1659                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1660                 },
1661                 &block_size,
1662                 BLCKSZ, BLCKSZ, BLCKSZ, NULL, NULL
1663         },
1664
1665         {
1666                 {"autovacuum_naptime", PGC_SIGHUP, AUTOVACUUM,
1667                         gettext_noop("Time to sleep between autovacuum runs."),
1668                         NULL,
1669                         GUC_UNIT_S
1670                 },
1671                 &autovacuum_naptime,
1672                 60, 1, INT_MAX / 1000, NULL, NULL
1673         },
1674         {
1675                 {"autovacuum_vacuum_threshold", PGC_SIGHUP, AUTOVACUUM,
1676                         gettext_noop("Minimum number of tuple updates or deletes prior to vacuum."),
1677                         NULL
1678                 },
1679                 &autovacuum_vac_thresh,
1680                 50, 0, INT_MAX, NULL, NULL
1681         },
1682         {
1683                 {"autovacuum_analyze_threshold", PGC_SIGHUP, AUTOVACUUM,
1684                         gettext_noop("Minimum number of tuple inserts, updates or deletes prior to analyze."),
1685                         NULL
1686                 },
1687                 &autovacuum_anl_thresh,
1688                 50, 0, INT_MAX, NULL, NULL
1689         },
1690         {
1691                 /* see varsup.c for why this is PGC_POSTMASTER not PGC_SIGHUP */
1692                 {"autovacuum_freeze_max_age", PGC_POSTMASTER, AUTOVACUUM,
1693                         gettext_noop("Age at which to autovacuum a table to prevent transaction ID wraparound."),
1694                         NULL
1695                 },
1696                 &autovacuum_freeze_max_age,
1697                 200000000, 100000000, 2000000000, NULL, NULL
1698         },
1699         {
1700                 /* see max_connections */
1701                 {"autovacuum_max_workers", PGC_POSTMASTER, AUTOVACUUM,
1702                         gettext_noop("Sets the maximum number of simultaneously running autovacuum worker processes."),
1703                         NULL
1704                 },
1705                 &autovacuum_max_workers,
1706                 3, 1, INT_MAX / 4, assign_autovacuum_max_workers, NULL
1707         },
1708
1709         {
1710                 {"tcp_keepalives_idle", PGC_USERSET, CLIENT_CONN_OTHER,
1711                         gettext_noop("Time between issuing TCP keepalives."),
1712                         gettext_noop("A value of 0 uses the system default."),
1713                         GUC_UNIT_S
1714                 },
1715                 &tcp_keepalives_idle,
1716                 0, 0, INT_MAX, assign_tcp_keepalives_idle, show_tcp_keepalives_idle
1717         },
1718
1719         {
1720                 {"tcp_keepalives_interval", PGC_USERSET, CLIENT_CONN_OTHER,
1721                         gettext_noop("Time between TCP keepalive retransmits."),
1722                         gettext_noop("A value of 0 uses the system default."),
1723                         GUC_UNIT_S
1724                 },
1725                 &tcp_keepalives_interval,
1726                 0, 0, INT_MAX, assign_tcp_keepalives_interval, show_tcp_keepalives_interval
1727         },
1728
1729         {
1730                 {"tcp_keepalives_count", PGC_USERSET, CLIENT_CONN_OTHER,
1731                         gettext_noop("Maximum number of TCP keepalive retransmits."),
1732                         gettext_noop("This controls the number of consecutive keepalive retransmits that can be "
1733                                                  "lost before a connection is considered dead. A value of 0 uses the "
1734                                                  "system default."),
1735                 },
1736                 &tcp_keepalives_count,
1737                 0, 0, INT_MAX, assign_tcp_keepalives_count, show_tcp_keepalives_count
1738         },
1739
1740         {
1741                 {"gin_fuzzy_search_limit", PGC_USERSET, CLIENT_CONN_OTHER,
1742                         gettext_noop("Sets the maximum allowed result for exact search by GIN."),
1743                         NULL,
1744                         0
1745                 },
1746                 &GinFuzzySearchLimit,
1747                 0, 0, INT_MAX, NULL, NULL
1748         },
1749
1750         {
1751                 {"effective_cache_size", PGC_USERSET, QUERY_TUNING_COST,
1752                         gettext_noop("Sets the planner's assumption about the size of the disk cache."),
1753                         gettext_noop("That is, the portion of the kernel's disk cache that "
1754                                                  "will be used for PostgreSQL data files. This is measured in disk "
1755                                                  "pages, which are normally 8 kB each."),
1756                         GUC_UNIT_BLOCKS,
1757                 },
1758                 &effective_cache_size,
1759                 DEFAULT_EFFECTIVE_CACHE_SIZE, 1, INT_MAX, NULL, NULL
1760         },
1761
1762         {
1763                 /* Can't be set in postgresql.conf */
1764                 {"server_version_num", PGC_INTERNAL, PRESET_OPTIONS,
1765                         gettext_noop("Shows the server version as an integer."),
1766                         NULL,
1767                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1768                 },
1769                 &server_version_num,
1770                 PG_VERSION_NUM, PG_VERSION_NUM, PG_VERSION_NUM, NULL, NULL
1771         },
1772
1773         {
1774                 {"log_temp_files", PGC_USERSET, LOGGING_WHAT,
1775                         gettext_noop("Log the use of temporary files larger than this number of kilobytes."),
1776                         gettext_noop("Zero logs all files. The default is -1 (turning this feature off)."),
1777                         GUC_UNIT_KB
1778                 },
1779                 &log_temp_files,
1780                 -1, -1, INT_MAX, NULL, NULL
1781         },
1782
1783         /* End-of-list marker */
1784         {
1785                 {NULL, 0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL
1786         }
1787 };
1788
1789
1790 static struct config_real ConfigureNamesReal[] =
1791 {
1792         {
1793                 {"seq_page_cost", PGC_USERSET, QUERY_TUNING_COST,
1794                         gettext_noop("Sets the planner's estimate of the cost of a "
1795                                                  "sequentially fetched disk page."),
1796                         NULL
1797                 },
1798                 &seq_page_cost,
1799                 DEFAULT_SEQ_PAGE_COST, 0, DBL_MAX, NULL, NULL
1800         },
1801         {
1802                 {"random_page_cost", PGC_USERSET, QUERY_TUNING_COST,
1803                         gettext_noop("Sets the planner's estimate of the cost of a "
1804                                                  "nonsequentially fetched disk page."),
1805                         NULL
1806                 },
1807                 &random_page_cost,
1808                 DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX, NULL, NULL
1809         },
1810         {
1811                 {"cpu_tuple_cost", PGC_USERSET, QUERY_TUNING_COST,
1812                         gettext_noop("Sets the planner's estimate of the cost of "
1813                                                  "processing each tuple (row)."),
1814                         NULL
1815                 },
1816                 &cpu_tuple_cost,
1817                 DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX, NULL, NULL
1818         },
1819         {
1820                 {"cpu_index_tuple_cost", PGC_USERSET, QUERY_TUNING_COST,
1821                         gettext_noop("Sets the planner's estimate of the cost of "
1822                                                  "processing each index entry during an index scan."),
1823                         NULL
1824                 },
1825                 &cpu_index_tuple_cost,
1826                 DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX, NULL, NULL
1827         },
1828         {
1829                 {"cpu_operator_cost", PGC_USERSET, QUERY_TUNING_COST,
1830                         gettext_noop("Sets the planner's estimate of the cost of "
1831                                                  "processing each operator or function call."),
1832                         NULL
1833                 },
1834                 &cpu_operator_cost,
1835                 DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX, NULL, NULL
1836         },
1837
1838         {
1839                 {"geqo_selection_bias", PGC_USERSET, QUERY_TUNING_GEQO,
1840                         gettext_noop("GEQO: selective pressure within the population."),
1841                         NULL
1842                 },
1843                 &Geqo_selection_bias,
1844                 DEFAULT_GEQO_SELECTION_BIAS, MIN_GEQO_SELECTION_BIAS,
1845                 MAX_GEQO_SELECTION_BIAS, NULL, NULL
1846         },
1847
1848         {
1849                 {"bgwriter_lru_percent", PGC_SIGHUP, RESOURCES,
1850                         gettext_noop("Background writer percentage of LRU buffers to flush per round."),
1851                         NULL
1852                 },
1853                 &bgwriter_lru_percent,
1854                 1.0, 0.0, 100.0, NULL, NULL
1855         },
1856
1857         {
1858                 {"seed", PGC_USERSET, UNGROUPED,
1859                         gettext_noop("Sets the seed for random-number generation."),
1860                         NULL,
1861                         GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1862                 },
1863                 &phony_random_seed,
1864                 0.5, 0.0, 1.0, assign_random_seed, show_random_seed
1865         },
1866
1867         {
1868                 {"autovacuum_vacuum_scale_factor", PGC_SIGHUP, AUTOVACUUM,
1869                         gettext_noop("Number of tuple updates or deletes prior to vacuum as a fraction of reltuples."),
1870                         NULL
1871                 },
1872                 &autovacuum_vac_scale,
1873                 0.2, 0.0, 100.0, NULL, NULL
1874         },
1875         {
1876                 {"autovacuum_analyze_scale_factor", PGC_SIGHUP, AUTOVACUUM,
1877                         gettext_noop("Number of tuple inserts, updates or deletes prior to analyze as a fraction of reltuples."),
1878                         NULL
1879                 },
1880                 &autovacuum_anl_scale,
1881                 0.1, 0.0, 100.0, NULL, NULL
1882         },
1883
1884         {
1885                 {"checkpoint_completion_target", PGC_SIGHUP, WAL_CHECKPOINTS,
1886                         gettext_noop("Time spent flushing dirty buffers during checkpoint, as fraction of checkpoint interval."),
1887                         NULL
1888                 },
1889                 &CheckPointCompletionTarget,
1890                 0.5, 0.0, 1.0, NULL, NULL
1891         },
1892
1893         /* End-of-list marker */
1894         {
1895                 {NULL, 0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL
1896         }
1897 };
1898
1899
1900 static struct config_string ConfigureNamesString[] =
1901 {
1902         {
1903                 {"archive_command", PGC_SIGHUP, WAL_SETTINGS,
1904                         gettext_noop("Sets the shell command that will be called to archive a WAL file."),
1905                         NULL
1906                 },
1907                 &XLogArchiveCommand,
1908                 "", NULL, NULL
1909         },
1910
1911         {
1912                 {"backslash_quote", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1913                         gettext_noop("Sets whether \"\\'\" is allowed in string literals."),
1914                         gettext_noop("Valid values are ON, OFF, and SAFE_ENCODING.")
1915                 },
1916                 &backslash_quote_string,
1917                 "safe_encoding", assign_backslash_quote, NULL
1918         },
1919
1920         {
1921                 {"client_encoding", PGC_USERSET, CLIENT_CONN_LOCALE,
1922                         gettext_noop("Sets the client's character set encoding."),
1923                         NULL,
1924                         GUC_IS_NAME | GUC_REPORT
1925                 },
1926                 &client_encoding_string,
1927                 "SQL_ASCII", assign_client_encoding, NULL
1928         },
1929
1930         {
1931                 {"client_min_messages", PGC_USERSET, LOGGING_WHEN,
1932                         gettext_noop("Sets the message levels that are sent to the client."),
1933                         gettext_noop("Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, "
1934                                                  "DEBUG1, LOG, NOTICE, WARNING, and ERROR. Each level includes all the "
1935                                                  "levels that follow it. The later the level, the fewer messages are "
1936                                                  "sent.")
1937                 },
1938                 &client_min_messages_str,
1939                 "notice", assign_client_min_messages, NULL
1940         },
1941
1942         {
1943                 {"log_min_messages", PGC_SUSET, LOGGING_WHEN,
1944                         gettext_noop("Sets the message levels that are logged."),
1945                         gettext_noop("Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, "
1946                         "INFO, NOTICE, WARNING, ERROR, LOG, FATAL, and PANIC. Each level "
1947                                                  "includes all the levels that follow it.")
1948                 },
1949                 &log_min_messages_str,
1950                 "notice", assign_log_min_messages, NULL
1951         },
1952
1953         {
1954                 {"log_error_verbosity", PGC_SUSET, LOGGING_WHEN,
1955                         gettext_noop("Sets the verbosity of logged messages."),
1956                         gettext_noop("Valid values are \"terse\", \"default\", and \"verbose\".")
1957                 },
1958                 &log_error_verbosity_str,
1959                 "default", assign_log_error_verbosity, NULL
1960         },
1961         {
1962                 {"log_statement", PGC_SUSET, LOGGING_WHAT,
1963                         gettext_noop("Sets the type of statements logged."),
1964                         gettext_noop("Valid values are \"none\", \"ddl\", \"mod\", and \"all\".")
1965                 },
1966                 &log_statement_str,
1967                 "none", assign_log_statement, NULL
1968         },
1969
1970         {
1971                 {"log_min_error_statement", PGC_SUSET, LOGGING_WHEN,
1972                         gettext_noop("Causes all statements generating error at or above this level to be logged."),
1973                         gettext_noop("All SQL statements that cause an error of the "
1974                                                  "specified level or a higher level are logged.")
1975                 },
1976                 &log_min_error_statement_str,
1977                 "error", assign_min_error_statement, NULL
1978         },
1979
1980         {
1981                 {"log_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
1982                         gettext_noop("Controls information prefixed to each log line."),
1983                         gettext_noop("If blank, no prefix is used.")
1984                 },
1985                 &Log_line_prefix,
1986                 "", NULL, NULL
1987         },
1988
1989         {
1990                 {"log_timezone", PGC_SIGHUP, LOGGING_WHAT,
1991                         gettext_noop("Sets the time zone to use in log messages."),
1992                         NULL
1993                 },
1994                 &log_timezone_string,
1995                 "UNKNOWN", assign_log_timezone, show_log_timezone
1996         },
1997
1998         {
1999                 {"DateStyle", PGC_USERSET, CLIENT_CONN_LOCALE,
2000                         gettext_noop("Sets the display format for date and time values."),
2001                         gettext_noop("Also controls interpretation of ambiguous "
2002                                                  "date inputs."),
2003                         GUC_LIST_INPUT | GUC_REPORT
2004                 },
2005                 &datestyle_string,
2006                 "ISO, MDY", assign_datestyle, NULL
2007         },
2008
2009         {
2010                 {"default_tablespace", PGC_USERSET, CLIENT_CONN_STATEMENT,
2011                         gettext_noop("Sets the default tablespace to create tables and indexes in."),
2012                         gettext_noop("An empty string selects the database's default tablespace."),
2013                         GUC_IS_NAME
2014                 },
2015                 &default_tablespace,
2016                 "", assign_default_tablespace, NULL
2017         },
2018
2019         {
2020                 {"temp_tablespaces", PGC_USERSET, CLIENT_CONN_STATEMENT,
2021                         gettext_noop("Sets the tablespace(s) to use for temporary tables and sort files."),
2022                         NULL,
2023                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2024                 },
2025                 &temp_tablespaces,
2026                 "", assign_temp_tablespaces, NULL
2027         },
2028
2029         {
2030                 {"default_transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
2031                         gettext_noop("Sets the transaction isolation level of each new transaction."),
2032                         gettext_noop("Each SQL transaction has an isolation level, which "
2033                                                  "can be either \"read uncommitted\", \"read committed\", \"repeatable read\", or \"serializable\".")
2034                 },
2035                 &default_iso_level_string,
2036                 "read committed", assign_defaultxactisolevel, NULL
2037         },
2038
2039         {
2040                 {"session_replication_role", PGC_SUSET, CLIENT_CONN_STATEMENT,
2041                         gettext_noop("Sets the sessions behavior for triggers and rewrite rules."),
2042                         gettext_noop("Each session can be either"
2043                                                  " \"origin\", \"replica\" or \"local\".")
2044                 },
2045                 &session_replication_role_string,
2046                 "origin", assign_session_replication_role, NULL
2047         },
2048
2049         {
2050                 {"dynamic_library_path", PGC_SUSET, CLIENT_CONN_OTHER,
2051                         gettext_noop("Sets the path for dynamically loadable modules."),
2052                         gettext_noop("If a dynamically loadable module needs to be opened and "
2053                                                  "the specified name does not have a directory component (i.e., the "
2054                                                  "name does not contain a slash), the system will search this path for "
2055                                                  "the specified file."),
2056                         GUC_SUPERUSER_ONLY
2057                 },
2058                 &Dynamic_library_path,
2059                 "$libdir", NULL, NULL
2060         },
2061
2062         {
2063                 {"krb_server_keyfile", PGC_POSTMASTER, CONN_AUTH_SECURITY,
2064                         gettext_noop("Sets the location of the Kerberos server key file."),
2065                         NULL,
2066                         GUC_SUPERUSER_ONLY
2067                 },
2068                 &pg_krb_server_keyfile,
2069                 PG_KRB_SRVTAB, NULL, NULL
2070         },
2071
2072         {
2073                 {"krb_srvname", PGC_POSTMASTER, CONN_AUTH_SECURITY,
2074                         gettext_noop("Sets the name of the Kerberos service."),
2075                         NULL
2076                 },
2077                 &pg_krb_srvnam,
2078                 PG_KRB_SRVNAM, NULL, NULL
2079         },
2080
2081         {
2082                 {"krb_server_hostname", PGC_POSTMASTER, CONN_AUTH_SECURITY,
2083                         gettext_noop("Sets the hostname of the Kerberos server."),
2084                         NULL
2085                 },
2086                 &pg_krb_server_hostname,
2087                 NULL, NULL, NULL
2088         },
2089
2090         {
2091                 {"bonjour_name", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2092                         gettext_noop("Sets the Bonjour broadcast service name."),
2093                         NULL
2094                 },
2095                 &bonjour_name,
2096                 "", NULL, NULL
2097         },
2098
2099         /* See main.c about why defaults for LC_foo are not all alike */
2100
2101         {
2102                 {"lc_collate", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2103                         gettext_noop("Shows the collation order locale."),
2104                         NULL,
2105                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2106                 },
2107                 &locale_collate,
2108                 "C", NULL, NULL
2109         },
2110
2111         {
2112                 {"lc_ctype", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2113                         gettext_noop("Shows the character classification and case conversion locale."),
2114                         NULL,
2115                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2116                 },
2117                 &locale_ctype,
2118                 "C", NULL, NULL
2119         },
2120
2121         {
2122                 {"lc_messages", PGC_SUSET, CLIENT_CONN_LOCALE,
2123                         gettext_noop("Sets the language in which messages are displayed."),
2124                         NULL
2125                 },
2126                 &locale_messages,
2127                 "", locale_messages_assign, NULL
2128         },
2129
2130         {
2131                 {"lc_monetary", PGC_USERSET, CLIENT_CONN_LOCALE,
2132                         gettext_noop("Sets the locale for formatting monetary amounts."),
2133                         NULL
2134                 },
2135                 &locale_monetary,
2136                 "C", locale_monetary_assign, NULL
2137         },
2138
2139         {
2140                 {"lc_numeric", PGC_USERSET, CLIENT_CONN_LOCALE,
2141                         gettext_noop("Sets the locale for formatting numbers."),
2142                         NULL
2143                 },
2144                 &locale_numeric,
2145                 "C", locale_numeric_assign, NULL
2146         },
2147
2148         {
2149                 {"lc_time", PGC_USERSET, CLIENT_CONN_LOCALE,
2150                         gettext_noop("Sets the locale for formatting date and time values."),
2151                         NULL
2152                 },
2153                 &locale_time,
2154                 "C", locale_time_assign, NULL
2155         },
2156
2157         {
2158                 {"shared_preload_libraries", PGC_POSTMASTER, RESOURCES_KERNEL,
2159                         gettext_noop("Lists shared libraries to preload into server."),
2160                         NULL,
2161                         GUC_LIST_INPUT | GUC_LIST_QUOTE | GUC_SUPERUSER_ONLY
2162                 },
2163                 &shared_preload_libraries_string,
2164                 "", NULL, NULL
2165         },
2166
2167         {
2168                 {"local_preload_libraries", PGC_BACKEND, CLIENT_CONN_OTHER,
2169                         gettext_noop("Lists shared libraries to preload into each backend."),
2170                         NULL,
2171                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2172                 },
2173                 &local_preload_libraries_string,
2174                 "", NULL, NULL
2175         },
2176
2177         {
2178                 {"regex_flavor", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
2179                         gettext_noop("Sets the regular expression \"flavor\"."),
2180                         gettext_noop("This can be set to advanced, extended, or basic.")
2181                 },
2182                 &regex_flavor_string,
2183                 "advanced", assign_regex_flavor, NULL
2184         },
2185
2186         {
2187                 {"search_path", PGC_USERSET, CLIENT_CONN_STATEMENT,
2188                         gettext_noop("Sets the schema search order for names that are not schema-qualified."),
2189                         NULL,
2190                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2191                 },
2192                 &namespace_search_path,
2193                 "\"$user\",public", assign_search_path, NULL
2194         },
2195
2196         {
2197                 /* Can't be set in postgresql.conf */
2198                 {"server_encoding", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2199                         gettext_noop("Sets the server (database) character set encoding."),
2200                         NULL,
2201                         GUC_IS_NAME | GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2202                 },
2203                 &server_encoding_string,
2204                 "SQL_ASCII", NULL, NULL
2205         },
2206
2207         {
2208                 /* Can't be set in postgresql.conf */
2209                 {"server_version", PGC_INTERNAL, PRESET_OPTIONS,
2210                         gettext_noop("Shows the server version."),
2211                         NULL,
2212                         GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2213                 },
2214                 &server_version_string,
2215                 PG_VERSION, NULL, NULL
2216         },
2217
2218         {
2219                 /* Not for general use --- used by SET ROLE */
2220                 {"role", PGC_USERSET, UNGROUPED,
2221                         gettext_noop("Sets the current role."),
2222                         NULL,
2223                         GUC_IS_NAME | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2224                 },
2225                 &role_string,
2226                 "none", assign_role, show_role
2227         },
2228
2229         {
2230                 /* Not for general use --- used by SET SESSION AUTHORIZATION */
2231                 {"session_authorization", PGC_USERSET, UNGROUPED,
2232                         gettext_noop("Sets the session user name."),
2233                         NULL,
2234                         GUC_IS_NAME | GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2235                 },
2236                 &session_authorization_string,
2237                 NULL, assign_session_authorization, show_session_authorization
2238         },
2239
2240         {
2241                 {"log_destination", PGC_SIGHUP, LOGGING_WHERE,
2242                         gettext_noop("Sets the destination for server log output."),
2243                         gettext_noop("Valid values are combinations of \"stderr\", \"syslog\", "
2244                                                  " \"csvlog\" and \"eventlog\", depending on the platform."),
2245                         GUC_LIST_INPUT
2246                 },
2247                 &log_destination_string,
2248                 "stderr", assign_log_destination, NULL
2249         },
2250         {
2251                 {"log_directory", PGC_SIGHUP, LOGGING_WHERE,
2252                         gettext_noop("Sets the destination directory for log files."),
2253                         gettext_noop("Can be specified as relative to the data directory "
2254                                                  "or as absolute path."),
2255                         GUC_SUPERUSER_ONLY
2256                 },
2257                 &Log_directory,
2258                 "pg_log", assign_canonical_path, NULL
2259         },
2260         {
2261                 {"log_filename", PGC_SIGHUP, LOGGING_WHERE,
2262                         gettext_noop("Sets the file name pattern for log files."),
2263                         NULL,
2264                         GUC_SUPERUSER_ONLY
2265                 },
2266                 &Log_filename,
2267                 "postgresql-%Y-%m-%d_%H%M%S.log", NULL, NULL
2268         },
2269
2270 #ifdef HAVE_SYSLOG
2271         {
2272                 {"syslog_facility", PGC_SIGHUP, LOGGING_WHERE,
2273                         gettext_noop("Sets the syslog \"facility\" to be used when syslog enabled."),
2274                         gettext_noop("Valid values are LOCAL0, LOCAL1, LOCAL2, LOCAL3, "
2275                                                  "LOCAL4, LOCAL5, LOCAL6, LOCAL7.")
2276                 },
2277                 &syslog_facility_str,
2278                 "LOCAL0", assign_syslog_facility, NULL
2279         },
2280         {
2281                 {"syslog_ident", PGC_SIGHUP, LOGGING_WHERE,
2282                         gettext_noop("Sets the program name used to identify PostgreSQL "
2283                                                  "messages in syslog."),
2284                         NULL
2285                 },
2286                 &syslog_ident_str,
2287                 "postgres", assign_syslog_ident, NULL
2288         },
2289 #endif
2290
2291         {
2292                 {"TimeZone", PGC_USERSET, CLIENT_CONN_LOCALE,
2293                         gettext_noop("Sets the time zone for displaying and interpreting time stamps."),
2294                         NULL,
2295                         GUC_REPORT
2296                 },
2297                 &timezone_string,
2298                 "UNKNOWN", assign_timezone, show_timezone
2299         },
2300         {
2301                 {"timezone_abbreviations", PGC_USERSET, CLIENT_CONN_LOCALE,
2302                         gettext_noop("Selects a file of time zone abbreviations."),
2303                         NULL,
2304                 },
2305                 &timezone_abbreviations_string,
2306                 "UNKNOWN", assign_timezone_abbreviations, NULL
2307         },
2308
2309         {
2310                 {"transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
2311                         gettext_noop("Sets the current transaction's isolation level."),
2312                         NULL,
2313                         GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2314                 },
2315                 &XactIsoLevel_string,
2316                 NULL, assign_XactIsoLevel, show_XactIsoLevel
2317         },
2318
2319         {
2320                 {"unix_socket_group", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2321                         gettext_noop("Sets the owning group of the Unix-domain socket."),
2322                         gettext_noop("The owning user of the socket is always the user "
2323                                                  "that starts the server.")
2324                 },
2325                 &Unix_socket_group,
2326                 "", NULL, NULL
2327         },
2328
2329         {
2330                 {"unix_socket_directory", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2331                         gettext_noop("Sets the directory where the Unix-domain socket will be created."),
2332                         NULL,
2333                         GUC_SUPERUSER_ONLY
2334                 },
2335                 &UnixSocketDir,
2336                 "", assign_canonical_path, NULL
2337         },
2338
2339         {
2340                 {"listen_addresses", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2341                         gettext_noop("Sets the host name or IP address(es) to listen to."),
2342                         NULL,
2343                         GUC_LIST_INPUT
2344                 },
2345                 &ListenAddresses,
2346                 "localhost", NULL, NULL
2347         },
2348
2349         {
2350                 {"wal_sync_method", PGC_SIGHUP, WAL_SETTINGS,
2351                         gettext_noop("Selects the method used for forcing WAL updates to disk."),
2352                         NULL
2353                 },
2354                 &XLOG_sync_method,
2355                 XLOG_sync_method_default, assign_xlog_sync_method, NULL
2356         },
2357
2358         {
2359                 {"custom_variable_classes", PGC_SIGHUP, CUSTOM_OPTIONS,
2360                         gettext_noop("Sets the list of known custom variable classes."),
2361                         NULL,
2362                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2363                 },
2364                 &custom_variable_classes,
2365                 NULL, assign_custom_variable_classes, NULL
2366         },
2367
2368         {
2369                 {"data_directory", PGC_POSTMASTER, FILE_LOCATIONS,
2370                         gettext_noop("Sets the server's data directory."),
2371                         NULL,
2372                         GUC_SUPERUSER_ONLY
2373                 },
2374                 &data_directory,
2375                 NULL, NULL, NULL
2376         },
2377
2378         {
2379                 {"config_file", PGC_POSTMASTER, FILE_LOCATIONS,
2380                         gettext_noop("Sets the server's main configuration file."),
2381                         NULL,
2382                         GUC_DISALLOW_IN_FILE | GUC_SUPERUSER_ONLY
2383                 },
2384                 &ConfigFileName,
2385                 NULL, NULL, NULL
2386         },
2387
2388         {
2389                 {"hba_file", PGC_POSTMASTER, FILE_LOCATIONS,
2390                         gettext_noop("Sets the server's \"hba\" configuration file."),
2391                         NULL,
2392                         GUC_SUPERUSER_ONLY
2393                 },
2394                 &HbaFileName,
2395                 NULL, NULL, NULL
2396         },
2397
2398         {
2399                 {"ident_file", PGC_POSTMASTER, FILE_LOCATIONS,
2400                         gettext_noop("Sets the server's \"ident\" configuration file."),
2401                         NULL,
2402                         GUC_SUPERUSER_ONLY
2403                 },
2404                 &IdentFileName,
2405                 NULL, NULL, NULL
2406         },
2407
2408         {
2409                 {"external_pid_file", PGC_POSTMASTER, FILE_LOCATIONS,
2410                         gettext_noop("Writes the postmaster PID to the specified file."),
2411                         NULL,
2412                         GUC_SUPERUSER_ONLY
2413                 },
2414                 &external_pid_file,
2415                 NULL, assign_canonical_path, NULL
2416         },
2417
2418         {
2419                 {"xmlbinary", PGC_USERSET, CLIENT_CONN_STATEMENT,
2420                         gettext_noop("Sets how binary values are to be encoded in XML."),
2421                         gettext_noop("Valid values are BASE64 and HEX.")
2422                 },
2423                 &xmlbinary_string,
2424                 "base64", assign_xmlbinary, NULL
2425         },
2426
2427         {
2428                 {"xmloption", PGC_USERSET, CLIENT_CONN_STATEMENT,
2429                         gettext_noop("Sets whether XML data in implicit parsing and serialization "
2430                                                  "operations is to be considered as documents or content fragments."),
2431                         gettext_noop("Valid values are DOCUMENT and CONTENT.")
2432                 },
2433                 &xmloption_string,
2434                 "content", assign_xmloption, NULL
2435         },
2436
2437 #ifdef USE_SSL
2438         {
2439                 {"ssl_ciphers", PGC_POSTMASTER, CONN_AUTH_SECURITY,
2440                         gettext_noop("Sets the list of allowed SSL ciphers."),
2441                         NULL,
2442                         GUC_SUPERUSER_ONLY
2443                 },
2444                 &SSLCipherSuites,
2445                 "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH", NULL, NULL
2446         },
2447 #endif /* USE_SSL */
2448
2449         /* End-of-list marker */
2450         {
2451                 {NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL
2452         }
2453 };
2454
2455
2456 /******** end of options list ********/
2457
2458
2459 /*
2460  * To allow continued support of obsolete names for GUC variables, we apply
2461  * the following mappings to any unrecognized name.  Note that an old name
2462  * should be mapped to a new one only if the new variable has very similar
2463  * semantics to the old.
2464  */
2465 static const char *const map_old_guc_names[] = {
2466         "sort_mem", "work_mem",
2467         "vacuum_mem", "maintenance_work_mem",
2468         NULL
2469 };
2470
2471
2472 /*
2473  * Actual lookup of variables is done through this single, sorted array.
2474  */
2475 static struct config_generic **guc_variables;
2476
2477 /* Current number of variables contained in the vector */
2478 static int      num_guc_variables;
2479
2480 /* Vector capacity */
2481 static int      size_guc_variables;
2482
2483
2484 static bool guc_dirty;                  /* TRUE if need to do commit/abort work */
2485
2486 static bool reporting_enabled;  /* TRUE to enable GUC_REPORT */
2487
2488
2489 static int      guc_var_compare(const void *a, const void *b);
2490 static int      guc_name_compare(const char *namea, const char *nameb);
2491 static void push_old_value(struct config_generic * gconf);
2492 static void ReportGUCOption(struct config_generic * record);
2493 static void ShowGUCConfigOption(const char *name, DestReceiver *dest);
2494 static void ShowAllGUCConfig(DestReceiver *dest);
2495 static char *_ShowOption(struct config_generic * record, bool use_units);
2496 static bool is_newvalue_equal(struct config_generic * record, const char *newvalue);
2497
2498
2499 /*
2500  * Some infrastructure for checking malloc/strdup/realloc calls
2501  */
2502 static void *
2503 guc_malloc(int elevel, size_t size)
2504 {
2505         void       *data;
2506
2507         data = malloc(size);
2508         if (data == NULL)
2509                 ereport(elevel,
2510                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2511                                  errmsg("out of memory")));
2512         return data;
2513 }
2514
2515 static void *
2516 guc_realloc(int elevel, void *old, size_t size)
2517 {
2518         void       *data;
2519
2520         data = realloc(old, size);
2521         if (data == NULL)
2522                 ereport(elevel,
2523                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2524                                  errmsg("out of memory")));
2525         return data;
2526 }
2527
2528 static char *
2529 guc_strdup(int elevel, const char *src)
2530 {
2531         char       *data;
2532
2533         data = strdup(src);
2534         if (data == NULL)
2535                 ereport(elevel,
2536                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2537                                  errmsg("out of memory")));
2538         return data;
2539 }
2540
2541
2542 /*
2543  * Support for assigning to a field of a string GUC item.  Free the prior
2544  * value if it's not referenced anywhere else in the item (including stacked
2545  * states).
2546  */
2547 static void
2548 set_string_field(struct config_string * conf, char **field, char *newval)
2549 {
2550         char       *oldval = *field;
2551         GucStack   *stack;
2552
2553         /* Do the assignment */
2554         *field = newval;
2555
2556         /* Exit if any duplicate references, or if old value was NULL anyway */
2557         if (oldval == NULL ||
2558                 oldval == *(conf->variable) ||
2559                 oldval == conf->reset_val ||
2560                 oldval == conf->tentative_val ||
2561                 oldval == conf->boot_val)
2562                 return;
2563         for (stack = conf->gen.stack; stack; stack = stack->prev)
2564         {
2565                 if (oldval == stack->tentative_val.stringval ||
2566                         oldval == stack->value.stringval)
2567                         return;
2568         }
2569
2570         /* Not used anymore, so free it */
2571         free(oldval);
2572 }
2573
2574 /*
2575  * Detect whether strval is referenced anywhere in a GUC string item
2576  */
2577 static bool
2578 string_field_used(struct config_string * conf, char *strval)
2579 {
2580         GucStack   *stack;
2581
2582         if (strval == *(conf->variable) ||
2583                 strval == conf->reset_val ||
2584                 strval == conf->tentative_val ||
2585                 strval == conf->boot_val)
2586                 return true;
2587         for (stack = conf->gen.stack; stack; stack = stack->prev)
2588         {
2589                 if (strval == stack->tentative_val.stringval ||
2590                         strval == stack->value.stringval)
2591                         return true;
2592         }
2593         return false;
2594 }
2595
2596
2597 struct config_generic **
2598 get_guc_variables(void)
2599 {
2600         return guc_variables;
2601 }
2602
2603
2604 /*
2605  * Build the sorted array.      This is split out so that it could be
2606  * re-executed after startup (eg, we could allow loadable modules to
2607  * add vars, and then we'd need to re-sort).
2608  */
2609 void
2610 build_guc_variables(void)
2611 {
2612         int                     size_vars;
2613         int                     num_vars = 0;
2614         struct config_generic **guc_vars;
2615         int                     i;
2616
2617         for (i = 0; ConfigureNamesBool[i].gen.name; i++)
2618         {
2619                 struct config_bool *conf = &ConfigureNamesBool[i];
2620
2621                 /* Rather than requiring vartype to be filled in by hand, do this: */
2622                 conf->gen.vartype = PGC_BOOL;
2623                 num_vars++;
2624         }
2625
2626         for (i = 0; ConfigureNamesInt[i].gen.name; i++)
2627         {
2628                 struct config_int *conf = &ConfigureNamesInt[i];
2629
2630                 conf->gen.vartype = PGC_INT;
2631                 num_vars++;
2632         }
2633
2634         for (i = 0; ConfigureNamesReal[i].gen.name; i++)
2635         {
2636                 struct config_real *conf = &ConfigureNamesReal[i];
2637
2638                 conf->gen.vartype = PGC_REAL;
2639                 num_vars++;
2640         }
2641
2642         for (i = 0; ConfigureNamesString[i].gen.name; i++)
2643         {
2644                 struct config_string *conf = &ConfigureNamesString[i];
2645
2646                 conf->gen.vartype = PGC_STRING;
2647                 num_vars++;
2648         }
2649
2650         /*
2651          * Create table with 20% slack
2652          */
2653         size_vars = num_vars + num_vars / 4;
2654
2655         guc_vars = (struct config_generic **)
2656                 guc_malloc(FATAL, size_vars * sizeof(struct config_generic *));
2657
2658         num_vars = 0;
2659
2660         for (i = 0; ConfigureNamesBool[i].gen.name; i++)
2661                 guc_vars[num_vars++] = &ConfigureNamesBool[i].gen;
2662
2663         for (i = 0; ConfigureNamesInt[i].gen.name; i++)
2664                 guc_vars[num_vars++] = &ConfigureNamesInt[i].gen;
2665
2666         for (i = 0; ConfigureNamesReal[i].gen.name; i++)
2667                 guc_vars[num_vars++] = &ConfigureNamesReal[i].gen;
2668
2669         for (i = 0; ConfigureNamesString[i].gen.name; i++)
2670                 guc_vars[num_vars++] = &ConfigureNamesString[i].gen;
2671
2672         if (guc_variables)
2673                 free(guc_variables);
2674         guc_variables = guc_vars;
2675         num_guc_variables = num_vars;
2676         size_guc_variables = size_vars;
2677         qsort((void *) guc_variables, num_guc_variables,
2678                   sizeof(struct config_generic *), guc_var_compare);
2679 }
2680
2681 static bool
2682 is_custom_class(const char *name, int dotPos)
2683 {
2684         /*
2685          * assign_custom_variable_classes() has made sure no empty identifiers or
2686          * whitespace exists in the variable
2687          */
2688         bool            result = false;
2689         const char *ccs = GetConfigOption("custom_variable_classes");
2690
2691         if (ccs != NULL)
2692         {
2693                 const char *start = ccs;
2694
2695                 for (;; ++ccs)
2696                 {
2697                         int                     c = *ccs;
2698
2699                         if (c == 0 || c == ',')
2700                         {
2701                                 if (dotPos == ccs - start && strncmp(start, name, dotPos) == 0)
2702                                 {
2703                                         result = true;
2704                                         break;
2705                                 }
2706                                 if (c == 0)
2707                                         break;
2708                                 start = ccs + 1;
2709                         }
2710                 }
2711         }
2712         return result;
2713 }
2714
2715 /*
2716  * Add a new GUC variable to the list of known variables. The
2717  * list is expanded if needed.
2718  */
2719 static bool
2720 add_guc_variable(struct config_generic * var, int elevel)
2721 {
2722         if (num_guc_variables + 1 >= size_guc_variables)
2723         {
2724                 /*
2725                  * Increase the vector by 25%
2726                  */
2727                 int                     size_vars = size_guc_variables + size_guc_variables / 4;
2728                 struct config_generic **guc_vars;
2729
2730                 if (size_vars == 0)
2731                 {
2732                         size_vars = 100;
2733                         guc_vars = (struct config_generic **)
2734                                 guc_malloc(elevel, size_vars * sizeof(struct config_generic *));
2735                 }
2736                 else
2737                 {
2738                         guc_vars = (struct config_generic **)
2739                                 guc_realloc(elevel, guc_variables, size_vars * sizeof(struct config_generic *));
2740                 }
2741
2742                 if (guc_vars == NULL)
2743                         return false;           /* out of memory */
2744
2745                 guc_variables = guc_vars;
2746                 size_guc_variables = size_vars;
2747         }
2748         guc_variables[num_guc_variables++] = var;
2749         qsort((void *) guc_variables, num_guc_variables,
2750                   sizeof(struct config_generic *), guc_var_compare);
2751         return true;
2752 }
2753
2754 /*
2755  * Create and add a placeholder variable. It's presumed to belong
2756  * to a valid custom variable class at this point.
2757  */
2758 static struct config_string *
2759 add_placeholder_variable(const char *name, int elevel)
2760 {
2761         size_t          sz = sizeof(struct config_string) + sizeof(char *);
2762         struct config_string *var;
2763         struct config_generic *gen;
2764
2765         var = (struct config_string *) guc_malloc(elevel, sz);
2766         if (var == NULL)
2767                 return NULL;
2768
2769         gen = &var->gen;
2770         memset(var, 0, sz);
2771
2772         gen->name = guc_strdup(elevel, name);
2773         if (gen->name == NULL)
2774         {
2775                 free(var);
2776                 return NULL;
2777         }
2778
2779         gen->context = PGC_USERSET;
2780         gen->group = CUSTOM_OPTIONS;
2781         gen->short_desc = "GUC placeholder variable";
2782         gen->flags = GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE | GUC_CUSTOM_PLACEHOLDER;
2783         gen->vartype = PGC_STRING;
2784
2785         /*
2786          * The char* is allocated at the end of the struct since we have no
2787          * 'static' place to point to.
2788          */
2789         var->variable = (char **) (var + 1);
2790
2791         if (!add_guc_variable((struct config_generic *) var, elevel))
2792         {
2793                 free((void *) gen->name);
2794                 free(var);
2795                 return NULL;
2796         }
2797
2798         return var;
2799 }
2800
2801 /*
2802  * Look up option NAME. If it exists, return a pointer to its record,
2803  * else return NULL.
2804  */
2805 static struct config_generic *
2806 find_option(const char *name, int elevel)
2807 {
2808         const char *dot;
2809         const char **key = &name;
2810         struct config_generic **res;
2811         int                     i;
2812
2813         Assert(name);
2814
2815         /*
2816          * By equating const char ** with struct config_generic *, we are assuming
2817          * the name field is first in config_generic.
2818          */
2819         res = (struct config_generic **) bsearch((void *) &key,
2820                                                                                          (void *) guc_variables,
2821                                                                                          num_guc_variables,
2822                                                                                          sizeof(struct config_generic *),
2823                                                                                          guc_var_compare);
2824         if (res)
2825                 return *res;
2826
2827         /*
2828          * See if the name is an obsolete name for a variable.  We assume that the
2829          * set of supported old names is short enough that a brute-force search is
2830          * the best way.
2831          */
2832         for (i = 0; map_old_guc_names[i] != NULL; i += 2)
2833         {
2834                 if (guc_name_compare(name, map_old_guc_names[i]) == 0)
2835                         return find_option(map_old_guc_names[i + 1], elevel);
2836         }
2837
2838         /*
2839          * Check if the name is qualified, and if so, check if the qualifier maps
2840          * to a custom variable class.
2841          */
2842         dot = strchr(name, GUC_QUALIFIER_SEPARATOR);
2843         if (dot != NULL && is_custom_class(name, dot - name))
2844                 /* Add a placeholder variable for this name */
2845                 return (struct config_generic *) add_placeholder_variable(name, elevel);
2846
2847         /* Unknown name */
2848         return NULL;
2849 }
2850
2851
2852 /*
2853  * comparator for qsorting and bsearching guc_variables array
2854  */
2855 static int
2856 guc_var_compare(const void *a, const void *b)
2857 {
2858         struct config_generic *confa = *(struct config_generic **) a;
2859         struct config_generic *confb = *(struct config_generic **) b;
2860
2861         return guc_name_compare(confa->name, confb->name);
2862 }
2863
2864
2865 static int
2866 guc_name_compare(const char *namea, const char *nameb)
2867 {
2868         /*
2869          * The temptation to use strcasecmp() here must be resisted, because the
2870          * array ordering has to remain stable across setlocale() calls. So, build
2871          * our own with a simple ASCII-only downcasing.
2872          */
2873         while (*namea && *nameb)
2874         {
2875                 char            cha = *namea++;
2876                 char            chb = *nameb++;
2877
2878                 if (cha >= 'A' && cha <= 'Z')
2879                         cha += 'a' - 'A';
2880                 if (chb >= 'A' && chb <= 'Z')
2881                         chb += 'a' - 'A';
2882                 if (cha != chb)
2883                         return cha - chb;
2884         }
2885         if (*namea)
2886                 return 1;                               /* a is longer */
2887         if (*nameb)
2888                 return -1;                              /* b is longer */
2889         return 0;
2890 }
2891
2892
2893 static int
2894 guc_get_index(const char *name)
2895 {
2896         const char **key = &name;
2897         struct config_generic **res;
2898
2899         Assert(name);
2900
2901         /*
2902          * By equating const char ** with struct config_generic *, we are assuming
2903          * the name field is first in config_generic.
2904          */
2905         res = (struct config_generic **) bsearch((void *) &key,
2906                                                                                          (void *) guc_variables,
2907                                                                                          num_guc_variables,
2908                                                                                          sizeof(struct config_generic *),
2909                                                                                          guc_var_compare);
2910         if (!res)
2911                 return -1;
2912
2913         return res - guc_variables;
2914 }
2915
2916
2917 /*
2918  * Initialize GUC options during program startup.
2919  *
2920  * Note that we cannot read the config file yet, since we have not yet
2921  * processed command-line switches.
2922  */
2923 void
2924 InitializeGUCOptions(void)
2925 {
2926         int                     i;
2927         char       *env;
2928         long            stack_rlimit;
2929
2930         /*
2931          * Before log_line_prefix could possibly receive a nonempty setting,
2932          * make sure that timezone processing is minimally alive (see elog.c).
2933          */
2934         pg_timezone_pre_initialize();
2935
2936         /*
2937          * Build sorted array of all GUC variables.
2938          */
2939         build_guc_variables();
2940
2941         /*
2942          * Load all variables with their compiled-in defaults, and initialize
2943          * status fields as needed.
2944          */
2945         for (i = 0; i < num_guc_variables; i++)
2946         {
2947                 struct config_generic *gconf = guc_variables[i];
2948
2949                 gconf->status = 0;
2950                 gconf->reset_source = PGC_S_DEFAULT;
2951                 gconf->tentative_source = PGC_S_DEFAULT;
2952                 gconf->source = PGC_S_DEFAULT;
2953                 gconf->stack = NULL;
2954
2955                 switch (gconf->vartype)
2956                 {
2957                         case PGC_BOOL:
2958                                 {
2959                                         struct config_bool *conf = (struct config_bool *) gconf;
2960
2961                                         if (conf->assign_hook)
2962                                                 if (!(*conf->assign_hook) (conf->boot_val, true,
2963                                                                                                    PGC_S_DEFAULT))
2964                                                         elog(FATAL, "failed to initialize %s to %d",
2965                                                                  conf->gen.name, (int) conf->boot_val);
2966                                         *conf->variable = conf->reset_val = conf->boot_val;
2967                                         break;
2968                                 }
2969                         case PGC_INT:
2970                                 {
2971                                         struct config_int *conf = (struct config_int *) gconf;
2972
2973                                         Assert(conf->boot_val >= conf->min);
2974                                         Assert(conf->boot_val <= conf->max);
2975                                         if (conf->assign_hook)
2976                                                 if (!(*conf->assign_hook) (conf->boot_val, true,
2977                                                                                                    PGC_S_DEFAULT))
2978                                                         elog(FATAL, "failed to initialize %s to %d",
2979                                                                  conf->gen.name, conf->boot_val);
2980                                         *conf->variable = conf->reset_val = conf->boot_val;
2981                                         break;
2982                                 }
2983                         case PGC_REAL:
2984                                 {
2985                                         struct config_real *conf = (struct config_real *) gconf;
2986
2987                                         Assert(conf->boot_val >= conf->min);
2988                                         Assert(conf->boot_val <= conf->max);
2989                                         if (conf->assign_hook)
2990                                                 if (!(*conf->assign_hook) (conf->boot_val, true,
2991                                                                                                    PGC_S_DEFAULT))
2992                                                         elog(FATAL, "failed to initialize %s to %g",
2993                                                                  conf->gen.name, conf->boot_val);
2994                                         *conf->variable = conf->reset_val = conf->boot_val;
2995                                         break;
2996                                 }
2997                         case PGC_STRING:
2998                                 {
2999                                         struct config_string *conf = (struct config_string *) gconf;
3000                                         char       *str;
3001
3002                                         *conf->variable = NULL;
3003                                         conf->reset_val = NULL;
3004                                         conf->tentative_val = NULL;
3005
3006                                         if (conf->boot_val == NULL)
3007                                         {
3008                                                 /* Cannot set value yet */
3009                                                 break;
3010                                         }
3011
3012                                         str = guc_strdup(FATAL, conf->boot_val);
3013                                         conf->reset_val = str;
3014
3015                                         if (conf->assign_hook)
3016                                         {
3017                                                 const char *newstr;
3018
3019                                                 newstr = (*conf->assign_hook) (str, true,
3020                                                                                                            PGC_S_DEFAULT);
3021                                                 if (newstr == NULL)
3022                                                 {
3023                                                         elog(FATAL, "failed to initialize %s to \"%s\"",
3024                                                                  conf->gen.name, str);
3025                                                 }
3026                                                 else if (newstr != str)
3027                                                 {
3028                                                         free(str);
3029
3030                                                         /*
3031                                                          * See notes in set_config_option about casting
3032                                                          */
3033                                                         str = (char *) newstr;
3034                                                         conf->reset_val = str;
3035                                                 }
3036                                         }
3037                                         *conf->variable = str;
3038                                         break;
3039                                 }
3040                 }
3041         }
3042
3043         guc_dirty = false;
3044
3045         reporting_enabled = false;
3046
3047         /*
3048          * Prevent any attempt to override the transaction modes from
3049          * non-interactive sources.
3050          */
3051         SetConfigOption("transaction_isolation", "default",
3052                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3053         SetConfigOption("transaction_read_only", "no",
3054                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3055
3056         /*
3057          * For historical reasons, some GUC parameters can receive defaults from
3058          * environment variables.  Process those settings.
3059          */
3060
3061         env = getenv("PGPORT");
3062         if (env != NULL)
3063                 SetConfigOption("port", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3064
3065         env = getenv("PGDATESTYLE");
3066         if (env != NULL)
3067                 SetConfigOption("datestyle", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3068
3069         env = getenv("PGCLIENTENCODING");
3070         if (env != NULL)
3071                 SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3072
3073         /*
3074          * rlimit isn't exactly an "environment variable", but it behaves about
3075          * the same.  If we can identify the platform stack depth rlimit, increase
3076          * default stack depth setting up to whatever is safe (but at most 2MB).
3077          */
3078         stack_rlimit = get_stack_depth_rlimit();
3079         if (stack_rlimit > 0)
3080         {
3081                 int             new_limit = (stack_rlimit - STACK_DEPTH_SLOP) / 1024L;
3082
3083                 if (new_limit > 100)
3084                 {
3085                         char    limbuf[16];
3086
3087                         new_limit = Min(new_limit, 2048);
3088                         sprintf(limbuf, "%d", new_limit);
3089                         SetConfigOption("max_stack_depth", limbuf,
3090                                                         PGC_POSTMASTER, PGC_S_ENV_VAR);
3091                 }
3092         }
3093 }
3094
3095
3096 /*
3097  * Select the configuration files and data directory to be used, and
3098  * do the initial read of postgresql.conf.
3099  *
3100  * This is called after processing command-line switches.
3101  *              userDoption is the -D switch value if any (NULL if unspecified).
3102  *              progname is just for use in error messages.
3103  *
3104  * Returns true on success; on failure, prints a suitable error message
3105  * to stderr and returns false.
3106  */
3107 bool
3108 SelectConfigFiles(const char *userDoption, const char *progname)
3109 {
3110         char       *configdir;
3111         char       *fname;
3112         struct stat stat_buf;
3113
3114         /* configdir is -D option, or $PGDATA if no -D */
3115         if (userDoption)
3116                 configdir = make_absolute_path(userDoption);
3117         else
3118                 configdir = make_absolute_path(getenv("PGDATA"));
3119
3120         /*
3121          * Find the configuration file: if config_file was specified on the
3122          * command line, use it, else use configdir/postgresql.conf.  In any case
3123          * ensure the result is an absolute path, so that it will be interpreted
3124          * the same way by future backends.
3125          */
3126         if (ConfigFileName)
3127                 fname = make_absolute_path(ConfigFileName);
3128         else if (configdir)
3129         {
3130                 fname = guc_malloc(FATAL,
3131                                                    strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
3132                 sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
3133         }
3134         else
3135         {
3136                 write_stderr("%s does not know where to find the server configuration file.\n"
3137                                          "You must specify the --config-file or -D invocation "
3138                                          "option or set the PGDATA environment variable.\n",
3139                                          progname);
3140                 return false;
3141         }
3142
3143         /*
3144          * Set the ConfigFileName GUC variable to its final value, ensuring that
3145          * it can't be overridden later.
3146          */
3147         SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
3148         free(fname);
3149
3150         /*
3151          * Now read the config file for the first time.
3152          */
3153         if (stat(ConfigFileName, &stat_buf) != 0)
3154         {
3155                 write_stderr("%s cannot access the server configuration file \"%s\": %s\n",
3156                                          progname, ConfigFileName, strerror(errno));
3157                 return false;
3158         }
3159
3160         ProcessConfigFile(PGC_POSTMASTER);
3161
3162         /*
3163          * If the data_directory GUC variable has been set, use that as DataDir;
3164          * otherwise use configdir if set; else punt.
3165          *
3166          * Note: SetDataDir will copy and absolute-ize its argument, so we don't
3167          * have to.
3168          */
3169         if (data_directory)
3170                 SetDataDir(data_directory);
3171         else if (configdir)
3172                 SetDataDir(configdir);
3173         else
3174         {
3175                 write_stderr("%s does not know where to find the database system data.\n"
3176                                          "This can be specified as \"data_directory\" in \"%s\", "
3177                                          "or by the -D invocation option, or by the "
3178                                          "PGDATA environment variable.\n",
3179                                          progname, ConfigFileName);
3180                 return false;
3181         }
3182
3183         /*
3184          * Reflect the final DataDir value back into the data_directory GUC var.
3185          * (If you are wondering why we don't just make them a single variable,
3186          * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
3187          * child backends specially.  XXX is that still true?  Given that we now
3188          * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
3189          * DataDir in advance.)
3190          */
3191         SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);
3192
3193         /*
3194          * Figure out where pg_hba.conf is, and make sure the path is absolute.
3195          */
3196         if (HbaFileName)
3197                 fname = make_absolute_path(HbaFileName);
3198         else if (configdir)
3199         {
3200                 fname = guc_malloc(FATAL,
3201                                                    strlen(configdir) + strlen(HBA_FILENAME) + 2);
3202                 sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
3203         }
3204         else
3205         {
3206                 write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
3207                                          "This can be specified as \"hba_file\" in \"%s\", "
3208                                          "or by the -D invocation option, or by the "
3209                                          "PGDATA environment variable.\n",
3210                                          progname, ConfigFileName);
3211                 return false;
3212         }
3213         SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
3214         free(fname);
3215
3216         /*
3217          * Likewise for pg_ident.conf.
3218          */
3219         if (IdentFileName)
3220                 fname = make_absolute_path(IdentFileName);
3221         else if (configdir)
3222         {
3223                 fname = guc_malloc(FATAL,
3224                                                    strlen(configdir) + strlen(IDENT_FILENAME) + 2);
3225                 sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
3226         }
3227         else
3228         {
3229                 write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
3230                                          "This can be specified as \"ident_file\" in \"%s\", "
3231                                          "or by the -D invocation option, or by the "
3232                                          "PGDATA environment variable.\n",
3233                                          progname, ConfigFileName);
3234                 return false;
3235         }
3236         SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
3237         free(fname);
3238
3239         free(configdir);
3240
3241         return true;
3242 }
3243
3244
3245 /*
3246  * Reset all options to their saved default values (implements RESET ALL)
3247  */
3248 void
3249 ResetAllOptions(void)
3250 {
3251         int                     i;
3252
3253         for (i = 0; i < num_guc_variables; i++)
3254         {
3255                 struct config_generic *gconf = guc_variables[i];
3256
3257                 /* Don't reset non-SET-able values */
3258                 if (gconf->context != PGC_SUSET &&
3259                         gconf->context != PGC_USERSET)
3260                         continue;
3261                 /* Don't reset if special exclusion from RESET ALL */
3262                 if (gconf->flags & GUC_NO_RESET_ALL)
3263                         continue;
3264                 /* No need to reset if wasn't SET */
3265                 if (gconf->source <= PGC_S_OVERRIDE)
3266                         continue;
3267
3268                 /* Save old value to support transaction abort */
3269                 push_old_value(gconf);
3270
3271                 switch (gconf->vartype)
3272                 {
3273                         case PGC_BOOL:
3274                                 {
3275                                         struct config_bool *conf = (struct config_bool *) gconf;
3276
3277                                         if (conf->assign_hook)
3278                                                 if (!(*conf->assign_hook) (conf->reset_val, true,
3279                                                                                                    PGC_S_SESSION))
3280                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3281                                         *conf->variable = conf->reset_val;
3282                                         conf->tentative_val = conf->reset_val;
3283                                         conf->gen.source = conf->gen.reset_source;
3284                                         conf->gen.tentative_source = conf->gen.reset_source;
3285                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3286                                         guc_dirty = true;
3287                                         break;
3288                                 }
3289                         case PGC_INT:
3290                                 {
3291                                         struct config_int *conf = (struct config_int *) gconf;
3292
3293                                         if (conf->assign_hook)
3294                                                 if (!(*conf->assign_hook) (conf->reset_val, true,
3295                                                                                                    PGC_S_SESSION))
3296                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3297                                         *conf->variable = conf->reset_val;
3298                                         conf->tentative_val = conf->reset_val;
3299                                         conf->gen.source = conf->gen.reset_source;
3300                                         conf->gen.tentative_source = conf->gen.reset_source;
3301                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3302                                         guc_dirty = true;
3303                                         break;
3304                                 }
3305                         case PGC_REAL:
3306                                 {
3307                                         struct config_real *conf = (struct config_real *) gconf;
3308
3309                                         if (conf->assign_hook)
3310                                                 if (!(*conf->assign_hook) (conf->reset_val, true,
3311                                                                                                    PGC_S_SESSION))
3312                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3313                                         *conf->variable = conf->reset_val;
3314                                         conf->tentative_val = conf->reset_val;
3315                                         conf->gen.source = conf->gen.reset_source;
3316                                         conf->gen.tentative_source = conf->gen.reset_source;
3317                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3318                                         guc_dirty = true;
3319                                         break;
3320                                 }
3321                         case PGC_STRING:
3322                                 {
3323                                         struct config_string *conf = (struct config_string *) gconf;
3324                                         char       *str;
3325
3326                                         if (conf->reset_val == NULL)
3327                                         {
3328                                                 /* Nothing to reset to, as yet; so do nothing */
3329                                                 break;
3330                                         }
3331
3332                                         /* We need not strdup here */
3333                                         str = conf->reset_val;
3334
3335                                         if (conf->assign_hook)
3336                                         {
3337                                                 const char *newstr;
3338
3339                                                 newstr = (*conf->assign_hook) (str, true,
3340                                                                                                            PGC_S_SESSION);
3341                                                 if (newstr == NULL)
3342                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3343                                                 else if (newstr != str)
3344                                                 {
3345                                                         /*
3346                                                          * See notes in set_config_option about casting
3347                                                          */
3348                                                         str = (char *) newstr;
3349                                                 }
3350                                         }
3351
3352                                         set_string_field(conf, conf->variable, str);
3353                                         set_string_field(conf, &conf->tentative_val, str);
3354                                         conf->gen.source = conf->gen.reset_source;
3355                                         conf->gen.tentative_source = conf->gen.reset_source;
3356                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3357                                         guc_dirty = true;
3358                                         break;
3359                                 }
3360                 }
3361
3362                 if (gconf->flags & GUC_REPORT)
3363                         ReportGUCOption(gconf);
3364         }
3365 }
3366
3367
3368 /*
3369  * push_old_value
3370  *              Push previous state during first assignment to a GUC variable
3371  *              within a particular transaction.
3372  *
3373  * We have to be willing to "back-fill" the state stack if the first
3374  * assignment occurs within a subtransaction nested several levels deep.
3375  * This ensures that if an intermediate transaction aborts, it will have
3376  * the proper value available to restore the setting to.
3377  */
3378 static void
3379 push_old_value(struct config_generic * gconf)
3380 {
3381         int                     my_level = GetCurrentTransactionNestLevel();
3382         GucStack   *stack;
3383
3384         /* If we're not inside a transaction, do nothing */
3385         if (my_level == 0)
3386                 return;
3387
3388         for (;;)
3389         {
3390                 /* Done if we already pushed it at this nesting depth */
3391                 if (gconf->stack && gconf->stack->nest_level >= my_level)
3392                         return;
3393
3394                 /*
3395                  * We keep all the stack entries in TopTransactionContext so as to
3396                  * avoid allocation problems when a subtransaction back-fills stack
3397                  * entries for upper transaction levels.
3398                  */
3399                 stack = (GucStack *) MemoryContextAlloc(TopTransactionContext,
3400                                                                                                 sizeof(GucStack));
3401
3402                 stack->prev = gconf->stack;
3403                 stack->nest_level = stack->prev ? stack->prev->nest_level + 1 : 1;
3404                 stack->status = gconf->status;
3405                 stack->tentative_source = gconf->tentative_source;
3406                 stack->source = gconf->source;
3407
3408                 switch (gconf->vartype)
3409                 {
3410                         case PGC_BOOL:
3411                                 stack->tentative_val.boolval =
3412                                         ((struct config_bool *) gconf)->tentative_val;
3413                                 stack->value.boolval =
3414                                         *((struct config_bool *) gconf)->variable;
3415                                 break;
3416
3417                         case PGC_INT:
3418                                 stack->tentative_val.intval =
3419                                         ((struct config_int *) gconf)->tentative_val;
3420                                 stack->value.intval =
3421                                         *((struct config_int *) gconf)->variable;
3422                                 break;
3423
3424                         case PGC_REAL:
3425                                 stack->tentative_val.realval =
3426                                         ((struct config_real *) gconf)->tentative_val;
3427                                 stack->value.realval =
3428                                         *((struct config_real *) gconf)->variable;
3429                                 break;
3430
3431                         case PGC_STRING:
3432                                 stack->tentative_val.stringval =
3433                                         ((struct config_string *) gconf)->tentative_val;
3434                                 stack->value.stringval =
3435                                         *((struct config_string *) gconf)->variable;
3436                                 break;
3437                 }
3438
3439                 gconf->stack = stack;
3440
3441                 /* Set state to indicate nothing happened yet within this level */
3442                 gconf->status = GUC_HAVE_STACK;
3443
3444                 /* Ensure we remember to pop at end of xact */
3445                 guc_dirty = true;
3446         }
3447 }
3448
3449 /*
3450  * Do GUC processing at transaction or subtransaction commit or abort.
3451  */
3452 void
3453 AtEOXact_GUC(bool isCommit, bool isSubXact)
3454 {
3455         int                     my_level;
3456         int                     i;
3457
3458         /* Quick exit if nothing's changed in this transaction */
3459         if (!guc_dirty)
3460                 return;
3461
3462         my_level = GetCurrentTransactionNestLevel();
3463         Assert(isSubXact ? (my_level > 1) : (my_level == 1));
3464
3465         for (i = 0; i < num_guc_variables; i++)
3466         {
3467                 struct config_generic *gconf = guc_variables[i];
3468                 int                     my_status = gconf->status;
3469                 GucStack   *stack = gconf->stack;
3470                 bool            useTentative;
3471                 bool            changed;
3472
3473                 /*
3474                  * Skip if nothing's happened to this var in this transaction
3475                  */
3476                 if (my_status == 0)
3477                 {
3478                         Assert(stack == NULL);
3479                         continue;
3480                 }
3481                 /* Assert that we stacked old value before changing it */
3482                 Assert(stack != NULL && (my_status & GUC_HAVE_STACK));
3483                 /* However, the last change may have been at an outer xact level */
3484                 if (stack->nest_level < my_level)
3485                         continue;
3486                 Assert(stack->nest_level == my_level);
3487
3488                 /*
3489                  * We will pop the stack entry.  Start by restoring outer xact status
3490                  * (since we may want to modify it below).      Be careful to use
3491                  * my_status to reference the inner xact status below this point...
3492                  */
3493                 gconf->status = stack->status;
3494
3495                 /*
3496                  * We have two cases:
3497                  *
3498                  * If commit and HAVE_TENTATIVE, set actual value to tentative (this
3499                  * is to override a SET LOCAL if one occurred later than SET). We keep
3500                  * the tentative value and propagate HAVE_TENTATIVE to the parent
3501                  * status, allowing the SET's effect to percolate up. (But if we're
3502                  * exiting the outermost transaction, we'll drop the HAVE_TENTATIVE
3503                  * bit below.)
3504                  *
3505                  * Otherwise, we have a transaction that aborted or executed only SET
3506                  * LOCAL (or no SET at all).  In either case it should have no further
3507                  * effect, so restore both tentative and actual values from the stack
3508                  * entry.
3509                  */
3510
3511                 useTentative = isCommit && (my_status & GUC_HAVE_TENTATIVE) != 0;
3512                 changed = false;
3513
3514                 switch (gconf->vartype)
3515                 {
3516                         case PGC_BOOL:
3517                                 {
3518                                         struct config_bool *conf = (struct config_bool *) gconf;
3519                                         bool            newval;
3520                                         GucSource       newsource;
3521
3522                                         if (useTentative)
3523                                         {
3524                                                 newval = conf->tentative_val;
3525                                                 newsource = conf->gen.tentative_source;
3526                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3527                                         }
3528                                         else
3529                                         {
3530                                                 newval = stack->value.boolval;
3531                                                 newsource = stack->source;
3532                                                 conf->tentative_val = stack->tentative_val.boolval;
3533                                                 conf->gen.tentative_source = stack->tentative_source;
3534                                         }
3535
3536                                         if (*conf->variable != newval)
3537                                         {
3538                                                 if (conf->assign_hook)
3539                                                         if (!(*conf->assign_hook) (newval,
3540                                                                                                            true, PGC_S_OVERRIDE))
3541                                                                 elog(LOG, "failed to commit %s",
3542                                                                          conf->gen.name);
3543                                                 *conf->variable = newval;
3544                                                 changed = true;
3545                                         }
3546                                         conf->gen.source = newsource;
3547                                         break;
3548                                 }
3549                         case PGC_INT:
3550                                 {
3551                                         struct config_int *conf = (struct config_int *) gconf;
3552                                         int                     newval;
3553                                         GucSource       newsource;
3554
3555                                         if (useTentative)
3556                                         {
3557                                                 newval = conf->tentative_val;
3558                                                 newsource = conf->gen.tentative_source;
3559                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3560                                         }
3561                                         else
3562                                         {
3563                                                 newval = stack->value.intval;
3564                                                 newsource = stack->source;
3565                                                 conf->tentative_val = stack->tentative_val.intval;
3566                                                 conf->gen.tentative_source = stack->tentative_source;
3567                                         }
3568
3569                                         if (*conf->variable != newval)
3570                                         {
3571                                                 if (conf->assign_hook)
3572                                                         if (!(*conf->assign_hook) (newval,
3573                                                                                                            true, PGC_S_OVERRIDE))
3574                                                                 elog(LOG, "failed to commit %s",
3575                                                                          conf->gen.name);
3576                                                 *conf->variable = newval;
3577                                                 changed = true;
3578                                         }
3579                                         conf->gen.source = newsource;
3580                                         break;
3581                                 }
3582                         case PGC_REAL:
3583                                 {
3584                                         struct config_real *conf = (struct config_real *) gconf;
3585                                         double          newval;
3586                                         GucSource       newsource;
3587
3588                                         if (useTentative)
3589                                         {
3590                                                 newval = conf->tentative_val;
3591                                                 newsource = conf->gen.tentative_source;
3592                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3593                                         }
3594                                         else
3595                                         {
3596                                                 newval = stack->value.realval;
3597                                                 newsource = stack->source;
3598                                                 conf->tentative_val = stack->tentative_val.realval;
3599                                                 conf->gen.tentative_source = stack->tentative_source;
3600                                         }
3601
3602                                         if (*conf->variable != newval)
3603                                         {
3604                                                 if (conf->assign_hook)
3605                                                         if (!(*conf->assign_hook) (newval,
3606                                                                                                            true, PGC_S_OVERRIDE))
3607                                                                 elog(LOG, "failed to commit %s",
3608                                                                          conf->gen.name);
3609                                                 *conf->variable = newval;
3610                                                 changed = true;
3611                                         }
3612                                         conf->gen.source = newsource;
3613                                         break;
3614                                 }
3615                         case PGC_STRING:
3616                                 {
3617                                         struct config_string *conf = (struct config_string *) gconf;
3618                                         char       *newval;
3619                                         GucSource       newsource;
3620
3621                                         if (useTentative)
3622                                         {
3623                                                 newval = conf->tentative_val;
3624                                                 newsource = conf->gen.tentative_source;
3625                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3626                                         }
3627                                         else
3628                                         {
3629                                                 newval = stack->value.stringval;
3630                                                 newsource = stack->source;
3631                                                 set_string_field(conf, &conf->tentative_val,
3632                                                                                  stack->tentative_val.stringval);
3633                                                 conf->gen.tentative_source = stack->tentative_source;
3634                                         }
3635
3636                                         if (*conf->variable != newval)
3637                                         {
3638                                                 if (conf->assign_hook)
3639                                                 {
3640                                                         const char *newstr;
3641
3642                                                         newstr = (*conf->assign_hook) (newval, true,
3643                                                                                                                    PGC_S_OVERRIDE);
3644                                                         if (newstr == NULL)
3645                                                                 elog(LOG, "failed to commit %s",
3646                                                                          conf->gen.name);
3647                                                         else if (newstr != newval)
3648                                                         {
3649                                                                 /*
3650                                                                  * If newval should now be freed, it'll be
3651                                                                  * taken care of below.
3652                                                                  *
3653                                                                  * See notes in set_config_option about
3654                                                                  * casting
3655                                                                  */
3656                                                                 newval = (char *) newstr;
3657                                                         }
3658                                                 }
3659
3660                                                 set_string_field(conf, conf->variable, newval);
3661                                                 changed = true;
3662                                         }
3663                                         conf->gen.source = newsource;
3664                                         /* Release stacked values if not used anymore */
3665                                         set_string_field(conf, &stack->value.stringval,
3666                                                                          NULL);
3667                                         set_string_field(conf, &stack->tentative_val.stringval,
3668                                                                          NULL);
3669                                         /* Don't store tentative value separately after commit */
3670                                         if (!isSubXact)
3671                                                 set_string_field(conf, &conf->tentative_val, NULL);
3672                                         break;
3673                                 }
3674                 }
3675
3676                 /* Finish popping the state stack */
3677                 gconf->stack = stack->prev;
3678                 pfree(stack);
3679
3680                 /*
3681                  * If we're now out of all xact levels, forget TENTATIVE status bit;
3682                  * there's nothing tentative about the value anymore.
3683                  */
3684                 if (!isSubXact)
3685                 {
3686                         Assert(gconf->stack == NULL);
3687                         gconf->status = 0;
3688                 }
3689
3690                 /* Report new value if we changed it */
3691                 if (changed && (gconf->flags & GUC_REPORT))
3692                         ReportGUCOption(gconf);
3693         }
3694
3695         /*
3696          * If we're now out of all xact levels, we can clear guc_dirty. (Note: we
3697          * cannot reset guc_dirty when exiting a subtransaction, because we know
3698          * that all outer transaction levels will have stacked values to deal
3699          * with.)
3700          */
3701         if (!isSubXact)
3702                 guc_dirty = false;
3703 }
3704
3705
3706 /*
3707  * Start up automatic reporting of changes to variables marked GUC_REPORT.
3708  * This is executed at completion of backend startup.
3709  */
3710 void
3711 BeginReportingGUCOptions(void)
3712 {
3713         int                     i;
3714
3715         /*
3716          * Don't do anything unless talking to an interactive frontend of protocol
3717          * 3.0 or later.
3718          */
3719         if (whereToSendOutput != DestRemote ||
3720                 PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
3721                 return;
3722
3723         reporting_enabled = true;
3724
3725         /* Transmit initial values of interesting variables */
3726         for (i = 0; i < num_guc_variables; i++)
3727         {
3728                 struct config_generic *conf = guc_variables[i];
3729
3730                 if (conf->flags & GUC_REPORT)
3731                         ReportGUCOption(conf);
3732         }
3733 }
3734
3735 /*
3736  * ReportGUCOption: if appropriate, transmit option value to frontend
3737  */
3738 static void
3739 ReportGUCOption(struct config_generic * record)
3740 {
3741         if (reporting_enabled && (record->flags & GUC_REPORT))
3742         {
3743                 char       *val = _ShowOption(record, false);
3744                 StringInfoData msgbuf;
3745
3746                 pq_beginmessage(&msgbuf, 'S');
3747                 pq_sendstring(&msgbuf, record->name);
3748                 pq_sendstring(&msgbuf, val);
3749                 pq_endmessage(&msgbuf);
3750
3751                 pfree(val);
3752         }
3753 }
3754
3755
3756 /*
3757  * Try to interpret value as boolean value.  Valid values are: true,
3758  * false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
3759  * If the string parses okay, return true, else false.
3760  * If okay and result is not NULL, return the value in *result.
3761  */
3762 static bool
3763 parse_bool(const char *value, bool *result)
3764 {
3765         size_t          len = strlen(value);
3766
3767         if (pg_strncasecmp(value, "true", len) == 0)
3768         {
3769                 if (result)
3770                         *result = true;
3771         }
3772         else if (pg_strncasecmp(value, "false", len) == 0)
3773         {
3774                 if (result)
3775                         *result = false;
3776         }
3777
3778         else if (pg_strncasecmp(value, "yes", len) == 0)
3779         {
3780                 if (result)
3781                         *result = true;
3782         }
3783         else if (pg_strncasecmp(value, "no", len) == 0)
3784         {
3785                 if (result)
3786                         *result = false;
3787         }
3788
3789         /* 'o' is not unique enough */
3790         else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
3791         {
3792                 if (result)
3793                         *result = true;
3794         }
3795         else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
3796         {
3797                 if (result)
3798                         *result = false;
3799         }
3800
3801         else if (pg_strcasecmp(value, "1") == 0)
3802         {
3803                 if (result)
3804                         *result = true;
3805         }
3806         else if (pg_strcasecmp(value, "0") == 0)
3807         {
3808                 if (result)
3809                         *result = false;
3810         }
3811
3812         else
3813         {
3814                 if (result)
3815                         *result = false;        /* suppress compiler warning */
3816                 return false;
3817         }
3818         return true;
3819 }
3820
3821
3822
3823 /*
3824  * Try to parse value as an integer.  The accepted formats are the
3825  * usual decimal, octal, or hexadecimal formats, optionally followed by
3826  * a unit name if "flags" indicates a unit is allowed.
3827  *
3828  * If the string parses okay, return true, else false.
3829  * If okay and result is not NULL, return the value in *result.
3830  * If not okay and hintmsg is not NULL, *hintmsg is set to a suitable
3831  *      HINT message, or NULL if no hint provided.
3832  */
3833 static bool
3834 parse_int(const char *value, int *result, int flags, const char **hintmsg)
3835 {
3836         int64           val;
3837         char       *endptr;
3838
3839         /* To suppress compiler warnings, always set output params */
3840         if (result)
3841                 *result = 0;
3842         if (hintmsg)
3843                 *hintmsg = NULL;
3844
3845         /* We assume here that int64 is at least as wide as long */
3846         errno = 0;
3847         val = strtol(value, &endptr, 0);
3848
3849         if (endptr == value)
3850                 return false;                   /* no HINT for integer syntax error */
3851
3852         if (errno == ERANGE || val != (int64) ((int32) val))
3853         {
3854                 if (hintmsg)
3855                         *hintmsg = gettext_noop("Value exceeds integer range.");
3856                 return false;
3857         }
3858
3859         /* allow whitespace between integer and unit */
3860         while (isspace((unsigned char) *endptr))
3861                 endptr++;
3862
3863         /* Handle possible unit */
3864         if (*endptr != '\0')
3865         {
3866                 /*
3867                  * Note: the multiple-switch coding technique here is a bit tedious,
3868                  * but seems necessary to avoid intermediate-value overflows.
3869                  *
3870                  * If INT64_IS_BUSTED (ie, it's really int32) we will fail to detect
3871                  * overflow due to units conversion, but there are few enough such
3872                  * machines that it does not seem worth trying to be smarter.
3873                  */
3874                 if (flags & GUC_UNIT_MEMORY)
3875                 {
3876                         /* Set hint for use if no match or trailing garbage */
3877                         if (hintmsg)
3878                                 *hintmsg = gettext_noop("Valid units for this parameter are \"kB\", \"MB\", and \"GB\".");
3879
3880 #if BLCKSZ < 1024 || BLCKSZ > (1024*1024)
3881 #error BLCKSZ must be between 1KB and 1MB
3882 #endif
3883 #if XLOG_BLCKSZ < 1024 || XLOG_BLCKSZ > (1024*1024)
3884 #error XLOG_BLCKSZ must be between 1KB and 1MB
3885 #endif
3886
3887                         if (strncmp(endptr, "kB", 2) == 0)
3888                         {
3889                                 endptr += 2;
3890                                 switch (flags & GUC_UNIT_MEMORY)
3891                                 {
3892                                         case GUC_UNIT_BLOCKS:
3893                                                 val /= (BLCKSZ / 1024);
3894                                                 break;
3895                                         case GUC_UNIT_XBLOCKS:
3896                                                 val /= (XLOG_BLCKSZ / 1024);
3897                                                 break;
3898                                 }
3899                         }
3900                         else if (strncmp(endptr, "MB", 2) == 0)
3901                         {
3902                                 endptr += 2;
3903                                 switch (flags & GUC_UNIT_MEMORY)
3904                                 {
3905                                         case GUC_UNIT_KB:
3906                                                 val *= KB_PER_MB;
3907                                                 break;
3908                                         case GUC_UNIT_BLOCKS:
3909                                                 val *= KB_PER_MB / (BLCKSZ / 1024);
3910                                                 break;
3911                                         case GUC_UNIT_XBLOCKS:
3912                                                 val *= KB_PER_MB / (XLOG_BLCKSZ / 1024);
3913                                                 break;
3914                                 }
3915                         }
3916                         else if (strncmp(endptr, "GB", 2) == 0)
3917                         {
3918                                 endptr += 2;
3919                                 switch (flags & GUC_UNIT_MEMORY)
3920                                 {
3921                                         case GUC_UNIT_KB:
3922                                                 val *= KB_PER_GB;
3923                                                 break;
3924                                         case GUC_UNIT_BLOCKS:
3925                                                 val *= KB_PER_GB / (BLCKSZ / 1024);
3926                                                 break;
3927                                         case GUC_UNIT_XBLOCKS:
3928                                                 val *= KB_PER_GB / (XLOG_BLCKSZ / 1024);
3929                                                 break;
3930                                 }
3931                         }
3932                 }
3933                 else if (flags & GUC_UNIT_TIME)
3934                 {
3935                         /* Set hint for use if no match or trailing garbage */
3936                         if (hintmsg)
3937                                 *hintmsg = gettext_noop("Valid units for this parameter are \"ms\", \"s\", \"min\", \"h\", and \"d\".");
3938
3939                         if (strncmp(endptr, "ms", 2) == 0)
3940                         {
3941                                 endptr += 2;
3942                                 switch (flags & GUC_UNIT_TIME)
3943                                 {
3944                                         case GUC_UNIT_S:
3945                                                 val /= MS_PER_S;
3946                                                 break;
3947                                         case GUC_UNIT_MIN:
3948                                                 val /= MS_PER_MIN;
3949                                                 break;
3950                                 }
3951                         }
3952                         else if (strncmp(endptr, "s", 1) == 0)
3953                         {
3954                                 endptr += 1;
3955                                 switch (flags & GUC_UNIT_TIME)
3956                                 {
3957                                         case GUC_UNIT_MS:
3958                                                 val *= MS_PER_S;
3959                                                 break;
3960                                         case GUC_UNIT_MIN:
3961                                                 val /= S_PER_MIN;
3962                                                 break;
3963                                 }
3964                         }
3965                         else if (strncmp(endptr, "min", 3) == 0)
3966                         {
3967                                 endptr += 3;
3968                                 switch (flags & GUC_UNIT_TIME)
3969                                 {
3970                                         case GUC_UNIT_MS:
3971                                                 val *= MS_PER_MIN;
3972                                                 break;
3973                                         case GUC_UNIT_S:
3974                                                 val *= S_PER_MIN;
3975                                                 break;
3976                                 }
3977                         }
3978                         else if (strncmp(endptr, "h", 1) == 0)
3979                         {
3980                                 endptr += 1;
3981                                 switch (flags & GUC_UNIT_TIME)
3982                                 {
3983                                         case GUC_UNIT_MS:
3984                                                 val *= MS_PER_H;
3985                                                 break;
3986                                         case GUC_UNIT_S:
3987                                                 val *= S_PER_H;
3988                                                 break;
3989                                         case GUC_UNIT_MIN:
3990                                                 val *= MIN_PER_H;
3991                                                 break;
3992                                 }
3993                         }
3994                         else if (strncmp(endptr, "d", 1) == 0)
3995                         {
3996                                 endptr += 1;
3997                                 switch (flags & GUC_UNIT_TIME)
3998                                 {
3999                                         case GUC_UNIT_MS:
4000                                                 val *= MS_PER_D;
4001                                                 break;
4002                                         case GUC_UNIT_S:
4003                                                 val *= S_PER_D;
4004                                                 break;
4005                                         case GUC_UNIT_MIN:
4006                                                 val *= MIN_PER_D;
4007                                                 break;
4008                                 }
4009                         }
4010                 }
4011
4012                 /* allow whitespace after unit */
4013                 while (isspace((unsigned char) *endptr))
4014                         endptr++;
4015
4016                 if (*endptr != '\0')
4017                         return false;           /* appropriate hint, if any, already set */
4018
4019                 /* Check for overflow due to units conversion */
4020                 if (val != (int64) ((int32) val))
4021                 {
4022                         if (hintmsg)
4023                                 *hintmsg = gettext_noop("Value exceeds integer range.");
4024                         return false;
4025                 }
4026         }
4027
4028         if (result)
4029                 *result = (int) val;
4030         return true;
4031 }
4032
4033
4034
4035 /*
4036  * Try to parse value as a floating point number in the usual format.
4037  * If the string parses okay, return true, else false.
4038  * If okay and result is not NULL, return the value in *result.
4039  */
4040 static bool
4041 parse_real(const char *value, double *result)
4042 {
4043         double          val;
4044         char       *endptr;
4045
4046         if (result)
4047                 *result = 0;                    /* suppress compiler warning */
4048
4049         errno = 0;
4050         val = strtod(value, &endptr);
4051         if (endptr == value || errno == ERANGE)
4052                 return false;
4053
4054         /* allow whitespace after number */
4055         while (isspace((unsigned char) *endptr))
4056                 endptr++;
4057         if (*endptr != '\0')
4058                 return false;
4059
4060         if (result)
4061                 *result = val;
4062         return true;
4063 }
4064
4065
4066 /*
4067  * Call a GucStringAssignHook function, being careful to free the
4068  * "newval" string if the hook ereports.
4069  *
4070  * This is split out of set_config_option just to avoid the "volatile"
4071  * qualifiers that would otherwise have to be plastered all over.
4072  */
4073 static const char *
4074 call_string_assign_hook(GucStringAssignHook assign_hook,
4075                                                 char *newval, bool doit, GucSource source)
4076 {
4077         const char *result;
4078
4079         PG_TRY();
4080         {
4081                 result = (*assign_hook) (newval, doit, source);
4082         }
4083         PG_CATCH();
4084         {
4085                 free(newval);
4086                 PG_RE_THROW();
4087         }
4088         PG_END_TRY();
4089
4090         return result;
4091 }
4092
4093
4094 /*
4095  * Sets option `name' to given value. The value should be a string
4096  * which is going to be parsed and converted to the appropriate data
4097  * type.  The context and source parameters indicate in which context this
4098  * function is being called so it can apply the access restrictions
4099  * properly.
4100  *
4101  * If value is NULL, set the option to its default value. If the
4102  * parameter changeVal is false then don't really set the option but do all
4103  * the checks to see if it would work.
4104  *
4105  * If there is an error (non-existing option, invalid value) then an
4106  * ereport(ERROR) is thrown *unless* this is called in a context where we
4107  * don't want to ereport (currently, startup or SIGHUP config file reread).
4108  * In that case we write a suitable error message via ereport(DEBUG) and
4109  * return false. This is working around the deficiencies in the ereport
4110  * mechanism, so don't blame me.  In all other cases, the function
4111  * returns true, including cases where the input is valid but we chose
4112  * not to apply it because of context or source-priority considerations.
4113  *
4114  * See also SetConfigOption for an external interface.
4115  */
4116 bool
4117 set_config_option(const char *name, const char *value,
4118                                   GucContext context, GucSource source,
4119                                   bool isLocal, bool changeVal)
4120 {
4121         struct config_generic *record;
4122         int                     elevel;
4123         bool            makeDefault;
4124
4125         if (context == PGC_SIGHUP || source == PGC_S_DEFAULT)
4126         {
4127                 /*
4128                  * To avoid cluttering the log, only the postmaster bleats loudly
4129                  * about problems with the config file.
4130                  */
4131                 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
4132         }
4133         else if (source == PGC_S_DATABASE || source == PGC_S_USER)
4134                 elevel = INFO;
4135         else
4136                 elevel = ERROR;
4137
4138         record = find_option(name, elevel);
4139         if (record == NULL)
4140         {
4141                 ereport(elevel,
4142                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4143                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4144                 return false;
4145         }
4146
4147         /*
4148          * Do not replace a value that has been set on the command line by a SIGHUP
4149          * reload
4150          */
4151         if (context == PGC_SIGHUP && record->source == PGC_S_ARGV)
4152                 return true;
4153
4154         /*
4155          * Check if the option can be set at this time. See guc.h for the precise
4156          * rules. Note that we don't want to throw errors if we're in the SIGHUP
4157          * context. In that case we just ignore the attempt and return true.
4158          */
4159         switch (record->context)
4160         {
4161                 case PGC_INTERNAL:
4162                         if (context == PGC_SIGHUP)
4163                                 return true;
4164                         if (context != PGC_INTERNAL)
4165                         {
4166                                 ereport(elevel,
4167                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4168                                                  errmsg("parameter \"%s\" cannot be changed",
4169                                                                 name)));
4170                                 return false;
4171                         }
4172                         break;
4173                 case PGC_POSTMASTER:
4174                         if (context == PGC_SIGHUP)
4175                         {
4176                                 if (changeVal && !is_newvalue_equal(record, value))
4177                                         ereport(elevel,
4178                                                         (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4179                                                          errmsg("parameter \"%s\" cannot be changed after server start; configuration file change ignored",
4180                                                                         name)));
4181
4182                                 return true;
4183                         }
4184                         if (context != PGC_POSTMASTER)
4185                         {
4186                                 ereport(elevel,
4187                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4188                                                  errmsg("parameter \"%s\" cannot be changed after server start",
4189                                                                 name)));
4190                                 return false;
4191                         }
4192                         break;
4193                 case PGC_SIGHUP:
4194                         if (context != PGC_SIGHUP && context != PGC_POSTMASTER)
4195                         {
4196                                 ereport(elevel,
4197                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4198                                                  errmsg("parameter \"%s\" cannot be changed now",
4199                                                                 name)));
4200                                 return false;
4201                         }
4202
4203                         /*
4204                          * Hmm, the idea of the SIGHUP context is "ought to be global, but
4205                          * can be changed after postmaster start". But there's nothing
4206                          * that prevents a crafty administrator from sending SIGHUP
4207                          * signals to individual backends only.
4208                          */
4209                         break;
4210                 case PGC_BACKEND:
4211                         if (context == PGC_SIGHUP)
4212                         {
4213                                 /*
4214                                  * If a PGC_BACKEND parameter is changed in the config file,
4215                                  * we want to accept the new value in the postmaster (whence
4216                                  * it will propagate to subsequently-started backends), but
4217                                  * ignore it in existing backends.      This is a tad klugy, but
4218                                  * necessary because we don't re-read the config file during
4219                                  * backend start.
4220                                  */
4221                                 if (IsUnderPostmaster)
4222                                         return true;
4223                         }
4224                         else if (context != PGC_BACKEND && context != PGC_POSTMASTER)
4225                         {
4226                                 ereport(elevel,
4227                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4228                                                  errmsg("parameter \"%s\" cannot be set after connection start",
4229                                                                 name)));
4230                                 return false;
4231                         }
4232                         break;
4233                 case PGC_SUSET:
4234                         if (context == PGC_USERSET || context == PGC_BACKEND)
4235                         {
4236                                 ereport(elevel,
4237                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4238                                                  errmsg("permission denied to set parameter \"%s\"",
4239                                                                 name)));
4240                                 return false;
4241                         }
4242                         break;
4243                 case PGC_USERSET:
4244                         /* always okay */
4245                         break;
4246         }
4247
4248         /*
4249          * Should we set reset/stacked values?  (If so, the behavior is not
4250          * transactional.)  This is done either when we get a default
4251          * value from the database's/user's/client's default settings or
4252          * when we reset a value to its default.
4253          */
4254         makeDefault = changeVal && (source <= PGC_S_OVERRIDE)
4255                                         && ((value != NULL) || source == PGC_S_DEFAULT);
4256
4257         /*
4258          * Ignore attempted set if overridden by previously processed
4259          * setting.  However, if changeVal is false then plow ahead anyway
4260          * since we are trying to find out if the value is potentially
4261          * good, not actually use it.  Also keep going if makeDefault is
4262          * true, since we may want to set the reset/stacked values even if
4263          * we can't set the variable itself.  There's one exception to
4264          * this rule: if we want to apply the default value to variables
4265          * that were removed from the configuration file.  This is
4266          * indicated by source == PGC_S_DEFAULT and context == PGC_SIGHUP.
4267          */
4268         if (record->source > source
4269                 && !(source == PGC_S_DEFAULT && context == PGC_SIGHUP))
4270         {
4271                 if (changeVal && !makeDefault)
4272                 {
4273                         elog(DEBUG3, "\"%s\": setting ignored because previous source is higher priority",
4274                                  name);
4275                         return true;
4276                 }
4277                 changeVal = false;
4278         }
4279
4280         /*
4281          * Evaluate value and set variable.
4282          */
4283         switch (record->vartype)
4284         {
4285                 case PGC_BOOL:
4286                         {
4287                                 struct config_bool *conf = (struct config_bool *) record;
4288                                 bool            newval;
4289
4290                                 if (value)
4291                                 {
4292                                         if (!parse_bool(value, &newval))
4293                                         {
4294                                                 ereport(elevel,
4295                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4296                                                   errmsg("parameter \"%s\" requires a Boolean value",
4297                                                                  name)));
4298                                                 return false;
4299                                         }
4300                                 }
4301                                 /*
4302                                  * If value == NULL and source == PGC_S_DEFAULT then
4303                                  * we reset some value to its default (removed from
4304                                  * configuration file).
4305                                  */
4306                                 else if (source == PGC_S_DEFAULT)
4307                                         newval = conf->boot_val;
4308                                 /* else we handle a "RESET varname" command */
4309                                 else
4310                                 {
4311                                         newval = conf->reset_val;
4312                                         source = conf->gen.reset_source;
4313                                 }
4314
4315                                 if (conf->assign_hook)
4316                                         if (!(*conf->assign_hook) (newval, changeVal, source))
4317                                         {
4318                                                 ereport(elevel,
4319                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4320                                                          errmsg("invalid value for parameter \"%s\": %d",
4321                                                                         name, (int) newval)));
4322                                                 return false;
4323                                         }
4324
4325                                 if (changeVal || makeDefault)
4326                                 {
4327                                         /* Save old value to support transaction abort */
4328                                         if (!makeDefault)
4329                                                 push_old_value(&conf->gen);
4330                                         if (changeVal)
4331                                         {
4332                                                 *conf->variable = newval;
4333                                                 conf->gen.source = source;
4334                                         }
4335                                         if (makeDefault)
4336                                         {
4337                                                 GucStack   *stack;
4338
4339                                                 if (conf->gen.reset_source <= source)
4340                                                 {
4341                                                         conf->reset_val = newval;
4342                                                         conf->gen.reset_source = source;
4343                                                 }
4344                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4345                                                 {
4346                                                         if (stack->source <= source)
4347                                                         {
4348                                                                 stack->value.boolval = newval;
4349                                                                 stack->source = source;
4350                                                         }
4351                                                 }
4352                                         }
4353                                         else if (isLocal)
4354                                         {
4355                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4356                                                 guc_dirty = true;
4357                                         }
4358                                         else
4359                                         {
4360                                                 conf->tentative_val = newval;
4361                                                 conf->gen.tentative_source = source;
4362                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4363                                                 guc_dirty = true;
4364                                         }
4365                                 }
4366                                 break;
4367                         }
4368
4369                 case PGC_INT:
4370                         {
4371                                 struct config_int *conf = (struct config_int *) record;
4372                                 int                     newval;
4373
4374                                 if (value)
4375                                 {
4376                                         const char *hintmsg;
4377
4378                                         if (!parse_int(value, &newval, conf->gen.flags, &hintmsg))
4379                                         {
4380                                                 ereport(elevel,
4381                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4382                                                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
4383                                                                                 name, value),
4384                                                                  hintmsg ? errhint(hintmsg) : 0));
4385                                                 return false;
4386                                         }
4387                                         if (newval < conf->min || newval > conf->max)
4388                                         {
4389                                                 ereport(elevel,
4390                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4391                                                                  errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)",
4392                                                                                 newval, name, conf->min, conf->max)));
4393                                                 return false;
4394                                         }
4395                                 }
4396                                 /*
4397                                  * If value == NULL and source == PGC_S_DEFAULT then
4398                                  * we reset some value to its default (removed from
4399                                  * configuration file).
4400                                  */
4401                                 else if (source == PGC_S_DEFAULT)
4402                                         newval = conf->boot_val;
4403                                 /* else we handle a "RESET varname" command */
4404                                 else
4405                                 {
4406                                         newval = conf->reset_val;
4407                                         source = conf->gen.reset_source;
4408                                 }
4409
4410                                 if (conf->assign_hook)
4411                                         if (!(*conf->assign_hook) (newval, changeVal, source))
4412                                         {
4413                                                 ereport(elevel,
4414                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4415                                                          errmsg("invalid value for parameter \"%s\": %d",
4416                                                                         name, newval)));
4417                                                 return false;
4418                                         }
4419
4420                                 if (changeVal || makeDefault)
4421                                 {
4422                                         /* Save old value to support transaction abort */
4423                                         if (!makeDefault)
4424                                                 push_old_value(&conf->gen);
4425                                         if (changeVal)
4426                                         {
4427                                                 *conf->variable = newval;
4428                                                 conf->gen.source = source;
4429                                         }
4430                                         if (makeDefault)
4431                                         {
4432                                                 GucStack   *stack;
4433
4434                                                 if (conf->gen.reset_source <= source)
4435                                                 {
4436                                                         conf->reset_val = newval;
4437                                                         conf->gen.reset_source = source;
4438                                                 }
4439                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4440                                                 {
4441                                                         if (stack->source <= source)
4442                                                         {
4443                                                                 stack->value.intval = newval;
4444                                                                 stack->source = source;
4445                                                         }
4446                                                 }
4447                                         }
4448                                         else if (isLocal)
4449                                         {
4450                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4451                                                 guc_dirty = true;
4452                                         }
4453                                         else
4454                                         {
4455                                                 conf->tentative_val = newval;
4456                                                 conf->gen.tentative_source = source;
4457                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4458                                                 guc_dirty = true;
4459                                         }
4460                                 }
4461                                 break;
4462                         }
4463
4464                 case PGC_REAL:
4465                         {
4466                                 struct config_real *conf = (struct config_real *) record;
4467                                 double          newval;
4468
4469                                 if (value)
4470                                 {
4471                                         if (!parse_real(value, &newval))
4472                                         {
4473                                                 ereport(elevel,
4474                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4475                                                   errmsg("parameter \"%s\" requires a numeric value",
4476                                                                  name)));
4477                                                 return false;
4478                                         }
4479                                         if (newval < conf->min || newval > conf->max)
4480                                         {
4481                                                 ereport(elevel,
4482                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4483                                                                  errmsg("%g is outside the valid range for parameter \"%s\" (%g .. %g)",
4484                                                                                 newval, name, conf->min, conf->max)));
4485                                                 return false;
4486                                         }
4487                                 }
4488                                 /*
4489                                  * If value == NULL and source == PGC_S_DEFAULT then
4490                                  * we reset some value to its default (removed from
4491                                  * configuration file).
4492                                  */
4493                                 else if (source == PGC_S_DEFAULT)
4494                                         newval = conf->boot_val;
4495                                 /* else we handle a "RESET varname" command */
4496                                 else
4497                                 {
4498                                         newval = conf->reset_val;
4499                                         source = conf->gen.reset_source;
4500                                 }
4501
4502                                 if (conf->assign_hook)
4503                                         if (!(*conf->assign_hook) (newval, changeVal, source))
4504                                         {
4505                                                 ereport(elevel,
4506                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4507                                                          errmsg("invalid value for parameter \"%s\": %g",
4508                                                                         name, newval)));
4509                                                 return false;
4510                                         }
4511
4512                                 if (changeVal || makeDefault)
4513                                 {
4514                                         /* Save old value to support transaction abort */
4515                                         if (!makeDefault)
4516                                                 push_old_value(&conf->gen);
4517                                         if (changeVal)
4518                                         {
4519                                                 *conf->variable = newval;
4520                                                 conf->gen.source = source;
4521                                         }
4522                                         if (makeDefault)
4523                                         {
4524                                                 GucStack   *stack;
4525
4526                                                 if (conf->gen.reset_source <= source)
4527                                                 {
4528                                                         conf->reset_val = newval;
4529                                                         conf->gen.reset_source = source;
4530                                                 }
4531                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4532                                                 {
4533                                                         if (stack->source <= source)
4534                                                         {
4535                                                                 stack->value.realval = newval;
4536                                                                 stack->source = source;
4537                                                         }
4538                                                 }
4539                                         }
4540                                         else if (isLocal)
4541                                         {
4542                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4543                                                 guc_dirty = true;
4544                                         }
4545                                         else
4546                                         {
4547                                                 conf->tentative_val = newval;
4548                                                 conf->gen.tentative_source = source;
4549                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4550                                                 guc_dirty = true;
4551                                         }
4552                                 }
4553                                 break;
4554                         }
4555
4556                 case PGC_STRING:
4557                         {
4558                                 struct config_string *conf = (struct config_string *) record;
4559                                 char       *newval;
4560
4561                                 if (value)
4562                                 {
4563                                         newval = guc_strdup(elevel, value);
4564                                         if (newval == NULL)
4565                                                 return false;
4566
4567                                         /*
4568                                          * The only sort of "parsing" check we need to do is apply
4569                                          * truncation if GUC_IS_NAME.
4570                                          */
4571                                         if (conf->gen.flags & GUC_IS_NAME)
4572                                                 truncate_identifier(newval, strlen(newval), true);
4573                                 }
4574                                 /*
4575                                  * If value == NULL and source == PGC_S_DEFAULT then
4576                                  * we reset some value to its default (removed from
4577                                  * configuration file).
4578                                  */
4579                                 else if (source == PGC_S_DEFAULT)
4580                                 {
4581                                         if (conf->boot_val == NULL)
4582                                                 newval = NULL;
4583                                         else
4584                                         {
4585                                                 newval = guc_strdup(elevel, conf->boot_val);
4586                                                 if (newval == NULL)
4587                                                         return false;
4588                                         }
4589                                 }
4590                                 /* else we handle a "RESET varname" command */
4591                                 else if (conf->reset_val)
4592                                 {
4593                                         /*
4594                                          * We could possibly avoid strdup here, but easier to make
4595                                          * this case work the same as the normal assignment case.
4596                                          */
4597                                         newval = guc_strdup(elevel, conf->reset_val);
4598                                         if (newval == NULL)
4599                                                 return false;
4600                                         source = conf->gen.reset_source;
4601                                 }
4602                                 else
4603                                 {
4604                                         /* Nothing to reset to, as yet; so do nothing */
4605                                         break;
4606                                 }
4607
4608                                 if (conf->assign_hook)
4609                                 {
4610                                         const char *hookresult;
4611
4612                                         /*
4613                                          * If the hook ereports, we have to make sure we free
4614                                          * newval, else it will be a permanent memory leak.
4615                                          */
4616                                         hookresult = call_string_assign_hook(conf->assign_hook,
4617                                                                                                                  newval,
4618                                                                                                                  changeVal,
4619                                                                                                                  source);
4620                                         if (hookresult == NULL)
4621                                         {
4622                                                 free(newval);
4623                                                 ereport(elevel,
4624                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4625                                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
4626                                                                 name, value ? value : "")));
4627                                                 return false;
4628                                         }
4629                                         else if (hookresult != newval)
4630                                         {
4631                                                 free(newval);
4632
4633                                                 /*
4634                                                  * Having to cast away const here is annoying, but the
4635                                                  * alternative is to declare assign_hooks as returning
4636                                                  * char*, which would mean they'd have to cast away
4637                                                  * const, or as both taking and returning char*, which
4638                                                  * doesn't seem attractive either --- we don't want
4639                                                  * them to scribble on the passed str.
4640                                                  */
4641                                                 newval = (char *) hookresult;
4642                                         }
4643                                 }
4644
4645                                 if (changeVal || makeDefault)
4646                                 {
4647                                         /* Save old value to support transaction abort */
4648                                         if (!makeDefault)
4649                                                 push_old_value(&conf->gen);
4650                                         if (changeVal)
4651                                         {
4652                                                 set_string_field(conf, conf->variable, newval);
4653                                                 conf->gen.source = source;
4654                                         }
4655                                         if (makeDefault)
4656                                         {
4657                                                 GucStack   *stack;
4658
4659                                                 if (conf->gen.reset_source <= source)
4660                                                 {
4661                                                         set_string_field(conf, &conf->reset_val, newval);
4662                                                         conf->gen.reset_source = source;
4663                                                 }
4664                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4665                                                 {
4666                                                         if (stack->source <= source)
4667                                                         {
4668                                                                 set_string_field(conf, &stack->value.stringval,
4669                                                                                                  newval);
4670                                                                 stack->source = source;
4671                                                         }
4672                                                 }
4673                                                 /* Perhaps we didn't install newval anywhere */
4674                                                 if (!string_field_used(conf, newval))
4675                                                         free(newval);
4676                                         }
4677                                         else if (isLocal)
4678                                         {
4679                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4680                                                 guc_dirty = true;
4681                                         }
4682                                         else
4683                                         {
4684                                                 set_string_field(conf, &conf->tentative_val, newval);
4685                                                 conf->gen.tentative_source = source;
4686                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4687                                                 guc_dirty = true;
4688                                         }
4689                                 }
4690                                 else
4691                                         free(newval);
4692                                 break;
4693                         }
4694         }
4695
4696         if (changeVal && (record->flags & GUC_REPORT))
4697                 ReportGUCOption(record);
4698
4699         return true;
4700 }
4701
4702
4703 /*
4704  * Set a config option to the given value. See also set_config_option,
4705  * this is just the wrapper to be called from outside GUC.      NB: this
4706  * is used only for non-transactional operations.
4707  */
4708 void
4709 SetConfigOption(const char *name, const char *value,
4710                                 GucContext context, GucSource source)
4711 {
4712         (void) set_config_option(name, value, context, source, false, true);
4713 }
4714
4715
4716
4717 /*
4718  * Fetch the current value of the option `name'. If the option doesn't exist,
4719  * throw an ereport and don't return.
4720  *
4721  * The string is *not* allocated for modification and is really only
4722  * valid until the next call to configuration related functions.
4723  */
4724 const char *
4725 GetConfigOption(const char *name)
4726 {
4727         struct config_generic *record;
4728         static char buffer[256];
4729
4730         record = find_option(name, ERROR);
4731         if (record == NULL)
4732                 ereport(ERROR,
4733                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4734                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4735         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
4736                 ereport(ERROR,
4737                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4738                                  errmsg("must be superuser to examine \"%s\"", name)));
4739
4740         switch (record->vartype)
4741         {
4742                 case PGC_BOOL:
4743                         return *((struct config_bool *) record)->variable ? "on" : "off";
4744
4745                 case PGC_INT:
4746                         snprintf(buffer, sizeof(buffer), "%d",
4747                                          *((struct config_int *) record)->variable);
4748                         return buffer;
4749
4750                 case PGC_REAL:
4751                         snprintf(buffer, sizeof(buffer), "%g",
4752                                          *((struct config_real *) record)->variable);
4753                         return buffer;
4754
4755                 case PGC_STRING:
4756                         return *((struct config_string *) record)->variable;
4757         }
4758         return NULL;
4759 }
4760
4761 /*
4762  * Get the RESET value associated with the given option.
4763  */
4764 const char *
4765 GetConfigOptionResetString(const char *name)
4766 {
4767         struct config_generic *record;
4768         static char buffer[256];
4769
4770         record = find_option(name, ERROR);
4771         if (record == NULL)
4772                 ereport(ERROR,
4773                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4774                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4775         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
4776                 ereport(ERROR,
4777                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4778                                  errmsg("must be superuser to examine \"%s\"", name)));
4779
4780         switch (record->vartype)
4781         {
4782                 case PGC_BOOL:
4783                         return ((struct config_bool *) record)->reset_val ? "on" : "off";
4784
4785                 case PGC_INT:
4786                         snprintf(buffer, sizeof(buffer), "%d",
4787                                          ((struct config_int *) record)->reset_val);
4788                         return buffer;
4789
4790                 case PGC_REAL:
4791                         snprintf(buffer, sizeof(buffer), "%g",
4792                                          ((struct config_real *) record)->reset_val);
4793                         return buffer;
4794
4795                 case PGC_STRING:
4796                         return ((struct config_string *) record)->reset_val;
4797         }
4798         return NULL;
4799 }
4800
4801 /*
4802  * Detect whether the given configuration option can only be set by
4803  * a superuser.
4804  */
4805 bool
4806 IsSuperuserConfigOption(const char *name)
4807 {
4808         struct config_generic *record;
4809
4810         record = find_option(name, ERROR);
4811         /* On an unrecognized name, don't error, just return false. */
4812         if (record == NULL)
4813                 return false;
4814         return (record->context == PGC_SUSET);
4815 }
4816
4817
4818 /*
4819  * flatten_set_variable_args
4820  *              Given a parsenode List as emitted by the grammar for SET,
4821  *              convert to the flat string representation used by GUC.
4822  *
4823  * We need to be told the name of the variable the args are for, because
4824  * the flattening rules vary (ugh).
4825  *
4826  * The result is NULL if input is NIL (ie, SET ... TO DEFAULT), otherwise
4827  * a palloc'd string.
4828  */
4829 char *
4830 flatten_set_variable_args(const char *name, List *args)
4831 {
4832         struct config_generic *record;
4833         int                     flags;
4834         StringInfoData buf;
4835         ListCell   *l;
4836
4837         /*
4838          * Fast path if just DEFAULT.  We do not check the variable name in this
4839          * case --- necessary for RESET ALL to work correctly.
4840          */
4841         if (args == NIL)
4842                 return NULL;
4843
4844         /* Else get flags for the variable */
4845         record = find_option(name, ERROR);
4846         if (record == NULL)
4847                 ereport(ERROR,
4848                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4849                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4850
4851         flags = record->flags;
4852
4853         /* Complain if list input and non-list variable */
4854         if ((flags & GUC_LIST_INPUT) == 0 &&
4855                 list_length(args) != 1)
4856                 ereport(ERROR,
4857                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4858                                  errmsg("SET %s takes only one argument", name)));
4859
4860         initStringInfo(&buf);
4861
4862         foreach(l, args)
4863         {
4864                 A_Const    *arg = (A_Const *) lfirst(l);
4865                 char       *val;
4866
4867                 if (l != list_head(args))
4868                         appendStringInfo(&buf, ", ");
4869
4870                 if (!IsA(arg, A_Const))
4871                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(arg));
4872
4873                 switch (nodeTag(&arg->val))
4874                 {
4875                         case T_Integer:
4876                                 appendStringInfo(&buf, "%ld", intVal(&arg->val));
4877                                 break;
4878                         case T_Float:
4879                                 /* represented as a string, so just copy it */
4880                                 appendStringInfoString(&buf, strVal(&arg->val));
4881                                 break;
4882                         case T_String:
4883                                 val = strVal(&arg->val);
4884                                 if (arg->typename != NULL)
4885                                 {
4886                                         /*
4887                                          * Must be a ConstInterval argument for TIME ZONE. Coerce
4888                                          * to interval and back to normalize the value and account
4889                                          * for any typmod.
4890                                          */
4891                                         int32           typmod;
4892                                         Datum           interval;
4893                                         char       *intervalout;
4894
4895                                         typmod = typenameTypeMod(NULL, arg->typename, INTERVALOID);
4896
4897                                         interval =
4898                                                 DirectFunctionCall3(interval_in,
4899                                                                                         CStringGetDatum(val),
4900                                                                                         ObjectIdGetDatum(InvalidOid),
4901                                                                                         Int32GetDatum(typmod));
4902
4903                                         intervalout =
4904                                                 DatumGetCString(DirectFunctionCall1(interval_out,
4905                                                                                                                         interval));
4906                                         appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
4907                                 }
4908                                 else
4909                                 {
4910                                         /*
4911                                          * Plain string literal or identifier.  For quote mode,
4912                                          * quote it if it's not a vanilla identifier.
4913                                          */
4914                                         if (flags & GUC_LIST_QUOTE)
4915                                                 appendStringInfoString(&buf, quote_identifier(val));
4916                                         else
4917                                                 appendStringInfoString(&buf, val);
4918                                 }
4919                                 break;
4920                         default:
4921                                 elog(ERROR, "unrecognized node type: %d",
4922                                          (int) nodeTag(&arg->val));
4923                                 break;
4924                 }
4925         }
4926
4927         return buf.data;
4928 }
4929
4930
4931 /*
4932  * SET command
4933  */
4934 void
4935 SetPGVariable(const char *name, List *args, bool is_local)
4936 {
4937         char       *argstring = flatten_set_variable_args(name, args);
4938
4939         /* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
4940         set_config_option(name,
4941                                           argstring,
4942                                           (superuser() ? PGC_SUSET : PGC_USERSET),
4943                                           PGC_S_SESSION,
4944                                           is_local,
4945                                           true);
4946 }
4947
4948 /*
4949  * SET command wrapped as a SQL callable function.
4950  */
4951 Datum
4952 set_config_by_name(PG_FUNCTION_ARGS)
4953 {
4954         char       *name;
4955         char       *value;
4956         char       *new_value;
4957         bool            is_local;
4958         text       *result_text;
4959
4960         if (PG_ARGISNULL(0))
4961                 ereport(ERROR,
4962                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4963                                  errmsg("SET requires parameter name")));
4964
4965         /* Get the GUC variable name */
4966         name = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
4967
4968         /* Get the desired value or set to NULL for a reset request */
4969         if (PG_ARGISNULL(1))
4970                 value = NULL;
4971         else
4972                 value = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(1)));
4973
4974         /*
4975          * Get the desired state of is_local. Default to false if provided value
4976          * is NULL
4977          */
4978         if (PG_ARGISNULL(2))
4979                 is_local = false;
4980         else
4981                 is_local = PG_GETARG_BOOL(2);
4982
4983         /* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
4984         set_config_option(name,
4985                                           value,
4986                                           (superuser() ? PGC_SUSET : PGC_USERSET),
4987                                           PGC_S_SESSION,
4988                                           is_local,
4989                                           true);
4990
4991         /* get the new current value */
4992         new_value = GetConfigOptionByName(name, NULL);
4993
4994         /* Convert return string to text */
4995         result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(new_value)));
4996
4997         /* return it */
4998         PG_RETURN_TEXT_P(result_text);
4999 }
5000
5001 static void
5002 define_custom_variable(struct config_generic * variable)
5003 {
5004         const char *name = variable->name;
5005         const char **nameAddr = &name;
5006         const char *value;
5007         struct config_string *pHolder;
5008         struct config_generic **res = (struct config_generic **) bsearch(
5009                                                                                                                   (void *) &nameAddr,
5010                                                                                                           (void *) guc_variables,
5011                                                                                                                    num_guc_variables,
5012                                                                                          sizeof(struct config_generic *),
5013                                                                                                                         guc_var_compare);
5014
5015         if (res == NULL)
5016         {
5017                 add_guc_variable(variable, ERROR);
5018                 return;
5019         }
5020
5021         /*
5022          * This better be a placeholder
5023          */
5024         if (((*res)->flags & GUC_CUSTOM_PLACEHOLDER) == 0)
5025                 ereport(ERROR,
5026                                 (errcode(ERRCODE_INTERNAL_ERROR),
5027                                  errmsg("attempt to redefine parameter \"%s\"", name)));
5028
5029         Assert((*res)->vartype == PGC_STRING);
5030         pHolder = (struct config_string *) * res;
5031
5032         /* We have the same name, no sorting is necessary */
5033         *res = variable;
5034
5035         value = *pHolder->variable;
5036
5037         /*
5038          * Assign the string value stored in the placeholder to the real variable.
5039          *
5040          * XXX this is not really good enough --- it should be a nontransactional
5041          * assignment, since we don't want it to roll back if the current xact
5042          * fails later.
5043          */
5044         set_config_option(name, value,
5045                                           pHolder->gen.context, pHolder->gen.source,
5046                                           false, true);
5047
5048         /*
5049          * Free up as much as we conveniently can of the placeholder structure
5050          * (this neglects any stack items...)
5051          */
5052         set_string_field(pHolder, pHolder->variable, NULL);
5053         set_string_field(pHolder, &pHolder->reset_val, NULL);
5054         set_string_field(pHolder, &pHolder->tentative_val, NULL);
5055
5056         free(pHolder);
5057 }
5058
5059 static void
5060 init_custom_variable(struct config_generic * gen,
5061                                          const char *name,
5062                                          const char *short_desc,
5063                                          const char *long_desc,
5064                                          GucContext context,
5065                                          enum config_type type)
5066 {
5067         gen->name = guc_strdup(ERROR, name);
5068         gen->context = context;
5069         gen->group = CUSTOM_OPTIONS;
5070         gen->short_desc = short_desc;
5071         gen->long_desc = long_desc;
5072         gen->vartype = type;
5073 }
5074
5075 void
5076 DefineCustomBoolVariable(const char *name,
5077                                                  const char *short_desc,
5078                                                  const char *long_desc,
5079                                                  bool *valueAddr,
5080                                                  GucContext context,
5081                                                  GucBoolAssignHook assign_hook,
5082                                                  GucShowHook show_hook)
5083 {
5084         size_t          sz = sizeof(struct config_bool);
5085         struct config_bool *var = (struct config_bool *) guc_malloc(ERROR, sz);
5086
5087         memset(var, 0, sz);
5088         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_BOOL);
5089
5090         var->variable = valueAddr;
5091         var->reset_val = *valueAddr;
5092         var->assign_hook = assign_hook;
5093         var->show_hook = show_hook;
5094         define_custom_variable(&var->gen);
5095 }
5096
5097 void
5098 DefineCustomIntVariable(const char *name,
5099                                                 const char *short_desc,
5100                                                 const char *long_desc,
5101                                                 int *valueAddr,
5102                                                 int minValue,
5103                                                 int maxValue,
5104                                                 GucContext context,
5105                                                 GucIntAssignHook assign_hook,
5106                                                 GucShowHook show_hook)
5107 {
5108         size_t          sz = sizeof(struct config_int);
5109         struct config_int *var = (struct config_int *) guc_malloc(ERROR, sz);
5110
5111         memset(var, 0, sz);
5112         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_INT);
5113
5114         var->variable = valueAddr;
5115         var->reset_val = *valueAddr;
5116         var->min = minValue;
5117         var->max = maxValue;
5118         var->assign_hook = assign_hook;
5119         var->show_hook = show_hook;
5120         define_custom_variable(&var->gen);
5121 }
5122
5123 void
5124 DefineCustomRealVariable(const char *name,
5125                                                  const char *short_desc,
5126                                                  const char *long_desc,
5127                                                  double *valueAddr,
5128                                                  double minValue,
5129                                                  double maxValue,
5130                                                  GucContext context,
5131                                                  GucRealAssignHook assign_hook,
5132                                                  GucShowHook show_hook)
5133 {
5134         size_t          sz = sizeof(struct config_real);
5135         struct config_real *var = (struct config_real *) guc_malloc(ERROR, sz);
5136
5137         memset(var, 0, sz);
5138         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_REAL);
5139
5140         var->variable = valueAddr;
5141         var->reset_val = *valueAddr;
5142         var->min = minValue;
5143         var->max = maxValue;
5144         var->assign_hook = assign_hook;
5145         var->show_hook = show_hook;
5146         define_custom_variable(&var->gen);
5147 }
5148
5149 void
5150 DefineCustomStringVariable(const char *name,
5151                                                    const char *short_desc,
5152                                                    const char *long_desc,
5153                                                    char **valueAddr,
5154                                                    GucContext context,
5155                                                    GucStringAssignHook assign_hook,
5156                                                    GucShowHook show_hook)
5157 {
5158         size_t          sz = sizeof(struct config_string);
5159         struct config_string *var = (struct config_string *) guc_malloc(ERROR, sz);
5160
5161         memset(var, 0, sz);
5162         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_STRING);
5163
5164         var->variable = valueAddr;
5165         var->reset_val = *valueAddr;
5166         var->assign_hook = assign_hook;
5167         var->show_hook = show_hook;
5168         define_custom_variable(&var->gen);
5169 }
5170
5171 void
5172 EmitWarningsOnPlaceholders(const char *className)
5173 {
5174         struct config_generic **vars = guc_variables;
5175         struct config_generic **last = vars + num_guc_variables;
5176
5177         int                     nameLen = strlen(className);
5178
5179         while (vars < last)
5180         {
5181                 struct config_generic *var = *vars++;
5182
5183                 if ((var->flags & GUC_CUSTOM_PLACEHOLDER) != 0 &&
5184                         strncmp(className, var->name, nameLen) == 0 &&
5185                         var->name[nameLen] == GUC_QUALIFIER_SEPARATOR)
5186                 {
5187                         ereport(INFO,
5188                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
5189                                          errmsg("unrecognized configuration parameter \"%s\"", var->name)));
5190                 }
5191         }
5192 }
5193
5194
5195 /*
5196  * SHOW command
5197  */
5198 void
5199 GetPGVariable(const char *name, DestReceiver *dest)
5200 {
5201         if (pg_strcasecmp(name, "all") == 0)
5202                 ShowAllGUCConfig(dest);
5203         else
5204                 ShowGUCConfigOption(name, dest);
5205 }
5206
5207 TupleDesc
5208 GetPGVariableResultDesc(const char *name)
5209 {
5210         TupleDesc       tupdesc;
5211
5212         if (pg_strcasecmp(name, "all") == 0)
5213         {
5214                 /* need a tuple descriptor representing three TEXT columns */
5215                 tupdesc = CreateTemplateTupleDesc(3, false);
5216                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
5217                                                    TEXTOID, -1, 0);
5218                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
5219                                                    TEXTOID, -1, 0);
5220                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
5221                                                    TEXTOID, -1, 0);
5222
5223         }
5224         else
5225         {
5226                 const char *varname;
5227
5228                 /* Get the canonical spelling of name */
5229                 (void) GetConfigOptionByName(name, &varname);
5230
5231                 /* need a tuple descriptor representing a single TEXT column */
5232                 tupdesc = CreateTemplateTupleDesc(1, false);
5233                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
5234                                                    TEXTOID, -1, 0);
5235         }
5236         return tupdesc;
5237 }
5238
5239 /*
5240  * RESET command
5241  */
5242 void
5243 ResetPGVariable(const char *name, bool isTopLevel)
5244 {
5245         if (pg_strcasecmp(name, "all") == 0)
5246                 ResetAllOptions();
5247         else
5248                 set_config_option(name,
5249                                                   NULL,
5250                                                   (superuser() ? PGC_SUSET : PGC_USERSET),
5251                                                   PGC_S_SESSION,
5252                                                   false,
5253                                                   true);
5254 }
5255
5256
5257 /*
5258  * SHOW command
5259  */
5260 static void
5261 ShowGUCConfigOption(const char *name, DestReceiver *dest)
5262 {
5263         TupOutputState *tstate;
5264         TupleDesc       tupdesc;
5265         const char *varname;
5266         char       *value;
5267
5268         /* Get the value and canonical spelling of name */
5269         value = GetConfigOptionByName(name, &varname);
5270
5271         /* need a tuple descriptor representing a single TEXT column */
5272         tupdesc = CreateTemplateTupleDesc(1, false);
5273         TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
5274                                            TEXTOID, -1, 0);
5275
5276         /* prepare for projection of tuples */
5277         tstate = begin_tup_output_tupdesc(dest, tupdesc);
5278
5279         /* Send it */
5280         do_text_output_oneline(tstate, value);
5281
5282         end_tup_output(tstate);
5283 }
5284
5285 /*
5286  * SHOW ALL command
5287  */
5288 static void
5289 ShowAllGUCConfig(DestReceiver *dest)
5290 {
5291         bool            am_superuser = superuser();
5292         int                     i;
5293         TupOutputState *tstate;
5294         TupleDesc       tupdesc;
5295         char       *values[3];
5296
5297         /* need a tuple descriptor representing three TEXT columns */
5298         tupdesc = CreateTemplateTupleDesc(3, false);
5299         TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
5300                                            TEXTOID, -1, 0);
5301         TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
5302                                            TEXTOID, -1, 0);
5303         TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
5304                                            TEXTOID, -1, 0);
5305
5306
5307         /* prepare for projection of tuples */
5308         tstate = begin_tup_output_tupdesc(dest, tupdesc);
5309
5310         for (i = 0; i < num_guc_variables; i++)
5311         {
5312                 struct config_generic *conf = guc_variables[i];
5313
5314                 if ((conf->flags & GUC_NO_SHOW_ALL) ||
5315                         ((conf->flags & GUC_SUPERUSER_ONLY) && !am_superuser))
5316                         continue;
5317
5318                 /* assign to the values array */
5319                 values[0] = (char *) conf->name;
5320                 values[1] = _ShowOption(conf, true);
5321                 values[2] = (char *) conf->short_desc;
5322
5323                 /* send it to dest */
5324                 do_tup_output(tstate, values);
5325
5326                 /* clean up */
5327                 if (values[1] != NULL)
5328                         pfree(values[1]);
5329         }
5330
5331         end_tup_output(tstate);
5332 }
5333
5334 /*
5335  * Return GUC variable value by name; optionally return canonical
5336  * form of name.  Return value is palloc'd.
5337  */
5338 char *
5339 GetConfigOptionByName(const char *name, const char **varname)
5340 {
5341         struct config_generic *record;
5342
5343         record = find_option(name, ERROR);
5344         if (record == NULL)
5345                 ereport(ERROR,
5346                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
5347                            errmsg("unrecognized configuration parameter \"%s\"", name)));
5348         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
5349                 ereport(ERROR,
5350                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5351                                  errmsg("must be superuser to examine \"%s\"", name)));
5352
5353         if (varname)
5354                 *varname = record->name;
5355
5356         return _ShowOption(record, true);
5357 }
5358
5359 /*
5360  * Return GUC variable value by variable number; optionally return canonical
5361  * form of name.  Return value is palloc'd.
5362  */
5363 void
5364 GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
5365 {
5366         char            buffer[256];
5367         struct config_generic *conf;
5368
5369         /* check requested variable number valid */
5370         Assert((varnum >= 0) && (varnum < num_guc_variables));
5371
5372         conf = guc_variables[varnum];
5373
5374         if (noshow)
5375         {
5376                 if ((conf->flags & GUC_NO_SHOW_ALL) ||
5377                         ((conf->flags & GUC_SUPERUSER_ONLY) && !superuser()))
5378                         *noshow = true;
5379                 else
5380                         *noshow = false;
5381         }
5382
5383         /* first get the generic attributes */
5384
5385         /* name */
5386         values[0] = conf->name;
5387
5388         /* setting : use _ShowOption in order to avoid duplicating the logic */
5389         values[1] = _ShowOption(conf, false);
5390
5391         /* unit */
5392         if (conf->vartype == PGC_INT)
5393         {
5394                 static char buf[8];
5395
5396                 switch (conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
5397                 {
5398                         case GUC_UNIT_KB:
5399                                 values[2] = "kB";
5400                                 break;
5401                         case GUC_UNIT_BLOCKS:
5402                                 snprintf(buf, sizeof(buf), "%dkB", BLCKSZ / 1024);
5403                                 values[2] = buf;
5404                                 break;
5405                         case GUC_UNIT_XBLOCKS:
5406                                 snprintf(buf, sizeof(buf), "%dkB", XLOG_BLCKSZ / 1024);
5407                                 values[2] = buf;
5408                                 break;
5409                         case GUC_UNIT_MS:
5410                                 values[2] = "ms";
5411                                 break;
5412                         case GUC_UNIT_S:
5413                                 values[2] = "s";
5414                                 break;
5415                         case GUC_UNIT_MIN:
5416                                 values[2] = "min";
5417                                 break;
5418                         default:
5419                                 values[2] = "";
5420                                 break;
5421                 }
5422         }
5423         else
5424                 values[2] = NULL;
5425
5426         /* group */
5427         values[3] = config_group_names[conf->group];
5428
5429         /* short_desc */
5430         values[4] = conf->short_desc;
5431
5432         /* extra_desc */
5433         values[5] = conf->long_desc;
5434
5435         /* context */
5436         values[6] = GucContext_Names[conf->context];
5437
5438         /* vartype */
5439         values[7] = config_type_names[conf->vartype];
5440
5441         /* source */
5442         values[8] = GucSource_Names[conf->source];
5443
5444         /* now get the type specifc attributes */
5445         switch (conf->vartype)
5446         {
5447                 case PGC_BOOL:
5448                         {
5449                                 /* min_val */
5450                                 values[9] = NULL;
5451
5452                                 /* max_val */
5453                                 values[10] = NULL;
5454                         }
5455                         break;
5456
5457                 case PGC_INT:
5458                         {
5459                                 struct config_int *lconf = (struct config_int *) conf;
5460
5461                                 /* min_val */
5462                                 snprintf(buffer, sizeof(buffer), "%d", lconf->min);
5463                                 values[9] = pstrdup(buffer);
5464
5465                                 /* max_val */
5466                                 snprintf(buffer, sizeof(buffer), "%d", lconf->max);
5467                                 values[10] = pstrdup(buffer);
5468                         }
5469                         break;
5470
5471                 case PGC_REAL:
5472                         {
5473                                 struct config_real *lconf = (struct config_real *) conf;
5474
5475                                 /* min_val */
5476                                 snprintf(buffer, sizeof(buffer), "%g", lconf->min);
5477                                 values[9] = pstrdup(buffer);
5478
5479                                 /* max_val */
5480                                 snprintf(buffer, sizeof(buffer), "%g", lconf->max);
5481                                 values[10] = pstrdup(buffer);
5482                         }
5483                         break;
5484
5485                 case PGC_STRING:
5486                         {
5487                                 /* min_val */
5488                                 values[9] = NULL;
5489
5490                                 /* max_val */
5491                                 values[10] = NULL;
5492                         }
5493                         break;
5494
5495                 default:
5496                         {
5497                                 /*
5498                                  * should never get here, but in case we do, set 'em to NULL
5499                                  */
5500
5501                                 /* min_val */
5502                                 values[9] = NULL;
5503
5504                                 /* max_val */
5505                                 values[10] = NULL;
5506                         }
5507                         break;
5508         }
5509 }
5510
5511 /*
5512  * Return the total number of GUC variables
5513  */
5514 int
5515 GetNumConfigOptions(void)
5516 {
5517         return num_guc_variables;
5518 }
5519
5520 /*
5521  * show_config_by_name - equiv to SHOW X command but implemented as
5522  * a function.
5523  */
5524 Datum
5525 show_config_by_name(PG_FUNCTION_ARGS)
5526 {
5527         char       *varname;
5528         char       *varval;
5529         text       *result_text;
5530
5531         /* Get the GUC variable name */
5532         varname = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
5533
5534         /* Get the value */
5535         varval = GetConfigOptionByName(varname, NULL);
5536
5537         /* Convert to text */
5538         result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(varval)));
5539
5540         /* return it */
5541         PG_RETURN_TEXT_P(result_text);
5542 }
5543
5544 /*
5545  * show_all_settings - equiv to SHOW ALL command but implemented as
5546  * a Table Function.
5547  */
5548 #define NUM_PG_SETTINGS_ATTS    11
5549
5550 Datum
5551 show_all_settings(PG_FUNCTION_ARGS)
5552 {
5553         FuncCallContext *funcctx;
5554         TupleDesc       tupdesc;
5555         int                     call_cntr;
5556         int                     max_calls;
5557         AttInMetadata *attinmeta;
5558         MemoryContext oldcontext;
5559
5560         /* stuff done only on the first call of the function */
5561         if (SRF_IS_FIRSTCALL())
5562         {
5563                 /* create a function context for cross-call persistence */
5564                 funcctx = SRF_FIRSTCALL_INIT();
5565
5566                 /*
5567                  * switch to memory context appropriate for multiple function calls
5568                  */
5569                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5570
5571                 /*
5572                  * need a tuple descriptor representing NUM_PG_SETTINGS_ATTS columns
5573                  * of the appropriate types
5574                  */
5575                 tupdesc = CreateTemplateTupleDesc(NUM_PG_SETTINGS_ATTS, false);
5576                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
5577                                                    TEXTOID, -1, 0);
5578                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
5579                                                    TEXTOID, -1, 0);
5580                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "unit",
5581                                                    TEXTOID, -1, 0);
5582                 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "category",
5583                                                    TEXTOID, -1, 0);
5584                 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "short_desc",
5585                                                    TEXTOID, -1, 0);
5586                 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "extra_desc",
5587                                                    TEXTOID, -1, 0);
5588                 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "context",
5589                                                    TEXTOID, -1, 0);
5590                 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "vartype",
5591                                                    TEXTOID, -1, 0);
5592                 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "source",
5593                                                    TEXTOID, -1, 0);
5594                 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "min_val",
5595                                                    TEXTOID, -1, 0);
5596                 TupleDescInitEntry(tupdesc, (AttrNumber) 11, "max_val",
5597                                                    TEXTOID, -1, 0);
5598
5599                 /*
5600                  * Generate attribute metadata needed later to produce tuples from raw
5601                  * C strings
5602                  */
5603                 attinmeta = TupleDescGetAttInMetadata(tupdesc);
5604                 funcctx->attinmeta = attinmeta;
5605
5606                 /* total number of tuples to be returned */
5607                 funcctx->max_calls = GetNumConfigOptions();
5608
5609                 MemoryContextSwitchTo(oldcontext);
5610         }
5611
5612         /* stuff done on every call of the function */
5613         funcctx = SRF_PERCALL_SETUP();
5614
5615         call_cntr = funcctx->call_cntr;
5616         max_calls = funcctx->max_calls;
5617         attinmeta = funcctx->attinmeta;
5618
5619         if (call_cntr < max_calls)      /* do when there is more left to send */
5620         {
5621                 char       *values[NUM_PG_SETTINGS_ATTS];
5622                 bool            noshow;
5623                 HeapTuple       tuple;
5624                 Datum           result;
5625
5626                 /*
5627                  * Get the next visible GUC variable name and value
5628                  */
5629                 do
5630                 {
5631                         GetConfigOptionByNum(call_cntr, (const char **) values, &noshow);
5632                         if (noshow)
5633                         {
5634                                 /* bump the counter and get the next config setting */
5635                                 call_cntr = ++funcctx->call_cntr;
5636
5637                                 /* make sure we haven't gone too far now */
5638                                 if (call_cntr >= max_calls)
5639                                         SRF_RETURN_DONE(funcctx);
5640                         }
5641                 } while (noshow);
5642
5643                 /* build a tuple */
5644                 tuple = BuildTupleFromCStrings(attinmeta, values);
5645
5646                 /* make the tuple into a datum */
5647                 result = HeapTupleGetDatum(tuple);
5648
5649                 SRF_RETURN_NEXT(funcctx, result);
5650         }
5651         else
5652         {
5653                 /* do when there is no more left */
5654                 SRF_RETURN_DONE(funcctx);
5655         }
5656 }
5657
5658 static char *
5659 _ShowOption(struct config_generic * record, bool use_units)
5660 {
5661         char            buffer[256];
5662         const char *val;
5663
5664         switch (record->vartype)
5665         {
5666                 case PGC_BOOL:
5667                         {
5668                                 struct config_bool *conf = (struct config_bool *) record;
5669
5670                                 if (conf->show_hook)
5671                                         val = (*conf->show_hook) ();
5672                                 else
5673                                         val = *conf->variable ? "on" : "off";
5674                         }
5675                         break;
5676
5677                 case PGC_INT:
5678                         {
5679                                 struct config_int *conf = (struct config_int *) record;
5680
5681                                 if (conf->show_hook)
5682                                         val = (*conf->show_hook) ();
5683                                 else
5684                                 {
5685                                         char            unit[4];
5686                                         int                     result = *conf->variable;
5687
5688                                         if (use_units && result > 0 && (record->flags & GUC_UNIT_MEMORY))
5689                                         {
5690                                                 switch (record->flags & GUC_UNIT_MEMORY)
5691                                                 {
5692                                                         case GUC_UNIT_BLOCKS:
5693                                                                 result *= BLCKSZ / 1024;
5694                                                                 break;
5695                                                         case GUC_UNIT_XBLOCKS:
5696                                                                 result *= XLOG_BLCKSZ / 1024;
5697                                                                 break;
5698                                                 }
5699
5700                                                 if (result % KB_PER_GB == 0)
5701                                                 {
5702                                                         result /= KB_PER_GB;
5703                                                         strcpy(unit, "GB");
5704                                                 }
5705                                                 else if (result % KB_PER_MB == 0)
5706                                                 {
5707                                                         result /= KB_PER_MB;
5708                                                         strcpy(unit, "MB");
5709                                                 }
5710                                                 else
5711                                                 {
5712                                                         strcpy(unit, "kB");
5713                                                 }
5714                                         }
5715                                         else if (use_units && result > 0 && (record->flags & GUC_UNIT_TIME))
5716                                         {
5717                                                 switch (record->flags & GUC_UNIT_TIME)
5718                                                 {
5719                                                         case GUC_UNIT_S:
5720                                                                 result *= MS_PER_S;
5721                                                                 break;
5722                                                         case GUC_UNIT_MIN:
5723                                                                 result *= MS_PER_MIN;
5724                                                                 break;
5725                                                 }
5726
5727                                                 if (result % MS_PER_D == 0)
5728                                                 {
5729                                                         result /= MS_PER_D;
5730                                                         strcpy(unit, "d");
5731                                                 }
5732                                                 else if (result % MS_PER_H == 0)
5733                                                 {
5734                                                         result /= MS_PER_H;
5735                                                         strcpy(unit, "h");
5736                                                 }
5737                                                 else if (result % MS_PER_MIN == 0)
5738                                                 {
5739                                                         result /= MS_PER_MIN;
5740                                                         strcpy(unit, "min");
5741                                                 }
5742                                                 else if (result % MS_PER_S == 0)
5743                                                 {
5744                                                         result /= MS_PER_S;
5745                                                         strcpy(unit, "s");
5746                                                 }
5747                                                 else
5748                                                 {
5749                                                         strcpy(unit, "ms");
5750                                                 }
5751                                         }
5752                                         else
5753                                                 strcpy(unit, "");
5754
5755                                         snprintf(buffer, sizeof(buffer), "%d%s",
5756                                                          (int) result, unit);
5757                                         val = buffer;
5758                                 }
5759                         }
5760                         break;
5761
5762                 case PGC_REAL:
5763                         {
5764                                 struct config_real *conf = (struct config_real *) record;
5765
5766                                 if (conf->show_hook)
5767                                         val = (*conf->show_hook) ();
5768                                 else
5769                                 {
5770                                         snprintf(buffer, sizeof(buffer), "%g",
5771                                                          *conf->variable);
5772                                         val = buffer;
5773                                 }
5774                         }
5775                         break;
5776
5777                 case PGC_STRING:
5778                         {
5779                                 struct config_string *conf = (struct config_string *) record;
5780
5781                                 if (conf->show_hook)
5782                                         val = (*conf->show_hook) ();
5783                                 else if (*conf->variable && **conf->variable)
5784                                         val = *conf->variable;
5785                                 else
5786                                         val = "";
5787                         }
5788                         break;
5789
5790                 default:
5791                         /* just to keep compiler quiet */
5792                         val = "???";
5793                         break;
5794         }
5795
5796         return pstrdup(val);
5797 }
5798
5799
5800 static bool
5801 is_newvalue_equal(struct config_generic * record, const char *newvalue)
5802 {
5803         switch (record->vartype)
5804         {
5805                 case PGC_BOOL:
5806                         {
5807                                 struct config_bool *conf = (struct config_bool *) record;
5808                                 bool            newval;
5809
5810                                 return parse_bool(newvalue, &newval)
5811                                         && *conf->variable == newval;
5812                         }
5813                 case PGC_INT:
5814                         {
5815                                 struct config_int *conf = (struct config_int *) record;
5816                                 int                     newval;
5817
5818                                 return parse_int(newvalue, &newval, record->flags, NULL)
5819                                         && *conf->variable == newval;
5820                         }
5821                 case PGC_REAL:
5822                         {
5823                                 struct config_real *conf = (struct config_real *) record;
5824                                 double          newval;
5825
5826                                 return parse_real(newvalue, &newval)
5827                                         && *conf->variable == newval;
5828                         }
5829                 case PGC_STRING:
5830                         {
5831                                 struct config_string *conf = (struct config_string *) record;
5832
5833                                 return strcmp(*conf->variable, newvalue) == 0;
5834                         }
5835         }
5836
5837         return false;
5838 }
5839
5840
5841 #ifdef EXEC_BACKEND
5842
5843 /*
5844  *      This routine dumps out all non-default GUC options into a binary
5845  *      file that is read by all exec'ed backends.  The format is:
5846  *
5847  *              variable name, string, null terminated
5848  *              variable value, string, null terminated
5849  *              variable source, integer
5850  */
5851 void
5852 write_nondefault_variables(GucContext context)
5853 {
5854         int                     i;
5855         int                     elevel;
5856         FILE       *fp;
5857
5858         Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
5859
5860         elevel = (context == PGC_SIGHUP) ? LOG : ERROR;
5861
5862         /*
5863          * Open file
5864          */
5865         fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, "w");
5866         if (!fp)
5867         {
5868                 ereport(elevel,
5869                                 (errcode_for_file_access(),
5870                                  errmsg("could not write to file \"%s\": %m",
5871                                                 CONFIG_EXEC_PARAMS_NEW)));
5872                 return;
5873         }
5874
5875         for (i = 0; i < num_guc_variables; i++)
5876         {
5877                 struct config_generic *gconf = guc_variables[i];
5878
5879                 if (gconf->source != PGC_S_DEFAULT)
5880                 {
5881                         fprintf(fp, "%s", gconf->name);
5882                         fputc(0, fp);
5883
5884                         switch (gconf->vartype)
5885                         {
5886                                 case PGC_BOOL:
5887                                         {
5888                                                 struct config_bool *conf = (struct config_bool *) gconf;
5889
5890                                                 if (*conf->variable == 0)
5891                                                         fprintf(fp, "false");
5892                                                 else
5893                                                         fprintf(fp, "true");
5894                                         }
5895                                         break;
5896
5897                                 case PGC_INT:
5898                                         {
5899                                                 struct config_int *conf = (struct config_int *) gconf;
5900
5901                                                 fprintf(fp, "%d", *conf->variable);
5902                                         }
5903                                         break;
5904
5905                                 case PGC_REAL:
5906                                         {
5907                                                 struct config_real *conf = (struct config_real *) gconf;
5908
5909                                                 /* Could lose precision here? */
5910                                                 fprintf(fp, "%f", *conf->variable);
5911                                         }
5912                                         break;
5913
5914                                 case PGC_STRING:
5915                                         {
5916                                                 struct config_string *conf = (struct config_string *) gconf;
5917
5918                                                 fprintf(fp, "%s", *conf->variable);
5919                                         }
5920                                         break;
5921                         }
5922
5923                         fputc(0, fp);
5924
5925                         fwrite(&gconf->source, sizeof(gconf->source), 1, fp);
5926                 }
5927         }
5928
5929         if (FreeFile(fp))
5930         {
5931                 ereport(elevel,
5932                                 (errcode_for_file_access(),
5933                                  errmsg("could not write to file \"%s\": %m",
5934                                                 CONFIG_EXEC_PARAMS_NEW)));
5935                 return;
5936         }
5937
5938         /*
5939          * Put new file in place.  This could delay on Win32, but we don't hold
5940          * any exclusive locks.
5941          */
5942         rename(CONFIG_EXEC_PARAMS_NEW, CONFIG_EXEC_PARAMS);
5943 }
5944
5945
5946 /*
5947  *      Read string, including null byte from file
5948  *
5949  *      Return NULL on EOF and nothing read
5950  */
5951 static char *
5952 read_string_with_null(FILE *fp)
5953 {
5954         int                     i = 0,
5955                                 ch,
5956                                 maxlen = 256;
5957         char       *str = NULL;
5958
5959         do
5960         {
5961                 if ((ch = fgetc(fp)) == EOF)
5962                 {
5963                         if (i == 0)
5964                                 return NULL;
5965                         else
5966                                 elog(FATAL, "invalid format of exec config params file");
5967                 }
5968                 if (i == 0)
5969                         str = guc_malloc(FATAL, maxlen);
5970                 else if (i == maxlen)
5971                         str = guc_realloc(FATAL, str, maxlen *= 2);
5972                 str[i++] = ch;
5973         } while (ch != 0);
5974
5975         return str;
5976 }
5977
5978
5979 /*
5980  *      This routine loads a previous postmaster dump of its non-default
5981  *      settings.
5982  */
5983 void
5984 read_nondefault_variables(void)
5985 {
5986         FILE       *fp;
5987         char       *varname,
5988                            *varvalue;
5989         int                     varsource;
5990
5991         /*
5992          * Open file
5993          */
5994         fp = AllocateFile(CONFIG_EXEC_PARAMS, "r");
5995         if (!fp)
5996         {
5997                 /* File not found is fine */
5998                 if (errno != ENOENT)
5999                         ereport(FATAL,
6000                                         (errcode_for_file_access(),
6001                                          errmsg("could not read from file \"%s\": %m",
6002                                                         CONFIG_EXEC_PARAMS)));
6003                 return;
6004         }
6005
6006         for (;;)
6007         {
6008                 struct config_generic *record;
6009
6010                 if ((varname = read_string_with_null(fp)) == NULL)
6011                         break;
6012
6013                 if ((record = find_option(varname, FATAL)) == NULL)
6014                         elog(FATAL, "failed to locate variable %s in exec config params file", varname);
6015                 if ((varvalue = read_string_with_null(fp)) == NULL)
6016                         elog(FATAL, "invalid format of exec config params file");
6017                 if (fread(&varsource, sizeof(varsource), 1, fp) == 0)
6018                         elog(FATAL, "invalid format of exec config params file");
6019
6020                 (void) set_config_option(varname, varvalue, record->context,
6021                                                                  varsource, false, true);
6022                 free(varname);
6023                 free(varvalue);
6024         }
6025
6026         FreeFile(fp);
6027 }
6028 #endif   /* EXEC_BACKEND */
6029
6030
6031 /*
6032  * A little "long argument" simulation, although not quite GNU
6033  * compliant. Takes a string of the form "some-option=some value" and
6034  * returns name = "some_option" and value = "some value" in malloc'ed
6035  * storage. Note that '-' is converted to '_' in the option name. If
6036  * there is no '=' in the input string then value will be NULL.
6037  */
6038 void
6039 ParseLongOption(const char *string, char **name, char **value)
6040 {
6041         size_t          equal_pos;
6042         char       *cp;
6043
6044         AssertArg(string);
6045         AssertArg(name);
6046         AssertArg(value);
6047
6048         equal_pos = strcspn(string, "=");
6049
6050         if (string[equal_pos] == '=')
6051         {
6052                 *name = guc_malloc(FATAL, equal_pos + 1);
6053                 strlcpy(*name, string, equal_pos + 1);
6054
6055                 *value = guc_strdup(FATAL, &string[equal_pos + 1]);
6056         }
6057         else
6058         {
6059                 /* no equal sign in string */
6060                 *name = guc_strdup(FATAL, string);
6061                 *value = NULL;
6062         }
6063
6064         for (cp = *name; *cp; cp++)
6065                 if (*cp == '-')
6066                         *cp = '_';
6067 }
6068
6069
6070 /*
6071  * Handle options fetched from pg_database.datconfig or pg_authid.rolconfig.
6072  * The array parameter must be an array of TEXT (it must not be NULL).
6073  */
6074 void
6075 ProcessGUCArray(ArrayType *array, GucSource source)
6076 {
6077         int                     i;
6078
6079         Assert(array != NULL);
6080         Assert(ARR_ELEMTYPE(array) == TEXTOID);
6081         Assert(ARR_NDIM(array) == 1);
6082         Assert(ARR_LBOUND(array)[0] == 1);
6083         Assert(source == PGC_S_DATABASE || source == PGC_S_USER);
6084
6085         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6086         {
6087                 Datum           d;
6088                 bool            isnull;
6089                 char       *s;
6090                 char       *name;
6091                 char       *value;
6092
6093                 d = array_ref(array, 1, &i,
6094                                           -1 /* varlenarray */ ,
6095                                           -1 /* TEXT's typlen */ ,
6096                                           false /* TEXT's typbyval */ ,
6097                                           'i' /* TEXT's typalign */ ,
6098                                           &isnull);
6099
6100                 if (isnull)
6101                         continue;
6102
6103                 s = DatumGetCString(DirectFunctionCall1(textout, d));
6104
6105                 ParseLongOption(s, &name, &value);
6106                 if (!value)
6107                 {
6108                         ereport(WARNING,
6109                                         (errcode(ERRCODE_SYNTAX_ERROR),
6110                           errmsg("could not parse setting for parameter \"%s\"", name)));
6111                         free(name);
6112                         continue;
6113                 }
6114
6115                 /*
6116                  * We process all these options at SUSET level.  We assume that the
6117                  * right to insert an option into pg_database or pg_authid was checked
6118                  * when it was inserted.
6119                  */
6120                 SetConfigOption(name, value, PGC_SUSET, source);
6121
6122                 free(name);
6123                 if (value)
6124                         free(value);
6125         }
6126 }
6127
6128
6129 /*
6130  * Add an entry to an option array.  The array parameter may be NULL
6131  * to indicate the current table entry is NULL.
6132  */
6133 ArrayType *
6134 GUCArrayAdd(ArrayType *array, const char *name, const char *value)
6135 {
6136         const char *varname;
6137         Datum           datum;
6138         char       *newval;
6139         ArrayType  *a;
6140
6141         Assert(name);
6142         Assert(value);
6143
6144         /* test if the option is valid */
6145         set_config_option(name, value,
6146                                           superuser() ? PGC_SUSET : PGC_USERSET,
6147                                           PGC_S_TEST, false, false);
6148
6149         /* convert name to canonical spelling, so we can use plain strcmp */
6150         (void) GetConfigOptionByName(name, &varname);
6151         name = varname;
6152
6153         newval = palloc(strlen(name) + 1 + strlen(value) + 1);
6154         sprintf(newval, "%s=%s", name, value);
6155         datum = DirectFunctionCall1(textin, CStringGetDatum(newval));
6156
6157         if (array)
6158         {
6159                 int                     index;
6160                 bool            isnull;
6161                 int                     i;
6162
6163                 Assert(ARR_ELEMTYPE(array) == TEXTOID);
6164                 Assert(ARR_NDIM(array) == 1);
6165                 Assert(ARR_LBOUND(array)[0] == 1);
6166
6167                 index = ARR_DIMS(array)[0] + 1; /* add after end */
6168
6169                 for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6170                 {
6171                         Datum           d;
6172                         char       *current;
6173
6174                         d = array_ref(array, 1, &i,
6175                                                   -1 /* varlenarray */ ,
6176                                                   -1 /* TEXT's typlen */ ,
6177                                                   false /* TEXT's typbyval */ ,
6178                                                   'i' /* TEXT's typalign */ ,
6179                                                   &isnull);
6180                         if (isnull)
6181                                 continue;
6182                         current = DatumGetCString(DirectFunctionCall1(textout, d));
6183                         if (strncmp(current, newval, strlen(name) + 1) == 0)
6184                         {
6185                                 index = i;
6186                                 break;
6187                         }
6188                 }
6189
6190                 a = array_set(array, 1, &index,
6191                                           datum,
6192                                           false,
6193                                           -1 /* varlena array */ ,
6194                                           -1 /* TEXT's typlen */ ,
6195                                           false /* TEXT's typbyval */ ,
6196                                           'i' /* TEXT's typalign */ );
6197         }
6198         else
6199                 a = construct_array(&datum, 1,
6200                                                         TEXTOID,
6201                                                         -1, false, 'i');
6202
6203         return a;
6204 }
6205
6206
6207 /*
6208  * Delete an entry from an option array.  The array parameter may be NULL
6209  * to indicate the current table entry is NULL.  Also, if the return value
6210  * is NULL then a null should be stored.
6211  */
6212 ArrayType *
6213 GUCArrayDelete(ArrayType *array, const char *name)
6214 {
6215         const char *varname;
6216         ArrayType  *newarray;
6217         int                     i;
6218         int                     index;
6219
6220         Assert(name);
6221
6222         /* test if the option is valid */
6223         set_config_option(name, NULL,
6224                                           superuser() ? PGC_SUSET : PGC_USERSET,
6225                                           PGC_S_TEST, false, false);
6226
6227         /* convert name to canonical spelling, so we can use plain strcmp */
6228         (void) GetConfigOptionByName(name, &varname);
6229         name = varname;
6230
6231         /* if array is currently null, then surely nothing to delete */
6232         if (!array)
6233                 return NULL;
6234
6235         newarray = NULL;
6236         index = 1;
6237
6238         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6239         {
6240                 Datum           d;
6241                 char       *val;
6242                 bool            isnull;
6243
6244                 d = array_ref(array, 1, &i,
6245                                           -1 /* varlenarray */ ,
6246                                           -1 /* TEXT's typlen */ ,
6247                                           false /* TEXT's typbyval */ ,
6248                                           'i' /* TEXT's typalign */ ,
6249                                           &isnull);
6250                 if (isnull)
6251                         continue;
6252                 val = DatumGetCString(DirectFunctionCall1(textout, d));
6253
6254                 /* ignore entry if it's what we want to delete */
6255                 if (strncmp(val, name, strlen(name)) == 0
6256                         && val[strlen(name)] == '=')
6257                         continue;
6258
6259                 /* else add it to the output array */
6260                 if (newarray)
6261                 {
6262                         newarray = array_set(newarray, 1, &index,
6263                                                                  d,
6264                                                                  false,
6265                                                                  -1 /* varlenarray */ ,
6266                                                                  -1 /* TEXT's typlen */ ,
6267                                                                  false /* TEXT's typbyval */ ,
6268                                                                  'i' /* TEXT's typalign */ );
6269                 }
6270                 else
6271                         newarray = construct_array(&d, 1,
6272                                                                            TEXTOID,
6273                                                                            -1, false, 'i');
6274
6275                 index++;
6276         }
6277
6278         return newarray;
6279 }
6280
6281
6282 /*
6283  * assign_hook subroutines
6284  */
6285
6286 static const char *
6287 assign_log_destination(const char *value, bool doit, GucSource source)
6288 {
6289         char       *rawstring;
6290         List       *elemlist;
6291         ListCell   *l;
6292         int                     newlogdest = 0;
6293
6294         /* Need a modifiable copy of string */
6295         rawstring = pstrdup(value);
6296
6297         /* Parse string into list of identifiers */
6298         if (!SplitIdentifierString(rawstring, ',', &elemlist))
6299         {
6300                 /* syntax error in list */
6301                 pfree(rawstring);
6302                 list_free(elemlist);
6303                 if (source >= PGC_S_INTERACTIVE)
6304                         ereport(ERROR,
6305                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6306                         errmsg("invalid list syntax for parameter \"log_destination\"")));
6307                 return NULL;
6308         }
6309
6310         foreach(l, elemlist)
6311         {
6312                 char       *tok = (char *) lfirst(l);
6313
6314                 if (pg_strcasecmp(tok, "stderr") == 0)
6315                         newlogdest |= LOG_DESTINATION_STDERR;
6316                 else if (pg_strcasecmp(tok, "csvlog") == 0)
6317            newlogdest |= LOG_DESTINATION_CSVLOG;
6318 #ifdef HAVE_SYSLOG
6319                 else if (pg_strcasecmp(tok, "syslog") == 0)
6320                         newlogdest |= LOG_DESTINATION_SYSLOG;
6321 #endif
6322 #ifdef WIN32
6323                 else if (pg_strcasecmp(tok, "eventlog") == 0)
6324                         newlogdest |= LOG_DESTINATION_EVENTLOG;
6325 #endif
6326                 else
6327                 {
6328                         if (source >= PGC_S_INTERACTIVE)
6329                                 ereport(ERROR,
6330                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6331                                   errmsg("unrecognized \"log_destination\" key word: \"%s\"",
6332                                                  tok)));
6333                         pfree(rawstring);
6334                         list_free(elemlist);
6335                         return NULL;
6336                 }
6337         }
6338
6339         if (doit)
6340                 Log_destination = newlogdest;
6341
6342         pfree(rawstring);
6343         list_free(elemlist);
6344
6345         return value;
6346 }
6347
6348 #ifdef HAVE_SYSLOG
6349
6350 static const char *
6351 assign_syslog_facility(const char *facility, bool doit, GucSource source)
6352 {
6353         int                     syslog_fac;
6354
6355         if (pg_strcasecmp(facility, "LOCAL0") == 0)
6356                 syslog_fac = LOG_LOCAL0;
6357         else if (pg_strcasecmp(facility, "LOCAL1") == 0)
6358                 syslog_fac = LOG_LOCAL1;
6359         else if (pg_strcasecmp(facility, "LOCAL2") == 0)
6360                 syslog_fac = LOG_LOCAL2;
6361         else if (pg_strcasecmp(facility, "LOCAL3") == 0)
6362                 syslog_fac = LOG_LOCAL3;
6363         else if (pg_strcasecmp(facility, "LOCAL4") == 0)
6364                 syslog_fac = LOG_LOCAL4;
6365         else if (pg_strcasecmp(facility, "LOCAL5") == 0)
6366                 syslog_fac = LOG_LOCAL5;
6367         else if (pg_strcasecmp(facility, "LOCAL6") == 0)
6368                 syslog_fac = LOG_LOCAL6;
6369         else if (pg_strcasecmp(facility, "LOCAL7") == 0)
6370                 syslog_fac = LOG_LOCAL7;
6371         else
6372                 return NULL;                    /* reject */
6373
6374         if (doit)
6375         {
6376                 syslog_facility = syslog_fac;
6377                 set_syslog_parameters(syslog_ident_str ? syslog_ident_str : "postgres",
6378                                                           syslog_facility);
6379         }
6380
6381         return facility;
6382 }
6383
6384 static const char *
6385 assign_syslog_ident(const char *ident, bool doit, GucSource source)
6386 {
6387         if (doit)
6388                 set_syslog_parameters(ident, syslog_facility);
6389
6390         return ident;
6391 }
6392 #endif   /* HAVE_SYSLOG */
6393
6394
6395 static const char *
6396 assign_defaultxactisolevel(const char *newval, bool doit, GucSource source)
6397 {
6398         if (pg_strcasecmp(newval, "serializable") == 0)
6399         {
6400                 if (doit)
6401                         DefaultXactIsoLevel = XACT_SERIALIZABLE;
6402         }
6403         else if (pg_strcasecmp(newval, "repeatable read") == 0)
6404         {
6405                 if (doit)
6406                         DefaultXactIsoLevel = XACT_REPEATABLE_READ;
6407         }
6408         else if (pg_strcasecmp(newval, "read committed") == 0)
6409         {
6410                 if (doit)
6411                         DefaultXactIsoLevel = XACT_READ_COMMITTED;
6412         }
6413         else if (pg_strcasecmp(newval, "read uncommitted") == 0)
6414         {
6415                 if (doit)
6416                         DefaultXactIsoLevel = XACT_READ_UNCOMMITTED;
6417         }
6418         else
6419                 return NULL;
6420         return newval;
6421 }
6422
6423 static const char *
6424 assign_session_replication_role(const char *newval, bool doit, GucSource source)
6425 {
6426         int             newrole;
6427
6428         if (pg_strcasecmp(newval, "origin") == 0)
6429                 newrole = SESSION_REPLICATION_ROLE_ORIGIN;
6430         else if (pg_strcasecmp(newval, "replica") == 0)
6431                 newrole = SESSION_REPLICATION_ROLE_REPLICA;
6432         else if (pg_strcasecmp(newval, "local") == 0)
6433                 newrole = SESSION_REPLICATION_ROLE_LOCAL;
6434         else
6435                 return NULL;
6436
6437         /*
6438          * Must flush the plan cache when changing replication role; but don't
6439          * flush unnecessarily.
6440          */
6441         if (doit && SessionReplicationRole != newrole)
6442         {
6443                 ResetPlanCache();
6444                 SessionReplicationRole = newrole;
6445         }
6446
6447         return newval;
6448 }
6449
6450 static const char *
6451 assign_log_min_messages(const char *newval,
6452                                                 bool doit, GucSource source)
6453 {
6454         return (assign_msglvl(&log_min_messages, newval, doit, source));
6455 }
6456
6457 static const char *
6458 assign_client_min_messages(const char *newval, bool doit, GucSource source)
6459 {
6460         return (assign_msglvl(&client_min_messages, newval, doit, source));
6461 }
6462
6463 static const char *
6464 assign_min_error_statement(const char *newval, bool doit, GucSource source)
6465 {
6466         return (assign_msglvl(&log_min_error_statement, newval, doit, source));
6467 }
6468
6469 static const char *
6470 assign_msglvl(int *var, const char *newval, bool doit, GucSource source)
6471 {
6472         if (pg_strcasecmp(newval, "debug") == 0)
6473         {
6474                 if (doit)
6475                         (*var) = DEBUG2;
6476         }
6477         else if (pg_strcasecmp(newval, "debug5") == 0)
6478         {
6479                 if (doit)
6480                         (*var) = DEBUG5;
6481         }
6482         else if (pg_strcasecmp(newval, "debug4") == 0)
6483         {
6484                 if (doit)
6485                         (*var) = DEBUG4;
6486         }
6487         else if (pg_strcasecmp(newval, "debug3") == 0)
6488         {
6489                 if (doit)
6490                         (*var) = DEBUG3;
6491         }
6492         else if (pg_strcasecmp(newval, "debug2") == 0)
6493         {
6494                 if (doit)
6495                         (*var) = DEBUG2;
6496         }
6497         else if (pg_strcasecmp(newval, "debug1") == 0)
6498         {
6499                 if (doit)
6500                         (*var) = DEBUG1;
6501         }
6502         else if (pg_strcasecmp(newval, "log") == 0)
6503         {
6504                 if (doit)
6505                         (*var) = LOG;
6506         }
6507
6508         /*
6509          * Client_min_messages always prints 'info', but we allow it as a value
6510          * anyway.
6511          */
6512         else if (pg_strcasecmp(newval, "info") == 0)
6513         {
6514                 if (doit)
6515                         (*var) = INFO;
6516         }
6517         else if (pg_strcasecmp(newval, "notice") == 0)
6518         {
6519                 if (doit)
6520                         (*var) = NOTICE;
6521         }
6522         else if (pg_strcasecmp(newval, "warning") == 0)
6523         {
6524                 if (doit)
6525                         (*var) = WARNING;
6526         }
6527         else if (pg_strcasecmp(newval, "error") == 0)
6528         {
6529                 if (doit)
6530                         (*var) = ERROR;
6531         }
6532         /* We allow FATAL/PANIC for client-side messages too. */
6533         else if (pg_strcasecmp(newval, "fatal") == 0)
6534         {
6535                 if (doit)
6536                         (*var) = FATAL;
6537         }
6538         else if (pg_strcasecmp(newval, "panic") == 0)
6539         {
6540                 if (doit)
6541                         (*var) = PANIC;
6542         }
6543         else
6544                 return NULL;                    /* fail */
6545         return newval;                          /* OK */
6546 }
6547
6548 static const char *
6549 assign_log_error_verbosity(const char *newval, bool doit, GucSource source)
6550 {
6551         if (pg_strcasecmp(newval, "terse") == 0)
6552         {
6553                 if (doit)
6554                         Log_error_verbosity = PGERROR_TERSE;
6555         }
6556         else if (pg_strcasecmp(newval, "default") == 0)
6557         {
6558                 if (doit)
6559                         Log_error_verbosity = PGERROR_DEFAULT;
6560         }
6561         else if (pg_strcasecmp(newval, "verbose") == 0)
6562         {
6563                 if (doit)
6564                         Log_error_verbosity = PGERROR_VERBOSE;
6565         }
6566         else
6567                 return NULL;                    /* fail */
6568         return newval;                          /* OK */
6569 }
6570
6571 static const char *
6572 assign_log_statement(const char *newval, bool doit, GucSource source)
6573 {
6574         if (pg_strcasecmp(newval, "none") == 0)
6575         {
6576                 if (doit)
6577                         log_statement = LOGSTMT_NONE;
6578         }
6579         else if (pg_strcasecmp(newval, "ddl") == 0)
6580         {
6581                 if (doit)
6582                         log_statement = LOGSTMT_DDL;
6583         }
6584         else if (pg_strcasecmp(newval, "mod") == 0)
6585         {
6586                 if (doit)
6587                         log_statement = LOGSTMT_MOD;
6588         }
6589         else if (pg_strcasecmp(newval, "all") == 0)
6590         {
6591                 if (doit)
6592                         log_statement = LOGSTMT_ALL;
6593         }
6594         else
6595                 return NULL;                    /* fail */
6596         return newval;                          /* OK */
6597 }
6598
6599 static const char *
6600 show_num_temp_buffers(void)
6601 {
6602         /*
6603          * We show the GUC var until local buffers have been initialized, and
6604          * NLocBuffer afterwards.
6605          */
6606         static char nbuf[32];
6607
6608         sprintf(nbuf, "%d", NLocBuffer ? NLocBuffer : num_temp_buffers);
6609         return nbuf;
6610 }
6611
6612 static bool
6613 assign_phony_autocommit(bool newval, bool doit, GucSource source)
6614 {
6615         if (!newval)
6616         {
6617                 if (doit && source >= PGC_S_INTERACTIVE)
6618                         ereport(ERROR,
6619                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6620                                          errmsg("SET AUTOCOMMIT TO OFF is no longer supported")));
6621                 return false;
6622         }
6623         return true;
6624 }
6625
6626 static const char *
6627 assign_custom_variable_classes(const char *newval, bool doit, GucSource source)
6628 {
6629         /*
6630          * Check syntax. newval must be a comma separated list of identifiers.
6631          * Whitespace is allowed but skipped.
6632          */
6633         bool            hasSpaceAfterToken = false;
6634         const char *cp = newval;
6635         int                     symLen = 0;
6636         int                     c;
6637         StringInfoData buf;
6638
6639         /*
6640          * Resetting custom_variable_classes by removing it from the
6641          * configuration file will lead to newval = NULL
6642          */
6643         if (newval == NULL)
6644                 return guc_strdup(ERROR, "");
6645
6646         initStringInfo(&buf);
6647         while ((c = *cp++) != 0)
6648         {
6649                 if (isspace((unsigned char) c))
6650                 {
6651                         if (symLen > 0)
6652                                 hasSpaceAfterToken = true;
6653                         continue;
6654                 }
6655
6656                 if (c == ',')
6657                 {
6658                         hasSpaceAfterToken = false;
6659                         if (symLen > 0)
6660                         {
6661                                 symLen = 0;
6662                                 appendStringInfoChar(&buf, ',');
6663                         }
6664                         continue;
6665                 }
6666
6667                 if (hasSpaceAfterToken || !isalnum((unsigned char) c))
6668                 {
6669                         /*
6670                          * Syntax error due to token following space after token or non
6671                          * alpha numeric character
6672                          */
6673                         ereport(LOG,
6674                                         (errcode(ERRCODE_SYNTAX_ERROR),
6675                                          errmsg("invalid syntax for \"custom_variable_classes\": \"%s\"", newval)));
6676                         pfree(buf.data);
6677                         return NULL;
6678                 }
6679                 symLen++;
6680                 appendStringInfoChar(&buf, (char) c);
6681         }
6682
6683         /* Remove stray ',' at end */
6684         if (symLen == 0 && buf.len > 0)
6685                 buf.data[--buf.len] = '\0';
6686
6687         if (buf.len == 0)
6688                 newval = NULL;
6689         else if (doit)
6690                 newval = guc_strdup(ERROR, buf.data);
6691
6692         pfree(buf.data);
6693         return newval;
6694 }
6695
6696 static bool
6697 assign_debug_assertions(bool newval, bool doit, GucSource source)
6698 {
6699 #ifndef USE_ASSERT_CHECKING
6700         if (newval)
6701                 ereport(ERROR,
6702                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6703                            errmsg("assertion checking is not supported by this build")));
6704 #endif
6705         return true;
6706 }
6707
6708 static bool
6709 assign_ssl(bool newval, bool doit, GucSource source)
6710 {
6711 #ifndef USE_SSL
6712         if (newval)
6713                 ereport(ERROR,
6714                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6715                                  errmsg("SSL is not supported by this build")));
6716 #endif
6717         return true;
6718 }
6719
6720 static bool
6721 assign_stage_log_stats(bool newval, bool doit, GucSource source)
6722 {
6723         if (newval && log_statement_stats)
6724         {
6725                 if (source >= PGC_S_INTERACTIVE)
6726                         ereport(ERROR,
6727                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6728                                          errmsg("cannot enable parameter when \"log_statement_stats\" is true")));
6729                 /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
6730                 else if (source != PGC_S_OVERRIDE)
6731                         return false;
6732         }
6733         return true;
6734 }
6735
6736 static bool
6737 assign_log_stats(bool newval, bool doit, GucSource source)
6738 {
6739         if (newval &&
6740                 (log_parser_stats || log_planner_stats || log_executor_stats))
6741         {
6742                 if (source >= PGC_S_INTERACTIVE)
6743                         ereport(ERROR,
6744                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6745                                          errmsg("cannot enable \"log_statement_stats\" when "
6746                                                         "\"log_parser_stats\", \"log_planner_stats\", "
6747                                                         "or \"log_executor_stats\" is true")));
6748                 /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
6749                 else if (source != PGC_S_OVERRIDE)
6750                         return false;
6751         }
6752         return true;
6753 }
6754
6755 static bool
6756 assign_transaction_read_only(bool newval, bool doit, GucSource source)
6757 {
6758         /* Can't go to r/w mode inside a r/o transaction */
6759         if (newval == false && XactReadOnly && IsSubTransaction())
6760         {
6761                 if (source >= PGC_S_INTERACTIVE)
6762                         ereport(ERROR,
6763                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6764                                          errmsg("cannot set transaction read-write mode inside a read-only transaction")));
6765                 /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
6766                 else if (source != PGC_S_OVERRIDE)
6767                         return false;
6768         }
6769         return true;
6770 }
6771
6772 static const char *
6773 assign_canonical_path(const char *newval, bool doit, GucSource source)
6774 {
6775         if (doit)
6776         {
6777                 char       *canon_val = guc_strdup(ERROR, newval);
6778
6779                 canonicalize_path(canon_val);
6780                 return canon_val;
6781         }
6782         else
6783                 return newval;
6784 }
6785
6786 static const char *
6787 assign_backslash_quote(const char *newval, bool doit, GucSource source)
6788 {
6789         BackslashQuoteType bq;
6790         bool            bqbool;
6791
6792         /*
6793          * Although only "on", "off", and "safe_encoding" are documented, we use
6794          * parse_bool so we can accept all the likely variants of "on" and "off".
6795          */
6796         if (pg_strcasecmp(newval, "safe_encoding") == 0)
6797                 bq = BACKSLASH_QUOTE_SAFE_ENCODING;
6798         else if (parse_bool(newval, &bqbool))
6799         {
6800                 bq = bqbool ? BACKSLASH_QUOTE_ON : BACKSLASH_QUOTE_OFF;
6801         }
6802         else
6803                 return NULL;                    /* reject */
6804
6805         if (doit)
6806                 backslash_quote = bq;
6807
6808         return newval;
6809 }
6810
6811 static const char *
6812 assign_timezone_abbreviations(const char *newval, bool doit, GucSource source)
6813 {
6814         /*
6815          * The powerup value shown above for timezone_abbreviations is "UNKNOWN".
6816          * When we see this we just do nothing.  If this value isn't overridden
6817          * from the config file then pg_timezone_abbrev_initialize() will
6818          * eventually replace it with "Default".  This hack has two purposes: to
6819          * avoid wasting cycles loading values that might soon be overridden from
6820          * the config file, and to avoid trying to read the timezone abbrev files
6821          * during InitializeGUCOptions().  The latter doesn't work in an
6822          * EXEC_BACKEND subprocess because my_exec_path hasn't been set yet and so
6823          * we can't locate PGSHAREDIR.  (Essentially the same hack is used to
6824          * delay initializing TimeZone ... if we have any more, we should try to
6825          * clean up and centralize this mechanism ...)
6826          */
6827         if (strcmp(newval, "UNKNOWN") == 0)
6828         {
6829                 return newval;
6830         }
6831
6832         /* Loading abbrev file is expensive, so only do it when value changes */
6833         if (timezone_abbreviations_string == NULL ||
6834                 strcmp(timezone_abbreviations_string, newval) != 0)
6835         {
6836                 int                     elevel;
6837
6838                 /*
6839                  * If reading config file, only the postmaster should bleat loudly
6840                  * about problems.      Otherwise, it's just this one process doing it,
6841                  * and we use WARNING message level.
6842                  */
6843                 if (source == PGC_S_FILE)
6844                         elevel = IsUnderPostmaster ? DEBUG2 : LOG;
6845                 else
6846                         elevel = WARNING;
6847                 if (!load_tzoffsets(newval, doit, elevel))
6848                         return NULL;
6849         }
6850         return newval;
6851 }
6852
6853 /*
6854  * pg_timezone_abbrev_initialize --- set default value if not done already
6855  *
6856  * This is called after initial loading of postgresql.conf.  If no
6857  * timezone_abbreviations setting was found therein, select default.
6858  */
6859 void
6860 pg_timezone_abbrev_initialize(void)
6861 {
6862         if (strcmp(timezone_abbreviations_string, "UNKNOWN") == 0)
6863         {
6864                 SetConfigOption("timezone_abbreviations", "Default",
6865                                                 PGC_POSTMASTER, PGC_S_ARGV);
6866         }
6867 }
6868
6869 static const char *
6870 assign_xmlbinary(const char *newval, bool doit, GucSource source)
6871 {
6872         XmlBinaryType xb;
6873
6874         if (pg_strcasecmp(newval, "base64") == 0)
6875                 xb = XMLBINARY_BASE64;
6876         else if (pg_strcasecmp(newval, "hex") == 0)
6877                 xb = XMLBINARY_HEX;
6878         else
6879                 return NULL;                    /* reject */
6880
6881         if (doit)
6882                 xmlbinary = xb;
6883
6884         return newval;
6885 }
6886
6887 static const char *
6888 assign_xmloption(const char *newval, bool doit, GucSource source)
6889 {
6890         XmlOptionType xo;
6891
6892         if (pg_strcasecmp(newval, "document") == 0)
6893                 xo = XMLOPTION_DOCUMENT;
6894         else if (pg_strcasecmp(newval, "content") == 0)
6895                 xo = XMLOPTION_CONTENT;
6896         else
6897                 return NULL;                    /* reject */
6898
6899         if (doit)
6900                 xmloption = xo;
6901
6902         return newval;
6903 }
6904
6905 static bool
6906 assign_tcp_keepalives_idle(int newval, bool doit, GucSource source)
6907 {
6908         if (doit)
6909                 return (pq_setkeepalivesidle(newval, MyProcPort) == STATUS_OK);
6910
6911         return true;
6912 }
6913
6914 static const char *
6915 show_tcp_keepalives_idle(void)
6916 {
6917         static char nbuf[16];
6918
6919         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
6920         return nbuf;
6921 }
6922
6923 static bool
6924 assign_tcp_keepalives_interval(int newval, bool doit, GucSource source)
6925 {
6926         if (doit)
6927                 return (pq_setkeepalivesinterval(newval, MyProcPort) == STATUS_OK);
6928
6929         return true;
6930 }
6931
6932 static const char *
6933 show_tcp_keepalives_interval(void)
6934 {
6935         static char nbuf[16];
6936
6937         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
6938         return nbuf;
6939 }
6940
6941 static bool
6942 assign_tcp_keepalives_count(int newval, bool doit, GucSource source)
6943 {
6944         if (doit)
6945                 return (pq_setkeepalivescount(newval, MyProcPort) == STATUS_OK);
6946
6947         return true;
6948 }
6949
6950 static const char *
6951 show_tcp_keepalives_count(void)
6952 {
6953         static char nbuf[16];
6954
6955         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
6956         return nbuf;
6957 }
6958
6959 static bool
6960 assign_maxconnections(int newval, bool doit, GucSource source)
6961 {
6962         if (newval + autovacuum_max_workers > INT_MAX / 4)
6963                 return false;
6964
6965         if (doit)
6966                 MaxBackends = newval + autovacuum_max_workers;
6967
6968         return true;
6969 }
6970
6971 static bool
6972 assign_autovacuum_max_workers(int newval, bool doit, GucSource source)
6973 {
6974         if (newval + MaxConnections > INT_MAX / 4)
6975                 return false;
6976
6977         if (doit)
6978                 MaxBackends = newval + MaxConnections;
6979
6980         return true;
6981 }
6982
6983 #include "guc-file.c"