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