]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/guc.c
Allow external recovery_config_directory
[postgresql] / src / backend / utils / misc / guc.c
1 /*--------------------------------------------------------------------
2  * guc.c
3  *
4  * Support for grand unified configuration scheme, including SET
5  * command, configuration file, and command line options.
6  * See src/backend/utils/misc/README for more information.
7  *
8  *
9  * Copyright (c) 2000-2013, PostgreSQL Global Development Group
10  * Written by Peter Eisentraut <peter_e@gmx.net>.
11  *
12  * IDENTIFICATION
13  *        src/backend/utils/misc/guc.c
14  *
15  *--------------------------------------------------------------------
16  */
17 #include "postgres.h"
18
19 #include <ctype.h>
20 #include <float.h>
21 #include <math.h>
22 #include <limits.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #ifdef HAVE_SYSLOG
26 #include <syslog.h>
27 #endif
28
29 #include "access/gin.h"
30 #include "access/transam.h"
31 #include "access/twophase.h"
32 #include "access/xact.h"
33 #include "catalog/namespace.h"
34 #include "commands/async.h"
35 #include "commands/prepare.h"
36 #include "commands/vacuum.h"
37 #include "commands/variable.h"
38 #include "commands/trigger.h"
39 #include "funcapi.h"
40 #include "libpq/auth.h"
41 #include "libpq/be-fsstubs.h"
42 #include "libpq/libpq.h"
43 #include "libpq/pqformat.h"
44 #include "miscadmin.h"
45 #include "optimizer/cost.h"
46 #include "optimizer/geqo.h"
47 #include "optimizer/paths.h"
48 #include "optimizer/planmain.h"
49 #include "parser/parse_expr.h"
50 #include "parser/parse_type.h"
51 #include "parser/parser.h"
52 #include "parser/scansup.h"
53 #include "pgstat.h"
54 #include "postmaster/autovacuum.h"
55 #include "postmaster/bgworker.h"
56 #include "postmaster/bgwriter.h"
57 #include "postmaster/postmaster.h"
58 #include "postmaster/syslogger.h"
59 #include "postmaster/walwriter.h"
60 #include "replication/syncrep.h"
61 #include "replication/walreceiver.h"
62 #include "replication/walsender.h"
63 #include "storage/bufmgr.h"
64 #include "storage/standby.h"
65 #include "storage/fd.h"
66 #include "storage/proc.h"
67 #include "storage/predicate.h"
68 #include "tcop/tcopprot.h"
69 #include "tsearch/ts_cache.h"
70 #include "utils/builtins.h"
71 #include "utils/bytea.h"
72 #include "utils/guc_tables.h"
73 #include "utils/memutils.h"
74 #include "utils/pg_locale.h"
75 #include "utils/plancache.h"
76 #include "utils/portal.h"
77 #include "utils/ps_status.h"
78 #include "utils/snapmgr.h"
79 #include "utils/tzparser.h"
80 #include "utils/xml.h"
81
82 #ifndef PG_KRB_SRVTAB
83 #define PG_KRB_SRVTAB ""
84 #endif
85 #ifndef PG_KRB_SRVNAM
86 #define PG_KRB_SRVNAM ""
87 #endif
88
89 #define CONFIG_FILENAME "postgresql.conf"
90 #define HBA_FILENAME    "pg_hba.conf"
91 #define IDENT_FILENAME  "pg_ident.conf"
92
93 #ifdef EXEC_BACKEND
94 #define CONFIG_EXEC_PARAMS "global/config_exec_params"
95 #define CONFIG_EXEC_PARAMS_NEW "global/config_exec_params.new"
96 #endif
97
98 /* upper limit for GUC variables measured in kilobytes of memory */
99 /* note that various places assume the byte size fits in a "long" variable */
100 #if SIZEOF_SIZE_T > 4 && SIZEOF_LONG > 4
101 #define MAX_KILOBYTES   INT_MAX
102 #else
103 #define MAX_KILOBYTES   (INT_MAX / 1024)
104 #endif
105
106 #define KB_PER_MB (1024)
107 #define KB_PER_GB (1024*1024)
108
109 #define MS_PER_S 1000
110 #define S_PER_MIN 60
111 #define MS_PER_MIN (1000 * 60)
112 #define MIN_PER_H 60
113 #define S_PER_H (60 * 60)
114 #define MS_PER_H (1000 * 60 * 60)
115 #define MIN_PER_D (60 * 24)
116 #define S_PER_D (60 * 60 * 24)
117 #define MS_PER_D (1000 * 60 * 60 * 24)
118
119 /* XXX these should appear in other modules' header files */
120 extern bool Log_disconnections;
121 extern int      CommitDelay;
122 extern int      CommitSiblings;
123 extern char *default_tablespace;
124 extern char *temp_tablespaces;
125 extern bool ignore_checksum_failure;
126 extern bool synchronize_seqscans;
127 extern int      ssl_renegotiation_limit;
128 extern char *SSLCipherSuites;
129
130 #ifdef TRACE_SORT
131 extern bool trace_sort;
132 #endif
133 #ifdef TRACE_SYNCSCAN
134 extern bool trace_syncscan;
135 #endif
136 #ifdef DEBUG_BOUNDED_SORT
137 extern bool optimize_bounded_sort;
138 #endif
139
140 static int      GUC_check_errcode_value;
141
142 /* global variables for check hook support */
143 char       *GUC_check_errmsg_string;
144 char       *GUC_check_errdetail_string;
145 char       *GUC_check_errhint_string;
146
147
148 static void set_config_sourcefile(const char *name, char *sourcefile,
149                                           int sourceline);
150 static bool call_bool_check_hook(struct config_bool * conf, bool *newval,
151                                          void **extra, GucSource source, int elevel);
152 static bool call_int_check_hook(struct config_int * conf, int *newval,
153                                         void **extra, GucSource source, int elevel);
154 static bool call_real_check_hook(struct config_real * conf, double *newval,
155                                          void **extra, GucSource source, int elevel);
156 static bool call_string_check_hook(struct config_string * conf, char **newval,
157                                            void **extra, GucSource source, int elevel);
158 static bool call_enum_check_hook(struct config_enum * conf, int *newval,
159                                          void **extra, GucSource source, int elevel);
160
161 static bool check_log_destination(char **newval, void **extra, GucSource source);
162 static void assign_log_destination(const char *newval, void *extra);
163
164 #ifdef HAVE_SYSLOG
165 static int      syslog_facility = LOG_LOCAL0;
166 #else
167 static int      syslog_facility = 0;
168 #endif
169
170 static void assign_syslog_facility(int newval, void *extra);
171 static void assign_syslog_ident(const char *newval, void *extra);
172 static void assign_session_replication_role(int newval, void *extra);
173 static bool check_temp_buffers(int *newval, void **extra, GucSource source);
174 static bool check_phony_autocommit(bool *newval, void **extra, GucSource source);
175 static bool check_debug_assertions(bool *newval, void **extra, GucSource source);
176 static bool check_bonjour(bool *newval, void **extra, GucSource source);
177 static bool check_ssl(bool *newval, void **extra, GucSource source);
178 static bool check_stage_log_stats(bool *newval, void **extra, GucSource source);
179 static bool check_log_stats(bool *newval, void **extra, GucSource source);
180 static bool check_canonical_path(char **newval, void **extra, GucSource source);
181 static bool check_timezone_abbreviations(char **newval, void **extra, GucSource source);
182 static void assign_timezone_abbreviations(const char *newval, void *extra);
183 static void pg_timezone_abbrev_initialize(void);
184 static const char *show_archive_command(void);
185 static void assign_tcp_keepalives_idle(int newval, void *extra);
186 static void assign_tcp_keepalives_interval(int newval, void *extra);
187 static void assign_tcp_keepalives_count(int newval, void *extra);
188 static const char *show_tcp_keepalives_idle(void);
189 static const char *show_tcp_keepalives_interval(void);
190 static const char *show_tcp_keepalives_count(void);
191 static bool check_maxconnections(int *newval, void **extra, GucSource source);
192 static bool check_autovacuum_max_workers(int *newval, void **extra, GucSource source);
193 static bool check_effective_io_concurrency(int *newval, void **extra, GucSource source);
194 static void assign_effective_io_concurrency(int newval, void *extra);
195 static void assign_pgstat_temp_directory(const char *newval, void *extra);
196 static bool check_application_name(char **newval, void **extra, GucSource source);
197 static void assign_application_name(const char *newval, void *extra);
198 static const char *show_unix_socket_permissions(void);
199 static const char *show_log_file_mode(void);
200
201 static char *config_enum_get_options(struct config_enum * record,
202                                                 const char *prefix, const char *suffix,
203                                                 const char *separator);
204
205
206 /*
207  * Options for enum values defined in this module.
208  *
209  * NOTE! Option values may not contain double quotes!
210  */
211
212 static const struct config_enum_entry bytea_output_options[] = {
213         {"escape", BYTEA_OUTPUT_ESCAPE, false},
214         {"hex", BYTEA_OUTPUT_HEX, false},
215         {NULL, 0, false}
216 };
217
218 /*
219  * We have different sets for client and server message level options because
220  * they sort slightly different (see "log" level)
221  */
222 static const struct config_enum_entry client_message_level_options[] = {
223         {"debug", DEBUG2, true},
224         {"debug5", DEBUG5, false},
225         {"debug4", DEBUG4, false},
226         {"debug3", DEBUG3, false},
227         {"debug2", DEBUG2, false},
228         {"debug1", DEBUG1, false},
229         {"log", LOG, false},
230         {"info", INFO, true},
231         {"notice", NOTICE, false},
232         {"warning", WARNING, false},
233         {"error", ERROR, false},
234         {"fatal", FATAL, true},
235         {"panic", PANIC, true},
236         {NULL, 0, false}
237 };
238
239 static const struct config_enum_entry server_message_level_options[] = {
240         {"debug", DEBUG2, true},
241         {"debug5", DEBUG5, false},
242         {"debug4", DEBUG4, false},
243         {"debug3", DEBUG3, false},
244         {"debug2", DEBUG2, false},
245         {"debug1", DEBUG1, false},
246         {"info", INFO, false},
247         {"notice", NOTICE, false},
248         {"warning", WARNING, false},
249         {"error", ERROR, false},
250         {"log", LOG, false},
251         {"fatal", FATAL, false},
252         {"panic", PANIC, false},
253         {NULL, 0, false}
254 };
255
256 static const struct config_enum_entry intervalstyle_options[] = {
257         {"postgres", INTSTYLE_POSTGRES, false},
258         {"postgres_verbose", INTSTYLE_POSTGRES_VERBOSE, false},
259         {"sql_standard", INTSTYLE_SQL_STANDARD, false},
260         {"iso_8601", INTSTYLE_ISO_8601, false},
261         {NULL, 0, false}
262 };
263
264 static const struct config_enum_entry log_error_verbosity_options[] = {
265         {"terse", PGERROR_TERSE, false},
266         {"default", PGERROR_DEFAULT, false},
267         {"verbose", PGERROR_VERBOSE, false},
268         {NULL, 0, false}
269 };
270
271 static const struct config_enum_entry log_statement_options[] = {
272         {"none", LOGSTMT_NONE, false},
273         {"ddl", LOGSTMT_DDL, false},
274         {"mod", LOGSTMT_MOD, false},
275         {"all", LOGSTMT_ALL, false},
276         {NULL, 0, false}
277 };
278
279 static const struct config_enum_entry isolation_level_options[] = {
280         {"serializable", XACT_SERIALIZABLE, false},
281         {"repeatable read", XACT_REPEATABLE_READ, false},
282         {"read committed", XACT_READ_COMMITTED, false},
283         {"read uncommitted", XACT_READ_UNCOMMITTED, false},
284         {NULL, 0}
285 };
286
287 static const struct config_enum_entry session_replication_role_options[] = {
288         {"origin", SESSION_REPLICATION_ROLE_ORIGIN, false},
289         {"replica", SESSION_REPLICATION_ROLE_REPLICA, false},
290         {"local", SESSION_REPLICATION_ROLE_LOCAL, false},
291         {NULL, 0, false}
292 };
293
294 static const struct config_enum_entry syslog_facility_options[] = {
295 #ifdef HAVE_SYSLOG
296         {"local0", LOG_LOCAL0, false},
297         {"local1", LOG_LOCAL1, false},
298         {"local2", LOG_LOCAL2, false},
299         {"local3", LOG_LOCAL3, false},
300         {"local4", LOG_LOCAL4, false},
301         {"local5", LOG_LOCAL5, false},
302         {"local6", LOG_LOCAL6, false},
303         {"local7", LOG_LOCAL7, false},
304 #else
305         {"none", 0, false},
306 #endif
307         {NULL, 0}
308 };
309
310 static const struct config_enum_entry track_function_options[] = {
311         {"none", TRACK_FUNC_OFF, false},
312         {"pl", TRACK_FUNC_PL, false},
313         {"all", TRACK_FUNC_ALL, false},
314         {NULL, 0, false}
315 };
316
317 static const struct config_enum_entry xmlbinary_options[] = {
318         {"base64", XMLBINARY_BASE64, false},
319         {"hex", XMLBINARY_HEX, false},
320         {NULL, 0, false}
321 };
322
323 static const struct config_enum_entry xmloption_options[] = {
324         {"content", XMLOPTION_CONTENT, false},
325         {"document", XMLOPTION_DOCUMENT, false},
326         {NULL, 0, false}
327 };
328
329 /*
330  * Although only "on", "off", and "safe_encoding" are documented, we
331  * accept all the likely variants of "on" and "off".
332  */
333 static const struct config_enum_entry backslash_quote_options[] = {
334         {"safe_encoding", BACKSLASH_QUOTE_SAFE_ENCODING, false},
335         {"on", BACKSLASH_QUOTE_ON, false},
336         {"off", BACKSLASH_QUOTE_OFF, false},
337         {"true", BACKSLASH_QUOTE_ON, true},
338         {"false", BACKSLASH_QUOTE_OFF, true},
339         {"yes", BACKSLASH_QUOTE_ON, true},
340         {"no", BACKSLASH_QUOTE_OFF, true},
341         {"1", BACKSLASH_QUOTE_ON, true},
342         {"0", BACKSLASH_QUOTE_OFF, true},
343         {NULL, 0, false}
344 };
345
346 /*
347  * Although only "on", "off", and "partition" are documented, we
348  * accept all the likely variants of "on" and "off".
349  */
350 static const struct config_enum_entry constraint_exclusion_options[] = {
351         {"partition", CONSTRAINT_EXCLUSION_PARTITION, false},
352         {"on", CONSTRAINT_EXCLUSION_ON, false},
353         {"off", CONSTRAINT_EXCLUSION_OFF, false},
354         {"true", CONSTRAINT_EXCLUSION_ON, true},
355         {"false", CONSTRAINT_EXCLUSION_OFF, true},
356         {"yes", CONSTRAINT_EXCLUSION_ON, true},
357         {"no", CONSTRAINT_EXCLUSION_OFF, true},
358         {"1", CONSTRAINT_EXCLUSION_ON, true},
359         {"0", CONSTRAINT_EXCLUSION_OFF, true},
360         {NULL, 0, false}
361 };
362
363 /*
364  * Although only "on", "off", "remote_write", and "local" are documented, we
365  * accept all the likely variants of "on" and "off".
366  */
367 static const struct config_enum_entry synchronous_commit_options[] = {
368         {"local", SYNCHRONOUS_COMMIT_LOCAL_FLUSH, false},
369         {"remote_write", SYNCHRONOUS_COMMIT_REMOTE_WRITE, false},
370         {"on", SYNCHRONOUS_COMMIT_ON, false},
371         {"off", SYNCHRONOUS_COMMIT_OFF, false},
372         {"true", SYNCHRONOUS_COMMIT_ON, true},
373         {"false", SYNCHRONOUS_COMMIT_OFF, true},
374         {"yes", SYNCHRONOUS_COMMIT_ON, true},
375         {"no", SYNCHRONOUS_COMMIT_OFF, true},
376         {"1", SYNCHRONOUS_COMMIT_ON, true},
377         {"0", SYNCHRONOUS_COMMIT_OFF, true},
378         {NULL, 0, false}
379 };
380
381 /*
382  * Options for enum values stored in other modules
383  */
384 extern const struct config_enum_entry wal_level_options[];
385 extern const struct config_enum_entry sync_method_options[];
386
387 /*
388  * GUC option variables that are exported from this module
389  */
390 #ifdef USE_ASSERT_CHECKING
391 bool            assert_enabled = true;
392 #else
393 bool            assert_enabled = false;
394 #endif
395 bool            log_duration = false;
396 bool            Debug_print_plan = false;
397 bool            Debug_print_parse = false;
398 bool            Debug_print_rewritten = false;
399 bool            Debug_pretty_print = true;
400
401 bool            log_parser_stats = false;
402 bool            log_planner_stats = false;
403 bool            log_executor_stats = false;
404 bool            log_statement_stats = false;            /* this is sort of all three
405                                                                                                  * above together */
406 bool            log_btree_build_stats = false;
407 char       *event_source;
408
409 bool            check_function_bodies = true;
410 bool            default_with_oids = false;
411 bool            SQL_inheritance = true;
412
413 bool            Password_encryption = true;
414
415 int                     log_min_error_statement = ERROR;
416 int                     log_min_messages = WARNING;
417 int                     client_min_messages = NOTICE;
418 int                     log_min_duration_statement = -1;
419 int                     log_temp_files = -1;
420 int                     trace_recovery_messages = LOG;
421
422 int                     temp_file_limit = -1;
423
424 int                     num_temp_buffers = 1024;
425
426 char       *data_directory;
427 char       *recovery_config_directory;
428 char       *ConfigFileName;
429 char       *HbaFileName;
430 char       *IdentFileName;
431 char       *external_pid_file;
432
433 char       *pgstat_temp_directory;
434
435 char       *application_name;
436
437 int                     tcp_keepalives_idle;
438 int                     tcp_keepalives_interval;
439 int                     tcp_keepalives_count;
440
441 /*
442  * These variables are all dummies that don't do anything, except in some
443  * cases provide the value for SHOW to display.  The real state is elsewhere
444  * and is kept in sync by assign_hooks.
445  */
446 static char *log_destination_string;
447
448 static char *syslog_ident_str;
449 static bool phony_autocommit;
450 static bool session_auth_is_superuser;
451 static double phony_random_seed;
452 static char *client_encoding_string;
453 static char *datestyle_string;
454 static char *locale_collate;
455 static char *locale_ctype;
456 static char *server_encoding_string;
457 static char *server_version_string;
458 static int      server_version_num;
459 static char *timezone_string;
460 static char *log_timezone_string;
461 static char *timezone_abbreviations_string;
462 static char *XactIsoLevel_string;
463 static char *session_authorization_string;
464 static int      max_function_args;
465 static int      max_index_keys;
466 static int      max_identifier_length;
467 static int      block_size;
468 static int      segment_size;
469 static int      wal_block_size;
470 static int      wal_segment_size;
471 static bool integer_datetimes;
472 static int      effective_io_concurrency;
473
474 /* should be static, but commands/variable.c needs to get at this */
475 char       *role_string;
476
477
478 /*
479  * Displayable names for context types (enum GucContext)
480  *
481  * Note: these strings are deliberately not localized.
482  */
483 const char *const GucContext_Names[] =
484 {
485          /* PGC_INTERNAL */ "internal",
486          /* PGC_POSTMASTER */ "postmaster",
487          /* PGC_SIGHUP */ "sighup",
488          /* PGC_BACKEND */ "backend",
489          /* PGC_SUSET */ "superuser",
490          /* PGC_USERSET */ "user"
491 };
492
493 /*
494  * Displayable names for source types (enum GucSource)
495  *
496  * Note: these strings are deliberately not localized.
497  */
498 const char *const GucSource_Names[] =
499 {
500          /* PGC_S_DEFAULT */ "default",
501          /* PGC_S_DYNAMIC_DEFAULT */ "default",
502          /* PGC_S_ENV_VAR */ "environment variable",
503          /* PGC_S_FILE */ "configuration file",
504          /* PGC_S_ARGV */ "command line",
505          /* PGC_S_GLOBAL */ "global",
506          /* PGC_S_DATABASE */ "database",
507          /* PGC_S_USER */ "user",
508          /* PGC_S_DATABASE_USER */ "database user",
509          /* PGC_S_CLIENT */ "client",
510          /* PGC_S_OVERRIDE */ "override",
511          /* PGC_S_INTERACTIVE */ "interactive",
512          /* PGC_S_TEST */ "test",
513          /* PGC_S_SESSION */ "session"
514 };
515
516 /*
517  * Displayable names for the groupings defined in enum config_group
518  */
519 const char *const config_group_names[] =
520 {
521         /* UNGROUPED */
522         gettext_noop("Ungrouped"),
523         /* FILE_LOCATIONS */
524         gettext_noop("File Locations"),
525         /* CONN_AUTH */
526         gettext_noop("Connections and Authentication"),
527         /* CONN_AUTH_SETTINGS */
528         gettext_noop("Connections and Authentication / Connection Settings"),
529         /* CONN_AUTH_SECURITY */
530         gettext_noop("Connections and Authentication / Security and Authentication"),
531         /* RESOURCES */
532         gettext_noop("Resource Usage"),
533         /* RESOURCES_MEM */
534         gettext_noop("Resource Usage / Memory"),
535         /* RESOURCES_DISK */
536         gettext_noop("Resource Usage / Disk"),
537         /* RESOURCES_KERNEL */
538         gettext_noop("Resource Usage / Kernel Resources"),
539         /* RESOURCES_VACUUM_DELAY */
540         gettext_noop("Resource Usage / Cost-Based Vacuum Delay"),
541         /* RESOURCES_BGWRITER */
542         gettext_noop("Resource Usage / Background Writer"),
543         /* RESOURCES_ASYNCHRONOUS */
544         gettext_noop("Resource Usage / Asynchronous Behavior"),
545         /* WAL */
546         gettext_noop("Write-Ahead Log"),
547         /* WAL_SETTINGS */
548         gettext_noop("Write-Ahead Log / Settings"),
549         /* WAL_CHECKPOINTS */
550         gettext_noop("Write-Ahead Log / Checkpoints"),
551         /* WAL_ARCHIVING */
552         gettext_noop("Write-Ahead Log / Archiving"),
553         /* REPLICATION */
554         gettext_noop("Replication"),
555         /* REPLICATION_SENDING */
556         gettext_noop("Replication / Sending Servers"),
557         /* REPLICATION_MASTER */
558         gettext_noop("Replication / Master Server"),
559         /* REPLICATION_STANDBY */
560         gettext_noop("Replication / Standby Servers"),
561         /* QUERY_TUNING */
562         gettext_noop("Query Tuning"),
563         /* QUERY_TUNING_METHOD */
564         gettext_noop("Query Tuning / Planner Method Configuration"),
565         /* QUERY_TUNING_COST */
566         gettext_noop("Query Tuning / Planner Cost Constants"),
567         /* QUERY_TUNING_GEQO */
568         gettext_noop("Query Tuning / Genetic Query Optimizer"),
569         /* QUERY_TUNING_OTHER */
570         gettext_noop("Query Tuning / Other Planner Options"),
571         /* LOGGING */
572         gettext_noop("Reporting and Logging"),
573         /* LOGGING_WHERE */
574         gettext_noop("Reporting and Logging / Where to Log"),
575         /* LOGGING_WHEN */
576         gettext_noop("Reporting and Logging / When to Log"),
577         /* LOGGING_WHAT */
578         gettext_noop("Reporting and Logging / What to Log"),
579         /* STATS */
580         gettext_noop("Statistics"),
581         /* STATS_MONITORING */
582         gettext_noop("Statistics / Monitoring"),
583         /* STATS_COLLECTOR */
584         gettext_noop("Statistics / Query and Index Statistics Collector"),
585         /* AUTOVACUUM */
586         gettext_noop("Autovacuum"),
587         /* CLIENT_CONN */
588         gettext_noop("Client Connection Defaults"),
589         /* CLIENT_CONN_STATEMENT */
590         gettext_noop("Client Connection Defaults / Statement Behavior"),
591         /* CLIENT_CONN_LOCALE */
592         gettext_noop("Client Connection Defaults / Locale and Formatting"),
593         /* CLIENT_CONN_OTHER */
594         gettext_noop("Client Connection Defaults / Other Defaults"),
595         /* LOCK_MANAGEMENT */
596         gettext_noop("Lock Management"),
597         /* COMPAT_OPTIONS */
598         gettext_noop("Version and Platform Compatibility"),
599         /* COMPAT_OPTIONS_PREVIOUS */
600         gettext_noop("Version and Platform Compatibility / Previous PostgreSQL Versions"),
601         /* COMPAT_OPTIONS_CLIENT */
602         gettext_noop("Version and Platform Compatibility / Other Platforms and Clients"),
603         /* ERROR_HANDLING */
604         gettext_noop("Error Handling"),
605         /* PRESET_OPTIONS */
606         gettext_noop("Preset Options"),
607         /* CUSTOM_OPTIONS */
608         gettext_noop("Customized Options"),
609         /* DEVELOPER_OPTIONS */
610         gettext_noop("Developer Options"),
611         /* help_config wants this array to be null-terminated */
612         NULL
613 };
614
615 /*
616  * Displayable names for GUC variable types (enum config_type)
617  *
618  * Note: these strings are deliberately not localized.
619  */
620 const char *const config_type_names[] =
621 {
622          /* PGC_BOOL */ "bool",
623          /* PGC_INT */ "integer",
624          /* PGC_REAL */ "real",
625          /* PGC_STRING */ "string",
626          /* PGC_ENUM */ "enum"
627 };
628
629
630 /*
631  * Contents of GUC tables
632  *
633  * See src/backend/utils/misc/README for design notes.
634  *
635  * TO ADD AN OPTION:
636  *
637  * 1. Declare a global variable of type bool, int, double, or char*
638  *        and make use of it.
639  *
640  * 2. Decide at what times it's safe to set the option. See guc.h for
641  *        details.
642  *
643  * 3. Decide on a name, a default value, upper and lower bounds (if
644  *        applicable), etc.
645  *
646  * 4. Add a record below.
647  *
648  * 5. Add it to src/backend/utils/misc/postgresql.conf.sample, if
649  *        appropriate.
650  *
651  * 6. Don't forget to document the option (at least in config.sgml).
652  *
653  * 7. If it's a new GUC_LIST option you must edit pg_dumpall.c to ensure
654  *        it is not single quoted at dump time.
655  */
656
657
658 /******** option records follow ********/
659
660 static struct config_bool ConfigureNamesBool[] =
661 {
662         {
663                 {"enable_seqscan", PGC_USERSET, QUERY_TUNING_METHOD,
664                         gettext_noop("Enables the planner's use of sequential-scan plans."),
665                         NULL
666                 },
667                 &enable_seqscan,
668                 true,
669                 NULL, NULL, NULL
670         },
671         {
672                 {"enable_indexscan", PGC_USERSET, QUERY_TUNING_METHOD,
673                         gettext_noop("Enables the planner's use of index-scan plans."),
674                         NULL
675                 },
676                 &enable_indexscan,
677                 true,
678                 NULL, NULL, NULL
679         },
680         {
681                 {"enable_indexonlyscan", PGC_USERSET, QUERY_TUNING_METHOD,
682                         gettext_noop("Enables the planner's use of index-only-scan plans."),
683                         NULL
684                 },
685                 &enable_indexonlyscan,
686                 true,
687                 NULL, NULL, NULL
688         },
689         {
690                 {"enable_bitmapscan", PGC_USERSET, QUERY_TUNING_METHOD,
691                         gettext_noop("Enables the planner's use of bitmap-scan plans."),
692                         NULL
693                 },
694                 &enable_bitmapscan,
695                 true,
696                 NULL, NULL, NULL
697         },
698         {
699                 {"enable_tidscan", PGC_USERSET, QUERY_TUNING_METHOD,
700                         gettext_noop("Enables the planner's use of TID scan plans."),
701                         NULL
702                 },
703                 &enable_tidscan,
704                 true,
705                 NULL, NULL, NULL
706         },
707         {
708                 {"enable_sort", PGC_USERSET, QUERY_TUNING_METHOD,
709                         gettext_noop("Enables the planner's use of explicit sort steps."),
710                         NULL
711                 },
712                 &enable_sort,
713                 true,
714                 NULL, NULL, NULL
715         },
716         {
717                 {"enable_hashagg", PGC_USERSET, QUERY_TUNING_METHOD,
718                         gettext_noop("Enables the planner's use of hashed aggregation plans."),
719                         NULL
720                 },
721                 &enable_hashagg,
722                 true,
723                 NULL, NULL, NULL
724         },
725         {
726                 {"enable_material", PGC_USERSET, QUERY_TUNING_METHOD,
727                         gettext_noop("Enables the planner's use of materialization."),
728                         NULL
729                 },
730                 &enable_material,
731                 true,
732                 NULL, NULL, NULL
733         },
734         {
735                 {"enable_nestloop", PGC_USERSET, QUERY_TUNING_METHOD,
736                         gettext_noop("Enables the planner's use of nested-loop join plans."),
737                         NULL
738                 },
739                 &enable_nestloop,
740                 true,
741                 NULL, NULL, NULL
742         },
743         {
744                 {"enable_mergejoin", PGC_USERSET, QUERY_TUNING_METHOD,
745                         gettext_noop("Enables the planner's use of merge join plans."),
746                         NULL
747                 },
748                 &enable_mergejoin,
749                 true,
750                 NULL, NULL, NULL
751         },
752         {
753                 {"enable_hashjoin", PGC_USERSET, QUERY_TUNING_METHOD,
754                         gettext_noop("Enables the planner's use of hash join plans."),
755                         NULL
756                 },
757                 &enable_hashjoin,
758                 true,
759                 NULL, NULL, NULL
760         },
761         {
762                 {"geqo", PGC_USERSET, QUERY_TUNING_GEQO,
763                         gettext_noop("Enables genetic query optimization."),
764                         gettext_noop("This algorithm attempts to do planning without "
765                                                  "exhaustive searching.")
766                 },
767                 &enable_geqo,
768                 true,
769                 NULL, NULL, NULL
770         },
771         {
772                 /* Not for general use --- used by SET SESSION AUTHORIZATION */
773                 {"is_superuser", PGC_INTERNAL, UNGROUPED,
774                         gettext_noop("Shows whether the current user is a superuser."),
775                         NULL,
776                         GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
777                 },
778                 &session_auth_is_superuser,
779                 false,
780                 NULL, NULL, NULL
781         },
782         {
783                 {"bonjour", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
784                         gettext_noop("Enables advertising the server via Bonjour."),
785                         NULL
786                 },
787                 &enable_bonjour,
788                 false,
789                 check_bonjour, NULL, NULL
790         },
791         {
792                 {"ssl", PGC_POSTMASTER, CONN_AUTH_SECURITY,
793                         gettext_noop("Enables SSL connections."),
794                         NULL
795                 },
796                 &EnableSSL,
797                 false,
798                 check_ssl, NULL, NULL
799         },
800         {
801                 {"fsync", PGC_SIGHUP, WAL_SETTINGS,
802                         gettext_noop("Forces synchronization of updates to disk."),
803                         gettext_noop("The server will use the fsync() system call in several places to make "
804                         "sure that updates are physically written to disk. This insures "
805                                                  "that a database cluster will recover to a consistent state after "
806                                                  "an operating system or hardware crash.")
807                 },
808                 &enableFsync,
809                 true,
810                 NULL, NULL, NULL
811         },
812         {
813                 {"ignore_checksum_failure", PGC_SUSET, DEVELOPER_OPTIONS,
814                         gettext_noop("Continues processing after a checksum failure."),
815                         gettext_noop("Detection of a checksum failure normally causes PostgreSQL to "
816                                 "report an error, aborting the current transaction. Setting "
817                                                  "ignore_checksum_failure to true causes the system to ignore the failure "
818                                                  "(but still report a warning), and continue processing. This "
819                                                  "behavior could cause crashes or other serious problems. Only "
820                                                  "has an effect if checksums are enabled."),
821                         GUC_NOT_IN_SAMPLE
822                 },
823                 &ignore_checksum_failure,
824                 false,
825                 NULL, NULL, NULL
826         },
827         {
828                 {"zero_damaged_pages", PGC_SUSET, DEVELOPER_OPTIONS,
829                         gettext_noop("Continues processing past damaged page headers."),
830                         gettext_noop("Detection of a damaged page header normally causes PostgreSQL to "
831                                 "report an error, aborting the current transaction. Setting "
832                                                  "zero_damaged_pages to true causes the system to instead report a "
833                                                  "warning, zero out the damaged page, and continue processing. This "
834                                                  "behavior will destroy data, namely all the rows on the damaged page."),
835                         GUC_NOT_IN_SAMPLE
836                 },
837                 &zero_damaged_pages,
838                 false,
839                 NULL, NULL, NULL
840         },
841         {
842                 {"full_page_writes", PGC_SIGHUP, WAL_SETTINGS,
843                         gettext_noop("Writes full pages to WAL when first modified after a checkpoint."),
844                         gettext_noop("A page write in process during an operating system crash might be "
845                                                  "only partially written to disk.  During recovery, the row changes "
846                           "stored in WAL are not enough to recover.  This option writes "
847                                                  "pages when first modified after a checkpoint to WAL so full recovery "
848                                                  "is possible.")
849                 },
850                 &fullPageWrites,
851                 true,
852                 NULL, NULL, NULL
853         },
854         {
855                 {"log_checkpoints", PGC_SIGHUP, LOGGING_WHAT,
856                         gettext_noop("Logs each checkpoint."),
857                         NULL
858                 },
859                 &log_checkpoints,
860                 false,
861                 NULL, NULL, NULL
862         },
863         {
864                 {"log_connections", PGC_BACKEND, LOGGING_WHAT,
865                         gettext_noop("Logs each successful connection."),
866                         NULL
867                 },
868                 &Log_connections,
869                 false,
870                 NULL, NULL, NULL
871         },
872         {
873                 {"log_disconnections", PGC_BACKEND, LOGGING_WHAT,
874                         gettext_noop("Logs end of a session, including duration."),
875                         NULL
876                 },
877                 &Log_disconnections,
878                 false,
879                 NULL, NULL, NULL
880         },
881         {
882                 {"debug_assertions", PGC_USERSET, DEVELOPER_OPTIONS,
883                         gettext_noop("Turns on various assertion checks."),
884                         gettext_noop("This is a debugging aid."),
885                         GUC_NOT_IN_SAMPLE
886                 },
887                 &assert_enabled,
888 #ifdef USE_ASSERT_CHECKING
889                 true,
890 #else
891                 false,
892 #endif
893                 check_debug_assertions, NULL, NULL
894         },
895
896         {
897                 {"exit_on_error", PGC_USERSET, ERROR_HANDLING_OPTIONS,
898                         gettext_noop("Terminate session on any error."),
899                         NULL
900                 },
901                 &ExitOnAnyError,
902                 false,
903                 NULL, NULL, NULL
904         },
905         {
906                 {"restart_after_crash", PGC_SIGHUP, ERROR_HANDLING_OPTIONS,
907                         gettext_noop("Reinitialize server after backend crash."),
908                         NULL
909                 },
910                 &restart_after_crash,
911                 true,
912                 NULL, NULL, NULL
913         },
914
915         {
916                 {"log_duration", PGC_SUSET, LOGGING_WHAT,
917                         gettext_noop("Logs the duration of each completed SQL statement."),
918                         NULL
919                 },
920                 &log_duration,
921                 false,
922                 NULL, NULL, NULL
923         },
924         {
925                 {"debug_print_parse", PGC_USERSET, LOGGING_WHAT,
926                         gettext_noop("Logs each query's parse tree."),
927                         NULL
928                 },
929                 &Debug_print_parse,
930                 false,
931                 NULL, NULL, NULL
932         },
933         {
934                 {"debug_print_rewritten", PGC_USERSET, LOGGING_WHAT,
935                         gettext_noop("Logs each query's rewritten parse tree."),
936                         NULL
937                 },
938                 &Debug_print_rewritten,
939                 false,
940                 NULL, NULL, NULL
941         },
942         {
943                 {"debug_print_plan", PGC_USERSET, LOGGING_WHAT,
944                         gettext_noop("Logs each query's execution plan."),
945                         NULL
946                 },
947                 &Debug_print_plan,
948                 false,
949                 NULL, NULL, NULL
950         },
951         {
952                 {"debug_pretty_print", PGC_USERSET, LOGGING_WHAT,
953                         gettext_noop("Indents parse and plan tree displays."),
954                         NULL
955                 },
956                 &Debug_pretty_print,
957                 true,
958                 NULL, NULL, NULL
959         },
960         {
961                 {"log_parser_stats", PGC_SUSET, STATS_MONITORING,
962                         gettext_noop("Writes parser performance statistics to the server log."),
963                         NULL
964                 },
965                 &log_parser_stats,
966                 false,
967                 check_stage_log_stats, NULL, NULL
968         },
969         {
970                 {"log_planner_stats", PGC_SUSET, STATS_MONITORING,
971                         gettext_noop("Writes planner performance statistics to the server log."),
972                         NULL
973                 },
974                 &log_planner_stats,
975                 false,
976                 check_stage_log_stats, NULL, NULL
977         },
978         {
979                 {"log_executor_stats", PGC_SUSET, STATS_MONITORING,
980                         gettext_noop("Writes executor performance statistics to the server log."),
981                         NULL
982                 },
983                 &log_executor_stats,
984                 false,
985                 check_stage_log_stats, NULL, NULL
986         },
987         {
988                 {"log_statement_stats", PGC_SUSET, STATS_MONITORING,
989                         gettext_noop("Writes cumulative performance statistics to the server log."),
990                         NULL
991                 },
992                 &log_statement_stats,
993                 false,
994                 check_log_stats, NULL, NULL
995         },
996 #ifdef BTREE_BUILD_STATS
997         {
998                 {"log_btree_build_stats", PGC_SUSET, DEVELOPER_OPTIONS,
999                         gettext_noop("No description available."),
1000                         NULL,
1001                         GUC_NOT_IN_SAMPLE
1002                 },
1003                 &log_btree_build_stats,
1004                 false,
1005                 NULL, NULL, NULL
1006         },
1007 #endif
1008
1009         {
1010                 {"track_activities", PGC_SUSET, STATS_COLLECTOR,
1011                         gettext_noop("Collects information about executing commands."),
1012                         gettext_noop("Enables the collection of information on the currently "
1013                                                  "executing command of each session, along with "
1014                                                  "the time at which that command began execution.")
1015                 },
1016                 &pgstat_track_activities,
1017                 true,
1018                 NULL, NULL, NULL
1019         },
1020         {
1021                 {"track_counts", PGC_SUSET, STATS_COLLECTOR,
1022                         gettext_noop("Collects statistics on database activity."),
1023                         NULL
1024                 },
1025                 &pgstat_track_counts,
1026                 true,
1027                 NULL, NULL, NULL
1028         },
1029         {
1030                 {"track_io_timing", PGC_SUSET, STATS_COLLECTOR,
1031                         gettext_noop("Collects timing statistics for database I/O activity."),
1032                         NULL
1033                 },
1034                 &track_io_timing,
1035                 false,
1036                 NULL, NULL, NULL
1037         },
1038
1039         {
1040                 {"update_process_title", PGC_SUSET, STATS_COLLECTOR,
1041                         gettext_noop("Updates the process title to show the active SQL command."),
1042                         gettext_noop("Enables updating of the process title every time a new SQL command is received by the server.")
1043                 },
1044                 &update_process_title,
1045                 true,
1046                 NULL, NULL, NULL
1047         },
1048
1049         {
1050                 {"autovacuum", PGC_SIGHUP, AUTOVACUUM,
1051                         gettext_noop("Starts the autovacuum subprocess."),
1052                         NULL
1053                 },
1054                 &autovacuum_start_daemon,
1055                 true,
1056                 NULL, NULL, NULL
1057         },
1058
1059         {
1060                 {"trace_notify", PGC_USERSET, DEVELOPER_OPTIONS,
1061                         gettext_noop("Generates debugging output for LISTEN and NOTIFY."),
1062                         NULL,
1063                         GUC_NOT_IN_SAMPLE
1064                 },
1065                 &Trace_notify,
1066                 false,
1067                 NULL, NULL, NULL
1068         },
1069
1070 #ifdef LOCK_DEBUG
1071         {
1072                 {"trace_locks", PGC_SUSET, DEVELOPER_OPTIONS,
1073                         gettext_noop("No description available."),
1074                         NULL,
1075                         GUC_NOT_IN_SAMPLE
1076                 },
1077                 &Trace_locks,
1078                 false,
1079                 NULL, NULL, NULL
1080         },
1081         {
1082                 {"trace_userlocks", PGC_SUSET, DEVELOPER_OPTIONS,
1083                         gettext_noop("No description available."),
1084                         NULL,
1085                         GUC_NOT_IN_SAMPLE
1086                 },
1087                 &Trace_userlocks,
1088                 false,
1089                 NULL, NULL, NULL
1090         },
1091         {
1092                 {"trace_lwlocks", PGC_SUSET, DEVELOPER_OPTIONS,
1093                         gettext_noop("No description available."),
1094                         NULL,
1095                         GUC_NOT_IN_SAMPLE
1096                 },
1097                 &Trace_lwlocks,
1098                 false,
1099                 NULL, NULL, NULL
1100         },
1101         {
1102                 {"debug_deadlocks", PGC_SUSET, DEVELOPER_OPTIONS,
1103                         gettext_noop("No description available."),
1104                         NULL,
1105                         GUC_NOT_IN_SAMPLE
1106                 },
1107                 &Debug_deadlocks,
1108                 false,
1109                 NULL, NULL, NULL
1110         },
1111 #endif
1112
1113         {
1114                 {"log_lock_waits", PGC_SUSET, LOGGING_WHAT,
1115                         gettext_noop("Logs long lock waits."),
1116                         NULL
1117                 },
1118                 &log_lock_waits,
1119                 false,
1120                 NULL, NULL, NULL
1121         },
1122
1123         {
1124                 {"log_hostname", PGC_SIGHUP, LOGGING_WHAT,
1125                         gettext_noop("Logs the host name in the connection logs."),
1126                         gettext_noop("By default, connection logs only show the IP address "
1127                                                  "of the connecting host. If you want them to show the host name you "
1128                           "can turn this on, but depending on your host name resolution "
1129                            "setup it might impose a non-negligible performance penalty.")
1130                 },
1131                 &log_hostname,
1132                 false,
1133                 NULL, NULL, NULL
1134         },
1135         {
1136                 {"sql_inheritance", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1137                         gettext_noop("Causes subtables to be included by default in various commands."),
1138                         NULL
1139                 },
1140                 &SQL_inheritance,
1141                 true,
1142                 NULL, NULL, NULL
1143         },
1144         {
1145                 {"password_encryption", PGC_USERSET, CONN_AUTH_SECURITY,
1146                         gettext_noop("Encrypt passwords."),
1147                         gettext_noop("When a password is specified in CREATE USER or "
1148                            "ALTER USER without writing either ENCRYPTED or UNENCRYPTED, "
1149                                                  "this parameter determines whether the password is to be encrypted.")
1150                 },
1151                 &Password_encryption,
1152                 true,
1153                 NULL, NULL, NULL
1154         },
1155         {
1156                 {"transform_null_equals", PGC_USERSET, COMPAT_OPTIONS_CLIENT,
1157                         gettext_noop("Treats \"expr=NULL\" as \"expr IS NULL\"."),
1158                         gettext_noop("When turned on, expressions of the form expr = NULL "
1159                            "(or NULL = expr) are treated as expr IS NULL, that is, they "
1160                                 "return true if expr evaluates to the null value, and false "
1161                            "otherwise. The correct behavior of expr = NULL is to always "
1162                                                  "return null (unknown).")
1163                 },
1164                 &Transform_null_equals,
1165                 false,
1166                 NULL, NULL, NULL
1167         },
1168         {
1169                 {"db_user_namespace", PGC_SIGHUP, CONN_AUTH_SECURITY,
1170                         gettext_noop("Enables per-database user names."),
1171                         NULL
1172                 },
1173                 &Db_user_namespace,
1174                 false,
1175                 NULL, NULL, NULL
1176         },
1177         {
1178                 /* only here for backwards compatibility */
1179                 {"autocommit", PGC_USERSET, CLIENT_CONN_STATEMENT,
1180                         gettext_noop("This parameter doesn't do anything."),
1181                         gettext_noop("It's just here so that we won't choke on SET AUTOCOMMIT TO ON from 7.3-vintage clients."),
1182                         GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE
1183                 },
1184                 &phony_autocommit,
1185                 true,
1186                 check_phony_autocommit, NULL, NULL
1187         },
1188         {
1189                 {"default_transaction_read_only", PGC_USERSET, CLIENT_CONN_STATEMENT,
1190                         gettext_noop("Sets the default read-only status of new transactions."),
1191                         NULL
1192                 },
1193                 &DefaultXactReadOnly,
1194                 false,
1195                 NULL, NULL, NULL
1196         },
1197         {
1198                 {"transaction_read_only", PGC_USERSET, CLIENT_CONN_STATEMENT,
1199                         gettext_noop("Sets the current transaction's read-only status."),
1200                         NULL,
1201                         GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1202                 },
1203                 &XactReadOnly,
1204                 false,
1205                 check_transaction_read_only, NULL, NULL
1206         },
1207         {
1208                 {"default_transaction_deferrable", PGC_USERSET, CLIENT_CONN_STATEMENT,
1209                         gettext_noop("Sets the default deferrable status of new transactions."),
1210                         NULL
1211                 },
1212                 &DefaultXactDeferrable,
1213                 false,
1214                 NULL, NULL, NULL
1215         },
1216         {
1217                 {"transaction_deferrable", PGC_USERSET, CLIENT_CONN_STATEMENT,
1218                         gettext_noop("Whether to defer a read-only serializable transaction until it can be executed with no possible serialization failures."),
1219                         NULL,
1220                         GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1221                 },
1222                 &XactDeferrable,
1223                 false,
1224                 check_transaction_deferrable, NULL, NULL
1225         },
1226         {
1227                 {"check_function_bodies", PGC_USERSET, CLIENT_CONN_STATEMENT,
1228                         gettext_noop("Check function bodies during CREATE FUNCTION."),
1229                         NULL
1230                 },
1231                 &check_function_bodies,
1232                 true,
1233                 NULL, NULL, NULL
1234         },
1235         {
1236                 {"array_nulls", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1237                         gettext_noop("Enable input of NULL elements in arrays."),
1238                         gettext_noop("When turned on, unquoted NULL in an array input "
1239                                                  "value means a null value; "
1240                                                  "otherwise it is taken literally.")
1241                 },
1242                 &Array_nulls,
1243                 true,
1244                 NULL, NULL, NULL
1245         },
1246         {
1247                 {"default_with_oids", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1248                         gettext_noop("Create new tables with OIDs by default."),
1249                         NULL
1250                 },
1251                 &default_with_oids,
1252                 false,
1253                 NULL, NULL, NULL
1254         },
1255         {
1256                 {"logging_collector", PGC_POSTMASTER, LOGGING_WHERE,
1257                         gettext_noop("Start a subprocess to capture stderr output and/or csvlogs into log files."),
1258                         NULL
1259                 },
1260                 &Logging_collector,
1261                 false,
1262                 NULL, NULL, NULL
1263         },
1264         {
1265                 {"log_truncate_on_rotation", PGC_SIGHUP, LOGGING_WHERE,
1266                         gettext_noop("Truncate existing log files of same name during log rotation."),
1267                         NULL
1268                 },
1269                 &Log_truncate_on_rotation,
1270                 false,
1271                 NULL, NULL, NULL
1272         },
1273
1274 #ifdef TRACE_SORT
1275         {
1276                 {"trace_sort", PGC_USERSET, DEVELOPER_OPTIONS,
1277                         gettext_noop("Emit information about resource usage in sorting."),
1278                         NULL,
1279                         GUC_NOT_IN_SAMPLE
1280                 },
1281                 &trace_sort,
1282                 false,
1283                 NULL, NULL, NULL
1284         },
1285 #endif
1286
1287 #ifdef TRACE_SYNCSCAN
1288         /* this is undocumented because not exposed in a standard build */
1289         {
1290                 {"trace_syncscan", PGC_USERSET, DEVELOPER_OPTIONS,
1291                         gettext_noop("Generate debugging output for synchronized scanning."),
1292                         NULL,
1293                         GUC_NOT_IN_SAMPLE
1294                 },
1295                 &trace_syncscan,
1296                 false,
1297                 NULL, NULL, NULL
1298         },
1299 #endif
1300
1301 #ifdef DEBUG_BOUNDED_SORT
1302         /* this is undocumented because not exposed in a standard build */
1303         {
1304                 {
1305                         "optimize_bounded_sort", PGC_USERSET, QUERY_TUNING_METHOD,
1306                         gettext_noop("Enable bounded sorting using heap sort."),
1307                         NULL,
1308                         GUC_NOT_IN_SAMPLE
1309                 },
1310                 &optimize_bounded_sort,
1311                 true,
1312                 NULL, NULL, NULL
1313         },
1314 #endif
1315
1316 #ifdef WAL_DEBUG
1317         {
1318                 {"wal_debug", PGC_SUSET, DEVELOPER_OPTIONS,
1319                         gettext_noop("Emit WAL-related debugging output."),
1320                         NULL,
1321                         GUC_NOT_IN_SAMPLE
1322                 },
1323                 &XLOG_DEBUG,
1324                 false,
1325                 NULL, NULL, NULL
1326         },
1327 #endif
1328
1329         {
1330                 {"integer_datetimes", PGC_INTERNAL, PRESET_OPTIONS,
1331                         gettext_noop("Datetimes are integer based."),
1332                         NULL,
1333                         GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1334                 },
1335                 &integer_datetimes,
1336 #ifdef HAVE_INT64_TIMESTAMP
1337                 true,
1338 #else
1339                 false,
1340 #endif
1341                 NULL, NULL, NULL
1342         },
1343
1344         {
1345                 {"krb_caseins_users", PGC_SIGHUP, CONN_AUTH_SECURITY,
1346                         gettext_noop("Sets whether Kerberos and GSSAPI user names should be treated as case-insensitive."),
1347                         NULL
1348                 },
1349                 &pg_krb_caseins_users,
1350                 false,
1351                 NULL, NULL, NULL
1352         },
1353
1354         {
1355                 {"escape_string_warning", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1356                         gettext_noop("Warn about backslash escapes in ordinary string literals."),
1357                         NULL
1358                 },
1359                 &escape_string_warning,
1360                 true,
1361                 NULL, NULL, NULL
1362         },
1363
1364         {
1365                 {"standard_conforming_strings", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1366                         gettext_noop("Causes '...' strings to treat backslashes literally."),
1367                         NULL,
1368                         GUC_REPORT
1369                 },
1370                 &standard_conforming_strings,
1371                 true,
1372                 NULL, NULL, NULL
1373         },
1374
1375         {
1376                 {"synchronize_seqscans", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1377                         gettext_noop("Enable synchronized sequential scans."),
1378                         NULL
1379                 },
1380                 &synchronize_seqscans,
1381                 true,
1382                 NULL, NULL, NULL
1383         },
1384
1385         {
1386                 {"archive_mode", PGC_POSTMASTER, WAL_ARCHIVING,
1387                         gettext_noop("Allows archiving of WAL files using archive_command."),
1388                         NULL
1389                 },
1390                 &XLogArchiveMode,
1391                 false,
1392                 NULL, NULL, NULL
1393         },
1394
1395         {
1396                 {"hot_standby", PGC_POSTMASTER, REPLICATION_STANDBY,
1397                         gettext_noop("Allows connections and queries during recovery."),
1398                         NULL
1399                 },
1400                 &EnableHotStandby,
1401                 false,
1402                 NULL, NULL, NULL
1403         },
1404
1405         {
1406                 {"hot_standby_feedback", PGC_SIGHUP, REPLICATION_STANDBY,
1407                         gettext_noop("Allows feedback from a hot standby to the primary that will avoid query conflicts."),
1408                         NULL
1409                 },
1410                 &hot_standby_feedback,
1411                 false,
1412                 NULL, NULL, NULL
1413         },
1414
1415         {
1416                 {"allow_system_table_mods", PGC_POSTMASTER, DEVELOPER_OPTIONS,
1417                         gettext_noop("Allows modifications of the structure of system tables."),
1418                         NULL,
1419                         GUC_NOT_IN_SAMPLE
1420                 },
1421                 &allowSystemTableMods,
1422                 false,
1423                 NULL, NULL, NULL
1424         },
1425
1426         {
1427                 {"ignore_system_indexes", PGC_BACKEND, DEVELOPER_OPTIONS,
1428                         gettext_noop("Disables reading from system indexes."),
1429                         gettext_noop("It does not prevent updating the indexes, so it is safe "
1430                                                  "to use.  The worst consequence is slowness."),
1431                         GUC_NOT_IN_SAMPLE
1432                 },
1433                 &IgnoreSystemIndexes,
1434                 false,
1435                 NULL, NULL, NULL
1436         },
1437
1438         {
1439                 {"lo_compat_privileges", PGC_SUSET, COMPAT_OPTIONS_PREVIOUS,
1440                         gettext_noop("Enables backward compatibility mode for privilege checks on large objects."),
1441                         gettext_noop("Skips privilege checks when reading or modifying large objects, "
1442                                   "for compatibility with PostgreSQL releases prior to 9.0.")
1443                 },
1444                 &lo_compat_privileges,
1445                 false,
1446                 NULL, NULL, NULL
1447         },
1448
1449         {
1450                 {"quote_all_identifiers", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1451                         gettext_noop("When generating SQL fragments, quote all identifiers."),
1452                         NULL,
1453                 },
1454                 &quote_all_identifiers,
1455                 false,
1456                 NULL, NULL, NULL
1457         },
1458
1459         /* End-of-list marker */
1460         {
1461                 {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL
1462         }
1463 };
1464
1465
1466 static struct config_int ConfigureNamesInt[] =
1467 {
1468         {
1469                 {"archive_timeout", PGC_SIGHUP, WAL_ARCHIVING,
1470                         gettext_noop("Forces a switch to the next xlog file if a "
1471                                                  "new file has not been started within N seconds."),
1472                         NULL,
1473                         GUC_UNIT_S
1474                 },
1475                 &XLogArchiveTimeout,
1476                 0, 0, INT_MAX / 2,
1477                 NULL, NULL, NULL
1478         },
1479         {
1480                 {"post_auth_delay", PGC_BACKEND, DEVELOPER_OPTIONS,
1481                         gettext_noop("Waits N seconds on connection startup after authentication."),
1482                         gettext_noop("This allows attaching a debugger to the process."),
1483                         GUC_NOT_IN_SAMPLE | GUC_UNIT_S
1484                 },
1485                 &PostAuthDelay,
1486                 0, 0, INT_MAX / 1000000,
1487                 NULL, NULL, NULL
1488         },
1489         {
1490                 {"default_statistics_target", PGC_USERSET, QUERY_TUNING_OTHER,
1491                         gettext_noop("Sets the default statistics target."),
1492                         gettext_noop("This applies to table columns that have not had a "
1493                                 "column-specific target set via ALTER TABLE SET STATISTICS.")
1494                 },
1495                 &default_statistics_target,
1496                 100, 1, 10000,
1497                 NULL, NULL, NULL
1498         },
1499         {
1500                 {"from_collapse_limit", PGC_USERSET, QUERY_TUNING_OTHER,
1501                         gettext_noop("Sets the FROM-list size beyond which subqueries "
1502                                                  "are not collapsed."),
1503                         gettext_noop("The planner will merge subqueries into upper "
1504                                 "queries if the resulting FROM list would have no more than "
1505                                                  "this many items.")
1506                 },
1507                 &from_collapse_limit,
1508                 8, 1, INT_MAX,
1509                 NULL, NULL, NULL
1510         },
1511         {
1512                 {"join_collapse_limit", PGC_USERSET, QUERY_TUNING_OTHER,
1513                         gettext_noop("Sets the FROM-list size beyond which JOIN "
1514                                                  "constructs are not flattened."),
1515                         gettext_noop("The planner will flatten explicit JOIN "
1516                                                  "constructs into lists of FROM items whenever a "
1517                                                  "list of no more than this many items would result.")
1518                 },
1519                 &join_collapse_limit,
1520                 8, 1, INT_MAX,
1521                 NULL, NULL, NULL
1522         },
1523         {
1524                 {"geqo_threshold", PGC_USERSET, QUERY_TUNING_GEQO,
1525                         gettext_noop("Sets the threshold of FROM items beyond which GEQO is used."),
1526                         NULL
1527                 },
1528                 &geqo_threshold,
1529                 12, 2, INT_MAX,
1530                 NULL, NULL, NULL
1531         },
1532         {
1533                 {"geqo_effort", PGC_USERSET, QUERY_TUNING_GEQO,
1534                         gettext_noop("GEQO: effort is used to set the default for other GEQO parameters."),
1535                         NULL
1536                 },
1537                 &Geqo_effort,
1538                 DEFAULT_GEQO_EFFORT, MIN_GEQO_EFFORT, MAX_GEQO_EFFORT,
1539                 NULL, NULL, NULL
1540         },
1541         {
1542                 {"geqo_pool_size", PGC_USERSET, QUERY_TUNING_GEQO,
1543                         gettext_noop("GEQO: number of individuals in the population."),
1544                         gettext_noop("Zero selects a suitable default value.")
1545                 },
1546                 &Geqo_pool_size,
1547                 0, 0, INT_MAX,
1548                 NULL, NULL, NULL
1549         },
1550         {
1551                 {"geqo_generations", PGC_USERSET, QUERY_TUNING_GEQO,
1552                         gettext_noop("GEQO: number of iterations of the algorithm."),
1553                         gettext_noop("Zero selects a suitable default value.")
1554                 },
1555                 &Geqo_generations,
1556                 0, 0, INT_MAX,
1557                 NULL, NULL, NULL
1558         },
1559
1560         {
1561                 /* This is PGC_SUSET to prevent hiding from log_lock_waits. */
1562                 {"deadlock_timeout", PGC_SUSET, LOCK_MANAGEMENT,
1563                         gettext_noop("Sets the time to wait on a lock before checking for deadlock."),
1564                         NULL,
1565                         GUC_UNIT_MS
1566                 },
1567                 &DeadlockTimeout,
1568                 1000, 1, INT_MAX,
1569                 NULL, NULL, NULL
1570         },
1571
1572         {
1573                 {"max_standby_archive_delay", PGC_SIGHUP, REPLICATION_STANDBY,
1574                         gettext_noop("Sets the maximum delay before canceling queries when a hot standby server is processing archived WAL data."),
1575                         NULL,
1576                         GUC_UNIT_MS
1577                 },
1578                 &max_standby_archive_delay,
1579                 30 * 1000, -1, INT_MAX,
1580                 NULL, NULL, NULL
1581         },
1582
1583         {
1584                 {"max_standby_streaming_delay", PGC_SIGHUP, REPLICATION_STANDBY,
1585                         gettext_noop("Sets the maximum delay before canceling queries when a hot standby server is processing streamed WAL data."),
1586                         NULL,
1587                         GUC_UNIT_MS
1588                 },
1589                 &max_standby_streaming_delay,
1590                 30 * 1000, -1, INT_MAX,
1591                 NULL, NULL, NULL
1592         },
1593
1594         {
1595                 {"wal_receiver_status_interval", PGC_SIGHUP, REPLICATION_STANDBY,
1596                         gettext_noop("Sets the maximum interval between WAL receiver status reports to the primary."),
1597                         NULL,
1598                         GUC_UNIT_S
1599                 },
1600                 &wal_receiver_status_interval,
1601                 10, 0, INT_MAX / 1000,
1602                 NULL, NULL, NULL
1603         },
1604
1605         {
1606                 {"wal_receiver_timeout", PGC_SIGHUP, REPLICATION_STANDBY,
1607                         gettext_noop("Sets the maximum wait time to receive data from master."),
1608                         NULL,
1609                         GUC_UNIT_MS
1610                 },
1611                 &wal_receiver_timeout,
1612                 60 * 1000, 0, INT_MAX,
1613                 NULL, NULL, NULL
1614         },
1615
1616         {
1617                 {"max_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1618                         gettext_noop("Sets the maximum number of concurrent connections."),
1619                         NULL
1620                 },
1621                 &MaxConnections,
1622                 100, 1, MAX_BACKENDS,
1623                 check_maxconnections, NULL, NULL
1624         },
1625
1626         {
1627                 {"superuser_reserved_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1628                         gettext_noop("Sets the number of connection slots reserved for superusers."),
1629                         NULL
1630                 },
1631                 &ReservedBackends,
1632                 3, 0, MAX_BACKENDS,
1633                 NULL, NULL, NULL
1634         },
1635
1636         /*
1637          * We sometimes multiply the number of shared buffers by two without
1638          * checking for overflow, so we mustn't allow more than INT_MAX / 2.
1639          */
1640         {
1641                 {"shared_buffers", PGC_POSTMASTER, RESOURCES_MEM,
1642                         gettext_noop("Sets the number of shared memory buffers used by the server."),
1643                         NULL,
1644                         GUC_UNIT_BLOCKS
1645                 },
1646                 &NBuffers,
1647                 1024, 16, INT_MAX / 2,
1648                 NULL, NULL, NULL
1649         },
1650
1651         {
1652                 {"temp_buffers", PGC_USERSET, RESOURCES_MEM,
1653                         gettext_noop("Sets the maximum number of temporary buffers used by each session."),
1654                         NULL,
1655                         GUC_UNIT_BLOCKS
1656                 },
1657                 &num_temp_buffers,
1658                 1024, 100, INT_MAX / 2,
1659                 check_temp_buffers, NULL, NULL
1660         },
1661
1662         {
1663                 {"port", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1664                         gettext_noop("Sets the TCP port the server listens on."),
1665                         NULL
1666                 },
1667                 &PostPortNumber,
1668                 DEF_PGPORT, 1, 65535,
1669                 NULL, NULL, NULL
1670         },
1671
1672         {
1673                 {"unix_socket_permissions", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1674                         gettext_noop("Sets the access permissions of the Unix-domain socket."),
1675                         gettext_noop("Unix-domain sockets use the usual Unix file system "
1676                                                  "permission set. The parameter value is expected "
1677                                                  "to be a numeric mode specification in the form "
1678                                                  "accepted by the chmod and umask system calls. "
1679                                                  "(To use the customary octal format the number must "
1680                                                  "start with a 0 (zero).)")
1681                 },
1682                 &Unix_socket_permissions,
1683                 0777, 0000, 0777,
1684                 NULL, NULL, show_unix_socket_permissions
1685         },
1686
1687         {
1688                 {"log_file_mode", PGC_SIGHUP, LOGGING_WHERE,
1689                         gettext_noop("Sets the file permissions for log files."),
1690                         gettext_noop("The parameter value is expected "
1691                                                  "to be a numeric mode specification in the form "
1692                                                  "accepted by the chmod and umask system calls. "
1693                                                  "(To use the customary octal format the number must "
1694                                                  "start with a 0 (zero).)")
1695                 },
1696                 &Log_file_mode,
1697                 0600, 0000, 0777,
1698                 NULL, NULL, show_log_file_mode
1699         },
1700
1701         {
1702                 {"work_mem", PGC_USERSET, RESOURCES_MEM,
1703                         gettext_noop("Sets the maximum memory to be used for query workspaces."),
1704                         gettext_noop("This much memory can be used by each internal "
1705                                                  "sort operation and hash table before switching to "
1706                                                  "temporary disk files."),
1707                         GUC_UNIT_KB
1708                 },
1709                 &work_mem,
1710                 1024, 64, MAX_KILOBYTES,
1711                 NULL, NULL, NULL
1712         },
1713
1714         {
1715                 {"maintenance_work_mem", PGC_USERSET, RESOURCES_MEM,
1716                         gettext_noop("Sets the maximum memory to be used for maintenance operations."),
1717                         gettext_noop("This includes operations such as VACUUM and CREATE INDEX."),
1718                         GUC_UNIT_KB
1719                 },
1720                 &maintenance_work_mem,
1721                 16384, 1024, MAX_KILOBYTES,
1722                 NULL, NULL, NULL
1723         },
1724
1725         /*
1726          * We use the hopefully-safely-small value of 100kB as the compiled-in
1727          * default for max_stack_depth.  InitializeGUCOptions will increase it if
1728          * possible, depending on the actual platform-specific stack limit.
1729          */
1730         {
1731                 {"max_stack_depth", PGC_SUSET, RESOURCES_MEM,
1732                         gettext_noop("Sets the maximum stack depth, in kilobytes."),
1733                         NULL,
1734                         GUC_UNIT_KB
1735                 },
1736                 &max_stack_depth,
1737                 100, 100, MAX_KILOBYTES,
1738                 check_max_stack_depth, assign_max_stack_depth, NULL
1739         },
1740
1741         {
1742                 {"temp_file_limit", PGC_SUSET, RESOURCES_DISK,
1743                         gettext_noop("Limits the total size of all temporary files used by each session."),
1744                         gettext_noop("-1 means no limit."),
1745                         GUC_UNIT_KB
1746                 },
1747                 &temp_file_limit,
1748                 -1, -1, INT_MAX,
1749                 NULL, NULL, NULL
1750         },
1751
1752         {
1753                 {"vacuum_cost_page_hit", PGC_USERSET, RESOURCES_VACUUM_DELAY,
1754                         gettext_noop("Vacuum cost for a page found in the buffer cache."),
1755                         NULL
1756                 },
1757                 &VacuumCostPageHit,
1758                 1, 0, 10000,
1759                 NULL, NULL, NULL
1760         },
1761
1762         {
1763                 {"vacuum_cost_page_miss", PGC_USERSET, RESOURCES_VACUUM_DELAY,
1764                         gettext_noop("Vacuum cost for a page not found in the buffer cache."),
1765                         NULL
1766                 },
1767                 &VacuumCostPageMiss,
1768                 10, 0, 10000,
1769                 NULL, NULL, NULL
1770         },
1771
1772         {
1773                 {"vacuum_cost_page_dirty", PGC_USERSET, RESOURCES_VACUUM_DELAY,
1774                         gettext_noop("Vacuum cost for a page dirtied by vacuum."),
1775                         NULL
1776                 },
1777                 &VacuumCostPageDirty,
1778                 20, 0, 10000,
1779                 NULL, NULL, NULL
1780         },
1781
1782         {
1783                 {"vacuum_cost_limit", PGC_USERSET, RESOURCES_VACUUM_DELAY,
1784                         gettext_noop("Vacuum cost amount available before napping."),
1785                         NULL
1786                 },
1787                 &VacuumCostLimit,
1788                 200, 1, 10000,
1789                 NULL, NULL, NULL
1790         },
1791
1792         {
1793                 {"vacuum_cost_delay", PGC_USERSET, RESOURCES_VACUUM_DELAY,
1794                         gettext_noop("Vacuum cost delay in milliseconds."),
1795                         NULL,
1796                         GUC_UNIT_MS
1797                 },
1798                 &VacuumCostDelay,
1799                 0, 0, 100,
1800                 NULL, NULL, NULL
1801         },
1802
1803         {
1804                 {"autovacuum_vacuum_cost_delay", PGC_SIGHUP, AUTOVACUUM,
1805                         gettext_noop("Vacuum cost delay in milliseconds, for autovacuum."),
1806                         NULL,
1807                         GUC_UNIT_MS
1808                 },
1809                 &autovacuum_vac_cost_delay,
1810                 20, -1, 100,
1811                 NULL, NULL, NULL
1812         },
1813
1814         {
1815                 {"autovacuum_vacuum_cost_limit", PGC_SIGHUP, AUTOVACUUM,
1816                         gettext_noop("Vacuum cost amount available before napping, for autovacuum."),
1817                         NULL
1818                 },
1819                 &autovacuum_vac_cost_limit,
1820                 -1, -1, 10000,
1821                 NULL, NULL, NULL
1822         },
1823
1824         {
1825                 {"max_files_per_process", PGC_POSTMASTER, RESOURCES_KERNEL,
1826                         gettext_noop("Sets the maximum number of simultaneously open files for each server process."),
1827                         NULL
1828                 },
1829                 &max_files_per_process,
1830                 1000, 25, INT_MAX,
1831                 NULL, NULL, NULL
1832         },
1833
1834         /*
1835          * See also CheckRequiredParameterValues() if this parameter changes
1836          */
1837         {
1838                 {"max_prepared_transactions", PGC_POSTMASTER, RESOURCES_MEM,
1839                         gettext_noop("Sets the maximum number of simultaneously prepared transactions."),
1840                         NULL
1841                 },
1842                 &max_prepared_xacts,
1843                 0, 0, MAX_BACKENDS,
1844                 NULL, NULL, NULL
1845         },
1846
1847 #ifdef LOCK_DEBUG
1848         {
1849                 {"trace_lock_oidmin", PGC_SUSET, DEVELOPER_OPTIONS,
1850                         gettext_noop("No description available."),
1851                         NULL,
1852                         GUC_NOT_IN_SAMPLE
1853                 },
1854                 &Trace_lock_oidmin,
1855                 FirstNormalObjectId, 0, INT_MAX,
1856                 NULL, NULL, NULL
1857         },
1858         {
1859                 {"trace_lock_table", PGC_SUSET, DEVELOPER_OPTIONS,
1860                         gettext_noop("No description available."),
1861                         NULL,
1862                         GUC_NOT_IN_SAMPLE
1863                 },
1864                 &Trace_lock_table,
1865                 0, 0, INT_MAX,
1866                 NULL, NULL, NULL
1867         },
1868 #endif
1869
1870         {
1871                 {"statement_timeout", PGC_USERSET, CLIENT_CONN_STATEMENT,
1872                         gettext_noop("Sets the maximum allowed duration of any statement."),
1873                         gettext_noop("A value of 0 turns off the timeout."),
1874                         GUC_UNIT_MS
1875                 },
1876                 &StatementTimeout,
1877                 0, 0, INT_MAX,
1878                 NULL, NULL, NULL
1879         },
1880
1881         {
1882                 {"lock_timeout", PGC_USERSET, CLIENT_CONN_STATEMENT,
1883                         gettext_noop("Sets the maximum allowed duration of any wait for a lock."),
1884                         gettext_noop("A value of 0 turns off the timeout."),
1885                         GUC_UNIT_MS
1886                 },
1887                 &LockTimeout,
1888                 0, 0, INT_MAX,
1889                 NULL, NULL, NULL
1890         },
1891
1892         {
1893                 {"vacuum_freeze_min_age", PGC_USERSET, CLIENT_CONN_STATEMENT,
1894                         gettext_noop("Minimum age at which VACUUM should freeze a table row."),
1895                         NULL
1896                 },
1897                 &vacuum_freeze_min_age,
1898                 50000000, 0, 1000000000,
1899                 NULL, NULL, NULL
1900         },
1901
1902         {
1903                 {"vacuum_freeze_table_age", PGC_USERSET, CLIENT_CONN_STATEMENT,
1904                         gettext_noop("Age at which VACUUM should scan whole table to freeze tuples."),
1905                         NULL
1906                 },
1907                 &vacuum_freeze_table_age,
1908                 150000000, 0, 2000000000,
1909                 NULL, NULL, NULL
1910         },
1911
1912         {
1913                 {"vacuum_defer_cleanup_age", PGC_SIGHUP, REPLICATION_MASTER,
1914                         gettext_noop("Number of transactions by which VACUUM and HOT cleanup should be deferred, if any."),
1915                         NULL
1916                 },
1917                 &vacuum_defer_cleanup_age,
1918                 0, 0, 1000000,
1919                 NULL, NULL, NULL
1920         },
1921
1922         /*
1923          * See also CheckRequiredParameterValues() if this parameter changes
1924          */
1925         {
1926                 {"max_locks_per_transaction", PGC_POSTMASTER, LOCK_MANAGEMENT,
1927                         gettext_noop("Sets the maximum number of locks per transaction."),
1928                         gettext_noop("The shared lock table is sized on the assumption that "
1929                           "at most max_locks_per_transaction * max_connections distinct "
1930                                                  "objects will need to be locked at any one time.")
1931                 },
1932                 &max_locks_per_xact,
1933                 64, 10, INT_MAX,
1934                 NULL, NULL, NULL
1935         },
1936
1937         {
1938                 {"max_pred_locks_per_transaction", PGC_POSTMASTER, LOCK_MANAGEMENT,
1939                         gettext_noop("Sets the maximum number of predicate locks per transaction."),
1940                         gettext_noop("The shared predicate lock table is sized on the assumption that "
1941                                                  "at most max_pred_locks_per_transaction * max_connections distinct "
1942                                                  "objects will need to be locked at any one time.")
1943                 },
1944                 &max_predicate_locks_per_xact,
1945                 64, 10, INT_MAX,
1946                 NULL, NULL, NULL
1947         },
1948
1949         {
1950                 {"authentication_timeout", PGC_SIGHUP, CONN_AUTH_SECURITY,
1951                         gettext_noop("Sets the maximum allowed time to complete client authentication."),
1952                         NULL,
1953                         GUC_UNIT_S
1954                 },
1955                 &AuthenticationTimeout,
1956                 60, 1, 600,
1957                 NULL, NULL, NULL
1958         },
1959
1960         {
1961                 /* Not for general use */
1962                 {"pre_auth_delay", PGC_SIGHUP, DEVELOPER_OPTIONS,
1963                         gettext_noop("Waits N seconds on connection startup before authentication."),
1964                         gettext_noop("This allows attaching a debugger to the process."),
1965                         GUC_NOT_IN_SAMPLE | GUC_UNIT_S
1966                 },
1967                 &PreAuthDelay,
1968                 0, 0, 60,
1969                 NULL, NULL, NULL
1970         },
1971
1972         {
1973                 {"wal_keep_segments", PGC_SIGHUP, REPLICATION_SENDING,
1974                         gettext_noop("Sets the number of WAL files held for standby servers."),
1975                         NULL
1976                 },
1977                 &wal_keep_segments,
1978                 0, 0, INT_MAX,
1979                 NULL, NULL, NULL
1980         },
1981
1982         {
1983                 {"checkpoint_segments", PGC_SIGHUP, WAL_CHECKPOINTS,
1984                         gettext_noop("Sets the maximum distance in log segments between automatic WAL checkpoints."),
1985                         NULL
1986                 },
1987                 &CheckPointSegments,
1988                 3, 1, INT_MAX,
1989                 NULL, NULL, NULL
1990         },
1991
1992         {
1993                 {"checkpoint_timeout", PGC_SIGHUP, WAL_CHECKPOINTS,
1994                         gettext_noop("Sets the maximum time between automatic WAL checkpoints."),
1995                         NULL,
1996                         GUC_UNIT_S
1997                 },
1998                 &CheckPointTimeout,
1999                 300, 30, 3600,
2000                 NULL, NULL, NULL
2001         },
2002
2003         {
2004                 {"checkpoint_warning", PGC_SIGHUP, WAL_CHECKPOINTS,
2005                         gettext_noop("Enables warnings if checkpoint segments are filled more "
2006                                                  "frequently than this."),
2007                         gettext_noop("Write a message to the server log if checkpoints "
2008                         "caused by the filling of checkpoint segment files happens more "
2009                                                  "frequently than this number of seconds. Zero turns off the warning."),
2010                         GUC_UNIT_S
2011                 },
2012                 &CheckPointWarning,
2013                 30, 0, INT_MAX,
2014                 NULL, NULL, NULL
2015         },
2016
2017         {
2018                 {"wal_buffers", PGC_POSTMASTER, WAL_SETTINGS,
2019                         gettext_noop("Sets the number of disk-page buffers in shared memory for WAL."),
2020                         NULL,
2021                         GUC_UNIT_XBLOCKS
2022                 },
2023                 &XLOGbuffers,
2024                 -1, -1, INT_MAX,
2025                 check_wal_buffers, NULL, NULL
2026         },
2027
2028         {
2029                 {"wal_writer_delay", PGC_SIGHUP, WAL_SETTINGS,
2030                         gettext_noop("WAL writer sleep time between WAL flushes."),
2031                         NULL,
2032                         GUC_UNIT_MS
2033                 },
2034                 &WalWriterDelay,
2035                 200, 1, 10000,
2036                 NULL, NULL, NULL
2037         },
2038
2039         {
2040                 /* see max_connections */
2041                 {"max_wal_senders", PGC_POSTMASTER, REPLICATION_SENDING,
2042                         gettext_noop("Sets the maximum number of simultaneously running WAL sender processes."),
2043                         NULL
2044                 },
2045                 &max_wal_senders,
2046                 0, 0, MAX_BACKENDS,
2047                 NULL, NULL, NULL
2048         },
2049
2050         {
2051                 {"wal_sender_timeout", PGC_SIGHUP, REPLICATION_SENDING,
2052                         gettext_noop("Sets the maximum time to wait for WAL replication."),
2053                         NULL,
2054                         GUC_UNIT_MS
2055                 },
2056                 &wal_sender_timeout,
2057                 60 * 1000, 0, INT_MAX,
2058                 NULL, NULL, NULL
2059         },
2060
2061         {
2062                 {"commit_delay", PGC_SUSET, WAL_SETTINGS,
2063                         gettext_noop("Sets the delay in microseconds between transaction commit and "
2064                                                  "flushing WAL to disk."),
2065                         NULL
2066                         /* we have no microseconds designation, so can't supply units here */
2067                 },
2068                 &CommitDelay,
2069                 0, 0, 100000,
2070                 NULL, NULL, NULL
2071         },
2072
2073         {
2074                 {"commit_siblings", PGC_USERSET, WAL_SETTINGS,
2075                         gettext_noop("Sets the minimum concurrent open transactions before performing "
2076                                                  "commit_delay."),
2077                         NULL
2078                 },
2079                 &CommitSiblings,
2080                 5, 0, 1000,
2081                 NULL, NULL, NULL
2082         },
2083
2084         {
2085                 {"extra_float_digits", PGC_USERSET, CLIENT_CONN_LOCALE,
2086                         gettext_noop("Sets the number of digits displayed for floating-point values."),
2087                         gettext_noop("This affects real, double precision, and geometric data types. "
2088                          "The parameter value is added to the standard number of digits "
2089                                                  "(FLT_DIG or DBL_DIG as appropriate).")
2090                 },
2091                 &extra_float_digits,
2092                 0, -15, 3,
2093                 NULL, NULL, NULL
2094         },
2095
2096         {
2097                 {"log_min_duration_statement", PGC_SUSET, LOGGING_WHEN,
2098                         gettext_noop("Sets the minimum execution time above which "
2099                                                  "statements will be logged."),
2100                         gettext_noop("Zero prints all queries. -1 turns this feature off."),
2101                         GUC_UNIT_MS
2102                 },
2103                 &log_min_duration_statement,
2104                 -1, -1, INT_MAX,
2105                 NULL, NULL, NULL
2106         },
2107
2108         {
2109                 {"log_autovacuum_min_duration", PGC_SIGHUP, LOGGING_WHAT,
2110                         gettext_noop("Sets the minimum execution time above which "
2111                                                  "autovacuum actions will be logged."),
2112                         gettext_noop("Zero prints all actions. -1 turns autovacuum logging off."),
2113                         GUC_UNIT_MS
2114                 },
2115                 &Log_autovacuum_min_duration,
2116                 -1, -1, INT_MAX,
2117                 NULL, NULL, NULL
2118         },
2119
2120         {
2121                 {"bgwriter_delay", PGC_SIGHUP, RESOURCES_BGWRITER,
2122                         gettext_noop("Background writer sleep time between rounds."),
2123                         NULL,
2124                         GUC_UNIT_MS
2125                 },
2126                 &BgWriterDelay,
2127                 200, 10, 10000,
2128                 NULL, NULL, NULL
2129         },
2130
2131         {
2132                 {"bgwriter_lru_maxpages", PGC_SIGHUP, RESOURCES_BGWRITER,
2133                         gettext_noop("Background writer maximum number of LRU pages to flush per round."),
2134                         NULL
2135                 },
2136                 &bgwriter_lru_maxpages,
2137                 100, 0, 1000,
2138                 NULL, NULL, NULL
2139         },
2140
2141         {
2142                 {"effective_io_concurrency",
2143 #ifdef USE_PREFETCH
2144                         PGC_USERSET,
2145 #else
2146                         PGC_INTERNAL,
2147 #endif
2148                         RESOURCES_ASYNCHRONOUS,
2149                         gettext_noop("Number of simultaneous requests that can be handled efficiently by the disk subsystem."),
2150                         gettext_noop("For RAID arrays, this should be approximately the number of drive spindles in the array.")
2151                 },
2152                 &effective_io_concurrency,
2153 #ifdef USE_PREFETCH
2154                 1, 0, 1000,
2155 #else
2156                 0, 0, 0,
2157 #endif
2158                 check_effective_io_concurrency, assign_effective_io_concurrency, NULL
2159         },
2160
2161         {
2162                 {"log_rotation_age", PGC_SIGHUP, LOGGING_WHERE,
2163                         gettext_noop("Automatic log file rotation will occur after N minutes."),
2164                         NULL,
2165                         GUC_UNIT_MIN
2166                 },
2167                 &Log_RotationAge,
2168                 HOURS_PER_DAY * MINS_PER_HOUR, 0, INT_MAX / SECS_PER_MINUTE,
2169                 NULL, NULL, NULL
2170         },
2171
2172         {
2173                 {"log_rotation_size", PGC_SIGHUP, LOGGING_WHERE,
2174                         gettext_noop("Automatic log file rotation will occur after N kilobytes."),
2175                         NULL,
2176                         GUC_UNIT_KB
2177                 },
2178                 &Log_RotationSize,
2179                 10 * 1024, 0, INT_MAX / 1024,
2180                 NULL, NULL, NULL
2181         },
2182
2183         {
2184                 {"max_function_args", PGC_INTERNAL, PRESET_OPTIONS,
2185                         gettext_noop("Shows the maximum number of function arguments."),
2186                         NULL,
2187                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2188                 },
2189                 &max_function_args,
2190                 FUNC_MAX_ARGS, FUNC_MAX_ARGS, FUNC_MAX_ARGS,
2191                 NULL, NULL, NULL
2192         },
2193
2194         {
2195                 {"max_index_keys", PGC_INTERNAL, PRESET_OPTIONS,
2196                         gettext_noop("Shows the maximum number of index keys."),
2197                         NULL,
2198                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2199                 },
2200                 &max_index_keys,
2201                 INDEX_MAX_KEYS, INDEX_MAX_KEYS, INDEX_MAX_KEYS,
2202                 NULL, NULL, NULL
2203         },
2204
2205         {
2206                 {"max_identifier_length", PGC_INTERNAL, PRESET_OPTIONS,
2207                         gettext_noop("Shows the maximum identifier length."),
2208                         NULL,
2209                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2210                 },
2211                 &max_identifier_length,
2212                 NAMEDATALEN - 1, NAMEDATALEN - 1, NAMEDATALEN - 1,
2213                 NULL, NULL, NULL
2214         },
2215
2216         {
2217                 {"block_size", PGC_INTERNAL, PRESET_OPTIONS,
2218                         gettext_noop("Shows the size of a disk block."),
2219                         NULL,
2220                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2221                 },
2222                 &block_size,
2223                 BLCKSZ, BLCKSZ, BLCKSZ,
2224                 NULL, NULL, NULL
2225         },
2226
2227         {
2228                 {"segment_size", PGC_INTERNAL, PRESET_OPTIONS,
2229                         gettext_noop("Shows the number of pages per disk file."),
2230                         NULL,
2231                         GUC_UNIT_BLOCKS | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2232                 },
2233                 &segment_size,
2234                 RELSEG_SIZE, RELSEG_SIZE, RELSEG_SIZE,
2235                 NULL, NULL, NULL
2236         },
2237
2238         {
2239                 {"wal_block_size", PGC_INTERNAL, PRESET_OPTIONS,
2240                         gettext_noop("Shows the block size in the write ahead log."),
2241                         NULL,
2242                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2243                 },
2244                 &wal_block_size,
2245                 XLOG_BLCKSZ, XLOG_BLCKSZ, XLOG_BLCKSZ,
2246                 NULL, NULL, NULL
2247         },
2248
2249         {
2250                 {"wal_segment_size", PGC_INTERNAL, PRESET_OPTIONS,
2251                         gettext_noop("Shows the number of pages per write ahead log segment."),
2252                         NULL,
2253                         GUC_UNIT_XBLOCKS | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2254                 },
2255                 &wal_segment_size,
2256                 (XLOG_SEG_SIZE / XLOG_BLCKSZ),
2257                 (XLOG_SEG_SIZE / XLOG_BLCKSZ),
2258                 (XLOG_SEG_SIZE / XLOG_BLCKSZ),
2259                 NULL, NULL, NULL
2260         },
2261
2262         {
2263                 {"autovacuum_naptime", PGC_SIGHUP, AUTOVACUUM,
2264                         gettext_noop("Time to sleep between autovacuum runs."),
2265                         NULL,
2266                         GUC_UNIT_S
2267                 },
2268                 &autovacuum_naptime,
2269                 60, 1, INT_MAX / 1000,
2270                 NULL, NULL, NULL
2271         },
2272         {
2273                 {"autovacuum_vacuum_threshold", PGC_SIGHUP, AUTOVACUUM,
2274                         gettext_noop("Minimum number of tuple updates or deletes prior to vacuum."),
2275                         NULL
2276                 },
2277                 &autovacuum_vac_thresh,
2278                 50, 0, INT_MAX,
2279                 NULL, NULL, NULL
2280         },
2281         {
2282                 {"autovacuum_analyze_threshold", PGC_SIGHUP, AUTOVACUUM,
2283                         gettext_noop("Minimum number of tuple inserts, updates, or deletes prior to analyze."),
2284                         NULL
2285                 },
2286                 &autovacuum_anl_thresh,
2287                 50, 0, INT_MAX,
2288                 NULL, NULL, NULL
2289         },
2290         {
2291                 /* see varsup.c for why this is PGC_POSTMASTER not PGC_SIGHUP */
2292                 {"autovacuum_freeze_max_age", PGC_POSTMASTER, AUTOVACUUM,
2293                         gettext_noop("Age at which to autovacuum a table to prevent transaction ID wraparound."),
2294                         NULL
2295                 },
2296                 &autovacuum_freeze_max_age,
2297                 /* see pg_resetxlog if you change the upper-limit value */
2298                 200000000, 100000000, 2000000000,
2299                 NULL, NULL, NULL
2300         },
2301         {
2302                 /* see max_connections */
2303                 {"autovacuum_max_workers", PGC_POSTMASTER, AUTOVACUUM,
2304                         gettext_noop("Sets the maximum number of simultaneously running autovacuum worker processes."),
2305                         NULL
2306                 },
2307                 &autovacuum_max_workers,
2308                 3, 1, MAX_BACKENDS,
2309                 check_autovacuum_max_workers, NULL, NULL
2310         },
2311
2312         {
2313                 {"tcp_keepalives_idle", PGC_USERSET, CLIENT_CONN_OTHER,
2314                         gettext_noop("Time between issuing TCP keepalives."),
2315                         gettext_noop("A value of 0 uses the system default."),
2316                         GUC_UNIT_S
2317                 },
2318                 &tcp_keepalives_idle,
2319                 0, 0, INT_MAX,
2320                 NULL, assign_tcp_keepalives_idle, show_tcp_keepalives_idle
2321         },
2322
2323         {
2324                 {"tcp_keepalives_interval", PGC_USERSET, CLIENT_CONN_OTHER,
2325                         gettext_noop("Time between TCP keepalive retransmits."),
2326                         gettext_noop("A value of 0 uses the system default."),
2327                         GUC_UNIT_S
2328                 },
2329                 &tcp_keepalives_interval,
2330                 0, 0, INT_MAX,
2331                 NULL, assign_tcp_keepalives_interval, show_tcp_keepalives_interval
2332         },
2333
2334         {
2335                 {"ssl_renegotiation_limit", PGC_USERSET, CONN_AUTH_SECURITY,
2336                         gettext_noop("Set the amount of traffic to send and receive before renegotiating the encryption keys."),
2337                         NULL,
2338                         GUC_UNIT_KB,
2339                 },
2340                 &ssl_renegotiation_limit,
2341                 512 * 1024, 0, MAX_KILOBYTES,
2342                 NULL, NULL, NULL
2343         },
2344
2345         {
2346                 {"tcp_keepalives_count", PGC_USERSET, CLIENT_CONN_OTHER,
2347                         gettext_noop("Maximum number of TCP keepalive retransmits."),
2348                         gettext_noop("This controls the number of consecutive keepalive retransmits that can be "
2349                                                  "lost before a connection is considered dead. A value of 0 uses the "
2350                                                  "system default."),
2351                 },
2352                 &tcp_keepalives_count,
2353                 0, 0, INT_MAX,
2354                 NULL, assign_tcp_keepalives_count, show_tcp_keepalives_count
2355         },
2356
2357         {
2358                 {"gin_fuzzy_search_limit", PGC_USERSET, CLIENT_CONN_OTHER,
2359                         gettext_noop("Sets the maximum allowed result for exact search by GIN."),
2360                         NULL,
2361                         0
2362                 },
2363                 &GinFuzzySearchLimit,
2364                 0, 0, INT_MAX,
2365                 NULL, NULL, NULL
2366         },
2367
2368         {
2369                 {"effective_cache_size", PGC_USERSET, QUERY_TUNING_COST,
2370                         gettext_noop("Sets the planner's assumption about the size of the disk cache."),
2371                         gettext_noop("That is, the portion of the kernel's disk cache that "
2372                                                  "will be used for PostgreSQL data files. This is measured in disk "
2373                                                  "pages, which are normally 8 kB each."),
2374                         GUC_UNIT_BLOCKS,
2375                 },
2376                 &effective_cache_size,
2377                 DEFAULT_EFFECTIVE_CACHE_SIZE, 1, INT_MAX,
2378                 NULL, NULL, NULL
2379         },
2380
2381         {
2382                 /* Can't be set in postgresql.conf */
2383                 {"server_version_num", PGC_INTERNAL, PRESET_OPTIONS,
2384                         gettext_noop("Shows the server version as an integer."),
2385                         NULL,
2386                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2387                 },
2388                 &server_version_num,
2389                 PG_VERSION_NUM, PG_VERSION_NUM, PG_VERSION_NUM,
2390                 NULL, NULL, NULL
2391         },
2392
2393         {
2394                 {"log_temp_files", PGC_SUSET, LOGGING_WHAT,
2395                         gettext_noop("Log the use of temporary files larger than this number of kilobytes."),
2396                         gettext_noop("Zero logs all files. The default is -1 (turning this feature off)."),
2397                         GUC_UNIT_KB
2398                 },
2399                 &log_temp_files,
2400                 -1, -1, INT_MAX,
2401                 NULL, NULL, NULL
2402         },
2403
2404         {
2405                 {"track_activity_query_size", PGC_POSTMASTER, RESOURCES_MEM,
2406                         gettext_noop("Sets the size reserved for pg_stat_activity.query, in bytes."),
2407                         NULL,
2408                 },
2409                 &pgstat_track_activity_query_size,
2410                 1024, 100, 102400,
2411                 NULL, NULL, NULL
2412         },
2413
2414         /* End-of-list marker */
2415         {
2416                 {NULL, 0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL, NULL
2417         }
2418 };
2419
2420
2421 static struct config_real ConfigureNamesReal[] =
2422 {
2423         {
2424                 {"seq_page_cost", PGC_USERSET, QUERY_TUNING_COST,
2425                         gettext_noop("Sets the planner's estimate of the cost of a "
2426                                                  "sequentially fetched disk page."),
2427                         NULL
2428                 },
2429                 &seq_page_cost,
2430                 DEFAULT_SEQ_PAGE_COST, 0, DBL_MAX,
2431                 NULL, NULL, NULL
2432         },
2433         {
2434                 {"random_page_cost", PGC_USERSET, QUERY_TUNING_COST,
2435                         gettext_noop("Sets the planner's estimate of the cost of a "
2436                                                  "nonsequentially fetched disk page."),
2437                         NULL
2438                 },
2439                 &random_page_cost,
2440                 DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX,
2441                 NULL, NULL, NULL
2442         },
2443         {
2444                 {"cpu_tuple_cost", PGC_USERSET, QUERY_TUNING_COST,
2445                         gettext_noop("Sets the planner's estimate of the cost of "
2446                                                  "processing each tuple (row)."),
2447                         NULL
2448                 },
2449                 &cpu_tuple_cost,
2450                 DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX,
2451                 NULL, NULL, NULL
2452         },
2453         {
2454                 {"cpu_index_tuple_cost", PGC_USERSET, QUERY_TUNING_COST,
2455                         gettext_noop("Sets the planner's estimate of the cost of "
2456                                                  "processing each index entry during an index scan."),
2457                         NULL
2458                 },
2459                 &cpu_index_tuple_cost,
2460                 DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX,
2461                 NULL, NULL, NULL
2462         },
2463         {
2464                 {"cpu_operator_cost", PGC_USERSET, QUERY_TUNING_COST,
2465                         gettext_noop("Sets the planner's estimate of the cost of "
2466                                                  "processing each operator or function call."),
2467                         NULL
2468                 },
2469                 &cpu_operator_cost,
2470                 DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX,
2471                 NULL, NULL, NULL
2472         },
2473
2474         {
2475                 {"cursor_tuple_fraction", PGC_USERSET, QUERY_TUNING_OTHER,
2476                         gettext_noop("Sets the planner's estimate of the fraction of "
2477                                                  "a cursor's rows that will be retrieved."),
2478                         NULL
2479                 },
2480                 &cursor_tuple_fraction,
2481                 DEFAULT_CURSOR_TUPLE_FRACTION, 0.0, 1.0,
2482                 NULL, NULL, NULL
2483         },
2484
2485         {
2486                 {"geqo_selection_bias", PGC_USERSET, QUERY_TUNING_GEQO,
2487                         gettext_noop("GEQO: selective pressure within the population."),
2488                         NULL
2489                 },
2490                 &Geqo_selection_bias,
2491                 DEFAULT_GEQO_SELECTION_BIAS,
2492                 MIN_GEQO_SELECTION_BIAS, MAX_GEQO_SELECTION_BIAS,
2493                 NULL, NULL, NULL
2494         },
2495         {
2496                 {"geqo_seed", PGC_USERSET, QUERY_TUNING_GEQO,
2497                         gettext_noop("GEQO: seed for random path selection."),
2498                         NULL
2499                 },
2500                 &Geqo_seed,
2501                 0.0, 0.0, 1.0,
2502                 NULL, NULL, NULL
2503         },
2504
2505         {
2506                 {"bgwriter_lru_multiplier", PGC_SIGHUP, RESOURCES_BGWRITER,
2507                         gettext_noop("Multiple of the average buffer usage to free per round."),
2508                         NULL
2509                 },
2510                 &bgwriter_lru_multiplier,
2511                 2.0, 0.0, 10.0,
2512                 NULL, NULL, NULL
2513         },
2514
2515         {
2516                 {"seed", PGC_USERSET, UNGROUPED,
2517                         gettext_noop("Sets the seed for random-number generation."),
2518                         NULL,
2519                         GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2520                 },
2521                 &phony_random_seed,
2522                 0.0, -1.0, 1.0,
2523                 check_random_seed, assign_random_seed, show_random_seed
2524         },
2525
2526         {
2527                 {"autovacuum_vacuum_scale_factor", PGC_SIGHUP, AUTOVACUUM,
2528                         gettext_noop("Number of tuple updates or deletes prior to vacuum as a fraction of reltuples."),
2529                         NULL
2530                 },
2531                 &autovacuum_vac_scale,
2532                 0.2, 0.0, 100.0,
2533                 NULL, NULL, NULL
2534         },
2535         {
2536                 {"autovacuum_analyze_scale_factor", PGC_SIGHUP, AUTOVACUUM,
2537                         gettext_noop("Number of tuple inserts, updates, or deletes prior to analyze as a fraction of reltuples."),
2538                         NULL
2539                 },
2540                 &autovacuum_anl_scale,
2541                 0.1, 0.0, 100.0,
2542                 NULL, NULL, NULL
2543         },
2544
2545         {
2546                 {"checkpoint_completion_target", PGC_SIGHUP, WAL_CHECKPOINTS,
2547                         gettext_noop("Time spent flushing dirty buffers during checkpoint, as fraction of checkpoint interval."),
2548                         NULL
2549                 },
2550                 &CheckPointCompletionTarget,
2551                 0.5, 0.0, 1.0,
2552                 NULL, NULL, NULL
2553         },
2554
2555         /* End-of-list marker */
2556         {
2557                 {NULL, 0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL, NULL
2558         }
2559 };
2560
2561
2562 static struct config_string ConfigureNamesString[] =
2563 {
2564         {
2565                 {"archive_command", PGC_SIGHUP, WAL_ARCHIVING,
2566                         gettext_noop("Sets the shell command that will be called to archive a WAL file."),
2567                         NULL
2568                 },
2569                 &XLogArchiveCommand,
2570                 "",
2571                 NULL, NULL, show_archive_command
2572         },
2573
2574         {
2575                 {"client_encoding", PGC_USERSET, CLIENT_CONN_LOCALE,
2576                         gettext_noop("Sets the client's character set encoding."),
2577                         NULL,
2578                         GUC_IS_NAME | GUC_REPORT
2579                 },
2580                 &client_encoding_string,
2581                 "SQL_ASCII",
2582                 check_client_encoding, assign_client_encoding, NULL
2583         },
2584
2585         {
2586                 {"log_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
2587                         gettext_noop("Controls information prefixed to each log line."),
2588                         gettext_noop("If blank, no prefix is used.")
2589                 },
2590                 &Log_line_prefix,
2591                 "",
2592                 NULL, NULL, NULL
2593         },
2594
2595         {
2596                 {"log_timezone", PGC_SIGHUP, LOGGING_WHAT,
2597                         gettext_noop("Sets the time zone to use in log messages."),
2598                         NULL
2599                 },
2600                 &log_timezone_string,
2601                 "GMT",
2602                 check_log_timezone, assign_log_timezone, show_log_timezone
2603         },
2604
2605         {
2606                 {"DateStyle", PGC_USERSET, CLIENT_CONN_LOCALE,
2607                         gettext_noop("Sets the display format for date and time values."),
2608                         gettext_noop("Also controls interpretation of ambiguous "
2609                                                  "date inputs."),
2610                         GUC_LIST_INPUT | GUC_REPORT
2611                 },
2612                 &datestyle_string,
2613                 "ISO, MDY",
2614                 check_datestyle, assign_datestyle, NULL
2615         },
2616
2617         {
2618                 {"default_tablespace", PGC_USERSET, CLIENT_CONN_STATEMENT,
2619                         gettext_noop("Sets the default tablespace to create tables and indexes in."),
2620                         gettext_noop("An empty string selects the database's default tablespace."),
2621                         GUC_IS_NAME
2622                 },
2623                 &default_tablespace,
2624                 "",
2625                 check_default_tablespace, NULL, NULL
2626         },
2627
2628         {
2629                 {"temp_tablespaces", PGC_USERSET, CLIENT_CONN_STATEMENT,
2630                         gettext_noop("Sets the tablespace(s) to use for temporary tables and sort files."),
2631                         NULL,
2632                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2633                 },
2634                 &temp_tablespaces,
2635                 "",
2636                 check_temp_tablespaces, assign_temp_tablespaces, NULL
2637         },
2638
2639         {
2640                 {"dynamic_library_path", PGC_SUSET, CLIENT_CONN_OTHER,
2641                         gettext_noop("Sets the path for dynamically loadable modules."),
2642                         gettext_noop("If a dynamically loadable module needs to be opened and "
2643                                                  "the specified name does not have a directory component (i.e., the "
2644                                                  "name does not contain a slash), the system will search this path for "
2645                                                  "the specified file."),
2646                         GUC_SUPERUSER_ONLY
2647                 },
2648                 &Dynamic_library_path,
2649                 "$libdir",
2650                 NULL, NULL, NULL
2651         },
2652
2653         {
2654                 {"krb_server_keyfile", PGC_SIGHUP, CONN_AUTH_SECURITY,
2655                         gettext_noop("Sets the location of the Kerberos server key file."),
2656                         NULL,
2657                         GUC_SUPERUSER_ONLY
2658                 },
2659                 &pg_krb_server_keyfile,
2660                 PG_KRB_SRVTAB,
2661                 NULL, NULL, NULL
2662         },
2663
2664         {
2665                 {"krb_srvname", PGC_SIGHUP, CONN_AUTH_SECURITY,
2666                         gettext_noop("Sets the name of the Kerberos service."),
2667                         NULL
2668                 },
2669                 &pg_krb_srvnam,
2670                 PG_KRB_SRVNAM,
2671                 NULL, NULL, NULL
2672         },
2673
2674         {
2675                 {"bonjour_name", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2676                         gettext_noop("Sets the Bonjour service name."),
2677                         NULL
2678                 },
2679                 &bonjour_name,
2680                 "",
2681                 NULL, NULL, NULL
2682         },
2683
2684         /* See main.c about why defaults for LC_foo are not all alike */
2685
2686         {
2687                 {"lc_collate", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2688                         gettext_noop("Shows the collation order locale."),
2689                         NULL,
2690                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2691                 },
2692                 &locale_collate,
2693                 "C",
2694                 NULL, NULL, NULL
2695         },
2696
2697         {
2698                 {"lc_ctype", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2699                         gettext_noop("Shows the character classification and case conversion locale."),
2700                         NULL,
2701                         GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2702                 },
2703                 &locale_ctype,
2704                 "C",
2705                 NULL, NULL, NULL
2706         },
2707
2708         {
2709                 {"lc_messages", PGC_SUSET, CLIENT_CONN_LOCALE,
2710                         gettext_noop("Sets the language in which messages are displayed."),
2711                         NULL
2712                 },
2713                 &locale_messages,
2714                 "",
2715                 check_locale_messages, assign_locale_messages, NULL
2716         },
2717
2718         {
2719                 {"lc_monetary", PGC_USERSET, CLIENT_CONN_LOCALE,
2720                         gettext_noop("Sets the locale for formatting monetary amounts."),
2721                         NULL
2722                 },
2723                 &locale_monetary,
2724                 "C",
2725                 check_locale_monetary, assign_locale_monetary, NULL
2726         },
2727
2728         {
2729                 {"lc_numeric", PGC_USERSET, CLIENT_CONN_LOCALE,
2730                         gettext_noop("Sets the locale for formatting numbers."),
2731                         NULL
2732                 },
2733                 &locale_numeric,
2734                 "C",
2735                 check_locale_numeric, assign_locale_numeric, NULL
2736         },
2737
2738         {
2739                 {"lc_time", PGC_USERSET, CLIENT_CONN_LOCALE,
2740                         gettext_noop("Sets the locale for formatting date and time values."),
2741                         NULL
2742                 },
2743                 &locale_time,
2744                 "C",
2745                 check_locale_time, assign_locale_time, NULL
2746         },
2747
2748         {
2749                 {"shared_preload_libraries", PGC_POSTMASTER, RESOURCES_KERNEL,
2750                         gettext_noop("Lists shared libraries to preload into server."),
2751                         NULL,
2752                         GUC_LIST_INPUT | GUC_LIST_QUOTE | GUC_SUPERUSER_ONLY
2753                 },
2754                 &shared_preload_libraries_string,
2755                 "",
2756                 NULL, NULL, NULL
2757         },
2758
2759         {
2760                 {"local_preload_libraries", PGC_BACKEND, CLIENT_CONN_OTHER,
2761                         gettext_noop("Lists shared libraries to preload into each backend."),
2762                         NULL,
2763                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2764                 },
2765                 &local_preload_libraries_string,
2766                 "",
2767                 NULL, NULL, NULL
2768         },
2769
2770         {
2771                 {"search_path", PGC_USERSET, CLIENT_CONN_STATEMENT,
2772                         gettext_noop("Sets the schema search order for names that are not schema-qualified."),
2773                         NULL,
2774                         GUC_LIST_INPUT | GUC_LIST_QUOTE
2775                 },
2776                 &namespace_search_path,
2777                 "\"$user\",public",
2778                 check_search_path, assign_search_path, NULL
2779         },
2780
2781         {
2782                 /* Can't be set in postgresql.conf */
2783                 {"server_encoding", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2784                         gettext_noop("Sets the server (database) character set encoding."),
2785                         NULL,
2786                         GUC_IS_NAME | GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2787                 },
2788                 &server_encoding_string,
2789                 "SQL_ASCII",
2790                 NULL, NULL, NULL
2791         },
2792
2793         {
2794                 /* Can't be set in postgresql.conf */
2795                 {"server_version", PGC_INTERNAL, PRESET_OPTIONS,
2796                         gettext_noop("Shows the server version."),
2797                         NULL,
2798                         GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2799                 },
2800                 &server_version_string,
2801                 PG_VERSION,
2802                 NULL, NULL, NULL
2803         },
2804
2805         {
2806                 /* Not for general use --- used by SET ROLE */
2807                 {"role", PGC_USERSET, UNGROUPED,
2808                         gettext_noop("Sets the current role."),
2809                         NULL,
2810                         GUC_IS_NAME | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_NOT_WHILE_SEC_REST
2811                 },
2812                 &role_string,
2813                 "none",
2814                 check_role, assign_role, show_role
2815         },
2816
2817         {
2818                 /* Not for general use --- used by SET SESSION AUTHORIZATION */
2819                 {"session_authorization", PGC_USERSET, UNGROUPED,
2820                         gettext_noop("Sets the session user name."),
2821                         NULL,
2822                         GUC_IS_NAME | GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_NOT_WHILE_SEC_REST
2823                 },
2824                 &session_authorization_string,
2825                 NULL,
2826                 check_session_authorization, assign_session_authorization, NULL
2827         },
2828
2829         {
2830                 {"log_destination", PGC_SIGHUP, LOGGING_WHERE,
2831                         gettext_noop("Sets the destination for server log output."),
2832                         gettext_noop("Valid values are combinations of \"stderr\", "
2833                                                  "\"syslog\", \"csvlog\", and \"eventlog\", "
2834                                                  "depending on the platform."),
2835                         GUC_LIST_INPUT
2836                 },
2837                 &log_destination_string,
2838                 "stderr",
2839                 check_log_destination, assign_log_destination, NULL
2840         },
2841         {
2842                 {"log_directory", PGC_SIGHUP, LOGGING_WHERE,
2843                         gettext_noop("Sets the destination directory for log files."),
2844                         gettext_noop("Can be specified as relative to the data directory "
2845                                                  "or as absolute path."),
2846                         GUC_SUPERUSER_ONLY
2847                 },
2848                 &Log_directory,
2849                 "pg_log",
2850                 check_canonical_path, NULL, NULL
2851         },
2852         {
2853                 {"log_filename", PGC_SIGHUP, LOGGING_WHERE,
2854                         gettext_noop("Sets the file name pattern for log files."),
2855                         NULL,
2856                         GUC_SUPERUSER_ONLY
2857                 },
2858                 &Log_filename,
2859                 "postgresql-%Y-%m-%d_%H%M%S.log",
2860                 NULL, NULL, NULL
2861         },
2862
2863         {
2864                 {"syslog_ident", PGC_SIGHUP, LOGGING_WHERE,
2865                         gettext_noop("Sets the program name used to identify PostgreSQL "
2866                                                  "messages in syslog."),
2867                         NULL
2868                 },
2869                 &syslog_ident_str,
2870                 "postgres",
2871                 NULL, assign_syslog_ident, NULL
2872         },
2873
2874         {
2875                 {"event_source", PGC_POSTMASTER, LOGGING_WHERE,
2876                         gettext_noop("Sets the application name used to identify "
2877                                                  "PostgreSQL messages in the event log."),
2878                         NULL
2879                 },
2880                 &event_source,
2881                 "PostgreSQL",
2882                 NULL, NULL, NULL
2883         },
2884
2885         {
2886                 {"TimeZone", PGC_USERSET, CLIENT_CONN_LOCALE,
2887                         gettext_noop("Sets the time zone for displaying and interpreting time stamps."),
2888                         NULL,
2889                         GUC_REPORT
2890                 },
2891                 &timezone_string,
2892                 "GMT",
2893                 check_timezone, assign_timezone, show_timezone
2894         },
2895         {
2896                 {"timezone_abbreviations", PGC_USERSET, CLIENT_CONN_LOCALE,
2897                         gettext_noop("Selects a file of time zone abbreviations."),
2898                         NULL
2899                 },
2900                 &timezone_abbreviations_string,
2901                 NULL,
2902                 check_timezone_abbreviations, assign_timezone_abbreviations, NULL
2903         },
2904
2905         {
2906                 {"transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
2907                         gettext_noop("Sets the current transaction's isolation level."),
2908                         NULL,
2909                         GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2910                 },
2911                 &XactIsoLevel_string,
2912                 "default",
2913                 check_XactIsoLevel, assign_XactIsoLevel, show_XactIsoLevel
2914         },
2915
2916         {
2917                 {"unix_socket_group", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2918                         gettext_noop("Sets the owning group of the Unix-domain socket."),
2919                         gettext_noop("The owning user of the socket is always the user "
2920                                                  "that starts the server.")
2921                 },
2922                 &Unix_socket_group,
2923                 "",
2924                 NULL, NULL, NULL
2925         },
2926
2927         {
2928                 {"unix_socket_directories", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2929                         gettext_noop("Sets the directories where Unix-domain sockets will be created."),
2930                         NULL,
2931                         GUC_SUPERUSER_ONLY
2932                 },
2933                 &Unix_socket_directories,
2934 #ifdef HAVE_UNIX_SOCKETS
2935                 DEFAULT_PGSOCKET_DIR,
2936 #else
2937                 "",
2938 #endif
2939                 NULL, NULL, NULL
2940         },
2941
2942         {
2943                 {"listen_addresses", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2944                         gettext_noop("Sets the host name or IP address(es) to listen to."),
2945                         NULL,
2946                         GUC_LIST_INPUT
2947                 },
2948                 &ListenAddresses,
2949                 "localhost",
2950                 NULL, NULL, NULL
2951         },
2952
2953         {
2954                 {"data_directory", PGC_POSTMASTER, FILE_LOCATIONS,
2955                         gettext_noop("Sets the server's data directory."),
2956                         NULL,
2957                         GUC_SUPERUSER_ONLY
2958                 },
2959                 &data_directory,
2960                 NULL,
2961                 NULL, NULL, NULL
2962         },
2963
2964         {
2965                 {"recovery_config_directory", PGC_POSTMASTER, FILE_LOCATIONS,
2966                         gettext_noop("Sets the server's recovery configuration directory."),
2967                         NULL,
2968                         GUC_SUPERUSER_ONLY
2969                 },
2970                 &recovery_config_directory,
2971                 NULL,
2972                 NULL, NULL, NULL
2973         },
2974
2975         {
2976                 {"config_file", PGC_POSTMASTER, FILE_LOCATIONS,
2977                         gettext_noop("Sets the server's main configuration file."),
2978                         NULL,
2979                         GUC_DISALLOW_IN_FILE | GUC_SUPERUSER_ONLY
2980                 },
2981                 &ConfigFileName,
2982                 NULL,
2983                 NULL, NULL, NULL
2984         },
2985
2986         {
2987                 {"hba_file", PGC_POSTMASTER, FILE_LOCATIONS,
2988                         gettext_noop("Sets the server's \"hba\" configuration file."),
2989                         NULL,
2990                         GUC_SUPERUSER_ONLY
2991                 },
2992                 &HbaFileName,
2993                 NULL,
2994                 NULL, NULL, NULL
2995         },
2996
2997         {
2998                 {"ident_file", PGC_POSTMASTER, FILE_LOCATIONS,
2999                         gettext_noop("Sets the server's \"ident\" configuration file."),
3000                         NULL,
3001                         GUC_SUPERUSER_ONLY
3002                 },
3003                 &IdentFileName,
3004                 NULL,
3005                 NULL, NULL, NULL
3006         },
3007
3008         {
3009                 {"external_pid_file", PGC_POSTMASTER, FILE_LOCATIONS,
3010                         gettext_noop("Writes the postmaster PID to the specified file."),
3011                         NULL,
3012                         GUC_SUPERUSER_ONLY
3013                 },
3014                 &external_pid_file,
3015                 NULL,
3016                 check_canonical_path, NULL, NULL
3017         },
3018
3019         {
3020                 {"ssl_cert_file", PGC_POSTMASTER, CONN_AUTH_SECURITY,
3021                         gettext_noop("Location of the SSL server certificate file."),
3022                         NULL
3023                 },
3024                 &ssl_cert_file,
3025                 "server.crt",
3026                 NULL, NULL, NULL
3027         },
3028
3029         {
3030                 {"ssl_key_file", PGC_POSTMASTER, CONN_AUTH_SECURITY,
3031                         gettext_noop("Location of the SSL server private key file."),
3032                         NULL
3033                 },
3034                 &ssl_key_file,
3035                 "server.key",
3036                 NULL, NULL, NULL
3037         },
3038
3039         {
3040                 {"ssl_ca_file", PGC_POSTMASTER, CONN_AUTH_SECURITY,
3041                         gettext_noop("Location of the SSL certificate authority file."),
3042                         NULL
3043                 },
3044                 &ssl_ca_file,
3045                 "",
3046                 NULL, NULL, NULL
3047         },
3048
3049         {
3050                 {"ssl_crl_file", PGC_POSTMASTER, CONN_AUTH_SECURITY,
3051                         gettext_noop("Location of the SSL certificate revocation list file."),
3052                         NULL
3053                 },
3054                 &ssl_crl_file,
3055                 "",
3056                 NULL, NULL, NULL
3057         },
3058
3059         {
3060                 {"stats_temp_directory", PGC_SIGHUP, STATS_COLLECTOR,
3061                         gettext_noop("Writes temporary statistics files to the specified directory."),
3062                         NULL,
3063                         GUC_SUPERUSER_ONLY
3064                 },
3065                 &pgstat_temp_directory,
3066                 "pg_stat_tmp",
3067                 check_canonical_path, assign_pgstat_temp_directory, NULL
3068         },
3069
3070         {
3071                 {"synchronous_standby_names", PGC_SIGHUP, REPLICATION_MASTER,
3072                         gettext_noop("List of names of potential synchronous standbys."),
3073                         NULL,
3074                         GUC_LIST_INPUT
3075                 },
3076                 &SyncRepStandbyNames,
3077                 "",
3078                 check_synchronous_standby_names, NULL, NULL
3079         },
3080
3081         {
3082                 {"default_text_search_config", PGC_USERSET, CLIENT_CONN_LOCALE,
3083                         gettext_noop("Sets default text search configuration."),
3084                         NULL
3085                 },
3086                 &TSCurrentConfig,
3087                 "pg_catalog.simple",
3088                 check_TSCurrentConfig, assign_TSCurrentConfig, NULL
3089         },
3090
3091         {
3092                 {"ssl_ciphers", PGC_POSTMASTER, CONN_AUTH_SECURITY,
3093                         gettext_noop("Sets the list of allowed SSL ciphers."),
3094                         NULL,
3095                         GUC_SUPERUSER_ONLY
3096                 },
3097                 &SSLCipherSuites,
3098 #ifdef USE_SSL
3099                 "DEFAULT:!LOW:!EXP:!MD5:@STRENGTH",
3100 #else
3101                 "none",
3102 #endif
3103                 NULL, NULL, NULL
3104         },
3105
3106         {
3107                 {"application_name", PGC_USERSET, LOGGING_WHAT,
3108                         gettext_noop("Sets the application name to be reported in statistics and logs."),
3109                         NULL,
3110                         GUC_IS_NAME | GUC_REPORT | GUC_NOT_IN_SAMPLE
3111                 },
3112                 &application_name,
3113                 "",
3114                 check_application_name, assign_application_name, NULL
3115         },
3116
3117         /* End-of-list marker */
3118         {
3119                 {NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL
3120         }
3121 };
3122
3123
3124 static struct config_enum ConfigureNamesEnum[] =
3125 {
3126         {
3127                 {"backslash_quote", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
3128                         gettext_noop("Sets whether \"\\'\" is allowed in string literals."),
3129                         NULL
3130                 },
3131                 &backslash_quote,
3132                 BACKSLASH_QUOTE_SAFE_ENCODING, backslash_quote_options,
3133                 NULL, NULL, NULL
3134         },
3135
3136         {
3137                 {"bytea_output", PGC_USERSET, CLIENT_CONN_STATEMENT,
3138                         gettext_noop("Sets the output format for bytea."),
3139                         NULL
3140                 },
3141                 &bytea_output,
3142                 BYTEA_OUTPUT_HEX, bytea_output_options,
3143                 NULL, NULL, NULL
3144         },
3145
3146         {
3147                 {"client_min_messages", PGC_USERSET, LOGGING_WHEN,
3148                         gettext_noop("Sets the message levels that are sent to the client."),
3149                         gettext_noop("Each level includes all the levels that follow it. The later"
3150                                                  " the level, the fewer messages are sent.")
3151                 },
3152                 &client_min_messages,
3153                 NOTICE, client_message_level_options,
3154                 NULL, NULL, NULL
3155         },
3156
3157         {
3158                 {"constraint_exclusion", PGC_USERSET, QUERY_TUNING_OTHER,
3159                         gettext_noop("Enables the planner to use constraints to optimize queries."),
3160                         gettext_noop("Table scans will be skipped if their constraints"
3161                                                  " guarantee that no rows match the query.")
3162                 },
3163                 &constraint_exclusion,
3164                 CONSTRAINT_EXCLUSION_PARTITION, constraint_exclusion_options,
3165                 NULL, NULL, NULL
3166         },
3167
3168         {
3169                 {"default_transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
3170                         gettext_noop("Sets the transaction isolation level of each new transaction."),
3171                         NULL
3172                 },
3173                 &DefaultXactIsoLevel,
3174                 XACT_READ_COMMITTED, isolation_level_options,
3175                 NULL, NULL, NULL
3176         },
3177
3178         {
3179                 {"IntervalStyle", PGC_USERSET, CLIENT_CONN_LOCALE,
3180                         gettext_noop("Sets the display format for interval values."),
3181                         NULL,
3182                         GUC_REPORT
3183                 },
3184                 &IntervalStyle,
3185                 INTSTYLE_POSTGRES, intervalstyle_options,
3186                 NULL, NULL, NULL
3187         },
3188
3189         {
3190                 {"log_error_verbosity", PGC_SUSET, LOGGING_WHAT,
3191                         gettext_noop("Sets the verbosity of logged messages."),
3192                         NULL
3193                 },
3194                 &Log_error_verbosity,
3195                 PGERROR_DEFAULT, log_error_verbosity_options,
3196                 NULL, NULL, NULL
3197         },
3198
3199         {
3200                 {"log_min_messages", PGC_SUSET, LOGGING_WHEN,
3201                         gettext_noop("Sets the message levels that are logged."),
3202                         gettext_noop("Each level includes all the levels that follow it. The later"
3203                                                  " the level, the fewer messages are sent.")
3204                 },
3205                 &log_min_messages,
3206                 WARNING, server_message_level_options,
3207                 NULL, NULL, NULL
3208         },
3209
3210         {
3211                 {"log_min_error_statement", PGC_SUSET, LOGGING_WHEN,
3212                         gettext_noop("Causes all statements generating error at or above this level to be logged."),
3213                         gettext_noop("Each level includes all the levels that follow it. The later"
3214                                                  " the level, the fewer messages are sent.")
3215                 },
3216                 &log_min_error_statement,
3217                 ERROR, server_message_level_options,
3218                 NULL, NULL, NULL
3219         },
3220
3221         {
3222                 {"log_statement", PGC_SUSET, LOGGING_WHAT,
3223                         gettext_noop("Sets the type of statements logged."),
3224                         NULL
3225                 },
3226                 &log_statement,
3227                 LOGSTMT_NONE, log_statement_options,
3228                 NULL, NULL, NULL
3229         },
3230
3231         {
3232                 {"syslog_facility", PGC_SIGHUP, LOGGING_WHERE,
3233                         gettext_noop("Sets the syslog \"facility\" to be used when syslog enabled."),
3234                         NULL
3235                 },
3236                 &syslog_facility,
3237 #ifdef HAVE_SYSLOG
3238                 LOG_LOCAL0,
3239 #else
3240                 0,
3241 #endif
3242                 syslog_facility_options,
3243                 NULL, assign_syslog_facility, NULL
3244         },
3245
3246         {
3247                 {"session_replication_role", PGC_SUSET, CLIENT_CONN_STATEMENT,
3248                         gettext_noop("Sets the session's behavior for triggers and rewrite rules."),
3249                         NULL
3250                 },
3251                 &SessionReplicationRole,
3252                 SESSION_REPLICATION_ROLE_ORIGIN, session_replication_role_options,
3253                 NULL, assign_session_replication_role, NULL
3254         },
3255
3256         {
3257                 {"synchronous_commit", PGC_USERSET, WAL_SETTINGS,
3258                         gettext_noop("Sets the current transaction's synchronization level."),
3259                         NULL
3260                 },
3261                 &synchronous_commit,
3262                 SYNCHRONOUS_COMMIT_ON, synchronous_commit_options,
3263                 NULL, assign_synchronous_commit, NULL
3264         },
3265
3266         {
3267                 {"trace_recovery_messages", PGC_SIGHUP, DEVELOPER_OPTIONS,
3268                         gettext_noop("Enables logging of recovery-related debugging information."),
3269                         gettext_noop("Each level includes all the levels that follow it. The later"
3270                                                  " the level, the fewer messages are sent.")
3271                 },
3272                 &trace_recovery_messages,
3273
3274                 /*
3275                  * client_message_level_options allows too many values, really, but
3276                  * it's not worth having a separate options array for this.
3277                  */
3278                 LOG, client_message_level_options,
3279                 NULL, NULL, NULL
3280         },
3281
3282         {
3283                 {"track_functions", PGC_SUSET, STATS_COLLECTOR,
3284                         gettext_noop("Collects function-level statistics on database activity."),
3285                         NULL
3286                 },
3287                 &pgstat_track_functions,
3288                 TRACK_FUNC_OFF, track_function_options,
3289                 NULL, NULL, NULL
3290         },
3291
3292         {
3293                 {"wal_level", PGC_POSTMASTER, WAL_SETTINGS,
3294                         gettext_noop("Set the level of information written to the WAL."),
3295                         NULL
3296                 },
3297                 &wal_level,
3298                 WAL_LEVEL_MINIMAL, wal_level_options,
3299                 NULL, NULL, NULL
3300         },
3301
3302         {
3303                 {"wal_sync_method", PGC_SIGHUP, WAL_SETTINGS,
3304                         gettext_noop("Selects the method used for forcing WAL updates to disk."),
3305                         NULL
3306                 },
3307                 &sync_method,
3308                 DEFAULT_SYNC_METHOD, sync_method_options,
3309                 NULL, assign_xlog_sync_method, NULL
3310         },
3311
3312         {
3313                 {"xmlbinary", PGC_USERSET, CLIENT_CONN_STATEMENT,
3314                         gettext_noop("Sets how binary values are to be encoded in XML."),
3315                         NULL
3316                 },
3317                 &xmlbinary,
3318                 XMLBINARY_BASE64, xmlbinary_options,
3319                 NULL, NULL, NULL
3320         },
3321
3322         {
3323                 {"xmloption", PGC_USERSET, CLIENT_CONN_STATEMENT,
3324                         gettext_noop("Sets whether XML data in implicit parsing and serialization "
3325                                                  "operations is to be considered as documents or content fragments."),
3326                         NULL
3327                 },
3328                 &xmloption,
3329                 XMLOPTION_CONTENT, xmloption_options,
3330                 NULL, NULL, NULL
3331         },
3332
3333
3334         /* End-of-list marker */
3335         {
3336                 {NULL, 0, 0, NULL, NULL}, NULL, 0, NULL, NULL, NULL, NULL
3337         }
3338 };
3339
3340 /******** end of options list ********/
3341
3342
3343 /*
3344  * To allow continued support of obsolete names for GUC variables, we apply
3345  * the following mappings to any unrecognized name.  Note that an old name
3346  * should be mapped to a new one only if the new variable has very similar
3347  * semantics to the old.
3348  */
3349 static const char *const map_old_guc_names[] = {
3350         "sort_mem", "work_mem",
3351         "vacuum_mem", "maintenance_work_mem",
3352         NULL
3353 };
3354
3355
3356 /*
3357  * Actual lookup of variables is done through this single, sorted array.
3358  */
3359 static struct config_generic **guc_variables;
3360
3361 /* Current number of variables contained in the vector */
3362 static int      num_guc_variables;
3363
3364 /* Vector capacity */
3365 static int      size_guc_variables;
3366
3367
3368 static bool guc_dirty;                  /* TRUE if need to do commit/abort work */
3369
3370 static bool reporting_enabled;  /* TRUE to enable GUC_REPORT */
3371
3372 static int      GUCNestLevel = 0;       /* 1 when in main transaction */
3373
3374
3375 static int      guc_var_compare(const void *a, const void *b);
3376 static int      guc_name_compare(const char *namea, const char *nameb);
3377 static void InitializeGUCOptionsFromEnvironment(void);
3378 static void InitializeOneGUCOption(struct config_generic * gconf);
3379 static void push_old_value(struct config_generic * gconf, GucAction action);
3380 static void ReportGUCOption(struct config_generic * record);
3381 static void reapply_stacked_values(struct config_generic * variable,
3382                                            struct config_string * pHolder,
3383                                            GucStack *stack,
3384                                            const char *curvalue,
3385                                            GucContext curscontext, GucSource cursource);
3386 static void ShowGUCConfigOption(const char *name, DestReceiver *dest);
3387 static void ShowAllGUCConfig(DestReceiver *dest);
3388 static char *_ShowOption(struct config_generic * record, bool use_units);
3389 static bool validate_option_array_item(const char *name, const char *value,
3390                                                    bool skipIfNoPermissions);
3391
3392
3393 /*
3394  * Some infrastructure for checking malloc/strdup/realloc calls
3395  */
3396 static void *
3397 guc_malloc(int elevel, size_t size)
3398 {
3399         void       *data;
3400
3401         /* Avoid unportable behavior of malloc(0) */
3402         if (size == 0)
3403                 size = 1;
3404         data = malloc(size);
3405         if (data == NULL)
3406                 ereport(elevel,
3407                                 (errcode(ERRCODE_OUT_OF_MEMORY),
3408                                  errmsg("out of memory")));
3409         return data;
3410 }
3411
3412 static void *
3413 guc_realloc(int elevel, void *old, size_t size)
3414 {
3415         void       *data;
3416
3417         /* Avoid unportable behavior of realloc(NULL, 0) */
3418         if (old == NULL && size == 0)
3419                 size = 1;
3420         data = realloc(old, size);
3421         if (data == NULL)
3422                 ereport(elevel,
3423                                 (errcode(ERRCODE_OUT_OF_MEMORY),
3424                                  errmsg("out of memory")));
3425         return data;
3426 }
3427
3428 static char *
3429 guc_strdup(int elevel, const char *src)
3430 {
3431         char       *data;
3432
3433         data = strdup(src);
3434         if (data == NULL)
3435                 ereport(elevel,
3436                                 (errcode(ERRCODE_OUT_OF_MEMORY),
3437                                  errmsg("out of memory")));
3438         return data;
3439 }
3440
3441
3442 /*
3443  * Detect whether strval is referenced anywhere in a GUC string item
3444  */
3445 static bool
3446 string_field_used(struct config_string * conf, char *strval)
3447 {
3448         GucStack   *stack;
3449
3450         if (strval == *(conf->variable) ||
3451                 strval == conf->reset_val ||
3452                 strval == conf->boot_val)
3453                 return true;
3454         for (stack = conf->gen.stack; stack; stack = stack->prev)
3455         {
3456                 if (strval == stack->prior.val.stringval ||
3457                         strval == stack->masked.val.stringval)
3458                         return true;
3459         }
3460         return false;
3461 }
3462
3463 /*
3464  * Support for assigning to a field of a string GUC item.  Free the prior
3465  * value if it's not referenced anywhere else in the item (including stacked
3466  * states).
3467  */
3468 static void
3469 set_string_field(struct config_string * conf, char **field, char *newval)
3470 {
3471         char       *oldval = *field;
3472
3473         /* Do the assignment */
3474         *field = newval;
3475
3476         /* Free old value if it's not NULL and isn't referenced anymore */
3477         if (oldval && !string_field_used(conf, oldval))
3478                 free(oldval);
3479 }
3480
3481 /*
3482  * Detect whether an "extra" struct is referenced anywhere in a GUC item
3483  */
3484 static bool
3485 extra_field_used(struct config_generic * gconf, void *extra)
3486 {
3487         GucStack   *stack;
3488
3489         if (extra == gconf->extra)
3490                 return true;
3491         switch (gconf->vartype)
3492         {
3493                 case PGC_BOOL:
3494                         if (extra == ((struct config_bool *) gconf)->reset_extra)
3495                                 return true;
3496                         break;
3497                 case PGC_INT:
3498                         if (extra == ((struct config_int *) gconf)->reset_extra)
3499                                 return true;
3500                         break;
3501                 case PGC_REAL:
3502                         if (extra == ((struct config_real *) gconf)->reset_extra)
3503                                 return true;
3504                         break;
3505                 case PGC_STRING:
3506                         if (extra == ((struct config_string *) gconf)->reset_extra)
3507                                 return true;
3508                         break;
3509                 case PGC_ENUM:
3510                         if (extra == ((struct config_enum *) gconf)->reset_extra)
3511                                 return true;
3512                         break;
3513         }
3514         for (stack = gconf->stack; stack; stack = stack->prev)
3515         {
3516                 if (extra == stack->prior.extra ||
3517                         extra == stack->masked.extra)
3518                         return true;
3519         }
3520
3521         return false;
3522 }
3523
3524 /*
3525  * Support for assigning to an "extra" field of a GUC item.  Free the prior
3526  * value if it's not referenced anywhere else in the item (including stacked
3527  * states).
3528  */
3529 static void
3530 set_extra_field(struct config_generic * gconf, void **field, void *newval)
3531 {
3532         void       *oldval = *field;
3533
3534         /* Do the assignment */
3535         *field = newval;
3536
3537         /* Free old value if it's not NULL and isn't referenced anymore */
3538         if (oldval && !extra_field_used(gconf, oldval))
3539                 free(oldval);
3540 }
3541
3542 /*
3543  * Support for copying a variable's active value into a stack entry.
3544  * The "extra" field associated with the active value is copied, too.
3545  *
3546  * NB: be sure stringval and extra fields of a new stack entry are
3547  * initialized to NULL before this is used, else we'll try to free() them.
3548  */
3549 static void
3550 set_stack_value(struct config_generic * gconf, config_var_value *val)
3551 {
3552         switch (gconf->vartype)
3553         {
3554                 case PGC_BOOL:
3555                         val->val.boolval =
3556                                 *((struct config_bool *) gconf)->variable;
3557                         break;
3558                 case PGC_INT:
3559                         val->val.intval =
3560                                 *((struct config_int *) gconf)->variable;
3561                         break;
3562                 case PGC_REAL:
3563                         val->val.realval =
3564                                 *((struct config_real *) gconf)->variable;
3565                         break;
3566                 case PGC_STRING:
3567                         set_string_field((struct config_string *) gconf,
3568                                                          &(val->val.stringval),
3569                                                          *((struct config_string *) gconf)->variable);
3570                         break;
3571                 case PGC_ENUM:
3572                         val->val.enumval =
3573                                 *((struct config_enum *) gconf)->variable;
3574                         break;
3575         }
3576         set_extra_field(gconf, &(val->extra), gconf->extra);
3577 }
3578
3579 /*
3580  * Support for discarding a no-longer-needed value in a stack entry.
3581  * The "extra" field associated with the stack entry is cleared, too.
3582  */
3583 static void
3584 discard_stack_value(struct config_generic * gconf, config_var_value *val)
3585 {
3586         switch (gconf->vartype)
3587         {
3588                 case PGC_BOOL:
3589                 case PGC_INT:
3590                 case PGC_REAL:
3591                 case PGC_ENUM:
3592                         /* no need to do anything */
3593                         break;
3594                 case PGC_STRING:
3595                         set_string_field((struct config_string *) gconf,
3596                                                          &(val->val.stringval),
3597                                                          NULL);
3598                         break;
3599         }
3600         set_extra_field(gconf, &(val->extra), NULL);
3601 }
3602
3603
3604 /*
3605  * Fetch the sorted array pointer (exported for help_config.c's use ONLY)
3606  */
3607 struct config_generic **
3608 get_guc_variables(void)
3609 {
3610         return guc_variables;
3611 }
3612
3613
3614 /*
3615  * Build the sorted array.      This is split out so that it could be
3616  * re-executed after startup (eg, we could allow loadable modules to
3617  * add vars, and then we'd need to re-sort).
3618  */
3619 void
3620 build_guc_variables(void)
3621 {
3622         int                     size_vars;
3623         int                     num_vars = 0;
3624         struct config_generic **guc_vars;
3625         int                     i;
3626
3627         for (i = 0; ConfigureNamesBool[i].gen.name; i++)
3628         {
3629                 struct config_bool *conf = &ConfigureNamesBool[i];
3630
3631                 /* Rather than requiring vartype to be filled in by hand, do this: */
3632                 conf->gen.vartype = PGC_BOOL;
3633                 num_vars++;
3634         }
3635
3636         for (i = 0; ConfigureNamesInt[i].gen.name; i++)
3637         {
3638                 struct config_int *conf = &ConfigureNamesInt[i];
3639
3640                 conf->gen.vartype = PGC_INT;
3641                 num_vars++;
3642         }
3643
3644         for (i = 0; ConfigureNamesReal[i].gen.name; i++)
3645         {
3646                 struct config_real *conf = &ConfigureNamesReal[i];
3647
3648                 conf->gen.vartype = PGC_REAL;
3649                 num_vars++;
3650         }
3651
3652         for (i = 0; ConfigureNamesString[i].gen.name; i++)
3653         {
3654                 struct config_string *conf = &ConfigureNamesString[i];
3655
3656                 conf->gen.vartype = PGC_STRING;
3657                 num_vars++;
3658         }
3659
3660         for (i = 0; ConfigureNamesEnum[i].gen.name; i++)
3661         {
3662                 struct config_enum *conf = &ConfigureNamesEnum[i];
3663
3664                 conf->gen.vartype = PGC_ENUM;
3665                 num_vars++;
3666         }
3667
3668         /*
3669          * Create table with 20% slack
3670          */
3671         size_vars = num_vars + num_vars / 4;
3672
3673         guc_vars = (struct config_generic **)
3674                 guc_malloc(FATAL, size_vars * sizeof(struct config_generic *));
3675
3676         num_vars = 0;
3677
3678         for (i = 0; ConfigureNamesBool[i].gen.name; i++)
3679                 guc_vars[num_vars++] = &ConfigureNamesBool[i].gen;
3680
3681         for (i = 0; ConfigureNamesInt[i].gen.name; i++)
3682                 guc_vars[num_vars++] = &ConfigureNamesInt[i].gen;
3683
3684         for (i = 0; ConfigureNamesReal[i].gen.name; i++)
3685                 guc_vars[num_vars++] = &ConfigureNamesReal[i].gen;
3686
3687         for (i = 0; ConfigureNamesString[i].gen.name; i++)
3688                 guc_vars[num_vars++] = &ConfigureNamesString[i].gen;
3689
3690         for (i = 0; ConfigureNamesEnum[i].gen.name; i++)
3691                 guc_vars[num_vars++] = &ConfigureNamesEnum[i].gen;
3692
3693         if (guc_variables)
3694                 free(guc_variables);
3695         guc_variables = guc_vars;
3696         num_guc_variables = num_vars;
3697         size_guc_variables = size_vars;
3698         qsort((void *) guc_variables, num_guc_variables,
3699                   sizeof(struct config_generic *), guc_var_compare);
3700 }
3701
3702 /*
3703  * Add a new GUC variable to the list of known variables. The
3704  * list is expanded if needed.
3705  */
3706 static bool
3707 add_guc_variable(struct config_generic * var, int elevel)
3708 {
3709         if (num_guc_variables + 1 >= size_guc_variables)
3710         {
3711                 /*
3712                  * Increase the vector by 25%
3713                  */
3714                 int                     size_vars = size_guc_variables + size_guc_variables / 4;
3715                 struct config_generic **guc_vars;
3716
3717                 if (size_vars == 0)
3718                 {
3719                         size_vars = 100;
3720                         guc_vars = (struct config_generic **)
3721                                 guc_malloc(elevel, size_vars * sizeof(struct config_generic *));
3722                 }
3723                 else
3724                 {
3725                         guc_vars = (struct config_generic **)
3726                                 guc_realloc(elevel, guc_variables, size_vars * sizeof(struct config_generic *));
3727                 }
3728
3729                 if (guc_vars == NULL)
3730                         return false;           /* out of memory */
3731
3732                 guc_variables = guc_vars;
3733                 size_guc_variables = size_vars;
3734         }
3735         guc_variables[num_guc_variables++] = var;
3736         qsort((void *) guc_variables, num_guc_variables,
3737                   sizeof(struct config_generic *), guc_var_compare);
3738         return true;
3739 }
3740
3741 /*
3742  * Create and add a placeholder variable for a custom variable name.
3743  */
3744 static struct config_generic *
3745 add_placeholder_variable(const char *name, int elevel)
3746 {
3747         size_t          sz = sizeof(struct config_string) + sizeof(char *);
3748         struct config_string *var;
3749         struct config_generic *gen;
3750
3751         var = (struct config_string *) guc_malloc(elevel, sz);
3752         if (var == NULL)
3753                 return NULL;
3754         memset(var, 0, sz);
3755         gen = &var->gen;
3756
3757         gen->name = guc_strdup(elevel, name);
3758         if (gen->name == NULL)
3759         {
3760                 free(var);
3761                 return NULL;
3762         }
3763
3764         gen->context = PGC_USERSET;
3765         gen->group = CUSTOM_OPTIONS;
3766         gen->short_desc = "GUC placeholder variable";
3767         gen->flags = GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE | GUC_CUSTOM_PLACEHOLDER;
3768         gen->vartype = PGC_STRING;
3769
3770         /*
3771          * The char* is allocated at the end of the struct since we have no
3772          * 'static' place to point to.  Note that the current value, as well as
3773          * the boot and reset values, start out NULL.
3774          */
3775         var->variable = (char **) (var + 1);
3776
3777         if (!add_guc_variable((struct config_generic *) var, elevel))
3778         {
3779                 free((void *) gen->name);
3780                 free(var);
3781                 return NULL;
3782         }
3783
3784         return gen;
3785 }
3786
3787 /*
3788  * Look up option NAME.  If it exists, return a pointer to its record,
3789  * else return NULL.  If create_placeholders is TRUE, we'll create a
3790  * placeholder record for a valid-looking custom variable name.
3791  */
3792 static struct config_generic *
3793 find_option(const char *name, bool create_placeholders, int elevel)
3794 {
3795         const char **key = &name;
3796         struct config_generic **res;
3797         int                     i;
3798
3799         Assert(name);
3800
3801         /*
3802          * By equating const char ** with struct config_generic *, we are assuming
3803          * the name field is first in config_generic.
3804          */
3805         res = (struct config_generic **) bsearch((void *) &key,
3806                                                                                          (void *) guc_variables,
3807                                                                                          num_guc_variables,
3808                                                                                          sizeof(struct config_generic *),
3809                                                                                          guc_var_compare);
3810         if (res)
3811                 return *res;
3812
3813         /*
3814          * See if the name is an obsolete name for a variable.  We assume that the
3815          * set of supported old names is short enough that a brute-force search is
3816          * the best way.
3817          */
3818         for (i = 0; map_old_guc_names[i] != NULL; i += 2)
3819         {
3820                 if (guc_name_compare(name, map_old_guc_names[i]) == 0)
3821                         return find_option(map_old_guc_names[i + 1], false, elevel);
3822         }
3823
3824         if (create_placeholders)
3825         {
3826                 /*
3827                  * Check if the name is qualified, and if so, add a placeholder.
3828                  */
3829                 if (strchr(name, GUC_QUALIFIER_SEPARATOR) != NULL)
3830                         return add_placeholder_variable(name, elevel);
3831         }
3832
3833         /* Unknown name */
3834         return NULL;
3835 }
3836
3837
3838 /*
3839  * comparator for qsorting and bsearching guc_variables array
3840  */
3841 static int
3842 guc_var_compare(const void *a, const void *b)
3843 {
3844         const struct config_generic *confa = *(struct config_generic * const *) a;
3845         const struct config_generic *confb = *(struct config_generic * const *) b;
3846
3847         return guc_name_compare(confa->name, confb->name);
3848 }
3849
3850 /*
3851  * the bare comparison function for GUC names
3852  */
3853 static int
3854 guc_name_compare(const char *namea, const char *nameb)
3855 {
3856         /*
3857          * The temptation to use strcasecmp() here must be resisted, because the
3858          * array ordering has to remain stable across setlocale() calls. So, build
3859          * our own with a simple ASCII-only downcasing.
3860          */
3861         while (*namea && *nameb)
3862         {
3863                 char            cha = *namea++;
3864                 char            chb = *nameb++;
3865
3866                 if (cha >= 'A' && cha <= 'Z')
3867                         cha += 'a' - 'A';
3868                 if (chb >= 'A' && chb <= 'Z')
3869                         chb += 'a' - 'A';
3870                 if (cha != chb)
3871                         return cha - chb;
3872         }
3873         if (*namea)
3874                 return 1;                               /* a is longer */
3875         if (*nameb)
3876                 return -1;                              /* b is longer */
3877         return 0;
3878 }
3879
3880
3881 /*
3882  * Initialize GUC options during program startup.
3883  *
3884  * Note that we cannot read the config file yet, since we have not yet
3885  * processed command-line switches.
3886  */
3887 void
3888 InitializeGUCOptions(void)
3889 {
3890         int                     i;
3891
3892         /*
3893          * Before log_line_prefix could possibly receive a nonempty setting, make
3894          * sure that timezone processing is minimally alive (see elog.c).
3895          */
3896         pg_timezone_initialize();
3897
3898         /*
3899          * Build sorted array of all GUC variables.
3900          */
3901         build_guc_variables();
3902
3903         /*
3904          * Load all variables with their compiled-in defaults, and initialize
3905          * status fields as needed.
3906          */
3907         for (i = 0; i < num_guc_variables; i++)
3908         {
3909                 InitializeOneGUCOption(guc_variables[i]);
3910         }
3911
3912         guc_dirty = false;
3913
3914         reporting_enabled = false;
3915
3916         /*
3917          * Prevent any attempt to override the transaction modes from
3918          * non-interactive sources.
3919          */
3920         SetConfigOption("transaction_isolation", "default",
3921                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3922         SetConfigOption("transaction_read_only", "no",
3923                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3924         SetConfigOption("transaction_deferrable", "no",
3925                                         PGC_POSTMASTER, PGC_S_OVERRIDE);
3926
3927         /*
3928          * For historical reasons, some GUC parameters can receive defaults from
3929          * environment variables.  Process those settings.
3930          */
3931         InitializeGUCOptionsFromEnvironment();
3932 }
3933
3934 /*
3935  * Assign any GUC values that can come from the server's environment.
3936  *
3937  * This is called from InitializeGUCOptions, and also from ProcessConfigFile
3938  * to deal with the possibility that a setting has been removed from
3939  * postgresql.conf and should now get a value from the environment.
3940  * (The latter is a kludge that should probably go away someday; if so,
3941  * fold this back into InitializeGUCOptions.)
3942  */
3943 static void
3944 InitializeGUCOptionsFromEnvironment(void)
3945 {
3946         char       *env;
3947         long            stack_rlimit;
3948
3949         env = getenv("PGPORT");
3950         if (env != NULL)
3951                 SetConfigOption("port", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3952
3953         env = getenv("PGDATESTYLE");
3954         if (env != NULL)
3955                 SetConfigOption("datestyle", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3956
3957         env = getenv("PGCLIENTENCODING");
3958         if (env != NULL)
3959                 SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3960
3961         /*
3962          * rlimit isn't exactly an "environment variable", but it behaves about
3963          * the same.  If we can identify the platform stack depth rlimit, increase
3964          * default stack depth setting up to whatever is safe (but at most 2MB).
3965          */
3966         stack_rlimit = get_stack_depth_rlimit();
3967         if (stack_rlimit > 0)
3968         {
3969                 long            new_limit = (stack_rlimit - STACK_DEPTH_SLOP) / 1024L;
3970
3971                 if (new_limit > 100)
3972                 {
3973                         char            limbuf[16];
3974
3975                         new_limit = Min(new_limit, 2048);
3976                         sprintf(limbuf, "%ld", new_limit);
3977                         SetConfigOption("max_stack_depth", limbuf,
3978                                                         PGC_POSTMASTER, PGC_S_ENV_VAR);
3979                 }
3980         }
3981 }
3982
3983 /*
3984  * Initialize one GUC option variable to its compiled-in default.
3985  *
3986  * Note: the reason for calling check_hooks is not that we think the boot_val
3987  * might fail, but that the hooks might wish to compute an "extra" struct.
3988  */
3989 static void
3990 InitializeOneGUCOption(struct config_generic * gconf)
3991 {
3992         gconf->status = 0;
3993         gconf->source = PGC_S_DEFAULT;
3994         gconf->reset_source = PGC_S_DEFAULT;
3995         gconf->scontext = PGC_INTERNAL;
3996         gconf->reset_scontext = PGC_INTERNAL;
3997         gconf->stack = NULL;
3998         gconf->extra = NULL;
3999         gconf->sourcefile = NULL;
4000         gconf->sourceline = 0;
4001
4002         switch (gconf->vartype)
4003         {
4004                 case PGC_BOOL:
4005                         {
4006                                 struct config_bool *conf = (struct config_bool *) gconf;
4007                                 bool            newval = conf->boot_val;
4008                                 void       *extra = NULL;
4009
4010                                 if (!call_bool_check_hook(conf, &newval, &extra,
4011                                                                                   PGC_S_DEFAULT, LOG))
4012                                         elog(FATAL, "failed to initialize %s to %d",
4013                                                  conf->gen.name, (int) newval);
4014                                 if (conf->assign_hook)
4015                                         (*conf->assign_hook) (newval, extra);
4016                                 *conf->variable = conf->reset_val = newval;
4017                                 conf->gen.extra = conf->reset_extra = extra;
4018                                 break;
4019                         }
4020                 case PGC_INT:
4021                         {
4022                                 struct config_int *conf = (struct config_int *) gconf;
4023                                 int                     newval = conf->boot_val;
4024                                 void       *extra = NULL;
4025
4026                                 Assert(newval >= conf->min);
4027                                 Assert(newval <= conf->max);
4028                                 if (!call_int_check_hook(conf, &newval, &extra,
4029                                                                                  PGC_S_DEFAULT, LOG))
4030                                         elog(FATAL, "failed to initialize %s to %d",
4031                                                  conf->gen.name, newval);
4032                                 if (conf->assign_hook)
4033                                         (*conf->assign_hook) (newval, extra);
4034                                 *conf->variable = conf->reset_val = newval;
4035                                 conf->gen.extra = conf->reset_extra = extra;
4036                                 break;
4037                         }
4038                 case PGC_REAL:
4039                         {
4040                                 struct config_real *conf = (struct config_real *) gconf;
4041                                 double          newval = conf->boot_val;
4042                                 void       *extra = NULL;
4043
4044                                 Assert(newval >= conf->min);
4045                                 Assert(newval <= conf->max);
4046                                 if (!call_real_check_hook(conf, &newval, &extra,
4047                                                                                   PGC_S_DEFAULT, LOG))
4048                                         elog(FATAL, "failed to initialize %s to %g",
4049                                                  conf->gen.name, newval);
4050                                 if (conf->assign_hook)
4051                                         (*conf->assign_hook) (newval, extra);
4052                                 *conf->variable = conf->reset_val = newval;
4053                                 conf->gen.extra = conf->reset_extra = extra;
4054                                 break;
4055                         }
4056                 case PGC_STRING:
4057                         {
4058                                 struct config_string *conf = (struct config_string *) gconf;
4059                                 char       *newval;
4060                                 void       *extra = NULL;
4061
4062                                 /* non-NULL boot_val must always get strdup'd */
4063                                 if (conf->boot_val != NULL)
4064                                         newval = guc_strdup(FATAL, conf->boot_val);
4065                                 else
4066                                         newval = NULL;
4067
4068                                 if (!call_string_check_hook(conf, &newval, &extra,
4069                                                                                         PGC_S_DEFAULT, LOG))
4070                                         elog(FATAL, "failed to initialize %s to \"%s\"",
4071                                                  conf->gen.name, newval ? newval : "");
4072                                 if (conf->assign_hook)
4073                                         (*conf->assign_hook) (newval, extra);
4074                                 *conf->variable = conf->reset_val = newval;
4075                                 conf->gen.extra = conf->reset_extra = extra;
4076                                 break;
4077                         }
4078                 case PGC_ENUM:
4079                         {
4080                                 struct config_enum *conf = (struct config_enum *) gconf;
4081                                 int                     newval = conf->boot_val;
4082                                 void       *extra = NULL;
4083
4084                                 if (!call_enum_check_hook(conf, &newval, &extra,
4085                                                                                   PGC_S_DEFAULT, LOG))
4086                                         elog(FATAL, "failed to initialize %s to %d",
4087                                                  conf->gen.name, newval);
4088                                 if (conf->assign_hook)
4089                                         (*conf->assign_hook) (newval, extra);
4090                                 *conf->variable = conf->reset_val = newval;
4091                                 conf->gen.extra = conf->reset_extra = extra;
4092                                 break;
4093                         }
4094         }
4095 }
4096
4097
4098 /*
4099  * Select the configuration files and data directory to be used, and
4100  * do the initial read of postgresql.conf.
4101  *
4102  * This is called after processing command-line switches.
4103  *              userDoption is the -D switch value if any (NULL if unspecified).
4104  *              progname is just for use in error messages.
4105  *
4106  * Returns true on success; on failure, prints a suitable error message
4107  * to stderr and returns false.
4108  */
4109 bool
4110 SelectConfigFiles(const char *userDoption, const char *progname)
4111 {
4112         char       *configdir;
4113         char       *fname;
4114         struct stat stat_buf;
4115
4116         /* configdir is -D option, or $PGDATA if no -D */
4117         if (userDoption)
4118                 configdir = make_absolute_path(userDoption);
4119         else
4120                 configdir = make_absolute_path(getenv("PGDATA"));
4121
4122         /*
4123          * Find the configuration file: if config_file was specified on the
4124          * command line, use it, else use configdir/postgresql.conf.  In any case
4125          * ensure the result is an absolute path, so that it will be interpreted
4126          * the same way by future backends.
4127          */
4128         if (ConfigFileName)
4129                 fname = make_absolute_path(ConfigFileName);
4130         else if (configdir)
4131         {
4132                 fname = guc_malloc(FATAL,
4133                                                    strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
4134                 sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
4135         }
4136         else
4137         {
4138                 write_stderr("%s does not know where to find the server configuration file.\n"
4139                                          "You must specify the --config-file or -D invocation "
4140                                          "option or set the PGDATA environment variable.\n",
4141                                          progname);
4142                 return false;
4143         }
4144
4145         /*
4146          * Set the ConfigFileName GUC variable to its final value, ensuring that
4147          * it can't be overridden later.
4148          */
4149         SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
4150         free(fname);
4151
4152         /*
4153          * Now read the config file for the first time.
4154          */
4155         if (stat(ConfigFileName, &stat_buf) != 0)
4156         {
4157                 write_stderr("%s cannot access the server configuration file \"%s\": %s\n",
4158                                          progname, ConfigFileName, strerror(errno));
4159                 free(configdir);
4160                 return false;
4161         }
4162
4163         ProcessConfigFile(PGC_POSTMASTER);
4164
4165         /*
4166          * If the data_directory GUC variable has been set, use that as DataDir;
4167          * otherwise use configdir if set; else punt.
4168          *
4169          * Note: SetDataDir will copy and absolute-ize its argument, so we don't
4170          * have to.
4171          */
4172         if (data_directory)
4173                 SetDataDir(data_directory);
4174         else if (configdir)
4175                 SetDataDir(configdir);
4176         else
4177         {
4178                 write_stderr("%s does not know where to find the database system data.\n"
4179                                          "This can be specified as \"data_directory\" in \"%s\", "
4180                                          "or by the -D invocation option, or by the "
4181                                          "PGDATA environment variable.\n",
4182                                          progname, ConfigFileName);
4183                 return false;
4184         }
4185
4186         /*
4187          * Reflect the final DataDir value back into the data_directory GUC var.
4188          * (If you are wondering why we don't just make them a single variable,
4189          * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
4190          * child backends specially.  XXX is that still true?  Given that we now
4191          * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
4192          * DataDir in advance.)
4193          */
4194         SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);
4195
4196         /*
4197          * If the recovery_config_directory GUC variable has been set, use that,
4198          * otherwise use DataDir.
4199          *
4200          * Note: SetRecoveryConfDir will copy and absolute-ize its argument,
4201          * so we don't have to.
4202          */
4203         if (recovery_config_directory)
4204                 SetRecoveryConfDir(recovery_config_directory);
4205         else
4206                 SetRecoveryConfDir(DataDir);
4207
4208         /*
4209          * If timezone_abbreviations wasn't set in the configuration file, install
4210          * the default value.  We do it this way because we can't safely install a
4211          * "real" value until my_exec_path is set, which may not have happened
4212          * when InitializeGUCOptions runs, so the bootstrap default value cannot
4213          * be the real desired default.
4214          */
4215         pg_timezone_abbrev_initialize();
4216
4217         /*
4218          * Figure out where pg_hba.conf is, and make sure the path is absolute.
4219          */
4220         if (HbaFileName)
4221                 fname = make_absolute_path(HbaFileName);
4222         else if (configdir)
4223         {
4224                 fname = guc_malloc(FATAL,
4225                                                    strlen(configdir) + strlen(HBA_FILENAME) + 2);
4226                 sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
4227         }
4228         else
4229         {
4230                 write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
4231                                          "This can be specified as \"hba_file\" in \"%s\", "
4232                                          "or by the -D invocation option, or by the "
4233                                          "PGDATA environment variable.\n",
4234                                          progname, ConfigFileName);
4235                 return false;
4236         }
4237         SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
4238         free(fname);
4239
4240         /*
4241          * Likewise for pg_ident.conf.
4242          */
4243         if (IdentFileName)
4244                 fname = make_absolute_path(IdentFileName);
4245         else if (configdir)
4246         {
4247                 fname = guc_malloc(FATAL,
4248                                                    strlen(configdir) + strlen(IDENT_FILENAME) + 2);
4249                 sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
4250         }
4251         else
4252         {
4253                 write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
4254                                          "This can be specified as \"ident_file\" in \"%s\", "
4255                                          "or by the -D invocation option, or by the "
4256                                          "PGDATA environment variable.\n",
4257                                          progname, ConfigFileName);
4258                 return false;
4259         }
4260         SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
4261         free(fname);
4262
4263         free(configdir);
4264
4265         return true;
4266 }
4267
4268
4269 /*
4270  * Reset all options to their saved default values (implements RESET ALL)
4271  */
4272 void
4273 ResetAllOptions(void)
4274 {
4275         int                     i;
4276
4277         for (i = 0; i < num_guc_variables; i++)
4278         {
4279                 struct config_generic *gconf = guc_variables[i];
4280
4281                 /* Don't reset non-SET-able values */
4282                 if (gconf->context != PGC_SUSET &&
4283                         gconf->context != PGC_USERSET)
4284                         continue;
4285                 /* Don't reset if special exclusion from RESET ALL */
4286                 if (gconf->flags & GUC_NO_RESET_ALL)
4287                         continue;
4288                 /* No need to reset if wasn't SET */
4289                 if (gconf->source <= PGC_S_OVERRIDE)
4290                         continue;
4291
4292                 /* Save old value to support transaction abort */
4293                 push_old_value(gconf, GUC_ACTION_SET);
4294
4295                 switch (gconf->vartype)
4296                 {
4297                         case PGC_BOOL:
4298                                 {
4299                                         struct config_bool *conf = (struct config_bool *) gconf;
4300
4301                                         if (conf->assign_hook)
4302                                                 (*conf->assign_hook) (conf->reset_val,
4303                                                                                           conf->reset_extra);
4304                                         *conf->variable = conf->reset_val;
4305                                         set_extra_field(&conf->gen, &conf->gen.extra,
4306                                                                         conf->reset_extra);
4307                                         break;
4308                                 }
4309                         case PGC_INT:
4310                                 {
4311                                         struct config_int *conf = (struct config_int *) gconf;
4312
4313                                         if (conf->assign_hook)
4314                                                 (*conf->assign_hook) (conf->reset_val,
4315                                                                                           conf->reset_extra);
4316                                         *conf->variable = conf->reset_val;
4317                                         set_extra_field(&conf->gen, &conf->gen.extra,
4318                                                                         conf->reset_extra);
4319                                         break;
4320                                 }
4321                         case PGC_REAL:
4322                                 {
4323                                         struct config_real *conf = (struct config_real *) gconf;
4324
4325                                         if (conf->assign_hook)
4326                                                 (*conf->assign_hook) (conf->reset_val,
4327                                                                                           conf->reset_extra);
4328                                         *conf->variable = conf->reset_val;
4329                                         set_extra_field(&conf->gen, &conf->gen.extra,
4330                                                                         conf->reset_extra);
4331                                         break;
4332                                 }
4333                         case PGC_STRING:
4334                                 {
4335                                         struct config_string *conf = (struct config_string *) gconf;
4336
4337                                         if (conf->assign_hook)
4338                                                 (*conf->assign_hook) (conf->reset_val,
4339                                                                                           conf->reset_extra);
4340                                         set_string_field(conf, conf->variable, conf->reset_val);
4341                                         set_extra_field(&conf->gen, &conf->gen.extra,
4342                                                                         conf->reset_extra);
4343                                         break;
4344                                 }
4345                         case PGC_ENUM:
4346                                 {
4347                                         struct config_enum *conf = (struct config_enum *) gconf;
4348
4349                                         if (conf->assign_hook)
4350                                                 (*conf->assign_hook) (conf->reset_val,
4351                                                                                           conf->reset_extra);
4352                                         *conf->variable = conf->reset_val;
4353                                         set_extra_field(&conf->gen, &conf->gen.extra,
4354                                                                         conf->reset_extra);
4355                                         break;
4356                                 }
4357                 }
4358
4359                 gconf->source = gconf->reset_source;
4360                 gconf->scontext = gconf->reset_scontext;
4361
4362                 if (gconf->flags & GUC_REPORT)
4363                         ReportGUCOption(gconf);
4364         }
4365 }
4366
4367
4368 /*
4369  * push_old_value
4370  *              Push previous state during transactional assignment to a GUC variable.
4371  */
4372 static void
4373 push_old_value(struct config_generic * gconf, GucAction action)
4374 {
4375         GucStack   *stack;
4376
4377         /* If we're not inside a nest level, do nothing */
4378         if (GUCNestLevel == 0)
4379                 return;
4380
4381         /* Do we already have a stack entry of the current nest level? */
4382         stack = gconf->stack;
4383         if (stack && stack->nest_level >= GUCNestLevel)
4384         {
4385                 /* Yes, so adjust its state if necessary */
4386                 Assert(stack->nest_level == GUCNestLevel);
4387                 switch (action)
4388                 {
4389                         case GUC_ACTION_SET:
4390                                 /* SET overrides any prior action at same nest level */
4391                                 if (stack->state == GUC_SET_LOCAL)
4392                                 {
4393                                         /* must discard old masked value */
4394                                         discard_stack_value(gconf, &stack->masked);
4395                                 }
4396                                 stack->state = GUC_SET;
4397                                 break;
4398                         case GUC_ACTION_LOCAL:
4399                                 if (stack->state == GUC_SET)
4400                                 {
4401                                         /* SET followed by SET LOCAL, remember SET's value */
4402                                         stack->masked_scontext = gconf->scontext;
4403                                         set_stack_value(gconf, &stack->masked);
4404                                         stack->state = GUC_SET_LOCAL;
4405                                 }
4406                                 /* in all other cases, no change to stack entry */
4407                                 break;
4408                         case GUC_ACTION_SAVE:
4409                                 /* Could only have a prior SAVE of same variable */
4410                                 Assert(stack->state == GUC_SAVE);
4411                                 break;
4412                 }
4413                 Assert(guc_dirty);              /* must be set already */
4414                 return;
4415         }
4416
4417         /*
4418          * Push a new stack entry
4419          *
4420          * We keep all the stack entries in TopTransactionContext for simplicity.
4421          */
4422         stack = (GucStack *) MemoryContextAllocZero(TopTransactionContext,
4423                                                                                                 sizeof(GucStack));
4424
4425         stack->prev = gconf->stack;
4426         stack->nest_level = GUCNestLevel;
4427         switch (action)
4428         {
4429                 case GUC_ACTION_SET:
4430                         stack->state = GUC_SET;
4431                         break;
4432                 case GUC_ACTION_LOCAL:
4433                         stack->state = GUC_LOCAL;
4434                         break;
4435                 case GUC_ACTION_SAVE:
4436                         stack->state = GUC_SAVE;
4437                         break;
4438         }
4439         stack->source = gconf->source;
4440         stack->scontext = gconf->scontext;
4441         set_stack_value(gconf, &stack->prior);
4442
4443         gconf->stack = stack;
4444
4445         /* Ensure we remember to pop at end of xact */
4446         guc_dirty = true;
4447 }
4448
4449
4450 /*
4451  * Do GUC processing at main transaction start.
4452  */
4453 void
4454 AtStart_GUC(void)
4455 {
4456         /*
4457          * The nest level should be 0 between transactions; if it isn't, somebody
4458          * didn't call AtEOXact_GUC, or called it with the wrong nestLevel.  We
4459          * throw a warning but make no other effort to clean up.
4460          */
4461         if (GUCNestLevel != 0)
4462                 elog(WARNING, "GUC nest level = %d at transaction start",
4463                          GUCNestLevel);
4464         GUCNestLevel = 1;
4465 }
4466
4467 /*
4468  * Enter a new nesting level for GUC values.  This is called at subtransaction
4469  * start, and when entering a function that has proconfig settings, and in
4470  * some other places where we want to set GUC variables transiently.
4471  * NOTE we must not risk error here, else subtransaction start will be unhappy.
4472  */
4473 int
4474 NewGUCNestLevel(void)
4475 {
4476         return ++GUCNestLevel;
4477 }
4478
4479 /*
4480  * Do GUC processing at transaction or subtransaction commit or abort, or
4481  * when exiting a function that has proconfig settings, or when undoing a
4482  * transient assignment to some GUC variables.  (The name is thus a bit of
4483  * a misnomer; perhaps it should be ExitGUCNestLevel or some such.)
4484  * During abort, we discard all GUC settings that were applied at nesting
4485  * levels >= nestLevel.  nestLevel == 1 corresponds to the main transaction.
4486  */
4487 void
4488 AtEOXact_GUC(bool isCommit, int nestLevel)
4489 {
4490         bool            still_dirty;
4491         int                     i;
4492
4493         /*
4494          * Note: it's possible to get here with GUCNestLevel == nestLevel-1 during
4495          * abort, if there is a failure during transaction start before
4496          * AtStart_GUC is called.
4497          */
4498         Assert(nestLevel > 0 &&
4499                    (nestLevel <= GUCNestLevel ||
4500                         (nestLevel == GUCNestLevel + 1 && !isCommit)));
4501
4502         /* Quick exit if nothing's changed in this transaction */
4503         if (!guc_dirty)
4504         {
4505                 GUCNestLevel = nestLevel - 1;
4506                 return;
4507         }
4508
4509         still_dirty = false;
4510         for (i = 0; i < num_guc_variables; i++)
4511         {
4512                 struct config_generic *gconf = guc_variables[i];
4513                 GucStack   *stack;
4514
4515                 /*
4516                  * Process and pop each stack entry within the nest level. To simplify
4517                  * fmgr_security_definer() and other places that use GUC_ACTION_SAVE,
4518                  * we allow failure exit from code that uses a local nest level to be
4519                  * recovered at the surrounding transaction or subtransaction abort;
4520                  * so there could be more than one stack entry to pop.
4521                  */
4522                 while ((stack = gconf->stack) != NULL &&
4523                            stack->nest_level >= nestLevel)
4524                 {
4525                         GucStack   *prev = stack->prev;
4526                         bool            restorePrior = false;
4527                         bool            restoreMasked = false;
4528                         bool            changed;
4529
4530                         /*
4531                          * In this next bit, if we don't set either restorePrior or
4532                          * restoreMasked, we must "discard" any unwanted fields of the
4533                          * stack entries to avoid leaking memory.  If we do set one of
4534                          * those flags, unused fields will be cleaned up after restoring.
4535                          */
4536                         if (!isCommit)          /* if abort, always restore prior value */
4537                                 restorePrior = true;
4538                         else if (stack->state == GUC_SAVE)
4539                                 restorePrior = true;
4540                         else if (stack->nest_level == 1)
4541                         {
4542                                 /* transaction commit */
4543                                 if (stack->state == GUC_SET_LOCAL)
4544                                         restoreMasked = true;
4545                                 else if (stack->state == GUC_SET)
4546                                 {
4547                                         /* we keep the current active value */
4548                                         discard_stack_value(gconf, &stack->prior);
4549                                 }
4550                                 else    /* must be GUC_LOCAL */
4551                                         restorePrior = true;
4552                         }
4553                         else if (prev == NULL ||
4554                                          prev->nest_level < stack->nest_level - 1)
4555                         {
4556                                 /* decrement entry's level and do not pop it */
4557                                 stack->nest_level--;
4558                                 continue;
4559                         }
4560                         else
4561                         {
4562                                 /*
4563                                  * We have to merge this stack entry into prev. See README for
4564                                  * discussion of this bit.
4565                                  */
4566                                 switch (stack->state)
4567                                 {
4568                                         case GUC_SAVE:
4569                                                 Assert(false);  /* can't get here */
4570
4571                                         case GUC_SET:
4572                                                 /* next level always becomes SET */
4573                                                 discard_stack_value(gconf, &stack->prior);
4574                                                 if (prev->state == GUC_SET_LOCAL)
4575                                                         discard_stack_value(gconf, &prev->masked);
4576                                                 prev->state = GUC_SET;
4577                                                 break;
4578
4579                                         case GUC_LOCAL:
4580                                                 if (prev->state == GUC_SET)
4581                                                 {
4582                                                         /* LOCAL migrates down */
4583                                                         prev->masked_scontext = stack->scontext;
4584                                                         prev->masked = stack->prior;
4585                                                         prev->state = GUC_SET_LOCAL;
4586                                                 }
4587                                                 else
4588                                                 {
4589                                                         /* else just forget this stack level */
4590                                                         discard_stack_value(gconf, &stack->prior);
4591                                                 }
4592                                                 break;
4593
4594                                         case GUC_SET_LOCAL:
4595                                                 /* prior state at this level no longer wanted */
4596                                                 discard_stack_value(gconf, &stack->prior);
4597                                                 /* copy down the masked state */
4598                                                 prev->masked_scontext = stack->masked_scontext;
4599                                                 if (prev->state == GUC_SET_LOCAL)
4600                                                         discard_stack_value(gconf, &prev->masked);
4601                                                 prev->masked = stack->masked;
4602                                                 prev->state = GUC_SET_LOCAL;
4603                                                 break;
4604                                 }
4605                         }
4606
4607                         changed = false;
4608
4609                         if (restorePrior || restoreMasked)
4610                         {
4611                                 /* Perform appropriate restoration of the stacked value */
4612                                 config_var_value newvalue;
4613                                 GucSource       newsource;
4614                                 GucContext      newscontext;
4615
4616                                 if (restoreMasked)
4617                                 {
4618                                         newvalue = stack->masked;
4619                                         newsource = PGC_S_SESSION;
4620                                         newscontext = stack->masked_scontext;
4621                                 }
4622                                 else
4623                                 {
4624                                         newvalue = stack->prior;
4625                                         newsource = stack->source;
4626                                         newscontext = stack->scontext;
4627                                 }
4628
4629                                 switch (gconf->vartype)
4630                                 {
4631                                         case PGC_BOOL:
4632                                                 {
4633                                                         struct config_bool *conf = (struct config_bool *) gconf;
4634                                                         bool            newval = newvalue.val.boolval;
4635                                                         void       *newextra = newvalue.extra;
4636
4637                                                         if (*conf->variable != newval ||
4638                                                                 conf->gen.extra != newextra)
4639                                                         {
4640                                                                 if (conf->assign_hook)
4641                                                                         (*conf->assign_hook) (newval, newextra);
4642                                                                 *conf->variable = newval;
4643                                                                 set_extra_field(&conf->gen, &conf->gen.extra,
4644                                                                                                 newextra);
4645                                                                 changed = true;
4646                                                         }
4647                                                         break;
4648                                                 }
4649                                         case PGC_INT:
4650                                                 {
4651                                                         struct config_int *conf = (struct config_int *) gconf;
4652                                                         int                     newval = newvalue.val.intval;
4653                                                         void       *newextra = newvalue.extra;
4654
4655                                                         if (*conf->variable != newval ||
4656                                                                 conf->gen.extra != newextra)
4657                                                         {
4658                                                                 if (conf->assign_hook)
4659                                                                         (*conf->assign_hook) (newval, newextra);
4660                                                                 *conf->variable = newval;
4661                                                                 set_extra_field(&conf->gen, &conf->gen.extra,
4662                                                                                                 newextra);
4663                                                                 changed = true;
4664                                                         }
4665                                                         break;
4666                                                 }
4667                                         case PGC_REAL:
4668                                                 {
4669                                                         struct config_real *conf = (struct config_real *) gconf;
4670                                                         double          newval = newvalue.val.realval;
4671                                                         void       *newextra = newvalue.extra;
4672
4673                                                         if (*conf->variable != newval ||
4674                                                                 conf->gen.extra != newextra)
4675                                                         {
4676                                                                 if (conf->assign_hook)
4677                                                                         (*conf->assign_hook) (newval, newextra);
4678                                                                 *conf->variable = newval;
4679                                                                 set_extra_field(&conf->gen, &conf->gen.extra,
4680                                                                                                 newextra);
4681                                                                 changed = true;
4682                                                         }
4683                                                         break;
4684                                                 }
4685                                         case PGC_STRING:
4686                                                 {
4687                                                         struct config_string *conf = (struct config_string *) gconf;
4688                                                         char       *newval = newvalue.val.stringval;
4689                                                         void       *newextra = newvalue.extra;
4690
4691                                                         if (*conf->variable != newval ||
4692                                                                 conf->gen.extra != newextra)
4693                                                         {
4694                                                                 if (conf->assign_hook)
4695                                                                         (*conf->assign_hook) (newval, newextra);
4696                                                                 set_string_field(conf, conf->variable, newval);
4697                                                                 set_extra_field(&conf->gen, &conf->gen.extra,
4698                                                                                                 newextra);
4699                                                                 changed = true;
4700                                                         }
4701
4702                                                         /*
4703                                                          * Release stacked values if not used anymore. We
4704                                                          * could use discard_stack_value() here, but since
4705                                                          * we have type-specific code anyway, might as
4706                                                          * well inline it.
4707                                                          */
4708                                                         set_string_field(conf, &stack->prior.val.stringval, NULL);
4709                                                         set_string_field(conf, &stack->masked.val.stringval, NULL);
4710                                                         break;
4711                                                 }
4712                                         case PGC_ENUM:
4713                                                 {
4714                                                         struct config_enum *conf = (struct config_enum *) gconf;
4715                                                         int                     newval = newvalue.val.enumval;
4716                                                         void       *newextra = newvalue.extra;
4717
4718                                                         if (*conf->variable != newval ||
4719                                                                 conf->gen.extra != newextra)
4720                                                         {
4721                                                                 if (conf->assign_hook)
4722                                                                         (*conf->assign_hook) (newval, newextra);
4723                                                                 *conf->variable = newval;
4724                                                                 set_extra_field(&conf->gen, &conf->gen.extra,
4725                                                                                                 newextra);
4726                                                                 changed = true;
4727                                                         }
4728                                                         break;
4729                                                 }
4730                                 }
4731
4732                                 /*
4733                                  * Release stacked extra values if not used anymore.
4734                                  */
4735                                 set_extra_field(gconf, &(stack->prior.extra), NULL);
4736                                 set_extra_field(gconf, &(stack->masked.extra), NULL);
4737
4738                                 /* And restore source information */
4739                                 gconf->source = newsource;
4740                                 gconf->scontext = newscontext;
4741                         }
4742
4743                         /* Finish popping the state stack */
4744                         gconf->stack = prev;
4745                         pfree(stack);
4746
4747                         /* Report new value if we changed it */
4748                         if (changed && (gconf->flags & GUC_REPORT))
4749                                 ReportGUCOption(gconf);
4750                 }                                               /* end of stack-popping loop */
4751
4752                 if (stack != NULL)
4753                         still_dirty = true;
4754         }
4755
4756         /* If there are no remaining stack entries, we can reset guc_dirty */
4757         guc_dirty = still_dirty;
4758
4759         /* Update nesting level */
4760         GUCNestLevel = nestLevel - 1;
4761 }
4762
4763
4764 /*
4765  * Start up automatic reporting of changes to variables marked GUC_REPORT.
4766  * This is executed at completion of backend startup.
4767  */
4768 void
4769 BeginReportingGUCOptions(void)
4770 {
4771         int                     i;
4772
4773         /*
4774          * Don't do anything unless talking to an interactive frontend of protocol
4775          * 3.0 or later.
4776          */
4777         if (whereToSendOutput != DestRemote ||
4778                 PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
4779                 return;
4780
4781         reporting_enabled = true;
4782
4783         /* Transmit initial values of interesting variables */
4784         for (i = 0; i < num_guc_variables; i++)
4785         {
4786                 struct config_generic *conf = guc_variables[i];
4787
4788                 if (conf->flags & GUC_REPORT)
4789                         ReportGUCOption(conf);
4790         }
4791 }
4792
4793 /*
4794  * ReportGUCOption: if appropriate, transmit option value to frontend
4795  */
4796 static void
4797 ReportGUCOption(struct config_generic * record)
4798 {
4799         if (reporting_enabled && (record->flags & GUC_REPORT))
4800         {
4801                 char       *val = _ShowOption(record, false);
4802                 StringInfoData msgbuf;
4803
4804                 pq_beginmessage(&msgbuf, 'S');
4805                 pq_sendstring(&msgbuf, record->name);
4806                 pq_sendstring(&msgbuf, val);
4807                 pq_endmessage(&msgbuf);
4808
4809                 pfree(val);
4810         }
4811 }
4812
4813 /*
4814  * Try to parse value as an integer.  The accepted formats are the
4815  * usual decimal, octal, or hexadecimal formats, optionally followed by
4816  * a unit name if "flags" indicates a unit is allowed.
4817  *
4818  * If the string parses okay, return true, else false.
4819  * If okay and result is not NULL, return the value in *result.
4820  * If not okay and hintmsg is not NULL, *hintmsg is set to a suitable
4821  *      HINT message, or NULL if no hint provided.
4822  */
4823 bool
4824 parse_int(const char *value, int *result, int flags, const char **hintmsg)
4825 {
4826         int64           val;
4827         char       *endptr;
4828
4829         /* To suppress compiler warnings, always set output params */
4830         if (result)
4831                 *result = 0;
4832         if (hintmsg)
4833                 *hintmsg = NULL;
4834
4835         /* We assume here that int64 is at least as wide as long */
4836         errno = 0;
4837         val = strtol(value, &endptr, 0);
4838
4839         if (endptr == value)
4840                 return false;                   /* no HINT for integer syntax error */
4841
4842         if (errno == ERANGE || val != (int64) ((int32) val))
4843         {
4844                 if (hintmsg)
4845                         *hintmsg = gettext_noop("Value exceeds integer range.");
4846                 return false;
4847         }
4848
4849         /* allow whitespace between integer and unit */
4850         while (isspace((unsigned char) *endptr))
4851                 endptr++;
4852
4853         /* Handle possible unit */
4854         if (*endptr != '\0')
4855         {
4856                 /*
4857                  * Note: the multiple-switch coding technique here is a bit tedious,
4858                  * but seems necessary to avoid intermediate-value overflows.
4859                  */
4860                 if (flags & GUC_UNIT_MEMORY)
4861                 {
4862                         /* Set hint for use if no match or trailing garbage */
4863                         if (hintmsg)
4864                                 *hintmsg = gettext_noop("Valid units for this parameter are \"kB\", \"MB\", and \"GB\".");
4865
4866 #if BLCKSZ < 1024 || BLCKSZ > (1024*1024)
4867 #error BLCKSZ must be between 1KB and 1MB
4868 #endif
4869 #if XLOG_BLCKSZ < 1024 || XLOG_BLCKSZ > (1024*1024)
4870 #error XLOG_BLCKSZ must be between 1KB and 1MB
4871 #endif
4872
4873                         if (strncmp(endptr, "kB", 2) == 0)
4874                         {
4875                                 endptr += 2;
4876                                 switch (flags & GUC_UNIT_MEMORY)
4877                                 {
4878                                         case GUC_UNIT_BLOCKS:
4879                                                 val /= (BLCKSZ / 1024);
4880                                                 break;
4881                                         case GUC_UNIT_XBLOCKS:
4882                                                 val /= (XLOG_BLCKSZ / 1024);
4883                                                 break;
4884                                 }
4885                         }
4886                         else if (strncmp(endptr, "MB", 2) == 0)
4887                         {
4888                                 endptr += 2;
4889                                 switch (flags & GUC_UNIT_MEMORY)
4890                                 {
4891                                         case GUC_UNIT_KB:
4892                                                 val *= KB_PER_MB;
4893                                                 break;
4894                                         case GUC_UNIT_BLOCKS:
4895                                                 val *= KB_PER_MB / (BLCKSZ / 1024);
4896                                                 break;
4897                                         case GUC_UNIT_XBLOCKS:
4898                                                 val *= KB_PER_MB / (XLOG_BLCKSZ / 1024);
4899                                                 break;
4900                                 }
4901                         }
4902                         else if (strncmp(endptr, "GB", 2) == 0)
4903                         {
4904                                 endptr += 2;
4905                                 switch (flags & GUC_UNIT_MEMORY)
4906                                 {
4907                                         case GUC_UNIT_KB:
4908                                                 val *= KB_PER_GB;
4909                                                 break;
4910                                         case GUC_UNIT_BLOCKS:
4911                                                 val *= KB_PER_GB / (BLCKSZ / 1024);
4912                                                 break;
4913                                         case GUC_UNIT_XBLOCKS:
4914                                                 val *= KB_PER_GB / (XLOG_BLCKSZ / 1024);
4915                                                 break;
4916                                 }
4917                         }
4918                 }
4919                 else if (flags & GUC_UNIT_TIME)
4920                 {
4921                         /* Set hint for use if no match or trailing garbage */
4922                         if (hintmsg)
4923                                 *hintmsg = gettext_noop("Valid units for this parameter are \"ms\", \"s\", \"min\", \"h\", and \"d\".");
4924
4925                         if (strncmp(endptr, "ms", 2) == 0)
4926                         {
4927                                 endptr += 2;
4928                                 switch (flags & GUC_UNIT_TIME)
4929                                 {
4930                                         case GUC_UNIT_S:
4931                                                 val /= MS_PER_S;
4932                                                 break;
4933                                         case GUC_UNIT_MIN:
4934                                                 val /= MS_PER_MIN;
4935                                                 break;
4936                                 }
4937                         }
4938                         else if (strncmp(endptr, "s", 1) == 0)
4939                         {
4940                                 endptr += 1;
4941                                 switch (flags & GUC_UNIT_TIME)
4942                                 {
4943                                         case GUC_UNIT_MS:
4944                                                 val *= MS_PER_S;
4945                                                 break;
4946                                         case GUC_UNIT_MIN:
4947                                                 val /= S_PER_MIN;
4948                                                 break;
4949                                 }
4950                         }
4951                         else if (strncmp(endptr, "min", 3) == 0)
4952                         {
4953                                 endptr += 3;
4954                                 switch (flags & GUC_UNIT_TIME)
4955                                 {
4956                                         case GUC_UNIT_MS:
4957                                                 val *= MS_PER_MIN;
4958                                                 break;
4959                                         case GUC_UNIT_S:
4960                                                 val *= S_PER_MIN;
4961                                                 break;
4962                                 }
4963                         }
4964                         else if (strncmp(endptr, "h", 1) == 0)
4965                         {
4966                                 endptr += 1;
4967                                 switch (flags & GUC_UNIT_TIME)
4968                                 {
4969                                         case GUC_UNIT_MS:
4970                                                 val *= MS_PER_H;
4971                                                 break;
4972                                         case GUC_UNIT_S:
4973                                                 val *= S_PER_H;
4974                                                 break;
4975                                         case GUC_UNIT_MIN:
4976                                                 val *= MIN_PER_H;
4977                                                 break;
4978                                 }
4979                         }
4980                         else if (strncmp(endptr, "d", 1) == 0)
4981                         {
4982                                 endptr += 1;
4983                                 switch (flags & GUC_UNIT_TIME)
4984                                 {
4985                                         case GUC_UNIT_MS:
4986                                                 val *= MS_PER_D;
4987                                                 break;
4988                                         case GUC_UNIT_S:
4989                                                 val *= S_PER_D;
4990                                                 break;
4991                                         case GUC_UNIT_MIN:
4992                                                 val *= MIN_PER_D;
4993                                                 break;
4994                                 }
4995                         }
4996                 }
4997
4998                 /* allow whitespace after unit */
4999                 while (isspace((unsigned char) *endptr))
5000                         endptr++;
5001
5002                 if (*endptr != '\0')
5003                         return false;           /* appropriate hint, if any, already set */
5004
5005                 /* Check for overflow due to units conversion */
5006                 if (val != (int64) ((int32) val))
5007                 {
5008                         if (hintmsg)
5009                                 *hintmsg = gettext_noop("Value exceeds integer range.");
5010                         return false;
5011                 }
5012         }
5013
5014         if (result)
5015                 *result = (int) val;
5016         return true;
5017 }
5018
5019
5020
5021 /*
5022  * Try to parse value as a floating point number in the usual format.
5023  * If the string parses okay, return true, else false.
5024  * If okay and result is not NULL, return the value in *result.
5025  */
5026 bool
5027 parse_real(const char *value, double *result)
5028 {
5029         double          val;
5030         char       *endptr;
5031
5032         if (result)
5033                 *result = 0;                    /* suppress compiler warning */
5034
5035         errno = 0;
5036         val = strtod(value, &endptr);
5037         if (endptr == value || errno == ERANGE)
5038                 return false;
5039
5040         /* allow whitespace after number */
5041         while (isspace((unsigned char) *endptr))
5042                 endptr++;
5043         if (*endptr != '\0')
5044                 return false;
5045
5046         if (result)
5047                 *result = val;
5048         return true;
5049 }
5050
5051
5052 /*
5053  * Lookup the name for an enum option with the selected value.
5054  * Should only ever be called with known-valid values, so throws
5055  * an elog(ERROR) if the enum option is not found.
5056  *
5057  * The returned string is a pointer to static data and not
5058  * allocated for modification.
5059  */
5060 const char *
5061 config_enum_lookup_by_value(struct config_enum * record, int val)
5062 {
5063         const struct config_enum_entry *entry;
5064
5065         for (entry = record->options; entry && entry->name; entry++)
5066         {
5067                 if (entry->val == val)
5068                         return entry->name;
5069         }
5070
5071         elog(ERROR, "could not find enum option %d for %s",
5072                  val, record->gen.name);
5073         return NULL;                            /* silence compiler */
5074 }
5075
5076
5077 /*
5078  * Lookup the value for an enum option with the selected name
5079  * (case-insensitive).
5080  * If the enum option is found, sets the retval value and returns
5081  * true. If it's not found, return FALSE and retval is set to 0.
5082  */
5083 bool
5084 config_enum_lookup_by_name(struct config_enum * record, const char *value,
5085                                                    int *retval)
5086 {
5087         const struct config_enum_entry *entry;
5088
5089         for (entry = record->options; entry && entry->name; entry++)
5090         {
5091                 if (pg_strcasecmp(value, entry->name) == 0)
5092                 {
5093                         *retval = entry->val;
5094                         return TRUE;
5095                 }
5096         }
5097
5098         *retval = 0;
5099         return FALSE;
5100 }
5101
5102
5103 /*
5104  * Return a list of all available options for an enum, excluding
5105  * hidden ones, separated by the given separator.
5106  * If prefix is non-NULL, it is added before the first enum value.
5107  * If suffix is non-NULL, it is added to the end of the string.
5108  */
5109 static char *
5110 config_enum_get_options(struct config_enum * record, const char *prefix,
5111                                                 const char *suffix, const char *separator)
5112 {
5113         const struct config_enum_entry *entry;
5114         StringInfoData retstr;
5115         int                     seplen;
5116
5117         initStringInfo(&retstr);
5118         appendStringInfoString(&retstr, prefix);
5119
5120         seplen = strlen(separator);
5121         for (entry = record->options; entry && entry->name; entry++)
5122         {
5123                 if (!entry->hidden)
5124                 {
5125                         appendStringInfoString(&retstr, entry->name);
5126                         appendBinaryStringInfo(&retstr, separator, seplen);
5127                 }
5128         }
5129
5130         /*
5131          * All the entries may have been hidden, leaving the string empty if no
5132          * prefix was given. This indicates a broken GUC setup, since there is no
5133          * use for an enum without any values, so we just check to make sure we
5134          * don't write to invalid memory instead of actually trying to do
5135          * something smart with it.
5136          */
5137         if (retstr.len >= seplen)
5138         {
5139                 /* Replace final separator */
5140                 retstr.data[retstr.len - seplen] = '\0';
5141                 retstr.len -= seplen;
5142         }
5143
5144         appendStringInfoString(&retstr, suffix);
5145
5146         return retstr.data;
5147 }
5148
5149
5150 /*
5151  * Sets option `name' to given value.
5152  *
5153  * The value should be a string, which will be parsed and converted to
5154  * the appropriate data type.  The context and source parameters indicate
5155  * in which context this function is being called, so that it can apply the
5156  * access restrictions properly.
5157  *
5158  * If value is NULL, set the option to its default value (normally the
5159  * reset_val, but if source == PGC_S_DEFAULT we instead use the boot_val).
5160  *
5161  * action indicates whether to set the value globally in the session, locally
5162  * to the current top transaction, or just for the duration of a function call.
5163  *
5164  * If changeVal is false then don't really set the option but do all
5165  * the checks to see if it would work.
5166  *
5167  * elevel should normally be passed as zero, allowing this function to make
5168  * its standard choice of ereport level.  However some callers need to be
5169  * able to override that choice; they should pass the ereport level to use.
5170  *
5171  * Return value:
5172  *      +1: the value is valid and was successfully applied.
5173  *      0:      the name or value is invalid (but see below).
5174  *      -1: the value was not applied because of context, priority, or changeVal.
5175  *
5176  * If there is an error (non-existing option, invalid value) then an
5177  * ereport(ERROR) is thrown *unless* this is called for a source for which
5178  * we don't want an ERROR (currently, those are defaults, the config file,
5179  * and per-database or per-user settings, as well as callers who specify
5180  * a less-than-ERROR elevel).  In those cases we write a suitable error
5181  * message via ereport() and return 0.
5182  *
5183  * See also SetConfigOption for an external interface.
5184  */
5185 int
5186 set_config_option(const char *name, const char *value,
5187                                   GucContext context, GucSource source,
5188                                   GucAction action, bool changeVal, int elevel)
5189 {
5190         struct config_generic *record;
5191         bool            prohibitValueChange = false;
5192         bool            makeDefault;
5193
5194         if (elevel == 0)
5195         {
5196                 if (source == PGC_S_DEFAULT || source == PGC_S_FILE)
5197                 {
5198                         /*
5199                          * To avoid cluttering the log, only the postmaster bleats loudly
5200                          * about problems with the config file.
5201                          */
5202                         elevel = IsUnderPostmaster ? DEBUG3 : LOG;
5203                 }
5204                 else if (source == PGC_S_GLOBAL || source == PGC_S_DATABASE || source == PGC_S_USER ||
5205                                  source == PGC_S_DATABASE_USER)
5206                         elevel = WARNING;
5207                 else
5208                         elevel = ERROR;
5209         }
5210
5211         record = find_option(name, true, elevel);
5212         if (record == NULL)
5213         {
5214                 ereport(elevel,
5215                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
5216                            errmsg("unrecognized configuration parameter \"%s\"", name)));
5217                 return 0;
5218         }
5219
5220         /*
5221          * Check if the option can be set at this time. See guc.h for the precise
5222          * rules.
5223          */
5224         switch (record->context)
5225         {
5226                 case PGC_INTERNAL:
5227                         if (context != PGC_INTERNAL)
5228                         {
5229                                 ereport(elevel,
5230                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5231                                                  errmsg("parameter \"%s\" cannot be changed",
5232                                                                 name)));
5233                                 return 0;
5234                         }
5235                         break;
5236                 case PGC_POSTMASTER:
5237                         if (context == PGC_SIGHUP)
5238                         {
5239                                 /*
5240                                  * We are re-reading a PGC_POSTMASTER variable from
5241                                  * postgresql.conf.  We can't change the setting, so we should
5242                                  * give a warning if the DBA tries to change it.  However,
5243                                  * because of variant formats, canonicalization by check
5244                                  * hooks, etc, we can't just compare the given string directly
5245                                  * to what's stored.  Set a flag to check below after we have
5246                                  * the final storable value.
5247                                  */
5248                                 prohibitValueChange = true;
5249                         }
5250                         else if (context != PGC_POSTMASTER)
5251                         {
5252                                 ereport(elevel,
5253                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5254                                                  errmsg("parameter \"%s\" cannot be changed without restarting the server",
5255                                                                 name)));
5256                                 return 0;
5257                         }
5258                         break;
5259                 case PGC_SIGHUP:
5260                         if (context != PGC_SIGHUP && context != PGC_POSTMASTER)
5261                         {
5262                                 ereport(elevel,
5263                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5264                                                  errmsg("parameter \"%s\" cannot be changed now",
5265                                                                 name)));
5266                                 return 0;
5267                         }
5268
5269                         /*
5270                          * Hmm, the idea of the SIGHUP context is "ought to be global, but
5271                          * can be changed after postmaster start". But there's nothing
5272                          * that prevents a crafty administrator from sending SIGHUP
5273                          * signals to individual backends only.
5274                          */
5275                         break;
5276                 case PGC_BACKEND:
5277                         if (context == PGC_SIGHUP)
5278                         {
5279                                 /*
5280                                  * If a PGC_BACKEND parameter is changed in the config file,
5281                                  * we want to accept the new value in the postmaster (whence
5282                                  * it will propagate to subsequently-started backends), but
5283                                  * ignore it in existing backends.      This is a tad klugy, but
5284                                  * necessary because we don't re-read the config file during
5285                                  * backend start.
5286                                  */
5287                                 if (IsUnderPostmaster)
5288                                         return -1;
5289                         }
5290                         else if (context != PGC_POSTMASTER && context != PGC_BACKEND &&
5291                                          source != PGC_S_CLIENT)
5292                         {
5293                                 ereport(elevel,
5294                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5295                                                  errmsg("parameter \"%s\" cannot be set after connection start",
5296                                                                 name)));
5297                                 return 0;
5298                         }
5299                         break;
5300                 case PGC_SUSET:
5301                         if (context == PGC_USERSET || context == PGC_BACKEND)
5302                         {
5303                                 ereport(elevel,
5304                                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5305                                                  errmsg("permission denied to set parameter \"%s\"",
5306                                                                 name)));
5307                                 return 0;
5308                         }
5309                         break;
5310                 case PGC_USERSET:
5311                         /* always okay */
5312                         break;
5313         }
5314
5315         /*
5316          * Disallow changing GUC_NOT_WHILE_SEC_REST values if we are inside a
5317          * security restriction context.  We can reject this regardless of the GUC
5318          * context or source, mainly because sources that it might be reasonable
5319          * to override for won't be seen while inside a function.
5320          *
5321          * Note: variables marked GUC_NOT_WHILE_SEC_REST should usually be marked
5322          * GUC_NO_RESET_ALL as well, because ResetAllOptions() doesn't check this.
5323          * An exception might be made if the reset value is assumed to be "safe".
5324          *
5325          * Note: this flag is currently used for "session_authorization" and
5326          * "role".      We need to prohibit changing these inside a local userid
5327          * context because when we exit it, GUC won't be notified, leaving things
5328          * out of sync.  (This could be fixed by forcing a new GUC nesting level,
5329          * but that would change behavior in possibly-undesirable ways.)  Also, we
5330          * prohibit changing these in a security-restricted operation because
5331          * otherwise RESET could be used to regain the session user's privileges.
5332          */
5333         if (record->flags & GUC_NOT_WHILE_SEC_REST)
5334         {
5335                 if (InLocalUserIdChange())
5336                 {
5337                         /*
5338                          * Phrasing of this error message is historical, but it's the most
5339                          * common case.
5340                          */
5341                         ereport(elevel,
5342                                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5343                                          errmsg("cannot set parameter \"%s\" within security-definer function",
5344                                                         name)));
5345                         return 0;
5346                 }
5347                 if (InSecurityRestrictedOperation())
5348                 {
5349                         ereport(elevel,
5350                                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5351                                          errmsg("cannot set parameter \"%s\" within security-restricted operation",
5352                                                         name)));
5353                         return 0;
5354                 }
5355         }
5356
5357         /*
5358          * Should we set reset/stacked values?  (If so, the behavior is not
5359          * transactional.)      This is done either when we get a default value from
5360          * the database's/user's/client's default settings or when we reset a
5361          * value to its default.
5362          */
5363         makeDefault = changeVal && (source <= PGC_S_OVERRIDE) &&
5364                 ((value != NULL) || source == PGC_S_DEFAULT);
5365
5366         /*
5367          * Ignore attempted set if overridden by previously processed setting.
5368          * However, if changeVal is false then plow ahead anyway since we are
5369          * trying to find out if the value is potentially good, not actually use
5370          * it. Also keep going if makeDefault is true, since we may want to set
5371          * the reset/stacked values even if we can't set the variable itself.
5372          */
5373         if (record->source > source)
5374         {
5375                 if (changeVal && !makeDefault)
5376                 {
5377                         elog(DEBUG3, "\"%s\": setting ignored because previous source is higher priority",
5378                                  name);
5379                         return -1;
5380                 }
5381                 changeVal = false;
5382         }
5383
5384         /*
5385          * Evaluate value and set variable.
5386          */
5387         switch (record->vartype)
5388         {
5389                 case PGC_BOOL:
5390                         {
5391                                 struct config_bool *conf = (struct config_bool *) record;
5392                                 bool            newval;
5393                                 void       *newextra = NULL;
5394
5395                                 if (value)
5396                                 {
5397                                         if (!parse_bool(value, &newval))
5398                                         {
5399                                                 ereport(elevel,
5400                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5401                                                   errmsg("parameter \"%s\" requires a Boolean value",
5402                                                                  name)));
5403                                                 return 0;
5404                                         }
5405                                         if (!call_bool_check_hook(conf, &newval, &newextra,
5406                                                                                           source, elevel))
5407                                                 return 0;
5408                                 }
5409                                 else if (source == PGC_S_DEFAULT)
5410                                 {
5411                                         newval = conf->boot_val;
5412                                         if (!call_bool_check_hook(conf, &newval, &newextra,
5413                                                                                           source, elevel))
5414                                                 return 0;
5415                                 }
5416                                 else
5417                                 {
5418                                         newval = conf->reset_val;
5419                                         newextra = conf->reset_extra;
5420                                         source = conf->gen.reset_source;
5421                                         context = conf->gen.reset_scontext;
5422                                 }
5423
5424                                 if (prohibitValueChange)
5425                                 {
5426                                         if (*conf->variable != newval)
5427                                         {
5428                                                 ereport(elevel,
5429                                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5430                                                                  errmsg("parameter \"%s\" cannot be changed without restarting the server",
5431                                                                                 name)));
5432                                                 return 0;
5433                                         }
5434                                         return -1;
5435                                 }
5436
5437                                 if (changeVal)
5438                                 {
5439                                         /* Save old value to support transaction abort */
5440                                         if (!makeDefault)
5441                                                 push_old_value(&conf->gen, action);
5442
5443                                         if (conf->assign_hook)
5444                                                 (*conf->assign_hook) (newval, newextra);
5445                                         *conf->variable = newval;
5446                                         set_extra_field(&conf->gen, &conf->gen.extra,
5447                                                                         newextra);
5448                                         conf->gen.source = source;
5449                                         conf->gen.scontext = context;
5450                                 }
5451                                 if (makeDefault)
5452                                 {
5453                                         GucStack   *stack;
5454
5455                                         if (conf->gen.reset_source <= source)
5456                                         {
5457                                                 conf->reset_val = newval;
5458                                                 set_extra_field(&conf->gen, &conf->reset_extra,
5459                                                                                 newextra);
5460                                                 conf->gen.reset_source = source;
5461                                                 conf->gen.reset_scontext = context;
5462                                         }
5463                                         for (stack = conf->gen.stack; stack; stack = stack->prev)
5464                                         {
5465                                                 if (stack->source <= source)
5466                                                 {
5467                                                         stack->prior.val.boolval = newval;
5468                                                         set_extra_field(&conf->gen, &stack->prior.extra,
5469                                                                                         newextra);
5470                                                         stack->source = source;
5471                                                         stack->scontext = context;
5472                                                 }
5473                                         }
5474                                 }
5475
5476                                 /* Perhaps we didn't install newextra anywhere */
5477                                 if (newextra && !extra_field_used(&conf->gen, newextra))
5478                                         free(newextra);
5479                                 break;
5480                         }
5481
5482                 case PGC_INT:
5483                         {
5484                                 struct config_int *conf = (struct config_int *) record;
5485                                 int                     newval;
5486                                 void       *newextra = NULL;
5487
5488                                 if (value)
5489                                 {
5490                                         const char *hintmsg;
5491
5492                                         if (!parse_int(value, &newval, conf->gen.flags, &hintmsg))
5493                                         {
5494                                                 ereport(elevel,
5495                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5496                                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
5497                                                                 name, value),
5498                                                                  hintmsg ? errhint("%s", _(hintmsg)) : 0));
5499                                                 return 0;
5500                                         }
5501                                         if (newval < conf->min || newval > conf->max)
5502                                         {
5503                                                 ereport(elevel,
5504                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5505                                                                  errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)",
5506                                                                                 newval, name, conf->min, conf->max)));
5507                                                 return 0;
5508                                         }
5509                                         if (!call_int_check_hook(conf, &newval, &newextra,
5510                                                                                          source, elevel))
5511                                                 return 0;
5512                                 }
5513                                 else if (source == PGC_S_DEFAULT)
5514                                 {
5515                                         newval = conf->boot_val;
5516                                         if (!call_int_check_hook(conf, &newval, &newextra,
5517                                                                                          source, elevel))
5518                                                 return 0;
5519                                 }
5520                                 else
5521                                 {
5522                                         newval = conf->reset_val;
5523                                         newextra = conf->reset_extra;
5524                                         source = conf->gen.reset_source;
5525                                         context = conf->gen.reset_scontext;
5526                                 }
5527
5528                                 if (prohibitValueChange)
5529                                 {
5530                                         if (*conf->variable != newval)
5531                                         {
5532                                                 ereport(elevel,
5533                                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5534                                                                  errmsg("parameter \"%s\" cannot be changed without restarting the server",
5535                                                                                 name)));
5536                                                 return 0;
5537                                         }
5538                                         return -1;
5539                                 }
5540
5541                                 if (changeVal)
5542                                 {
5543                                         /* Save old value to support transaction abort */
5544                                         if (!makeDefault)
5545                                                 push_old_value(&conf->gen, action);
5546
5547                                         if (conf->assign_hook)
5548                                                 (*conf->assign_hook) (newval, newextra);
5549                                         *conf->variable = newval;
5550                                         set_extra_field(&conf->gen, &conf->gen.extra,
5551                                                                         newextra);
5552                                         conf->gen.source = source;
5553                                         conf->gen.scontext = context;
5554                                 }
5555                                 if (makeDefault)
5556                                 {
5557                                         GucStack   *stack;
5558
5559                                         if (conf->gen.reset_source <= source)
5560                                         {
5561                                                 conf->reset_val = newval;
5562                                                 set_extra_field(&conf->gen, &conf->reset_extra,
5563                                                                                 newextra);
5564                                                 conf->gen.reset_source = source;
5565                                                 conf->gen.reset_scontext = context;
5566                                         }
5567                                         for (stack = conf->gen.stack; stack; stack = stack->prev)
5568                                         {
5569                                                 if (stack->source <= source)
5570                                                 {
5571                                                         stack->prior.val.intval = newval;
5572                                                         set_extra_field(&conf->gen, &stack->prior.extra,
5573                                                                                         newextra);
5574                                                         stack->source = source;
5575                                                         stack->scontext = context;
5576                                                 }
5577                                         }
5578                                 }
5579
5580                                 /* Perhaps we didn't install newextra anywhere */
5581                                 if (newextra && !extra_field_used(&conf->gen, newextra))
5582                                         free(newextra);
5583                                 break;
5584                         }
5585
5586                 case PGC_REAL:
5587                         {
5588                                 struct config_real *conf = (struct config_real *) record;
5589                                 double          newval;
5590                                 void       *newextra = NULL;
5591
5592                                 if (value)
5593                                 {
5594                                         if (!parse_real(value, &newval))
5595                                         {
5596                                                 ereport(elevel,
5597                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5598                                                   errmsg("parameter \"%s\" requires a numeric value",
5599                                                                  name)));
5600                                                 return 0;
5601                                         }
5602                                         if (newval < conf->min || newval > conf->max)
5603                                         {
5604                                                 ereport(elevel,
5605                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5606                                                                  errmsg("%g is outside the valid range for parameter \"%s\" (%g .. %g)",
5607                                                                                 newval, name, conf->min, conf->max)));
5608                                                 return 0;
5609                                         }
5610                                         if (!call_real_check_hook(conf, &newval, &newextra,
5611                                                                                           source, elevel))
5612                                                 return 0;
5613                                 }
5614                                 else if (source == PGC_S_DEFAULT)
5615                                 {
5616                                         newval = conf->boot_val;
5617                                         if (!call_real_check_hook(conf, &newval, &newextra,
5618                                                                                           source, elevel))
5619                                                 return 0;
5620                                 }
5621                                 else
5622                                 {
5623                                         newval = conf->reset_val;
5624                                         newextra = conf->reset_extra;
5625                                         source = conf->gen.reset_source;
5626                                         context = conf->gen.reset_scontext;
5627                                 }
5628
5629                                 if (prohibitValueChange)
5630                                 {
5631                                         if (*conf->variable != newval)
5632                                         {
5633                                                 ereport(elevel,
5634                                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5635                                                                  errmsg("parameter \"%s\" cannot be changed without restarting the server",
5636                                                                                 name)));
5637                                                 return 0;
5638                                         }
5639                                         return -1;
5640                                 }
5641
5642                                 if (changeVal)
5643                                 {
5644                                         /* Save old value to support transaction abort */
5645                                         if (!makeDefault)
5646                                                 push_old_value(&conf->gen, action);
5647
5648                                         if (conf->assign_hook)
5649                                                 (*conf->assign_hook) (newval, newextra);
5650                                         *conf->variable = newval;
5651                                         set_extra_field(&conf->gen, &conf->gen.extra,
5652                                                                         newextra);
5653                                         conf->gen.source = source;
5654                                         conf->gen.scontext = context;
5655                                 }
5656                                 if (makeDefault)
5657                                 {
5658                                         GucStack   *stack;
5659
5660                                         if (conf->gen.reset_source <= source)
5661                                         {
5662                                                 conf->reset_val = newval;
5663                                                 set_extra_field(&conf->gen, &conf->reset_extra,
5664                                                                                 newextra);
5665                                                 conf->gen.reset_source = source;
5666                                                 conf->gen.reset_scontext = context;
5667                                         }
5668                                         for (stack = conf->gen.stack; stack; stack = stack->prev)
5669                                         {
5670                                                 if (stack->source <= source)
5671                                                 {
5672                                                         stack->prior.val.realval = newval;
5673                                                         set_extra_field(&conf->gen, &stack->prior.extra,
5674                                                                                         newextra);
5675                                                         stack->source = source;
5676                                                         stack->scontext = context;
5677                                                 }
5678                                         }
5679                                 }
5680
5681                                 /* Perhaps we didn't install newextra anywhere */
5682                                 if (newextra && !extra_field_used(&conf->gen, newextra))
5683                                         free(newextra);
5684                                 break;
5685                         }
5686
5687                 case PGC_STRING:
5688                         {
5689                                 struct config_string *conf = (struct config_string *) record;
5690                                 char       *newval;
5691                                 void       *newextra = NULL;
5692
5693                                 if (value)
5694                                 {
5695                                         /*
5696                                          * The value passed by the caller could be transient, so
5697                                          * we always strdup it.
5698                                          */
5699                                         newval = guc_strdup(elevel, value);
5700                                         if (newval == NULL)
5701                                                 return 0;
5702
5703                                         /*
5704                                          * The only built-in "parsing" check we have is to apply
5705                                          * truncation if GUC_IS_NAME.
5706                                          */
5707                                         if (conf->gen.flags & GUC_IS_NAME)
5708                                                 truncate_identifier(newval, strlen(newval), true);
5709
5710                                         if (!call_string_check_hook(conf, &newval, &newextra,
5711                                                                                                 source, elevel))
5712                                         {
5713                                                 free(newval);
5714                                                 return 0;
5715                                         }
5716                                 }
5717                                 else if (source == PGC_S_DEFAULT)
5718                                 {
5719                                         /* non-NULL boot_val must always get strdup'd */
5720                                         if (conf->boot_val != NULL)
5721                                         {
5722                                                 newval = guc_strdup(elevel, conf->boot_val);
5723                                                 if (newval == NULL)
5724                                                         return 0;
5725                                         }
5726                                         else
5727                                                 newval = NULL;
5728
5729                                         if (!call_string_check_hook(conf, &newval, &newextra,
5730                                                                                                 source, elevel))
5731                                         {
5732                                                 free(newval);
5733                                                 return 0;
5734                                         }
5735                                 }
5736                                 else
5737                                 {
5738                                         /*
5739                                          * strdup not needed, since reset_val is already under
5740                                          * guc.c's control
5741                                          */
5742                                         newval = conf->reset_val;
5743                                         newextra = conf->reset_extra;
5744                                         source = conf->gen.reset_source;
5745                                         context = conf->gen.reset_scontext;
5746                                 }
5747
5748                                 if (prohibitValueChange)
5749                                 {
5750                                         /* newval shouldn't be NULL, so we're a bit sloppy here */
5751                                         if (*conf->variable == NULL || newval == NULL ||
5752                                                 strcmp(*conf->variable, newval) != 0)
5753                                         {
5754                                                 ereport(elevel,
5755                                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5756                                                                  errmsg("parameter \"%s\" cannot be changed without restarting the server",
5757                                                                                 name)));
5758                                                 return 0;
5759                                         }
5760                                         return -1;
5761                                 }
5762
5763                                 if (changeVal)
5764                                 {
5765                                         /* Save old value to support transaction abort */
5766                                         if (!makeDefault)
5767                                                 push_old_value(&conf->gen, action);
5768
5769                                         if (conf->assign_hook)
5770                                                 (*conf->assign_hook) (newval, newextra);
5771                                         set_string_field(conf, conf->variable, newval);
5772                                         set_extra_field(&conf->gen, &conf->gen.extra,
5773                                                                         newextra);
5774                                         conf->gen.source = source;
5775                                         conf->gen.scontext = context;
5776                                 }
5777
5778                                 if (makeDefault)
5779                                 {
5780                                         GucStack   *stack;
5781
5782                                         if (conf->gen.reset_source <= source)
5783                                         {
5784                                                 set_string_field(conf, &conf->reset_val, newval);
5785                                                 set_extra_field(&conf->gen, &conf->reset_extra,
5786                                                                                 newextra);
5787                                                 conf->gen.reset_source = source;
5788                                                 conf->gen.reset_scontext = context;
5789                                         }
5790                                         for (stack = conf->gen.stack; stack; stack = stack->prev)
5791                                         {
5792                                                 if (stack->source <= source)
5793                                                 {
5794                                                         set_string_field(conf, &stack->prior.val.stringval,
5795                                                                                          newval);
5796                                                         set_extra_field(&conf->gen, &stack->prior.extra,
5797                                                                                         newextra);
5798                                                         stack->source = source;
5799                                                         stack->scontext = context;
5800                                                 }
5801                                         }
5802                                 }
5803
5804                                 /* Perhaps we didn't install newval anywhere */
5805                                 if (newval && !string_field_used(conf, newval))
5806                                         free(newval);
5807                                 /* Perhaps we didn't install newextra anywhere */
5808                                 if (newextra && !extra_field_used(&conf->gen, newextra))
5809                                         free(newextra);
5810                                 break;
5811                         }
5812
5813                 case PGC_ENUM:
5814                         {
5815                                 struct config_enum *conf = (struct config_enum *) record;
5816                                 int                     newval;
5817                                 void       *newextra = NULL;
5818
5819                                 if (value)
5820                                 {
5821                                         if (!config_enum_lookup_by_name(conf, value, &newval))
5822                                         {
5823                                                 char       *hintmsg;
5824
5825                                                 hintmsg = config_enum_get_options(conf,
5826                                                                                                                 "Available values: ",
5827                                                                                                                   ".", ", ");
5828
5829                                                 ereport(elevel,
5830                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5831                                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
5832                                                                 name, value),
5833                                                                  hintmsg ? errhint("%s", _(hintmsg)) : 0));
5834
5835                                                 if (hintmsg)
5836                                                         pfree(hintmsg);
5837                                                 return 0;
5838                                         }
5839                                         if (!call_enum_check_hook(conf, &newval, &newextra,
5840                                                                                           source, elevel))
5841                                                 return 0;
5842                                 }
5843                                 else if (source == PGC_S_DEFAULT)
5844                                 {
5845                                         newval = conf->boot_val;
5846                                         if (!call_enum_check_hook(conf, &newval, &newextra,
5847                                                                                           source, elevel))
5848                                                 return 0;
5849                                 }
5850                                 else
5851                                 {
5852                                         newval = conf->reset_val;
5853                                         newextra = conf->reset_extra;
5854                                         source = conf->gen.reset_source;
5855                                         context = conf->gen.reset_scontext;
5856                                 }
5857
5858                                 if (prohibitValueChange)
5859                                 {
5860                                         if (*conf->variable != newval)
5861                                         {
5862                                                 ereport(elevel,
5863                                                                 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5864                                                                  errmsg("parameter \"%s\" cannot be changed without restarting the server",
5865                                                                                 name)));
5866                                                 return 0;
5867                                         }
5868                                         return -1;
5869                                 }
5870
5871                                 if (changeVal)
5872                                 {
5873                                         /* Save old value to support transaction abort */
5874                                         if (!makeDefault)
5875                                                 push_old_value(&conf->gen, action);
5876
5877                                         if (conf->assign_hook)
5878                                                 (*conf->assign_hook) (newval, newextra);
5879                                         *conf->variable = newval;
5880                                         set_extra_field(&conf->gen, &conf->gen.extra,
5881                                                                         newextra);
5882                                         conf->gen.source = source;
5883                                         conf->gen.scontext = context;
5884                                 }
5885                                 if (makeDefault)
5886                                 {
5887                                         GucStack   *stack;
5888
5889                                         if (conf->gen.reset_source <= source)
5890                                         {
5891                                                 conf->reset_val = newval;
5892                                                 set_extra_field(&conf->gen, &conf->reset_extra,
5893                                                                                 newextra);
5894                                                 conf->gen.reset_source = source;
5895                                                 conf->gen.reset_scontext = context;
5896                                         }
5897                                         for (stack = conf->gen.stack; stack; stack = stack->prev)
5898                                         {
5899                                                 if (stack->source <= source)
5900                                                 {
5901                                                         stack->prior.val.enumval = newval;
5902                                                         set_extra_field(&conf->gen, &stack->prior.extra,
5903                                                                                         newextra);
5904                                                         stack->source = source;
5905                                                         stack->scontext = context;
5906                                                 }
5907                                         }
5908                                 }
5909
5910                                 /* Perhaps we didn't install newextra anywhere */
5911                                 if (newextra && !extra_field_used(&conf->gen, newextra))
5912                                         free(newextra);
5913                                 break;
5914                         }
5915         }
5916
5917         if (changeVal && (record->flags & GUC_REPORT))
5918                 ReportGUCOption(record);
5919
5920         return changeVal ? 1 : -1;
5921 }
5922
5923
5924 /*
5925  * Set the fields for source file and line number the setting came from.
5926  */
5927 static void
5928 set_config_sourcefile(const char *name, char *sourcefile, int sourceline)
5929 {
5930         struct config_generic *record;
5931         int                     elevel;
5932
5933         /*
5934          * To avoid cluttering the log, only the postmaster bleats loudly about
5935          * problems with the config file.
5936          */
5937         elevel = IsUnderPostmaster ? DEBUG3 : LOG;
5938
5939         record = find_option(name, true, elevel);
5940         /* should not happen */
5941         if (record == NULL)
5942                 elog(ERROR, "unrecognized configuration parameter \"%s\"", name);
5943
5944         sourcefile = guc_strdup(elevel, sourcefile);
5945         if (record->sourcefile)
5946                 free(record->sourcefile);
5947         record->sourcefile = sourcefile;
5948         record->sourceline = sourceline;
5949 }
5950
5951 /*
5952  * Set a config option to the given value.
5953  *
5954  * See also set_config_option; this is just the wrapper to be called from
5955  * outside GUC.  (This function should be used when possible, because its API
5956  * is more stable than set_config_option's.)
5957  *
5958  * Note: there is no support here for setting source file/line, as it
5959  * is currently not needed.
5960  */
5961 void
5962 SetConfigOption(const char *name, const char *value,
5963                                 GucContext context, GucSource source)
5964 {
5965         (void) set_config_option(name, value, context, source,
5966                                                          GUC_ACTION_SET, true, 0);
5967 }
5968
5969
5970
5971 /*
5972  * Fetch the current value of the option `name', as a string.
5973  *
5974  * If the option doesn't exist, return NULL if missing_ok is true (NOTE that
5975  * this cannot be distinguished from a string variable with a NULL value!),
5976  * otherwise throw an ereport and don't return.
5977  *
5978  * If restrict_superuser is true, we also enforce that only superusers can
5979  * see GUC_SUPERUSER_ONLY variables.  This should only be passed as true
5980  * in user-driven calls.
5981  *
5982  * The string is *not* allocated for modification and is really only
5983  * valid until the next call to configuration related functions.
5984  */
5985 const char *
5986 GetConfigOption(const char *name, bool missing_ok, bool restrict_superuser)
5987 {
5988         struct config_generic *record;
5989         static char buffer[256];
5990
5991         record = find_option(name, false, ERROR);
5992         if (record == NULL)
5993         {
5994                 if (missing_ok)
5995                         return NULL;
5996                 ereport(ERROR,
5997                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
5998                                  errmsg("unrecognized configuration parameter \"%s\"",
5999                                                 name)));
6000         }
6001         if (restrict_superuser &&
6002                 (record->flags & GUC_SUPERUSER_ONLY) &&
6003                 !superuser())
6004                 ereport(ERROR,
6005                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
6006                                  errmsg("must be superuser to examine \"%s\"", name)));
6007
6008         switch (record->vartype)
6009         {
6010                 case PGC_BOOL:
6011                         return *((struct config_bool *) record)->variable ? "on" : "off";
6012
6013                 case PGC_INT:
6014                         snprintf(buffer, sizeof(buffer), "%d",
6015                                          *((struct config_int *) record)->variable);
6016                         return buffer;
6017
6018                 case PGC_REAL:
6019                         snprintf(buffer, sizeof(buffer), "%g",
6020                                          *((struct config_real *) record)->variable);
6021                         return buffer;
6022
6023                 case PGC_STRING:
6024                         return *((struct config_string *) record)->variable;
6025
6026                 case PGC_ENUM:
6027                         return config_enum_lookup_by_value((struct config_enum *) record,
6028                                                                  *((struct config_enum *) record)->variable);
6029         }
6030         return NULL;
6031 }
6032
6033 /*
6034  * Get the RESET value associated with the given option.
6035  *
6036  * Note: this is not re-entrant, due to use of static result buffer;
6037  * not to mention that a string variable could have its reset_val changed.
6038  * Beware of assuming the result value is good for very long.
6039  */
6040 const char *
6041 GetConfigOptionResetString(const char *name)
6042 {
6043         struct config_generic *record;
6044         static char buffer[256];
6045
6046         record = find_option(name, false, ERROR);
6047         if (record == NULL)
6048                 ereport(ERROR,
6049                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
6050                            errmsg("unrecognized configuration parameter \"%s\"", name)));
6051         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
6052                 ereport(ERROR,
6053                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
6054                                  errmsg("must be superuser to examine \"%s\"", name)));
6055
6056         switch (record->vartype)
6057         {
6058                 case PGC_BOOL:
6059                         return ((struct config_bool *) record)->reset_val ? "on" : "off";
6060
6061                 case PGC_INT:
6062                         snprintf(buffer, sizeof(buffer), "%d",
6063                                          ((struct config_int *) record)->reset_val);
6064                         return buffer;
6065
6066                 case PGC_REAL:
6067                         snprintf(buffer, sizeof(buffer), "%g",
6068                                          ((struct config_real *) record)->reset_val);
6069                         return buffer;
6070
6071                 case PGC_STRING:
6072                         return ((struct config_string *) record)->reset_val;
6073
6074                 case PGC_ENUM:
6075                         return config_enum_lookup_by_value((struct config_enum *) record,
6076                                                                  ((struct config_enum *) record)->reset_val);
6077         }
6078         return NULL;
6079 }
6080
6081
6082 /*
6083  * flatten_set_variable_args
6084  *              Given a parsenode List as emitted by the grammar for SET,
6085  *              convert to the flat string representation used by GUC.
6086  *
6087  * We need to be told the name of the variable the args are for, because
6088  * the flattening rules vary (ugh).
6089  *
6090  * The result is NULL if args is NIL (ie, SET ... TO DEFAULT), otherwise
6091  * a palloc'd string.
6092  */
6093 static char *
6094 flatten_set_variable_args(const char *name, List *args)
6095 {
6096         struct config_generic *record;
6097         int                     flags;
6098         StringInfoData buf;
6099         ListCell   *l;
6100
6101         /* Fast path if just DEFAULT */
6102         if (args == NIL)
6103                 return NULL;
6104
6105         /*
6106          * Get flags for the variable; if it's not known, use default flags.
6107          * (Caller might throw error later, but not our business to do so here.)
6108          */
6109         record = find_option(name, false, WARNING);
6110         if (record)
6111                 flags = record->flags;
6112         else
6113                 flags = 0;
6114
6115         /* Complain if list input and non-list variable */
6116         if ((flags & GUC_LIST_INPUT) == 0 &&
6117                 list_length(args) != 1)
6118                 ereport(ERROR,
6119                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6120                                  errmsg("SET %s takes only one argument", name)));
6121
6122         initStringInfo(&buf);
6123
6124         /*
6125          * Each list member may be a plain A_Const node, or an A_Const within a
6126          * TypeCast; the latter case is supported only for ConstInterval arguments
6127          * (for SET TIME ZONE).
6128          */
6129         foreach(l, args)
6130         {
6131                 Node       *arg = (Node *) lfirst(l);
6132                 char       *val;
6133                 TypeName   *typeName = NULL;
6134                 A_Const    *con;
6135
6136                 if (l != list_head(args))
6137                         appendStringInfo(&buf, ", ");
6138
6139                 if (IsA(arg, TypeCast))
6140                 {
6141                         TypeCast   *tc = (TypeCast *) arg;
6142
6143                         arg = tc->arg;
6144                         typeName = tc->typeName;
6145                 }
6146
6147                 if (!IsA(arg, A_Const))
6148                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(arg));
6149                 con = (A_Const *) arg;
6150
6151                 switch (nodeTag(&con->val))
6152                 {
6153                         case T_Integer:
6154                                 appendStringInfo(&buf, "%ld", intVal(&con->val));
6155                                 break;
6156                         case T_Float:
6157                                 /* represented as a string, so just copy it */
6158                                 appendStringInfoString(&buf, strVal(&con->val));
6159                                 break;
6160                         case T_String:
6161                                 val = strVal(&con->val);
6162                                 if (typeName != NULL)
6163                                 {
6164                                         /*
6165                                          * Must be a ConstInterval argument for TIME ZONE. Coerce
6166                                          * to interval and back to normalize the value and account
6167                                          * for any typmod.
6168                                          */
6169                                         Oid                     typoid;
6170                                         int32           typmod;
6171                                         Datum           interval;
6172                                         char       *intervalout;
6173
6174                                         typenameTypeIdAndMod(NULL, typeName, &typoid, &typmod);
6175                                         Assert(typoid == INTERVALOID);
6176
6177                                         interval =
6178                                                 DirectFunctionCall3(interval_in,
6179                                                                                         CStringGetDatum(val),
6180                                                                                         ObjectIdGetDatum(InvalidOid),
6181                                                                                         Int32GetDatum(typmod));
6182
6183                                         intervalout =
6184                                                 DatumGetCString(DirectFunctionCall1(interval_out,
6185                                                                                                                         interval));
6186                                         appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
6187                                 }
6188                                 else
6189                                 {
6190                                         /*
6191                                          * Plain string literal or identifier.  For quote mode,
6192                                          * quote it if it's not a vanilla identifier.
6193                                          */
6194                                         if (flags & GUC_LIST_QUOTE)
6195                                                 appendStringInfoString(&buf, quote_identifier(val));
6196                                         else
6197                                                 appendStringInfoString(&buf, val);
6198                                 }
6199                                 break;
6200                         default:
6201                                 elog(ERROR, "unrecognized node type: %d",
6202                                          (int) nodeTag(&con->val));
6203                                 break;
6204                 }
6205         }
6206
6207         return buf.data;
6208 }
6209
6210
6211 /*
6212  * SET command
6213  */
6214 void
6215 ExecSetVariableStmt(VariableSetStmt *stmt)
6216 {
6217         GucAction       action = stmt->is_local ? GUC_ACTION_LOCAL : GUC_ACTION_SET;
6218
6219         switch (stmt->kind)
6220         {
6221                 case VAR_SET_VALUE:
6222                 case VAR_SET_CURRENT:
6223                         (void) set_config_option(stmt->name,
6224                                                                          ExtractSetVariableArgs(stmt),
6225                                                                          (superuser() ? PGC_SUSET : PGC_USERSET),
6226                                                                          PGC_S_SESSION,
6227                                                                          action,
6228                                                                          true,
6229                                                                          0);
6230                         break;
6231                 case VAR_SET_MULTI:
6232
6233                         /*
6234                          * Special-case SQL syntaxes.  The TRANSACTION and SESSION
6235                          * CHARACTERISTICS cases effectively set more than one variable
6236                          * per statement.  TRANSACTION SNAPSHOT only takes one argument,
6237                          * but we put it here anyway since it's a special case and not
6238                          * related to any GUC variable.
6239                          */
6240                         if (strcmp(stmt->name, "TRANSACTION") == 0)
6241                         {
6242                                 ListCell   *head;
6243
6244                                 foreach(head, stmt->args)
6245                                 {
6246                                         DefElem    *item = (DefElem *) lfirst(head);
6247
6248                                         if (strcmp(item->defname, "transaction_isolation") == 0)
6249                                                 SetPGVariable("transaction_isolation",
6250                                                                           list_make1(item->arg), stmt->is_local);
6251                                         else if (strcmp(item->defname, "transaction_read_only") == 0)
6252                                                 SetPGVariable("transaction_read_only",
6253                                                                           list_make1(item->arg), stmt->is_local);
6254                                         else if (strcmp(item->defname, "transaction_deferrable") == 0)
6255                                                 SetPGVariable("transaction_deferrable",
6256                                                                           list_make1(item->arg), stmt->is_local);
6257                                         else
6258                                                 elog(ERROR, "unexpected SET TRANSACTION element: %s",
6259                                                          item->defname);
6260                                 }
6261                         }
6262                         else if (strcmp(stmt->name, "SESSION CHARACTERISTICS") == 0)
6263                         {
6264                                 ListCell   *head;
6265
6266                                 foreach(head, stmt->args)
6267                                 {
6268                                         DefElem    *item = (DefElem *) lfirst(head);
6269
6270                                         if (strcmp(item->defname, "transaction_isolation") == 0)
6271                                                 SetPGVariable("default_transaction_isolation",
6272                                                                           list_make1(item->arg), stmt->is_local);
6273                                         else if (strcmp(item->defname, "transaction_read_only") == 0)
6274                                                 SetPGVariable("default_transaction_read_only",
6275                                                                           list_make1(item->arg), stmt->is_local);
6276                                         else if (strcmp(item->defname, "transaction_deferrable") == 0)
6277                                                 SetPGVariable("default_transaction_deferrable",
6278                                                                           list_make1(item->arg), stmt->is_local);
6279                                         else
6280                                                 elog(ERROR, "unexpected SET SESSION element: %s",
6281                                                          item->defname);
6282                                 }
6283                         }
6284                         else if (strcmp(stmt->name, "TRANSACTION SNAPSHOT") == 0)
6285                         {
6286                                 A_Const    *con = (A_Const *) linitial(stmt->args);
6287
6288                                 if (stmt->is_local)
6289                                         ereport(ERROR,
6290                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6291                                                          errmsg("SET LOCAL TRANSACTION SNAPSHOT is not implemented")));
6292                                 Assert(IsA(con, A_Const));
6293                                 Assert(nodeTag(&con->val) == T_String);
6294                                 ImportSnapshot(strVal(&con->val));
6295                         }
6296                         else
6297                                 elog(ERROR, "unexpected SET MULTI element: %s",
6298                                          stmt->name);
6299                         break;
6300                 case VAR_SET_DEFAULT:
6301                 case VAR_RESET:
6302                         (void) set_config_option(stmt->name,
6303                                                                          NULL,
6304                                                                          (superuser() ? PGC_SUSET : PGC_USERSET),
6305                                                                          PGC_S_SESSION,
6306                                                                          action,
6307                                                                          true,
6308                                                                          0);
6309                         break;
6310                 case VAR_RESET_ALL:
6311                         ResetAllOptions();
6312                         break;
6313         }
6314 }
6315
6316 /*
6317  * Get the value to assign for a VariableSetStmt, or NULL if it's RESET.
6318  * The result is palloc'd.
6319  *
6320  * This is exported for use by actions such as ALTER ROLE SET.
6321  */
6322 char *
6323 ExtractSetVariableArgs(VariableSetStmt *stmt)
6324 {
6325         switch (stmt->kind)
6326         {
6327                 case VAR_SET_VALUE:
6328                         return flatten_set_variable_args(stmt->name, stmt->args);
6329                 case VAR_SET_CURRENT:
6330                         return GetConfigOptionByName(stmt->name, NULL);
6331                 default:
6332                         return NULL;
6333         }
6334 }
6335
6336 /*
6337  * SetPGVariable - SET command exported as an easily-C-callable function.
6338  *
6339  * This provides access to SET TO value, as well as SET TO DEFAULT (expressed
6340  * by passing args == NIL), but not SET FROM CURRENT functionality.
6341  */
6342 void
6343 SetPGVariable(const char *name, List *args, bool is_local)
6344 {
6345         char       *argstring = flatten_set_variable_args(name, args);
6346
6347         /* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
6348         (void) set_config_option(name,
6349                                                          argstring,
6350                                                          (superuser() ? PGC_SUSET : PGC_USERSET),
6351                                                          PGC_S_SESSION,
6352                                                          is_local ? GUC_ACTION_LOCAL : GUC_ACTION_SET,
6353                                                          true,
6354                                                          0);
6355 }
6356
6357 /*
6358  * SET command wrapped as a SQL callable function.
6359  */
6360 Datum
6361 set_config_by_name(PG_FUNCTION_ARGS)
6362 {
6363         char       *name;
6364         char       *value;
6365         char       *new_value;
6366         bool            is_local;
6367
6368         if (PG_ARGISNULL(0))
6369                 ereport(ERROR,
6370                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6371                                  errmsg("SET requires parameter name")));
6372
6373         /* Get the GUC variable name */
6374         name = TextDatumGetCString(PG_GETARG_DATUM(0));
6375
6376         /* Get the desired value or set to NULL for a reset request */
6377         if (PG_ARGISNULL(1))
6378                 value = NULL;
6379         else
6380                 value = TextDatumGetCString(PG_GETARG_DATUM(1));
6381
6382         /*
6383          * Get the desired state of is_local. Default to false if provided value
6384          * is NULL
6385          */
6386         if (PG_ARGISNULL(2))
6387                 is_local = false;
6388         else
6389                 is_local = PG_GETARG_BOOL(2);
6390
6391         /* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
6392         (void) set_config_option(name,
6393                                                          value,
6394                                                          (superuser() ? PGC_SUSET : PGC_USERSET),
6395                                                          PGC_S_SESSION,
6396                                                          is_local ? GUC_ACTION_LOCAL : GUC_ACTION_SET,
6397                                                          true,
6398                                                          0);
6399
6400         /* get the new current value */
6401         new_value = GetConfigOptionByName(name, NULL);
6402
6403         /* Convert return string to text */
6404         PG_RETURN_TEXT_P(cstring_to_text(new_value));
6405 }
6406
6407
6408 /*
6409  * Common code for DefineCustomXXXVariable subroutines: allocate the
6410  * new variable's config struct and fill in generic fields.
6411  */
6412 static struct config_generic *
6413 init_custom_variable(const char *name,
6414                                          const char *short_desc,
6415                                          const char *long_desc,
6416                                          GucContext context,
6417                                          int flags,
6418                                          enum config_type type,
6419                                          size_t sz)
6420 {
6421         struct config_generic *gen;
6422
6423         /*
6424          * Only allow custom PGC_POSTMASTER variables to be created during shared
6425          * library preload; any later than that, we can't ensure that the value
6426          * doesn't change after startup.  This is a fatal elog if it happens; just
6427          * erroring out isn't safe because we don't know what the calling loadable
6428          * module might already have hooked into.
6429          */
6430         if (context == PGC_POSTMASTER &&
6431                 !process_shared_preload_libraries_in_progress)
6432                 elog(FATAL, "cannot create PGC_POSTMASTER variables after startup");
6433
6434         gen = (struct config_generic *) guc_malloc(ERROR, sz);
6435         memset(gen, 0, sz);
6436
6437         gen->name = guc_strdup(ERROR, name);
6438         gen->context = context;
6439         gen->group = CUSTOM_OPTIONS;
6440         gen->short_desc = short_desc;
6441         gen->long_desc = long_desc;
6442         gen->flags = flags;
6443         gen->vartype = type;
6444
6445         return gen;
6446 }
6447
6448 /*
6449  * Common code for DefineCustomXXXVariable subroutines: insert the new
6450  * variable into the GUC variable array, replacing any placeholder.
6451  */
6452 static void
6453 define_custom_variable(struct config_generic * variable)
6454 {
6455         const char *name = variable->name;
6456         const char **nameAddr = &name;
6457         struct config_string *pHolder;
6458         struct config_generic **res;
6459
6460         /*
6461          * See if there's a placeholder by the same name.
6462          */
6463         res = (struct config_generic **) bsearch((void *) &nameAddr,
6464                                                                                          (void *) guc_variables,
6465                                                                                          num_guc_variables,
6466                                                                                          sizeof(struct config_generic *),
6467                                                                                          guc_var_compare);
6468         if (res == NULL)
6469         {
6470                 /*
6471                  * No placeholder to replace, so we can just add it ... but first,
6472                  * make sure it's initialized to its default value.
6473                  */
6474                 InitializeOneGUCOption(variable);
6475                 add_guc_variable(variable, ERROR);
6476                 return;
6477         }
6478
6479         /*
6480          * This better be a placeholder
6481          */
6482         if (((*res)->flags & GUC_CUSTOM_PLACEHOLDER) == 0)
6483                 ereport(ERROR,
6484                                 (errcode(ERRCODE_INTERNAL_ERROR),
6485                                  errmsg("attempt to redefine parameter \"%s\"", name)));
6486
6487         Assert((*res)->vartype == PGC_STRING);
6488         pHolder = (struct config_string *) (*res);
6489
6490         /*
6491          * First, set the variable to its default value.  We must do this even
6492          * though we intend to immediately apply a new value, since it's possible
6493          * that the new value is invalid.
6494          */
6495         InitializeOneGUCOption(variable);
6496
6497         /*
6498          * Replace the placeholder. We aren't changing the name, so no re-sorting
6499          * is necessary
6500          */
6501         *res = variable;
6502
6503         /*
6504          * Assign the string value(s) stored in the placeholder to the real
6505          * variable.  Essentially, we need to duplicate all the active and stacked
6506          * values, but with appropriate validation and datatype adjustment.
6507          *
6508          * If an assignment fails, we report a WARNING and keep going.  We don't
6509          * want to throw ERROR for bad values, because it'd bollix the add-on
6510          * module that's presumably halfway through getting loaded.  In such cases
6511          * the default or previous state will become active instead.
6512          */
6513
6514         /* First, apply the reset value if any */
6515         if (pHolder->reset_val)
6516                 (void) set_config_option(name, pHolder->reset_val,
6517                                                                  pHolder->gen.reset_scontext,
6518                                                                  pHolder->gen.reset_source,
6519                                                                  GUC_ACTION_SET, true, WARNING);
6520         /* That should not have resulted in stacking anything */
6521         Assert(variable->stack == NULL);
6522
6523         /* Now, apply current and stacked values, in the order they were stacked */
6524         reapply_stacked_values(variable, pHolder, pHolder->gen.stack,
6525                                                    *(pHolder->variable),
6526                                                    pHolder->gen.scontext, pHolder->gen.source);
6527
6528         /* Also copy over any saved source-location information */
6529         if (pHolder->gen.sourcefile)
6530                 set_config_sourcefile(name, pHolder->gen.sourcefile,
6531                                                           pHolder->gen.sourceline);
6532
6533         /*
6534          * Free up as much as we conveniently can of the placeholder structure.
6535          * (This neglects any stack items, so it's possible for some memory to be
6536          * leaked.      Since this can only happen once per session per variable, it
6537          * doesn't seem worth spending much code on.)
6538          */
6539         set_string_field(pHolder, pHolder->variable, NULL);
6540         set_string_field(pHolder, &pHolder->reset_val, NULL);
6541
6542         free(pHolder);
6543 }
6544
6545 /*
6546  * Recursive subroutine for define_custom_variable: reapply non-reset values
6547  *
6548  * We recurse so that the values are applied in the same order as originally.
6549  * At each recursion level, apply the upper-level value (passed in) in the
6550  * fashion implied by the stack entry.
6551  */
6552 static void
6553 reapply_stacked_values(struct config_generic * variable,
6554                                            struct config_string * pHolder,
6555                                            GucStack *stack,
6556                                            const char *curvalue,
6557                                            GucContext curscontext, GucSource cursource)
6558 {
6559         const char *name = variable->name;
6560         GucStack   *oldvarstack = variable->stack;
6561
6562         if (stack != NULL)
6563         {
6564                 /* First, recurse, so that stack items are processed bottom to top */
6565                 reapply_stacked_values(variable, pHolder, stack->prev,
6566                                                            stack->prior.val.stringval,
6567                                                            stack->scontext, stack->source);
6568
6569                 /* See how to apply the passed-in value */
6570                 switch (stack->state)
6571                 {
6572                         case GUC_SAVE:
6573                                 (void) set_config_option(name, curvalue,
6574                                                                                  curscontext, cursource,
6575                                                                                  GUC_ACTION_SAVE, true, WARNING);
6576                                 break;
6577
6578                         case GUC_SET:
6579                                 (void) set_config_option(name, curvalue,
6580                                                                                  curscontext, cursource,
6581                                                                                  GUC_ACTION_SET, true, WARNING);
6582                                 break;
6583
6584                         case GUC_LOCAL:
6585                                 (void) set_config_option(name, curvalue,
6586                                                                                  curscontext, cursource,
6587                                                                                  GUC_ACTION_LOCAL, true, WARNING);
6588                                 break;
6589
6590                         case GUC_SET_LOCAL:
6591                                 /* first, apply the masked value as SET */
6592                                 (void) set_config_option(name, stack->masked.val.stringval,
6593                                                                            stack->masked_scontext, PGC_S_SESSION,
6594                                                                                  GUC_ACTION_SET, true, WARNING);
6595                                 /* then apply the current value as LOCAL */
6596                                 (void) set_config_option(name, curvalue,
6597                                                                                  curscontext, cursource,
6598                                                                                  GUC_ACTION_LOCAL, true, WARNING);
6599                                 break;
6600                 }
6601
6602                 /* If we successfully made a stack entry, adjust its nest level */
6603                 if (variable->stack != oldvarstack)
6604                         variable->stack->nest_level = stack->nest_level;
6605         }
6606         else
6607         {
6608                 /*
6609                  * We are at the end of the stack.      If the active/previous value is
6610                  * different from the reset value, it must represent a previously
6611                  * committed session value.  Apply it, and then drop the stack entry
6612                  * that set_config_option will have created under the impression that
6613                  * this is to be just a transactional assignment.  (We leak the stack
6614                  * entry.)
6615                  */
6616                 if (curvalue != pHolder->reset_val ||
6617                         curscontext != pHolder->gen.reset_scontext ||
6618                         cursource != pHolder->gen.reset_source)
6619                 {
6620                         (void) set_config_option(name, curvalue,
6621                                                                          curscontext, cursource,
6622                                                                          GUC_ACTION_SET, true, WARNING);
6623                         variable->stack = NULL;
6624                 }
6625         }
6626 }
6627
6628 void
6629 DefineCustomBoolVariable(const char *name,
6630                                                  const char *short_desc,
6631                                                  const char *long_desc,
6632                                                  bool *valueAddr,
6633                                                  bool bootValue,
6634                                                  GucContext context,
6635                                                  int flags,
6636                                                  GucBoolCheckHook check_hook,
6637                                                  GucBoolAssignHook assign_hook,
6638                                                  GucShowHook show_hook)
6639 {
6640         struct config_bool *var;
6641
6642         var = (struct config_bool *)
6643                 init_custom_variable(name, short_desc, long_desc, context, flags,
6644                                                          PGC_BOOL, sizeof(struct config_bool));
6645         var->variable = valueAddr;
6646         var->boot_val = bootValue;
6647         var->reset_val = bootValue;
6648         var->check_hook = check_hook;
6649         var->assign_hook = assign_hook;
6650         var->show_hook = show_hook;
6651         define_custom_variable(&var->gen);
6652 }
6653
6654 void
6655 DefineCustomIntVariable(const char *name,
6656                                                 const char *short_desc,
6657                                                 const char *long_desc,
6658                                                 int *valueAddr,
6659                                                 int bootValue,
6660                                                 int minValue,
6661                                                 int maxValue,
6662                                                 GucContext context,
6663                                                 int flags,
6664                                                 GucIntCheckHook check_hook,
6665                                                 GucIntAssignHook assign_hook,
6666                                                 GucShowHook show_hook)
6667 {
6668         struct config_int *var;
6669
6670         var = (struct config_int *)
6671                 init_custom_variable(name, short_desc, long_desc, context, flags,
6672                                                          PGC_INT, sizeof(struct config_int));
6673         var->variable = valueAddr;
6674         var->boot_val = bootValue;
6675         var->reset_val = bootValue;
6676         var->min = minValue;
6677         var->max = maxValue;
6678         var->check_hook = check_hook;
6679         var->assign_hook = assign_hook;
6680         var->show_hook = show_hook;
6681         define_custom_variable(&var->gen);
6682 }
6683
6684 void
6685 DefineCustomRealVariable(const char *name,
6686                                                  const char *short_desc,
6687                                                  const char *long_desc,
6688                                                  double *valueAddr,
6689                                                  double bootValue,
6690                                                  double minValue,
6691                                                  double maxValue,
6692                                                  GucContext context,
6693                                                  int flags,
6694                                                  GucRealCheckHook check_hook,
6695                                                  GucRealAssignHook assign_hook,
6696                                                  GucShowHook show_hook)
6697 {
6698         struct config_real *var;
6699
6700         var = (struct config_real *)
6701                 init_custom_variable(name, short_desc, long_desc, context, flags,
6702                                                          PGC_REAL, sizeof(struct config_real));
6703         var->variable = valueAddr;
6704         var->boot_val = bootValue;
6705         var->reset_val = bootValue;
6706         var->min = minValue;
6707         var->max = maxValue;
6708         var->check_hook = check_hook;
6709         var->assign_hook = assign_hook;
6710         var->show_hook = show_hook;
6711         define_custom_variable(&var->gen);
6712 }
6713
6714 void
6715 DefineCustomStringVariable(const char *name,
6716                                                    const char *short_desc,
6717                                                    const char *long_desc,
6718                                                    char **valueAddr,
6719                                                    const char *bootValue,
6720                                                    GucContext context,
6721                                                    int flags,
6722                                                    GucStringCheckHook check_hook,
6723                                                    GucStringAssignHook assign_hook,
6724                                                    GucShowHook show_hook)
6725 {
6726         struct config_string *var;
6727
6728         var = (struct config_string *)
6729                 init_custom_variable(name, short_desc, long_desc, context, flags,
6730                                                          PGC_STRING, sizeof(struct config_string));
6731         var->variable = valueAddr;
6732         var->boot_val = bootValue;
6733         var->check_hook = check_hook;
6734         var->assign_hook = assign_hook;
6735         var->show_hook = show_hook;
6736         define_custom_variable(&var->gen);
6737 }
6738
6739 void
6740 DefineCustomEnumVariable(const char *name,
6741                                                  const char *short_desc,
6742                                                  const char *long_desc,
6743                                                  int *valueAddr,
6744                                                  int bootValue,
6745                                                  const struct config_enum_entry * options,
6746                                                  GucContext context,
6747                                                  int flags,
6748                                                  GucEnumCheckHook check_hook,
6749                                                  GucEnumAssignHook assign_hook,
6750                                                  GucShowHook show_hook)
6751 {
6752         struct config_enum *var;
6753
6754         var = (struct config_enum *)
6755                 init_custom_variable(name, short_desc, long_desc, context, flags,
6756                                                          PGC_ENUM, sizeof(struct config_enum));
6757         var->variable = valueAddr;
6758         var->boot_val = bootValue;
6759         var->reset_val = bootValue;
6760         var->options = options;
6761         var->check_hook = check_hook;
6762         var->assign_hook = assign_hook;
6763         var->show_hook = show_hook;
6764         define_custom_variable(&var->gen);
6765 }
6766
6767 void
6768 EmitWarningsOnPlaceholders(const char *className)
6769 {
6770         int                     classLen = strlen(className);
6771         int                     i;
6772
6773         for (i = 0; i < num_guc_variables; i++)
6774         {
6775                 struct config_generic *var = guc_variables[i];
6776
6777                 if ((var->flags & GUC_CUSTOM_PLACEHOLDER) != 0 &&
6778                         strncmp(className, var->name, classLen) == 0 &&
6779                         var->name[classLen] == GUC_QUALIFIER_SEPARATOR)
6780                 {
6781                         ereport(WARNING,
6782                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
6783                                          errmsg("unrecognized configuration parameter \"%s\"",
6784                                                         var->name)));
6785                 }
6786         }
6787 }
6788
6789
6790 /*
6791  * SHOW command
6792  */
6793 void
6794 GetPGVariable(const char *name, DestReceiver *dest)
6795 {
6796         if (guc_name_compare(name, "all") == 0)
6797                 ShowAllGUCConfig(dest);
6798         else
6799                 ShowGUCConfigOption(name, dest);
6800 }
6801
6802 TupleDesc
6803 GetPGVariableResultDesc(const char *name)
6804 {
6805         TupleDesc       tupdesc;
6806
6807         if (guc_name_compare(name, "all") == 0)
6808         {
6809                 /* need a tuple descriptor representing three TEXT columns */
6810                 tupdesc = CreateTemplateTupleDesc(3, false);
6811                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
6812                                                    TEXTOID, -1, 0);
6813                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
6814                                                    TEXTOID, -1, 0);
6815                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
6816                                                    TEXTOID, -1, 0);
6817         }
6818         else
6819         {
6820                 const char *varname;
6821
6822                 /* Get the canonical spelling of name */
6823                 (void) GetConfigOptionByName(name, &varname);
6824
6825                 /* need a tuple descriptor representing a single TEXT column */
6826                 tupdesc = CreateTemplateTupleDesc(1, false);
6827                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
6828                                                    TEXTOID, -1, 0);
6829         }
6830         return tupdesc;
6831 }
6832
6833
6834 /*
6835  * SHOW command
6836  */
6837 static void
6838 ShowGUCConfigOption(const char *name, DestReceiver *dest)
6839 {
6840         TupOutputState *tstate;
6841         TupleDesc       tupdesc;
6842         const char *varname;
6843         char       *value;
6844
6845         /* Get the value and canonical spelling of name */
6846         value = GetConfigOptionByName(name, &varname);
6847
6848         /* need a tuple descriptor representing a single TEXT column */
6849         tupdesc = CreateTemplateTupleDesc(1, false);
6850         TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
6851                                            TEXTOID, -1, 0);
6852
6853         /* prepare for projection of tuples */
6854         tstate = begin_tup_output_tupdesc(dest, tupdesc);
6855
6856         /* Send it */
6857         do_text_output_oneline(tstate, value);
6858
6859         end_tup_output(tstate);
6860 }
6861
6862 /*
6863  * SHOW ALL command
6864  */
6865 static void
6866 ShowAllGUCConfig(DestReceiver *dest)
6867 {
6868         bool            am_superuser = superuser();
6869         int                     i;
6870         TupOutputState *tstate;
6871         TupleDesc       tupdesc;
6872         Datum           values[3];
6873         bool            isnull[3] = {false, false, false};
6874
6875         /* need a tuple descriptor representing three TEXT columns */
6876         tupdesc = CreateTemplateTupleDesc(3, false);
6877         TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
6878                                            TEXTOID, -1, 0);
6879         TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
6880                                            TEXTOID, -1, 0);
6881         TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
6882                                            TEXTOID, -1, 0);
6883
6884         /* prepare for projection of tuples */
6885         tstate = begin_tup_output_tupdesc(dest, tupdesc);
6886
6887         for (i = 0; i < num_guc_variables; i++)
6888         {
6889                 struct config_generic *conf = guc_variables[i];
6890                 char       *setting;
6891
6892                 if ((conf->flags & GUC_NO_SHOW_ALL) ||
6893                         ((conf->flags & GUC_SUPERUSER_ONLY) && !am_superuser))
6894                         continue;
6895
6896                 /* assign to the values array */
6897                 values[0] = PointerGetDatum(cstring_to_text(conf->name));
6898
6899                 setting = _ShowOption(conf, true);
6900                 if (setting)
6901                 {
6902                         values[1] = PointerGetDatum(cstring_to_text(setting));
6903                         isnull[1] = false;
6904                 }
6905                 else
6906                 {
6907                         values[1] = PointerGetDatum(NULL);
6908                         isnull[1] = true;
6909                 }
6910
6911                 values[2] = PointerGetDatum(cstring_to_text(conf->short_desc));
6912
6913                 /* send it to dest */
6914                 do_tup_output(tstate, values, isnull);
6915
6916                 /* clean up */
6917                 pfree(DatumGetPointer(values[0]));
6918                 if (setting)
6919                 {
6920                         pfree(setting);
6921                         pfree(DatumGetPointer(values[1]));
6922                 }
6923                 pfree(DatumGetPointer(values[2]));
6924         }
6925
6926         end_tup_output(tstate);
6927 }
6928
6929 /*
6930  * Return GUC variable value by name; optionally return canonical
6931  * form of name.  Return value is palloc'd.
6932  */
6933 char *
6934 GetConfigOptionByName(const char *name, const char **varname)
6935 {
6936         struct config_generic *record;
6937
6938         record = find_option(name, false, ERROR);
6939         if (record == NULL)
6940                 ereport(ERROR,
6941                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
6942                            errmsg("unrecognized configuration parameter \"%s\"", name)));
6943         if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
6944                 ereport(ERROR,
6945                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
6946                                  errmsg("must be superuser to examine \"%s\"", name)));
6947
6948         if (varname)
6949                 *varname = record->name;
6950
6951         return _ShowOption(record, true);
6952 }
6953
6954 /*
6955  * Return GUC variable value by variable number; optionally return canonical
6956  * form of name.  Return value is palloc'd.
6957  */
6958 void
6959 GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
6960 {
6961         char            buffer[256];
6962         struct config_generic *conf;
6963
6964         /* check requested variable number valid */
6965         Assert((varnum >= 0) && (varnum < num_guc_variables));
6966
6967         conf = guc_variables[varnum];
6968
6969         if (noshow)
6970         {
6971                 if ((conf->flags & GUC_NO_SHOW_ALL) ||
6972                         ((conf->flags & GUC_SUPERUSER_ONLY) && !superuser()))
6973                         *noshow = true;
6974                 else
6975                         *noshow = false;
6976         }
6977
6978         /* first get the generic attributes */
6979
6980         /* name */
6981         values[0] = conf->name;
6982
6983         /* setting : use _ShowOption in order to avoid duplicating the logic */
6984         values[1] = _ShowOption(conf, false);
6985
6986         /* unit */
6987         if (conf->vartype == PGC_INT)
6988         {
6989                 static char buf[8];
6990
6991                 switch (conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
6992                 {
6993                         case GUC_UNIT_KB:
6994                                 values[2] = "kB";
6995                                 break;
6996                         case GUC_UNIT_BLOCKS:
6997                                 snprintf(buf, sizeof(buf), "%dkB", BLCKSZ / 1024);
6998                                 values[2] = buf;
6999                                 break;
7000                         case GUC_UNIT_XBLOCKS:
7001                                 snprintf(buf, sizeof(buf), "%dkB", XLOG_BLCKSZ / 1024);
7002                                 values[2] = buf;
7003                                 break;
7004                         case GUC_UNIT_MS:
7005                                 values[2] = "ms";
7006                                 break;
7007                         case GUC_UNIT_S:
7008                                 values[2] = "s";
7009                                 break;
7010                         case GUC_UNIT_MIN:
7011                                 values[2] = "min";
7012                                 break;
7013                         default:
7014                                 values[2] = "";
7015                                 break;
7016                 }
7017         }
7018         else
7019                 values[2] = NULL;
7020
7021         /* group */
7022         values[3] = config_group_names[conf->group];
7023
7024         /* short_desc */
7025         values[4] = conf->short_desc;
7026
7027         /* extra_desc */
7028         values[5] = conf->long_desc;
7029
7030         /* context */
7031         values[6] = GucContext_Names[conf->context];
7032
7033         /* vartype */
7034         values[7] = config_type_names[conf->vartype];
7035
7036         /* source */
7037         values[8] = GucSource_Names[conf->source];
7038
7039         /* now get the type specifc attributes */
7040         switch (conf->vartype)
7041         {
7042                 case PGC_BOOL:
7043                         {
7044                                 struct config_bool *lconf = (struct config_bool *) conf;
7045
7046                                 /* min_val */
7047                                 values[9] = NULL;
7048
7049                                 /* max_val */
7050                                 values[10] = NULL;
7051
7052                                 /* enumvals */
7053                                 values[11] = NULL;
7054
7055                                 /* boot_val */
7056                                 values[12] = pstrdup(lconf->boot_val ? "on" : "off");
7057
7058                                 /* reset_val */
7059                                 values[13] = pstrdup(lconf->reset_val ? "on" : "off");
7060                         }
7061                         break;
7062
7063                 case PGC_INT:
7064                         {
7065                                 struct config_int *lconf = (struct config_int *) conf;
7066
7067                                 /* min_val */
7068                                 snprintf(buffer, sizeof(buffer), "%d", lconf->min);
7069                                 values[9] = pstrdup(buffer);
7070
7071                                 /* max_val */
7072                                 snprintf(buffer, sizeof(buffer), "%d", lconf->max);
7073                                 values[10] = pstrdup(buffer);
7074
7075                                 /* enumvals */
7076                                 values[11] = NULL;
7077
7078                                 /* boot_val */
7079                                 snprintf(buffer, sizeof(buffer), "%d", lconf->boot_val);
7080                                 values[12] = pstrdup(buffer);
7081
7082                                 /* reset_val */
7083                                 snprintf(buffer, sizeof(buffer), "%d", lconf->reset_val);
7084                                 values[13] = pstrdup(buffer);
7085                         }
7086                         break;
7087
7088                 case PGC_REAL:
7089                         {
7090                                 struct config_real *lconf = (struct config_real *) conf;
7091
7092                                 /* min_val */
7093                                 snprintf(buffer, sizeof(buffer), "%g", lconf->min);
7094                                 values[9] = pstrdup(buffer);
7095
7096                                 /* max_val */
7097                                 snprintf(buffer, sizeof(buffer), "%g", lconf->max);
7098                                 values[10] = pstrdup(buffer);
7099
7100                                 /* enumvals */
7101                                 values[11] = NULL;
7102
7103                                 /* boot_val */
7104                                 snprintf(buffer, sizeof(buffer), "%g", lconf->boot_val);
7105                                 values[12] = pstrdup(buffer);
7106
7107                                 /* reset_val */
7108                                 snprintf(buffer, sizeof(buffer), "%g", lconf->reset_val);
7109                                 values[13] = pstrdup(buffer);
7110                         }
7111                         break;
7112
7113                 case PGC_STRING:
7114                         {
7115                                 struct config_string *lconf = (struct config_string *) conf;
7116
7117                                 /* min_val */
7118                                 values[9] = NULL;
7119
7120                                 /* max_val */
7121                                 values[10] = NULL;
7122
7123                                 /* enumvals */
7124                                 values[11] = NULL;
7125
7126                                 /* boot_val */
7127                                 if (lconf->boot_val == NULL)
7128                                         values[12] = NULL;
7129                                 else
7130                                         values[12] = pstrdup(lconf->boot_val);
7131
7132                                 /* reset_val */
7133                                 if (lconf->reset_val == NULL)
7134                                         values[13] = NULL;
7135                                 else
7136                                         values[13] = pstrdup(lconf->reset_val);
7137                         }
7138                         break;
7139
7140                 case PGC_ENUM:
7141                         {
7142                                 struct config_enum *lconf = (struct config_enum *) conf;
7143
7144                                 /* min_val */
7145                                 values[9] = NULL;
7146
7147                                 /* max_val */
7148                                 values[10] = NULL;
7149
7150                                 /* enumvals */
7151
7152                                 /*
7153                                  * NOTE! enumvals with double quotes in them are not
7154                                  * supported!
7155                                  */
7156                                 values[11] = config_enum_get_options((struct config_enum *) conf,
7157                                                                                                          "{\"", "\"}", "\",\"");
7158
7159                                 /* boot_val */
7160                                 values[12] = pstrdup(config_enum_lookup_by_value(lconf,
7161                                                                                                                    lconf->boot_val));
7162
7163                                 /* reset_val */
7164                                 values[13] = pstrdup(config_enum_lookup_by_value(lconf,
7165                                                                                                                   lconf->reset_val));
7166                         }
7167                         break;
7168
7169                 default:
7170                         {
7171                                 /*
7172                                  * should never get here, but in case we do, set 'em to NULL
7173                                  */
7174
7175                                 /* min_val */
7176                                 values[9] = NULL;
7177
7178                                 /* max_val */
7179                                 values[10] = NULL;
7180
7181                                 /* enumvals */
7182                                 values[11] = NULL;
7183
7184                                 /* boot_val */
7185                                 values[12] = NULL;
7186
7187                                 /* reset_val */
7188                                 values[13] = NULL;
7189                         }
7190                         break;
7191         }
7192
7193         /*
7194          * If the setting came from a config file, set the source location. For
7195          * security reasons, we don't show source file/line number for
7196          * non-superusers.
7197          */
7198         if (conf->source == PGC_S_FILE && superuser())
7199         {
7200                 values[14] = conf->sourcefile;
7201                 snprintf(buffer, sizeof(buffer), "%d", conf->sourceline);
7202                 values[15] = pstrdup(buffer);
7203         }
7204         else
7205         {
7206                 values[14] = NULL;
7207                 values[15] = NULL;
7208         }
7209 }
7210
7211 /*
7212  * Return the total number of GUC variables
7213  */
7214 int
7215 GetNumConfigOptions(void)
7216 {
7217         return num_guc_variables;
7218 }
7219
7220 /*
7221  * show_config_by_name - equiv to SHOW X command but implemented as
7222  * a function.
7223  */
7224 Datum
7225 show_config_by_name(PG_FUNCTION_ARGS)
7226 {
7227         char       *varname;
7228         char       *varval;
7229
7230         /* Get the GUC variable name */
7231         varname = TextDatumGetCString(PG_GETARG_DATUM(0));
7232
7233         /* Get the value */
7234         varval = GetConfigOptionByName(varname, NULL);
7235
7236         /* Convert to text */
7237         PG_RETURN_TEXT_P(cstring_to_text(varval));
7238 }
7239
7240 /*
7241  * show_all_settings - equiv to SHOW ALL command but implemented as
7242  * a Table Function.
7243  */
7244 #define NUM_PG_SETTINGS_ATTS    16
7245
7246 Datum
7247 show_all_settings(PG_FUNCTION_ARGS)
7248 {
7249         FuncCallContext *funcctx;
7250         TupleDesc       tupdesc;
7251         int                     call_cntr;
7252         int                     max_calls;
7253         AttInMetadata *attinmeta;
7254         MemoryContext oldcontext;
7255
7256         /* stuff done only on the first call of the function */
7257         if (SRF_IS_FIRSTCALL())
7258         {
7259                 /* create a function context for cross-call persistence */
7260                 funcctx = SRF_FIRSTCALL_INIT();
7261
7262                 /*
7263                  * switch to memory context appropriate for multiple function calls
7264                  */
7265                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
7266
7267                 /*
7268                  * need a tuple descriptor representing NUM_PG_SETTINGS_ATTS columns
7269                  * of the appropriate types
7270                  */
7271                 tupdesc = CreateTemplateTupleDesc(NUM_PG_SETTINGS_ATTS, false);
7272                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
7273                                                    TEXTOID, -1, 0);
7274                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
7275                                                    TEXTOID, -1, 0);
7276                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "unit",
7277                                                    TEXTOID, -1, 0);
7278                 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "category",
7279                                                    TEXTOID, -1, 0);
7280                 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "short_desc",
7281                                                    TEXTOID, -1, 0);
7282                 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "extra_desc",
7283                                                    TEXTOID, -1, 0);
7284                 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "context",
7285                                                    TEXTOID, -1, 0);
7286                 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "vartype",
7287                                                    TEXTOID, -1, 0);
7288                 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "source",
7289                                                    TEXTOID, -1, 0);
7290                 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "min_val",
7291                                                    TEXTOID, -1, 0);
7292                 TupleDescInitEntry(tupdesc, (AttrNumber) 11, "max_val",
7293                                                    TEXTOID, -1, 0);
7294                 TupleDescInitEntry(tupdesc, (AttrNumber) 12, "enumvals",
7295                                                    TEXTARRAYOID, -1, 0);
7296                 TupleDescInitEntry(tupdesc, (AttrNumber) 13, "boot_val",
7297                                                    TEXTOID, -1, 0);
7298                 TupleDescInitEntry(tupdesc, (AttrNumber) 14, "reset_val",
7299                                                    TEXTOID, -1, 0);
7300                 TupleDescInitEntry(tupdesc, (AttrNumber) 15, "sourcefile",
7301                                                    TEXTOID, -1, 0);
7302                 TupleDescInitEntry(tupdesc, (AttrNumber) 16, "sourceline",
7303                                                    INT4OID, -1, 0);
7304
7305                 /*
7306                  * Generate attribute metadata needed later to produce tuples from raw
7307                  * C strings
7308                  */
7309                 attinmeta = TupleDescGetAttInMetadata(tupdesc);
7310                 funcctx->attinmeta = attinmeta;
7311
7312                 /* total number of tuples to be returned */
7313                 funcctx->max_calls = GetNumConfigOptions();
7314
7315                 MemoryContextSwitchTo(oldcontext);
7316         }
7317
7318         /* stuff done on every call of the function */
7319         funcctx = SRF_PERCALL_SETUP();
7320
7321         call_cntr = funcctx->call_cntr;
7322         max_calls = funcctx->max_calls;
7323         attinmeta = funcctx->attinmeta;
7324
7325         if (call_cntr < max_calls)      /* do when there is more left to send */
7326         {
7327                 char       *values[NUM_PG_SETTINGS_ATTS];
7328                 bool            noshow;
7329                 HeapTuple       tuple;
7330                 Datum           result;
7331
7332                 /*
7333                  * Get the next visible GUC variable name and value
7334                  */
7335                 do
7336                 {
7337                         GetConfigOptionByNum(call_cntr, (const char **) values, &noshow);
7338                         if (noshow)
7339                         {
7340                                 /* bump the counter and get the next config setting */
7341                                 call_cntr = ++funcctx->call_cntr;
7342
7343                                 /* make sure we haven't gone too far now */
7344                                 if (call_cntr >= max_calls)
7345                                         SRF_RETURN_DONE(funcctx);
7346                         }
7347                 } while (noshow);
7348
7349                 /* build a tuple */
7350                 tuple = BuildTupleFromCStrings(attinmeta, values);
7351
7352                 /* make the tuple into a datum */
7353                 result = HeapTupleGetDatum(tuple);
7354
7355                 SRF_RETURN_NEXT(funcctx, result);
7356         }
7357         else
7358         {
7359                 /* do when there is no more left */
7360                 SRF_RETURN_DONE(funcctx);
7361         }
7362 }
7363
7364 static char *
7365 _ShowOption(struct config_generic * record, bool use_units)
7366 {
7367         char            buffer[256];
7368         const char *val;
7369
7370         switch (record->vartype)
7371         {
7372                 case PGC_BOOL:
7373                         {
7374                                 struct config_bool *conf = (struct config_bool *) record;
7375
7376                                 if (conf->show_hook)
7377                                         val = (*conf->show_hook) ();
7378                                 else
7379                                         val = *conf->variable ? "on" : "off";
7380                         }
7381                         break;
7382
7383                 case PGC_INT:
7384                         {
7385                                 struct config_int *conf = (struct config_int *) record;
7386
7387                                 if (conf->show_hook)
7388                                         val = (*conf->show_hook) ();
7389                                 else
7390                                 {
7391                                         /*
7392                                          * Use int64 arithmetic to avoid overflows in units
7393                                          * conversion.
7394                                          */
7395                                         int64           result = *conf->variable;
7396                                         const char *unit;
7397
7398                                         if (use_units && result > 0 &&
7399                                                 (record->flags & GUC_UNIT_MEMORY))
7400                                         {
7401                                                 switch (record->flags & GUC_UNIT_MEMORY)
7402                                                 {
7403                                                         case GUC_UNIT_BLOCKS:
7404                                                                 result *= BLCKSZ / 1024;
7405                                                                 break;
7406                                                         case GUC_UNIT_XBLOCKS:
7407                                                                 result *= XLOG_BLCKSZ / 1024;
7408                                                                 break;
7409                                                 }
7410
7411                                                 if (result % KB_PER_GB == 0)
7412                                                 {
7413                                                         result /= KB_PER_GB;
7414                                                         unit = "GB";
7415                                                 }
7416                                                 else if (result % KB_PER_MB == 0)
7417                                                 {
7418                                                         result /= KB_PER_MB;
7419                                                         unit = "MB";
7420                                                 }
7421                                                 else
7422                                                 {
7423                                                         unit = "kB";
7424                                                 }
7425                                         }
7426                                         else if (use_units && result > 0 &&
7427                                                          (record->flags & GUC_UNIT_TIME))
7428                                         {
7429                                                 switch (record->flags & GUC_UNIT_TIME)
7430                                                 {
7431                                                         case GUC_UNIT_S:
7432                                                                 result *= MS_PER_S;
7433                                                                 break;
7434                                                         case GUC_UNIT_MIN:
7435                                                                 result *= MS_PER_MIN;
7436                                                                 break;
7437                                                 }
7438
7439                                                 if (result % MS_PER_D == 0)
7440                                                 {
7441                                                         result /= MS_PER_D;
7442                                                         unit = "d";
7443                                                 }
7444                                                 else if (result % MS_PER_H == 0)
7445                                                 {
7446                                                         result /= MS_PER_H;
7447                                                         unit = "h";
7448                                                 }
7449                                                 else if (result % MS_PER_MIN == 0)
7450                                                 {
7451                                                         result /= MS_PER_MIN;
7452                                                         unit = "min";
7453                                                 }
7454                                                 else if (result % MS_PER_S == 0)
7455                                                 {
7456                                                         result /= MS_PER_S;
7457                                                         unit = "s";
7458                                                 }
7459                                                 else
7460                                                 {
7461                                                         unit = "ms";
7462                                                 }
7463                                         }
7464                                         else
7465                                                 unit = "";
7466
7467                                         snprintf(buffer, sizeof(buffer), INT64_FORMAT "%s",
7468                                                          result, unit);
7469                                         val = buffer;
7470                                 }
7471                         }
7472                         break;
7473
7474                 case PGC_REAL:
7475                         {
7476                                 struct config_real *conf = (struct config_real *) record;
7477
7478                                 if (conf->show_hook)
7479                                         val = (*conf->show_hook) ();
7480                                 else
7481                                 {
7482                                         snprintf(buffer, sizeof(buffer), "%g",
7483                                                          *conf->variable);
7484                                         val = buffer;
7485                                 }
7486                         }
7487                         break;
7488
7489                 case PGC_STRING:
7490                         {
7491                                 struct config_string *conf = (struct config_string *) record;
7492
7493                                 if (conf->show_hook)
7494                                         val = (*conf->show_hook) ();
7495                                 else if (*conf->variable && **conf->variable)
7496                                         val = *conf->variable;
7497                                 else
7498                                         val = "";
7499                         }
7500                         break;
7501
7502                 case PGC_ENUM:
7503                         {
7504                                 struct config_enum *conf = (struct config_enum *) record;
7505
7506                                 if (conf->show_hook)
7507                                         val = (*conf->show_hook) ();
7508                                 else
7509                                         val = config_enum_lookup_by_value(conf, *conf->variable);
7510                         }
7511                         break;
7512
7513                 default:
7514                         /* just to keep compiler quiet */
7515                         val = "???";
7516                         break;
7517         }
7518
7519         return pstrdup(val);
7520 }
7521
7522
7523 #ifdef EXEC_BACKEND
7524
7525 /*
7526  *      These routines dump out all non-default GUC options into a binary
7527  *      file that is read by all exec'ed backends.  The format is:
7528  *
7529  *              variable name, string, null terminated
7530  *              variable value, string, null terminated
7531  *              variable sourcefile, string, null terminated (empty if none)
7532  *              variable sourceline, integer
7533  *              variable source, integer
7534  *              variable scontext, integer
7535  */
7536 static void
7537 write_one_nondefault_variable(FILE *fp, struct config_generic * gconf)
7538 {
7539         if (gconf->source == PGC_S_DEFAULT)
7540                 return;
7541
7542         fprintf(fp, "%s", gconf->name);
7543         fputc(0, fp);
7544
7545         switch (gconf->vartype)
7546         {
7547                 case PGC_BOOL:
7548                         {
7549                                 struct config_bool *conf = (struct config_bool *) gconf;
7550
7551                                 if (*conf->variable)
7552                                         fprintf(fp, "true");
7553                                 else
7554                                         fprintf(fp, "false");
7555                         }
7556                         break;
7557
7558                 case PGC_INT:
7559                         {
7560                                 struct config_int *conf = (struct config_int *) gconf;
7561
7562                                 fprintf(fp, "%d", *conf->variable);
7563                         }
7564                         break;
7565
7566                 case PGC_REAL:
7567                         {
7568                                 struct config_real *conf = (struct config_real *) gconf;
7569
7570                                 fprintf(fp, "%.17g", *conf->variable);
7571                         }
7572                         break;
7573
7574                 case PGC_STRING:
7575                         {
7576                                 struct config_string *conf = (struct config_string *) gconf;
7577
7578                                 fprintf(fp, "%s", *conf->variable);
7579                         }
7580                         break;
7581
7582                 case PGC_ENUM:
7583                         {
7584                                 struct config_enum *conf = (struct config_enum *) gconf;
7585
7586                                 fprintf(fp, "%s",
7587                                                 config_enum_lookup_by_value(conf, *conf->variable));
7588                         }
7589                         break;
7590         }
7591
7592         fputc(0, fp);
7593
7594         if (gconf->sourcefile)
7595                 fprintf(fp, "%s", gconf->sourcefile);
7596         fputc(0, fp);
7597
7598         fwrite(&gconf->sourceline, 1, sizeof(gconf->sourceline), fp);
7599         fwrite(&gconf->source, 1, sizeof(gconf->source), fp);
7600         fwrite(&gconf->scontext, 1, sizeof(gconf->scontext), fp);
7601 }
7602
7603 void
7604 write_nondefault_variables(GucContext context)
7605 {
7606         int                     elevel;
7607         FILE       *fp;
7608         int                     i;
7609
7610         Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
7611
7612         elevel = (context == PGC_SIGHUP) ? LOG : ERROR;
7613
7614         /*
7615          * Open file
7616          */
7617         fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, "w");
7618         if (!fp)
7619         {
7620                 ereport(elevel,
7621                                 (errcode_for_file_access(),
7622                                  errmsg("could not write to file \"%s\": %m",
7623                                                 CONFIG_EXEC_PARAMS_NEW)));
7624                 return;
7625         }
7626
7627         for (i = 0; i < num_guc_variables; i++)
7628         {
7629                 write_one_nondefault_variable(fp, guc_variables[i]);
7630         }
7631
7632         if (FreeFile(fp))
7633         {
7634                 ereport(elevel,
7635                                 (errcode_for_file_access(),
7636                                  errmsg("could not write to file \"%s\": %m",
7637                                                 CONFIG_EXEC_PARAMS_NEW)));
7638                 return;
7639         }
7640
7641         /*
7642          * Put new file in place.  This could delay on Win32, but we don't hold
7643          * any exclusive locks.
7644          */
7645         rename(CONFIG_EXEC_PARAMS_NEW, CONFIG_EXEC_PARAMS);
7646 }
7647
7648
7649 /*
7650  *      Read string, including null byte from file
7651  *
7652  *      Return NULL on EOF and nothing read
7653  */
7654 static char *
7655 read_string_with_null(FILE *fp)
7656 {
7657         int                     i = 0,
7658                                 ch,
7659                                 maxlen = 256;
7660         char       *str = NULL;
7661
7662         do
7663         {
7664                 if ((ch = fgetc(fp)) == EOF)
7665                 {
7666                         if (i == 0)
7667                                 return NULL;
7668                         else
7669                                 elog(FATAL, "invalid format of exec config params file");
7670                 }
7671                 if (i == 0)
7672                         str = guc_malloc(FATAL, maxlen);
7673                 else if (i == maxlen)
7674                         str = guc_realloc(FATAL, str, maxlen *= 2);
7675                 str[i++] = ch;
7676         } while (ch != 0);
7677
7678         return str;
7679 }
7680
7681
7682 /*
7683  *      This routine loads a previous postmaster dump of its non-default
7684  *      settings.
7685  */
7686 void
7687 read_nondefault_variables(void)
7688 {
7689         FILE       *fp;
7690         char       *varname,
7691                            *varvalue,
7692                            *varsourcefile;
7693         int                     varsourceline;
7694         GucSource       varsource;
7695         GucContext      varscontext;
7696
7697         /*
7698          * Open file
7699          */
7700         fp = AllocateFile(CONFIG_EXEC_PARAMS, "r");
7701         if (!fp)
7702         {
7703                 /* File not found is fine */
7704                 if (errno != ENOENT)
7705                         ereport(FATAL,
7706                                         (errcode_for_file_access(),
7707                                          errmsg("could not read from file \"%s\": %m",
7708                                                         CONFIG_EXEC_PARAMS)));
7709                 return;
7710         }
7711
7712         for (;;)
7713         {
7714                 struct config_generic *record;
7715
7716                 if ((varname = read_string_with_null(fp)) == NULL)
7717                         break;
7718
7719                 if ((record = find_option(varname, true, FATAL)) == NULL)
7720                         elog(FATAL, "failed to locate variable \"%s\" in exec config params file", varname);
7721
7722                 if ((varvalue = read_string_with_null(fp)) == NULL)
7723                         elog(FATAL, "invalid format of exec config params file");
7724                 if ((varsourcefile = read_string_with_null(fp)) == NULL)
7725                         elog(FATAL, "invalid format of exec config params file");
7726                 if (fread(&varsourceline, 1, sizeof(varsourceline), fp) != sizeof(varsourceline))
7727                         elog(FATAL, "invalid format of exec config params file");
7728                 if (fread(&varsource, 1, sizeof(varsource), fp) != sizeof(varsource))
7729                         elog(FATAL, "invalid format of exec config params file");
7730                 if (fread(&varscontext, 1, sizeof(varscontext), fp) != sizeof(varscontext))
7731                         elog(FATAL, "invalid format of exec config params file");
7732
7733                 (void) set_config_option(varname, varvalue,
7734                                                                  varscontext, varsource,
7735                                                                  GUC_ACTION_SET, true, 0);
7736                 if (varsourcefile[0])
7737                         set_config_sourcefile(varname, varsourcefile, varsourceline);
7738
7739                 free(varname);
7740                 free(varvalue);
7741                 free(varsourcefile);
7742         }
7743
7744         FreeFile(fp);
7745 }
7746 #endif   /* EXEC_BACKEND */
7747
7748
7749 /*
7750  * A little "long argument" simulation, although not quite GNU
7751  * compliant. Takes a string of the form "some-option=some value" and
7752  * returns name = "some_option" and value = "some value" in malloc'ed
7753  * storage. Note that '-' is converted to '_' in the option name. If
7754  * there is no '=' in the input string then value will be NULL.
7755  */
7756 void
7757 ParseLongOption(const char *string, char **name, char **value)
7758 {
7759         size_t          equal_pos;
7760         char       *cp;
7761
7762         AssertArg(string);
7763         AssertArg(name);
7764         AssertArg(value);
7765
7766         equal_pos = strcspn(string, "=");
7767
7768         if (string[equal_pos] == '=')
7769         {
7770                 *name = guc_malloc(FATAL, equal_pos + 1);
7771                 strlcpy(*name, string, equal_pos + 1);
7772
7773                 *value = guc_strdup(FATAL, &string[equal_pos + 1]);
7774         }
7775         else
7776         {
7777                 /* no equal sign in string */
7778                 *name = guc_strdup(FATAL, string);
7779                 *value = NULL;
7780         }
7781
7782         for (cp = *name; *cp; cp++)
7783                 if (*cp == '-')
7784                         *cp = '_';
7785 }
7786
7787
7788 /*
7789  * Handle options fetched from pg_db_role_setting.setconfig,
7790  * pg_proc.proconfig, etc.      Caller must specify proper context/source/action.
7791  *
7792  * The array parameter must be an array of TEXT (it must not be NULL).
7793  */
7794 void
7795 ProcessGUCArray(ArrayType *array,
7796                                 GucContext context, GucSource source, GucAction action)
7797 {
7798         int                     i;
7799
7800         Assert(array != NULL);
7801         Assert(ARR_ELEMTYPE(array) == TEXTOID);
7802         Assert(ARR_NDIM(array) == 1);
7803         Assert(ARR_LBOUND(array)[0] == 1);
7804
7805         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
7806         {
7807                 Datum           d;
7808                 bool            isnull;
7809                 char       *s;
7810                 char       *name;
7811                 char       *value;
7812
7813                 d = array_ref(array, 1, &i,
7814                                           -1 /* varlenarray */ ,
7815                                           -1 /* TEXT's typlen */ ,
7816                                           false /* TEXT's typbyval */ ,
7817                                           'i' /* TEXT's typalign */ ,
7818                                           &isnull);
7819
7820                 if (isnull)
7821                         continue;
7822
7823                 s = TextDatumGetCString(d);
7824
7825                 ParseLongOption(s, &name, &value);
7826                 if (!value)
7827                 {
7828                         ereport(WARNING,
7829                                         (errcode(ERRCODE_SYNTAX_ERROR),
7830                                          errmsg("could not parse setting for parameter \"%s\"",
7831                                                         name)));
7832                         free(name);
7833                         continue;
7834                 }
7835
7836                 (void) set_config_option(name, value,
7837                                                                  context, source,
7838                                                                  action, true, 0);
7839
7840                 free(name);
7841                 if (value)
7842                         free(value);
7843                 pfree(s);
7844         }
7845 }
7846
7847
7848 /*
7849  * Add an entry to an option array.  The array parameter may be NULL
7850  * to indicate the current table entry is NULL.
7851  */
7852 ArrayType *
7853 GUCArrayAdd(ArrayType *array, const char *name, const char *value)
7854 {
7855         struct config_generic *record;
7856         Datum           datum;
7857         char       *newval;
7858         ArrayType  *a;
7859
7860         Assert(name);
7861         Assert(value);
7862
7863         /* test if the option is valid and we're allowed to set it */
7864         (void) validate_option_array_item(name, value, false);
7865
7866         /* normalize name (converts obsolete GUC names to modern spellings) */
7867         record = find_option(name, false, WARNING);
7868         if (record)
7869                 name = record->name;
7870
7871         /* build new item for array */
7872         newval = palloc(strlen(name) + 1 + strlen(value) + 1);
7873         sprintf(newval, "%s=%s", name, value);
7874         datum = CStringGetTextDatum(newval);
7875
7876         if (array)
7877         {
7878                 int                     index;
7879                 bool            isnull;
7880                 int                     i;
7881
7882                 Assert(ARR_ELEMTYPE(array) == TEXTOID);
7883                 Assert(ARR_NDIM(array) == 1);
7884                 Assert(ARR_LBOUND(array)[0] == 1);
7885
7886                 index = ARR_DIMS(array)[0] + 1; /* add after end */
7887
7888                 for (i = 1; i <= ARR_DIMS(array)[0]; i++)
7889                 {
7890                         Datum           d;
7891                         char       *current;
7892
7893                         d = array_ref(array, 1, &i,
7894                                                   -1 /* varlenarray */ ,
7895                                                   -1 /* TEXT's typlen */ ,
7896                                                   false /* TEXT's typbyval */ ,
7897                                                   'i' /* TEXT's typalign */ ,
7898                                                   &isnull);
7899                         if (isnull)
7900                                 continue;
7901                         current = TextDatumGetCString(d);
7902
7903                         /* check for match up through and including '=' */
7904                         if (strncmp(current, newval, strlen(name) + 1) == 0)
7905                         {
7906                                 index = i;
7907                                 break;
7908                         }
7909                 }
7910
7911                 a = array_set(array, 1, &index,
7912                                           datum,
7913                                           false,
7914                                           -1 /* varlena array */ ,
7915                                           -1 /* TEXT's typlen */ ,
7916                                           false /* TEXT's typbyval */ ,
7917                                           'i' /* TEXT's typalign */ );
7918         }
7919         else
7920                 a = construct_array(&datum, 1,
7921                                                         TEXTOID,
7922                                                         -1, false, 'i');
7923
7924         return a;
7925 }
7926
7927
7928 /*
7929  * Delete an entry from an option array.  The array parameter may be NULL
7930  * to indicate the current table entry is NULL.  Also, if the return value
7931  * is NULL then a null should be stored.
7932  */
7933 ArrayType *
7934 GUCArrayDelete(ArrayType *array, const char *name)
7935 {
7936         struct config_generic *record;
7937         ArrayType  *newarray;
7938         int                     i;
7939         int                     index;
7940
7941         Assert(name);
7942
7943         /* test if the option is valid and we're allowed to set it */
7944         (void) validate_option_array_item(name, NULL, false);
7945
7946         /* normalize name (converts obsolete GUC names to modern spellings) */
7947         record = find_option(name, false, WARNING);
7948         if (record)
7949                 name = record->name;
7950
7951         /* if array is currently null, then surely nothing to delete */
7952         if (!array)
7953                 return NULL;
7954
7955         newarray = NULL;
7956         index = 1;
7957
7958         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
7959         {
7960                 Datum           d;
7961                 char       *val;
7962                 bool            isnull;
7963
7964                 d = array_ref(array, 1, &i,
7965                                           -1 /* varlenarray */ ,
7966                                           -1 /* TEXT's typlen */ ,
7967                                           false /* TEXT's typbyval */ ,
7968                                           'i' /* TEXT's typalign */ ,
7969                                           &isnull);
7970                 if (isnull)
7971                         continue;
7972                 val = TextDatumGetCString(d);
7973
7974                 /* ignore entry if it's what we want to delete */
7975                 if (strncmp(val, name, strlen(name)) == 0
7976                         && val[strlen(name)] == '=')
7977                         continue;
7978
7979                 /* else add it to the output array */
7980                 if (newarray)
7981                         newarray = array_set(newarray, 1, &index,
7982                                                                  d,
7983                                                                  false,
7984                                                                  -1 /* varlenarray */ ,
7985                                                                  -1 /* TEXT's typlen */ ,
7986                                                                  false /* TEXT's typbyval */ ,
7987                                                                  'i' /* TEXT's typalign */ );
7988                 else
7989                         newarray = construct_array(&d, 1,
7990                                                                            TEXTOID,
7991                                                                            -1, false, 'i');
7992
7993                 index++;
7994         }
7995
7996         return newarray;
7997 }
7998
7999
8000 /*
8001  * Given a GUC array, delete all settings from it that our permission
8002  * level allows: if superuser, delete them all; if regular user, only
8003  * those that are PGC_USERSET
8004  */
8005 ArrayType *
8006 GUCArrayReset(ArrayType *array)
8007 {
8008         ArrayType  *newarray;
8009         int                     i;
8010         int                     index;
8011
8012         /* if array is currently null, nothing to do */
8013         if (!array)
8014                 return NULL;
8015
8016         /* if we're superuser, we can delete everything, so just do it */
8017         if (superuser())
8018                 return NULL;
8019
8020         newarray = NULL;
8021         index = 1;
8022
8023         for (i = 1; i <= ARR_DIMS(array)[0]; i++)
8024         {
8025                 Datum           d;
8026                 char       *val;
8027                 char       *eqsgn;
8028                 bool            isnull;
8029
8030                 d = array_ref(array, 1, &i,
8031                                           -1 /* varlenarray */ ,
8032                                           -1 /* TEXT's typlen */ ,
8033                                           false /* TEXT's typbyval */ ,
8034                                           'i' /* TEXT's typalign */ ,
8035                                           &isnull);
8036                 if (isnull)
8037                         continue;
8038                 val = TextDatumGetCString(d);
8039
8040                 eqsgn = strchr(val, '=');
8041                 *eqsgn = '\0';
8042
8043                 /* skip if we have permission to delete it */
8044                 if (validate_option_array_item(val, NULL, true))
8045                         continue;
8046
8047                 /* else add it to the output array */
8048                 if (newarray)
8049                         newarray = array_set(newarray, 1, &index,
8050                                                                  d,
8051                                                                  false,
8052                                                                  -1 /* varlenarray */ ,
8053                                                                  -1 /* TEXT's typlen */ ,
8054                                                                  false /* TEXT's typbyval */ ,
8055                                                                  'i' /* TEXT's typalign */ );
8056                 else
8057                         newarray = construct_array(&d, 1,
8058                                                                            TEXTOID,
8059                                                                            -1, false, 'i');
8060
8061                 index++;
8062                 pfree(val);
8063         }
8064
8065         return newarray;
8066 }
8067
8068 /*
8069  * Validate a proposed option setting for GUCArrayAdd/Delete/Reset.
8070  *
8071  * name is the option name.  value is the proposed value for the Add case,
8072  * or NULL for the Delete/Reset cases.  If skipIfNoPermissions is true, it's
8073  * not an error to have no permissions to set the option.
8074  *
8075  * Returns TRUE if OK, FALSE if skipIfNoPermissions is true and user does not
8076  * have permission to change this option (all other error cases result in an
8077  * error being thrown).
8078  */
8079 static bool
8080 validate_option_array_item(const char *name, const char *value,
8081                                                    bool skipIfNoPermissions)
8082
8083 {
8084         struct config_generic *gconf;
8085
8086         /*
8087          * There are three cases to consider:
8088          *
8089          * name is a known GUC variable.  Check the value normally, check
8090          * permissions normally (ie, allow if variable is USERSET, or if it's
8091          * SUSET and user is superuser).
8092          *
8093          * name is not known, but exists or can be created as a placeholder (i.e.,
8094          * it has a prefixed name).  We allow this case if you're a superuser,
8095          * otherwise not.  Superusers are assumed to know what they're doing. We
8096          * can't allow it for other users, because when the placeholder is
8097          * resolved it might turn out to be a SUSET variable;
8098          * define_custom_variable assumes we checked that.
8099          *
8100          * name is not known and can't be created as a placeholder.  Throw error,
8101          * unless skipIfNoPermissions is true, in which case return FALSE.
8102          */
8103         gconf = find_option(name, true, WARNING);
8104         if (!gconf)
8105         {
8106                 /* not known, failed to make a placeholder */
8107                 if (skipIfNoPermissions)
8108                         return false;
8109                 ereport(ERROR,
8110                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
8111                                  errmsg("unrecognized configuration parameter \"%s\"",
8112                                                 name)));
8113         }
8114
8115         if (gconf->flags & GUC_CUSTOM_PLACEHOLDER)
8116         {
8117                 /*
8118                  * We cannot do any meaningful check on the value, so only permissions
8119                  * are useful to check.
8120                  */
8121                 if (superuser())
8122                         return true;
8123                 if (skipIfNoPermissions)
8124                         return false;
8125                 ereport(ERROR,
8126                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
8127                                  errmsg("permission denied to set parameter \"%s\"", name)));
8128         }
8129
8130         /* manual permissions check so we can avoid an error being thrown */
8131         if (gconf->context == PGC_USERSET)
8132                  /* ok */ ;
8133         else if (gconf->context == PGC_SUSET && superuser())
8134                  /* ok */ ;
8135         else if (skipIfNoPermissions)
8136                 return false;
8137         /* if a permissions error should be thrown, let set_config_option do it */
8138
8139         /* test for permissions and valid option value */
8140         (void) set_config_option(name, value,
8141                                                          superuser() ? PGC_SUSET : PGC_USERSET,
8142                                                          PGC_S_TEST, GUC_ACTION_SET, false, 0);
8143
8144         return true;
8145 }
8146
8147
8148 /*
8149  * Called by check_hooks that want to override the normal
8150  * ERRCODE_INVALID_PARAMETER_VALUE SQLSTATE for check hook failures.
8151  *
8152  * Note that GUC_check_errmsg() etc are just macros that result in a direct
8153  * assignment to the associated variables.      That is ugly, but forced by the
8154  * limitations of C's macro mechanisms.
8155  */
8156 void
8157 GUC_check_errcode(int sqlerrcode)
8158 {
8159         GUC_check_errcode_value = sqlerrcode;
8160 }
8161
8162
8163 /*
8164  * Convenience functions to manage calling a variable's check_hook.
8165  * These mostly take care of the protocol for letting check hooks supply
8166  * portions of the error report on failure.
8167  */
8168
8169 static bool
8170 call_bool_check_hook(struct config_bool * conf, bool *newval, void **extra,
8171                                          GucSource source, int elevel)
8172 {
8173         /* Quick success if no hook */
8174         if (!conf->check_hook)
8175                 return true;
8176
8177         /* Reset variables that might be set by hook */
8178         GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
8179         GUC_check_errmsg_string = NULL;
8180         GUC_check_errdetail_string = NULL;
8181         GUC_check_errhint_string = NULL;
8182
8183         if (!(*conf->check_hook) (newval, extra, source))
8184         {
8185                 ereport(elevel,
8186                                 (errcode(GUC_check_errcode_value),
8187                                  GUC_check_errmsg_string ?
8188                                  errmsg_internal("%s", GUC_check_errmsg_string) :
8189                                  errmsg("invalid value for parameter \"%s\": %d",
8190                                                 conf->gen.name, (int) *newval),
8191                                  GUC_check_errdetail_string ?
8192                                  errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8193                                  GUC_check_errhint_string ?
8194                                  errhint("%s", GUC_check_errhint_string) : 0));
8195                 /* Flush any strings created in ErrorContext */
8196                 FlushErrorState();
8197                 return false;
8198         }
8199
8200         return true;
8201 }
8202
8203 static bool
8204 call_int_check_hook(struct config_int * conf, int *newval, void **extra,
8205                                         GucSource source, int elevel)
8206 {
8207         /* Quick success if no hook */
8208         if (!conf->check_hook)
8209                 return true;
8210
8211         /* Reset variables that might be set by hook */
8212         GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
8213         GUC_check_errmsg_string = NULL;
8214         GUC_check_errdetail_string = NULL;
8215         GUC_check_errhint_string = NULL;
8216
8217         if (!(*conf->check_hook) (newval, extra, source))
8218         {
8219                 ereport(elevel,
8220                                 (errcode(GUC_check_errcode_value),
8221                                  GUC_check_errmsg_string ?
8222                                  errmsg_internal("%s", GUC_check_errmsg_string) :
8223                                  errmsg("invalid value for parameter \"%s\": %d",
8224                                                 conf->gen.name, *newval),
8225                                  GUC_check_errdetail_string ?
8226                                  errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8227                                  GUC_check_errhint_string ?
8228                                  errhint("%s", GUC_check_errhint_string) : 0));
8229                 /* Flush any strings created in ErrorContext */
8230                 FlushErrorState();
8231                 return false;
8232         }
8233
8234         return true;
8235 }
8236
8237 static bool
8238 call_real_check_hook(struct config_real * conf, double *newval, void **extra,
8239                                          GucSource source, int elevel)
8240 {
8241         /* Quick success if no hook */
8242         if (!conf->check_hook)
8243                 return true;
8244
8245         /* Reset variables that might be set by hook */
8246         GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
8247         GUC_check_errmsg_string = NULL;
8248         GUC_check_errdetail_string = NULL;
8249         GUC_check_errhint_string = NULL;
8250
8251         if (!(*conf->check_hook) (newval, extra, source))
8252         {
8253                 ereport(elevel,
8254                                 (errcode(GUC_check_errcode_value),
8255                                  GUC_check_errmsg_string ?
8256                                  errmsg_internal("%s", GUC_check_errmsg_string) :
8257                                  errmsg("invalid value for parameter \"%s\": %g",
8258                                                 conf->gen.name, *newval),
8259                                  GUC_check_errdetail_string ?
8260                                  errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8261                                  GUC_check_errhint_string ?
8262                                  errhint("%s", GUC_check_errhint_string) : 0));
8263                 /* Flush any strings created in ErrorContext */
8264                 FlushErrorState();
8265                 return false;
8266         }
8267
8268         return true;
8269 }
8270
8271 static bool
8272 call_string_check_hook(struct config_string * conf, char **newval, void **extra,
8273                                            GucSource source, int elevel)
8274 {
8275         /* Quick success if no hook */
8276         if (!conf->check_hook)
8277                 return true;
8278
8279         /* Reset variables that might be set by hook */
8280         GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
8281         GUC_check_errmsg_string = NULL;
8282         GUC_check_errdetail_string = NULL;
8283         GUC_check_errhint_string = NULL;
8284
8285         if (!(*conf->check_hook) (newval, extra, source))
8286         {
8287                 ereport(elevel,
8288                                 (errcode(GUC_check_errcode_value),
8289                                  GUC_check_errmsg_string ?
8290                                  errmsg_internal("%s", GUC_check_errmsg_string) :
8291                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
8292                                                 conf->gen.name, *newval ? *newval : ""),
8293                                  GUC_check_errdetail_string ?
8294                                  errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8295                                  GUC_check_errhint_string ?
8296                                  errhint("%s", GUC_check_errhint_string) : 0));
8297                 /* Flush any strings created in ErrorContext */
8298                 FlushErrorState();
8299                 return false;
8300         }
8301
8302         return true;
8303 }
8304
8305 static bool
8306 call_enum_check_hook(struct config_enum * conf, int *newval, void **extra,
8307                                          GucSource source, int elevel)
8308 {
8309         /* Quick success if no hook */
8310         if (!conf->check_hook)
8311                 return true;
8312
8313         /* Reset variables that might be set by hook */
8314         GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
8315         GUC_check_errmsg_string = NULL;
8316         GUC_check_errdetail_string = NULL;
8317         GUC_check_errhint_string = NULL;
8318
8319         if (!(*conf->check_hook) (newval, extra, source))
8320         {
8321                 ereport(elevel,
8322                                 (errcode(GUC_check_errcode_value),
8323                                  GUC_check_errmsg_string ?
8324                                  errmsg_internal("%s", GUC_check_errmsg_string) :
8325                                  errmsg("invalid value for parameter \"%s\": \"%s\"",
8326                                                 conf->gen.name,
8327                                                 config_enum_lookup_by_value(conf, *newval)),
8328                                  GUC_check_errdetail_string ?
8329                                  errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8330                                  GUC_check_errhint_string ?
8331                                  errhint("%s", GUC_check_errhint_string) : 0));
8332                 /* Flush any strings created in ErrorContext */
8333                 FlushErrorState();
8334                 return false;
8335         }
8336
8337         return true;
8338 }
8339
8340
8341 /*
8342  * check_hook, assign_hook and show_hook subroutines
8343  */
8344
8345 static bool
8346 check_log_destination(char **newval, void **extra, GucSource source)
8347 {
8348         char       *rawstring;
8349         List       *elemlist;
8350         ListCell   *l;
8351         int                     newlogdest = 0;
8352         int                *myextra;
8353
8354         /* Need a modifiable copy of string */
8355         rawstring = pstrdup(*newval);
8356
8357         /* Parse string into list of identifiers */
8358         if (!SplitIdentifierString(rawstring, ',', &elemlist))
8359         {
8360                 /* syntax error in list */
8361                 GUC_check_errdetail("List syntax is invalid.");
8362                 pfree(rawstring);
8363                 list_free(elemlist);
8364                 return false;
8365         }
8366
8367         foreach(l, elemlist)
8368         {
8369                 char       *tok = (char *) lfirst(l);
8370
8371                 if (pg_strcasecmp(tok, "stderr") == 0)
8372                         newlogdest |= LOG_DESTINATION_STDERR;
8373                 else if (pg_strcasecmp(tok, "csvlog") == 0)
8374                         newlogdest |= LOG_DESTINATION_CSVLOG;
8375 #ifdef HAVE_SYSLOG
8376                 else if (pg_strcasecmp(tok, "syslog") == 0)
8377                         newlogdest |= LOG_DESTINATION_SYSLOG;
8378 #endif
8379 #ifdef WIN32
8380                 else if (pg_strcasecmp(tok, "eventlog") == 0)
8381                         newlogdest |= LOG_DESTINATION_EVENTLOG;
8382 #endif
8383                 else
8384                 {
8385                         GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
8386                         pfree(rawstring);
8387                         list_free(elemlist);
8388                         return false;
8389                 }
8390         }
8391
8392         pfree(rawstring);
8393         list_free(elemlist);
8394
8395         myextra = (int *) guc_malloc(ERROR, sizeof(int));
8396         *myextra = newlogdest;
8397         *extra = (void *) myextra;
8398
8399         return true;
8400 }
8401
8402 static void
8403 assign_log_destination(const char *newval, void *extra)
8404 {
8405         Log_destination = *((int *) extra);
8406 }
8407
8408 static void
8409 assign_syslog_facility(int newval, void *extra)
8410 {
8411 #ifdef HAVE_SYSLOG
8412         set_syslog_parameters(syslog_ident_str ? syslog_ident_str : "postgres",
8413                                                   newval);
8414 #endif
8415         /* Without syslog support, just ignore it */
8416 }
8417
8418 static void
8419 assign_syslog_ident(const char *newval, void *extra)
8420 {
8421 #ifdef HAVE_SYSLOG
8422         set_syslog_parameters(newval, syslog_facility);
8423 #endif
8424         /* Without syslog support, it will always be set to "none", so ignore */
8425 }
8426
8427
8428 static void
8429 assign_session_replication_role(int newval, void *extra)
8430 {
8431         /*
8432          * Must flush the plan cache when changing replication role; but don't
8433          * flush unnecessarily.
8434          */
8435         if (SessionReplicationRole != newval)
8436                 ResetPlanCache();
8437 }
8438
8439 static bool
8440 check_temp_buffers(int *newval, void **extra, GucSource source)
8441 {
8442         /*
8443          * Once local buffers have been initialized, it's too late to change this.
8444          */
8445         if (NLocBuffer && NLocBuffer != *newval)
8446         {
8447                 GUC_check_errdetail("\"temp_buffers\" cannot be changed after any temporary tables have been accessed in the session.");
8448                 return false;
8449         }
8450         return true;
8451 }
8452
8453 static bool
8454 check_phony_autocommit(bool *newval, void **extra, GucSource source)
8455 {
8456         if (!*newval)
8457         {
8458                 GUC_check_errcode(ERRCODE_FEATURE_NOT_SUPPORTED);
8459                 GUC_check_errmsg("SET AUTOCOMMIT TO OFF is no longer supported");
8460                 return false;
8461         }
8462         return true;
8463 }
8464
8465 static bool
8466 check_debug_assertions(bool *newval, void **extra, GucSource source)
8467 {
8468 #ifndef USE_ASSERT_CHECKING
8469         if (*newval)
8470         {
8471                 GUC_check_errmsg("assertion checking is not supported by this build");
8472                 return false;
8473         }
8474 #endif
8475         return true;
8476 }
8477
8478 static bool
8479 check_bonjour(bool *newval, void **extra, GucSource source)
8480 {
8481 #ifndef USE_BONJOUR
8482         if (*newval)
8483         {
8484                 GUC_check_errmsg("Bonjour is not supported by this build");
8485                 return false;
8486         }
8487 #endif
8488         return true;
8489 }
8490
8491 static bool
8492 check_ssl(bool *newval, void **extra, GucSource source)
8493 {
8494 #ifndef USE_SSL
8495         if (*newval)
8496         {
8497                 GUC_check_errmsg("SSL is not supported by this build");
8498                 return false;
8499         }
8500 #endif
8501         return true;
8502 }
8503
8504 static bool
8505 check_stage_log_stats(bool *newval, void **extra, GucSource source)
8506 {
8507         if (*newval && log_statement_stats)
8508         {
8509                 GUC_check_errdetail("Cannot enable parameter when \"log_statement_stats\" is true.");
8510                 return false;
8511         }
8512         return true;
8513 }
8514
8515 static bool
8516 check_log_stats(bool *newval, void **extra, GucSource source)
8517 {
8518         if (*newval &&
8519                 (log_parser_stats || log_planner_stats || log_executor_stats))
8520         {
8521                 GUC_check_errdetail("Cannot enable \"log_statement_stats\" when "
8522                                                         "\"log_parser_stats\", \"log_planner_stats\", "
8523                                                         "or \"log_executor_stats\" is true.");
8524                 return false;
8525         }
8526         return true;
8527 }
8528
8529 static bool
8530 check_canonical_path(char **newval, void **extra, GucSource source)
8531 {
8532         /*
8533          * Since canonicalize_path never enlarges the string, we can just modify
8534          * newval in-place.  But watch out for NULL, which is the default value
8535          * for external_pid_file.
8536          */
8537         if (*newval)
8538                 canonicalize_path(*newval);
8539         return true;
8540 }
8541
8542 static bool
8543 check_timezone_abbreviations(char **newval, void **extra, GucSource source)
8544 {
8545         /*
8546          * The boot_val given above for timezone_abbreviations is NULL. When we
8547          * see this we just do nothing.  If this value isn't overridden from the
8548          * config file then pg_timezone_abbrev_initialize() will eventually
8549          * replace it with "Default".  This hack has two purposes: to avoid
8550          * wasting cycles loading values that might soon be overridden from the
8551          * config file, and to avoid trying to read the timezone abbrev files
8552          * during InitializeGUCOptions().  The latter doesn't work in an
8553          * EXEC_BACKEND subprocess because my_exec_path hasn't been set yet and so
8554          * we can't locate PGSHAREDIR.
8555          */
8556         if (*newval == NULL)
8557         {
8558                 Assert(source == PGC_S_DEFAULT);
8559                 return true;
8560         }
8561
8562         /* OK, load the file and produce a malloc'd TimeZoneAbbrevTable */
8563         *extra = load_tzoffsets(*newval);
8564
8565         /* tzparser.c returns NULL on failure, reporting via GUC_check_errmsg */
8566         if (!*extra)
8567                 return false;
8568
8569         return true;
8570 }
8571
8572 static void
8573 assign_timezone_abbreviations(const char *newval, void *extra)
8574 {
8575         /* Do nothing for the boot_val default of NULL */
8576         if (!extra)
8577                 return;
8578
8579         InstallTimeZoneAbbrevs((TimeZoneAbbrevTable *) extra);
8580 }
8581
8582 /*
8583  * pg_timezone_abbrev_initialize --- set default value if not done already
8584  *
8585  * This is called after initial loading of postgresql.conf.  If no
8586  * timezone_abbreviations setting was found therein, select default.
8587  * If a non-default value is already installed, nothing will happen.
8588  *
8589  * This can also be called from ProcessConfigFile to establish the default
8590  * value after a postgresql.conf entry for it is removed.
8591  */
8592 static void
8593 pg_timezone_abbrev_initialize(void)
8594 {
8595         SetConfigOption("timezone_abbreviations", "Default",
8596                                         PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT);
8597 }
8598
8599 static const char *
8600 show_archive_command(void)
8601 {
8602         if (XLogArchivingActive())
8603                 return XLogArchiveCommand;
8604         else
8605                 return "(disabled)";
8606 }
8607
8608 static void
8609 assign_tcp_keepalives_idle(int newval, void *extra)
8610 {
8611         /*
8612          * The kernel API provides no way to test a value without setting it; and
8613          * once we set it we might fail to unset it.  So there seems little point
8614          * in fully implementing the check-then-assign GUC API for these
8615          * variables.  Instead we just do the assignment on demand.  pqcomm.c
8616          * reports any problems via elog(LOG).
8617          *
8618          * This approach means that the GUC value might have little to do with the
8619          * actual kernel value, so we use a show_hook that retrieves the kernel
8620          * value rather than trusting GUC's copy.
8621          */
8622         (void) pq_setkeepalivesidle(newval, MyProcPort);
8623 }
8624
8625 static const char *
8626 show_tcp_keepalives_idle(void)
8627 {
8628         /* See comments in assign_tcp_keepalives_idle */
8629         static char nbuf[16];
8630
8631         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
8632         return nbuf;
8633 }
8634
8635 static void
8636 assign_tcp_keepalives_interval(int newval, void *extra)
8637 {
8638         /* See comments in assign_tcp_keepalives_idle */
8639         (void) pq_setkeepalivesinterval(newval, MyProcPort);
8640 }
8641
8642 static const char *
8643 show_tcp_keepalives_interval(void)
8644 {
8645         /* See comments in assign_tcp_keepalives_idle */
8646         static char nbuf[16];
8647
8648         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
8649         return nbuf;
8650 }
8651
8652 static void
8653 assign_tcp_keepalives_count(int newval, void *extra)
8654 {
8655         /* See comments in assign_tcp_keepalives_idle */
8656         (void) pq_setkeepalivescount(newval, MyProcPort);
8657 }
8658
8659 static const char *
8660 show_tcp_keepalives_count(void)
8661 {
8662         /* See comments in assign_tcp_keepalives_idle */
8663         static char nbuf[16];
8664
8665         snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
8666         return nbuf;
8667 }
8668
8669 static bool
8670 check_maxconnections(int *newval, void **extra, GucSource source)
8671 {
8672         if (*newval + GetNumShmemAttachedBgworkers() + autovacuum_max_workers + 1 >
8673                 MAX_BACKENDS)
8674                 return false;
8675         return true;
8676 }
8677
8678 static bool
8679 check_autovacuum_max_workers(int *newval, void **extra, GucSource source)
8680 {
8681         if (MaxConnections + *newval + 1 + GetNumShmemAttachedBgworkers() >
8682                 MAX_BACKENDS)
8683                 return false;
8684         return true;
8685 }
8686
8687 static bool
8688 check_effective_io_concurrency(int *newval, void **extra, GucSource source)
8689 {
8690 #ifdef USE_PREFETCH
8691         double          new_prefetch_pages = 0.0;
8692         int                     i;
8693
8694         /*----------
8695          * The user-visible GUC parameter is the number of drives (spindles),
8696          * which we need to translate to a number-of-pages-to-prefetch target.
8697          * The target value is stashed in *extra and then assigned to the actual
8698          * variable by assign_effective_io_concurrency.
8699          *
8700          * The expected number of prefetch pages needed to keep N drives busy is:
8701          *
8702          * drives |   I/O requests
8703          * -------+----------------
8704          *              1 |   1
8705          *              2 |   2/1 + 2/2 = 3
8706          *              3 |   3/1 + 3/2 + 3/3 = 5 1/2
8707          *              4 |   4/1 + 4/2 + 4/3 + 4/4 = 8 1/3
8708          *              n |   n * H(n)
8709          *
8710          * This is called the "coupon collector problem" and H(n) is called the
8711          * harmonic series.  This could be approximated by n * ln(n), but for
8712          * reasonable numbers of drives we might as well just compute the series.
8713          *
8714          * Alternatively we could set the target to the number of pages necessary
8715          * so that the expected number of active spindles is some arbitrary
8716          * percentage of the total.  This sounds the same but is actually slightly
8717          * different.  The result ends up being ln(1-P)/ln((n-1)/n) where P is
8718          * that desired fraction.
8719          *
8720          * Experimental results show that both of these formulas aren't aggressive
8721          * enough, but we don't really have any better proposals.
8722          *
8723          * Note that if *newval = 0 (disabled), we must set target = 0.
8724          *----------
8725          */
8726
8727         for (i = 1; i <= *newval; i++)
8728                 new_prefetch_pages += (double) *newval / (double) i;
8729
8730         /* This range check shouldn't fail, but let's be paranoid */
8731         if (new_prefetch_pages >= 0.0 && new_prefetch_pages < (double) INT_MAX)
8732         {
8733                 int                *myextra = (int *) guc_malloc(ERROR, sizeof(int));
8734
8735                 *myextra = (int) rint(new_prefetch_pages);
8736                 *extra = (void *) myextra;
8737
8738                 return true;
8739         }
8740         else
8741                 return false;
8742 #else
8743         return true;
8744 #endif   /* USE_PREFETCH */
8745 }
8746
8747 static void
8748 assign_effective_io_concurrency(int newval, void *extra)
8749 {
8750 #ifdef USE_PREFETCH
8751         target_prefetch_pages = *((int *) extra);
8752 #endif   /* USE_PREFETCH */
8753 }
8754
8755 static void
8756 assign_pgstat_temp_directory(const char *newval, void *extra)
8757 {
8758         /* check_canonical_path already canonicalized newval for us */
8759         char       *dname;
8760         char       *tname;
8761         char       *fname;
8762
8763         /* directory */
8764         dname = guc_malloc(ERROR, strlen(newval) + 1);          /* runtime dir */
8765         sprintf(dname, "%s", newval);
8766
8767         /* global stats */
8768         tname = guc_malloc(ERROR, strlen(newval) + 12);         /* /global.tmp */
8769         sprintf(tname, "%s/global.tmp", newval);
8770         fname = guc_malloc(ERROR, strlen(newval) + 13);         /* /global.stat */
8771         sprintf(fname, "%s/global.stat", newval);
8772
8773         if (pgstat_stat_directory)
8774                 free(pgstat_stat_directory);
8775         pgstat_stat_directory = dname;
8776         if (pgstat_stat_tmpname)
8777                 free(pgstat_stat_tmpname);
8778         pgstat_stat_tmpname = tname;
8779         if (pgstat_stat_filename)
8780                 free(pgstat_stat_filename);
8781         pgstat_stat_filename = fname;
8782 }
8783
8784 static bool
8785 check_application_name(char **newval, void **extra, GucSource source)
8786 {
8787         /* Only allow clean ASCII chars in the application name */
8788         char       *p;
8789
8790         for (p = *newval; *p; p++)
8791         {
8792                 if (*p < 32 || *p > 126)
8793                         *p = '?';
8794         }
8795
8796         return true;
8797 }
8798
8799 static void
8800 assign_application_name(const char *newval, void *extra)
8801 {
8802         /* Update the pg_stat_activity view */
8803         pgstat_report_appname(newval);
8804 }
8805
8806 static const char *
8807 show_unix_socket_permissions(void)
8808 {
8809         static char buf[8];
8810
8811         snprintf(buf, sizeof(buf), "%04o", Unix_socket_permissions);
8812         return buf;
8813 }
8814
8815 static const char *
8816 show_log_file_mode(void)
8817 {
8818         static char buf[8];
8819
8820         snprintf(buf, sizeof(buf), "%04o", Log_file_mode);
8821         return buf;
8822 }
8823
8824 #include "guc-file.c"