OSDN Git Service

Support SET FROM CURRENT in CREATE/ALTER FUNCTION, ALTER DATABASE, ALTER ROLE.
[pg-rex/syncrep.git] / 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.416 2007/09/03 18:46:30 tgl 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 #include "access/gin.h"
29 #include "access/transam.h"
30 #include "access/twophase.h"
31 #include "access/xact.h"
32 #include "catalog/namespace.h"
33 #include "commands/async.h"
34 #include "commands/prepare.h"
35 #include "commands/vacuum.h"
36 #include "commands/variable.h"
37 #include "commands/trigger.h"
38 #include "funcapi.h"
39 #include "libpq/auth.h"
40 #include "libpq/pqformat.h"
41 #include "miscadmin.h"
42 #include "optimizer/cost.h"
43 #include "optimizer/geqo.h"
44 #include "optimizer/paths.h"
45 #include "optimizer/planmain.h"
46 #include "parser/gramparse.h"
47 #include "parser/parse_expr.h"
48 #include "parser/parse_relation.h"
49 #include "parser/parse_type.h"
50 #include "parser/scansup.h"
51 #include "pgstat.h"
52 #include "postmaster/autovacuum.h"
53 #include "postmaster/bgwriter.h"
54 #include "postmaster/postmaster.h"
55 #include "postmaster/syslogger.h"
56 #include "postmaster/walwriter.h"
57 #include "storage/fd.h"
58 #include "storage/freespace.h"
59 #include "tcop/tcopprot.h"
60 #include "tsearch/ts_cache.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\", "
2244                                                  "\"syslog\", \"csvlog\", and \"eventlog\", "
2245                                                  "depending on the platform."),
2246                         GUC_LIST_INPUT
2247                 },
2248                 &log_destination_string,
2249                 "stderr", assign_log_destination, NULL
2250         },
2251         {
2252                 {"log_directory", PGC_SIGHUP, LOGGING_WHERE,
2253                         gettext_noop("Sets the destination directory for log files."),
2254                         gettext_noop("Can be specified as relative to the data directory "
2255                                                  "or as absolute path."),
2256                         GUC_SUPERUSER_ONLY
2257                 },
2258                 &Log_directory,
2259                 "pg_log", assign_canonical_path, NULL
2260         },
2261         {
2262                 {"log_filename", PGC_SIGHUP, LOGGING_WHERE,
2263                         gettext_noop("Sets the file name pattern for log files."),
2264                         NULL,
2265                         GUC_SUPERUSER_ONLY
2266                 },
2267                 &Log_filename,
2268                 "postgresql-%Y-%m-%d_%H%M%S.log", NULL, NULL
2269         },
2270
2271 #ifdef HAVE_SYSLOG
2272         {
2273                 {"syslog_facility", PGC_SIGHUP, LOGGING_WHERE,
2274                         gettext_noop("Sets the syslog \"facility\" to be used when syslog enabled."),
2275                         gettext_noop("Valid values are LOCAL0, LOCAL1, LOCAL2, LOCAL3, "
2276                                                  "LOCAL4, LOCAL5, LOCAL6, LOCAL7.")
2277                 },
2278                 &syslog_facility_str,
2279                 "LOCAL0", assign_syslog_facility, NULL
2280         },
2281         {
2282                 {"syslog_ident", PGC_SIGHUP, LOGGING_WHERE,
2283                         gettext_noop("Sets the program name used to identify PostgreSQL "
2284                                                  "messages in syslog."),
2285                         NULL
2286                 },
2287                 &syslog_ident_str,
2288                 "postgres", assign_syslog_ident, NULL
2289         },
2290 #endif
2291
2292         {
2293                 {"TimeZone", PGC_USERSET, CLIENT_CONN_LOCALE,
2294                         gettext_noop("Sets the time zone for displaying and interpreting time stamps."),
2295                         NULL,
2296                         GUC_REPORT
2297                 },
2298                 &timezone_string,
2299                 "UNKNOWN", assign_timezone, show_timezone
2300         },
2301         {
2302                 {"timezone_abbreviations", PGC_USERSET, CLIENT_CONN_LOCALE,
2303                         gettext_noop("Selects a file of time zone abbreviations."),
2304                         NULL,
2305                 },
2306                 &timezone_abbreviations_string,
2307                 "UNKNOWN", assign_timezone_abbreviations, NULL
2308         },
2309
2310         {
2311                 {"transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
2312                         gettext_noop("Sets the current transaction's isolation level."),
2313                         NULL,
2314                         GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2315                 },
2316                 &XactIsoLevel_string,
2317                 NULL, assign_XactIsoLevel, show_XactIsoLevel
2318         },
2319
2320         {
2321                 {"unix_socket_group", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2322                         gettext_noop("Sets the owning group of the Unix-domain socket."),
2323                         gettext_noop("The owning user of the socket is always the user "
2324                                                  "that starts the server.")
2325                 },
2326                 &Unix_socket_group,
2327                 "", NULL, NULL
2328         },
2329
2330         {
2331                 {"unix_socket_directory", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2332                         gettext_noop("Sets the directory where the Unix-domain socket will be created."),
2333                         NULL,
2334                         GUC_SUPERUSER_ONLY
2335                 },
2336                 &UnixSocketDir,
2337                 "", assign_canonical_path, NULL
2338         },
2339
2340         {
2341                 {"listen_addresses", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2342                         gettext_noop("Sets the host name or IP address(es) to listen to."),
2343                         NULL,
2344                         GUC_LIST_INPUT
2345                 },
2346                 &ListenAddresses,
2347                 "localhost", NULL, NULL
2348         },
2349
2350         {
2351                 {"wal_sync_method", PGC_SIGHUP, WAL_SETTINGS,
2352                         gettext_noop("Selects the method used for forcing WAL updates to disk."),
2353                         NULL
2354                 },
2355                 &XLOG_sync_method,
2356                 XLOG_sync_method_default, assign_xlog_sync_method, NULL
2357         },
2358
2359         {
2360                 {"custom_variable_classes", PGC_SIGHUP, CUSTOM_OPTIONS,
2361                         gettext_noop("Sets the list of known custom variable classes."),
2362                         NULL,
2363                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2364                 },
2365                 &custom_variable_classes,
2366                 NULL, assign_custom_variable_classes, NULL
2367         },
2368
2369         {
2370                 {"data_directory", PGC_POSTMASTER, FILE_LOCATIONS,
2371                         gettext_noop("Sets the server's data directory."),
2372                         NULL,
2373                         GUC_SUPERUSER_ONLY
2374                 },
2375                 &data_directory,
2376                 NULL, NULL, NULL
2377         },
2378
2379         {
2380                 {"config_file", PGC_POSTMASTER, FILE_LOCATIONS,
2381                         gettext_noop("Sets the server's main configuration file."),
2382                         NULL,
2383                         GUC_DISALLOW_IN_FILE | GUC_SUPERUSER_ONLY
2384                 },
2385                 &ConfigFileName,
2386                 NULL, NULL, NULL
2387         },
2388
2389         {
2390                 {"hba_file", PGC_POSTMASTER, FILE_LOCATIONS,
2391                         gettext_noop("Sets the server's \"hba\" configuration file."),
2392                         NULL,
2393                         GUC_SUPERUSER_ONLY
2394                 },
2395                 &HbaFileName,
2396                 NULL, NULL, NULL
2397         },
2398
2399         {
2400                 {"ident_file", PGC_POSTMASTER, FILE_LOCATIONS,
2401                         gettext_noop("Sets the server's \"ident\" configuration file."),
2402                         NULL,
2403                         GUC_SUPERUSER_ONLY
2404                 },
2405                 &IdentFileName,
2406                 NULL, NULL, NULL
2407         },
2408
2409         {
2410                 {"external_pid_file", PGC_POSTMASTER, FILE_LOCATIONS,
2411                         gettext_noop("Writes the postmaster PID to the specified file."),
2412                         NULL,
2413                         GUC_SUPERUSER_ONLY
2414                 },
2415                 &external_pid_file,
2416                 NULL, assign_canonical_path, NULL
2417         },
2418
2419         {
2420                 {"xmlbinary", PGC_USERSET, CLIENT_CONN_STATEMENT,
2421                         gettext_noop("Sets how binary values are to be encoded in XML."),
2422                         gettext_noop("Valid values are BASE64 and HEX.")
2423                 },
2424                 &xmlbinary_string,
2425                 "base64", assign_xmlbinary, NULL
2426         },
2427
2428         {
2429                 {"xmloption", PGC_USERSET, CLIENT_CONN_STATEMENT,
2430                         gettext_noop("Sets whether XML data in implicit parsing and serialization "
2431                                                  "operations is to be considered as documents or content fragments."),
2432                         gettext_noop("Valid values are DOCUMENT and CONTENT.")
2433                 },
2434                 &xmloption_string,
2435                 "content", assign_xmloption, NULL
2436         },
2437
2438         {
2439                 {"default_text_search_config", PGC_USERSET, CLIENT_CONN_LOCALE,
2440                         gettext_noop("Sets default text search configuration."),
2441                         NULL
2442                 },
2443                 &TSCurrentConfig,
2444                 "pg_catalog.simple", assignTSCurrentConfig, NULL
2445         },
2446
2447 #ifdef USE_SSL
2448         {
2449                 {"ssl_ciphers", PGC_POSTMASTER, CONN_AUTH_SECURITY,
2450                         gettext_noop("Sets the list of allowed SSL ciphers."),
2451                         NULL,
2452                         GUC_SUPERUSER_ONLY
2453                 },
2454                 &SSLCipherSuites,
2455                 "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH", NULL, NULL
2456         },
2457 #endif /* USE_SSL */
2458
2459         /* End-of-list marker */
2460         {
2461                 {NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL
2462         }
2463 };
2464
2465
2466 /******** end of options list ********/
2467
2468
2469 /*
2470  * To allow continued support of obsolete names for GUC variables, we apply
2471  * the following mappings to any unrecognized name.  Note that an old name
2472  * should be mapped to a new one only if the new variable has very similar
2473  * semantics to the old.
2474  */
2475 static const char *const map_old_guc_names[] = {
2476         "sort_mem", "work_mem",
2477         "vacuum_mem", "maintenance_work_mem",
2478         NULL
2479 };
2480
2481
2482 /*
2483  * Actual lookup of variables is done through this single, sorted array.
2484  */
2485 static struct config_generic **guc_variables;
2486
2487 /* Current number of variables contained in the vector */
2488 static int      num_guc_variables;
2489
2490 /* Vector capacity */
2491 static int      size_guc_variables;
2492
2493
2494 static bool guc_dirty;                  /* TRUE if need to do commit/abort work */
2495
2496 static bool reporting_enabled;  /* TRUE to enable GUC_REPORT */
2497
2498 static int      GUCNestLevel = 0;       /* 1 when in main transaction */
2499
2500
2501 static int      guc_var_compare(const void *a, const void *b);
2502 static int      guc_name_compare(const char *namea, const char *nameb);
2503 static void push_old_value(struct config_generic * gconf);
2504 static void ReportGUCOption(struct config_generic * record);
2505 static void ShowGUCConfigOption(const char *name, DestReceiver *dest);
2506 static void ShowAllGUCConfig(DestReceiver *dest);
2507 static char *_ShowOption(struct config_generic * record, bool use_units);
2508 static bool is_newvalue_equal(struct config_generic * record, const char *newvalue);
2509
2510
2511 /*
2512  * Some infrastructure for checking malloc/strdup/realloc calls
2513  */
2514 static void *
2515 guc_malloc(int elevel, size_t size)
2516 {
2517         void       *data;
2518
2519         data = malloc(size);
2520         if (data == NULL)
2521                 ereport(elevel,
2522                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2523                                  errmsg("out of memory")));
2524         return data;
2525 }
2526
2527 static void *
2528 guc_realloc(int elevel, void *old, size_t size)
2529 {
2530         void       *data;
2531
2532         data = realloc(old, size);
2533         if (data == NULL)
2534                 ereport(elevel,
2535                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2536                                  errmsg("out of memory")));
2537         return data;
2538 }
2539
2540 static char *
2541 guc_strdup(int elevel, const char *src)
2542 {
2543         char       *data;
2544
2545         data = strdup(src);
2546         if (data == NULL)
2547                 ereport(elevel,
2548                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2549                                  errmsg("out of memory")));
2550         return data;
2551 }
2552
2553
2554 /*
2555  * Support for assigning to a field of a string GUC item.  Free the prior
2556  * value if it's not referenced anywhere else in the item (including stacked
2557  * states).
2558  */
2559 static void
2560 set_string_field(struct config_string * conf, char **field, char *newval)
2561 {
2562         char       *oldval = *field;
2563         GucStack   *stack;
2564
2565         /* Do the assignment */
2566         *field = newval;
2567
2568         /* Exit if any duplicate references, or if old value was NULL anyway */
2569         if (oldval == NULL ||
2570                 oldval == *(conf->variable) ||
2571                 oldval == conf->reset_val ||
2572                 oldval == conf->tentative_val ||
2573                 oldval == conf->boot_val)
2574                 return;
2575         for (stack = conf->gen.stack; stack; stack = stack->prev)
2576         {
2577                 if (oldval == stack->tentative_val.stringval ||
2578                         oldval == stack->value.stringval)
2579                         return;
2580         }
2581
2582         /* Not used anymore, so free it */
2583         free(oldval);
2584 }
2585
2586 /*
2587  * Detect whether strval is referenced anywhere in a GUC string item
2588  */
2589 static bool
2590 string_field_used(struct config_string * conf, char *strval)
2591 {
2592         GucStack   *stack;
2593
2594         if (strval == *(conf->variable) ||
2595                 strval == conf->reset_val ||
2596                 strval == conf->tentative_val ||
2597                 strval == conf->boot_val)
2598                 return true;
2599         for (stack = conf->gen.stack; stack; stack = stack->prev)
2600         {
2601                 if (strval == stack->tentative_val.stringval ||
2602                         strval == stack->value.stringval)
2603                         return true;
2604         }
2605         return false;
2606 }
2607
2608
2609 struct config_generic **
2610 get_guc_variables(void)
2611 {
2612         return guc_variables;
2613 }
2614
2615
2616 /*
2617  * Build the sorted array.      This is split out so that it could be
2618  * re-executed after startup (eg, we could allow loadable modules to
2619  * add vars, and then we'd need to re-sort).
2620  */
2621 void
2622 build_guc_variables(void)
2623 {
2624         int                     size_vars;
2625         int                     num_vars = 0;
2626         struct config_generic **guc_vars;
2627         int                     i;
2628
2629         for (i = 0; ConfigureNamesBool[i].gen.name; i++)
2630         {
2631                 struct config_bool *conf = &ConfigureNamesBool[i];
2632
2633                 /* Rather than requiring vartype to be filled in by hand, do this: */
2634                 conf->gen.vartype = PGC_BOOL;
2635                 num_vars++;
2636         }
2637
2638         for (i = 0; ConfigureNamesInt[i].gen.name; i++)
2639         {
2640                 struct config_int *conf = &ConfigureNamesInt[i];
2641
2642                 conf->gen.vartype = PGC_INT;
2643                 num_vars++;
2644         }
2645
2646         for (i = 0; ConfigureNamesReal[i].gen.name; i++)
2647         {
2648                 struct config_real *conf = &ConfigureNamesReal[i];
2649
2650                 conf->gen.vartype = PGC_REAL;
2651                 num_vars++;
2652         }
2653
2654         for (i = 0; ConfigureNamesString[i].gen.name; i++)
2655         {
2656                 struct config_string *conf = &ConfigureNamesString[i];
2657
2658                 conf->gen.vartype = PGC_STRING;
2659                 num_vars++;
2660         }
2661
2662         /*
2663          * Create table with 20% slack
2664          */
2665         size_vars = num_vars + num_vars / 4;
2666
2667         guc_vars = (struct config_generic **)
2668                 guc_malloc(FATAL, size_vars * sizeof(struct config_generic *));
2669
2670         num_vars = 0;
2671
2672         for (i = 0; ConfigureNamesBool[i].gen.name; i++)
2673                 guc_vars[num_vars++] = &ConfigureNamesBool[i].gen;
2674
2675         for (i = 0; ConfigureNamesInt[i].gen.name; i++)
2676                 guc_vars[num_vars++] = &ConfigureNamesInt[i].gen;
2677
2678         for (i = 0; ConfigureNamesReal[i].gen.name; i++)
2679                 guc_vars[num_vars++] = &ConfigureNamesReal[i].gen;
2680
2681         for (i = 0; ConfigureNamesString[i].gen.name; i++)
2682                 guc_vars[num_vars++] = &ConfigureNamesString[i].gen;
2683
2684         if (guc_variables)
2685                 free(guc_variables);
2686         guc_variables = guc_vars;
2687         num_guc_variables = num_vars;
2688         size_guc_variables = size_vars;
2689         qsort((void *) guc_variables, num_guc_variables,
2690                   sizeof(struct config_generic *), guc_var_compare);
2691 }
2692
2693 static bool
2694 is_custom_class(const char *name, int dotPos)
2695 {
2696         /*
2697          * assign_custom_variable_classes() has made sure no empty identifiers or
2698          * whitespace exists in the variable
2699          */
2700         bool            result = false;
2701         const char *ccs = GetConfigOption("custom_variable_classes");
2702
2703         if (ccs != NULL)
2704         {
2705                 const char *start = ccs;
2706
2707                 for (;; ++ccs)
2708                 {
2709                         int                     c = *ccs;
2710
2711                         if (c == 0 || c == ',')
2712                         {
2713                                 if (dotPos == ccs - start && strncmp(start, name, dotPos) == 0)
2714                                 {
2715                                         result = true;
2716                                         break;
2717                                 }
2718                                 if (c == 0)
2719                                         break;
2720                                 start = ccs + 1;
2721                         }
2722                 }
2723         }
2724         return result;
2725 }
2726
2727 /*
2728  * Add a new GUC variable to the list of known variables. The
2729  * list is expanded if needed.
2730  */
2731 static bool
2732 add_guc_variable(struct config_generic * var, int elevel)
2733 {
2734         if (num_guc_variables + 1 >= size_guc_variables)
2735         {
2736                 /*
2737                  * Increase the vector by 25%
2738                  */
2739                 int                     size_vars = size_guc_variables + size_guc_variables / 4;
2740                 struct config_generic **guc_vars;
2741
2742                 if (size_vars == 0)
2743                 {
2744                         size_vars = 100;
2745                         guc_vars = (struct config_generic **)
2746                                 guc_malloc(elevel, size_vars * sizeof(struct config_generic *));
2747                 }
2748                 else
2749                 {
2750                         guc_vars = (struct config_generic **)
2751                                 guc_realloc(elevel, guc_variables, size_vars * sizeof(struct config_generic *));
2752                 }
2753
2754                 if (guc_vars == NULL)
2755                         return false;           /* out of memory */
2756
2757                 guc_variables = guc_vars;
2758                 size_guc_variables = size_vars;
2759         }
2760         guc_variables[num_guc_variables++] = var;
2761         qsort((void *) guc_variables, num_guc_variables,
2762                   sizeof(struct config_generic *), guc_var_compare);
2763         return true;
2764 }
2765
2766 /*
2767  * Create and add a placeholder variable. It's presumed to belong
2768  * to a valid custom variable class at this point.
2769  */
2770 static struct config_string *
2771 add_placeholder_variable(const char *name, int elevel)
2772 {
2773         size_t          sz = sizeof(struct config_string) + sizeof(char *);
2774         struct config_string *var;
2775         struct config_generic *gen;
2776
2777         var = (struct config_string *) guc_malloc(elevel, sz);
2778         if (var == NULL)
2779                 return NULL;
2780
2781         gen = &var->gen;
2782         memset(var, 0, sz);
2783
2784         gen->name = guc_strdup(elevel, name);
2785         if (gen->name == NULL)
2786         {
2787                 free(var);
2788                 return NULL;
2789         }
2790
2791         gen->context = PGC_USERSET;
2792         gen->group = CUSTOM_OPTIONS;
2793         gen->short_desc = "GUC placeholder variable";
2794         gen->flags = GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE | GUC_CUSTOM_PLACEHOLDER;
2795         gen->vartype = PGC_STRING;
2796
2797         /*
2798          * The char* is allocated at the end of the struct since we have no
2799          * 'static' place to point to.
2800          */
2801         var->variable = (char **) (var + 1);
2802
2803         if (!add_guc_variable((struct config_generic *) var, elevel))
2804         {
2805                 free((void *) gen->name);
2806                 free(var);
2807                 return NULL;
2808         }
2809
2810         return var;
2811 }
2812
2813 /*
2814  * Look up option NAME. If it exists, return a pointer to its record,
2815  * else return NULL.
2816  */
2817 static struct config_generic *
2818 find_option(const char *name, int elevel)
2819 {
2820         const char *dot;
2821         const char **key = &name;
2822         struct config_generic **res;
2823         int                     i;
2824
2825         Assert(name);
2826
2827         /*
2828          * By equating const char ** with struct config_generic *, we are assuming
2829          * the name field is first in config_generic.
2830          */
2831         res = (struct config_generic **) bsearch((void *) &key,
2832                                                                                          (void *) guc_variables,
2833                                                                                          num_guc_variables,
2834                                                                                          sizeof(struct config_generic *),
2835                                                                                          guc_var_compare);
2836         if (res)
2837                 return *res;
2838
2839         /*
2840          * See if the name is an obsolete name for a variable.  We assume that the
2841          * set of supported old names is short enough that a brute-force search is
2842          * the best way.
2843          */
2844         for (i = 0; map_old_guc_names[i] != NULL; i += 2)
2845         {
2846                 if (guc_name_compare(name, map_old_guc_names[i]) == 0)
2847                         return find_option(map_old_guc_names[i + 1], elevel);
2848         }
2849
2850         /*
2851          * Check if the name is qualified, and if so, check if the qualifier maps
2852          * to a custom variable class.
2853          */
2854         dot = strchr(name, GUC_QUALIFIER_SEPARATOR);
2855         if (dot != NULL && is_custom_class(name, dot - name))
2856                 /* Add a placeholder variable for this name */
2857                 return (struct config_generic *) add_placeholder_variable(name, elevel);
2858
2859         /* Unknown name */
2860         return NULL;
2861 }
2862
2863
2864 /*
2865  * comparator for qsorting and bsearching guc_variables array
2866  */
2867 static int
2868 guc_var_compare(const void *a, const void *b)
2869 {
2870         struct config_generic *confa = *(struct config_generic **) a;
2871         struct config_generic *confb = *(struct config_generic **) b;
2872
2873         return guc_name_compare(confa->name, confb->name);
2874 }
2875
2876
2877 static int
2878 guc_name_compare(const char *namea, const char *nameb)
2879 {
2880         /*
2881          * The temptation to use strcasecmp() here must be resisted, because the
2882          * array ordering has to remain stable across setlocale() calls. So, build
2883          * our own with a simple ASCII-only downcasing.
2884          */
2885         while (*namea && *nameb)
2886         {
2887                 char            cha = *namea++;
2888                 char            chb = *nameb++;
2889
2890                 if (cha >= 'A' && cha <= 'Z')
2891                         cha += 'a' - 'A';
2892                 if (chb >= 'A' && chb <= 'Z')
2893                         chb += 'a' - 'A';
2894                 if (cha != chb)
2895                         return cha - chb;
2896         }
2897         if (*namea)
2898                 return 1;                               /* a is longer */
2899         if (*nameb)
2900                 return -1;                              /* b is longer */
2901         return 0;
2902 }
2903
2904
2905 static int
2906 guc_get_index(const char *name)
2907 {
2908         const char **key = &name;
2909         struct config_generic **res;
2910
2911         Assert(name);
2912
2913         /*
2914          * By equating const char ** with struct config_generic *, we are assuming
2915          * the name field is first in config_generic.
2916          */
2917         res = (struct config_generic **) bsearch((void *) &key,
2918                                                                                          (void *) guc_variables,
2919                                                                                          num_guc_variables,
2920                                                                                          sizeof(struct config_generic *),
2921                                                                                          guc_var_compare);
2922         if (!res)
2923                 return -1;
2924
2925         return res - guc_variables;
2926 }
2927
2928
2929 /*
2930  * Initialize GUC options during program startup.
2931  *
2932  * Note that we cannot read the config file yet, since we have not yet
2933  * processed command-line switches.
2934  */
2935 void
2936 InitializeGUCOptions(void)
2937 {
2938         int                     i;
2939         char       *env;
2940         long            stack_rlimit;
2941
2942         /*
2943          * Before log_line_prefix could possibly receive a nonempty setting,
2944          * make sure that timezone processing is minimally alive (see elog.c).
2945          */
2946         pg_timezone_pre_initialize();
2947
2948         /*
2949          * Build sorted array of all GUC variables.
2950          */
2951         build_guc_variables();
2952
2953         /*
2954          * Load all variables with their compiled-in defaults, and initialize
2955          * status fields as needed.
2956          */
2957         for (i = 0; i < num_guc_variables; i++)
2958         {
2959                 struct config_generic *gconf = guc_variables[i];
2960
2961                 gconf->status = 0;
2962                 gconf->reset_source = PGC_S_DEFAULT;
2963                 gconf->tentative_source = PGC_S_DEFAULT;
2964                 gconf->source = PGC_S_DEFAULT;
2965                 gconf->stack = NULL;
2966
2967                 switch (gconf->vartype)
2968                 {
2969                         case PGC_BOOL:
2970                                 {
2971                                         struct config_bool *conf = (struct config_bool *) gconf;
2972
2973                                         if (conf->assign_hook)
2974                                                 if (!(*conf->assign_hook) (conf->boot_val, true,
2975                                                                                                    PGC_S_DEFAULT))
2976                                                         elog(FATAL, "failed to initialize %s to %d",
2977                                                                  conf->gen.name, (int) conf->boot_val);
2978                                         *conf->variable = conf->reset_val = conf->boot_val;
2979                                         break;
2980                                 }
2981                         case PGC_INT:
2982                                 {
2983                                         struct config_int *conf = (struct config_int *) gconf;
2984
2985                                         Assert(conf->boot_val >= conf->min);
2986                                         Assert(conf->boot_val <= conf->max);
2987                                         if (conf->assign_hook)
2988                                                 if (!(*conf->assign_hook) (conf->boot_val, true,
2989                                                                                                    PGC_S_DEFAULT))
2990                                                         elog(FATAL, "failed to initialize %s to %d",
2991                                                                  conf->gen.name, conf->boot_val);
2992                                         *conf->variable = conf->reset_val = conf->boot_val;
2993                                         break;
2994                                 }
2995                         case PGC_REAL:
2996                                 {
2997                                         struct config_real *conf = (struct config_real *) gconf;
2998
2999                                         Assert(conf->boot_val >= conf->min);
3000                                         Assert(conf->boot_val <= conf->max);
3001                                         if (conf->assign_hook)
3002                                                 if (!(*conf->assign_hook) (conf->boot_val, true,
3003                                                                                                    PGC_S_DEFAULT))
3004                                                         elog(FATAL, "failed to initialize %s to %g",
3005                                                                  conf->gen.name, conf->boot_val);
3006                                         *conf->variable = conf->reset_val = conf->boot_val;
3007                                         break;
3008                                 }
3009                         case PGC_STRING:
3010                                 {
3011                                         struct config_string *conf = (struct config_string *) gconf;
3012                                         char       *str;
3013
3014                                         *conf->variable = NULL;
3015                                         conf->reset_val = NULL;
3016                                         conf->tentative_val = NULL;
3017
3018                                         if (conf->boot_val == NULL)
3019                                         {
3020                                                 /* Cannot set value yet */
3021                                                 break;
3022                                         }
3023
3024                                         str = guc_strdup(FATAL, conf->boot_val);
3025                                         conf->reset_val = str;
3026
3027                                         if (conf->assign_hook)
3028                                         {
3029                                                 const char *newstr;
3030
3031                                                 newstr = (*conf->assign_hook) (str, true,
3032                                                                                                            PGC_S_DEFAULT);
3033                                                 if (newstr == NULL)
3034                                                 {
3035                                                         elog(FATAL, "failed to initialize %s to \"%s\"",
3036                                                                  conf->gen.name, str);
3037                                                 }
3038                                                 else if (newstr != str)
3039                                                 {
3040                                                         free(str);
3041
3042                                                         /*
3043                                                          * See notes in set_config_option about casting
3044                                                          */
3045                                                         str = (char *) newstr;
3046                                                         conf->reset_val = str;
3047                                                 }
3048                                         }
3049                                         *conf->variable = str;
3050                                         break;
3051                                 }
3052                 }
3053         }
3054
3055         guc_dirty = false;
3056
3057         reporting_enabled = false;
3058
3059         /*
3060          * Prevent any attempt to override the transaction modes from
3061          * non-interactive sources.
3062          */
3063         SetConfigOption("transaction_isolation", "default",
3064                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3065         SetConfigOption("transaction_read_only", "no",
3066                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3067
3068         /*
3069          * For historical reasons, some GUC parameters can receive defaults from
3070          * environment variables.  Process those settings.
3071          */
3072
3073         env = getenv("PGPORT");
3074         if (env != NULL)
3075                 SetConfigOption("port", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3076
3077         env = getenv("PGDATESTYLE");
3078         if (env != NULL)
3079                 SetConfigOption("datestyle", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3080
3081         env = getenv("PGCLIENTENCODING");
3082         if (env != NULL)
3083                 SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3084
3085         /*
3086          * rlimit isn't exactly an "environment variable", but it behaves about
3087          * the same.  If we can identify the platform stack depth rlimit, increase
3088          * default stack depth setting up to whatever is safe (but at most 2MB).
3089          */
3090         stack_rlimit = get_stack_depth_rlimit();
3091         if (stack_rlimit > 0)
3092         {
3093                 int             new_limit = (stack_rlimit - STACK_DEPTH_SLOP) / 1024L;
3094
3095                 if (new_limit > 100)
3096                 {
3097                         char    limbuf[16];
3098
3099                         new_limit = Min(new_limit, 2048);
3100                         sprintf(limbuf, "%d", new_limit);
3101                         SetConfigOption("max_stack_depth", limbuf,
3102                                                         PGC_POSTMASTER, PGC_S_ENV_VAR);
3103                 }
3104         }
3105 }
3106
3107
3108 /*
3109  * Select the configuration files and data directory to be used, and
3110  * do the initial read of postgresql.conf.
3111  *
3112  * This is called after processing command-line switches.
3113  *              userDoption is the -D switch value if any (NULL if unspecified).
3114  *              progname is just for use in error messages.
3115  *
3116  * Returns true on success; on failure, prints a suitable error message
3117  * to stderr and returns false.
3118  */
3119 bool
3120 SelectConfigFiles(const char *userDoption, const char *progname)
3121 {
3122         char       *configdir;
3123         char       *fname;
3124         struct stat stat_buf;
3125
3126         /* configdir is -D option, or $PGDATA if no -D */
3127         if (userDoption)
3128                 configdir = make_absolute_path(userDoption);
3129         else
3130                 configdir = make_absolute_path(getenv("PGDATA"));
3131
3132         /*
3133          * Find the configuration file: if config_file was specified on the
3134          * command line, use it, else use configdir/postgresql.conf.  In any case
3135          * ensure the result is an absolute path, so that it will be interpreted
3136          * the same way by future backends.
3137          */
3138         if (ConfigFileName)
3139                 fname = make_absolute_path(ConfigFileName);
3140         else if (configdir)
3141         {
3142                 fname = guc_malloc(FATAL,
3143                                                    strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
3144                 sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
3145         }
3146         else
3147         {
3148                 write_stderr("%s does not know where to find the server configuration file.\n"
3149                                          "You must specify the --config-file or -D invocation "
3150                                          "option or set the PGDATA environment variable.\n",
3151                                          progname);
3152                 return false;
3153         }
3154
3155         /*
3156          * Set the ConfigFileName GUC variable to its final value, ensuring that
3157          * it can't be overridden later.
3158          */
3159         SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
3160         free(fname);
3161
3162         /*
3163          * Now read the config file for the first time.
3164          */
3165         if (stat(ConfigFileName, &stat_buf) != 0)
3166         {
3167                 write_stderr("%s cannot access the server configuration file \"%s\": %s\n",
3168                                          progname, ConfigFileName, strerror(errno));
3169                 return false;
3170         }
3171
3172         ProcessConfigFile(PGC_POSTMASTER);
3173
3174         /*
3175          * If the data_directory GUC variable has been set, use that as DataDir;
3176          * otherwise use configdir if set; else punt.
3177          *
3178          * Note: SetDataDir will copy and absolute-ize its argument, so we don't
3179          * have to.
3180          */
3181         if (data_directory)
3182                 SetDataDir(data_directory);
3183         else if (configdir)
3184                 SetDataDir(configdir);
3185         else
3186         {
3187                 write_stderr("%s does not know where to find the database system data.\n"
3188                                          "This can be specified as \"data_directory\" in \"%s\", "
3189                                          "or by the -D invocation option, or by the "
3190                                          "PGDATA environment variable.\n",
3191                                          progname, ConfigFileName);
3192                 return false;
3193         }
3194
3195         /*
3196          * Reflect the final DataDir value back into the data_directory GUC var.
3197          * (If you are wondering why we don't just make them a single variable,
3198          * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
3199          * child backends specially.  XXX is that still true?  Given that we now
3200          * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
3201          * DataDir in advance.)
3202          */
3203         SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);
3204
3205         /*
3206          * Figure out where pg_hba.conf is, and make sure the path is absolute.
3207          */
3208         if (HbaFileName)
3209                 fname = make_absolute_path(HbaFileName);
3210         else if (configdir)
3211         {
3212                 fname = guc_malloc(FATAL,
3213                                                    strlen(configdir) + strlen(HBA_FILENAME) + 2);
3214                 sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
3215         }
3216         else
3217         {
3218                 write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
3219                                          "This can be specified as \"hba_file\" in \"%s\", "
3220                                          "or by the -D invocation option, or by the "
3221                                          "PGDATA environment variable.\n",
3222                                          progname, ConfigFileName);
3223                 return false;
3224         }
3225         SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
3226         free(fname);
3227
3228         /*
3229          * Likewise for pg_ident.conf.
3230          */
3231         if (IdentFileName)
3232                 fname = make_absolute_path(IdentFileName);
3233         else if (configdir)
3234         {
3235                 fname = guc_malloc(FATAL,
3236                                                    strlen(configdir) + strlen(IDENT_FILENAME) + 2);
3237                 sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
3238         }
3239         else
3240         {
3241                 write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
3242                                          "This can be specified as \"ident_file\" in \"%s\", "
3243                                          "or by the -D invocation option, or by the "
3244                                          "PGDATA environment variable.\n",
3245                                          progname, ConfigFileName);
3246                 return false;
3247         }
3248         SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
3249         free(fname);
3250
3251         free(configdir);
3252
3253         return true;
3254 }
3255
3256
3257 /*
3258  * Reset all options to their saved default values (implements RESET ALL)
3259  */
3260 void
3261 ResetAllOptions(void)
3262 {
3263         int                     i;
3264
3265         for (i = 0; i < num_guc_variables; i++)
3266         {
3267                 struct config_generic *gconf = guc_variables[i];
3268
3269                 /* Don't reset non-SET-able values */
3270                 if (gconf->context != PGC_SUSET &&
3271                         gconf->context != PGC_USERSET)
3272                         continue;
3273                 /* Don't reset if special exclusion from RESET ALL */
3274                 if (gconf->flags & GUC_NO_RESET_ALL)
3275                         continue;
3276                 /* No need to reset if wasn't SET */
3277                 if (gconf->source <= PGC_S_OVERRIDE)
3278                         continue;
3279
3280                 /* Save old value to support transaction abort */
3281                 push_old_value(gconf);
3282
3283                 switch (gconf->vartype)
3284                 {
3285                         case PGC_BOOL:
3286                                 {
3287                                         struct config_bool *conf = (struct config_bool *) gconf;
3288
3289                                         if (conf->assign_hook)
3290                                                 if (!(*conf->assign_hook) (conf->reset_val, true,
3291                                                                                                    PGC_S_SESSION))
3292                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3293                                         *conf->variable = conf->reset_val;
3294                                         conf->tentative_val = conf->reset_val;
3295                                         conf->gen.source = conf->gen.reset_source;
3296                                         conf->gen.tentative_source = conf->gen.reset_source;
3297                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3298                                         guc_dirty = true;
3299                                         break;
3300                                 }
3301                         case PGC_INT:
3302                                 {
3303                                         struct config_int *conf = (struct config_int *) gconf;
3304
3305                                         if (conf->assign_hook)
3306                                                 if (!(*conf->assign_hook) (conf->reset_val, true,
3307                                                                                                    PGC_S_SESSION))
3308                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3309                                         *conf->variable = conf->reset_val;
3310                                         conf->tentative_val = conf->reset_val;
3311                                         conf->gen.source = conf->gen.reset_source;
3312                                         conf->gen.tentative_source = conf->gen.reset_source;
3313                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3314                                         guc_dirty = true;
3315                                         break;
3316                                 }
3317                         case PGC_REAL:
3318                                 {
3319                                         struct config_real *conf = (struct config_real *) gconf;
3320
3321                                         if (conf->assign_hook)
3322                                                 if (!(*conf->assign_hook) (conf->reset_val, true,
3323                                                                                                    PGC_S_SESSION))
3324                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3325                                         *conf->variable = conf->reset_val;
3326                                         conf->tentative_val = conf->reset_val;
3327                                         conf->gen.source = conf->gen.reset_source;
3328                                         conf->gen.tentative_source = conf->gen.reset_source;
3329                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3330                                         guc_dirty = true;
3331                                         break;
3332                                 }
3333                         case PGC_STRING:
3334                                 {
3335                                         struct config_string *conf = (struct config_string *) gconf;
3336                                         char       *str;
3337
3338                                         if (conf->reset_val == NULL)
3339                                         {
3340                                                 /* Nothing to reset to, as yet; so do nothing */
3341                                                 break;
3342                                         }
3343
3344                                         /* We need not strdup here */
3345                                         str = conf->reset_val;
3346
3347                                         if (conf->assign_hook)
3348                                         {
3349                                                 const char *newstr;
3350
3351                                                 newstr = (*conf->assign_hook) (str, true,
3352                                                                                                            PGC_S_SESSION);
3353                                                 if (newstr == NULL)
3354                                                         elog(ERROR, "failed to reset %s", conf->gen.name);
3355                                                 else if (newstr != str)
3356                                                 {
3357                                                         /*
3358                                                          * See notes in set_config_option about casting
3359                                                          */
3360                                                         str = (char *) newstr;
3361                                                 }
3362                                         }
3363
3364                                         set_string_field(conf, conf->variable, str);
3365                                         set_string_field(conf, &conf->tentative_val, str);
3366                                         conf->gen.source = conf->gen.reset_source;
3367                                         conf->gen.tentative_source = conf->gen.reset_source;
3368                                         conf->gen.status |= GUC_HAVE_TENTATIVE;
3369                                         guc_dirty = true;
3370                                         break;
3371                                 }
3372                 }
3373
3374                 if (gconf->flags & GUC_REPORT)
3375                         ReportGUCOption(gconf);
3376         }
3377 }
3378
3379
3380 /*
3381  * push_old_value
3382  *              Push previous state during first assignment to a GUC variable
3383  *              within a particular transaction.
3384  *
3385  * We have to be willing to "back-fill" the state stack if the first
3386  * assignment occurs within a subtransaction nested several levels deep.
3387  * This ensures that if an intermediate transaction aborts, it will have
3388  * the proper value available to restore the setting to.
3389  */
3390 static void
3391 push_old_value(struct config_generic * gconf)
3392 {
3393         GucStack   *stack;
3394
3395         /* If we're not inside a transaction, do nothing */
3396         if (GUCNestLevel == 0)
3397                 return;
3398
3399         for (;;)
3400         {
3401                 /* Done if we already pushed it at this nesting depth */
3402                 if (gconf->stack && gconf->stack->nest_level >= GUCNestLevel)
3403                         return;
3404
3405                 /*
3406                  * We keep all the stack entries in TopTransactionContext so as to
3407                  * avoid allocation problems when a subtransaction back-fills stack
3408                  * entries for upper transaction levels.
3409                  */
3410                 stack = (GucStack *) MemoryContextAlloc(TopTransactionContext,
3411                                                                                                 sizeof(GucStack));
3412
3413                 stack->prev = gconf->stack;
3414                 stack->nest_level = stack->prev ? stack->prev->nest_level + 1 : 1;
3415                 stack->status = gconf->status;
3416                 stack->tentative_source = gconf->tentative_source;
3417                 stack->source = gconf->source;
3418
3419                 switch (gconf->vartype)
3420                 {
3421                         case PGC_BOOL:
3422                                 stack->tentative_val.boolval =
3423                                         ((struct config_bool *) gconf)->tentative_val;
3424                                 stack->value.boolval =
3425                                         *((struct config_bool *) gconf)->variable;
3426                                 break;
3427
3428                         case PGC_INT:
3429                                 stack->tentative_val.intval =
3430                                         ((struct config_int *) gconf)->tentative_val;
3431                                 stack->value.intval =
3432                                         *((struct config_int *) gconf)->variable;
3433                                 break;
3434
3435                         case PGC_REAL:
3436                                 stack->tentative_val.realval =
3437                                         ((struct config_real *) gconf)->tentative_val;
3438                                 stack->value.realval =
3439                                         *((struct config_real *) gconf)->variable;
3440                                 break;
3441
3442                         case PGC_STRING:
3443                                 stack->tentative_val.stringval =
3444                                         ((struct config_string *) gconf)->tentative_val;
3445                                 stack->value.stringval =
3446                                         *((struct config_string *) gconf)->variable;
3447                                 break;
3448                 }
3449
3450                 gconf->stack = stack;
3451
3452                 /* Set state to indicate nothing happened yet within this level */
3453                 gconf->status = GUC_HAVE_STACK;
3454
3455                 /* Ensure we remember to pop at end of xact */
3456                 guc_dirty = true;
3457         }
3458 }
3459
3460 /*
3461  * Do GUC processing at main transaction start.
3462  */
3463 void
3464 AtStart_GUC(void)
3465 {
3466         /*
3467          * The nest level should be 0 between transactions; if it isn't,
3468          * somebody didn't call AtEOXact_GUC, or called it with the wrong
3469          * nestLevel.  We throw a warning but make no other effort to clean up.
3470          */
3471         if (GUCNestLevel != 0)
3472                 elog(WARNING, "GUC nest level = %d at transaction start",
3473                          GUCNestLevel);
3474         GUCNestLevel = 1;
3475 }
3476
3477 /*
3478  * Enter a new nesting level for GUC values.  This is called at subtransaction
3479  * start and when entering a function that has proconfig settings.  NOTE that
3480  * we must not risk error here, else subtransaction start will be unhappy.
3481  */
3482 int
3483 NewGUCNestLevel(void)
3484 {
3485         return ++GUCNestLevel;
3486 }
3487
3488 /*
3489  * Do GUC processing at transaction or subtransaction commit or abort, or
3490  * when exiting a function that has proconfig settings.  (The name is thus
3491  * a bit of a misnomer; perhaps it should be ExitGUCNestLevel or some such.)
3492  * During abort, we discard all GUC settings that were applied at nesting
3493  * levels >= nestLevel.  nestLevel == 1 corresponds to the main transaction.
3494  */
3495 void
3496 AtEOXact_GUC(bool isCommit, int nestLevel)
3497 {
3498         int                     i;
3499
3500         Assert(nestLevel > 0 && nestLevel <= GUCNestLevel);
3501
3502         /* Quick exit if nothing's changed in this transaction */
3503         if (!guc_dirty)
3504         {
3505                 GUCNestLevel = nestLevel - 1;
3506                 return;
3507         }
3508
3509         for (i = 0; i < num_guc_variables; i++)
3510         {
3511                 struct config_generic *gconf = guc_variables[i];
3512                 int                     my_status = gconf->status;
3513                 GucStack   *stack = gconf->stack;
3514                 bool            useTentative;
3515                 bool            changed;
3516
3517                 /*
3518                  * Skip if nothing's happened to this var in this transaction
3519                  */
3520                 if (my_status == 0)
3521                 {
3522                         Assert(stack == NULL);
3523                         continue;
3524                 }
3525                 /* Assert that we stacked old value before changing it */
3526                 Assert(stack != NULL && (my_status & GUC_HAVE_STACK));
3527                 /* However, the last change may have been at an outer xact level */
3528                 if (stack->nest_level < nestLevel)
3529                         continue;
3530                 Assert(stack->nest_level == nestLevel);
3531
3532                 /*
3533                  * We will pop the stack entry.  Start by restoring outer xact status
3534                  * (since we may want to modify it below).      Be careful to use
3535                  * my_status to reference the inner xact status below this point...
3536                  */
3537                 gconf->status = stack->status;
3538
3539                 /*
3540                  * We have two cases:
3541                  *
3542                  * If commit and HAVE_TENTATIVE, set actual value to tentative (this
3543                  * is to override a SET LOCAL if one occurred later than SET). We keep
3544                  * the tentative value and propagate HAVE_TENTATIVE to the parent
3545                  * status, allowing the SET's effect to percolate up. (But if we're
3546                  * exiting the outermost transaction, we'll drop the HAVE_TENTATIVE
3547                  * bit below.)
3548                  *
3549                  * Otherwise, we have a transaction that aborted or executed only SET
3550                  * LOCAL (or no SET at all).  In either case it should have no further
3551                  * effect, so restore both tentative and actual values from the stack
3552                  * entry.
3553                  */
3554
3555                 useTentative = isCommit && (my_status & GUC_HAVE_TENTATIVE) != 0;
3556                 changed = false;
3557
3558                 switch (gconf->vartype)
3559                 {
3560                         case PGC_BOOL:
3561                                 {
3562                                         struct config_bool *conf = (struct config_bool *) gconf;
3563                                         bool            newval;
3564                                         GucSource       newsource;
3565
3566                                         if (useTentative)
3567                                         {
3568                                                 newval = conf->tentative_val;
3569                                                 newsource = conf->gen.tentative_source;
3570                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3571                                         }
3572                                         else
3573                                         {
3574                                                 newval = stack->value.boolval;
3575                                                 newsource = stack->source;
3576                                                 conf->tentative_val = stack->tentative_val.boolval;
3577                                                 conf->gen.tentative_source = stack->tentative_source;
3578                                         }
3579
3580                                         if (*conf->variable != newval)
3581                                         {
3582                                                 if (conf->assign_hook)
3583                                                         if (!(*conf->assign_hook) (newval,
3584                                                                                                            true, PGC_S_OVERRIDE))
3585                                                                 elog(LOG, "failed to commit %s",
3586                                                                          conf->gen.name);
3587                                                 *conf->variable = newval;
3588                                                 changed = true;
3589                                         }
3590                                         conf->gen.source = newsource;
3591                                         break;
3592                                 }
3593                         case PGC_INT:
3594                                 {
3595                                         struct config_int *conf = (struct config_int *) gconf;
3596                                         int                     newval;
3597                                         GucSource       newsource;
3598
3599                                         if (useTentative)
3600                                         {
3601                                                 newval = conf->tentative_val;
3602                                                 newsource = conf->gen.tentative_source;
3603                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3604                                         }
3605                                         else
3606                                         {
3607                                                 newval = stack->value.intval;
3608                                                 newsource = stack->source;
3609                                                 conf->tentative_val = stack->tentative_val.intval;
3610                                                 conf->gen.tentative_source = stack->tentative_source;
3611                                         }
3612
3613                                         if (*conf->variable != newval)
3614                                         {
3615                                                 if (conf->assign_hook)
3616                                                         if (!(*conf->assign_hook) (newval,
3617                                                                                                            true, PGC_S_OVERRIDE))
3618                                                                 elog(LOG, "failed to commit %s",
3619                                                                          conf->gen.name);
3620                                                 *conf->variable = newval;
3621                                                 changed = true;
3622                                         }
3623                                         conf->gen.source = newsource;
3624                                         break;
3625                                 }
3626                         case PGC_REAL:
3627                                 {
3628                                         struct config_real *conf = (struct config_real *) gconf;
3629                                         double          newval;
3630                                         GucSource       newsource;
3631
3632                                         if (useTentative)
3633                                         {
3634                                                 newval = conf->tentative_val;
3635                                                 newsource = conf->gen.tentative_source;
3636                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3637                                         }
3638                                         else
3639                                         {
3640                                                 newval = stack->value.realval;
3641                                                 newsource = stack->source;
3642                                                 conf->tentative_val = stack->tentative_val.realval;
3643                                                 conf->gen.tentative_source = stack->tentative_source;
3644                                         }
3645
3646                                         if (*conf->variable != newval)
3647                                         {
3648                                                 if (conf->assign_hook)
3649                                                         if (!(*conf->assign_hook) (newval,
3650                                                                                                            true, PGC_S_OVERRIDE))
3651                                                                 elog(LOG, "failed to commit %s",
3652                                                                          conf->gen.name);
3653                                                 *conf->variable = newval;
3654                                                 changed = true;
3655                                         }
3656                                         conf->gen.source = newsource;
3657                                         break;
3658                                 }
3659                         case PGC_STRING:
3660                                 {
3661                                         struct config_string *conf = (struct config_string *) gconf;
3662                                         char       *newval;
3663                                         GucSource       newsource;
3664
3665                                         if (useTentative)
3666                                         {
3667                                                 newval = conf->tentative_val;
3668                                                 newsource = conf->gen.tentative_source;
3669                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
3670                                         }
3671                                         else
3672                                         {
3673                                                 newval = stack->value.stringval;
3674                                                 newsource = stack->source;
3675                                                 set_string_field(conf, &conf->tentative_val,
3676                                                                                  stack->tentative_val.stringval);
3677                                                 conf->gen.tentative_source = stack->tentative_source;
3678                                         }
3679
3680                                         if (*conf->variable != newval)
3681                                         {
3682                                                 if (conf->assign_hook)
3683                                                 {
3684                                                         const char *newstr;
3685
3686                                                         newstr = (*conf->assign_hook) (newval, true,
3687                                                                                                                    PGC_S_OVERRIDE);
3688                                                         if (newstr == NULL)
3689                                                                 elog(LOG, "failed to commit %s",
3690                                                                          conf->gen.name);
3691                                                         else if (newstr != newval)
3692                                                         {
3693                                                                 /*
3694                                                                  * If newval should now be freed, it'll be
3695                                                                  * taken care of below.
3696                                                                  *
3697                                                                  * See notes in set_config_option about
3698                                                                  * casting
3699                                                                  */
3700                                                                 newval = (char *) newstr;
3701                                                         }
3702                                                 }
3703
3704                                                 set_string_field(conf, conf->variable, newval);
3705                                                 changed = true;
3706                                         }
3707                                         conf->gen.source = newsource;
3708                                         /* Release stacked values if not used anymore */
3709                                         set_string_field(conf, &stack->value.stringval,
3710                                                                          NULL);
3711                                         set_string_field(conf, &stack->tentative_val.stringval,
3712                                                                          NULL);
3713                                         /* Don't store tentative value separately after commit */
3714                                         if (nestLevel == 1)
3715                                                 set_string_field(conf, &conf->tentative_val, NULL);
3716                                         break;
3717                                 }
3718                 }
3719
3720                 /* Finish popping the state stack */
3721                 gconf->stack = stack->prev;
3722                 pfree(stack);
3723
3724                 /*
3725                  * If we're now out of all xact levels, forget TENTATIVE status bit;
3726                  * there's nothing tentative about the value anymore.
3727                  */
3728                 if (nestLevel == 1)
3729                 {
3730                         Assert(gconf->stack == NULL);
3731                         gconf->status = 0;
3732                 }
3733
3734                 /* Report new value if we changed it */
3735                 if (changed && (gconf->flags & GUC_REPORT))
3736                         ReportGUCOption(gconf);
3737         }
3738
3739         /*
3740          * If we're now out of all xact levels, we can clear guc_dirty. (Note: we
3741          * cannot reset guc_dirty when exiting a subtransaction, because we know
3742          * that all outer transaction levels will have stacked values to deal
3743          * with.)
3744          */
3745         if (nestLevel == 1)
3746                 guc_dirty = false;
3747
3748         /* Update nesting level */
3749         GUCNestLevel = nestLevel - 1;
3750 }
3751
3752
3753 /*
3754  * Start up automatic reporting of changes to variables marked GUC_REPORT.
3755  * This is executed at completion of backend startup.
3756  */
3757 void
3758 BeginReportingGUCOptions(void)
3759 {
3760         int                     i;
3761
3762         /*
3763          * Don't do anything unless talking to an interactive frontend of protocol
3764          * 3.0 or later.
3765          */
3766         if (whereToSendOutput != DestRemote ||
3767                 PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
3768                 return;
3769
3770         reporting_enabled = true;
3771
3772         /* Transmit initial values of interesting variables */
3773         for (i = 0; i < num_guc_variables; i++)
3774         {
3775                 struct config_generic *conf = guc_variables[i];
3776
3777                 if (conf->flags & GUC_REPORT)
3778                         ReportGUCOption(conf);
3779         }
3780 }
3781
3782 /*
3783  * ReportGUCOption: if appropriate, transmit option value to frontend
3784  */
3785 static void
3786 ReportGUCOption(struct config_generic * record)
3787 {
3788         if (reporting_enabled && (record->flags & GUC_REPORT))
3789         {
3790                 char       *val = _ShowOption(record, false);
3791                 StringInfoData msgbuf;
3792
3793                 pq_beginmessage(&msgbuf, 'S');
3794                 pq_sendstring(&msgbuf, record->name);
3795                 pq_sendstring(&msgbuf, val);
3796                 pq_endmessage(&msgbuf);
3797
3798                 pfree(val);
3799         }
3800 }
3801
3802
3803 /*
3804  * Try to interpret value as boolean value.  Valid values are: true,
3805  * false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
3806  * If the string parses okay, return true, else false.
3807  * If okay and result is not NULL, return the value in *result.
3808  */
3809 static bool
3810 parse_bool(const char *value, bool *result)
3811 {
3812         size_t          len = strlen(value);
3813
3814         if (pg_strncasecmp(value, "true", len) == 0)
3815         {
3816                 if (result)
3817                         *result = true;
3818         }
3819         else if (pg_strncasecmp(value, "false", len) == 0)
3820         {
3821                 if (result)
3822                         *result = false;
3823         }
3824
3825         else if (pg_strncasecmp(value, "yes", len) == 0)
3826         {
3827                 if (result)
3828                         *result = true;
3829         }
3830         else if (pg_strncasecmp(value, "no", len) == 0)
3831         {
3832                 if (result)
3833                         *result = false;
3834         }
3835
3836         /* 'o' is not unique enough */
3837         else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
3838         {
3839                 if (result)
3840                         *result = true;
3841         }
3842         else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
3843         {
3844                 if (result)
3845                         *result = false;
3846         }
3847
3848         else if (pg_strcasecmp(value, "1") == 0)
3849         {
3850                 if (result)
3851                         *result = true;
3852         }
3853         else if (pg_strcasecmp(value, "0") == 0)
3854         {
3855                 if (result)
3856                         *result = false;
3857         }
3858
3859         else
3860         {
3861                 if (result)
3862                         *result = false;        /* suppress compiler warning */
3863                 return false;
3864         }
3865         return true;
3866 }
3867
3868
3869
3870 /*
3871  * Try to parse value as an integer.  The accepted formats are the
3872  * usual decimal, octal, or hexadecimal formats, optionally followed by
3873  * a unit name if "flags" indicates a unit is allowed.
3874  *
3875  * If the string parses okay, return true, else false.
3876  * If okay and result is not NULL, return the value in *result.
3877  * If not okay and hintmsg is not NULL, *hintmsg is set to a suitable
3878  *      HINT message, or NULL if no hint provided.
3879  */
3880 static bool
3881 parse_int(const char *value, int *result, int flags, const char **hintmsg)
3882 {
3883         int64           val;
3884         char       *endptr;
3885
3886         /* To suppress compiler warnings, always set output params */
3887         if (result)
3888                 *result = 0;
3889         if (hintmsg)
3890                 *hintmsg = NULL;
3891
3892         /* We assume here that int64 is at least as wide as long */
3893         errno = 0;
3894         val = strtol(value, &endptr, 0);
3895
3896         if (endptr == value)
3897                 return false;                   /* no HINT for integer syntax error */
3898
3899         if (errno == ERANGE || val != (int64) ((int32) val))
3900         {
3901                 if (hintmsg)
3902                         *hintmsg = gettext_noop("Value exceeds integer range.");
3903                 return false;
3904         }
3905
3906         /* allow whitespace between integer and unit */
3907         while (isspace((unsigned char) *endptr))
3908                 endptr++;
3909
3910         /* Handle possible unit */
3911         if (*endptr != '\0')
3912         {
3913                 /*
3914                  * Note: the multiple-switch coding technique here is a bit tedious,
3915                  * but seems necessary to avoid intermediate-value overflows.
3916                  *
3917                  * If INT64_IS_BUSTED (ie, it's really int32) we will fail to detect
3918                  * overflow due to units conversion, but there are few enough such
3919                  * machines that it does not seem worth trying to be smarter.
3920                  */
3921                 if (flags & GUC_UNIT_MEMORY)
3922                 {
3923                         /* Set hint for use if no match or trailing garbage */
3924                         if (hintmsg)
3925                                 *hintmsg = gettext_noop("Valid units for this parameter are \"kB\", \"MB\", and \"GB\".");
3926
3927 #if BLCKSZ < 1024 || BLCKSZ > (1024*1024)
3928 #error BLCKSZ must be between 1KB and 1MB
3929 #endif
3930 #if XLOG_BLCKSZ < 1024 || XLOG_BLCKSZ > (1024*1024)
3931 #error XLOG_BLCKSZ must be between 1KB and 1MB
3932 #endif
3933
3934                         if (strncmp(endptr, "kB", 2) == 0)
3935                         {
3936                                 endptr += 2;
3937                                 switch (flags & GUC_UNIT_MEMORY)
3938                                 {
3939                                         case GUC_UNIT_BLOCKS:
3940                                                 val /= (BLCKSZ / 1024);
3941                                                 break;
3942                                         case GUC_UNIT_XBLOCKS:
3943                                                 val /= (XLOG_BLCKSZ / 1024);
3944                                                 break;
3945                                 }
3946                         }
3947                         else if (strncmp(endptr, "MB", 2) == 0)
3948                         {
3949                                 endptr += 2;
3950                                 switch (flags & GUC_UNIT_MEMORY)
3951                                 {
3952                                         case GUC_UNIT_KB:
3953                                                 val *= KB_PER_MB;
3954                                                 break;
3955                                         case GUC_UNIT_BLOCKS:
3956                                                 val *= KB_PER_MB / (BLCKSZ / 1024);
3957                                                 break;
3958                                         case GUC_UNIT_XBLOCKS:
3959                                                 val *= KB_PER_MB / (XLOG_BLCKSZ / 1024);
3960                                                 break;
3961                                 }
3962                         }
3963                         else if (strncmp(endptr, "GB", 2) == 0)
3964                         {
3965                                 endptr += 2;
3966                                 switch (flags & GUC_UNIT_MEMORY)
3967                                 {
3968                                         case GUC_UNIT_KB:
3969                                                 val *= KB_PER_GB;
3970                                                 break;
3971                                         case GUC_UNIT_BLOCKS:
3972                                                 val *= KB_PER_GB / (BLCKSZ / 1024);
3973                                                 break;
3974                                         case GUC_UNIT_XBLOCKS:
3975                                                 val *= KB_PER_GB / (XLOG_BLCKSZ / 1024);
3976                                                 break;
3977                                 }
3978                         }
3979                 }
3980                 else if (flags & GUC_UNIT_TIME)
3981                 {
3982                         /* Set hint for use if no match or trailing garbage */
3983                         if (hintmsg)
3984                                 *hintmsg = gettext_noop("Valid units for this parameter are \"ms\", \"s\", \"min\", \"h\", and \"d\".");
3985
3986                         if (strncmp(endptr, "ms", 2) == 0)
3987                         {
3988                                 endptr += 2;
3989                                 switch (flags & GUC_UNIT_TIME)
3990                                 {
3991                                         case GUC_UNIT_S:
3992                                                 val /= MS_PER_S;
3993                                                 break;
3994                                         case GUC_UNIT_MIN:
3995                                                 val /= MS_PER_MIN;
3996                                                 break;
3997                                 }
3998                         }
3999                         else if (strncmp(endptr, "s", 1) == 0)
4000                         {
4001                                 endptr += 1;
4002                                 switch (flags & GUC_UNIT_TIME)
4003                                 {
4004                                         case GUC_UNIT_MS:
4005                                                 val *= MS_PER_S;
4006                                                 break;
4007                                         case GUC_UNIT_MIN:
4008                                                 val /= S_PER_MIN;
4009                                                 break;
4010                                 }
4011                         }
4012                         else if (strncmp(endptr, "min", 3) == 0)
4013                         {
4014                                 endptr += 3;
4015                                 switch (flags & GUC_UNIT_TIME)
4016                                 {
4017                                         case GUC_UNIT_MS:
4018                                                 val *= MS_PER_MIN;
4019                                                 break;
4020                                         case GUC_UNIT_S:
4021                                                 val *= S_PER_MIN;
4022                                                 break;
4023                                 }
4024                         }
4025                         else if (strncmp(endptr, "h", 1) == 0)
4026                         {
4027                                 endptr += 1;
4028                                 switch (flags & GUC_UNIT_TIME)
4029                                 {
4030                                         case GUC_UNIT_MS:
4031                                                 val *= MS_PER_H;
4032                                                 break;
4033                                         case GUC_UNIT_S:
4034                                                 val *= S_PER_H;
4035                                                 break;
4036                                         case GUC_UNIT_MIN:
4037                                                 val *= MIN_PER_H;
4038                                                 break;
4039                                 }
4040                         }
4041                         else if (strncmp(endptr, "d", 1) == 0)
4042                         {
4043                                 endptr += 1;
4044                                 switch (flags & GUC_UNIT_TIME)
4045                                 {
4046                                         case GUC_UNIT_MS:
4047                                                 val *= MS_PER_D;
4048                                                 break;
4049                                         case GUC_UNIT_S:
4050                                                 val *= S_PER_D;
4051                                                 break;
4052                                         case GUC_UNIT_MIN:
4053                                                 val *= MIN_PER_D;
4054                                                 break;
4055                                 }
4056                         }
4057                 }
4058
4059                 /* allow whitespace after unit */
4060                 while (isspace((unsigned char) *endptr))
4061                         endptr++;
4062
4063                 if (*endptr != '\0')
4064                         return false;           /* appropriate hint, if any, already set */
4065
4066                 /* Check for overflow due to units conversion */
4067                 if (val != (int64) ((int32) val))
4068                 {
4069                         if (hintmsg)
4070                                 *hintmsg = gettext_noop("Value exceeds integer range.");
4071                         return false;
4072                 }
4073         }
4074
4075         if (result)
4076                 *result = (int) val;
4077         return true;
4078 }
4079
4080
4081
4082 /*
4083  * Try to parse value as a floating point number in the usual format.
4084  * If the string parses okay, return true, else false.
4085  * If okay and result is not NULL, return the value in *result.
4086  */
4087 static bool
4088 parse_real(const char *value, double *result)
4089 {
4090         double          val;
4091         char       *endptr;
4092
4093         if (result)
4094                 *result = 0;                    /* suppress compiler warning */
4095
4096         errno = 0;
4097         val = strtod(value, &endptr);
4098         if (endptr == value || errno == ERANGE)
4099                 return false;
4100
4101         /* allow whitespace after number */
4102         while (isspace((unsigned char) *endptr))
4103                 endptr++;
4104         if (*endptr != '\0')
4105                 return false;
4106
4107         if (result)
4108                 *result = val;
4109         return true;
4110 }
4111
4112
4113 /*
4114  * Call a GucStringAssignHook function, being careful to free the
4115  * "newval" string if the hook ereports.
4116  *
4117  * This is split out of set_config_option just to avoid the "volatile"
4118  * qualifiers that would otherwise have to be plastered all over.
4119  */
4120 static const char *
4121 call_string_assign_hook(GucStringAssignHook assign_hook,
4122                                                 char *newval, bool doit, GucSource source)
4123 {
4124         const char *result;
4125
4126         PG_TRY();
4127         {
4128                 result = (*assign_hook) (newval, doit, source);
4129         }
4130         PG_CATCH();
4131         {
4132                 free(newval);
4133                 PG_RE_THROW();
4134         }
4135         PG_END_TRY();
4136
4137         return result;
4138 }
4139
4140
4141 /*
4142  * Sets option `name' to given value. The value should be a string
4143  * which is going to be parsed and converted to the appropriate data
4144  * type.  The context and source parameters indicate in which context this
4145  * function is being called so it can apply the access restrictions
4146  * properly.
4147  *
4148  * If value is NULL, set the option to its default value. If the
4149  * parameter changeVal is false then don't really set the option but do all
4150  * the checks to see if it would work.
4151  *
4152  * If there is an error (non-existing option, invalid value) then an
4153  * ereport(ERROR) is thrown *unless* this is called in a context where we
4154  * don't want to ereport (currently, startup or SIGHUP config file reread).
4155  * In that case we write a suitable error message via ereport(DEBUG) and
4156  * return false. This is working around the deficiencies in the ereport
4157  * mechanism, so don't blame me.  In all other cases, the function
4158  * returns true, including cases where the input is valid but we chose
4159  * not to apply it because of context or source-priority considerations.
4160  *
4161  * See also SetConfigOption for an external interface.
4162  */
4163 bool
4164 set_config_option(const char *name, const char *value,
4165                                   GucContext context, GucSource source,
4166                                   bool isLocal, bool changeVal)
4167 {
4168         struct config_generic *record;
4169         int                     elevel;
4170         bool            makeDefault;
4171
4172         if (context == PGC_SIGHUP || source == PGC_S_DEFAULT)
4173         {
4174                 /*
4175                  * To avoid cluttering the log, only the postmaster bleats loudly
4176                  * about problems with the config file.
4177                  */
4178                 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
4179         }
4180         else if (source == PGC_S_DATABASE || source == PGC_S_USER)
4181                 elevel = INFO;
4182         else
4183                 elevel = ERROR;
4184
4185         record = find_option(name, elevel);
4186         if (record == NULL)
4187         {
4188                 ereport(elevel,
4189                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4190                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4191                 return false;
4192         }
4193
4194         /*
4195          * Do not replace a value that has been set on the command line by a SIGHUP
4196          * reload
4197          */
4198         if (context == PGC_SIGHUP && record->source == PGC_S_ARGV)
4199                 return true;
4200
4201         /*
4202          * Check if the option can be set at this time. See guc.h for the precise
4203          * rules. Note that we don't want to throw errors if we're in the SIGHUP
4204          * context. In that case we just ignore the attempt and return true.
4205          */
4206         switch (record->context)
4207         {
4208                 case PGC_INTERNAL:
4209                         if (context == PGC_SIGHUP)
4210                                 return true;
4211                         if (context != PGC_INTERNAL)
4212                         {
4213                                 ereport(elevel,
4214                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4215                                                  errmsg("parameter \"%s\" cannot be changed",
4216                                                                 name)));
4217                                 return false;
4218                         }
4219                         break;
4220                 case PGC_POSTMASTER:
4221                         if (context == PGC_SIGHUP)
4222                         {
4223                                 if (changeVal && !is_newvalue_equal(record, value))
4224                                         ereport(elevel,
4225                                                         (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4226                                                          errmsg("parameter \"%s\" cannot be changed after server start; configuration file change ignored",
4227                                                                         name)));
4228
4229                                 return true;
4230                         }
4231                         if (context != PGC_POSTMASTER)
4232                         {
4233                                 ereport(elevel,
4234                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4235                                                  errmsg("parameter \"%s\" cannot be changed after server start",
4236                                                                 name)));
4237                                 return false;
4238                         }
4239                         break;
4240                 case PGC_SIGHUP:
4241                         if (context != PGC_SIGHUP && context != PGC_POSTMASTER)
4242                         {
4243                                 ereport(elevel,
4244                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4245                                                  errmsg("parameter \"%s\" cannot be changed now",
4246                                                                 name)));
4247                                 return false;
4248                         }
4249
4250                         /*
4251                          * Hmm, the idea of the SIGHUP context is "ought to be global, but
4252                          * can be changed after postmaster start". But there's nothing
4253                          * that prevents a crafty administrator from sending SIGHUP
4254                          * signals to individual backends only.
4255                          */
4256                         break;
4257                 case PGC_BACKEND:
4258                         if (context == PGC_SIGHUP)
4259                         {
4260                                 /*
4261                                  * If a PGC_BACKEND parameter is changed in the config file,
4262                                  * we want to accept the new value in the postmaster (whence
4263                                  * it will propagate to subsequently-started backends), but
4264                                  * ignore it in existing backends.      This is a tad klugy, but
4265                                  * necessary because we don't re-read the config file during
4266                                  * backend start.
4267                                  */
4268                                 if (IsUnderPostmaster)
4269                                         return true;
4270                         }
4271                         else if (context != PGC_BACKEND && context != PGC_POSTMASTER)
4272                         {
4273                                 ereport(elevel,
4274                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4275                                                  errmsg("parameter \"%s\" cannot be set after connection start",
4276                                                                 name)));
4277                                 return false;
4278                         }
4279                         break;
4280                 case PGC_SUSET:
4281                         if (context == PGC_USERSET || context == PGC_BACKEND)
4282                         {
4283                                 ereport(elevel,
4284                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4285                                                  errmsg("permission denied to set parameter \"%s\"",
4286                                                                 name)));
4287                                 return false;
4288                         }
4289                         break;
4290                 case PGC_USERSET:
4291                         /* always okay */
4292                         break;
4293         }
4294
4295         /*
4296          * Should we set reset/stacked values?  (If so, the behavior is not
4297          * transactional.)  This is done either when we get a default
4298          * value from the database's/user's/client's default settings or
4299          * when we reset a value to its default.
4300          */
4301         makeDefault = changeVal && (source <= PGC_S_OVERRIDE)
4302                                         && ((value != NULL) || source == PGC_S_DEFAULT);
4303
4304         /*
4305          * Ignore attempted set if overridden by previously processed
4306          * setting.  However, if changeVal is false then plow ahead anyway
4307          * since we are trying to find out if the value is potentially
4308          * good, not actually use it.  Also keep going if makeDefault is
4309          * true, since we may want to set the reset/stacked values even if
4310          * we can't set the variable itself.  There's one exception to
4311          * this rule: if we want to apply the default value to variables
4312          * that were removed from the configuration file.  This is
4313          * indicated by source == PGC_S_DEFAULT and context == PGC_SIGHUP.
4314          */
4315         if (record->source > source
4316                 && !(source == PGC_S_DEFAULT && context == PGC_SIGHUP))
4317         {
4318                 if (changeVal && !makeDefault)
4319                 {
4320                         elog(DEBUG3, "\"%s\": setting ignored because previous source is higher priority",
4321                                  name);
4322                         return true;
4323                 }
4324                 changeVal = false;
4325         }
4326
4327         /*
4328          * Evaluate value and set variable.
4329          */
4330         switch (record->vartype)
4331         {
4332                 case PGC_BOOL:
4333                         {
4334                                 struct config_bool *conf = (struct config_bool *) record;
4335                                 bool            newval;
4336
4337                                 if (value)
4338                                 {
4339                                         if (!parse_bool(value, &newval))
4340                                         {
4341                                                 ereport(elevel,
4342                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4343                                                   errmsg("parameter \"%s\" requires a Boolean value",
4344                                                                  name)));
4345                                                 return false;
4346                                         }
4347                                 }
4348                                 /*
4349                                  * If value == NULL and source == PGC_S_DEFAULT then
4350                                  * we reset some value to its default (removed from
4351                                  * configuration file).
4352                                  */
4353                                 else if (source == PGC_S_DEFAULT)
4354                                         newval = conf->boot_val;
4355                                 /* else we handle a "RESET varname" command */
4356                                 else
4357                                 {
4358                                         newval = conf->reset_val;
4359                                         source = conf->gen.reset_source;
4360                                 }
4361
4362                                 if (conf->assign_hook)
4363                                         if (!(*conf->assign_hook) (newval, changeVal, source))
4364                                         {
4365                                                 ereport(elevel,
4366                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4367                                                          errmsg("invalid value for parameter \"%s\": %d",
4368                                                                         name, (int) newval)));
4369                                                 return false;
4370                                         }
4371
4372                                 if (changeVal || makeDefault)
4373                                 {
4374                                         /* Save old value to support transaction abort */
4375                                         if (!makeDefault)
4376                                                 push_old_value(&conf->gen);
4377                                         if (changeVal)
4378                                         {
4379                                                 *conf->variable = newval;
4380                                                 conf->gen.source = source;
4381                                         }
4382                                         if (makeDefault)
4383                                         {
4384                                                 GucStack   *stack;
4385
4386                                                 if (conf->gen.reset_source <= source)
4387                                                 {
4388                                                         conf->reset_val = newval;
4389                                                         conf->gen.reset_source = source;
4390                                                 }
4391                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4392                                                 {
4393                                                         if (stack->source <= source)
4394                                                         {
4395                                                                 stack->value.boolval = newval;
4396                                                                 stack->source = source;
4397                                                         }
4398                                                 }
4399                                         }
4400                                         else if (isLocal)
4401                                         {
4402                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4403                                                 guc_dirty = true;
4404                                         }
4405                                         else
4406                                         {
4407                                                 conf->tentative_val = newval;
4408                                                 conf->gen.tentative_source = source;
4409                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4410                                                 guc_dirty = true;
4411                                         }
4412                                 }
4413                                 break;
4414                         }
4415
4416                 case PGC_INT:
4417                         {
4418                                 struct config_int *conf = (struct config_int *) record;
4419                                 int                     newval;
4420
4421                                 if (value)
4422                                 {
4423                                         const char *hintmsg;
4424
4425                                         if (!parse_int(value, &newval, conf->gen.flags, &hintmsg))
4426                                         {
4427                                                 ereport(elevel,
4428                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4429                                                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
4430                                                                                 name, value),
4431                                                                  hintmsg ? errhint(hintmsg) : 0));
4432                                                 return false;
4433                                         }
4434                                         if (newval < conf->min || newval > conf->max)
4435                                         {
4436                                                 ereport(elevel,
4437                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4438                                                                  errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)",
4439                                                                                 newval, name, conf->min, conf->max)));
4440                                                 return false;
4441                                         }
4442                                 }
4443                                 /*
4444                                  * If value == NULL and source == PGC_S_DEFAULT then
4445                                  * we reset some value to its default (removed from
4446                                  * configuration file).
4447                                  */
4448                                 else if (source == PGC_S_DEFAULT)
4449                                         newval = conf->boot_val;
4450                                 /* else we handle a "RESET varname" command */
4451                                 else
4452                                 {
4453                                         newval = conf->reset_val;
4454                                         source = conf->gen.reset_source;
4455                                 }
4456
4457                                 if (conf->assign_hook)
4458                                         if (!(*conf->assign_hook) (newval, changeVal, source))
4459                                         {
4460                                                 ereport(elevel,
4461                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4462                                                          errmsg("invalid value for parameter \"%s\": %d",
4463                                                                         name, newval)));
4464                                                 return false;
4465                                         }
4466
4467                                 if (changeVal || makeDefault)
4468                                 {
4469                                         /* Save old value to support transaction abort */
4470                                         if (!makeDefault)
4471                                                 push_old_value(&conf->gen);
4472                                         if (changeVal)
4473                                         {
4474                                                 *conf->variable = newval;
4475                                                 conf->gen.source = source;
4476                                         }
4477                                         if (makeDefault)
4478                                         {
4479                                                 GucStack   *stack;
4480
4481                                                 if (conf->gen.reset_source <= source)
4482                                                 {
4483                                                         conf->reset_val = newval;
4484                                                         conf->gen.reset_source = source;
4485                                                 }
4486                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4487                                                 {
4488                                                         if (stack->source <= source)
4489                                                         {
4490                                                                 stack->value.intval = newval;
4491                                                                 stack->source = source;
4492                                                         }
4493                                                 }
4494                                         }
4495                                         else if (isLocal)
4496                                         {
4497                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4498                                                 guc_dirty = true;
4499                                         }
4500                                         else
4501                                         {
4502                                                 conf->tentative_val = newval;
4503                                                 conf->gen.tentative_source = source;
4504                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4505                                                 guc_dirty = true;
4506                                         }
4507                                 }
4508                                 break;
4509                         }
4510
4511                 case PGC_REAL:
4512                         {
4513                                 struct config_real *conf = (struct config_real *) record;
4514                                 double          newval;
4515
4516                                 if (value)
4517                                 {
4518                                         if (!parse_real(value, &newval))
4519                                         {
4520                                                 ereport(elevel,
4521                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4522                                                   errmsg("parameter \"%s\" requires a numeric value",
4523                                                                  name)));
4524                                                 return false;
4525                                         }
4526                                         if (newval < conf->min || newval > conf->max)
4527                                         {
4528                                                 ereport(elevel,
4529                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4530                                                                  errmsg("%g is outside the valid range for parameter \"%s\" (%g .. %g)",
4531                                                                                 newval, name, conf->min, conf->max)));
4532                                                 return false;
4533                                         }
4534                                 }
4535                                 /*
4536                                  * If value == NULL and source == PGC_S_DEFAULT then
4537                                  * we reset some value to its default (removed from
4538                                  * configuration file).
4539                                  */
4540                                 else if (source == PGC_S_DEFAULT)
4541                                         newval = conf->boot_val;
4542                                 /* else we handle a "RESET varname" command */
4543                                 else
4544                                 {
4545                                         newval = conf->reset_val;
4546                                         source = conf->gen.reset_source;
4547                                 }
4548
4549                                 if (conf->assign_hook)
4550                                         if (!(*conf->assign_hook) (newval, changeVal, source))
4551                                         {
4552                                                 ereport(elevel,
4553                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4554                                                          errmsg("invalid value for parameter \"%s\": %g",
4555                                                                         name, newval)));
4556                                                 return false;
4557                                         }
4558
4559                                 if (changeVal || makeDefault)
4560                                 {
4561                                         /* Save old value to support transaction abort */
4562                                         if (!makeDefault)
4563                                                 push_old_value(&conf->gen);
4564                                         if (changeVal)
4565                                         {
4566                                                 *conf->variable = newval;
4567                                                 conf->gen.source = source;
4568                                         }
4569                                         if (makeDefault)
4570                                         {
4571                                                 GucStack   *stack;
4572
4573                                                 if (conf->gen.reset_source <= source)
4574                                                 {
4575                                                         conf->reset_val = newval;
4576                                                         conf->gen.reset_source = source;
4577                                                 }
4578                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4579                                                 {
4580                                                         if (stack->source <= source)
4581                                                         {
4582                                                                 stack->value.realval = newval;
4583                                                                 stack->source = source;
4584                                                         }
4585                                                 }
4586                                         }
4587                                         else if (isLocal)
4588                                         {
4589                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4590                                                 guc_dirty = true;
4591                                         }
4592                                         else
4593                                         {
4594                                                 conf->tentative_val = newval;
4595                                                 conf->gen.tentative_source = source;
4596                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4597                                                 guc_dirty = true;
4598                                         }
4599                                 }
4600                                 break;
4601                         }
4602
4603                 case PGC_STRING:
4604                         {
4605                                 struct config_string *conf = (struct config_string *) record;
4606                                 char       *newval;
4607
4608                                 if (value)
4609                                 {
4610                                         newval = guc_strdup(elevel, value);
4611                                         if (newval == NULL)
4612                                                 return false;
4613
4614                                         /*
4615                                          * The only sort of "parsing" check we need to do is apply
4616                                          * truncation if GUC_IS_NAME.
4617                                          */
4618                                         if (conf->gen.flags & GUC_IS_NAME)
4619                                                 truncate_identifier(newval, strlen(newval), true);
4620                                 }
4621                                 /*
4622                                  * If value == NULL and source == PGC_S_DEFAULT then
4623                                  * we reset some value to its default (removed from
4624                                  * configuration file).
4625                                  */
4626                                 else if (source == PGC_S_DEFAULT)
4627                                 {
4628                                         if (conf->boot_val == NULL)
4629                                                 newval = NULL;
4630                                         else
4631                                         {
4632                                                 newval = guc_strdup(elevel, conf->boot_val);
4633                                                 if (newval == NULL)
4634                                                         return false;
4635                                         }
4636                                 }
4637                                 /* else we handle a "RESET varname" command */
4638                                 else if (conf->reset_val)
4639                                 {
4640                                         /*
4641                                          * We could possibly avoid strdup here, but easier to make
4642                                          * this case work the same as the normal assignment case.
4643                                          */
4644                                         newval = guc_strdup(elevel, conf->reset_val);
4645                                         if (newval == NULL)
4646                                                 return false;
4647                                         source = conf->gen.reset_source;
4648                                 }
4649                                 else
4650                                 {
4651                                         /* Nothing to reset to, as yet; so do nothing */
4652                                         break;
4653                                 }
4654
4655                                 if (conf->assign_hook)
4656                                 {
4657                                         const char *hookresult;
4658
4659                                         /*
4660                                          * If the hook ereports, we have to make sure we free
4661                                          * newval, else it will be a permanent memory leak.
4662                                          */
4663                                         hookresult = call_string_assign_hook(conf->assign_hook,
4664                                                                                                                  newval,
4665                                                                                                                  changeVal,
4666                                                                                                                  source);
4667                                         if (hookresult == NULL)
4668                                         {
4669                                                 free(newval);
4670                                                 ereport(elevel,
4671                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4672                                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
4673                                                                 name, value ? value : "")));
4674                                                 return false;
4675                                         }
4676                                         else if (hookresult != newval)
4677                                         {
4678                                                 free(newval);
4679
4680                                                 /*
4681                                                  * Having to cast away const here is annoying, but the
4682                                                  * alternative is to declare assign_hooks as returning
4683                                                  * char*, which would mean they'd have to cast away
4684                                                  * const, or as both taking and returning char*, which
4685                                                  * doesn't seem attractive either --- we don't want
4686                                                  * them to scribble on the passed str.
4687                                                  */
4688                                                 newval = (char *) hookresult;
4689                                         }
4690                                 }
4691
4692                                 if (changeVal || makeDefault)
4693                                 {
4694                                         /* Save old value to support transaction abort */
4695                                         if (!makeDefault)
4696                                                 push_old_value(&conf->gen);
4697                                         if (changeVal)
4698                                         {
4699                                                 set_string_field(conf, conf->variable, newval);
4700                                                 conf->gen.source = source;
4701                                         }
4702                                         if (makeDefault)
4703                                         {
4704                                                 GucStack   *stack;
4705
4706                                                 if (conf->gen.reset_source <= source)
4707                                                 {
4708                                                         set_string_field(conf, &conf->reset_val, newval);
4709                                                         conf->gen.reset_source = source;
4710                                                 }
4711                                                 for (stack = conf->gen.stack; stack; stack = stack->prev)
4712                                                 {
4713                                                         if (stack->source <= source)
4714                                                         {
4715                                                                 set_string_field(conf, &stack->value.stringval,
4716                                                                                                  newval);
4717                                                                 stack->source = source;
4718                                                         }
4719                                                 }
4720                                                 /* Perhaps we didn't install newval anywhere */
4721                                                 if (!string_field_used(conf, newval))
4722                                                         free(newval);
4723                                         }
4724                                         else if (isLocal)
4725                                         {
4726                                                 conf->gen.status |= GUC_HAVE_LOCAL;
4727                                                 guc_dirty = true;
4728                                         }
4729                                         else
4730                                         {
4731                                                 set_string_field(conf, &conf->tentative_val, newval);
4732                                                 conf->gen.tentative_source = source;
4733                                                 conf->gen.status |= GUC_HAVE_TENTATIVE;
4734                                                 guc_dirty = true;
4735                                         }
4736                                 }
4737                                 else
4738                                         free(newval);
4739                                 break;
4740                         }
4741         }
4742
4743         if (changeVal && (record->flags & GUC_REPORT))
4744                 ReportGUCOption(record);
4745
4746         return true;
4747 }
4748
4749
4750 /*
4751  * Set a config option to the given value. See also set_config_option,
4752  * this is just the wrapper to be called from outside GUC.      NB: this
4753  * is used only for non-transactional operations.
4754  */
4755 void
4756 SetConfigOption(const char *name, const char *value,
4757                                 GucContext context, GucSource source)
4758 {
4759         (void) set_config_option(name, value, context, source, false, true);
4760 }
4761
4762
4763
4764 /*
4765  * Fetch the current value of the option `name'. If the option doesn't exist,
4766  * throw an ereport and don't return.
4767  *
4768  * The string is *not* allocated for modification and is really only
4769  * valid until the next call to configuration related functions.
4770  */
4771 const char *
4772 GetConfigOption(const char *name)
4773 {
4774         struct config_generic *record;
4775         static char buffer[256];
4776
4777         record = find_option(name, ERROR);
4778         if (record == NULL)
4779                 ereport(ERROR,
4780                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4781                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4782         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
4783                 ereport(ERROR,
4784                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4785                                  errmsg("must be superuser to examine \"%s\"", name)));
4786
4787         switch (record->vartype)
4788         {
4789                 case PGC_BOOL:
4790                         return *((struct config_bool *) record)->variable ? "on" : "off";
4791
4792                 case PGC_INT:
4793                         snprintf(buffer, sizeof(buffer), "%d",
4794                                          *((struct config_int *) record)->variable);
4795                         return buffer;
4796
4797                 case PGC_REAL:
4798                         snprintf(buffer, sizeof(buffer), "%g",
4799                                          *((struct config_real *) record)->variable);
4800                         return buffer;
4801
4802                 case PGC_STRING:
4803                         return *((struct config_string *) record)->variable;
4804         }
4805         return NULL;
4806 }
4807
4808 /*
4809  * Get the RESET value associated with the given option.
4810  */
4811 const char *
4812 GetConfigOptionResetString(const char *name)
4813 {
4814         struct config_generic *record;
4815         static char buffer[256];
4816
4817         record = find_option(name, ERROR);
4818         if (record == NULL)
4819                 ereport(ERROR,
4820                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4821                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4822         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
4823                 ereport(ERROR,
4824                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4825                                  errmsg("must be superuser to examine \"%s\"", name)));
4826
4827         switch (record->vartype)
4828         {
4829                 case PGC_BOOL:
4830                         return ((struct config_bool *) record)->reset_val ? "on" : "off";
4831
4832                 case PGC_INT:
4833                         snprintf(buffer, sizeof(buffer), "%d",
4834                                          ((struct config_int *) record)->reset_val);
4835                         return buffer;
4836
4837                 case PGC_REAL:
4838                         snprintf(buffer, sizeof(buffer), "%g",
4839                                          ((struct config_real *) record)->reset_val);
4840                         return buffer;
4841
4842                 case PGC_STRING:
4843                         return ((struct config_string *) record)->reset_val;
4844         }
4845         return NULL;
4846 }
4847
4848 /*
4849  * Detect whether the given configuration option can only be set by
4850  * a superuser.
4851  */
4852 bool
4853 IsSuperuserConfigOption(const char *name)
4854 {
4855         struct config_generic *record;
4856
4857         record = find_option(name, ERROR);
4858         /* On an unrecognized name, don't error, just return false. */
4859         if (record == NULL)
4860                 return false;
4861         return (record->context == PGC_SUSET);
4862 }
4863
4864
4865 /*
4866  * flatten_set_variable_args
4867  *              Given a parsenode List as emitted by the grammar for SET,
4868  *              convert to the flat string representation used by GUC.
4869  *
4870  * We need to be told the name of the variable the args are for, because
4871  * the flattening rules vary (ugh).
4872  *
4873  * The result is NULL if args is NIL (ie, SET ... TO DEFAULT), otherwise
4874  * a palloc'd string.
4875  */
4876 static char *
4877 flatten_set_variable_args(const char *name, List *args)
4878 {
4879         struct config_generic *record;
4880         int                     flags;
4881         StringInfoData buf;
4882         ListCell   *l;
4883
4884         /* Fast path if just DEFAULT */
4885         if (args == NIL)
4886                 return NULL;
4887
4888         /* Else get flags for the variable */
4889         record = find_option(name, ERROR);
4890         if (record == NULL)
4891                 ereport(ERROR,
4892                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
4893                            errmsg("unrecognized configuration parameter \"%s\"", name)));
4894
4895         flags = record->flags;
4896
4897         /* Complain if list input and non-list variable */
4898         if ((flags & GUC_LIST_INPUT) == 0 &&
4899                 list_length(args) != 1)
4900                 ereport(ERROR,
4901                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4902                                  errmsg("SET %s takes only one argument", name)));
4903
4904         initStringInfo(&buf);
4905
4906         foreach(l, args)
4907         {
4908                 A_Const    *arg = (A_Const *) lfirst(l);
4909                 char       *val;
4910
4911                 if (l != list_head(args))
4912                         appendStringInfo(&buf, ", ");
4913
4914                 if (!IsA(arg, A_Const))
4915                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(arg));
4916
4917                 switch (nodeTag(&arg->val))
4918                 {
4919                         case T_Integer:
4920                                 appendStringInfo(&buf, "%ld", intVal(&arg->val));
4921                                 break;
4922                         case T_Float:
4923                                 /* represented as a string, so just copy it */
4924                                 appendStringInfoString(&buf, strVal(&arg->val));
4925                                 break;
4926                         case T_String:
4927                                 val = strVal(&arg->val);
4928                                 if (arg->typename != NULL)
4929                                 {
4930                                         /*
4931                                          * Must be a ConstInterval argument for TIME ZONE. Coerce
4932                                          * to interval and back to normalize the value and account
4933                                          * for any typmod.
4934                                          */
4935                                         int32           typmod;
4936                                         Datum           interval;
4937                                         char       *intervalout;
4938
4939                                         typmod = typenameTypeMod(NULL, arg->typename, INTERVALOID);
4940
4941                                         interval =
4942                                                 DirectFunctionCall3(interval_in,
4943                                                                                         CStringGetDatum(val),
4944                                                                                         ObjectIdGetDatum(InvalidOid),
4945                                                                                         Int32GetDatum(typmod));
4946
4947                                         intervalout =
4948                                                 DatumGetCString(DirectFunctionCall1(interval_out,
4949                                                                                                                         interval));
4950                                         appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
4951                                 }
4952                                 else
4953                                 {
4954                                         /*
4955                                          * Plain string literal or identifier.  For quote mode,
4956                                          * quote it if it's not a vanilla identifier.
4957                                          */
4958                                         if (flags & GUC_LIST_QUOTE)
4959                                                 appendStringInfoString(&buf, quote_identifier(val));
4960                                         else
4961                                                 appendStringInfoString(&buf, val);
4962                                 }
4963                                 break;
4964                         default:
4965                                 elog(ERROR, "unrecognized node type: %d",
4966                                          (int) nodeTag(&arg->val));
4967                                 break;
4968                 }
4969         }
4970
4971         return buf.data;
4972 }
4973
4974
4975 /*
4976  * SET command
4977  */
4978 void
4979 ExecSetVariableStmt(VariableSetStmt *stmt)
4980 {
4981         switch (stmt->kind)
4982         {
4983                 case VAR_SET_VALUE:
4984                 case VAR_SET_CURRENT:
4985                         set_config_option(stmt->name,
4986                                                           ExtractSetVariableArgs(stmt),
4987                                                           (superuser() ? PGC_SUSET : PGC_USERSET),
4988                                                           PGC_S_SESSION,
4989                                                           stmt->is_local,
4990                                                           true);
4991                         break;
4992                 case VAR_SET_MULTI:
4993                         /*
4994                          * Special case for special SQL syntax that effectively sets
4995                          * more than one variable per statement.
4996                          */
4997                         if (strcmp(stmt->name, "TRANSACTION") == 0)
4998                         {
4999                                 ListCell   *head;
5000
5001                                 foreach(head, stmt->args)
5002                                 {
5003                                         DefElem    *item = (DefElem *) lfirst(head);
5004
5005                                         if (strcmp(item->defname, "transaction_isolation") == 0)
5006                                                 SetPGVariable("transaction_isolation",
5007                                                                           list_make1(item->arg), stmt->is_local);
5008                                         else if (strcmp(item->defname, "transaction_read_only") == 0)
5009                                                 SetPGVariable("transaction_read_only",
5010                                                                           list_make1(item->arg), stmt->is_local);
5011                                         else
5012                                                 elog(ERROR, "unexpected SET TRANSACTION element: %s",
5013                                                          item->defname);
5014                                 }
5015                         }
5016                         else if (strcmp(stmt->name, "SESSION CHARACTERISTICS") == 0)
5017                         {
5018                                 ListCell   *head;
5019
5020                                 foreach(head, stmt->args)
5021                                 {
5022                                         DefElem    *item = (DefElem *) lfirst(head);
5023
5024                                         if (strcmp(item->defname, "transaction_isolation") == 0)
5025                                                 SetPGVariable("default_transaction_isolation",
5026                                                                           list_make1(item->arg), stmt->is_local);
5027                                         else if (strcmp(item->defname, "transaction_read_only") == 0)
5028                                                 SetPGVariable("default_transaction_read_only",
5029                                                                           list_make1(item->arg), stmt->is_local);
5030                                         else
5031                                                 elog(ERROR, "unexpected SET SESSION element: %s",
5032                                                          item->defname);
5033                                 }
5034                         }
5035                         else
5036                                 elog(ERROR, "unexpected SET MULTI element: %s",
5037                                          stmt->name);
5038                         break;
5039                 case VAR_SET_DEFAULT:
5040                 case VAR_RESET:
5041                         set_config_option(stmt->name,
5042                                                           NULL,
5043                                                           (superuser() ? PGC_SUSET : PGC_USERSET),
5044                                                           PGC_S_SESSION,
5045                                                           stmt->is_local,
5046                                                           true);
5047                         break;
5048                 case VAR_RESET_ALL:
5049                         ResetAllOptions();
5050                         break;
5051         }
5052 }
5053
5054 /*
5055  * Get the value to assign for a VariableSetStmt, or NULL if it's RESET.
5056  * The result is palloc'd.
5057  *
5058  * This is exported for use by actions such as ALTER ROLE SET.
5059  */
5060 char *
5061 ExtractSetVariableArgs(VariableSetStmt *stmt)
5062 {
5063         switch (stmt->kind)
5064         {
5065                 case VAR_SET_VALUE:
5066                         return flatten_set_variable_args(stmt->name, stmt->args);
5067                 case VAR_SET_CURRENT:
5068                         return GetConfigOptionByName(stmt->name, NULL);
5069                 default:
5070                         return NULL;
5071         }
5072 }
5073
5074 /*
5075  * SetPGVariable - SET command exported as an easily-C-callable function.
5076  *
5077  * This provides access to SET TO value, as well as SET TO DEFAULT (expressed
5078  * by passing args == NIL), but not SET FROM CURRENT functionality.
5079  */
5080 void
5081 SetPGVariable(const char *name, List *args, bool is_local)
5082 {
5083         char       *argstring = flatten_set_variable_args(name, args);
5084
5085         /* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
5086         set_config_option(name,
5087                                           argstring,
5088                                           (superuser() ? PGC_SUSET : PGC_USERSET),
5089                                           PGC_S_SESSION,
5090                                           is_local,
5091                                           true);
5092 }
5093
5094 /*
5095  * SET command wrapped as a SQL callable function.
5096  */
5097 Datum
5098 set_config_by_name(PG_FUNCTION_ARGS)
5099 {
5100         char       *name;
5101         char       *value;
5102         char       *new_value;
5103         bool            is_local;
5104         text       *result_text;
5105
5106         if (PG_ARGISNULL(0))
5107                 ereport(ERROR,
5108                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5109                                  errmsg("SET requires parameter name")));
5110
5111         /* Get the GUC variable name */
5112         name = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
5113
5114         /* Get the desired value or set to NULL for a reset request */
5115         if (PG_ARGISNULL(1))
5116                 value = NULL;
5117         else
5118                 value = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(1)));
5119
5120         /*
5121          * Get the desired state of is_local. Default to false if provided value
5122          * is NULL
5123          */
5124         if (PG_ARGISNULL(2))
5125                 is_local = false;
5126         else
5127                 is_local = PG_GETARG_BOOL(2);
5128
5129         /* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
5130         set_config_option(name,
5131                                           value,
5132                                           (superuser() ? PGC_SUSET : PGC_USERSET),
5133                                           PGC_S_SESSION,
5134                                           is_local,
5135                                           true);
5136
5137         /* get the new current value */
5138         new_value = GetConfigOptionByName(name, NULL);
5139
5140         /* Convert return string to text */
5141         result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(new_value)));
5142
5143         /* return it */
5144         PG_RETURN_TEXT_P(result_text);
5145 }
5146
5147
5148 static void
5149 define_custom_variable(struct config_generic * variable)
5150 {
5151         const char *name = variable->name;
5152         const char **nameAddr = &name;
5153         const char *value;
5154         struct config_string *pHolder;
5155         struct config_generic **res = (struct config_generic **) bsearch(
5156                                                                                                                   (void *) &nameAddr,
5157                                                                                                           (void *) guc_variables,
5158                                                                                                                    num_guc_variables,
5159                                                                                          sizeof(struct config_generic *),
5160                                                                                                                         guc_var_compare);
5161
5162         if (res == NULL)
5163         {
5164                 add_guc_variable(variable, ERROR);
5165                 return;
5166         }
5167
5168         /*
5169          * This better be a placeholder
5170          */
5171         if (((*res)->flags & GUC_CUSTOM_PLACEHOLDER) == 0)
5172                 ereport(ERROR,
5173                                 (errcode(ERRCODE_INTERNAL_ERROR),
5174                                  errmsg("attempt to redefine parameter \"%s\"", name)));
5175
5176         Assert((*res)->vartype == PGC_STRING);
5177         pHolder = (struct config_string *) * res;
5178
5179         /* We have the same name, no sorting is necessary */
5180         *res = variable;
5181
5182         value = *pHolder->variable;
5183
5184         /*
5185          * Assign the string value stored in the placeholder to the real variable.
5186          *
5187          * XXX this is not really good enough --- it should be a nontransactional
5188          * assignment, since we don't want it to roll back if the current xact
5189          * fails later.
5190          */
5191         set_config_option(name, value,
5192                                           pHolder->gen.context, pHolder->gen.source,
5193                                           false, true);
5194
5195         /*
5196          * Free up as much as we conveniently can of the placeholder structure
5197          * (this neglects any stack items...)
5198          */
5199         set_string_field(pHolder, pHolder->variable, NULL);
5200         set_string_field(pHolder, &pHolder->reset_val, NULL);
5201         set_string_field(pHolder, &pHolder->tentative_val, NULL);
5202
5203         free(pHolder);
5204 }
5205
5206 static void
5207 init_custom_variable(struct config_generic * gen,
5208                                          const char *name,
5209                                          const char *short_desc,
5210                                          const char *long_desc,
5211                                          GucContext context,
5212                                          enum config_type type)
5213 {
5214         gen->name = guc_strdup(ERROR, name);
5215         gen->context = context;
5216         gen->group = CUSTOM_OPTIONS;
5217         gen->short_desc = short_desc;
5218         gen->long_desc = long_desc;
5219         gen->vartype = type;
5220 }
5221
5222 void
5223 DefineCustomBoolVariable(const char *name,
5224                                                  const char *short_desc,
5225                                                  const char *long_desc,
5226                                                  bool *valueAddr,
5227                                                  GucContext context,
5228                                                  GucBoolAssignHook assign_hook,
5229                                                  GucShowHook show_hook)
5230 {
5231         size_t          sz = sizeof(struct config_bool);
5232         struct config_bool *var = (struct config_bool *) guc_malloc(ERROR, sz);
5233
5234         memset(var, 0, sz);
5235         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_BOOL);
5236
5237         var->variable = valueAddr;
5238         var->reset_val = *valueAddr;
5239         var->assign_hook = assign_hook;
5240         var->show_hook = show_hook;
5241         define_custom_variable(&var->gen);
5242 }
5243
5244 void
5245 DefineCustomIntVariable(const char *name,
5246                                                 const char *short_desc,
5247                                                 const char *long_desc,
5248                                                 int *valueAddr,
5249                                                 int minValue,
5250                                                 int maxValue,
5251                                                 GucContext context,
5252                                                 GucIntAssignHook assign_hook,
5253                                                 GucShowHook show_hook)
5254 {
5255         size_t          sz = sizeof(struct config_int);
5256         struct config_int *var = (struct config_int *) guc_malloc(ERROR, sz);
5257
5258         memset(var, 0, sz);
5259         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_INT);
5260
5261         var->variable = valueAddr;
5262         var->reset_val = *valueAddr;
5263         var->min = minValue;
5264         var->max = maxValue;
5265         var->assign_hook = assign_hook;
5266         var->show_hook = show_hook;
5267         define_custom_variable(&var->gen);
5268 }
5269
5270 void
5271 DefineCustomRealVariable(const char *name,
5272                                                  const char *short_desc,
5273                                                  const char *long_desc,
5274                                                  double *valueAddr,
5275                                                  double minValue,
5276                                                  double maxValue,
5277                                                  GucContext context,
5278                                                  GucRealAssignHook assign_hook,
5279                                                  GucShowHook show_hook)
5280 {
5281         size_t          sz = sizeof(struct config_real);
5282         struct config_real *var = (struct config_real *) guc_malloc(ERROR, sz);
5283
5284         memset(var, 0, sz);
5285         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_REAL);
5286
5287         var->variable = valueAddr;
5288         var->reset_val = *valueAddr;
5289         var->min = minValue;
5290         var->max = maxValue;
5291         var->assign_hook = assign_hook;
5292         var->show_hook = show_hook;
5293         define_custom_variable(&var->gen);
5294 }
5295
5296 void
5297 DefineCustomStringVariable(const char *name,
5298                                                    const char *short_desc,
5299                                                    const char *long_desc,
5300                                                    char **valueAddr,
5301                                                    GucContext context,
5302                                                    GucStringAssignHook assign_hook,
5303                                                    GucShowHook show_hook)
5304 {
5305         size_t          sz = sizeof(struct config_string);
5306         struct config_string *var = (struct config_string *) guc_malloc(ERROR, sz);
5307
5308         memset(var, 0, sz);
5309         init_custom_variable(&var->gen, name, short_desc, long_desc, context, PGC_STRING);
5310
5311         var->variable = valueAddr;
5312         var->reset_val = *valueAddr;
5313         var->assign_hook = assign_hook;
5314         var->show_hook = show_hook;
5315         define_custom_variable(&var->gen);
5316 }
5317
5318 void
5319 EmitWarningsOnPlaceholders(const char *className)
5320 {
5321         struct config_generic **vars = guc_variables;
5322         struct config_generic **last = vars + num_guc_variables;
5323
5324         int                     nameLen = strlen(className);
5325
5326         while (vars < last)
5327         {
5328                 struct config_generic *var = *vars++;
5329
5330                 if ((var->flags & GUC_CUSTOM_PLACEHOLDER) != 0 &&
5331                         strncmp(className, var->name, nameLen) == 0 &&
5332                         var->name[nameLen] == GUC_QUALIFIER_SEPARATOR)
5333                 {
5334                         ereport(INFO,
5335                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
5336                                          errmsg("unrecognized configuration parameter \"%s\"", var->name)));
5337                 }
5338         }
5339 }
5340
5341
5342 /*
5343  * SHOW command
5344  */
5345 void
5346 GetPGVariable(const char *name, DestReceiver *dest)
5347 {
5348         if (pg_strcasecmp(name, "all") == 0)
5349                 ShowAllGUCConfig(dest);
5350         else
5351                 ShowGUCConfigOption(name, dest);
5352 }
5353
5354 TupleDesc
5355 GetPGVariableResultDesc(const char *name)
5356 {
5357         TupleDesc       tupdesc;
5358
5359         if (pg_strcasecmp(name, "all") == 0)
5360         {
5361                 /* need a tuple descriptor representing three TEXT columns */
5362                 tupdesc = CreateTemplateTupleDesc(3, false);
5363                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
5364                                                    TEXTOID, -1, 0);
5365                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
5366                                                    TEXTOID, -1, 0);
5367                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
5368                                                    TEXTOID, -1, 0);
5369
5370         }
5371         else
5372         {
5373                 const char *varname;
5374
5375                 /* Get the canonical spelling of name */
5376                 (void) GetConfigOptionByName(name, &varname);
5377
5378                 /* need a tuple descriptor representing a single TEXT column */
5379                 tupdesc = CreateTemplateTupleDesc(1, false);
5380                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
5381                                                    TEXTOID, -1, 0);
5382         }
5383         return tupdesc;
5384 }
5385
5386
5387 /*
5388  * SHOW command
5389  */
5390 static void
5391 ShowGUCConfigOption(const char *name, DestReceiver *dest)
5392 {
5393         TupOutputState *tstate;
5394         TupleDesc       tupdesc;
5395         const char *varname;
5396         char       *value;
5397
5398         /* Get the value and canonical spelling of name */
5399         value = GetConfigOptionByName(name, &varname);
5400
5401         /* need a tuple descriptor representing a single TEXT column */
5402         tupdesc = CreateTemplateTupleDesc(1, false);
5403         TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
5404                                            TEXTOID, -1, 0);
5405
5406         /* prepare for projection of tuples */
5407         tstate = begin_tup_output_tupdesc(dest, tupdesc);
5408
5409         /* Send it */
5410         do_text_output_oneline(tstate, value);
5411
5412         end_tup_output(tstate);
5413 }
5414
5415 /*
5416  * SHOW ALL command
5417  */
5418 static void
5419 ShowAllGUCConfig(DestReceiver *dest)
5420 {
5421         bool            am_superuser = superuser();
5422         int                     i;
5423         TupOutputState *tstate;
5424         TupleDesc       tupdesc;
5425         char       *values[3];
5426
5427         /* need a tuple descriptor representing three TEXT columns */
5428         tupdesc = CreateTemplateTupleDesc(3, false);
5429         TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
5430                                            TEXTOID, -1, 0);
5431         TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
5432                                            TEXTOID, -1, 0);
5433         TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
5434                                            TEXTOID, -1, 0);
5435
5436
5437         /* prepare for projection of tuples */
5438         tstate = begin_tup_output_tupdesc(dest, tupdesc);
5439
5440         for (i = 0; i < num_guc_variables; i++)
5441         {
5442                 struct config_generic *conf = guc_variables[i];
5443
5444                 if ((conf->flags & GUC_NO_SHOW_ALL) ||
5445                         ((conf->flags & GUC_SUPERUSER_ONLY) && !am_superuser))
5446                         continue;
5447
5448                 /* assign to the values array */
5449                 values[0] = (char *) conf->name;
5450                 values[1] = _ShowOption(conf, true);
5451                 values[2] = (char *) conf->short_desc;
5452
5453                 /* send it to dest */
5454                 do_tup_output(tstate, values);
5455
5456                 /* clean up */
5457                 if (values[1] != NULL)
5458                         pfree(values[1]);
5459         }
5460
5461         end_tup_output(tstate);
5462 }
5463
5464 /*
5465  * Return GUC variable value by name; optionally return canonical
5466  * form of name.  Return value is palloc'd.
5467  */
5468 char *
5469 GetConfigOptionByName(const char *name, const char **varname)
5470 {
5471         struct config_generic *record;
5472
5473         record = find_option(name, ERROR);
5474         if (record == NULL)
5475                 ereport(ERROR,
5476                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
5477                            errmsg("unrecognized configuration parameter \"%s\"", name)));
5478         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
5479                 ereport(ERROR,
5480                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5481                                  errmsg("must be superuser to examine \"%s\"", name)));
5482
5483         if (varname)
5484                 *varname = record->name;
5485
5486         return _ShowOption(record, true);
5487 }
5488
5489 /*
5490  * Return GUC variable value by variable number; optionally return canonical
5491  * form of name.  Return value is palloc'd.
5492  */
5493 void
5494 GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
5495 {
5496         char            buffer[256];
5497         struct config_generic *conf;
5498
5499         /* check requested variable number valid */
5500         Assert((varnum >= 0) && (varnum < num_guc_variables));
5501
5502         conf = guc_variables[varnum];
5503
5504         if (noshow)
5505         {
5506                 if ((conf->flags & GUC_NO_SHOW_ALL) ||
5507                         ((conf->flags & GUC_SUPERUSER_ONLY) && !superuser()))
5508                         *noshow = true;
5509                 else
5510                         *noshow = false;
5511         }
5512
5513         /* first get the generic attributes */
5514
5515         /* name */
5516         values[0] = conf->name;
5517
5518         /* setting : use _ShowOption in order to avoid duplicating the logic */
5519         values[1] = _ShowOption(conf, false);
5520
5521         /* unit */
5522         if (conf->vartype == PGC_INT)
5523         {
5524                 static char buf[8];
5525
5526                 switch (conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
5527                 {
5528                         case GUC_UNIT_KB:
5529                                 values[2] = "kB";
5530                                 break;
5531                         case GUC_UNIT_BLOCKS:
5532                                 snprintf(buf, sizeof(buf), "%dkB", BLCKSZ / 1024);
5533                                 values[2] = buf;
5534                                 break;
5535                         case GUC_UNIT_XBLOCKS:
5536                                 snprintf(buf, sizeof(buf), "%dkB", XLOG_BLCKSZ / 1024);
5537                                 values[2] = buf;
5538                                 break;
5539                         case GUC_UNIT_MS:
5540                                 values[2] = "ms";
5541                                 break;
5542                         case GUC_UNIT_S:
5543                                 values[2] = "s";
5544                                 break;
5545                         case GUC_UNIT_MIN:
5546                                 values[2] = "min";
5547                                 break;
5548                         default:
5549                                 values[2] = "";
5550                                 break;
5551                 }
5552         }
5553         else
5554                 values[2] = NULL;
5555
5556         /* group */
5557         values[3] = config_group_names[conf->group];
5558
5559         /* short_desc */
5560         values[4] = conf->short_desc;
5561
5562         /* extra_desc */
5563         values[5] = conf->long_desc;
5564
5565         /* context */
5566         values[6] = GucContext_Names[conf->context];
5567
5568         /* vartype */
5569         values[7] = config_type_names[conf->vartype];
5570
5571         /* source */
5572         values[8] = GucSource_Names[conf->source];
5573
5574         /* now get the type specifc attributes */
5575         switch (conf->vartype)
5576         {
5577                 case PGC_BOOL:
5578                         {
5579                                 /* min_val */
5580                                 values[9] = NULL;
5581
5582                                 /* max_val */
5583                                 values[10] = NULL;
5584                         }
5585                         break;
5586
5587                 case PGC_INT:
5588                         {
5589                                 struct config_int *lconf = (struct config_int *) conf;
5590
5591                                 /* min_val */
5592                                 snprintf(buffer, sizeof(buffer), "%d", lconf->min);
5593                                 values[9] = pstrdup(buffer);
5594
5595                                 /* max_val */
5596                                 snprintf(buffer, sizeof(buffer), "%d", lconf->max);
5597                                 values[10] = pstrdup(buffer);
5598                         }
5599                         break;
5600
5601                 case PGC_REAL:
5602                         {
5603                                 struct config_real *lconf = (struct config_real *) conf;
5604
5605                                 /* min_val */
5606                                 snprintf(buffer, sizeof(buffer), "%g", lconf->min);
5607                                 values[9] = pstrdup(buffer);
5608
5609                                 /* max_val */
5610                                 snprintf(buffer, sizeof(buffer), "%g", lconf->max);
5611                                 values[10] = pstrdup(buffer);
5612                         }
5613                         break;
5614
5615                 case PGC_STRING:
5616                         {
5617                                 /* min_val */
5618                                 values[9] = NULL;
5619
5620                                 /* max_val */
5621                                 values[10] = NULL;
5622                         }
5623                         break;
5624
5625                 default:
5626                         {
5627                                 /*
5628                                  * should never get here, but in case we do, set 'em to NULL
5629                                  */
5630
5631                                 /* min_val */
5632                                 values[9] = NULL;
5633
5634                                 /* max_val */
5635                                 values[10] = NULL;
5636                         }
5637                         break;
5638         }
5639 }
5640
5641 /*
5642  * Return the total number of GUC variables
5643  */
5644 int
5645 GetNumConfigOptions(void)
5646 {
5647         return num_guc_variables;
5648 }
5649
5650 /*
5651  * show_config_by_name - equiv to SHOW X command but implemented as
5652  * a function.
5653  */
5654 Datum
5655 show_config_by_name(PG_FUNCTION_ARGS)
5656 {
5657         char       *varname;
5658         char       *varval;
5659         text       *result_text;
5660
5661         /* Get the GUC variable name */
5662         varname = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
5663
5664         /* Get the value */
5665         varval = GetConfigOptionByName(varname, NULL);
5666
5667         /* Convert to text */
5668         result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(varval)));
5669
5670         /* return it */
5671         PG_RETURN_TEXT_P(result_text);
5672 }
5673
5674 /*
5675  * show_all_settings - equiv to SHOW ALL command but implemented as
5676  * a Table Function.
5677  */
5678 #define NUM_PG_SETTINGS_ATTS    11
5679
5680 Datum
5681 show_all_settings(PG_FUNCTION_ARGS)
5682 {
5683         FuncCallContext *funcctx;
5684         TupleDesc       tupdesc;
5685         int                     call_cntr;
5686         int                     max_calls;
5687         AttInMetadata *attinmeta;
5688         MemoryContext oldcontext;
5689
5690         /* stuff done only on the first call of the function */
5691         if (SRF_IS_FIRSTCALL())
5692         {
5693                 /* create a function context for cross-call persistence */
5694                 funcctx = SRF_FIRSTCALL_INIT();
5695
5696                 /*
5697                  * switch to memory context appropriate for multiple function calls
5698                  */
5699                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5700
5701                 /*
5702                  * need a tuple descriptor representing NUM_PG_SETTINGS_ATTS columns
5703                  * of the appropriate types
5704                  */
5705                 tupdesc = CreateTemplateTupleDesc(NUM_PG_SETTINGS_ATTS, false);
5706                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
5707                                                    TEXTOID, -1, 0);
5708                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
5709                                                    TEXTOID, -1, 0);
5710                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "unit",
5711                                                    TEXTOID, -1, 0);
5712                 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "category",
5713                                                    TEXTOID, -1, 0);
5714                 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "short_desc",
5715                                                    TEXTOID, -1, 0);
5716                 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "extra_desc",
5717                                                    TEXTOID, -1, 0);
5718                 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "context",
5719                                                    TEXTOID, -1, 0);
5720                 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "vartype",
5721                                                    TEXTOID, -1, 0);
5722                 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "source",
5723                                                    TEXTOID, -1, 0);
5724                 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "min_val",
5725                                                    TEXTOID, -1, 0);
5726                 TupleDescInitEntry(tupdesc, (AttrNumber) 11, "max_val",
5727                                                    TEXTOID, -1, 0);
5728
5729                 /*
5730                  * Generate attribute metadata needed later to produce tuples from raw
5731                  * C strings
5732                  */
5733                 attinmeta = TupleDescGetAttInMetadata(tupdesc);
5734                 funcctx->attinmeta = attinmeta;
5735
5736                 /* total number of tuples to be returned */
5737                 funcctx->max_calls = GetNumConfigOptions();
5738
5739                 MemoryContextSwitchTo(oldcontext);
5740         }
5741
5742         /* stuff done on every call of the function */
5743         funcctx = SRF_PERCALL_SETUP();
5744
5745         call_cntr = funcctx->call_cntr;
5746         max_calls = funcctx->max_calls;
5747         attinmeta = funcctx->attinmeta;
5748
5749         if (call_cntr < max_calls)      /* do when there is more left to send */
5750         {
5751                 char       *values[NUM_PG_SETTINGS_ATTS];
5752                 bool            noshow;
5753                 HeapTuple       tuple;
5754                 Datum           result;
5755
5756                 /*
5757                  * Get the next visible GUC variable name and value
5758                  */
5759                 do
5760                 {
5761                         GetConfigOptionByNum(call_cntr, (const char **) values, &noshow);
5762                         if (noshow)
5763                         {
5764                                 /* bump the counter and get the next config setting */
5765                                 call_cntr = ++funcctx->call_cntr;
5766
5767                                 /* make sure we haven't gone too far now */
5768                                 if (call_cntr >= max_calls)
5769                                         SRF_RETURN_DONE(funcctx);
5770                         }
5771                 } while (noshow);
5772
5773                 /* build a tuple */
5774                 tuple = BuildTupleFromCStrings(attinmeta, values);
5775
5776                 /* make the tuple into a datum */
5777                 result = HeapTupleGetDatum(tuple);
5778
5779                 SRF_RETURN_NEXT(funcctx, result);
5780         }
5781         else
5782         {
5783                 /* do when there is no more left */
5784                 SRF_RETURN_DONE(funcctx);
5785         }
5786 }
5787
5788 static char *
5789 _ShowOption(struct config_generic * record, bool use_units)
5790 {
5791         char            buffer[256];
5792         const char *val;
5793
5794         switch (record->vartype)
5795         {
5796                 case PGC_BOOL:
5797                         {
5798                                 struct config_bool *conf = (struct config_bool *) record;
5799
5800                                 if (conf->show_hook)
5801                                         val = (*conf->show_hook) ();
5802                                 else
5803                                         val = *conf->variable ? "on" : "off";
5804                         }
5805                         break;
5806
5807                 case PGC_INT:
5808                         {
5809                                 struct config_int *conf = (struct config_int *) record;
5810
5811                                 if (conf->show_hook)
5812                                         val = (*conf->show_hook) ();
5813                                 else
5814                                 {
5815                                         char            unit[4];
5816                                         int                     result = *conf->variable;
5817
5818                                         if (use_units && result > 0 && (record->flags & GUC_UNIT_MEMORY))
5819                                         {
5820                                                 switch (record->flags & GUC_UNIT_MEMORY)
5821                                                 {
5822                                                         case GUC_UNIT_BLOCKS:
5823                                                                 result *= BLCKSZ / 1024;
5824                                                                 break;
5825                                                         case GUC_UNIT_XBLOCKS:
5826                                                                 result *= XLOG_BLCKSZ / 1024;
5827                                                                 break;
5828                                                 }
5829
5830                                                 if (result % KB_PER_GB == 0)
5831                                                 {
5832                                                         result /= KB_PER_GB;
5833                                                         strcpy(unit, "GB");
5834                                                 }
5835                                                 else if (result % KB_PER_MB == 0)
5836                                                 {
5837                                                         result /= KB_PER_MB;
5838                                                         strcpy(unit, "MB");
5839                                                 }
5840                                                 else
5841                                                 {
5842                                                         strcpy(unit, "kB");
5843                                                 }
5844                                         }
5845                                         else if (use_units && result > 0 && (record->flags & GUC_UNIT_TIME))
5846                                         {
5847                                                 switch (record->flags & GUC_UNIT_TIME)
5848                                                 {
5849                                                         case GUC_UNIT_S:
5850                                                                 result *= MS_PER_S;
5851                                                                 break;
5852                                                         case GUC_UNIT_MIN:
5853                                                                 result *= MS_PER_MIN;
5854                                                                 break;
5855                                                 }
5856
5857                                                 if (result % MS_PER_D == 0)
5858                                                 {
5859                                                         result /= MS_PER_D;
5860                                                         strcpy(unit, "d");
5861                                                 }
5862                                                 else if (result % MS_PER_H == 0)
5863                                                 {
5864                                                         result /= MS_PER_H;
5865                                                         strcpy(unit, "h");
5866                                                 }
5867                                                 else if (result % MS_PER_MIN == 0)
5868                                                 {
5869                                                         result /= MS_PER_MIN;
5870                                                         strcpy(unit, "min");
5871                                                 }
5872                                                 else if (result % MS_PER_S == 0)
5873                                                 {
5874                                                         result /= MS_PER_S;
5875                                                         strcpy(unit, "s");
5876                                                 }
5877                                                 else
5878                                                 {
5879                                                         strcpy(unit, "ms");
5880                                                 }
5881                                         }
5882                                         else
5883                                                 strcpy(unit, "");
5884
5885                                         snprintf(buffer, sizeof(buffer), "%d%s",
5886                                                          (int) result, unit);
5887                                         val = buffer;
5888                                 }
5889                         }
5890                         break;
5891
5892                 case PGC_REAL:
5893                         {
5894                                 struct config_real *conf = (struct config_real *) record;
5895
5896                                 if (conf->show_hook)
5897                                         val = (*conf->show_hook) ();
5898                                 else
5899                                 {
5900                                         snprintf(buffer, sizeof(buffer), "%g",
5901                                                          *conf->variable);
5902                                         val = buffer;
5903                                 }
5904                         }
5905                         break;
5906
5907                 case PGC_STRING:
5908                         {
5909                                 struct config_string *conf = (struct config_string *) record;
5910
5911                                 if (conf->show_hook)
5912                                         val = (*conf->show_hook) ();
5913                                 else if (*conf->variable && **conf->variable)
5914                                         val = *conf->variable;
5915                                 else
5916                                         val = "";
5917                         }
5918                         break;
5919
5920                 default:
5921                         /* just to keep compiler quiet */
5922                         val = "???";
5923                         break;
5924         }
5925
5926         return pstrdup(val);
5927 }
5928
5929
5930 static bool
5931 is_newvalue_equal(struct config_generic * record, const char *newvalue)
5932 {
5933         switch (record->vartype)
5934         {
5935                 case PGC_BOOL:
5936                         {
5937                                 struct config_bool *conf = (struct config_bool *) record;
5938                                 bool            newval;
5939
5940                                 return parse_bool(newvalue, &newval)
5941                                         && *conf->variable == newval;
5942                         }
5943                 case PGC_INT:
5944                         {
5945                                 struct config_int *conf = (struct config_int *) record;
5946                                 int                     newval;
5947
5948                                 return parse_int(newvalue, &newval, record->flags, NULL)
5949                                         && *conf->variable == newval;
5950                         }
5951                 case PGC_REAL:
5952                         {
5953                                 struct config_real *conf = (struct config_real *) record;
5954                                 double          newval;
5955
5956                                 return parse_real(newvalue, &newval)
5957                                         && *conf->variable == newval;
5958                         }
5959                 case PGC_STRING:
5960                         {
5961                                 struct config_string *conf = (struct config_string *) record;
5962
5963                                 return strcmp(*conf->variable, newvalue) == 0;
5964                         }
5965         }
5966
5967         return false;
5968 }
5969
5970
5971 #ifdef EXEC_BACKEND
5972
5973 /*
5974  *      This routine dumps out all non-default GUC options into a binary
5975  *      file that is read by all exec'ed backends.  The format is:
5976  *
5977  *              variable name, string, null terminated
5978  *              variable value, string, null terminated
5979  *              variable source, integer
5980  */
5981 void
5982 write_nondefault_variables(GucContext context)
5983 {
5984         int                     i;
5985         int                     elevel;
5986         FILE       *fp;
5987
5988         Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
5989
5990         elevel = (context == PGC_SIGHUP) ? LOG : ERROR;
5991
5992         /*
5993          * Open file
5994          */
5995         fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, "w");
5996         if (!fp)
5997         {
5998                 ereport(elevel,
5999                                 (errcode_for_file_access(),
6000                                  errmsg("could not write to file \"%s\": %m",
6001                                                 CONFIG_EXEC_PARAMS_NEW)));
6002                 return;
6003         }
6004
6005         for (i = 0; i < num_guc_variables; i++)
6006         {
6007                 struct config_generic *gconf = guc_variables[i];
6008
6009                 if (gconf->source != PGC_S_DEFAULT)
6010                 {
6011                         fprintf(fp, "%s", gconf->name);
6012                         fputc(0, fp);
6013
6014                         switch (gconf->vartype)
6015                         {
6016                                 case PGC_BOOL:
6017                                         {
6018                                                 struct config_bool *conf = (struct config_bool *) gconf;
6019
6020                                                 if (*conf->variable == 0)
6021                                                         fprintf(fp, "false");
6022                                                 else
6023                                                         fprintf(fp, "true");
6024                                         }
6025                                         break;
6026
6027                                 case PGC_INT:
6028                                         {
6029                                                 struct config_int *conf = (struct config_int *) gconf;
6030
6031                                                 fprintf(fp, "%d", *conf->variable);
6032                                         }
6033                                         break;
6034
6035                                 case PGC_REAL:
6036                                         {
6037                                                 struct config_real *conf = (struct config_real *) gconf;
6038
6039                                                 /* Could lose precision here? */
6040                                                 fprintf(fp, "%f", *conf->variable);
6041                                         }
6042                                         break;
6043
6044                                 case PGC_STRING:
6045                                         {
6046                                                 struct config_string *conf = (struct config_string *) gconf;
6047
6048                                                 fprintf(fp, "%s", *conf->variable);
6049                                         }
6050                                         break;
6051                         }
6052
6053                         fputc(0, fp);
6054
6055                         fwrite(&gconf->source, sizeof(gconf->source), 1, fp);
6056                 }
6057         }
6058
6059         if (FreeFile(fp))
6060         {
6061                 ereport(elevel,
6062                                 (errcode_for_file_access(),
6063                                  errmsg("could not write to file \"%s\": %m",
6064                                                 CONFIG_EXEC_PARAMS_NEW)));
6065                 return;
6066         }
6067
6068         /*
6069          * Put new file in place.  This could delay on Win32, but we don't hold
6070          * any exclusive locks.
6071          */
6072         rename(CONFIG_EXEC_PARAMS_NEW, CONFIG_EXEC_PARAMS);
6073 }
6074
6075
6076 /*
6077  *      Read string, including null byte from file
6078  *
6079  *      Return NULL on EOF and nothing read
6080  */
6081 static char *
6082 read_string_with_null(FILE *fp)
6083 {
6084         int                     i = 0,
6085                                 ch,
6086                                 maxlen = 256;
6087         char       *str = NULL;
6088
6089         do
6090         {
6091                 if ((ch = fgetc(fp)) == EOF)
6092                 {
6093                         if (i == 0)
6094                                 return NULL;
6095                         else
6096                                 elog(FATAL, "invalid format of exec config params file");
6097                 }
6098                 if (i == 0)
6099                         str = guc_malloc(FATAL, maxlen);
6100                 else if (i == maxlen)
6101                         str = guc_realloc(FATAL, str, maxlen *= 2);
6102                 str[i++] = ch;
6103         } while (ch != 0);
6104
6105         return str;
6106 }
6107
6108
6109 /*
6110  *      This routine loads a previous postmaster dump of its non-default
6111  *      settings.
6112  */
6113 void
6114 read_nondefault_variables(void)
6115 {
6116         FILE       *fp;
6117         char       *varname,
6118                            *varvalue;
6119         int                     varsource;
6120
6121         /*
6122          * Open file
6123          */
6124         fp = AllocateFile(CONFIG_EXEC_PARAMS, "r");
6125         if (!fp)
6126         {
6127                 /* File not found is fine */
6128                 if (errno != ENOENT)
6129                         ereport(FATAL,
6130                                         (errcode_for_file_access(),
6131                                          errmsg("could not read from file \"%s\": %m",
6132                                                         CONFIG_EXEC_PARAMS)));
6133                 return;
6134         }
6135
6136         for (;;)
6137         {
6138                 struct config_generic *record;
6139
6140                 if ((varname = read_string_with_null(fp)) == NULL)
6141                         break;
6142
6143                 if ((record = find_option(varname, FATAL)) == NULL)
6144                         elog(FATAL, "failed to locate variable %s in exec config params file", varname);
6145                 if ((varvalue = read_string_with_null(fp)) == NULL)
6146                         elog(FATAL, "invalid format of exec config params file");
6147                 if (fread(&varsource, sizeof(varsource), 1, fp) == 0)
6148                         elog(FATAL, "invalid format of exec config params file");
6149
6150                 (void) set_config_option(varname, varvalue, record->context,
6151                                                                  varsource, false, true);
6152                 free(varname);
6153                 free(varvalue);
6154         }
6155
6156         FreeFile(fp);
6157 }
6158 #endif   /* EXEC_BACKEND */
6159
6160
6161 /*
6162  * A little "long argument" simulation, although not quite GNU
6163  * compliant. Takes a string of the form "some-option=some value" and
6164  * returns name = "some_option" and value = "some value" in malloc'ed
6165  * storage. Note that '-' is converted to '_' in the option name. If
6166  * there is no '=' in the input string then value will be NULL.
6167  */
6168 void
6169 ParseLongOption(const char *string, char **name, char **value)
6170 {
6171         size_t          equal_pos;
6172         char       *cp;
6173
6174         AssertArg(string);
6175         AssertArg(name);
6176         AssertArg(value);
6177
6178         equal_pos = strcspn(string, "=");
6179
6180         if (string[equal_pos] == '=')
6181         {
6182                 *name = guc_malloc(FATAL, equal_pos + 1);
6183                 strlcpy(*name, string, equal_pos + 1);
6184
6185                 *value = guc_strdup(FATAL, &string[equal_pos + 1]);
6186         }
6187         else
6188         {
6189                 /* no equal sign in string */
6190                 *name = guc_strdup(FATAL, string);
6191                 *value = NULL;
6192         }
6193
6194         for (cp = *name; *cp; cp++)
6195                 if (*cp == '-')
6196                         *cp = '_';
6197 }
6198
6199
6200 /*
6201  * Handle options fetched from pg_database.datconfig, pg_authid.rolconfig,
6202  * pg_proc.proconfig, etc.  Caller must specify proper context/source/local.
6203  *
6204  * The array parameter must be an array of TEXT (it must not be NULL).
6205  */
6206 void
6207 ProcessGUCArray(ArrayType *array,
6208                                 GucContext context, GucSource source, bool isLocal)
6209 {
6210         int                     i;
6211
6212         Assert(array != NULL);
6213         Assert(ARR_ELEMTYPE(array) == TEXTOID);
6214         Assert(ARR_NDIM(array) == 1);
6215         Assert(ARR_LBOUND(array)[0] == 1);
6216
6217         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6218         {
6219                 Datum           d;
6220                 bool            isnull;
6221                 char       *s;
6222                 char       *name;
6223                 char       *value;
6224
6225                 d = array_ref(array, 1, &i,
6226                                           -1 /* varlenarray */ ,
6227                                           -1 /* TEXT's typlen */ ,
6228                                           false /* TEXT's typbyval */ ,
6229                                           'i' /* TEXT's typalign */ ,
6230                                           &isnull);
6231
6232                 if (isnull)
6233                         continue;
6234
6235                 s = DatumGetCString(DirectFunctionCall1(textout, d));
6236
6237                 ParseLongOption(s, &name, &value);
6238                 if (!value)
6239                 {
6240                         ereport(WARNING,
6241                                         (errcode(ERRCODE_SYNTAX_ERROR),
6242                                          errmsg("could not parse setting for parameter \"%s\"",
6243                                                         name)));
6244                         free(name);
6245                         continue;
6246                 }
6247
6248                 (void) set_config_option(name, value, context, source, isLocal, true);
6249
6250                 free(name);
6251                 if (value)
6252                         free(value);
6253         }
6254 }
6255
6256
6257 /*
6258  * Add an entry to an option array.  The array parameter may be NULL
6259  * to indicate the current table entry is NULL.
6260  */
6261 ArrayType *
6262 GUCArrayAdd(ArrayType *array, const char *name, const char *value)
6263 {
6264         const char *varname;
6265         Datum           datum;
6266         char       *newval;
6267         ArrayType  *a;
6268
6269         Assert(name);
6270         Assert(value);
6271
6272         /* test if the option is valid */
6273         set_config_option(name, value,
6274                                           superuser() ? PGC_SUSET : PGC_USERSET,
6275                                           PGC_S_TEST, false, false);
6276
6277         /* convert name to canonical spelling, so we can use plain strcmp */
6278         (void) GetConfigOptionByName(name, &varname);
6279         name = varname;
6280
6281         newval = palloc(strlen(name) + 1 + strlen(value) + 1);
6282         sprintf(newval, "%s=%s", name, value);
6283         datum = DirectFunctionCall1(textin, CStringGetDatum(newval));
6284
6285         if (array)
6286         {
6287                 int                     index;
6288                 bool            isnull;
6289                 int                     i;
6290
6291                 Assert(ARR_ELEMTYPE(array) == TEXTOID);
6292                 Assert(ARR_NDIM(array) == 1);
6293                 Assert(ARR_LBOUND(array)[0] == 1);
6294
6295                 index = ARR_DIMS(array)[0] + 1; /* add after end */
6296
6297                 for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6298                 {
6299                         Datum           d;
6300                         char       *current;
6301
6302                         d = array_ref(array, 1, &i,
6303                                                   -1 /* varlenarray */ ,
6304                                                   -1 /* TEXT's typlen */ ,
6305                                                   false /* TEXT's typbyval */ ,
6306                                                   'i' /* TEXT's typalign */ ,
6307                                                   &isnull);
6308                         if (isnull)
6309                                 continue;
6310                         current = DatumGetCString(DirectFunctionCall1(textout, d));
6311                         if (strncmp(current, newval, strlen(name) + 1) == 0)
6312                         {
6313                                 index = i;
6314                                 break;
6315                         }
6316                 }
6317
6318                 a = array_set(array, 1, &index,
6319                                           datum,
6320                                           false,
6321                                           -1 /* varlena array */ ,
6322                                           -1 /* TEXT's typlen */ ,
6323                                           false /* TEXT's typbyval */ ,
6324                                           'i' /* TEXT's typalign */ );
6325         }
6326         else
6327                 a = construct_array(&datum, 1,
6328                                                         TEXTOID,
6329                                                         -1, false, 'i');
6330
6331         return a;
6332 }
6333
6334
6335 /*
6336  * Delete an entry from an option array.  The array parameter may be NULL
6337  * to indicate the current table entry is NULL.  Also, if the return value
6338  * is NULL then a null should be stored.
6339  */
6340 ArrayType *
6341 GUCArrayDelete(ArrayType *array, const char *name)
6342 {
6343         const char *varname;
6344         ArrayType  *newarray;
6345         int                     i;
6346         int                     index;
6347
6348         Assert(name);
6349
6350         /* test if the option is valid */
6351         set_config_option(name, NULL,
6352                                           superuser() ? PGC_SUSET : PGC_USERSET,
6353                                           PGC_S_TEST, false, false);
6354
6355         /* convert name to canonical spelling, so we can use plain strcmp */
6356         (void) GetConfigOptionByName(name, &varname);
6357         name = varname;
6358
6359         /* if array is currently null, then surely nothing to delete */
6360         if (!array)
6361                 return NULL;
6362
6363         newarray = NULL;
6364         index = 1;
6365
6366         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6367         {
6368                 Datum           d;
6369                 char       *val;
6370                 bool            isnull;
6371
6372                 d = array_ref(array, 1, &i,
6373                                           -1 /* varlenarray */ ,
6374                                           -1 /* TEXT's typlen */ ,
6375                                           false /* TEXT's typbyval */ ,
6376                                           'i' /* TEXT's typalign */ ,
6377                                           &isnull);
6378                 if (isnull)
6379                         continue;
6380                 val = DatumGetCString(DirectFunctionCall1(textout, d));
6381
6382                 /* ignore entry if it's what we want to delete */
6383                 if (strncmp(val, name, strlen(name)) == 0
6384                         && val[strlen(name)] == '=')
6385                         continue;
6386
6387                 /* else add it to the output array */
6388                 if (newarray)
6389                 {
6390                         newarray = array_set(newarray, 1, &index,
6391                                                                  d,
6392                                                                  false,
6393                                                                  -1 /* varlenarray */ ,
6394                                                                  -1 /* TEXT's typlen */ ,
6395                                                                  false /* TEXT's typbyval */ ,
6396                                                                  'i' /* TEXT's typalign */ );
6397                 }
6398                 else
6399                         newarray = construct_array(&d, 1,
6400                                                                            TEXTOID,
6401                                                                            -1, false, 'i');
6402
6403                 index++;
6404         }
6405
6406         return newarray;
6407 }
6408
6409
6410 /*
6411  * assign_hook subroutines
6412  */
6413
6414 static const char *
6415 assign_log_destination(const char *value, bool doit, GucSource source)
6416 {
6417         char       *rawstring;
6418         List       *elemlist;
6419         ListCell   *l;
6420         int                     newlogdest = 0;
6421
6422         /* Need a modifiable copy of string */
6423         rawstring = pstrdup(value);
6424
6425         /* Parse string into list of identifiers */
6426         if (!SplitIdentifierString(rawstring, ',', &elemlist))
6427         {
6428                 /* syntax error in list */
6429                 pfree(rawstring);
6430                 list_free(elemlist);
6431                 if (source >= PGC_S_INTERACTIVE)
6432                         ereport(ERROR,
6433                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6434                         errmsg("invalid list syntax for parameter \"log_destination\"")));
6435                 return NULL;
6436         }
6437
6438         foreach(l, elemlist)
6439         {
6440                 char       *tok = (char *) lfirst(l);
6441
6442                 if (pg_strcasecmp(tok, "stderr") == 0)
6443                         newlogdest |= LOG_DESTINATION_STDERR;
6444                 else if (pg_strcasecmp(tok, "csvlog") == 0)
6445            newlogdest |= LOG_DESTINATION_CSVLOG;
6446 #ifdef HAVE_SYSLOG
6447                 else if (pg_strcasecmp(tok, "syslog") == 0)
6448                         newlogdest |= LOG_DESTINATION_SYSLOG;
6449 #endif
6450 #ifdef WIN32
6451                 else if (pg_strcasecmp(tok, "eventlog") == 0)
6452                         newlogdest |= LOG_DESTINATION_EVENTLOG;
6453 #endif
6454                 else
6455                 {
6456                         if (source >= PGC_S_INTERACTIVE)
6457                                 ereport(ERROR,
6458                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6459                                   errmsg("unrecognized \"log_destination\" key word: \"%s\"",
6460                                                  tok)));
6461                         pfree(rawstring);
6462                         list_free(elemlist);
6463                         return NULL;
6464                 }
6465         }
6466
6467         if (doit)
6468                 Log_destination = newlogdest;
6469
6470         pfree(rawstring);
6471         list_free(elemlist);
6472
6473         return value;
6474 }
6475
6476 #ifdef HAVE_SYSLOG
6477
6478 static const char *
6479 assign_syslog_facility(const char *facility, bool doit, GucSource source)
6480 {
6481         int                     syslog_fac;
6482
6483         if (pg_strcasecmp(facility, "LOCAL0") == 0)
6484                 syslog_fac = LOG_LOCAL0;
6485         else if (pg_strcasecmp(facility, "LOCAL1") == 0)
6486                 syslog_fac = LOG_LOCAL1;
6487         else if (pg_strcasecmp(facility, "LOCAL2") == 0)
6488                 syslog_fac = LOG_LOCAL2;
6489         else if (pg_strcasecmp(facility, "LOCAL3") == 0)
6490                 syslog_fac = LOG_LOCAL3;
6491         else if (pg_strcasecmp(facility, "LOCAL4") == 0)
6492                 syslog_fac = LOG_LOCAL4;
6493         else if (pg_strcasecmp(facility, "LOCAL5") == 0)
6494                 syslog_fac = LOG_LOCAL5;
6495         else if (pg_strcasecmp(facility, "LOCAL6") == 0)
6496                 syslog_fac = LOG_LOCAL6;
6497         else if (pg_strcasecmp(facility, "LOCAL7") == 0)
6498                 syslog_fac = LOG_LOCAL7;
6499         else
6500                 return NULL;                    /* reject */
6501
6502         if (doit)
6503         {
6504                 syslog_facility = syslog_fac;
6505                 set_syslog_parameters(syslog_ident_str ? syslog_ident_str : "postgres",
6506                                                           syslog_facility);
6507         }
6508
6509         return facility;
6510 }
6511
6512 static const char *
6513 assign_syslog_ident(const char *ident, bool doit, GucSource source)
6514 {
6515         if (doit)
6516                 set_syslog_parameters(ident, syslog_facility);
6517
6518         return ident;
6519 }
6520 #endif   /* HAVE_SYSLOG */
6521
6522
6523 static const char *
6524 assign_defaultxactisolevel(const char *newval, bool doit, GucSource source)
6525 {
6526         if (pg_strcasecmp(newval, "serializable") == 0)
6527         {
6528                 if (doit)
6529                         DefaultXactIsoLevel = XACT_SERIALIZABLE;
6530         }
6531         else if (pg_strcasecmp(newval, "repeatable read") == 0)
6532         {
6533                 if (doit)
6534                         DefaultXactIsoLevel = XACT_REPEATABLE_READ;
6535         }
6536         else if (pg_strcasecmp(newval, "read committed") == 0)
6537         {
6538                 if (doit)
6539                         DefaultXactIsoLevel = XACT_READ_COMMITTED;
6540         }
6541         else if (pg_strcasecmp(newval, "read uncommitted") == 0)
6542         {
6543                 if (doit)
6544                         DefaultXactIsoLevel = XACT_READ_UNCOMMITTED;
6545         }
6546         else
6547                 return NULL;
6548         return newval;
6549 }
6550
6551 static const char *
6552 assign_session_replication_role(const char *newval, bool doit, GucSource source)
6553 {
6554         int             newrole;
6555
6556         if (pg_strcasecmp(newval, "origin") == 0)
6557                 newrole = SESSION_REPLICATION_ROLE_ORIGIN;
6558         else if (pg_strcasecmp(newval, "replica") == 0)
6559                 newrole = SESSION_REPLICATION_ROLE_REPLICA;
6560         else if (pg_strcasecmp(newval, "local") == 0)
6561                 newrole = SESSION_REPLICATION_ROLE_LOCAL;
6562         else
6563                 return NULL;
6564
6565         /*
6566          * Must flush the plan cache when changing replication role; but don't
6567          * flush unnecessarily.
6568          */
6569         if (doit && SessionReplicationRole != newrole)
6570         {
6571                 ResetPlanCache();
6572                 SessionReplicationRole = newrole;
6573         }
6574
6575         return newval;
6576 }
6577
6578 static const char *
6579 assign_log_min_messages(const char *newval,
6580                                                 bool doit, GucSource source)
6581 {
6582         return (assign_msglvl(&log_min_messages, newval, doit, source));
6583 }
6584
6585 static const char *
6586 assign_client_min_messages(const char *newval, bool doit, GucSource source)
6587 {
6588         return (assign_msglvl(&client_min_messages, newval, doit, source));
6589 }
6590
6591 static const char *
6592 assign_min_error_statement(const char *newval, bool doit, GucSource source)
6593 {
6594         return (assign_msglvl(&log_min_error_statement, newval, doit, source));
6595 }
6596
6597 static const char *
6598 assign_msglvl(int *var, const char *newval, bool doit, GucSource source)
6599 {
6600         if (pg_strcasecmp(newval, "debug") == 0)
6601         {
6602                 if (doit)
6603                         (*var) = DEBUG2;
6604         }
6605         else if (pg_strcasecmp(newval, "debug5") == 0)
6606         {
6607                 if (doit)
6608                         (*var) = DEBUG5;
6609         }
6610         else if (pg_strcasecmp(newval, "debug4") == 0)
6611         {
6612                 if (doit)
6613                         (*var) = DEBUG4;
6614         }
6615         else if (pg_strcasecmp(newval, "debug3") == 0)
6616         {
6617                 if (doit)
6618                         (*var) = DEBUG3;
6619         }
6620         else if (pg_strcasecmp(newval, "debug2") == 0)
6621         {
6622                 if (doit)
6623                         (*var) = DEBUG2;
6624         }
6625         else if (pg_strcasecmp(newval, "debug1") == 0)
6626         {
6627                 if (doit)
6628                         (*var) = DEBUG1;
6629         }
6630         else if (pg_strcasecmp(newval, "log") == 0)
6631         {
6632                 if (doit)
6633                         (*var) = LOG;
6634         }
6635
6636         /*
6637          * Client_min_messages always prints 'info', but we allow it as a value
6638          * anyway.
6639          */
6640         else if (pg_strcasecmp(newval, "info") == 0)
6641         {
6642                 if (doit)
6643                         (*var) = INFO;
6644         }
6645         else if (pg_strcasecmp(newval, "notice") == 0)
6646         {
6647                 if (doit)
6648                         (*var) = NOTICE;
6649         }
6650         else if (pg_strcasecmp(newval, "warning") == 0)
6651         {
6652                 if (doit)
6653                         (*var) = WARNING;
6654         }
6655         else if (pg_strcasecmp(newval, "error") == 0)
6656         {
6657                 if (doit)
6658                         (*var) = ERROR;
6659         }
6660         /* We allow FATAL/PANIC for client-side messages too. */
6661         else if (pg_strcasecmp(newval, "fatal") == 0)
6662         {
6663                 if (doit)
6664                         (*var) = FATAL;
6665         }
6666         else if (pg_strcasecmp(newval, "panic") == 0)
6667         {
6668                 if (doit)
6669                         (*var) = PANIC;
6670         }
6671         else
6672                 return NULL;                    /* fail */
6673         return newval;                          /* OK */
6674 }
6675
6676 static const char *
6677 assign_log_error_verbosity(const char *newval, bool doit, GucSource source)
6678 {
6679         if (pg_strcasecmp(newval, "terse") == 0)
6680         {
6681                 if (doit)
6682                         Log_error_verbosity = PGERROR_TERSE;
6683         }
6684         else if (pg_strcasecmp(newval, "default") == 0)
6685         {
6686                 if (doit)
6687                         Log_error_verbosity = PGERROR_DEFAULT;
6688         }
6689         else if (pg_strcasecmp(newval, "verbose") == 0)
6690         {
6691                 if (doit)
6692                         Log_error_verbosity = PGERROR_VERBOSE;
6693         }
6694         else
6695                 return NULL;                    /* fail */
6696         return newval;                          /* OK */
6697 }
6698
6699 static const char *
6700 assign_log_statement(const char *newval, bool doit, GucSource source)
6701 {
6702         if (pg_strcasecmp(newval, "none") == 0)
6703         {
6704                 if (doit)
6705                         log_statement = LOGSTMT_NONE;
6706         }
6707         else if (pg_strcasecmp(newval, "ddl") == 0)
6708         {
6709                 if (doit)
6710                         log_statement = LOGSTMT_DDL;
6711         }
6712         else if (pg_strcasecmp(newval, "mod") == 0)
6713         {
6714                 if (doit)
6715                         log_statement = LOGSTMT_MOD;
6716         }
6717         else if (pg_strcasecmp(newval, "all") == 0)
6718         {
6719                 if (doit)
6720                         log_statement = LOGSTMT_ALL;
6721         }
6722         else
6723                 return NULL;                    /* fail */
6724         return newval;                          /* OK */
6725 }
6726
6727 static const char *
6728 show_num_temp_buffers(void)
6729 {
6730         /*
6731          * We show the GUC var until local buffers have been initialized, and
6732          * NLocBuffer afterwards.
6733          */
6734         static char nbuf[32];
6735
6736         sprintf(nbuf, "%d", NLocBuffer ? NLocBuffer : num_temp_buffers);
6737         return nbuf;
6738 }
6739
6740 static bool
6741 assign_phony_autocommit(bool newval, bool doit, GucSource source)
6742 {
6743         if (!newval)
6744         {
6745                 if (doit && source >= PGC_S_INTERACTIVE)
6746                         ereport(ERROR,
6747                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6748                                          errmsg("SET AUTOCOMMIT TO OFF is no longer supported")));
6749                 return false;
6750         }
6751         return true;
6752 }
6753
6754 static const char *
6755 assign_custom_variable_classes(const char *newval, bool doit, GucSource source)
6756 {
6757         /*
6758          * Check syntax. newval must be a comma separated list of identifiers.
6759          * Whitespace is allowed but skipped.
6760          */
6761         bool            hasSpaceAfterToken = false;
6762         const char *cp = newval;
6763         int                     symLen = 0;
6764         int                     c;
6765         StringInfoData buf;
6766
6767         /*
6768          * Resetting custom_variable_classes by removing it from the
6769          * configuration file will lead to newval = NULL
6770          */
6771         if (newval == NULL)
6772                 return guc_strdup(ERROR, "");
6773
6774         initStringInfo(&buf);
6775         while ((c = *cp++) != 0)
6776         {
6777                 if (isspace((unsigned char) c))
6778                 {
6779                         if (symLen > 0)
6780                                 hasSpaceAfterToken = true;
6781                         continue;
6782                 }
6783
6784                 if (c == ',')
6785                 {
6786                         hasSpaceAfterToken = false;
6787                         if (symLen > 0)
6788                         {
6789                                 symLen = 0;
6790                                 appendStringInfoChar(&buf, ',');
6791                         }
6792                         continue;
6793                 }
6794
6795                 if (hasSpaceAfterToken || !isalnum((unsigned char) c))
6796                 {
6797                         /*
6798                          * Syntax error due to token following space after token or non
6799                          * alpha numeric character
6800                          */
6801                         ereport(LOG,
6802                                         (errcode(ERRCODE_SYNTAX_ERROR),
6803                                          errmsg("invalid syntax for \"custom_variable_classes\": \"%s\"", newval)));
6804                         pfree(buf.data);
6805                         return NULL;
6806                 }
6807                 symLen++;
6808                 appendStringInfoChar(&buf, (char) c);
6809         }
6810
6811         /* Remove stray ',' at end */
6812         if (symLen == 0 && buf.len > 0)
6813                 buf.data[--buf.len] = '\0';
6814
6815         if (buf.len == 0)
6816                 newval = NULL;
6817         else if (doit)
6818                 newval = guc_strdup(ERROR, buf.data);
6819
6820         pfree(buf.data);
6821         return newval;
6822 }
6823
6824 static bool
6825 assign_debug_assertions(bool newval, bool doit, GucSource source)
6826 {
6827 #ifndef USE_ASSERT_CHECKING
6828         if (newval)
6829                 ereport(ERROR,
6830                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6831                            errmsg("assertion checking is not supported by this build")));
6832 #endif
6833         return true;
6834 }
6835
6836 static bool
6837 assign_ssl(bool newval, bool doit, GucSource source)
6838 {
6839 #ifndef USE_SSL
6840         if (newval)
6841                 ereport(ERROR,
6842                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6843                                  errmsg("SSL is not supported by this build")));
6844 #endif
6845         return true;
6846 }
6847
6848 static bool
6849 assign_stage_log_stats(bool newval, bool doit, GucSource source)
6850 {
6851         if (newval && log_statement_stats)
6852         {
6853                 if (source >= PGC_S_INTERACTIVE)
6854                         ereport(ERROR,
6855                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6856                                          errmsg("cannot enable parameter when \"log_statement_stats\" is true")));
6857                 /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
6858                 else if (source != PGC_S_OVERRIDE)
6859                         return false;
6860         }
6861         return true;
6862 }
6863
6864 static bool
6865 assign_log_stats(bool newval, bool doit, GucSource source)
6866 {
6867         if (newval &&
6868                 (log_parser_stats || log_planner_stats || log_executor_stats))
6869         {
6870                 if (source >= PGC_S_INTERACTIVE)
6871                         ereport(ERROR,
6872                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6873                                          errmsg("cannot enable \"log_statement_stats\" when "
6874                                                         "\"log_parser_stats\", \"log_planner_stats\", "
6875                                                         "or \"log_executor_stats\" is true")));
6876                 /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
6877                 else if (source != PGC_S_OVERRIDE)
6878                         return false;
6879         }
6880         return true;
6881 }
6882
6883 static bool
6884 assign_transaction_read_only(bool newval, bool doit, GucSource source)
6885 {
6886         /* Can't go to r/w mode inside a r/o transaction */
6887         if (newval == false && XactReadOnly && IsSubTransaction())
6888         {
6889                 if (source >= PGC_S_INTERACTIVE)
6890                         ereport(ERROR,
6891                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6892                                          errmsg("cannot set transaction read-write mode inside a read-only transaction")));
6893                 /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */
6894                 else if (source != PGC_S_OVERRIDE)
6895                         return false;
6896         }
6897         return true;
6898 }
6899
6900 static const char *
6901 assign_canonical_path(const char *newval, bool doit, GucSource source)
6902 {
6903         if (doit)
6904         {
6905                 char       *canon_val = guc_strdup(ERROR, newval);
6906
6907                 canonicalize_path(canon_val);
6908                 return canon_val;
6909         }
6910         else
6911                 return newval;
6912 }
6913
6914 static const char *
6915 assign_backslash_quote(const char *newval, bool doit, GucSource source)
6916 {
6917         BackslashQuoteType bq;
6918         bool            bqbool;
6919
6920         /*
6921          * Although only "on", "off", and "safe_encoding" are documented, we use
6922          * parse_bool so we can accept all the likely variants of "on" and "off".
6923          */
6924         if (pg_strcasecmp(newval, "safe_encoding") == 0)
6925                 bq = BACKSLASH_QUOTE_SAFE_ENCODING;
6926         else if (parse_bool(newval, &bqbool))
6927         {
6928                 bq = bqbool ? BACKSLASH_QUOTE_ON : BACKSLASH_QUOTE_OFF;
6929         }
6930         else
6931                 return NULL;                    /* reject */
6932
6933         if (doit)
6934                 backslash_quote = bq;
6935
6936         return newval;
6937 }
6938
6939 static const char *
6940 assign_timezone_abbreviations(const char *newval, bool doit, GucSource source)
6941 {
6942         /*
6943          * The powerup value shown above for timezone_abbreviations is "UNKNOWN".
6944          * When we see this we just do nothing.  If this value isn't overridden
6945          * from the config file then pg_timezone_abbrev_initialize() will
6946          * eventually replace it with "Default".  This hack has two purposes: to
6947          * avoid wasting cycles loading values that might soon be overridden from
6948          * the config file, and to avoid trying to read the timezone abbrev files
6949          * during InitializeGUCOptions().  The latter doesn't work in an
6950          * EXEC_BACKEND subprocess because my_exec_path hasn't been set yet and so
6951          * we can't locate PGSHAREDIR.  (Essentially the same hack is used to
6952          * delay initializing TimeZone ... if we have any more, we should try to
6953          * clean up and centralize this mechanism ...)
6954          */
6955         if (strcmp(newval, "UNKNOWN") == 0)
6956         {
6957                 return newval;
6958         }
6959
6960         /* Loading abbrev file is expensive, so only do it when value changes */
6961         if (timezone_abbreviations_string == NULL ||
6962                 strcmp(timezone_abbreviations_string, newval) != 0)
6963         {
6964                 int                     elevel;
6965
6966                 /*
6967                  * If reading config file, only the postmaster should bleat loudly
6968                  * about problems.      Otherwise, it's just this one process doing it,
6969                  * and we use WARNING message level.
6970                  */
6971                 if (source == PGC_S_FILE)
6972                         elevel = IsUnderPostmaster ? DEBUG2 : LOG;
6973                 else
6974                         elevel = WARNING;
6975                 if (!load_tzoffsets(newval, doit, elevel))
6976                         return NULL;
6977         }
6978         return newval;
6979 }
6980
6981 /*
6982  * pg_timezone_abbrev_initialize --- set default value if not done already
6983  *
6984  * This is called after initial loading of postgresql.conf.  If no
6985  * timezone_abbreviations setting was found therein, select default.
6986  */
6987 void
6988 pg_timezone_abbrev_initialize(void)
6989 {
6990         if (strcmp(timezone_abbreviations_string, "UNKNOWN") == 0)
6991         {
6992                 SetConfigOption("timezone_abbreviations", "Default",
6993                                                 PGC_POSTMASTER, PGC_S_ARGV);
6994         }
6995 }
6996
6997 static const char *
6998 assign_xmlbinary(const char *newval, bool doit, GucSource source)
6999 {
7000         XmlBinaryType xb;
7001
7002         if (pg_strcasecmp(newval, "base64") == 0)
7003                 xb = XMLBINARY_BASE64;
7004         else if (pg_strcasecmp(newval, "hex") == 0)
7005                 xb = XMLBINARY_HEX;
7006         else
7007                 return NULL;                    /* reject */
7008
7009         if (doit)
7010                 xmlbinary = xb;
7011
7012         return newval;
7013 }
7014
7015 static const char *
7016 assign_xmloption(const char *newval, bool doit, GucSource source)
7017 {
7018         XmlOptionType xo;
7019
7020         if (pg_strcasecmp(newval, "document") == 0)
7021                 xo = XMLOPTION_DOCUMENT;
7022         else if (pg_strcasecmp(newval, "content") == 0)
7023                 xo = XMLOPTION_CONTENT;
7024         else
7025                 return NULL;                    /* reject */
7026
7027         if (doit)
7028                 xmloption = xo;
7029
7030         return newval;
7031 }
7032
7033 static bool
7034 assign_tcp_keepalives_idle(int newval, bool doit, GucSource source)
7035 {
7036         if (doit)
7037                 return (pq_setkeepalivesidle(newval, MyProcPort) == STATUS_OK);
7038
7039         return true;
7040 }
7041
7042 static const char *
7043 show_tcp_keepalives_idle(void)
7044 {
7045         static char nbuf[16];
7046
7047         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
7048         return nbuf;
7049 }
7050
7051 static bool
7052 assign_tcp_keepalives_interval(int newval, bool doit, GucSource source)
7053 {
7054         if (doit)
7055                 return (pq_setkeepalivesinterval(newval, MyProcPort) == STATUS_OK);
7056
7057         return true;
7058 }
7059
7060 static const char *
7061 show_tcp_keepalives_interval(void)
7062 {
7063         static char nbuf[16];
7064
7065         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
7066         return nbuf;
7067 }
7068
7069 static bool
7070 assign_tcp_keepalives_count(int newval, bool doit, GucSource source)
7071 {
7072         if (doit)
7073                 return (pq_setkeepalivescount(newval, MyProcPort) == STATUS_OK);
7074
7075         return true;
7076 }
7077
7078 static const char *
7079 show_tcp_keepalives_count(void)
7080 {
7081         static char nbuf[16];
7082
7083         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
7084         return nbuf;
7085 }
7086
7087 static bool
7088 assign_maxconnections(int newval, bool doit, GucSource source)
7089 {
7090         if (newval + autovacuum_max_workers > INT_MAX / 4)
7091                 return false;
7092
7093         if (doit)
7094                 MaxBackends = newval + autovacuum_max_workers;
7095
7096         return true;
7097 }
7098
7099 static bool
7100 assign_autovacuum_max_workers(int newval, bool doit, GucSource source)
7101 {
7102         if (newval + MaxConnections > INT_MAX / 4)
7103                 return false;
7104
7105         if (doit)
7106                 MaxBackends = newval + MaxConnections;
7107
7108         return true;
7109 }
7110
7111 #include "guc-file.c"