guc.c 216.1 KB
Newer Older
1 2 3 4 5
/*--------------------------------------------------------------------
 * guc.c
 *
 * Support for grand unified configuration scheme, including SET
 * command, configuration file, and command line options.
6
 * See src/backend/utils/misc/README for more information.
7 8
 *
 *
B
Bruce Momjian 已提交
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 18
 *--------------------------------------------------------------------
 */
#include "postgres.h"

19
#include <ctype.h>
20
#include <float.h>
21
#include <math.h>
22 23
#include <limits.h>
#include <unistd.h>
24
#include <sys/stat.h>
25 26 27
#ifdef HAVE_SYSLOG
#include <syslog.h>
#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 44 45 46
#include "miscadmin.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#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 55
#include "postmaster/bgwriter.h"
#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 79 80
#ifndef PG_KRB_SRVTAB
#define PG_KRB_SRVTAB ""
#endif
B
 
Bruce Momjian 已提交
81 82 83
#ifndef PG_KRB_SRVNAM
#define PG_KRB_SRVNAM ""
#endif
84

B
Bruce Momjian 已提交
85
#define CONFIG_FILENAME "postgresql.conf"
86 87
#define HBA_FILENAME	"pg_hba.conf"
#define IDENT_FILENAME	"pg_ident.conf"
88

B
Bruce Momjian 已提交
89 90
#ifdef EXEC_BACKEND
#define CONFIG_EXEC_PARAMS "global/config_exec_params"
91
#define CONFIG_EXEC_PARAMS_NEW "global/config_exec_params.new"
B
Bruce Momjian 已提交
92
#endif
93

94
/* upper limit for GUC variables measured in kilobytes of memory */
95 96
/* note that various places assume the byte size fits in a "long" variable */
#if SIZEOF_SIZE_T > 4 && SIZEOF_LONG > 4
97 98 99 100 101
#define MAX_KILOBYTES	INT_MAX
#else
#define MAX_KILOBYTES	(INT_MAX / 1024)
#endif

102 103 104 105 106
/*
 * Note: MAX_BACKENDS is limited to 2^23-1 because inval.c stores the
 * backend ID as a 3-byte signed integer.  Even if that limitation were
 * removed, we still could not exceed INT_MAX/4 because some places compute
 * 4*MaxBackends without any overflow check.  This is rechecked in
107
 * check_maxconnections, since MaxBackends is computed as MaxConnections
108 109 110 111
 * plus autovacuum_max_workers plus one (for the autovacuum launcher).
 */
#define MAX_BACKENDS	0x7fffff

112 113 114 115
#define KB_PER_MB (1024)
#define KB_PER_GB (1024*1024)

#define MS_PER_S 1000
116
#define S_PER_MIN 60
117
#define MS_PER_MIN (1000 * 60)
118 119
#define MIN_PER_H 60
#define S_PER_H (60 * 60)
120
#define MS_PER_H (1000 * 60 * 60)
121 122
#define MIN_PER_D (60 * 24)
#define S_PER_D (60 * 60 * 24)
123 124
#define MS_PER_D (1000 * 60 * 60 * 24)

125
/* XXX these should appear in other modules' header files */
126
extern bool Log_disconnections;
B
Bruce Momjian 已提交
127 128
extern int	CommitDelay;
extern int	CommitSiblings;
129
extern char *default_tablespace;
130
extern char *temp_tablespaces;
131
extern bool synchronize_seqscans;
B
Bruce Momjian 已提交
132
extern bool fullPageWrites;
133
extern int	ssl_renegotiation_limit;
134
extern char *SSLCipherSuites;
135

136
#ifdef TRACE_SORT
B
Bruce Momjian 已提交
137
extern bool trace_sort;
138
#endif
139 140 141
#ifdef TRACE_SYNCSCAN
extern bool trace_syncscan;
#endif
142 143 144
#ifdef DEBUG_BOUNDED_SORT
extern bool optimize_bounded_sort;
#endif
145

146 147 148
static int	GUC_check_errcode_value;

/* global variables for check hook support */
149 150 151
char	   *GUC_check_errmsg_string;
char	   *GUC_check_errdetail_string;
char	   *GUC_check_errhint_string;
152 153


154 155
static void set_config_sourcefile(const char *name, char *sourcefile,
					  int sourceline);
156 157 158 159 160 161 162 163 164 165
static bool call_bool_check_hook(struct config_bool * conf, bool *newval,
					 void **extra, GucSource source, int elevel);
static bool call_int_check_hook(struct config_int * conf, int *newval,
					void **extra, GucSource source, int elevel);
static bool call_real_check_hook(struct config_real * conf, double *newval,
					 void **extra, GucSource source, int elevel);
static bool call_string_check_hook(struct config_string * conf, char **newval,
					   void **extra, GucSource source, int elevel);
static bool call_enum_check_hook(struct config_enum * conf, int *newval,
					 void **extra, GucSource source, int elevel);
166 167 168

static bool check_log_destination(char **newval, void **extra, GucSource source);
static void assign_log_destination(const char *newval, void *extra);
169

T
Tatsuo Ishii 已提交
170
#ifdef HAVE_SYSLOG
171
static int	syslog_facility = LOG_LOCAL0;
172 173 174
#else
static int	syslog_facility = 0;
#endif
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189
static void assign_syslog_facility(int newval, void *extra);
static void assign_syslog_ident(const char *newval, void *extra);
static void assign_session_replication_role(int newval, void *extra);
static bool check_temp_buffers(int *newval, void **extra, GucSource source);
static bool check_phony_autocommit(bool *newval, void **extra, GucSource source);
static bool check_custom_variable_classes(char **newval, void **extra, GucSource source);
static bool check_debug_assertions(bool *newval, void **extra, GucSource source);
static bool check_bonjour(bool *newval, void **extra, GucSource source);
static bool check_ssl(bool *newval, void **extra, GucSource source);
static bool check_stage_log_stats(bool *newval, void **extra, GucSource source);
static bool check_log_stats(bool *newval, void **extra, GucSource source);
static bool check_canonical_path(char **newval, void **extra, GucSource source);
static bool check_timezone_abbreviations(char **newval, void **extra, GucSource source);
static void assign_timezone_abbreviations(const char *newval, void *extra);
190
static const char *show_archive_command(void);
191 192 193
static void assign_tcp_keepalives_idle(int newval, void *extra);
static void assign_tcp_keepalives_interval(int newval, void *extra);
static void assign_tcp_keepalives_count(int newval, void *extra);
194 195 196
static const char *show_tcp_keepalives_idle(void);
static const char *show_tcp_keepalives_interval(void);
static const char *show_tcp_keepalives_count(void);
197 198 199 200 201 202 203 204 205
static bool check_maxconnections(int *newval, void **extra, GucSource source);
static void assign_maxconnections(int newval, void *extra);
static bool check_autovacuum_max_workers(int *newval, void **extra, GucSource source);
static void assign_autovacuum_max_workers(int newval, void *extra);
static bool check_effective_io_concurrency(int *newval, void **extra, GucSource source);
static void assign_effective_io_concurrency(int newval, void *extra);
static void assign_pgstat_temp_directory(const char *newval, void *extra);
static bool check_application_name(char **newval, void **extra, GucSource source);
static void assign_application_name(const char *newval, void *extra);
206 207
static const char *show_unix_socket_permissions(void);
static const char *show_log_file_mode(void);
208

209 210 211
static char *config_enum_get_options(struct config_enum * record,
						const char *prefix, const char *suffix,
						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 221 222 223 224 225
static const struct config_enum_entry bytea_output_options[] = {
	{"escape", BYTEA_OUTPUT_ESCAPE, false},
	{"hex", BYTEA_OUTPUT_HEX, false},
	{NULL, 0, false}
};

226 227 228 229 230
/*
 * We have different sets for client and server message level options because
 * they sort slightly different (see "log" level)
 */
static const struct config_enum_entry client_message_level_options[] = {
231
	{"debug", DEBUG2, true},
232 233 234 235 236 237
	{"debug5", DEBUG5, false},
	{"debug4", DEBUG4, false},
	{"debug3", DEBUG3, false},
	{"debug2", DEBUG2, false},
	{"debug1", DEBUG1, false},
	{"log", LOG, false},
238 239 240 241 242 243 244 245 246 247
	{"info", INFO, true},
	{"notice", NOTICE, false},
	{"warning", WARNING, false},
	{"error", ERROR, false},
	{"fatal", FATAL, true},
	{"panic", PANIC, true},
	{NULL, 0, false}
};

static const struct config_enum_entry server_message_level_options[] = {
248
	{"debug", DEBUG2, true},
249 250 251 252 253
	{"debug5", DEBUG5, false},
	{"debug4", DEBUG4, false},
	{"debug3", DEBUG3, false},
	{"debug2", DEBUG2, false},
	{"debug1", DEBUG1, false},
254 255 256 257
	{"info", INFO, false},
	{"notice", NOTICE, false},
	{"warning", WARNING, false},
	{"error", ERROR, false},
258
	{"log", LOG, false},
259 260 261
	{"fatal", FATAL, false},
	{"panic", PANIC, false},
	{NULL, 0, false}
262 263
};

264 265 266 267
static const struct config_enum_entry intervalstyle_options[] = {
	{"postgres", INTSTYLE_POSTGRES, false},
	{"postgres_verbose", INTSTYLE_POSTGRES_VERBOSE, false},
	{"sql_standard", INTSTYLE_SQL_STANDARD, false},
268
	{"iso_8601", INTSTYLE_ISO_8601, false},
269 270 271
	{NULL, 0, false}
};

272
static const struct config_enum_entry log_error_verbosity_options[] = {
273
	{"terse", PGERROR_TERSE, false},
274
	{"default", PGERROR_DEFAULT, false},
275 276
	{"verbose", PGERROR_VERBOSE, false},
	{NULL, 0, false}
277 278 279
};

static const struct config_enum_entry log_statement_options[] = {
280 281 282 283 284
	{"none", LOGSTMT_NONE, false},
	{"ddl", LOGSTMT_DDL, false},
	{"mod", LOGSTMT_MOD, false},
	{"all", LOGSTMT_ALL, false},
	{NULL, 0, false}
285 286
};

287
static const struct config_enum_entry isolation_level_options[] = {
288 289 290 291
	{"serializable", XACT_SERIALIZABLE, false},
	{"repeatable read", XACT_REPEATABLE_READ, false},
	{"read committed", XACT_READ_COMMITTED, false},
	{"read uncommitted", XACT_READ_UNCOMMITTED, false},
292 293 294 295
	{NULL, 0}
};

static const struct config_enum_entry session_replication_role_options[] = {
296 297 298 299
	{"origin", SESSION_REPLICATION_ROLE_ORIGIN, false},
	{"replica", SESSION_REPLICATION_ROLE_REPLICA, false},
	{"local", SESSION_REPLICATION_ROLE_LOCAL, false},
	{NULL, 0, false}
300 301
};

302
static const struct config_enum_entry syslog_facility_options[] = {
303
#ifdef HAVE_SYSLOG
304 305 306 307 308 309 310 311
	{"local0", LOG_LOCAL0, false},
	{"local1", LOG_LOCAL1, false},
	{"local2", LOG_LOCAL2, false},
	{"local3", LOG_LOCAL3, false},
	{"local4", LOG_LOCAL4, false},
	{"local5", LOG_LOCAL5, false},
	{"local6", LOG_LOCAL6, false},
	{"local7", LOG_LOCAL7, false},
312 313 314
#else
	{"none", 0, false},
#endif
315 316 317
	{NULL, 0}
};

318
static const struct config_enum_entry track_function_options[] = {
319 320 321 322
	{"none", TRACK_FUNC_OFF, false},
	{"pl", TRACK_FUNC_PL, false},
	{"all", TRACK_FUNC_ALL, false},
	{NULL, 0, false}
323 324
};

325
static const struct config_enum_entry xmlbinary_options[] = {
326 327 328
	{"base64", XMLBINARY_BASE64, false},
	{"hex", XMLBINARY_HEX, false},
	{NULL, 0, false}
329 330 331
};

static const struct config_enum_entry xmloption_options[] = {
332 333 334
	{"content", XMLOPTION_CONTENT, false},
	{"document", XMLOPTION_DOCUMENT, false},
	{NULL, 0, false}
335
};
336

337 338 339 340 341
/*
 * Although only "on", "off", and "safe_encoding" are documented, we
 * accept all the likely variants of "on" and "off".
 */
static const struct config_enum_entry backslash_quote_options[] = {
342 343 344 345 346 347 348 349 350 351
	{"safe_encoding", BACKSLASH_QUOTE_SAFE_ENCODING, false},
	{"on", BACKSLASH_QUOTE_ON, false},
	{"off", BACKSLASH_QUOTE_OFF, false},
	{"true", BACKSLASH_QUOTE_ON, true},
	{"false", BACKSLASH_QUOTE_OFF, true},
	{"yes", BACKSLASH_QUOTE_ON, true},
	{"no", BACKSLASH_QUOTE_OFF, true},
	{"1", BACKSLASH_QUOTE_ON, true},
	{"0", BACKSLASH_QUOTE_OFF, true},
	{NULL, 0, false}
352 353
};

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/*
 * Although only "on", "off", and "partition" are documented, we
 * accept all the likely variants of "on" and "off".
 */
static const struct config_enum_entry constraint_exclusion_options[] = {
	{"partition", CONSTRAINT_EXCLUSION_PARTITION, false},
	{"on", CONSTRAINT_EXCLUSION_ON, false},
	{"off", CONSTRAINT_EXCLUSION_OFF, false},
	{"true", CONSTRAINT_EXCLUSION_ON, true},
	{"false", CONSTRAINT_EXCLUSION_OFF, true},
	{"yes", CONSTRAINT_EXCLUSION_ON, true},
	{"no", CONSTRAINT_EXCLUSION_OFF, true},
	{"1", CONSTRAINT_EXCLUSION_ON, true},
	{"0", CONSTRAINT_EXCLUSION_OFF, true},
	{NULL, 0, false}
};

371 372 373 374 375
/*
 * Although only "on", "off", and "local" are documented, we
 * accept all the likely variants of "on" and "off".
 */
static const struct config_enum_entry synchronous_commit_options[] = {
376
	{"local", SYNCHRONOUS_COMMIT_LOCAL_FLUSH, false},
377 378 379 380 381 382 383 384 385 386 387
	{"on", SYNCHRONOUS_COMMIT_ON, false},
	{"off", SYNCHRONOUS_COMMIT_OFF, false},
	{"true", SYNCHRONOUS_COMMIT_ON, true},
	{"false", SYNCHRONOUS_COMMIT_OFF, true},
	{"yes", SYNCHRONOUS_COMMIT_ON, true},
	{"no", SYNCHRONOUS_COMMIT_OFF, true},
	{"1", SYNCHRONOUS_COMMIT_ON, true},
	{"0", SYNCHRONOUS_COMMIT_OFF, true},
	{NULL, 0, false}
};

388 389 390
/*
 * Options for enum values stored in other modules
 */
391
extern const struct config_enum_entry wal_level_options[];
392 393
extern const struct config_enum_entry sync_method_options[];

394 395
/*
 * GUC option variables that are exported from this module
396
 */
397
#ifdef USE_ASSERT_CHECKING
B
Bruce Momjian 已提交
398
bool		assert_enabled = true;
399 400 401
#else
bool		assert_enabled = false;
#endif
B
Rename:  
Bruce Momjian 已提交
402
bool		log_duration = false;
B
Bruce Momjian 已提交
403 404 405
bool		Debug_print_plan = false;
bool		Debug_print_parse = false;
bool		Debug_print_rewritten = false;
406
bool		Debug_pretty_print = true;
407

B
Rename:  
Bruce Momjian 已提交
408 409 410
bool		log_parser_stats = false;
bool		log_planner_stats = false;
bool		log_executor_stats = false;
B
Bruce Momjian 已提交
411 412
bool		log_statement_stats = false;		/* this is sort of all three
												 * above together */
413
bool		log_btree_build_stats = false;
414

415 416
bool		check_function_bodies = true;
bool		default_with_oids = false;
B
Bruce Momjian 已提交
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 429
int			temp_file_limit = -1;

430
int			num_temp_buffers = 1024;
431

432
char	   *data_directory;
433 434 435 436
char	   *ConfigFileName;
char	   *HbaFileName;
char	   *IdentFileName;
char	   *external_pid_file;
437

438 439
char	   *pgstat_temp_directory;

440 441
char	   *application_name;

B
Bruce Momjian 已提交
442 443 444
int			tcp_keepalives_idle;
int			tcp_keepalives_interval;
int			tcp_keepalives_count;
445

446 447 448 449 450
/*
 * These variables are all dummies that don't do anything, except in some
 * cases provide the value for SHOW to display.  The real state is elsewhere
 * and is kept in sync by assign_hooks.
 */
451
static char *log_destination_string;
B
Bruce Momjian 已提交
452

453
static char *syslog_ident_str;
454
static bool phony_autocommit;
455
static bool session_auth_is_superuser;
456 457 458
static double phony_random_seed;
static char *client_encoding_string;
static char *datestyle_string;
459 460
static char *locale_collate;
static char *locale_ctype;
461
static char *server_encoding_string;
462
static char *server_version_string;
B
Bruce Momjian 已提交
463
static int	server_version_num;
464
static char *timezone_string;
465
static char *log_timezone_string;
466
static char *timezone_abbreviations_string;
467 468
static char *XactIsoLevel_string;
static char *session_authorization_string;
469
static char *custom_variable_classes;
470 471 472 473
static int	max_function_args;
static int	max_index_keys;
static int	max_identifier_length;
static int	block_size;
474 475 476
static int	segment_size;
static int	wal_block_size;
static int	wal_segment_size;
B
Bruce Momjian 已提交
477
static bool integer_datetimes;
478
static int	effective_io_concurrency;
B
Bruce Momjian 已提交
479

480
/* should be static, but commands/variable.c needs to get at this */
481
char	   *role_string;
482

483

484
/*
485 486 487
 * Displayable names for context types (enum GucContext)
 *
 * Note: these strings are deliberately not localized.
488
 */
B
Bruce Momjian 已提交
489
const char *const GucContext_Names[] =
490
{
B
Bruce Momjian 已提交
491 492 493 494 495 496
	 /* PGC_INTERNAL */ "internal",
	 /* PGC_POSTMASTER */ "postmaster",
	 /* PGC_SIGHUP */ "sighup",
	 /* PGC_BACKEND */ "backend",
	 /* PGC_SUSET */ "superuser",
	 /* PGC_USERSET */ "user"
497 498 499 500 501 502
};

/*
 * Displayable names for source types (enum GucSource)
 *
 * Note: these strings are deliberately not localized.
B
Bruce Momjian 已提交
503 504
 */
const char *const GucSource_Names[] =
505
{
B
Bruce Momjian 已提交
506
	 /* PGC_S_DEFAULT */ "default",
507
	 /* PGC_S_DYNAMIC_DEFAULT */ "default",
B
Bruce Momjian 已提交
508 509 510 511 512
	 /* PGC_S_ENV_VAR */ "environment variable",
	 /* PGC_S_FILE */ "configuration file",
	 /* PGC_S_ARGV */ "command line",
	 /* PGC_S_DATABASE */ "database",
	 /* PGC_S_USER */ "user",
513
	 /* PGC_S_DATABASE_USER */ "database user",
B
Bruce Momjian 已提交
514 515 516 517 518
	 /* PGC_S_CLIENT */ "client",
	 /* PGC_S_OVERRIDE */ "override",
	 /* PGC_S_INTERACTIVE */ "interactive",
	 /* PGC_S_TEST */ "test",
	 /* PGC_S_SESSION */ "session"
519 520 521 522 523 524 525 526
};

/*
 * Displayable names for the groupings defined in enum config_group
 */
const char *const config_group_names[] =
{
	/* UNGROUPED */
527
	gettext_noop("Ungrouped"),
528 529
	/* FILE_LOCATIONS */
	gettext_noop("File Locations"),
530
	/* CONN_AUTH */
531
	gettext_noop("Connections and Authentication"),
532
	/* CONN_AUTH_SETTINGS */
533
	gettext_noop("Connections and Authentication / Connection Settings"),
534
	/* CONN_AUTH_SECURITY */
535
	gettext_noop("Connections and Authentication / Security and Authentication"),
536
	/* RESOURCES */
537
	gettext_noop("Resource Usage"),
538
	/* RESOURCES_MEM */
539
	gettext_noop("Resource Usage / Memory"),
540 541
	/* RESOURCES_DISK */
	gettext_noop("Resource Usage / Disk"),
542
	/* RESOURCES_KERNEL */
543
	gettext_noop("Resource Usage / Kernel Resources"),
544 545 546 547 548 549
	/* RESOURCES_VACUUM_DELAY */
	gettext_noop("Resource Usage / Cost-Based Vacuum Delay"),
	/* RESOURCES_BGWRITER */
	gettext_noop("Resource Usage / Background Writer"),
	/* RESOURCES_ASYNCHRONOUS */
	gettext_noop("Resource Usage / Asynchronous Behavior"),
550
	/* WAL */
551
	gettext_noop("Write-Ahead Log"),
552
	/* WAL_SETTINGS */
553
	gettext_noop("Write-Ahead Log / Settings"),
554
	/* WAL_CHECKPOINTS */
555
	gettext_noop("Write-Ahead Log / Checkpoints"),
556 557
	/* WAL_ARCHIVING */
	gettext_noop("Write-Ahead Log / Archiving"),
558 559
	/* REPLICATION */
	gettext_noop("Replication"),
560 561
	/* REPLICATION_SENDING */
	gettext_noop("Replication / Sending Servers"),
562 563 564 565
	/* REPLICATION_MASTER */
	gettext_noop("Replication / Master Server"),
	/* REPLICATION_STANDBY */
	gettext_noop("Replication / Standby Servers"),
566
	/* QUERY_TUNING */
567
	gettext_noop("Query Tuning"),
568
	/* QUERY_TUNING_METHOD */
569
	gettext_noop("Query Tuning / Planner Method Configuration"),
570
	/* QUERY_TUNING_COST */
571
	gettext_noop("Query Tuning / Planner Cost Constants"),
572
	/* QUERY_TUNING_GEQO */
573
	gettext_noop("Query Tuning / Genetic Query Optimizer"),
574
	/* QUERY_TUNING_OTHER */
575
	gettext_noop("Query Tuning / Other Planner Options"),
576
	/* LOGGING */
577
	gettext_noop("Reporting and Logging"),
578 579
	/* LOGGING_WHERE */
	gettext_noop("Reporting and Logging / Where to Log"),
580
	/* LOGGING_WHEN */
581
	gettext_noop("Reporting and Logging / When to Log"),
582
	/* LOGGING_WHAT */
583
	gettext_noop("Reporting and Logging / What to Log"),
584
	/* STATS */
585
	gettext_noop("Statistics"),
586
	/* STATS_MONITORING */
587
	gettext_noop("Statistics / Monitoring"),
588
	/* STATS_COLLECTOR */
589
	gettext_noop("Statistics / Query and Index Statistics Collector"),
590
	/* AUTOVACUUM */
591
	gettext_noop("Autovacuum"),
592
	/* CLIENT_CONN */
593
	gettext_noop("Client Connection Defaults"),
594
	/* CLIENT_CONN_STATEMENT */
595
	gettext_noop("Client Connection Defaults / Statement Behavior"),
596
	/* CLIENT_CONN_LOCALE */
597
	gettext_noop("Client Connection Defaults / Locale and Formatting"),
598
	/* CLIENT_CONN_OTHER */
599
	gettext_noop("Client Connection Defaults / Other Defaults"),
600
	/* LOCK_MANAGEMENT */
601
	gettext_noop("Lock Management"),
602
	/* COMPAT_OPTIONS */
603
	gettext_noop("Version and Platform Compatibility"),
604
	/* COMPAT_OPTIONS_PREVIOUS */
605
	gettext_noop("Version and Platform Compatibility / Previous PostgreSQL Versions"),
606
	/* COMPAT_OPTIONS_CLIENT */
607
	gettext_noop("Version and Platform Compatibility / Other Platforms and Clients"),
R
Robert Haas 已提交
608 609
	/* ERROR_HANDLING */
	gettext_noop("Error Handling"),
610 611 612 613
	/* PRESET_OPTIONS */
	gettext_noop("Preset Options"),
	/* CUSTOM_OPTIONS */
	gettext_noop("Customized Options"),
614
	/* DEVELOPER_OPTIONS */
615
	gettext_noop("Developer Options"),
616
	/* help_config wants this array to be null-terminated */
617 618 619
	NULL
};

620 621 622 623 624
/*
 * Displayable names for GUC variable types (enum config_type)
 *
 * Note: these strings are deliberately not localized.
 */
B
Bruce Momjian 已提交
625
const char *const config_type_names[] =
626
{
B
Bruce Momjian 已提交
627 628 629
	 /* PGC_BOOL */ "bool",
	 /* PGC_INT */ "integer",
	 /* PGC_REAL */ "real",
630 631
	 /* PGC_STRING */ "string",
	 /* PGC_ENUM */ "enum"
632 633
};

634

635
/*
636 637 638 639
 * Contents of GUC tables
 *
 * See src/backend/utils/misc/README for design notes.
 *
640 641 642
 * TO ADD AN OPTION:
 *
 * 1. Declare a global variable of type bool, int, double, or char*
B
Bruce Momjian 已提交
643
 *	  and make use of it.
644 645
 *
 * 2. Decide at what times it's safe to set the option. See guc.h for
B
Bruce Momjian 已提交
646
 *	  details.
647 648
 *
 * 3. Decide on a name, a default value, upper and lower bounds (if
B
Bruce Momjian 已提交
649
 *	  applicable), etc.
650 651 652
 *
 * 4. Add a record below.
 *
653
 * 5. Add it to src/backend/utils/misc/postgresql.conf.sample, if
654
 *	  appropriate.
655
 *
656
 * 6. Don't forget to document the option (at least in config.sgml).
657
 *
658
 * 7. If it's a new GUC_LIST option you must edit pg_dumpall.c to ensure
B
Bruce Momjian 已提交
659
 *	  it is not single quoted at dump time.
660 661
 */

662

663
/******** option records follow ********/
664

665
static struct config_bool ConfigureNamesBool[] =
666
{
667
	{
668
		{"enable_seqscan", PGC_USERSET, QUERY_TUNING_METHOD,
669
			gettext_noop("Enables the planner's use of sequential-scan plans."),
670 671 672
			NULL
		},
		&enable_seqscan,
673 674
		true,
		NULL, NULL, NULL
675
	},
676
	{
677
		{"enable_indexscan", PGC_USERSET, QUERY_TUNING_METHOD,
678
			gettext_noop("Enables the planner's use of index-scan plans."),
679 680 681
			NULL
		},
		&enable_indexscan,
682 683
		true,
		NULL, NULL, NULL
684
	},
685 686 687 688 689 690
	{
		{"enable_bitmapscan", PGC_USERSET, QUERY_TUNING_METHOD,
			gettext_noop("Enables the planner's use of bitmap-scan plans."),
			NULL
		},
		&enable_bitmapscan,
691 692
		true,
		NULL, NULL, NULL
693
	},
694
	{
695
		{"enable_tidscan", PGC_USERSET, QUERY_TUNING_METHOD,
696
			gettext_noop("Enables the planner's use of TID scan plans."),
697 698 699
			NULL
		},
		&enable_tidscan,
700 701
		true,
		NULL, NULL, NULL
702 703
	},
	{
704
		{"enable_sort", PGC_USERSET, QUERY_TUNING_METHOD,
705
			gettext_noop("Enables the planner's use of explicit sort steps."),
706 707 708
			NULL
		},
		&enable_sort,
709 710
		true,
		NULL, NULL, NULL
711
	},
712
	{
713
		{"enable_hashagg", PGC_USERSET, QUERY_TUNING_METHOD,
714
			gettext_noop("Enables the planner's use of hashed aggregation plans."),
715 716 717
			NULL
		},
		&enable_hashagg,
718 719
		true,
		NULL, NULL, NULL
720
	},
R
Robert Haas 已提交
721 722 723 724 725 726
	{
		{"enable_material", PGC_USERSET, QUERY_TUNING_METHOD,
			gettext_noop("Enables the planner's use of materialization."),
			NULL
		},
		&enable_material,
727 728
		true,
		NULL, NULL, NULL
R
Robert Haas 已提交
729
	},
730
	{
731
		{"enable_nestloop", PGC_USERSET, QUERY_TUNING_METHOD,
732
			gettext_noop("Enables the planner's use of nested-loop join plans."),
733 734 735
			NULL
		},
		&enable_nestloop,
736 737
		true,
		NULL, NULL, NULL
738 739
	},
	{
740
		{"enable_mergejoin", PGC_USERSET, QUERY_TUNING_METHOD,
741
			gettext_noop("Enables the planner's use of merge join plans."),
742 743 744
			NULL
		},
		&enable_mergejoin,
745 746
		true,
		NULL, NULL, NULL
747 748
	},
	{
749
		{"enable_hashjoin", PGC_USERSET, QUERY_TUNING_METHOD,
750
			gettext_noop("Enables the planner's use of hash join plans."),
751 752 753
			NULL
		},
		&enable_hashjoin,
754 755
		true,
		NULL, NULL, NULL
756 757
	},
	{
758
		{"geqo", PGC_USERSET, QUERY_TUNING_GEQO,
759
			gettext_noop("Enables genetic query optimization."),
760
			gettext_noop("This algorithm attempts to do planning without "
761
						 "exhaustive searching.")
762 763
		},
		&enable_geqo,
764 765
		true,
		NULL, NULL, NULL
766
	},
767
	{
768 769
		/* Not for general use --- used by SET SESSION AUTHORIZATION */
		{"is_superuser", PGC_INTERNAL, UNGROUPED,
770
			gettext_noop("Shows whether the current user is a superuser."),
771 772 773
			NULL,
			GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
774
		&session_auth_is_superuser,
775 776
		false,
		NULL, NULL, NULL
777
	},
778 779 780 781 782 783
	{
		{"bonjour", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
			gettext_noop("Enables advertising the server via Bonjour."),
			NULL
		},
		&enable_bonjour,
784 785
		false,
		check_bonjour, NULL, NULL
786
	},
787
	{
788
		{"ssl", PGC_POSTMASTER, CONN_AUTH_SECURITY,
789
			gettext_noop("Enables SSL connections."),
790 791 792
			NULL
		},
		&EnableSSL,
793 794
		false,
		check_ssl, NULL, NULL
795 796
	},
	{
797
		{"fsync", PGC_SIGHUP, WAL_SETTINGS,
798
			gettext_noop("Forces synchronization of updates to disk."),
799
			gettext_noop("The server will use the fsync() system call in several places to make "
800
			"sure that updates are physically written to disk. This insures "
801
						 "that a database cluster will recover to a consistent state after "
802
						 "an operating system or hardware crash.")
803 804
		},
		&enableFsync,
805 806
		true,
		NULL, NULL, NULL
807
	},
808
	{
809
		{"zero_damaged_pages", PGC_SUSET, DEVELOPER_OPTIONS,
810
			gettext_noop("Continues processing past damaged page headers."),
811
			gettext_noop("Detection of a damaged page header normally causes PostgreSQL to "
B
Bruce Momjian 已提交
812
				"report an error, aborting the current transaction. Setting "
813 814
						 "zero_damaged_pages to true causes the system to instead report a "
						 "warning, zero out the damaged page, and continue processing. This "
815
						 "behavior will destroy data, namely all the rows on the damaged page."),
816
			GUC_NOT_IN_SAMPLE
817 818
		},
		&zero_damaged_pages,
819 820
		false,
		NULL, NULL, NULL
821
	},
822 823 824 825
	{
		{"full_page_writes", PGC_SIGHUP, WAL_SETTINGS,
			gettext_noop("Writes full pages to WAL when first modified after a checkpoint."),
			gettext_noop("A page write in process during an operating system crash might be "
B
Bruce Momjian 已提交
826
						 "only partially written to disk.  During recovery, the row changes "
827
			  "stored in WAL are not enough to recover.  This option writes "
828 829 830 831
						 "pages when first modified after a checkpoint to WAL so full recovery "
						 "is possible.")
		},
		&fullPageWrites,
832 833
		true,
		NULL, NULL, NULL
834
	},
835 836 837 838 839 840
	{
		{"log_checkpoints", PGC_SIGHUP, LOGGING_WHAT,
			gettext_noop("Logs each checkpoint."),
			NULL
		},
		&log_checkpoints,
841 842
		false,
		NULL, NULL, NULL
843
	},
844
	{
845
		{"log_connections", PGC_BACKEND, LOGGING_WHAT,
846
			gettext_noop("Logs each successful connection."),
847 848 849
			NULL
		},
		&Log_connections,
850 851
		false,
		NULL, NULL, NULL
852
	},
853 854
	{
		{"log_disconnections", PGC_BACKEND, LOGGING_WHAT,
P
Peter Eisentraut 已提交
855
			gettext_noop("Logs end of a session, including duration."),
B
Bruce Momjian 已提交
856
			NULL
857 858
		},
		&Log_disconnections,
859 860
		false,
		NULL, NULL, NULL
861
	},
862
	{
863
		{"debug_assertions", PGC_USERSET, DEVELOPER_OPTIONS,
864
			gettext_noop("Turns on various assertion checks."),
865
			gettext_noop("This is a debugging aid."),
866 867 868
			GUC_NOT_IN_SAMPLE
		},
		&assert_enabled,
869 870 871 872
#ifdef USE_ASSERT_CHECKING
		true,
#else
		false,
873
#endif
874
		check_debug_assertions, NULL, NULL
875
	},
R
Robert Haas 已提交
876

877
	{
R
Robert Haas 已提交
878 879 880
		{"exit_on_error", PGC_USERSET, ERROR_HANDLING_OPTIONS,
			gettext_noop("Terminate session on any error."),
			NULL
881 882
		},
		&ExitOnAnyError,
883 884
		false,
		NULL, NULL, NULL
885
	},
R
Robert Haas 已提交
886 887
	{
		{"restart_after_crash", PGC_SIGHUP, ERROR_HANDLING_OPTIONS,
P
Peter Eisentraut 已提交
888
			gettext_noop("Reinitialize server after backend crash."),
R
Robert Haas 已提交
889 890 891
			NULL
		},
		&restart_after_crash,
892 893
		true,
		NULL, NULL, NULL
R
Robert Haas 已提交
894 895
	},

896
	{
897 898
		{"log_duration", PGC_SUSET, LOGGING_WHAT,
			gettext_noop("Logs the duration of each completed SQL statement."),
899 900 901
			NULL
		},
		&log_duration,
902 903
		false,
		NULL, NULL, NULL
904 905
	},
	{
906
		{"debug_print_parse", PGC_USERSET, LOGGING_WHAT,
907
			gettext_noop("Logs each query's parse tree."),
908
			NULL
909 910
		},
		&Debug_print_parse,
911 912
		false,
		NULL, NULL, NULL
913 914
	},
	{
915
		{"debug_print_rewritten", PGC_USERSET, LOGGING_WHAT,
916
			gettext_noop("Logs each query's rewritten parse tree."),
917 918 919
			NULL
		},
		&Debug_print_rewritten,
920 921
		false,
		NULL, NULL, NULL
922 923
	},
	{
924
		{"debug_print_plan", PGC_USERSET, LOGGING_WHAT,
925
			gettext_noop("Logs each query's execution plan."),
926 927 928
			NULL
		},
		&Debug_print_plan,
929 930
		false,
		NULL, NULL, NULL
931 932
	},
	{
933
		{"debug_pretty_print", PGC_USERSET, LOGGING_WHAT,
934
			gettext_noop("Indents parse and plan tree displays."),
935 936 937
			NULL
		},
		&Debug_pretty_print,
938 939
		true,
		NULL, NULL, NULL
940 941
	},
	{
942
		{"log_parser_stats", PGC_SUSET, STATS_MONITORING,
943
			gettext_noop("Writes parser performance statistics to the server log."),
944 945 946
			NULL
		},
		&log_parser_stats,
947 948
		false,
		check_stage_log_stats, NULL, NULL
949 950
	},
	{
951
		{"log_planner_stats", PGC_SUSET, STATS_MONITORING,
952
			gettext_noop("Writes planner performance statistics to the server log."),
953 954 955
			NULL
		},
		&log_planner_stats,
956 957
		false,
		check_stage_log_stats, NULL, NULL
958 959
	},
	{
960
		{"log_executor_stats", PGC_SUSET, STATS_MONITORING,
961
			gettext_noop("Writes executor performance statistics to the server log."),
962 963 964
			NULL
		},
		&log_executor_stats,
965 966
		false,
		check_stage_log_stats, NULL, NULL
967 968
	},
	{
969
		{"log_statement_stats", PGC_SUSET, STATS_MONITORING,
970
			gettext_noop("Writes cumulative performance statistics to the server log."),
971 972 973
			NULL
		},
		&log_statement_stats,
974 975
		false,
		check_log_stats, NULL, NULL
976
	},
977
#ifdef BTREE_BUILD_STATS
978
	{
979
		{"log_btree_build_stats", PGC_SUSET, DEVELOPER_OPTIONS,
980
			gettext_noop("No description available."),
981 982 983 984
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&log_btree_build_stats,
985 986
		false,
		NULL, NULL, NULL
987
	},
988 989
#endif

990
	{
991 992 993 994 995
		{"track_activities", PGC_SUSET, STATS_COLLECTOR,
			gettext_noop("Collects information about executing commands."),
			gettext_noop("Enables the collection of information on the currently "
						 "executing command of each session, along with "
						 "the time at which that command began execution.")
996
		},
997
		&pgstat_track_activities,
998 999
		true,
		NULL, NULL, NULL
1000 1001
	},
	{
1002 1003
		{"track_counts", PGC_SUSET, STATS_COLLECTOR,
			gettext_noop("Collects statistics on database activity."),
1004 1005
			NULL
		},
1006
		&pgstat_track_counts,
1007 1008
		true,
		NULL, NULL, NULL
1009 1010
	},

1011 1012 1013
	{
		{"update_process_title", PGC_SUSET, STATS_COLLECTOR,
			gettext_noop("Updates the process title to show the active SQL command."),
1014
			gettext_noop("Enables updating of the process title every time a new SQL command is received by the server.")
1015 1016
		},
		&update_process_title,
1017 1018
		true,
		NULL, NULL, NULL
1019 1020
	},

1021 1022
	{
		{"autovacuum", PGC_SIGHUP, AUTOVACUUM,
P
Peter Eisentraut 已提交
1023
			gettext_noop("Starts the autovacuum subprocess."),
1024 1025 1026
			NULL
		},
		&autovacuum_start_daemon,
1027 1028
		true,
		NULL, NULL, NULL
1029 1030
	},

1031
	{
1032
		{"trace_notify", PGC_USERSET, DEVELOPER_OPTIONS,
1033
			gettext_noop("Generates debugging output for LISTEN and NOTIFY."),
1034 1035 1036 1037
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Trace_notify,
1038 1039
		false,
		NULL, NULL, NULL
1040
	},
1041 1042

#ifdef LOCK_DEBUG
1043
	{
1044
		{"trace_locks", PGC_SUSET, DEVELOPER_OPTIONS,
1045
			gettext_noop("No description available."),
1046 1047 1048 1049
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Trace_locks,
1050 1051
		false,
		NULL, NULL, NULL
1052 1053
	},
	{
1054
		{"trace_userlocks", PGC_SUSET, DEVELOPER_OPTIONS,
1055
			gettext_noop("No description available."),
1056 1057 1058 1059
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Trace_userlocks,
1060 1061
		false,
		NULL, NULL, NULL
1062 1063
	},
	{
1064
		{"trace_lwlocks", PGC_SUSET, DEVELOPER_OPTIONS,
1065
			gettext_noop("No description available."),
1066 1067 1068 1069
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Trace_lwlocks,
1070 1071
		false,
		NULL, NULL, NULL
1072 1073
	},
	{
1074
		{"debug_deadlocks", PGC_SUSET, DEVELOPER_OPTIONS,
1075
			gettext_noop("No description available."),
1076 1077 1078 1079
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Debug_deadlocks,
1080 1081
		false,
		NULL, NULL, NULL
1082
	},
1083 1084
#endif

1085
	{
1086 1087
		{"log_lock_waits", PGC_SUSET, LOGGING_WHAT,
			gettext_noop("Logs long lock waits."),
1088 1089 1090
			NULL
		},
		&log_lock_waits,
1091 1092
		false,
		NULL, NULL, NULL
1093
	},
1094

1095
	{
1096
		{"log_hostname", PGC_SIGHUP, LOGGING_WHAT,
1097
			gettext_noop("Logs the host name in the connection logs."),
1098 1099
			gettext_noop("By default, connection logs only show the IP address "
						 "of the connecting host. If you want them to show the host name you "
B
Bruce Momjian 已提交
1100 1101
			  "can turn this on, but depending on your host name resolution "
			   "setup it might impose a non-negligible performance penalty.")
1102 1103
		},
		&log_hostname,
1104 1105
		false,
		NULL, NULL, NULL
1106 1107
	},
	{
1108
		{"sql_inheritance", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1109
			gettext_noop("Causes subtables to be included by default in various commands."),
1110
			NULL
1111 1112
		},
		&SQL_inheritance,
1113 1114
		true,
		NULL, NULL, NULL
1115 1116
	},
	{
1117
		{"password_encryption", PGC_USERSET, CONN_AUTH_SECURITY,
1118
			gettext_noop("Encrypt passwords."),
1119
			gettext_noop("When a password is specified in CREATE USER or "
B
Bruce Momjian 已提交
1120
			   "ALTER USER without writing either ENCRYPTED or UNENCRYPTED, "
1121
						 "this parameter determines whether the password is to be encrypted.")
1122 1123
		},
		&Password_encryption,
1124 1125
		true,
		NULL, NULL, NULL
1126 1127
	},
	{
1128
		{"transform_null_equals", PGC_USERSET, COMPAT_OPTIONS_CLIENT,
1129
			gettext_noop("Treats \"expr=NULL\" as \"expr IS NULL\"."),
1130
			gettext_noop("When turned on, expressions of the form expr = NULL "
B
Bruce Momjian 已提交
1131 1132 1133
			   "(or NULL = expr) are treated as expr IS NULL, that is, they "
				"return true if expr evaluates to the null value, and false "
			   "otherwise. The correct behavior of expr = NULL is to always "
1134
						 "return null (unknown).")
1135 1136
		},
		&Transform_null_equals,
1137 1138
		false,
		NULL, NULL, NULL
1139
	},
1140
	{
1141
		{"db_user_namespace", PGC_SIGHUP, CONN_AUTH_SECURITY,
1142
			gettext_noop("Enables per-database user names."),
1143 1144 1145
			NULL
		},
		&Db_user_namespace,
1146 1147
		false,
		NULL, NULL, NULL
1148
	},
1149
	{
1150 1151
		/* only here for backwards compatibility */
		{"autocommit", PGC_USERSET, CLIENT_CONN_STATEMENT,
1152 1153
			gettext_noop("This parameter doesn't do anything."),
			gettext_noop("It's just here so that we won't choke on SET AUTOCOMMIT TO ON from 7.3-vintage clients."),
1154 1155 1156
			GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE
		},
		&phony_autocommit,
1157 1158
		true,
		check_phony_autocommit, NULL, NULL
1159
	},
1160
	{
1161
		{"default_transaction_read_only", PGC_USERSET, CLIENT_CONN_STATEMENT,
1162
			gettext_noop("Sets the default read-only status of new transactions."),
1163 1164 1165
			NULL
		},
		&DefaultXactReadOnly,
1166 1167
		false,
		NULL, NULL, NULL
1168 1169
	},
	{
1170
		{"transaction_read_only", PGC_USERSET, CLIENT_CONN_STATEMENT,
1171
			gettext_noop("Sets the current transaction's read-only status."),
1172 1173 1174 1175
			NULL,
			GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&XactReadOnly,
1176 1177
		false,
		check_transaction_read_only, NULL, NULL
1178
	},
1179 1180 1181 1182 1183 1184
	{
		{"default_transaction_deferrable", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets the default deferrable status of new transactions."),
			NULL
		},
		&DefaultXactDeferrable,
1185 1186
		false,
		NULL, NULL, NULL
1187 1188 1189 1190 1191 1192 1193 1194
	},
	{
		{"transaction_deferrable", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Whether to defer a read-only serializable transaction until it can be executed with no possible serialization failures."),
			NULL,
			GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&XactDeferrable,
1195 1196
		false,
		check_transaction_deferrable, NULL, NULL
1197
	},
1198 1199
	{
		{"check_function_bodies", PGC_USERSET, CLIENT_CONN_STATEMENT,
1200
			gettext_noop("Check function bodies during CREATE FUNCTION."),
1201 1202 1203
			NULL
		},
		&check_function_bodies,
1204 1205
		true,
		NULL, NULL, NULL
1206
	},
1207 1208 1209 1210
	{
		{"array_nulls", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
			gettext_noop("Enable input of NULL elements in arrays."),
			gettext_noop("When turned on, unquoted NULL in an array input "
P
Peter Eisentraut 已提交
1211
						 "value means a null value; "
1212 1213 1214
						 "otherwise it is taken literally.")
		},
		&Array_nulls,
1215 1216
		true,
		NULL, NULL, NULL
1217
	},
1218 1219
	{
		{"default_with_oids", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1220
			gettext_noop("Create new tables with OIDs by default."),
B
Bruce Momjian 已提交
1221
			NULL
1222 1223
		},
		&default_with_oids,
1224 1225
		false,
		NULL, NULL, NULL
1226
	},
1227
	{
1228 1229
		{"logging_collector", PGC_POSTMASTER, LOGGING_WHERE,
			gettext_noop("Start a subprocess to capture stderr output and/or csvlogs into log files."),
B
Bruce Momjian 已提交
1230
			NULL
1231
		},
1232
		&Logging_collector,
1233 1234
		false,
		NULL, NULL, NULL
1235
	},
1236 1237
	{
		{"log_truncate_on_rotation", PGC_SIGHUP, LOGGING_WHERE,
P
Peter Eisentraut 已提交
1238
			gettext_noop("Truncate existing log files of same name during log rotation."),
1239 1240 1241
			NULL
		},
		&Log_truncate_on_rotation,
1242 1243
		false,
		NULL, NULL, NULL
1244
	},
1245

1246 1247 1248 1249 1250 1251 1252 1253
#ifdef TRACE_SORT
	{
		{"trace_sort", PGC_USERSET, DEVELOPER_OPTIONS,
			gettext_noop("Emit information about resource usage in sorting."),
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&trace_sort,
1254 1255
		false,
		NULL, NULL, NULL
1256 1257 1258
	},
#endif

1259 1260 1261 1262 1263 1264 1265 1266 1267
#ifdef TRACE_SYNCSCAN
	/* this is undocumented because not exposed in a standard build */
	{
		{"trace_syncscan", PGC_USERSET, DEVELOPER_OPTIONS,
			gettext_noop("Generate debugging output for synchronized scanning."),
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&trace_syncscan,
1268 1269
		false,
		NULL, NULL, NULL
1270 1271 1272
	},
#endif

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
#ifdef DEBUG_BOUNDED_SORT
	/* this is undocumented because not exposed in a standard build */
	{
		{
			"optimize_bounded_sort", PGC_USERSET, QUERY_TUNING_METHOD,
			gettext_noop("Enable bounded sorting using heap sort."),
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&optimize_bounded_sort,
1283 1284
		true,
		NULL, NULL, NULL
1285 1286 1287
	},
#endif

1288 1289 1290 1291 1292 1293 1294 1295
#ifdef WAL_DEBUG
	{
		{"wal_debug", PGC_SUSET, DEVELOPER_OPTIONS,
			gettext_noop("Emit WAL-related debugging output."),
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&XLOG_DEBUG,
1296 1297
		false,
		NULL, NULL, NULL
1298 1299 1300
	},
#endif

1301
	{
1302
		{"integer_datetimes", PGC_INTERNAL, PRESET_OPTIONS,
1303
			gettext_noop("Datetimes are integer based."),
1304
			NULL,
1305
			GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1306 1307 1308
		},
		&integer_datetimes,
#ifdef HAVE_INT64_TIMESTAMP
1309
		true,
1310
#else
1311
		false,
1312
#endif
1313
		NULL, NULL, NULL
1314 1315
	},

B
 
Bruce Momjian 已提交
1316
	{
1317
		{"krb_caseins_users", PGC_SIGHUP, CONN_AUTH_SECURITY,
1318
			gettext_noop("Sets whether Kerberos and GSSAPI user names should be treated as case-insensitive."),
B
 
Bruce Momjian 已提交
1319 1320 1321
			NULL
		},
		&pg_krb_caseins_users,
1322 1323
		false,
		NULL, NULL, NULL
B
 
Bruce Momjian 已提交
1324 1325
	},

1326 1327
	{
		{"escape_string_warning", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1328
			gettext_noop("Warn about backslash escapes in ordinary string literals."),
1329 1330 1331
			NULL
		},
		&escape_string_warning,
1332 1333
		true,
		NULL, NULL, NULL
1334 1335 1336
	},

	{
1337
		{"standard_conforming_strings", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
1338
			gettext_noop("Causes '...' strings to treat backslashes literally."),
1339
			NULL,
1340
			GUC_REPORT
1341
		},
1342
		&standard_conforming_strings,
1343 1344
		true,
		NULL, NULL, NULL
1345 1346
	},

1347 1348 1349 1350 1351 1352
	{
		{"synchronize_seqscans", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
			gettext_noop("Enable synchronized sequential scans."),
			NULL
		},
		&synchronize_seqscans,
1353 1354
		true,
		NULL, NULL, NULL
1355 1356
	},

B
Bruce Momjian 已提交
1357
	{
1358
		{"archive_mode", PGC_POSTMASTER, WAL_ARCHIVING,
B
Bruce Momjian 已提交
1359 1360 1361 1362
			gettext_noop("Allows archiving of WAL files using archive_command."),
			NULL
		},
		&XLogArchiveMode,
1363 1364
		false,
		NULL, NULL, NULL
B
Bruce Momjian 已提交
1365
	},
1366

1367
	{
1368
		{"hot_standby", PGC_POSTMASTER, REPLICATION_STANDBY,
1369
			gettext_noop("Allows connections and queries during recovery."),
1370 1371
			NULL
		},
1372
		&EnableHotStandby,
1373 1374
		false,
		NULL, NULL, NULL
1375 1376
	},

1377
	{
1378
		{"hot_standby_feedback", PGC_SIGHUP, REPLICATION_STANDBY,
P
Peter Eisentraut 已提交
1379
			gettext_noop("Allows feedback from a hot standby to the primary that will avoid query conflicts."),
1380 1381 1382
			NULL
		},
		&hot_standby_feedback,
1383 1384
		false,
		NULL, NULL, NULL
1385 1386
	},

1387
	{
1388
		{"allow_system_table_mods", PGC_POSTMASTER, DEVELOPER_OPTIONS,
B
Bruce Momjian 已提交
1389 1390 1391
			gettext_noop("Allows modifications of the structure of system tables."),
			NULL,
			GUC_NOT_IN_SAMPLE
1392 1393
		},
		&allowSystemTableMods,
1394 1395
		false,
		NULL, NULL, NULL
1396 1397 1398 1399
	},

	{
		{"ignore_system_indexes", PGC_BACKEND, DEVELOPER_OPTIONS,
B
Bruce Momjian 已提交
1400 1401 1402 1403
			gettext_noop("Disables reading from system indexes."),
			gettext_noop("It does not prevent updating the indexes, so it is safe "
						 "to use.  The worst consequence is slowness."),
			GUC_NOT_IN_SAMPLE
1404 1405
		},
		&IgnoreSystemIndexes,
1406 1407
		false,
		NULL, NULL, NULL
1408 1409
	},

1410 1411
	{
		{"lo_compat_privileges", PGC_SUSET, COMPAT_OPTIONS_PREVIOUS,
P
Peter Eisentraut 已提交
1412
			gettext_noop("Enables backward compatibility mode for privilege checks on large objects."),
1413
			gettext_noop("Skips privilege checks when reading or modifying large objects, "
B
Bruce Momjian 已提交
1414
				  "for compatibility with PostgreSQL releases prior to 9.0.")
1415 1416
		},
		&lo_compat_privileges,
1417 1418
		false,
		NULL, NULL, NULL
1419 1420
	},

1421 1422 1423 1424 1425 1426
	{
		{"quote_all_identifiers", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
			gettext_noop("When generating SQL fragments, quote all identifiers."),
			NULL,
		},
		&quote_all_identifiers,
1427 1428
		false,
		NULL, NULL, NULL
1429 1430
	},

1431
	/* End-of-list marker */
1432
	{
1433
		{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL
1434
	}
1435 1436 1437
};


1438
static struct config_int ConfigureNamesInt[] =
1439
{
1440
	{
1441
		{"archive_timeout", PGC_SIGHUP, WAL_ARCHIVING,
B
Bruce Momjian 已提交
1442 1443 1444 1445
			gettext_noop("Forces a switch to the next xlog file if a "
						 "new file has not been started within N seconds."),
			NULL,
			GUC_UNIT_S
1446 1447
		},
		&XLogArchiveTimeout,
1448 1449
		0, 0, INT_MAX,
		NULL, NULL, NULL
1450
	},
1451 1452
	{
		{"post_auth_delay", PGC_BACKEND, DEVELOPER_OPTIONS,
B
Bruce Momjian 已提交
1453 1454 1455
			gettext_noop("Waits N seconds on connection startup after authentication."),
			gettext_noop("This allows attaching a debugger to the process."),
			GUC_NOT_IN_SAMPLE | GUC_UNIT_S
1456 1457
		},
		&PostAuthDelay,
1458 1459
		0, 0, INT_MAX,
		NULL, NULL, NULL
1460
	},
1461
	{
1462
		{"default_statistics_target", PGC_USERSET, QUERY_TUNING_OTHER,
1463
			gettext_noop("Sets the default statistics target."),
1464
			gettext_noop("This applies to table columns that have not had a "
B
Bruce Momjian 已提交
1465
				"column-specific target set via ALTER TABLE SET STATISTICS.")
1466 1467
		},
		&default_statistics_target,
1468 1469
		100, 1, 10000,
		NULL, NULL, NULL
1470
	},
1471
	{
1472
		{"from_collapse_limit", PGC_USERSET, QUERY_TUNING_OTHER,
1473 1474
			gettext_noop("Sets the FROM-list size beyond which subqueries "
						 "are not collapsed."),
1475
			gettext_noop("The planner will merge subqueries into upper "
B
Bruce Momjian 已提交
1476
				"queries if the resulting FROM list would have no more than "
1477
						 "this many items.")
1478 1479
		},
		&from_collapse_limit,
1480 1481
		8, 1, INT_MAX,
		NULL, NULL, NULL
1482 1483
	},
	{
1484
		{"join_collapse_limit", PGC_USERSET, QUERY_TUNING_OTHER,
1485 1486
			gettext_noop("Sets the FROM-list size beyond which JOIN "
						 "constructs are not flattened."),
1487
			gettext_noop("The planner will flatten explicit JOIN "
1488 1489
						 "constructs into lists of FROM items whenever a "
						 "list of no more than this many items would result.")
1490 1491
		},
		&join_collapse_limit,
1492 1493
		8, 1, INT_MAX,
		NULL, NULL, NULL
1494 1495
	},
	{
1496
		{"geqo_threshold", PGC_USERSET, QUERY_TUNING_GEQO,
1497
			gettext_noop("Sets the threshold of FROM items beyond which GEQO is used."),
1498 1499 1500
			NULL
		},
		&geqo_threshold,
1501 1502
		12, 2, INT_MAX,
		NULL, NULL, NULL
1503
	},
1504
	{
1505 1506
		{"geqo_effort", PGC_USERSET, QUERY_TUNING_GEQO,
			gettext_noop("GEQO: effort is used to set the default for other GEQO parameters."),
1507 1508
			NULL
		},
1509
		&Geqo_effort,
1510 1511
		DEFAULT_GEQO_EFFORT, MIN_GEQO_EFFORT, MAX_GEQO_EFFORT,
		NULL, NULL, NULL
1512 1513 1514 1515 1516 1517
	},
	{
		{"geqo_pool_size", PGC_USERSET, QUERY_TUNING_GEQO,
			gettext_noop("GEQO: number of individuals in the population."),
			gettext_noop("Zero selects a suitable default value.")
		},
1518
		&Geqo_pool_size,
1519 1520
		0, 0, INT_MAX,
		NULL, NULL, NULL
1521 1522
	},
	{
1523
		{"geqo_generations", PGC_USERSET, QUERY_TUNING_GEQO,
1524
			gettext_noop("GEQO: number of iterations of the algorithm."),
1525
			gettext_noop("Zero selects a suitable default value.")
1526 1527
		},
		&Geqo_generations,
1528 1529
		0, 0, INT_MAX,
		NULL, NULL, NULL
1530 1531 1532
	},

	{
1533 1534
		/* This is PGC_SUSET to prevent hiding from log_lock_waits. */
		{"deadlock_timeout", PGC_SUSET, LOCK_MANAGEMENT,
1535
			gettext_noop("Sets the time to wait on a lock before checking for deadlock."),
1536 1537
			NULL,
			GUC_UNIT_MS
1538 1539
		},
		&DeadlockTimeout,
1540 1541
		1000, 1, INT_MAX,
		NULL, NULL, NULL
1542
	},
1543

1544
	{
1545
		{"max_standby_archive_delay", PGC_SIGHUP, REPLICATION_STANDBY,
1546 1547 1548 1549 1550
			gettext_noop("Sets the maximum delay before canceling queries when a hot standby server is processing archived WAL data."),
			NULL,
			GUC_UNIT_MS
		},
		&max_standby_archive_delay,
1551 1552
		30 * 1000, -1, INT_MAX,
		NULL, NULL, NULL
1553 1554 1555
	},

	{
1556
		{"max_standby_streaming_delay", PGC_SIGHUP, REPLICATION_STANDBY,
1557 1558 1559 1560 1561
			gettext_noop("Sets the maximum delay before canceling queries when a hot standby server is processing streamed WAL data."),
			NULL,
			GUC_UNIT_MS
		},
		&max_standby_streaming_delay,
1562 1563
		30 * 1000, -1, INT_MAX,
		NULL, NULL, NULL
1564 1565
	},

1566
	{
1567
		{"wal_receiver_status_interval", PGC_SIGHUP, REPLICATION_STANDBY,
P
Peter Eisentraut 已提交
1568
			gettext_noop("Sets the maximum interval between WAL receiver status reports to the primary."),
1569 1570 1571 1572
			NULL,
			GUC_UNIT_S
		},
		&wal_receiver_status_interval,
1573
		10, 0, INT_MAX / 1000,
1574
		NULL, NULL, NULL
1575 1576
	},

1577
	{
1578
		{"max_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1579
			gettext_noop("Sets the maximum number of concurrent connections."),
1580 1581
			NULL
		},
1582
		&MaxConnections,
1583 1584
		100, 1, MAX_BACKENDS,
		check_maxconnections, assign_maxconnections, NULL
1585
	},
1586

1587
	{
1588
		{"superuser_reserved_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1589
			gettext_noop("Sets the number of connection slots reserved for superusers."),
1590 1591 1592
			NULL
		},
		&ReservedBackends,
1593 1594
		3, 0, MAX_BACKENDS,
		NULL, NULL, NULL
1595 1596
	},

1597 1598 1599 1600
	/*
	 * We sometimes multiply the number of shared buffers by two without
	 * checking for overflow, so we mustn't allow more than INT_MAX / 2.
	 */
1601
	{
1602
		{"shared_buffers", PGC_POSTMASTER, RESOURCES_MEM,
1603
			gettext_noop("Sets the number of shared memory buffers used by the server."),
1604 1605
			NULL,
			GUC_UNIT_BLOCKS
1606 1607
		},
		&NBuffers,
1608 1609
		1024, 16, INT_MAX / 2,
		NULL, NULL, NULL
1610
	},
1611

1612 1613 1614
	{
		{"temp_buffers", PGC_USERSET, RESOURCES_MEM,
			gettext_noop("Sets the maximum number of temporary buffers used by each session."),
1615 1616
			NULL,
			GUC_UNIT_BLOCKS
1617 1618
		},
		&num_temp_buffers,
1619 1620
		1024, 100, INT_MAX / 2,
		check_temp_buffers, NULL, NULL
1621 1622
	},

1623
	{
1624
		{"port", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1625
			gettext_noop("Sets the TCP port the server listens on."),
1626 1627 1628
			NULL
		},
		&PostPortNumber,
1629 1630
		DEF_PGPORT, 1, 65535,
		NULL, NULL, NULL
1631
	},
1632

1633
	{
1634
		{"unix_socket_permissions", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
1635
			gettext_noop("Sets the access permissions of the Unix-domain socket."),
1636
			gettext_noop("Unix-domain sockets use the usual Unix file system "
1637 1638 1639 1640 1641
						 "permission set. The parameter value is expected "
						 "to be a numeric mode specification in the form "
						 "accepted by the chmod and umask system calls. "
						 "(To use the customary octal format the number must "
						 "start with a 0 (zero).)")
1642 1643
		},
		&Unix_socket_permissions,
1644 1645
		0777, 0000, 0777,
		NULL, NULL, show_unix_socket_permissions
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
	},

	{
		{"log_file_mode", PGC_SIGHUP, LOGGING_WHERE,
			gettext_noop("Sets the file permissions for log files."),
			gettext_noop("The parameter value is expected "
						 "to be a numeric mode specification in the form "
						 "accepted by the chmod and umask system calls. "
						 "(To use the customary octal format the number must "
						 "start with a 0 (zero).)")
		},
		&Log_file_mode,
1658 1659
		0600, 0000, 0777,
		NULL, NULL, show_log_file_mode
1660
	},
1661

1662
	{
1663 1664
		{"work_mem", PGC_USERSET, RESOURCES_MEM,
			gettext_noop("Sets the maximum memory to be used for query workspaces."),
1665
			gettext_noop("This much memory can be used by each internal "
B
Bruce Momjian 已提交
1666
						 "sort operation and hash table before switching to "
1667 1668
						 "temporary disk files."),
			GUC_UNIT_KB
1669
		},
1670
		&work_mem,
1671 1672
		1024, 64, MAX_KILOBYTES,
		NULL, NULL, NULL
1673
	},
1674

1675
	{
1676
		{"maintenance_work_mem", PGC_USERSET, RESOURCES_MEM,
1677
			gettext_noop("Sets the maximum memory to be used for maintenance operations."),
1678 1679
			gettext_noop("This includes operations such as VACUUM and CREATE INDEX."),
			GUC_UNIT_KB
1680
		},
1681
		&maintenance_work_mem,
1682 1683
		16384, 1024, MAX_KILOBYTES,
		NULL, NULL, NULL
1684
	},
1685

1686 1687 1688 1689 1690
	/*
	 * We use the hopefully-safely-small value of 100kB as the compiled-in
	 * default for max_stack_depth.  InitializeGUCOptions will increase it if
	 * possible, depending on the actual platform-specific stack limit.
	 */
1691 1692 1693
	{
		{"max_stack_depth", PGC_SUSET, RESOURCES_MEM,
			gettext_noop("Sets the maximum stack depth, in kilobytes."),
1694 1695
			NULL,
			GUC_UNIT_KB
1696 1697
		},
		&max_stack_depth,
1698 1699
		100, 100, MAX_KILOBYTES,
		check_max_stack_depth, assign_max_stack_depth, NULL
1700 1701
	},

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
	{
		{"temp_file_limit", PGC_SUSET, RESOURCES_DISK,
			gettext_noop("Limits the total size of all temp files used by each session."),
			gettext_noop("-1 means no limit."),
			GUC_UNIT_KB
		},
		&temp_file_limit,
		-1, -1, INT_MAX,
		NULL, NULL, NULL
	},

J
Jan Wieck 已提交
1713
	{
1714
		{"vacuum_cost_page_hit", PGC_USERSET, RESOURCES_VACUUM_DELAY,
J
Jan Wieck 已提交
1715 1716 1717 1718
			gettext_noop("Vacuum cost for a page found in the buffer cache."),
			NULL
		},
		&VacuumCostPageHit,
1719 1720
		1, 0, 10000,
		NULL, NULL, NULL
J
Jan Wieck 已提交
1721 1722 1723
	},

	{
1724
		{"vacuum_cost_page_miss", PGC_USERSET, RESOURCES_VACUUM_DELAY,
J
Jan Wieck 已提交
1725 1726 1727 1728
			gettext_noop("Vacuum cost for a page not found in the buffer cache."),
			NULL
		},
		&VacuumCostPageMiss,
1729 1730
		10, 0, 10000,
		NULL, NULL, NULL
J
Jan Wieck 已提交
1731 1732 1733
	},

	{
1734
		{"vacuum_cost_page_dirty", PGC_USERSET, RESOURCES_VACUUM_DELAY,
J
Jan Wieck 已提交
1735 1736 1737 1738
			gettext_noop("Vacuum cost for a page dirtied by vacuum."),
			NULL
		},
		&VacuumCostPageDirty,
1739 1740
		20, 0, 10000,
		NULL, NULL, NULL
J
Jan Wieck 已提交
1741 1742 1743
	},

	{
1744
		{"vacuum_cost_limit", PGC_USERSET, RESOURCES_VACUUM_DELAY,
J
Jan Wieck 已提交
1745 1746 1747 1748
			gettext_noop("Vacuum cost amount available before napping."),
			NULL
		},
		&VacuumCostLimit,
1749 1750
		200, 1, 10000,
		NULL, NULL, NULL
J
Jan Wieck 已提交
1751 1752 1753
	},

	{
1754
		{"vacuum_cost_delay", PGC_USERSET, RESOURCES_VACUUM_DELAY,
1755
			gettext_noop("Vacuum cost delay in milliseconds."),
1756 1757
			NULL,
			GUC_UNIT_MS
J
Jan Wieck 已提交
1758
		},
1759
		&VacuumCostDelay,
1760 1761
		0, 0, 100,
		NULL, NULL, NULL
J
Jan Wieck 已提交
1762 1763
	},

1764 1765 1766
	{
		{"autovacuum_vacuum_cost_delay", PGC_SIGHUP, AUTOVACUUM,
			gettext_noop("Vacuum cost delay in milliseconds, for autovacuum."),
1767 1768
			NULL,
			GUC_UNIT_MS
1769 1770
		},
		&autovacuum_vac_cost_delay,
1771 1772
		20, -1, 100,
		NULL, NULL, NULL
1773 1774 1775 1776 1777 1778 1779 1780
	},

	{
		{"autovacuum_vacuum_cost_limit", PGC_SIGHUP, AUTOVACUUM,
			gettext_noop("Vacuum cost amount available before napping, for autovacuum."),
			NULL
		},
		&autovacuum_vac_cost_limit,
1781 1782
		-1, -1, 10000,
		NULL, NULL, NULL
1783 1784
	},

1785
	{
1786
		{"max_files_per_process", PGC_POSTMASTER, RESOURCES_KERNEL,
1787
			gettext_noop("Sets the maximum number of simultaneously open files for each server process."),
1788 1789 1790
			NULL
		},
		&max_files_per_process,
1791 1792
		1000, 25, INT_MAX,
		NULL, NULL, NULL
1793
	},
1794

1795 1796 1797
	/*
	 * See also CheckRequiredParameterValues() if this parameter changes
	 */
1798
	{
1799
		{"max_prepared_transactions", PGC_POSTMASTER, RESOURCES_MEM,
1800 1801 1802 1803
			gettext_noop("Sets the maximum number of simultaneously prepared transactions."),
			NULL
		},
		&max_prepared_xacts,
1804 1805
		0, 0, MAX_BACKENDS,
		NULL, NULL, NULL
1806 1807
	},

1808
#ifdef LOCK_DEBUG
1809
	{
1810
		{"trace_lock_oidmin", PGC_SUSET, DEVELOPER_OPTIONS,
1811
			gettext_noop("No description available."),
1812 1813 1814 1815
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Trace_lock_oidmin,
1816 1817
		FirstNormalObjectId, 0, INT_MAX,
		NULL, NULL, NULL
1818 1819
	},
	{
1820
		{"trace_lock_table", PGC_SUSET, DEVELOPER_OPTIONS,
1821
			gettext_noop("No description available."),
1822 1823 1824 1825
			NULL,
			GUC_NOT_IN_SAMPLE
		},
		&Trace_lock_table,
1826 1827
		0, 0, INT_MAX,
		NULL, NULL, NULL
1828
	},
1829 1830
#endif

1831
	{
1832
		{"statement_timeout", PGC_USERSET, CLIENT_CONN_STATEMENT,
1833
			gettext_noop("Sets the maximum allowed duration of any statement."),
1834 1835
			gettext_noop("A value of 0 turns off the timeout."),
			GUC_UNIT_MS
1836 1837
		},
		&StatementTimeout,
1838 1839
		0, 0, INT_MAX,
		NULL, NULL, NULL
1840 1841
	},

1842 1843 1844 1845 1846 1847
	{
		{"vacuum_freeze_min_age", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Minimum age at which VACUUM should freeze a table row."),
			NULL
		},
		&vacuum_freeze_min_age,
1848 1849
		50000000, 0, 1000000000,
		NULL, NULL, NULL
1850 1851 1852 1853 1854 1855 1856 1857
	},

	{
		{"vacuum_freeze_table_age", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Age at which VACUUM should scan whole table to freeze tuples."),
			NULL
		},
		&vacuum_freeze_table_age,
1858 1859
		150000000, 0, 2000000000,
		NULL, NULL, NULL
1860 1861
	},

1862
	{
1863
		{"vacuum_defer_cleanup_age", PGC_SIGHUP, REPLICATION_MASTER,
1864
			gettext_noop("Number of transactions by which VACUUM and HOT cleanup should be deferred, if any."),
1865 1866 1867
			NULL
		},
		&vacuum_defer_cleanup_age,
1868 1869
		0, 0, 1000000,
		NULL, NULL, NULL
1870 1871 1872 1873 1874
	},

	/*
	 * See also CheckRequiredParameterValues() if this parameter changes
	 */
1875
	{
1876
		{"max_locks_per_transaction", PGC_POSTMASTER, LOCK_MANAGEMENT,
1877
			gettext_noop("Sets the maximum number of locks per transaction."),
1878
			gettext_noop("The shared lock table is sized on the assumption that "
B
Bruce Momjian 已提交
1879
			  "at most max_locks_per_transaction * max_connections distinct "
1880
						 "objects will need to be locked at any one time.")
1881 1882
		},
		&max_locks_per_xact,
1883 1884
		64, 10, INT_MAX,
		NULL, NULL, NULL
1885
	},
1886

1887
	{
1888
		{"max_pred_locks_per_transaction", PGC_POSTMASTER, LOCK_MANAGEMENT,
1889 1890
			gettext_noop("Sets the maximum number of predicate locks per transaction."),
			gettext_noop("The shared predicate lock table is sized on the assumption that "
1891
						 "at most max_pred_locks_per_transaction * max_connections distinct "
1892 1893 1894
						 "objects will need to be locked at any one time.")
		},
		&max_predicate_locks_per_xact,
1895 1896
		64, 10, INT_MAX,
		NULL, NULL, NULL
1897 1898
	},

1899
	{
1900
		{"authentication_timeout", PGC_SIGHUP, CONN_AUTH_SECURITY,
1901
			gettext_noop("Sets the maximum allowed time to complete client authentication."),
1902 1903
			NULL,
			GUC_UNIT_S
1904 1905
		},
		&AuthenticationTimeout,
1906 1907
		60, 1, 600,
		NULL, NULL, NULL
1908
	},
1909

1910
	{
1911 1912
		/* Not for general use */
		{"pre_auth_delay", PGC_SIGHUP, DEVELOPER_OPTIONS,
1913 1914
			gettext_noop("Waits N seconds on connection startup before authentication."),
			gettext_noop("This allows attaching a debugger to the process."),
1915
			GUC_NOT_IN_SAMPLE | GUC_UNIT_S
1916 1917
		},
		&PreAuthDelay,
1918 1919
		0, 0, 60,
		NULL, NULL, NULL
1920
	},
1921

1922
	{
1923
		{"wal_keep_segments", PGC_SIGHUP, REPLICATION_SENDING,
P
Peter Eisentraut 已提交
1924
			gettext_noop("Sets the number of WAL files held for standby servers."),
1925 1926
			NULL
		},
1927
		&wal_keep_segments,
1928 1929
		0, 0, INT_MAX,
		NULL, NULL, NULL
1930 1931
	},

1932
	{
1933
		{"checkpoint_segments", PGC_SIGHUP, WAL_CHECKPOINTS,
1934
			gettext_noop("Sets the maximum distance in log segments between automatic WAL checkpoints."),
1935 1936 1937
			NULL
		},
		&CheckPointSegments,
1938 1939
		3, 1, INT_MAX,
		NULL, NULL, NULL
1940
	},
T
Tom Lane 已提交
1941

1942
	{
1943
		{"checkpoint_timeout", PGC_SIGHUP, WAL_CHECKPOINTS,
1944
			gettext_noop("Sets the maximum time between automatic WAL checkpoints."),
1945 1946
			NULL,
			GUC_UNIT_S
1947 1948
		},
		&CheckPointTimeout,
1949 1950
		300, 30, 3600,
		NULL, NULL, NULL
1951
	},
V
Vadim B. Mikheev 已提交
1952

1953
	{
1954
		{"checkpoint_warning", PGC_SIGHUP, WAL_CHECKPOINTS,
1955 1956
			gettext_noop("Enables warnings if checkpoint segments are filled more "
						 "frequently than this."),
1957
			gettext_noop("Write a message to the server log if checkpoints "
B
Bruce Momjian 已提交
1958
			"caused by the filling of checkpoint segment files happens more "
1959 1960
						 "frequently than this number of seconds. Zero turns off the warning."),
			GUC_UNIT_S
1961 1962
		},
		&CheckPointWarning,
1963 1964
		30, 0, INT_MAX,
		NULL, NULL, NULL
1965 1966
	},

1967
	{
1968
		{"wal_buffers", PGC_POSTMASTER, WAL_SETTINGS,
1969
			gettext_noop("Sets the number of disk-page buffers in shared memory for WAL."),
1970 1971
			NULL,
			GUC_UNIT_XBLOCKS
1972 1973
		},
		&XLOGbuffers,
1974 1975
		-1, -1, INT_MAX,
		check_wal_buffers, NULL, NULL
1976
	},
V
Vadim B. Mikheev 已提交
1977

1978 1979
	{
		{"wal_writer_delay", PGC_SIGHUP, WAL_SETTINGS,
1980
			gettext_noop("WAL writer sleep time between WAL flushes."),
1981 1982 1983 1984
			NULL,
			GUC_UNIT_MS
		},
		&WalWriterDelay,
1985 1986
		200, 1, 10000,
		NULL, NULL, NULL
1987 1988
	},

1989 1990
	{
		/* see max_connections */
1991
		{"max_wal_senders", PGC_POSTMASTER, REPLICATION_SENDING,
1992 1993 1994
			gettext_noop("Sets the maximum number of simultaneously running WAL sender processes."),
			NULL
		},
1995
		&max_wal_senders,
1996 1997
		0, 0, MAX_BACKENDS,
		NULL, NULL, NULL
1998 1999 2000
	},

	{
2001
		{"wal_sender_delay", PGC_SIGHUP, REPLICATION_SENDING,
2002 2003 2004 2005 2006
			gettext_noop("WAL sender sleep time between WAL replications."),
			NULL,
			GUC_UNIT_MS
		},
		&WalSndDelay,
2007 2008
		1000, 1, 10000,
		NULL, NULL, NULL
2009 2010
	},

2011
	{
2012
		{"replication_timeout", PGC_SIGHUP, REPLICATION_SENDING,
2013 2014 2015 2016 2017
			gettext_noop("Sets the maximum time to wait for WAL replication."),
			NULL,
			GUC_UNIT_MS
		},
		&replication_timeout,
2018 2019
		60 * 1000, 0, INT_MAX,
		NULL, NULL, NULL
2020 2021
	},

2022
	{
2023
		{"commit_delay", PGC_USERSET, WAL_SETTINGS,
2024 2025
			gettext_noop("Sets the delay in microseconds between transaction commit and "
						 "flushing WAL to disk."),
2026 2027 2028
			NULL
		},
		&CommitDelay,
2029 2030
		0, 0, 100000,
		NULL, NULL, NULL
2031
	},
V
Vadim B. Mikheev 已提交
2032

2033
	{
2034
		{"commit_siblings", PGC_USERSET, WAL_SETTINGS,
2035 2036
			gettext_noop("Sets the minimum concurrent open transactions before performing "
						 "commit_delay."),
2037 2038 2039
			NULL
		},
		&CommitSiblings,
2040 2041
		5, 0, 1000,
		NULL, NULL, NULL
2042
	},
2043

2044
	{
2045
		{"extra_float_digits", PGC_USERSET, CLIENT_CONN_LOCALE,
2046
			gettext_noop("Sets the number of digits displayed for floating-point values."),
2047
			gettext_noop("This affects real, double precision, and geometric data types. "
B
Bruce Momjian 已提交
2048
			 "The parameter value is added to the standard number of digits "
2049
						 "(FLT_DIG or DBL_DIG as appropriate).")
2050 2051
		},
		&extra_float_digits,
2052 2053
		0, -15, 3,
		NULL, NULL, NULL
2054 2055
	},

B
Bruce Momjian 已提交
2056
	{
2057
		{"log_min_duration_statement", PGC_SUSET, LOGGING_WHEN,
2058 2059
			gettext_noop("Sets the minimum execution time above which "
						 "statements will be logged."),
2060
			gettext_noop("Zero prints all queries. -1 turns this feature off."),
2061
			GUC_UNIT_MS
2062 2063
		},
		&log_min_duration_statement,
2064 2065
		-1, -1, INT_MAX,
		NULL, NULL, NULL
B
Bruce Momjian 已提交
2066 2067
	},

2068
	{
2069
		{"log_autovacuum_min_duration", PGC_SIGHUP, LOGGING_WHAT,
2070
			gettext_noop("Sets the minimum execution time above which "
2071 2072
						 "autovacuum actions will be logged."),
			gettext_noop("Zero prints all actions. -1 turns autovacuum logging off."),
2073 2074
			GUC_UNIT_MS
		},
2075
		&Log_autovacuum_min_duration,
2076 2077
		-1, -1, INT_MAX,
		NULL, NULL, NULL
2078 2079
	},

J
Jan Wieck 已提交
2080
	{
2081
		{"bgwriter_delay", PGC_SIGHUP, RESOURCES_BGWRITER,
2082
			gettext_noop("Background writer sleep time between rounds."),
2083 2084
			NULL,
			GUC_UNIT_MS
J
Jan Wieck 已提交
2085 2086
		},
		&BgWriterDelay,
2087 2088
		200, 10, 10000,
		NULL, NULL, NULL
J
Jan Wieck 已提交
2089 2090 2091
	},

	{
2092
		{"bgwriter_lru_maxpages", PGC_SIGHUP, RESOURCES_BGWRITER,
2093
			gettext_noop("Background writer maximum number of LRU pages to flush per round."),
J
Jan Wieck 已提交
2094 2095
			NULL
		},
2096
		&bgwriter_lru_maxpages,
2097 2098
		100, 0, 1000,
		NULL, NULL, NULL
J
Jan Wieck 已提交
2099 2100
	},

2101
	{
2102 2103
		{"effective_io_concurrency",
#ifdef USE_PREFETCH
2104
			PGC_USERSET,
2105
#else
2106
			PGC_INTERNAL,
2107
#endif
2108
			RESOURCES_ASYNCHRONOUS,
2109 2110 2111 2112 2113 2114 2115 2116 2117
			gettext_noop("Number of simultaneous requests that can be handled efficiently by the disk subsystem."),
			gettext_noop("For RAID arrays, this should be approximately the number of drive spindles in the array.")
		},
		&effective_io_concurrency,
#ifdef USE_PREFETCH
		1, 0, 1000,
#else
		0, 0, 0,
#endif
2118
		check_effective_io_concurrency, assign_effective_io_concurrency, NULL
2119 2120
	},

2121 2122
	{
		{"log_rotation_age", PGC_SIGHUP, LOGGING_WHERE,
2123
			gettext_noop("Automatic log file rotation will occur after N minutes."),
2124 2125
			NULL,
			GUC_UNIT_MIN
2126 2127
		},
		&Log_RotationAge,
2128 2129
		HOURS_PER_DAY * MINS_PER_HOUR, 0, INT_MAX / MINS_PER_HOUR,
		NULL, NULL, NULL
2130 2131 2132 2133
	},

	{
		{"log_rotation_size", PGC_SIGHUP, LOGGING_WHERE,
2134
			gettext_noop("Automatic log file rotation will occur after N kilobytes."),
2135 2136
			NULL,
			GUC_UNIT_KB
2137 2138
		},
		&Log_RotationSize,
2139 2140
		10 * 1024, 0, INT_MAX / 1024,
		NULL, NULL, NULL
2141 2142
	},

2143
	{
2144
		{"max_function_args", PGC_INTERNAL, PRESET_OPTIONS,
P
Peter Eisentraut 已提交
2145
			gettext_noop("Shows the maximum number of function arguments."),
2146 2147 2148 2149
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&max_function_args,
2150 2151
		FUNC_MAX_ARGS, FUNC_MAX_ARGS, FUNC_MAX_ARGS,
		NULL, NULL, NULL
2152 2153 2154
	},

	{
2155
		{"max_index_keys", PGC_INTERNAL, PRESET_OPTIONS,
P
Peter Eisentraut 已提交
2156
			gettext_noop("Shows the maximum number of index keys."),
2157 2158 2159 2160
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&max_index_keys,
2161 2162
		INDEX_MAX_KEYS, INDEX_MAX_KEYS, INDEX_MAX_KEYS,
		NULL, NULL, NULL
2163 2164 2165
	},

	{
2166
		{"max_identifier_length", PGC_INTERNAL, PRESET_OPTIONS,
2167
			gettext_noop("Shows the maximum identifier length."),
2168 2169 2170 2171
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&max_identifier_length,
2172 2173
		NAMEDATALEN - 1, NAMEDATALEN - 1, NAMEDATALEN - 1,
		NULL, NULL, NULL
2174 2175 2176
	},

	{
2177
		{"block_size", PGC_INTERNAL, PRESET_OPTIONS,
2178
			gettext_noop("Shows the size of a disk block."),
2179 2180 2181 2182
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&block_size,
2183 2184
		BLCKSZ, BLCKSZ, BLCKSZ,
		NULL, NULL, NULL
2185 2186
	},

2187 2188
	{
		{"segment_size", PGC_INTERNAL, PRESET_OPTIONS,
2189 2190 2191
			gettext_noop("Shows the number of pages per disk file."),
			NULL,
			GUC_UNIT_BLOCKS | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2192 2193
		},
		&segment_size,
2194 2195
		RELSEG_SIZE, RELSEG_SIZE, RELSEG_SIZE,
		NULL, NULL, NULL
2196 2197 2198 2199 2200 2201 2202 2203 2204
	},

	{
		{"wal_block_size", PGC_INTERNAL, PRESET_OPTIONS,
			gettext_noop("Shows the block size in the write ahead log."),
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&wal_block_size,
2205 2206
		XLOG_BLCKSZ, XLOG_BLCKSZ, XLOG_BLCKSZ,
		NULL, NULL, NULL
2207 2208 2209 2210 2211 2212 2213 2214 2215
	},

	{
		{"wal_segment_size", PGC_INTERNAL, PRESET_OPTIONS,
			gettext_noop("Shows the number of pages per write ahead log segment."),
			NULL,
			GUC_UNIT_XBLOCKS | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&wal_segment_size,
2216 2217
		(XLOG_SEG_SIZE / XLOG_BLCKSZ),
		(XLOG_SEG_SIZE / XLOG_BLCKSZ),
2218
		(XLOG_SEG_SIZE / XLOG_BLCKSZ),
2219
		NULL, NULL, NULL
2220 2221
	},

2222 2223
	{
		{"autovacuum_naptime", PGC_SIGHUP, AUTOVACUUM,
2224
			gettext_noop("Time to sleep between autovacuum runs."),
2225 2226
			NULL,
			GUC_UNIT_S
2227 2228
		},
		&autovacuum_naptime,
2229 2230
		60, 1, INT_MAX / 1000,
		NULL, NULL, NULL
2231 2232 2233 2234 2235 2236 2237
	},
	{
		{"autovacuum_vacuum_threshold", PGC_SIGHUP, AUTOVACUUM,
			gettext_noop("Minimum number of tuple updates or deletes prior to vacuum."),
			NULL
		},
		&autovacuum_vac_thresh,
2238 2239
		50, 0, INT_MAX,
		NULL, NULL, NULL
2240 2241 2242 2243 2244 2245 2246
	},
	{
		{"autovacuum_analyze_threshold", PGC_SIGHUP, AUTOVACUUM,
			gettext_noop("Minimum number of tuple inserts, updates or deletes prior to analyze."),
			NULL
		},
		&autovacuum_anl_thresh,
2247 2248
		50, 0, INT_MAX,
		NULL, NULL, NULL
2249
	},
2250 2251 2252
	{
		/* see varsup.c for why this is PGC_POSTMASTER not PGC_SIGHUP */
		{"autovacuum_freeze_max_age", PGC_POSTMASTER, AUTOVACUUM,
P
Peter Eisentraut 已提交
2253
			gettext_noop("Age at which to autovacuum a table to prevent transaction ID wraparound."),
2254 2255 2256
			NULL
		},
		&autovacuum_freeze_max_age,
2257
		/* see pg_resetxlog if you change the upper-limit value */
2258 2259
		200000000, 100000000, 2000000000,
		NULL, NULL, NULL
2260
	},
2261 2262 2263 2264 2265 2266 2267
	{
		/* see max_connections */
		{"autovacuum_max_workers", PGC_POSTMASTER, AUTOVACUUM,
			gettext_noop("Sets the maximum number of simultaneously running autovacuum worker processes."),
			NULL
		},
		&autovacuum_max_workers,
2268 2269
		3, 1, MAX_BACKENDS,
		check_autovacuum_max_workers, assign_autovacuum_max_workers, NULL
2270
	},
2271

2272 2273
	{
		{"tcp_keepalives_idle", PGC_USERSET, CLIENT_CONN_OTHER,
2274
			gettext_noop("Time between issuing TCP keepalives."),
B
Bruce Momjian 已提交
2275
			gettext_noop("A value of 0 uses the system default."),
2276
			GUC_UNIT_S
B
Bruce Momjian 已提交
2277
		},
2278
		&tcp_keepalives_idle,
2279 2280
		0, 0, INT_MAX,
		NULL, assign_tcp_keepalives_idle, show_tcp_keepalives_idle
2281 2282 2283 2284
	},

	{
		{"tcp_keepalives_interval", PGC_USERSET, CLIENT_CONN_OTHER,
2285
			gettext_noop("Time between TCP keepalive retransmits."),
B
Bruce Momjian 已提交
2286
			gettext_noop("A value of 0 uses the system default."),
2287
			GUC_UNIT_S
B
Bruce Momjian 已提交
2288
		},
2289
		&tcp_keepalives_interval,
2290 2291
		0, 0, INT_MAX,
		NULL, assign_tcp_keepalives_interval, show_tcp_keepalives_interval
2292 2293
	},

2294 2295 2296 2297 2298 2299 2300
	{
		{"ssl_renegotiation_limit", PGC_USERSET, CONN_AUTH_SECURITY,
			gettext_noop("Set the amount of traffic to send and receive before renegotiating the encryption keys."),
			NULL,
			GUC_UNIT_KB,
		},
		&ssl_renegotiation_limit,
2301 2302
		512 * 1024, 0, MAX_KILOBYTES,
		NULL, NULL, NULL
2303 2304
	},

2305 2306
	{
		{"tcp_keepalives_count", PGC_USERSET, CLIENT_CONN_OTHER,
B
Bruce Momjian 已提交
2307 2308 2309 2310 2311
			gettext_noop("Maximum number of TCP keepalive retransmits."),
			gettext_noop("This controls the number of consecutive keepalive retransmits that can be "
						 "lost before a connection is considered dead. A value of 0 uses the "
						 "system default."),
		},
2312
		&tcp_keepalives_count,
2313 2314
		0, 0, INT_MAX,
		NULL, assign_tcp_keepalives_count, show_tcp_keepalives_count
2315 2316
	},

T
Teodor Sigaev 已提交
2317
	{
2318
		{"gin_fuzzy_search_limit", PGC_USERSET, CLIENT_CONN_OTHER,
P
Peter Eisentraut 已提交
2319
			gettext_noop("Sets the maximum allowed result for exact search by GIN."),
T
Teodor Sigaev 已提交
2320 2321 2322 2323
			NULL,
			0
		},
		&GinFuzzySearchLimit,
2324 2325
		0, 0, INT_MAX,
		NULL, NULL, NULL
T
Teodor Sigaev 已提交
2326 2327
	},

2328 2329
	{
		{"effective_cache_size", PGC_USERSET, QUERY_TUNING_COST,
2330
			gettext_noop("Sets the planner's assumption about the size of the disk cache."),
2331 2332
			gettext_noop("That is, the portion of the kernel's disk cache that "
						 "will be used for PostgreSQL data files. This is measured in disk "
2333 2334
						 "pages, which are normally 8 kB each."),
			GUC_UNIT_BLOCKS,
2335 2336
		},
		&effective_cache_size,
2337 2338
		DEFAULT_EFFECTIVE_CACHE_SIZE, 1, INT_MAX,
		NULL, NULL, NULL
2339 2340
	},

2341 2342 2343 2344 2345
	{
		/* Can't be set in postgresql.conf */
		{"server_version_num", PGC_INTERNAL, PRESET_OPTIONS,
			gettext_noop("Shows the server version as an integer."),
			NULL,
2346
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2347 2348
		},
		&server_version_num,
2349 2350
		PG_VERSION_NUM, PG_VERSION_NUM, PG_VERSION_NUM,
		NULL, NULL, NULL
2351
	},
2352

2353
	{
2354
		{"log_temp_files", PGC_SUSET, LOGGING_WHAT,
2355
			gettext_noop("Log the use of temporary files larger than this number of kilobytes."),
2356
			gettext_noop("Zero logs all files. The default is -1 (turning this feature off)."),
2357
			GUC_UNIT_KB
2358 2359
		},
		&log_temp_files,
2360 2361
		-1, -1, INT_MAX,
		NULL, NULL, NULL
2362
	},
2363

2364 2365 2366 2367 2368 2369
	{
		{"track_activity_query_size", PGC_POSTMASTER, RESOURCES_MEM,
			gettext_noop("Sets the size reserved for pg_stat_activity.current_query, in bytes."),
			NULL,
		},
		&pgstat_track_activity_query_size,
2370 2371
		1024, 100, 102400,
		NULL, NULL, NULL
2372 2373
	},

2374
	/* End-of-list marker */
2375
	{
2376
		{NULL, 0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL, NULL
2377
	}
2378 2379 2380
};


2381
static struct config_real ConfigureNamesReal[] =
2382
{
2383
	{
2384 2385 2386 2387
		{"seq_page_cost", PGC_USERSET, QUERY_TUNING_COST,
			gettext_noop("Sets the planner's estimate of the cost of a "
						 "sequentially fetched disk page."),
			NULL
2388
		},
2389
		&seq_page_cost,
2390 2391
		DEFAULT_SEQ_PAGE_COST, 0, DBL_MAX,
		NULL, NULL, NULL
2392
	},
2393
	{
2394
		{"random_page_cost", PGC_USERSET, QUERY_TUNING_COST,
2395 2396 2397
			gettext_noop("Sets the planner's estimate of the cost of a "
						 "nonsequentially fetched disk page."),
			NULL
2398 2399
		},
		&random_page_cost,
2400 2401
		DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX,
		NULL, NULL, NULL
2402 2403
	},
	{
2404
		{"cpu_tuple_cost", PGC_USERSET, QUERY_TUNING_COST,
2405 2406 2407
			gettext_noop("Sets the planner's estimate of the cost of "
						 "processing each tuple (row)."),
			NULL
2408 2409
		},
		&cpu_tuple_cost,
2410 2411
		DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX,
		NULL, NULL, NULL
2412 2413
	},
	{
2414
		{"cpu_index_tuple_cost", PGC_USERSET, QUERY_TUNING_COST,
2415 2416 2417
			gettext_noop("Sets the planner's estimate of the cost of "
						 "processing each index entry during an index scan."),
			NULL
2418 2419
		},
		&cpu_index_tuple_cost,
2420 2421
		DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX,
		NULL, NULL, NULL
2422 2423
	},
	{
2424
		{"cpu_operator_cost", PGC_USERSET, QUERY_TUNING_COST,
2425 2426 2427
			gettext_noop("Sets the planner's estimate of the cost of "
						 "processing each operator or function call."),
			NULL
2428 2429
		},
		&cpu_operator_cost,
2430 2431
		DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX,
		NULL, NULL, NULL
2432 2433
	},

2434 2435 2436 2437 2438 2439 2440
	{
		{"cursor_tuple_fraction", PGC_USERSET, QUERY_TUNING_OTHER,
			gettext_noop("Sets the planner's estimate of the fraction of "
						 "a cursor's rows that will be retrieved."),
			NULL
		},
		&cursor_tuple_fraction,
2441 2442
		DEFAULT_CURSOR_TUPLE_FRACTION, 0.0, 1.0,
		NULL, NULL, NULL
2443 2444
	},

2445
	{
2446
		{"geqo_selection_bias", PGC_USERSET, QUERY_TUNING_GEQO,
2447
			gettext_noop("GEQO: selective pressure within the population."),
2448 2449 2450
			NULL
		},
		&Geqo_selection_bias,
2451 2452 2453
		DEFAULT_GEQO_SELECTION_BIAS,
		MIN_GEQO_SELECTION_BIAS, MAX_GEQO_SELECTION_BIAS,
		NULL, NULL, NULL
2454
	},
2455 2456 2457 2458 2459 2460
	{
		{"geqo_seed", PGC_USERSET, QUERY_TUNING_GEQO,
			gettext_noop("GEQO: seed for random path selection."),
			NULL
		},
		&Geqo_seed,
2461 2462
		0.0, 0.0, 1.0,
		NULL, NULL, NULL
2463
	},
2464

2465
	{
2466
		{"bgwriter_lru_multiplier", PGC_SIGHUP, RESOURCES_BGWRITER,
2467
			gettext_noop("Multiple of the average buffer usage to free per round."),
2468 2469
			NULL
		},
2470
		&bgwriter_lru_multiplier,
2471 2472
		2.0, 0.0, 10.0,
		NULL, NULL, NULL
2473 2474
	},

2475
	{
2476
		{"seed", PGC_USERSET, UNGROUPED,
2477
			gettext_noop("Sets the seed for random-number generation."),
2478 2479 2480
			NULL,
			GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
2481
		&phony_random_seed,
2482 2483
		0.0, -1.0, 1.0,
		check_random_seed, assign_random_seed, show_random_seed
2484 2485
	},

2486 2487 2488 2489 2490 2491
	{
		{"autovacuum_vacuum_scale_factor", PGC_SIGHUP, AUTOVACUUM,
			gettext_noop("Number of tuple updates or deletes prior to vacuum as a fraction of reltuples."),
			NULL
		},
		&autovacuum_vac_scale,
2492 2493
		0.2, 0.0, 100.0,
		NULL, NULL, NULL
2494 2495 2496 2497 2498 2499 2500
	},
	{
		{"autovacuum_analyze_scale_factor", PGC_SIGHUP, AUTOVACUUM,
			gettext_noop("Number of tuple inserts, updates or deletes prior to analyze as a fraction of reltuples."),
			NULL
		},
		&autovacuum_anl_scale,
2501 2502
		0.1, 0.0, 100.0,
		NULL, NULL, NULL
2503 2504
	},

2505 2506 2507 2508 2509 2510
	{
		{"checkpoint_completion_target", PGC_SIGHUP, WAL_CHECKPOINTS,
			gettext_noop("Time spent flushing dirty buffers during checkpoint, as fraction of checkpoint interval."),
			NULL
		},
		&CheckPointCompletionTarget,
2511 2512
		0.5, 0.0, 1.0,
		NULL, NULL, NULL
2513 2514
	},

2515
	/* End-of-list marker */
2516
	{
2517
		{NULL, 0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL, NULL
2518
	}
2519 2520 2521
};


2522
static struct config_string ConfigureNamesString[] =
2523
{
B
Bruce Momjian 已提交
2524
	{
2525
		{"archive_command", PGC_SIGHUP, WAL_ARCHIVING,
2526 2527
			gettext_noop("Sets the shell command that will be called to archive a WAL file."),
			NULL
B
Bruce Momjian 已提交
2528 2529
		},
		&XLogArchiveCommand,
2530 2531
		"",
		NULL, NULL, show_archive_command
B
Bruce Momjian 已提交
2532
	},
2533

2534
	{
2535
		{"client_encoding", PGC_USERSET, CLIENT_CONN_LOCALE,
2536
			gettext_noop("Sets the client's character set encoding."),
2537
			NULL,
2538
			GUC_IS_NAME | GUC_REPORT
2539
		},
2540
		&client_encoding_string,
2541 2542
		"SQL_ASCII",
		check_client_encoding, assign_client_encoding, NULL
2543 2544
	},

B
Add:  
Bruce Momjian 已提交
2545 2546
	{
		{"log_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
2547 2548
			gettext_noop("Controls information prefixed to each log line."),
			gettext_noop("If blank, no prefix is used.")
B
Add:  
Bruce Momjian 已提交
2549 2550
		},
		&Log_line_prefix,
2551 2552
		"",
		NULL, NULL, NULL
B
Add:  
Bruce Momjian 已提交
2553 2554
	},

2555 2556 2557 2558 2559 2560
	{
		{"log_timezone", PGC_SIGHUP, LOGGING_WHAT,
			gettext_noop("Sets the time zone to use in log messages."),
			NULL
		},
		&log_timezone_string,
2561 2562
		NULL,
		check_log_timezone, assign_log_timezone, show_log_timezone
2563
	},
B
Add:  
Bruce Momjian 已提交
2564

2565
	{
2566
		{"DateStyle", PGC_USERSET, CLIENT_CONN_LOCALE,
2567
			gettext_noop("Sets the display format for date and time values."),
2568
			gettext_noop("Also controls interpretation of ambiguous "
2569
						 "date inputs."),
2570 2571
			GUC_LIST_INPUT | GUC_REPORT
		},
2572
		&datestyle_string,
2573 2574
		"ISO, MDY",
		check_datestyle, assign_datestyle, NULL
2575
	},
2576

2577 2578 2579
	{
		{"default_tablespace", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets the default tablespace to create tables and indexes in."),
2580
			gettext_noop("An empty string selects the database's default tablespace."),
B
Bruce Momjian 已提交
2581
			GUC_IS_NAME
2582 2583
		},
		&default_tablespace,
2584 2585
		"",
		check_default_tablespace, NULL, NULL
2586 2587
	},

2588 2589 2590 2591 2592 2593 2594
	{
		{"temp_tablespaces", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets the tablespace(s) to use for temporary tables and sort files."),
			NULL,
			GUC_LIST_INPUT | GUC_LIST_QUOTE
		},
		&temp_tablespaces,
2595 2596
		"",
		check_temp_tablespaces, assign_temp_tablespaces, NULL
2597 2598
	},

2599
	{
2600
		{"dynamic_library_path", PGC_SUSET, CLIENT_CONN_OTHER,
2601
			gettext_noop("Sets the path for dynamically loadable modules."),
2602
			gettext_noop("If a dynamically loadable module needs to be opened and "
2603
						 "the specified name does not have a directory component (i.e., the "
2604
						 "name does not contain a slash), the system will search this path for "
2605 2606
						 "the specified file."),
			GUC_SUPERUSER_ONLY
2607 2608
		},
		&Dynamic_library_path,
2609 2610
		"$libdir",
		NULL, NULL, NULL
2611 2612 2613
	},

	{
2614
		{"krb_server_keyfile", PGC_SIGHUP, CONN_AUTH_SECURITY,
2615
			gettext_noop("Sets the location of the Kerberos server key file."),
2616 2617
			NULL,
			GUC_SUPERUSER_ONLY
2618 2619
		},
		&pg_krb_server_keyfile,
2620 2621
		PG_KRB_SRVTAB,
		NULL, NULL, NULL
2622
	},
2623

B
 
Bruce Momjian 已提交
2624
	{
2625
		{"krb_srvname", PGC_SIGHUP, CONN_AUTH_SECURITY,
B
 
Bruce Momjian 已提交
2626 2627 2628 2629
			gettext_noop("Sets the name of the Kerberos service."),
			NULL
		},
		&pg_krb_srvnam,
2630 2631
		PG_KRB_SRVNAM,
		NULL, NULL, NULL
B
 
Bruce Momjian 已提交
2632 2633
	},

2634
	{
2635
		{"bonjour_name", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2636
			gettext_noop("Sets the Bonjour service name."),
2637 2638
			NULL
		},
2639
		&bonjour_name,
2640 2641
		"",
		NULL, NULL, NULL
2642 2643
	},

2644 2645
	/* See main.c about why defaults for LC_foo are not all alike */

2646
	{
2647
		{"lc_collate", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2648
			gettext_noop("Shows the collation order locale."),
2649 2650 2651 2652
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&locale_collate,
2653 2654
		"C",
		NULL, NULL, NULL
2655 2656 2657
	},

	{
2658
		{"lc_ctype", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2659
			gettext_noop("Shows the character classification and case conversion locale."),
2660 2661 2662 2663
			NULL,
			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
		&locale_ctype,
2664 2665
		"C",
		NULL, NULL, NULL
2666 2667
	},

2668
	{
2669
		{"lc_messages", PGC_SUSET, CLIENT_CONN_LOCALE,
2670
			gettext_noop("Sets the language in which messages are displayed."),
2671 2672 2673
			NULL
		},
		&locale_messages,
2674 2675
		"",
		check_locale_messages, assign_locale_messages, NULL
2676 2677 2678
	},

	{
2679
		{"lc_monetary", PGC_USERSET, CLIENT_CONN_LOCALE,
2680
			gettext_noop("Sets the locale for formatting monetary amounts."),
2681 2682 2683
			NULL
		},
		&locale_monetary,
2684 2685
		"C",
		check_locale_monetary, assign_locale_monetary, NULL
2686 2687 2688
	},

	{
2689
		{"lc_numeric", PGC_USERSET, CLIENT_CONN_LOCALE,
2690
			gettext_noop("Sets the locale for formatting numbers."),
2691 2692 2693
			NULL
		},
		&locale_numeric,
2694 2695
		"C",
		check_locale_numeric, assign_locale_numeric, NULL
2696 2697 2698
	},

	{
2699
		{"lc_time", PGC_USERSET, CLIENT_CONN_LOCALE,
2700
			gettext_noop("Sets the locale for formatting date and time values."),
2701 2702 2703
			NULL
		},
		&locale_time,
2704 2705
		"C",
		check_locale_time, assign_locale_time, NULL
2706 2707
	},

2708
	{
2709
		{"shared_preload_libraries", PGC_POSTMASTER, RESOURCES_KERNEL,
2710
			gettext_noop("Lists shared libraries to preload into server."),
2711
			NULL,
2712
			GUC_LIST_INPUT | GUC_LIST_QUOTE | GUC_SUPERUSER_ONLY
2713
		},
2714
		&shared_preload_libraries_string,
2715 2716
		"",
		NULL, NULL, NULL
2717 2718 2719 2720 2721 2722 2723 2724 2725
	},

	{
		{"local_preload_libraries", PGC_BACKEND, CLIENT_CONN_OTHER,
			gettext_noop("Lists shared libraries to preload into each backend."),
			NULL,
			GUC_LIST_INPUT | GUC_LIST_QUOTE
		},
		&local_preload_libraries_string,
2726 2727
		"",
		NULL, NULL, NULL
2728 2729
	},

2730
	{
2731
		{"search_path", PGC_USERSET, CLIENT_CONN_STATEMENT,
2732
			gettext_noop("Sets the schema search order for names that are not schema-qualified."),
2733 2734 2735
			NULL,
			GUC_LIST_INPUT | GUC_LIST_QUOTE
		},
2736
		&namespace_search_path,
2737 2738
		"\"$user\",public",
		check_search_path, assign_search_path, NULL
2739 2740 2741
	},

	{
2742 2743
		/* Can't be set in postgresql.conf */
		{"server_encoding", PGC_INTERNAL, CLIENT_CONN_LOCALE,
2744
			gettext_noop("Sets the server (database) character set encoding."),
2745
			NULL,
2746
			GUC_IS_NAME | GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
2747
		},
2748
		&server_encoding_string,
2749 2750
		"SQL_ASCII",
		NULL, NULL, NULL
2751 2752 2753
	},

	{
2754
		/* Can't be set in postgresql.conf */
2755
		{"server_version", PGC_INTERNAL, PRESET_OPTIONS,
2756
			gettext_noop("Shows the server version."),
2757 2758 2759
			NULL,
			GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
2760
		&server_version_string,
2761 2762
		PG_VERSION,
		NULL, NULL, NULL
2763 2764
	},

2765 2766 2767 2768 2769
	{
		/* Not for general use --- used by SET ROLE */
		{"role", PGC_USERSET, UNGROUPED,
			gettext_noop("Sets the current role."),
			NULL,
2770
			GUC_IS_NAME | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_NOT_WHILE_SEC_REST
2771 2772
		},
		&role_string,
2773 2774
		"none",
		check_role, assign_role, show_role
2775 2776
	},

2777
	{
2778 2779
		/* Not for general use --- used by SET SESSION AUTHORIZATION */
		{"session_authorization", PGC_USERSET, UNGROUPED,
2780
			gettext_noop("Sets the session user name."),
2781
			NULL,
2782
			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
2783
		},
2784
		&session_authorization_string,
2785 2786
		NULL,
		check_session_authorization, assign_session_authorization, NULL
2787 2788
	},

2789
	{
2790
		{"log_destination", PGC_SIGHUP, LOGGING_WHERE,
B
Bruce Momjian 已提交
2791
			gettext_noop("Sets the destination for server log output."),
2792 2793 2794
			gettext_noop("Valid values are combinations of \"stderr\", "
						 "\"syslog\", \"csvlog\", and \"eventlog\", "
						 "depending on the platform."),
B
Bruce Momjian 已提交
2795
			GUC_LIST_INPUT
2796 2797
		},
		&log_destination_string,
2798 2799
		"stderr",
		check_log_destination, assign_log_destination, NULL
2800
	},
2801 2802
	{
		{"log_directory", PGC_SIGHUP, LOGGING_WHERE,
2803
			gettext_noop("Sets the destination directory for log files."),
2804
			gettext_noop("Can be specified as relative to the data directory "
2805 2806
						 "or as absolute path."),
			GUC_SUPERUSER_ONLY
2807 2808
		},
		&Log_directory,
2809 2810
		"pg_log",
		check_canonical_path, NULL, NULL
2811 2812
	},
	{
2813 2814
		{"log_filename", PGC_SIGHUP, LOGGING_WHERE,
			gettext_noop("Sets the file name pattern for log files."),
2815 2816
			NULL,
			GUC_SUPERUSER_ONLY
2817
		},
2818
		&Log_filename,
2819 2820
		"postgresql-%Y-%m-%d_%H%M%S.log",
		NULL, NULL, NULL
2821
	},
2822

2823
	{
2824 2825 2826
		{"syslog_ident", PGC_SIGHUP, LOGGING_WHERE,
			gettext_noop("Sets the program name used to identify PostgreSQL "
						 "messages in syslog."),
2827 2828
			NULL
		},
2829
		&syslog_ident_str,
2830 2831
		"postgres",
		NULL, assign_syslog_ident, NULL
2832
	},
2833

2834
	{
2835
		{"TimeZone", PGC_USERSET, CLIENT_CONN_LOCALE,
2836
			gettext_noop("Sets the time zone for displaying and interpreting time stamps."),
2837 2838
			NULL,
			GUC_REPORT
2839 2840
		},
		&timezone_string,
2841 2842
		NULL,
		check_timezone, assign_timezone, show_timezone
2843
	},
2844 2845
	{
		{"timezone_abbreviations", PGC_USERSET, CLIENT_CONN_LOCALE,
2846
			gettext_noop("Selects a file of time zone abbreviations."),
2847
			NULL
2848
		},
2849
		&timezone_abbreviations_string,
2850 2851
		NULL,
		check_timezone_abbreviations, assign_timezone_abbreviations, NULL
2852
	},
2853 2854

	{
2855
		{"transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
2856
			gettext_noop("Sets the current transaction's isolation level."),
2857 2858 2859
			NULL,
			GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
		},
2860
		&XactIsoLevel_string,
2861 2862
		"default",
		check_XactIsoLevel, assign_XactIsoLevel, show_XactIsoLevel
2863 2864 2865
	},

	{
2866
		{"unix_socket_group", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2867
			gettext_noop("Sets the owning group of the Unix-domain socket."),
2868 2869
			gettext_noop("The owning user of the socket is always the user "
						 "that starts the server.")
2870 2871
		},
		&Unix_socket_group,
2872 2873
		"",
		NULL, NULL, NULL
2874
	},
2875

2876
	{
2877
		{"unix_socket_directory", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2878
			gettext_noop("Sets the directory where the Unix-domain socket will be created."),
2879 2880
			NULL,
			GUC_SUPERUSER_ONLY
2881 2882
		},
		&UnixSocketDir,
2883 2884
		"",
		check_canonical_path, NULL, NULL
2885
	},
2886

2887
	{
2888
		{"listen_addresses", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
2889 2890 2891
			gettext_noop("Sets the host name or IP address(es) to listen to."),
			NULL,
			GUC_LIST_INPUT
2892
		},
2893
		&ListenAddresses,
2894 2895
		"localhost",
		NULL, NULL, NULL
2896
	},
2897

2898
	{
2899
		{"custom_variable_classes", PGC_SIGHUP, CUSTOM_OPTIONS,
2900
			gettext_noop("Sets the list of known custom variable classes."),
2901 2902 2903 2904
			NULL,
			GUC_LIST_INPUT | GUC_LIST_QUOTE
		},
		&custom_variable_classes,
2905 2906
		NULL,
		check_custom_variable_classes, NULL, NULL
2907 2908
	},

2909
	{
2910
		{"data_directory", PGC_POSTMASTER, FILE_LOCATIONS,
B
Bruce Momjian 已提交
2911 2912 2913
			gettext_noop("Sets the server's data directory."),
			NULL,
			GUC_SUPERUSER_ONLY
2914
		},
2915
		&data_directory,
2916
		NULL,
2917
		NULL, NULL, NULL
2918 2919 2920
	},

	{
2921
		{"config_file", PGC_POSTMASTER, FILE_LOCATIONS,
B
Bruce Momjian 已提交
2922 2923 2924
			gettext_noop("Sets the server's main configuration file."),
			NULL,
			GUC_DISALLOW_IN_FILE | GUC_SUPERUSER_ONLY
2925 2926
		},
		&ConfigFileName,
2927
		NULL,
2928 2929 2930 2931 2932
		NULL, NULL, NULL
	},

	{
		{"hba_file", PGC_POSTMASTER, FILE_LOCATIONS,
2933
			gettext_noop("Sets the server's \"hba\" configuration file."),
B
Bruce Momjian 已提交
2934 2935
			NULL,
			GUC_SUPERUSER_ONLY
2936
		},
2937
		&HbaFileName,
2938
		NULL,
2939
		NULL, NULL, NULL
2940 2941 2942
	},

	{
2943
		{"ident_file", PGC_POSTMASTER, FILE_LOCATIONS,
2944
			gettext_noop("Sets the server's \"ident\" configuration file."),
B
Bruce Momjian 已提交
2945 2946
			NULL,
			GUC_SUPERUSER_ONLY
2947
		},
2948
		&IdentFileName,
2949
		NULL,
2950
		NULL, NULL, NULL
2951 2952 2953
	},

	{
2954
		{"external_pid_file", PGC_POSTMASTER, FILE_LOCATIONS,
B
Bruce Momjian 已提交
2955 2956 2957
			gettext_noop("Writes the postmaster PID to the specified file."),
			NULL,
			GUC_SUPERUSER_ONLY
2958
		},
2959
		&external_pid_file,
2960 2961
		NULL,
		check_canonical_path, NULL, NULL
2962 2963
	},

2964
	{
2965
		{"stats_temp_directory", PGC_SIGHUP, STATS_COLLECTOR,
2966 2967 2968 2969 2970
			gettext_noop("Writes temporary statistics files to the specified directory."),
			NULL,
			GUC_SUPERUSER_ONLY
		},
		&pgstat_temp_directory,
2971 2972
		"pg_stat_tmp",
		check_canonical_path, assign_pgstat_temp_directory, NULL
2973 2974
	},

2975
	{
2976
		{"synchronous_standby_names", PGC_SIGHUP, REPLICATION_MASTER,
P
Peter Eisentraut 已提交
2977
			gettext_noop("List of names of potential synchronous standbys."),
2978 2979 2980 2981
			NULL,
			GUC_LIST_INPUT
		},
		&SyncRepStandbyNames,
2982 2983
		"",
		check_synchronous_standby_names, NULL, NULL
2984 2985
	},

2986 2987 2988 2989 2990 2991
	{
		{"default_text_search_config", PGC_USERSET, CLIENT_CONN_LOCALE,
			gettext_noop("Sets default text search configuration."),
			NULL
		},
		&TSCurrentConfig,
2992 2993
		"pg_catalog.simple",
		check_TSCurrentConfig, assign_TSCurrentConfig, NULL
2994 2995
	},

B
Bruce Momjian 已提交
2996 2997 2998 2999 3000 3001 3002
	{
		{"ssl_ciphers", PGC_POSTMASTER, CONN_AUTH_SECURITY,
			gettext_noop("Sets the list of allowed SSL ciphers."),
			NULL,
			GUC_SUPERUSER_ONLY
		},
		&SSLCipherSuites,
3003 3004 3005 3006 3007
#ifdef USE_SSL
		"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH",
#else
		"none",
#endif
3008
		NULL, NULL, NULL
B
Bruce Momjian 已提交
3009
	},
T
Tom Lane 已提交
3010

3011
	{
3012
		{"application_name", PGC_USERSET, LOGGING_WHAT,
B
Bruce Momjian 已提交
3013 3014 3015
			gettext_noop("Sets the application name to be reported in statistics and logs."),
			NULL,
			GUC_IS_NAME | GUC_REPORT | GUC_NOT_IN_SAMPLE
3016 3017
		},
		&application_name,
3018 3019
		"",
		check_application_name, assign_application_name, NULL
3020 3021
	},

3022
	/* End-of-list marker */
3023
	{
3024
		{NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL
3025
	}
3026 3027 3028
};


3029 3030
static struct config_enum ConfigureNamesEnum[] =
{
3031 3032 3033
	{
		{"backslash_quote", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
			gettext_noop("Sets whether \"\\'\" is allowed in string literals."),
3034
			NULL
3035 3036
		},
		&backslash_quote,
3037 3038
		BACKSLASH_QUOTE_SAFE_ENCODING, backslash_quote_options,
		NULL, NULL, NULL
3039 3040
	},

3041 3042 3043 3044 3045 3046
	{
		{"bytea_output", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets the output format for bytea."),
			NULL
		},
		&bytea_output,
3047 3048
		BYTEA_OUTPUT_HEX, bytea_output_options,
		NULL, NULL, NULL
3049 3050
	},

3051 3052 3053
	{
		{"client_min_messages", PGC_USERSET, LOGGING_WHEN,
			gettext_noop("Sets the message levels that are sent to the client."),
3054 3055
			gettext_noop("Each level includes all the levels that follow it. The later"
						 " the level, the fewer messages are sent.")
3056 3057
		},
		&client_min_messages,
3058 3059
		NOTICE, client_message_level_options,
		NULL, NULL, NULL
3060 3061
	},

3062 3063 3064 3065 3066 3067 3068 3069
	{
		{"constraint_exclusion", PGC_USERSET, QUERY_TUNING_OTHER,
			gettext_noop("Enables the planner to use constraints to optimize queries."),
			gettext_noop("Table scans will be skipped if their constraints"
						 " guarantee that no rows match the query.")
		},
		&constraint_exclusion,
		CONSTRAINT_EXCLUSION_PARTITION, constraint_exclusion_options,
3070
		NULL, NULL, NULL
3071 3072
	},

3073 3074 3075
	{
		{"default_transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets the transaction isolation level of each new transaction."),
3076
			NULL
3077 3078
		},
		&DefaultXactIsoLevel,
3079 3080
		XACT_READ_COMMITTED, isolation_level_options,
		NULL, NULL, NULL
3081 3082
	},

3083
	{
3084 3085 3086 3087 3088 3089
		{"IntervalStyle", PGC_USERSET, CLIENT_CONN_LOCALE,
			gettext_noop("Sets the display format for interval values."),
			NULL,
			GUC_REPORT
		},
		&IntervalStyle,
3090 3091
		INTSTYLE_POSTGRES, intervalstyle_options,
		NULL, NULL, NULL
3092 3093
	},

3094
	{
3095
		{"log_error_verbosity", PGC_SUSET, LOGGING_WHAT,
3096
			gettext_noop("Sets the verbosity of logged messages."),
3097
			NULL
3098 3099
		},
		&Log_error_verbosity,
3100 3101
		PGERROR_DEFAULT, log_error_verbosity_options,
		NULL, NULL, NULL
3102 3103 3104 3105 3106
	},

	{
		{"log_min_messages", PGC_SUSET, LOGGING_WHEN,
			gettext_noop("Sets the message levels that are logged."),
3107 3108
			gettext_noop("Each level includes all the levels that follow it. The later"
						 " the level, the fewer messages are sent.")
3109 3110
		},
		&log_min_messages,
3111 3112
		WARNING, server_message_level_options,
		NULL, NULL, NULL
3113 3114 3115 3116 3117
	},

	{
		{"log_min_error_statement", PGC_SUSET, LOGGING_WHEN,
			gettext_noop("Causes all statements generating error at or above this level to be logged."),
3118 3119
			gettext_noop("Each level includes all the levels that follow it. The later"
						 " the level, the fewer messages are sent.")
3120 3121
		},
		&log_min_error_statement,
3122 3123
		ERROR, server_message_level_options,
		NULL, NULL, NULL
3124 3125 3126 3127 3128
	},

	{
		{"log_statement", PGC_SUSET, LOGGING_WHAT,
			gettext_noop("Sets the type of statements logged."),
3129
			NULL
3130 3131
		},
		&log_statement,
3132 3133
		LOGSTMT_NONE, log_statement_options,
		NULL, NULL, NULL
3134 3135
	},

3136 3137 3138
	{
		{"syslog_facility", PGC_SIGHUP, LOGGING_WHERE,
			gettext_noop("Sets the syslog \"facility\" to be used when syslog enabled."),
3139
			NULL
3140 3141
		},
		&syslog_facility,
3142 3143 3144 3145
#ifdef HAVE_SYSLOG
		LOG_LOCAL0,
#else
		0,
3146
#endif
3147 3148
		syslog_facility_options,
		NULL, assign_syslog_facility, NULL
3149
	},
3150

3151 3152 3153
	{
		{"session_replication_role", PGC_SUSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets the session's behavior for triggers and rewrite rules."),
3154
			NULL
3155 3156 3157
		},
		&SessionReplicationRole,
		SESSION_REPLICATION_ROLE_ORIGIN, session_replication_role_options,
3158
		NULL, assign_session_replication_role, NULL
3159
	},
3160

3161 3162 3163 3164 3165 3166 3167
	{
		{"synchronous_commit", PGC_USERSET, WAL_SETTINGS,
			gettext_noop("Sets the current transaction's synchronization level."),
			NULL
		},
		&synchronous_commit,
		SYNCHRONOUS_COMMIT_ON, synchronous_commit_options,
3168
		NULL, NULL, NULL
3169 3170
	},

3171
	{
3172 3173
		{"trace_recovery_messages", PGC_SIGHUP, DEVELOPER_OPTIONS,
			gettext_noop("Enables logging of recovery-related debugging information."),
3174 3175 3176 3177
			gettext_noop("Each level includes all the levels that follow it. The later"
						 " the level, the fewer messages are sent.")
		},
		&trace_recovery_messages,
3178

3179
		/*
3180 3181
		 * client_message_level_options allows too many values, really, but
		 * it's not worth having a separate options array for this.
3182
		 */
3183 3184
		LOG, client_message_level_options,
		NULL, NULL, NULL
3185 3186
	},

3187 3188 3189
	{
		{"track_functions", PGC_SUSET, STATS_COLLECTOR,
			gettext_noop("Collects function-level statistics on database activity."),
3190
			NULL
3191 3192
		},
		&pgstat_track_functions,
3193 3194
		TRACK_FUNC_OFF, track_function_options,
		NULL, NULL, NULL
3195 3196
	},

3197 3198 3199 3200 3201 3202
	{
		{"wal_level", PGC_POSTMASTER, WAL_SETTINGS,
			gettext_noop("Set the level of information written to the WAL."),
			NULL
		},
		&wal_level,
3203 3204
		WAL_LEVEL_MINIMAL, wal_level_options,
		NULL, NULL, NULL
3205 3206
	},

3207 3208 3209 3210 3211 3212
	{
		{"wal_sync_method", PGC_SIGHUP, WAL_SETTINGS,
			gettext_noop("Selects the method used for forcing WAL updates to disk."),
			NULL
		},
		&sync_method,
3213
		DEFAULT_SYNC_METHOD, sync_method_options,
3214
		NULL, assign_xlog_sync_method, NULL
3215 3216
	},

3217 3218 3219
	{
		{"xmlbinary", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets how binary values are to be encoded in XML."),
3220
			NULL
3221 3222
		},
		&xmlbinary,
3223 3224
		XMLBINARY_BASE64, xmlbinary_options,
		NULL, NULL, NULL
3225 3226 3227 3228 3229 3230
	},

	{
		{"xmloption", PGC_USERSET, CLIENT_CONN_STATEMENT,
			gettext_noop("Sets whether XML data in implicit parsing and serialization "
						 "operations is to be considered as documents or content fragments."),
3231
			NULL
3232 3233
		},
		&xmloption,
3234 3235
		XMLOPTION_CONTENT, xmloption_options,
		NULL, NULL, NULL
3236 3237
	},

3238 3239 3240

	/* End-of-list marker */
	{
3241
		{NULL, 0, 0, NULL, NULL}, NULL, 0, NULL, NULL, NULL, NULL
3242 3243 3244
	}
};

3245
/******** end of options list ********/
3246

B
Bruce Momjian 已提交
3247

3248 3249 3250 3251 3252 3253
/*
 * To allow continued support of obsolete names for GUC variables, we apply
 * the following mappings to any unrecognized name.  Note that an old name
 * should be mapped to a new one only if the new variable has very similar
 * semantics to the old.
 */
B
Bruce Momjian 已提交
3254
static const char *const map_old_guc_names[] = {
3255 3256 3257 3258 3259 3260
	"sort_mem", "work_mem",
	"vacuum_mem", "maintenance_work_mem",
	NULL
};


3261
/*
3262
 * Actual lookup of variables is done through this single, sorted array.
3263
 */
3264 3265
static struct config_generic **guc_variables;

3266
/* Current number of variables contained in the vector */
B
Bruce Momjian 已提交
3267
static int	num_guc_variables;
3268

3269
/* Vector capacity */
B
Bruce Momjian 已提交
3270
static int	size_guc_variables;
3271

3272

3273
static bool guc_dirty;			/* TRUE if need to do commit/abort work */
3274

3275 3276
static bool reporting_enabled;	/* TRUE to enable GUC_REPORT */

3277 3278
static int	GUCNestLevel = 0;	/* 1 when in main transaction */

3279

B
Bruce Momjian 已提交
3280
static int	guc_var_compare(const void *a, const void *b);
3281
static int	guc_name_compare(const char *namea, const char *nameb);
3282
static void InitializeGUCOptionsFromEnvironment(void);
3283
static void InitializeOneGUCOption(struct config_generic * gconf);
3284
static void push_old_value(struct config_generic * gconf, GucAction action);
3285
static void ReportGUCOption(struct config_generic * record);
3286 3287
static void ShowGUCConfigOption(const char *name, DestReceiver *dest);
static void ShowAllGUCConfig(DestReceiver *dest);
3288
static char *_ShowOption(struct config_generic * record, bool use_units);
3289
static bool validate_option_array_item(const char *name, const char *value,
B
Bruce Momjian 已提交
3290
						   bool skipIfNoPermissions);
3291

3292

3293 3294 3295 3296 3297 3298
/*
 * Some infrastructure for checking malloc/strdup/realloc calls
 */
static void *
guc_malloc(int elevel, size_t size)
{
B
Bruce Momjian 已提交
3299
	void	   *data;
3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311

	data = malloc(size);
	if (data == NULL)
		ereport(elevel,
				(errcode(ERRCODE_OUT_OF_MEMORY),
				 errmsg("out of memory")));
	return data;
}

static void *
guc_realloc(int elevel, void *old, size_t size)
{
B
Bruce Momjian 已提交
3312
	void	   *data;
3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324

	data = realloc(old, size);
	if (data == NULL)
		ereport(elevel,
				(errcode(ERRCODE_OUT_OF_MEMORY),
				 errmsg("out of memory")));
	return data;
}

static char *
guc_strdup(int elevel, const char *src)
{
B
Bruce Momjian 已提交
3325
	char	   *data;
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335

	data = strdup(src);
	if (data == NULL)
		ereport(elevel,
				(errcode(ERRCODE_OUT_OF_MEMORY),
				 errmsg("out of memory")));
	return data;
}


3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
/*
 * Detect whether strval is referenced anywhere in a GUC string item
 */
static bool
string_field_used(struct config_string * conf, char *strval)
{
	GucStack   *stack;

	if (strval == *(conf->variable) ||
		strval == conf->reset_val ||
		strval == conf->boot_val)
		return true;
	for (stack = conf->gen.stack; stack; stack = stack->prev)
	{
		if (strval == stack->prior.val.stringval ||
			strval == stack->masked.val.stringval)
			return true;
	}
	return false;
}

3357 3358 3359 3360 3361 3362
/*
 * Support for assigning to a field of a string GUC item.  Free the prior
 * value if it's not referenced anywhere else in the item (including stacked
 * states).
 */
static void
3363
set_string_field(struct config_string * conf, char **field, char *newval)
3364
{
B
Bruce Momjian 已提交
3365
	char	   *oldval = *field;
3366 3367 3368 3369

	/* Do the assignment */
	*field = newval;

3370 3371 3372
	/* Free old value if it's not NULL and isn't referenced anymore */
	if (oldval && !string_field_used(conf, oldval))
		free(oldval);
3373 3374 3375
}

/*
3376
 * Detect whether an "extra" struct is referenced anywhere in a GUC item
3377 3378
 */
static bool
3379
extra_field_used(struct config_generic * gconf, void *extra)
3380
{
B
Bruce Momjian 已提交
3381
	GucStack   *stack;
3382

3383
	if (extra == gconf->extra)
3384
		return true;
3385
	switch (gconf->vartype)
3386
	{
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411
		case PGC_BOOL:
			if (extra == ((struct config_bool *) gconf)->reset_extra)
				return true;
			break;
		case PGC_INT:
			if (extra == ((struct config_int *) gconf)->reset_extra)
				return true;
			break;
		case PGC_REAL:
			if (extra == ((struct config_real *) gconf)->reset_extra)
				return true;
			break;
		case PGC_STRING:
			if (extra == ((struct config_string *) gconf)->reset_extra)
				return true;
			break;
		case PGC_ENUM:
			if (extra == ((struct config_enum *) gconf)->reset_extra)
				return true;
			break;
	}
	for (stack = gconf->stack; stack; stack = stack->prev)
	{
		if (extra == stack->prior.extra ||
			extra == stack->masked.extra)
3412 3413
			return true;
	}
3414

3415 3416 3417
	return false;
}

3418
/*
3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441
 * Support for assigning to an "extra" field of a GUC item.  Free the prior
 * value if it's not referenced anywhere else in the item (including stacked
 * states).
 */
static void
set_extra_field(struct config_generic * gconf, void **field, void *newval)
{
	void	   *oldval = *field;

	/* Do the assignment */
	*field = newval;

	/* Free old value if it's not NULL and isn't referenced anymore */
	if (oldval && !extra_field_used(gconf, oldval))
		free(oldval);
}

/*
 * Support for copying a variable's active value into a stack entry.
 * The "extra" field associated with the active value is copied, too.
 *
 * NB: be sure stringval and extra fields of a new stack entry are
 * initialized to NULL before this is used, else we'll try to free() them.
3442 3443
 */
static void
3444
set_stack_value(struct config_generic * gconf, config_var_value *val)
3445 3446 3447 3448
{
	switch (gconf->vartype)
	{
		case PGC_BOOL:
3449
			val->val.boolval =
3450 3451 3452
				*((struct config_bool *) gconf)->variable;
			break;
		case PGC_INT:
3453
			val->val.intval =
3454 3455 3456
				*((struct config_int *) gconf)->variable;
			break;
		case PGC_REAL:
3457
			val->val.realval =
3458 3459 3460 3461
				*((struct config_real *) gconf)->variable;
			break;
		case PGC_STRING:
			set_string_field((struct config_string *) gconf,
3462
							 &(val->val.stringval),
3463 3464
							 *((struct config_string *) gconf)->variable);
			break;
3465
		case PGC_ENUM:
3466
			val->val.enumval =
3467 3468
				*((struct config_enum *) gconf)->variable;
			break;
3469
	}
3470
	set_extra_field(gconf, &(val->extra), gconf->extra);
3471 3472 3473
}

/*
3474 3475
 * Support for discarding a no-longer-needed value in a stack entry.
 * The "extra" field associated with the stack entry is cleared, too.
3476 3477
 */
static void
3478
discard_stack_value(struct config_generic * gconf, config_var_value *val)
3479 3480 3481 3482 3483 3484
{
	switch (gconf->vartype)
	{
		case PGC_BOOL:
		case PGC_INT:
		case PGC_REAL:
3485
		case PGC_ENUM:
3486 3487 3488 3489
			/* no need to do anything */
			break;
		case PGC_STRING:
			set_string_field((struct config_string *) gconf,
3490
							 &(val->val.stringval),
3491 3492 3493
							 NULL);
			break;
	}
3494
	set_extra_field(gconf, &(val->extra), NULL);
3495 3496
}

3497

3498 3499 3500
/*
 * Fetch the sorted array pointer (exported for help_config.c's use ONLY)
 */
3501 3502
struct config_generic **
get_guc_variables(void)
3503 3504 3505
{
	return guc_variables;
}
3506

3507

3508
/*
B
Bruce Momjian 已提交
3509
 * Build the sorted array.	This is split out so that it could be
3510 3511
 * re-executed after startup (eg, we could allow loadable modules to
 * add vars, and then we'd need to re-sort).
3512
 */
3513
void
3514
build_guc_variables(void)
3515
{
B
Bruce Momjian 已提交
3516
	int			size_vars;
3517 3518
	int			num_vars = 0;
	struct config_generic **guc_vars;
B
Bruce Momjian 已提交
3519
	int			i;
3520

3521
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
3522 3523 3524
	{
		struct config_bool *conf = &ConfigureNamesBool[i];

3525 3526 3527
		/* Rather than requiring vartype to be filled in by hand, do this: */
		conf->gen.vartype = PGC_BOOL;
		num_vars++;
3528
	}
3529

3530
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
3531 3532 3533
	{
		struct config_int *conf = &ConfigureNamesInt[i];

3534 3535
		conf->gen.vartype = PGC_INT;
		num_vars++;
3536
	}
3537

3538
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
3539 3540 3541
	{
		struct config_real *conf = &ConfigureNamesReal[i];

3542 3543
		conf->gen.vartype = PGC_REAL;
		num_vars++;
3544
	}
3545

3546
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
3547
	{
3548
		struct config_string *conf = &ConfigureNamesString[i];
3549

3550 3551
		conf->gen.vartype = PGC_STRING;
		num_vars++;
3552 3553
	}

3554 3555 3556 3557 3558 3559 3560 3561
	for (i = 0; ConfigureNamesEnum[i].gen.name; i++)
	{
		struct config_enum *conf = &ConfigureNamesEnum[i];

		conf->gen.vartype = PGC_ENUM;
		num_vars++;
	}

B
Bruce Momjian 已提交
3562 3563
	/*
	 * Create table with 20% slack
3564 3565 3566
	 */
	size_vars = num_vars + num_vars / 4;

3567
	guc_vars = (struct config_generic **)
3568
		guc_malloc(FATAL, size_vars * sizeof(struct config_generic *));
3569

3570
	num_vars = 0;
3571

3572
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
B
Bruce Momjian 已提交
3573
		guc_vars[num_vars++] = &ConfigureNamesBool[i].gen;
3574

3575
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
B
Bruce Momjian 已提交
3576
		guc_vars[num_vars++] = &ConfigureNamesInt[i].gen;
3577

3578
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
B
Bruce Momjian 已提交
3579
		guc_vars[num_vars++] = &ConfigureNamesReal[i].gen;
3580

3581
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
B
Bruce Momjian 已提交
3582
		guc_vars[num_vars++] = &ConfigureNamesString[i].gen;
3583

3584 3585 3586
	for (i = 0; ConfigureNamesEnum[i].gen.name; i++)
		guc_vars[num_vars++] = &ConfigureNamesEnum[i].gen;

3587 3588 3589 3590
	if (guc_variables)
		free(guc_variables);
	guc_variables = guc_vars;
	num_guc_variables = num_vars;
3591
	size_guc_variables = size_vars;
B
Bruce Momjian 已提交
3592 3593
	qsort((void *) guc_variables, num_guc_variables,
		  sizeof(struct config_generic *), guc_var_compare);
3594 3595
}

3596 3597 3598 3599
/*
 * Add a new GUC variable to the list of known variables. The
 * list is expanded if needed.
 */
3600
static bool
B
Bruce Momjian 已提交
3601
add_guc_variable(struct config_generic * var, int elevel)
3602
{
B
Bruce Momjian 已提交
3603
	if (num_guc_variables + 1 >= size_guc_variables)
3604
	{
B
Bruce Momjian 已提交
3605 3606
		/*
		 * Increase the vector by 25%
3607
		 */
B
Bruce Momjian 已提交
3608 3609
		int			size_vars = size_guc_variables + size_guc_variables / 4;
		struct config_generic **guc_vars;
3610

B
Bruce Momjian 已提交
3611
		if (size_vars == 0)
3612
		{
3613
			size_vars = 100;
B
Bruce Momjian 已提交
3614 3615
			guc_vars = (struct config_generic **)
				guc_malloc(elevel, size_vars * sizeof(struct config_generic *));
3616 3617
		}
		else
3618
		{
B
Bruce Momjian 已提交
3619 3620
			guc_vars = (struct config_generic **)
				guc_realloc(elevel, guc_variables, size_vars * sizeof(struct config_generic *));
3621 3622
		}

B
Bruce Momjian 已提交
3623
		if (guc_vars == NULL)
3624 3625
			return false;		/* out of memory */

3626 3627 3628 3629
		guc_variables = guc_vars;
		size_guc_variables = size_vars;
	}
	guc_variables[num_guc_variables++] = var;
B
Bruce Momjian 已提交
3630 3631
	qsort((void *) guc_variables, num_guc_variables,
		  sizeof(struct config_generic *), guc_var_compare);
3632
	return true;
3633 3634 3635
}

/*
3636
 * Create and add a placeholder variable. It's presumed to belong
3637 3638
 * to a valid custom variable class at this point.
 */
3639
static struct config_generic *
3640
add_placeholder_variable(const char *name, int elevel)
3641
{
B
Bruce Momjian 已提交
3642 3643 3644
	size_t		sz = sizeof(struct config_string) + sizeof(char *);
	struct config_string *var;
	struct config_generic *gen;
3645

B
Bruce Momjian 已提交
3646 3647
	var = (struct config_string *) guc_malloc(elevel, sz);
	if (var == NULL)
3648
		return NULL;
3649
	memset(var, 0, sz);
3650
	gen = &var->gen;
3651

3652 3653
	gen->name = guc_strdup(elevel, name);
	if (gen->name == NULL)
3654 3655 3656 3657 3658
	{
		free(var);
		return NULL;
	}

B
Bruce Momjian 已提交
3659 3660
	gen->context = PGC_USERSET;
	gen->group = CUSTOM_OPTIONS;
3661
	gen->short_desc = "GUC placeholder variable";
B
Bruce Momjian 已提交
3662 3663
	gen->flags = GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE | GUC_CUSTOM_PLACEHOLDER;
	gen->vartype = PGC_STRING;
3664

B
Bruce Momjian 已提交
3665 3666
	/*
	 * The char* is allocated at the end of the struct since we have no
B
Bruce Momjian 已提交
3667 3668
	 * 'static' place to point to.	Note that the current value, as well as
	 * the boot and reset values, start out NULL.
B
Bruce Momjian 已提交
3669 3670
	 */
	var->variable = (char **) (var + 1);
3671

B
Bruce Momjian 已提交
3672
	if (!add_guc_variable((struct config_generic *) var, elevel))
3673 3674 3675 3676 3677 3678
	{
		free((void *) gen->name);
		free(var);
		return NULL;
	}

3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
	return gen;
}

/*
 * Detect whether the portion of "name" before dotPos matches any custom
 * variable class name listed in custom_var_classes.  The latter must be
 * formatted the way that assign_custom_variable_classes does it, ie,
 * no whitespace.  NULL is valid for custom_var_classes.
 */
static bool
is_custom_class(const char *name, int dotPos, const char *custom_var_classes)
{
	bool		result = false;
	const char *ccs = custom_var_classes;

	if (ccs != NULL)
	{
		const char *start = ccs;

		for (;; ++ccs)
		{
			char		c = *ccs;

			if (c == '\0' || c == ',')
			{
				if (dotPos == ccs - start && strncmp(start, name, dotPos) == 0)
				{
					result = true;
					break;
				}
				if (c == '\0')
					break;
				start = ccs + 1;
			}
		}
	}
	return result;
3716
}
3717 3718

/*
3719 3720 3721
 * Look up option NAME.  If it exists, return a pointer to its record,
 * else return NULL.  If create_placeholders is TRUE, we'll create a
 * placeholder record for a valid-looking custom variable name.
3722
 */
3723
static struct config_generic *
3724
find_option(const char *name, bool create_placeholders, int elevel)
3725
{
3726 3727
	const char **key = &name;
	struct config_generic **res;
3728
	int			i;
3729

3730
	Assert(name);
3731

3732
	/*
B
Bruce Momjian 已提交
3733 3734
	 * By equating const char ** with struct config_generic *, we are assuming
	 * the name field is first in config_generic.
3735
	 */
B
Bruce Momjian 已提交
3736 3737 3738
	res = (struct config_generic **) bsearch((void *) &key,
											 (void *) guc_variables,
											 num_guc_variables,
B
Bruce Momjian 已提交
3739
											 sizeof(struct config_generic *),
B
Bruce Momjian 已提交
3740
											 guc_var_compare);
3741 3742
	if (res)
		return *res;
3743 3744

	/*
B
Bruce Momjian 已提交
3745 3746 3747
	 * See if the name is an obsolete name for a variable.	We assume that the
	 * set of supported old names is short enough that a brute-force search is
	 * the best way.
3748 3749 3750 3751
	 */
	for (i = 0; map_old_guc_names[i] != NULL; i += 2)
	{
		if (guc_name_compare(name, map_old_guc_names[i]) == 0)
3752
			return find_option(map_old_guc_names[i + 1], false, elevel);
3753 3754
	}

3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
	if (create_placeholders)
	{
		/*
		 * Check if the name is qualified, and if so, check if the qualifier
		 * matches any custom variable class.  If so, add a placeholder.
		 */
		const char *dot = strchr(name, GUC_QUALIFIER_SEPARATOR);

		if (dot != NULL &&
			is_custom_class(name, dot - name, custom_variable_classes))
			return add_placeholder_variable(name, elevel);
	}
3767

3768
	/* Unknown name */
3769 3770
	return NULL;
}
3771 3772 3773


/*
3774
 * comparator for qsorting and bsearching guc_variables array
3775
 */
3776 3777
static int
guc_var_compare(const void *a, const void *b)
3778
{
3779 3780
	struct config_generic *confa = *(struct config_generic **) a;
	struct config_generic *confb = *(struct config_generic **) b;
3781

3782 3783 3784
	return guc_name_compare(confa->name, confb->name);
}

3785 3786 3787
/*
 * the bare comparison function for GUC names
 */
3788 3789 3790
static int
guc_name_compare(const char *namea, const char *nameb)
{
3791
	/*
B
Bruce Momjian 已提交
3792 3793 3794
	 * The temptation to use strcasecmp() here must be resisted, because the
	 * array ordering has to remain stable across setlocale() calls. So, build
	 * our own with a simple ASCII-only downcasing.
3795
	 */
3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
	while (*namea && *nameb)
	{
		char		cha = *namea++;
		char		chb = *nameb++;

		if (cha >= 'A' && cha <= 'Z')
			cha += 'a' - 'A';
		if (chb >= 'A' && chb <= 'Z')
			chb += 'a' - 'A';
		if (cha != chb)
			return cha - chb;
3807
	}
3808 3809 3810 3811 3812
	if (*namea)
		return 1;				/* a is longer */
	if (*nameb)
		return -1;				/* b is longer */
	return 0;
3813
}
3814 3815 3816


/*
3817
 * Initialize GUC options during program startup.
3818 3819 3820
 *
 * Note that we cannot read the config file yet, since we have not yet
 * processed command-line switches.
3821
 */
3822 3823
void
InitializeGUCOptions(void)
3824
{
3825
	int			i;
3826

3827
	/*
B
Bruce Momjian 已提交
3828 3829
	 * Before log_line_prefix could possibly receive a nonempty setting, make
	 * sure that timezone processing is minimally alive (see elog.c).
3830 3831 3832
	 */
	pg_timezone_pre_initialize();

3833 3834 3835 3836
	/*
	 * Build sorted array of all GUC variables.
	 */
	build_guc_variables();
3837

3838
	/*
3839 3840
	 * Load all variables with their compiled-in defaults, and initialize
	 * status fields as needed.
3841
	 */
3842
	for (i = 0; i < num_guc_variables; i++)
3843
	{
3844
		InitializeOneGUCOption(guc_variables[i]);
3845 3846 3847 3848
	}

	guc_dirty = false;

3849 3850
	reporting_enabled = false;

3851
	/*
3852
	 * Prevent any attempt to override the transaction modes from
3853 3854
	 * non-interactive sources.
	 */
3855 3856 3857
	SetConfigOption("transaction_isolation", "default",
					PGC_POSTMASTER, PGC_S_OVERRIDE);
	SetConfigOption("transaction_read_only", "no",
3858
					PGC_POSTMASTER, PGC_S_OVERRIDE);
3859 3860
	SetConfigOption("transaction_deferrable", "no",
					PGC_POSTMASTER, PGC_S_OVERRIDE);
3861 3862

	/*
B
Bruce Momjian 已提交
3863
	 * For historical reasons, some GUC parameters can receive defaults from
3864
	 * environment variables.  Process those settings.
3865
	 */
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882
	InitializeGUCOptionsFromEnvironment();
}

/*
 * Assign any GUC values that can come from the server's environment.
 *
 * This is called from InitializeGUCOptions, and also from ProcessConfigFile
 * to deal with the possibility that a setting has been removed from
 * postgresql.conf and should now get a value from the environment.
 * (The latter is a kludge that should probably go away someday; if so,
 * fold this back into InitializeGUCOptions.)
 */
static void
InitializeGUCOptionsFromEnvironment(void)
{
	char	   *env;
	long		stack_rlimit;
3883 3884 3885 3886 3887

	env = getenv("PGPORT");
	if (env != NULL)
		SetConfigOption("port", env, PGC_POSTMASTER, PGC_S_ENV_VAR);

3888 3889
	env = getenv("PGDATESTYLE");
	if (env != NULL)
3890 3891
		SetConfigOption("datestyle", env, PGC_POSTMASTER, PGC_S_ENV_VAR);

3892 3893
	env = getenv("PGCLIENTENCODING");
	if (env != NULL)
3894
		SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
3895 3896 3897 3898 3899 3900 3901 3902 3903

	/*
	 * rlimit isn't exactly an "environment variable", but it behaves about
	 * the same.  If we can identify the platform stack depth rlimit, increase
	 * default stack depth setting up to whatever is safe (but at most 2MB).
	 */
	stack_rlimit = get_stack_depth_rlimit();
	if (stack_rlimit > 0)
	{
3904
		long		new_limit = (stack_rlimit - STACK_DEPTH_SLOP) / 1024L;
3905 3906 3907

		if (new_limit > 100)
		{
B
Bruce Momjian 已提交
3908
			char		limbuf[16];
3909 3910

			new_limit = Min(new_limit, 2048);
3911
			sprintf(limbuf, "%ld", new_limit);
3912 3913 3914 3915
			SetConfigOption("max_stack_depth", limbuf,
							PGC_POSTMASTER, PGC_S_ENV_VAR);
		}
	}
3916 3917
}

3918 3919
/*
 * Initialize one GUC option variable to its compiled-in default.
3920 3921 3922
 *
 * Note: the reason for calling check_hooks is not that we think the boot_val
 * might fail, but that the hooks might wish to compute an "extra" struct.
3923 3924
 */
static void
3925
InitializeOneGUCOption(struct config_generic * gconf)
3926 3927 3928 3929 3930
{
	gconf->status = 0;
	gconf->reset_source = PGC_S_DEFAULT;
	gconf->source = PGC_S_DEFAULT;
	gconf->stack = NULL;
3931
	gconf->extra = NULL;
3932 3933 3934 3935 3936 3937 3938
	gconf->sourcefile = NULL;
	gconf->sourceline = 0;

	switch (gconf->vartype)
	{
		case PGC_BOOL:
			{
3939
				struct config_bool *conf = (struct config_bool *) gconf;
3940 3941
				bool		newval = conf->boot_val;
				void	   *extra = NULL;
3942

3943 3944 3945 3946
				if (!call_bool_check_hook(conf, &newval, &extra,
										  PGC_S_DEFAULT, LOG))
					elog(FATAL, "failed to initialize %s to %d",
						 conf->gen.name, (int) newval);
3947
				if (conf->assign_hook)
3948 3949 3950
					(*conf->assign_hook) (newval, extra);
				*conf->variable = conf->reset_val = newval;
				conf->gen.extra = conf->reset_extra = extra;
3951 3952
				break;
			}
3953 3954 3955
		case PGC_INT:
			{
				struct config_int *conf = (struct config_int *) gconf;
3956 3957 3958 3959 3960 3961 3962 3963 3964
				int			newval = conf->boot_val;
				void	   *extra = NULL;

				Assert(newval >= conf->min);
				Assert(newval <= conf->max);
				if (!call_int_check_hook(conf, &newval, &extra,
										 PGC_S_DEFAULT, LOG))
					elog(FATAL, "failed to initialize %s to %d",
						 conf->gen.name, newval);
3965
				if (conf->assign_hook)
3966 3967 3968
					(*conf->assign_hook) (newval, extra);
				*conf->variable = conf->reset_val = newval;
				conf->gen.extra = conf->reset_extra = extra;
3969 3970 3971 3972 3973
				break;
			}
		case PGC_REAL:
			{
				struct config_real *conf = (struct config_real *) gconf;
3974 3975 3976 3977 3978 3979 3980 3981 3982
				double		newval = conf->boot_val;
				void	   *extra = NULL;

				Assert(newval >= conf->min);
				Assert(newval <= conf->max);
				if (!call_real_check_hook(conf, &newval, &extra,
										  PGC_S_DEFAULT, LOG))
					elog(FATAL, "failed to initialize %s to %g",
						 conf->gen.name, newval);
3983
				if (conf->assign_hook)
3984 3985 3986
					(*conf->assign_hook) (newval, extra);
				*conf->variable = conf->reset_val = newval;
				conf->gen.extra = conf->reset_extra = extra;
3987 3988 3989
				break;
			}
		case PGC_STRING:
3990
			{
3991
				struct config_string *conf = (struct config_string *) gconf;
3992 3993
				char	   *newval;
				void	   *extra = NULL;
3994

3995 3996 3997 3998 3999
				/* non-NULL boot_val must always get strdup'd */
				if (conf->boot_val != NULL)
					newval = guc_strdup(FATAL, conf->boot_val);
				else
					newval = NULL;
4000

4001 4002 4003 4004
				if (!call_string_check_hook(conf, &newval, &extra,
											PGC_S_DEFAULT, LOG))
					elog(FATAL, "failed to initialize %s to \"%s\"",
						 conf->gen.name, newval ? newval : "");
4005
				if (conf->assign_hook)
4006 4007 4008
					(*conf->assign_hook) (newval, extra);
				*conf->variable = conf->reset_val = newval;
				conf->gen.extra = conf->reset_extra = extra;
4009
				break;
4010 4011
			}
		case PGC_ENUM:
4012 4013
			{
				struct config_enum *conf = (struct config_enum *) gconf;
4014 4015
				int			newval = conf->boot_val;
				void	   *extra = NULL;
4016

4017 4018 4019 4020
				if (!call_enum_check_hook(conf, &newval, &extra,
										  PGC_S_DEFAULT, LOG))
					elog(FATAL, "failed to initialize %s to %d",
						 conf->gen.name, newval);
4021
				if (conf->assign_hook)
4022 4023 4024
					(*conf->assign_hook) (newval, extra);
				*conf->variable = conf->reset_val = newval;
				conf->gen.extra = conf->reset_extra = extra;
4025 4026
				break;
			}
4027 4028 4029
	}
}

4030

4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044
/*
 * Select the configuration files and data directory to be used, and
 * do the initial read of postgresql.conf.
 *
 * This is called after processing command-line switches.
 *		userDoption is the -D switch value if any (NULL if unspecified).
 *		progname is just for use in error messages.
 *
 * Returns true on success; on failure, prints a suitable error message
 * to stderr and returns false.
 */
bool
SelectConfigFiles(const char *userDoption, const char *progname)
{
4045 4046
	char	   *configdir;
	char	   *fname;
4047 4048
	struct stat stat_buf;

4049 4050 4051 4052 4053
	/* configdir is -D option, or $PGDATA if no -D */
	if (userDoption)
		configdir = make_absolute_path(userDoption);
	else
		configdir = make_absolute_path(getenv("PGDATA"));
4054 4055

	/*
4056
	 * Find the configuration file: if config_file was specified on the
B
Bruce Momjian 已提交
4057 4058 4059
	 * command line, use it, else use configdir/postgresql.conf.  In any case
	 * ensure the result is an absolute path, so that it will be interpreted
	 * the same way by future backends.
4060
	 */
4061 4062
	if (ConfigFileName)
		fname = make_absolute_path(ConfigFileName);
4063 4064 4065 4066 4067 4068 4069
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
	}
	else
4070
	{
4071
		write_stderr("%s does not know where to find the server configuration file.\n"
P
Peter Eisentraut 已提交
4072
					 "You must specify the --config-file or -D invocation "
4073 4074 4075
					 "option or set the PGDATA environment variable.\n",
					 progname);
		return false;
4076 4077
	}

4078
	/*
B
Bruce Momjian 已提交
4079 4080
	 * Set the ConfigFileName GUC variable to its final value, ensuring that
	 * it can't be overridden later.
4081 4082 4083 4084 4085 4086 4087
	 */
	SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);

	/*
	 * Now read the config file for the first time.
	 */
4088 4089
	if (stat(ConfigFileName, &stat_buf) != 0)
	{
4090
		write_stderr("%s cannot access the server configuration file \"%s\": %s\n",
4091 4092 4093 4094 4095 4096 4097
					 progname, ConfigFileName, strerror(errno));
		return false;
	}

	ProcessConfigFile(PGC_POSTMASTER);

	/*
4098 4099
	 * If the data_directory GUC variable has been set, use that as DataDir;
	 * otherwise use configdir if set; else punt.
4100
	 *
4101 4102
	 * Note: SetDataDir will copy and absolute-ize its argument, so we don't
	 * have to.
4103
	 */
4104 4105 4106 4107
	if (data_directory)
		SetDataDir(data_directory);
	else if (configdir)
		SetDataDir(configdir);
4108 4109 4110
	else
	{
		write_stderr("%s does not know where to find the database system data.\n"
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121
					 "This can be specified as \"data_directory\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}

	/*
	 * Reflect the final DataDir value back into the data_directory GUC var.
	 * (If you are wondering why we don't just make them a single variable,
	 * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
B
Bruce Momjian 已提交
4122 4123 4124
	 * child backends specially.  XXX is that still true?  Given that we now
	 * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
	 * DataDir in advance.)
4125 4126 4127 4128 4129 4130 4131 4132
	 */
	SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);

	/*
	 * Figure out where pg_hba.conf is, and make sure the path is absolute.
	 */
	if (HbaFileName)
		fname = make_absolute_path(HbaFileName);
4133 4134 4135 4136 4137 4138 4139
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(HBA_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
	}
	else
4140 4141 4142 4143 4144
	{
		write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
					 "This can be specified as \"hba_file\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
4145 4146 4147
					 progname, ConfigFileName);
		return false;
	}
4148 4149
	SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);
4150 4151

	/*
4152
	 * Likewise for pg_ident.conf.
4153
	 */
4154 4155
	if (IdentFileName)
		fname = make_absolute_path(IdentFileName);
4156 4157 4158 4159 4160 4161 4162
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(IDENT_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
	}
	else
4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174
	{
		write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
					 "This can be specified as \"ident_file\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}
	SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);

	free(configdir);
4175 4176 4177 4178 4179

	return true;
}


4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
/*
 * Reset all options to their saved default values (implements RESET ALL)
 */
void
ResetAllOptions(void)
{
	int			i;

	for (i = 0; i < num_guc_variables; i++)
	{
		struct config_generic *gconf = guc_variables[i];

		/* Don't reset non-SET-able values */
4193 4194
		if (gconf->context != PGC_SUSET &&
			gconf->context != PGC_USERSET)
4195 4196 4197 4198 4199 4200 4201 4202
			continue;
		/* Don't reset if special exclusion from RESET ALL */
		if (gconf->flags & GUC_NO_RESET_ALL)
			continue;
		/* No need to reset if wasn't SET */
		if (gconf->source <= PGC_S_OVERRIDE)
			continue;

4203
		/* Save old value to support transaction abort */
4204
		push_old_value(gconf, GUC_ACTION_SET);
4205

4206 4207 4208
		switch (gconf->vartype)
		{
			case PGC_BOOL:
B
Bruce Momjian 已提交
4209 4210
				{
					struct config_bool *conf = (struct config_bool *) gconf;
4211

B
Bruce Momjian 已提交
4212
					if (conf->assign_hook)
4213 4214
						(*conf->assign_hook) (conf->reset_val,
											  conf->reset_extra);
B
Bruce Momjian 已提交
4215
					*conf->variable = conf->reset_val;
4216 4217
					set_extra_field(&conf->gen, &conf->gen.extra,
									conf->reset_extra);
B
Bruce Momjian 已提交
4218 4219
					break;
				}
4220
			case PGC_INT:
B
Bruce Momjian 已提交
4221 4222
				{
					struct config_int *conf = (struct config_int *) gconf;
4223

B
Bruce Momjian 已提交
4224
					if (conf->assign_hook)
4225 4226
						(*conf->assign_hook) (conf->reset_val,
											  conf->reset_extra);
B
Bruce Momjian 已提交
4227
					*conf->variable = conf->reset_val;
4228 4229
					set_extra_field(&conf->gen, &conf->gen.extra,
									conf->reset_extra);
B
Bruce Momjian 已提交
4230 4231
					break;
				}
4232 4233
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
4234 4235 4236
					struct config_real *conf = (struct config_real *) gconf;

					if (conf->assign_hook)
4237 4238
						(*conf->assign_hook) (conf->reset_val,
											  conf->reset_extra);
B
Bruce Momjian 已提交
4239
					*conf->variable = conf->reset_val;
4240 4241
					set_extra_field(&conf->gen, &conf->gen.extra,
									conf->reset_extra);
4242 4243
					break;
				}
B
Bruce Momjian 已提交
4244 4245 4246
			case PGC_STRING:
				{
					struct config_string *conf = (struct config_string *) gconf;
4247

4248 4249 4250 4251 4252 4253
					if (conf->assign_hook)
						(*conf->assign_hook) (conf->reset_val,
											  conf->reset_extra);
					set_string_field(conf, conf->variable, conf->reset_val);
					set_extra_field(&conf->gen, &conf->gen.extra,
									conf->reset_extra);
B
Bruce Momjian 已提交
4254 4255
					break;
				}
4256 4257 4258 4259 4260
			case PGC_ENUM:
				{
					struct config_enum *conf = (struct config_enum *) gconf;

					if (conf->assign_hook)
4261 4262
						(*conf->assign_hook) (conf->reset_val,
											  conf->reset_extra);
4263
					*conf->variable = conf->reset_val;
4264 4265
					set_extra_field(&conf->gen, &conf->gen.extra,
									conf->reset_extra);
4266 4267
					break;
				}
4268
		}
4269

4270 4271
		gconf->source = gconf->reset_source;

4272 4273
		if (gconf->flags & GUC_REPORT)
			ReportGUCOption(gconf);
4274 4275 4276 4277 4278
	}
}


/*
4279
 * push_old_value
4280
 *		Push previous state during transactional assignment to a GUC variable.
4281 4282
 */
static void
4283
push_old_value(struct config_generic * gconf, GucAction action)
4284 4285 4286
{
	GucStack   *stack;

4287
	/* If we're not inside a nest level, do nothing */
4288
	if (GUCNestLevel == 0)
4289 4290
		return;

4291 4292 4293
	/* Do we already have a stack entry of the current nest level? */
	stack = gconf->stack;
	if (stack && stack->nest_level >= GUCNestLevel)
4294
	{
4295 4296 4297
		/* Yes, so adjust its state if necessary */
		Assert(stack->nest_level == GUCNestLevel);
		switch (action)
4298
		{
4299 4300 4301 4302 4303 4304 4305 4306
			case GUC_ACTION_SET:
				/* SET overrides any prior action at same nest level */
				if (stack->state == GUC_SET_LOCAL)
				{
					/* must discard old masked value */
					discard_stack_value(gconf, &stack->masked);
				}
				stack->state = GUC_SET;
4307
				break;
4308 4309 4310 4311 4312 4313 4314 4315
			case GUC_ACTION_LOCAL:
				if (stack->state == GUC_SET)
				{
					/* SET followed by SET LOCAL, remember SET's value */
					set_stack_value(gconf, &stack->masked);
					stack->state = GUC_SET_LOCAL;
				}
				/* in all other cases, no change to stack entry */
4316
				break;
4317 4318 4319
			case GUC_ACTION_SAVE:
				/* Could only have a prior SAVE of same variable */
				Assert(stack->state == GUC_SAVE);
4320 4321
				break;
		}
4322 4323 4324
		Assert(guc_dirty);		/* must be set already */
		return;
	}
4325

4326 4327 4328 4329 4330 4331 4332
	/*
	 * Push a new stack entry
	 *
	 * We keep all the stack entries in TopTransactionContext for simplicity.
	 */
	stack = (GucStack *) MemoryContextAllocZero(TopTransactionContext,
												sizeof(GucStack));
4333

4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346
	stack->prev = gconf->stack;
	stack->nest_level = GUCNestLevel;
	switch (action)
	{
		case GUC_ACTION_SET:
			stack->state = GUC_SET;
			break;
		case GUC_ACTION_LOCAL:
			stack->state = GUC_LOCAL;
			break;
		case GUC_ACTION_SAVE:
			stack->state = GUC_SAVE;
			break;
4347
	}
4348 4349 4350 4351 4352 4353 4354
	stack->source = gconf->source;
	set_stack_value(gconf, &stack->prior);

	gconf->stack = stack;

	/* Ensure we remember to pop at end of xact */
	guc_dirty = true;
4355 4356
}

4357

4358
/*
4359
 * Do GUC processing at main transaction start.
4360 4361
 */
void
4362 4363 4364
AtStart_GUC(void)
{
	/*
B
Bruce Momjian 已提交
4365 4366 4367
	 * The nest level should be 0 between transactions; if it isn't, somebody
	 * didn't call AtEOXact_GUC, or called it with the wrong nestLevel.  We
	 * throw a warning but make no other effort to clean up.
4368 4369 4370 4371 4372 4373 4374 4375 4376
	 */
	if (GUCNestLevel != 0)
		elog(WARNING, "GUC nest level = %d at transaction start",
			 GUCNestLevel);
	GUCNestLevel = 1;
}

/*
 * Enter a new nesting level for GUC values.  This is called at subtransaction
B
Bruce Momjian 已提交
4377
 * start and when entering a function that has proconfig settings.	NOTE that
4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394
 * we must not risk error here, else subtransaction start will be unhappy.
 */
int
NewGUCNestLevel(void)
{
	return ++GUCNestLevel;
}

/*
 * Do GUC processing at transaction or subtransaction commit or abort, or
 * when exiting a function that has proconfig settings.  (The name is thus
 * a bit of a misnomer; perhaps it should be ExitGUCNestLevel or some such.)
 * During abort, we discard all GUC settings that were applied at nesting
 * levels >= nestLevel.  nestLevel == 1 corresponds to the main transaction.
 */
void
AtEOXact_GUC(bool isCommit, int nestLevel)
4395
{
4396
	bool		still_dirty;
4397 4398
	int			i;

4399 4400 4401 4402 4403 4404 4405 4406
	/*
	 * Note: it's possible to get here with GUCNestLevel == nestLevel-1 during
	 * abort, if there is a failure during transaction start before
	 * AtStart_GUC is called.
	 */
	Assert(nestLevel > 0 &&
		   (nestLevel <= GUCNestLevel ||
			(nestLevel == GUCNestLevel + 1 && !isCommit)));
4407

4408 4409
	/* Quick exit if nothing's changed in this transaction */
	if (!guc_dirty)
4410 4411
	{
		GUCNestLevel = nestLevel - 1;
4412
		return;
4413
	}
4414

4415
	still_dirty = false;
4416 4417 4418
	for (i = 0; i < num_guc_variables; i++)
	{
		struct config_generic *gconf = guc_variables[i];
4419
		GucStack   *stack;
4420

4421
		/*
B
Bruce Momjian 已提交
4422 4423 4424
		 * Process and pop each stack entry within the nest level.	To
		 * simplify fmgr_security_definer(), we allow failure exit from a
		 * function-with-SET-options to be recovered at the surrounding
4425 4426
		 * transaction or subtransaction abort; so there could be more than
		 * one stack entry to pop.
4427
		 */
4428 4429
		while ((stack = gconf->stack) != NULL &&
			   stack->nest_level >= nestLevel)
4430
		{
4431 4432 4433 4434
			GucStack   *prev = stack->prev;
			bool		restorePrior = false;
			bool		restoreMasked = false;
			bool		changed;
4435

4436 4437 4438 4439 4440 4441
			/*
			 * In this next bit, if we don't set either restorePrior or
			 * restoreMasked, we must "discard" any unwanted fields of the
			 * stack entries to avoid leaking memory.  If we do set one of
			 * those flags, unused fields will be cleaned up after restoring.
			 */
B
Bruce Momjian 已提交
4442
			if (!isCommit)		/* if abort, always restore prior value */
4443 4444 4445 4446 4447 4448 4449 4450 4451
				restorePrior = true;
			else if (stack->state == GUC_SAVE)
				restorePrior = true;
			else if (stack->nest_level == 1)
			{
				/* transaction commit */
				if (stack->state == GUC_SET_LOCAL)
					restoreMasked = true;
				else if (stack->state == GUC_SET)
4452
				{
4453 4454 4455
					/* we keep the current active value */
					discard_stack_value(gconf, &stack->prior);
				}
B
Bruce Momjian 已提交
4456
				else	/* must be GUC_LOCAL */
4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468
					restorePrior = true;
			}
			else if (prev == NULL ||
					 prev->nest_level < stack->nest_level - 1)
			{
				/* decrement entry's level and do not pop it */
				stack->nest_level--;
				continue;
			}
			else
			{
				/*
B
Bruce Momjian 已提交
4469 4470
				 * We have to merge this stack entry into prev. See README for
				 * discussion of this bit.
4471 4472 4473 4474
				 */
				switch (stack->state)
				{
					case GUC_SAVE:
B
Bruce Momjian 已提交
4475
						Assert(false);	/* can't get here */
4476 4477 4478 4479 4480 4481 4482 4483

					case GUC_SET:
						/* next level always becomes SET */
						discard_stack_value(gconf, &stack->prior);
						if (prev->state == GUC_SET_LOCAL)
							discard_stack_value(gconf, &prev->masked);
						prev->state = GUC_SET;
						break;
4484

4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
					case GUC_LOCAL:
						if (prev->state == GUC_SET)
						{
							/* LOCAL migrates down */
							prev->masked = stack->prior;
							prev->state = GUC_SET_LOCAL;
						}
						else
						{
							/* else just forget this stack level */
							discard_stack_value(gconf, &stack->prior);
						}
						break;
B
Bruce Momjian 已提交
4498

4499 4500 4501 4502 4503 4504 4505 4506 4507
					case GUC_SET_LOCAL:
						/* prior state at this level no longer wanted */
						discard_stack_value(gconf, &stack->prior);
						/* copy down the masked state */
						if (prev->state == GUC_SET_LOCAL)
							discard_stack_value(gconf, &prev->masked);
						prev->masked = stack->masked;
						prev->state = GUC_SET_LOCAL;
						break;
4508
				}
4509
			}
4510

4511
			changed = false;
B
Bruce Momjian 已提交
4512

4513 4514 4515
			if (restorePrior || restoreMasked)
			{
				/* Perform appropriate restoration of the stacked value */
4516
				config_var_value newvalue;
4517 4518 4519 4520 4521 4522
				GucSource	newsource;

				if (restoreMasked)
				{
					newvalue = stack->masked;
					newsource = PGC_S_SESSION;
4523
				}
4524
				else
4525
				{
4526 4527 4528
					newvalue = stack->prior;
					newsource = stack->source;
				}
4529

4530 4531 4532 4533
				switch (gconf->vartype)
				{
					case PGC_BOOL:
						{
B
Bruce Momjian 已提交
4534
							struct config_bool *conf = (struct config_bool *) gconf;
4535 4536
							bool		newval = newvalue.val.boolval;
							void	   *newextra = newvalue.extra;
B
Bruce Momjian 已提交
4537

4538 4539
							if (*conf->variable != newval ||
								conf->gen.extra != newextra)
B
Bruce Momjian 已提交
4540 4541
							{
								if (conf->assign_hook)
4542
									(*conf->assign_hook) (newval, newextra);
B
Bruce Momjian 已提交
4543
								*conf->variable = newval;
4544 4545
								set_extra_field(&conf->gen, &conf->gen.extra,
												newextra);
B
Bruce Momjian 已提交
4546 4547 4548
								changed = true;
							}
							break;
4549 4550 4551
						}
					case PGC_INT:
						{
B
Bruce Momjian 已提交
4552
							struct config_int *conf = (struct config_int *) gconf;
4553 4554
							int			newval = newvalue.val.intval;
							void	   *newextra = newvalue.extra;
B
Bruce Momjian 已提交
4555

4556 4557
							if (*conf->variable != newval ||
								conf->gen.extra != newextra)
B
Bruce Momjian 已提交
4558 4559
							{
								if (conf->assign_hook)
4560
									(*conf->assign_hook) (newval, newextra);
B
Bruce Momjian 已提交
4561
								*conf->variable = newval;
4562 4563
								set_extra_field(&conf->gen, &conf->gen.extra,
												newextra);
B
Bruce Momjian 已提交
4564 4565 4566
								changed = true;
							}
							break;
4567 4568 4569
						}
					case PGC_REAL:
						{
B
Bruce Momjian 已提交
4570
							struct config_real *conf = (struct config_real *) gconf;
4571 4572
							double		newval = newvalue.val.realval;
							void	   *newextra = newvalue.extra;
B
Bruce Momjian 已提交
4573

4574 4575
							if (*conf->variable != newval ||
								conf->gen.extra != newextra)
B
Bruce Momjian 已提交
4576 4577
							{
								if (conf->assign_hook)
4578
									(*conf->assign_hook) (newval, newextra);
B
Bruce Momjian 已提交
4579
								*conf->variable = newval;
4580 4581
								set_extra_field(&conf->gen, &conf->gen.extra,
												newextra);
B
Bruce Momjian 已提交
4582 4583 4584
								changed = true;
							}
							break;
4585 4586
						}
					case PGC_STRING:
4587
						{
B
Bruce Momjian 已提交
4588
							struct config_string *conf = (struct config_string *) gconf;
4589 4590
							char	   *newval = newvalue.val.stringval;
							void	   *newextra = newvalue.extra;
B
Bruce Momjian 已提交
4591

4592 4593
							if (*conf->variable != newval ||
								conf->gen.extra != newextra)
B
Bruce Momjian 已提交
4594
							{
4595 4596
								if (conf->assign_hook)
									(*conf->assign_hook) (newval, newextra);
B
Bruce Momjian 已提交
4597
								set_string_field(conf, conf->variable, newval);
4598 4599
								set_extra_field(&conf->gen, &conf->gen.extra,
												newextra);
B
Bruce Momjian 已提交
4600
								changed = true;
B
Bruce Momjian 已提交
4601
							}
4602

B
Bruce Momjian 已提交
4603 4604 4605 4606 4607 4608
							/*
							 * Release stacked values if not used anymore. We
							 * could use discard_stack_value() here, but since
							 * we have type-specific code anyway, might as
							 * well inline it.
							 */
4609 4610
							set_string_field(conf, &stack->prior.val.stringval, NULL);
							set_string_field(conf, &stack->masked.val.stringval, NULL);
B
Bruce Momjian 已提交
4611
							break;
4612
						}
4613 4614 4615
					case PGC_ENUM:
						{
							struct config_enum *conf = (struct config_enum *) gconf;
4616 4617
							int			newval = newvalue.val.enumval;
							void	   *newextra = newvalue.extra;
4618

4619 4620
							if (*conf->variable != newval ||
								conf->gen.extra != newextra)
4621 4622
							{
								if (conf->assign_hook)
4623
									(*conf->assign_hook) (newval, newextra);
4624
								*conf->variable = newval;
4625 4626
								set_extra_field(&conf->gen, &conf->gen.extra,
												newextra);
4627 4628 4629 4630
								changed = true;
							}
							break;
						}
4631
				}
4632

4633 4634 4635 4636 4637 4638
				/*
				 * Release stacked extra values if not used anymore.
				 */
				set_extra_field(gconf, &(stack->prior.extra), NULL);
				set_extra_field(gconf, &(stack->masked.extra), NULL);

4639 4640
				gconf->source = newsource;
			}
4641

4642 4643 4644
			/* Finish popping the state stack */
			gconf->stack = prev;
			pfree(stack);
4645

4646 4647 4648
			/* Report new value if we changed it */
			if (changed && (gconf->flags & GUC_REPORT))
				ReportGUCOption(gconf);
B
Bruce Momjian 已提交
4649
		}						/* end of stack-popping loop */
4650 4651 4652

		if (stack != NULL)
			still_dirty = true;
4653 4654
	}

4655 4656
	/* If there are no remaining stack entries, we can reset guc_dirty */
	guc_dirty = still_dirty;
4657 4658 4659

	/* Update nesting level */
	GUCNestLevel = nestLevel - 1;
4660 4661 4662
}


4663 4664 4665 4666 4667 4668 4669 4670 4671 4672
/*
 * Start up automatic reporting of changes to variables marked GUC_REPORT.
 * This is executed at completion of backend startup.
 */
void
BeginReportingGUCOptions(void)
{
	int			i;

	/*
B
Bruce Momjian 已提交
4673 4674
	 * Don't do anything unless talking to an interactive frontend of protocol
	 * 3.0 or later.
4675
	 */
4676
	if (whereToSendOutput != DestRemote ||
4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695
		PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
		return;

	reporting_enabled = true;

	/* Transmit initial values of interesting variables */
	for (i = 0; i < num_guc_variables; i++)
	{
		struct config_generic *conf = guc_variables[i];

		if (conf->flags & GUC_REPORT)
			ReportGUCOption(conf);
	}
}

/*
 * ReportGUCOption: if appropriate, transmit option value to frontend
 */
static void
4696
ReportGUCOption(struct config_generic * record)
4697 4698 4699
{
	if (reporting_enabled && (record->flags & GUC_REPORT))
	{
4700
		char	   *val = _ShowOption(record, false);
4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711
		StringInfoData msgbuf;

		pq_beginmessage(&msgbuf, 'S');
		pq_sendstring(&msgbuf, record->name);
		pq_sendstring(&msgbuf, val);
		pq_endmessage(&msgbuf);

		pfree(val);
	}
}

4712 4713
/*
 * Try to parse value as an integer.  The accepted formats are the
4714 4715 4716 4717 4718 4719 4720
 * usual decimal, octal, or hexadecimal formats, optionally followed by
 * a unit name if "flags" indicates a unit is allowed.
 *
 * If the string parses okay, return true, else false.
 * If okay and result is not NULL, return the value in *result.
 * If not okay and hintmsg is not NULL, *hintmsg is set to a suitable
 *	HINT message, or NULL if no hint provided.
4721
 */
4722
bool
4723
parse_int(const char *value, int *result, int flags, const char **hintmsg)
4724
{
4725
	int64		val;
4726 4727
	char	   *endptr;

4728 4729 4730 4731 4732 4733 4734
	/* To suppress compiler warnings, always set output params */
	if (result)
		*result = 0;
	if (hintmsg)
		*hintmsg = NULL;

	/* We assume here that int64 is at least as wide as long */
4735 4736
	errno = 0;
	val = strtol(value, &endptr, 0);
4737

4738 4739 4740 4741
	if (endptr == value)
		return false;			/* no HINT for integer syntax error */

	if (errno == ERANGE || val != (int64) ((int32) val))
4742
	{
4743 4744 4745 4746
		if (hintmsg)
			*hintmsg = gettext_noop("Value exceeds integer range.");
		return false;
	}
4747

4748 4749 4750
	/* allow whitespace between integer and unit */
	while (isspace((unsigned char) *endptr))
		endptr++;
4751

4752 4753 4754 4755 4756 4757 4758 4759
	/* Handle possible unit */
	if (*endptr != '\0')
	{
		/*
		 * Note: the multiple-switch coding technique here is a bit tedious,
		 * but seems necessary to avoid intermediate-value overflows.
		 */
		if (flags & GUC_UNIT_MEMORY)
4760
		{
4761 4762 4763
			/* Set hint for use if no match or trailing garbage */
			if (hintmsg)
				*hintmsg = gettext_noop("Valid units for this parameter are \"kB\", \"MB\", and \"GB\".");
4764

4765 4766 4767 4768 4769
#if BLCKSZ < 1024 || BLCKSZ > (1024*1024)
#error BLCKSZ must be between 1KB and 1MB
#endif
#if XLOG_BLCKSZ < 1024 || XLOG_BLCKSZ > (1024*1024)
#error XLOG_BLCKSZ must be between 1KB and 1MB
4770 4771
#endif

4772
			if (strncmp(endptr, "kB", 2) == 0)
4773
			{
4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815
				endptr += 2;
				switch (flags & GUC_UNIT_MEMORY)
				{
					case GUC_UNIT_BLOCKS:
						val /= (BLCKSZ / 1024);
						break;
					case GUC_UNIT_XBLOCKS:
						val /= (XLOG_BLCKSZ / 1024);
						break;
				}
			}
			else if (strncmp(endptr, "MB", 2) == 0)
			{
				endptr += 2;
				switch (flags & GUC_UNIT_MEMORY)
				{
					case GUC_UNIT_KB:
						val *= KB_PER_MB;
						break;
					case GUC_UNIT_BLOCKS:
						val *= KB_PER_MB / (BLCKSZ / 1024);
						break;
					case GUC_UNIT_XBLOCKS:
						val *= KB_PER_MB / (XLOG_BLCKSZ / 1024);
						break;
				}
			}
			else if (strncmp(endptr, "GB", 2) == 0)
			{
				endptr += 2;
				switch (flags & GUC_UNIT_MEMORY)
				{
					case GUC_UNIT_KB:
						val *= KB_PER_GB;
						break;
					case GUC_UNIT_BLOCKS:
						val *= KB_PER_GB / (BLCKSZ / 1024);
						break;
					case GUC_UNIT_XBLOCKS:
						val *= KB_PER_GB / (XLOG_BLCKSZ / 1024);
						break;
				}
4816 4817
			}
		}
4818 4819 4820 4821 4822
		else if (flags & GUC_UNIT_TIME)
		{
			/* Set hint for use if no match or trailing garbage */
			if (hintmsg)
				*hintmsg = gettext_noop("Valid units for this parameter are \"ms\", \"s\", \"min\", \"h\", and \"d\".");
4823

4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
			if (strncmp(endptr, "ms", 2) == 0)
			{
				endptr += 2;
				switch (flags & GUC_UNIT_TIME)
				{
					case GUC_UNIT_S:
						val /= MS_PER_S;
						break;
					case GUC_UNIT_MIN:
						val /= MS_PER_MIN;
						break;
				}
			}
			else if (strncmp(endptr, "s", 1) == 0)
			{
				endptr += 1;
				switch (flags & GUC_UNIT_TIME)
				{
					case GUC_UNIT_MS:
						val *= MS_PER_S;
						break;
					case GUC_UNIT_MIN:
						val /= S_PER_MIN;
						break;
				}
			}
			else if (strncmp(endptr, "min", 3) == 0)
			{
				endptr += 3;
				switch (flags & GUC_UNIT_TIME)
				{
					case GUC_UNIT_MS:
						val *= MS_PER_MIN;
						break;
					case GUC_UNIT_S:
						val *= S_PER_MIN;
						break;
				}
			}
			else if (strncmp(endptr, "h", 1) == 0)
			{
				endptr += 1;
				switch (flags & GUC_UNIT_TIME)
				{
					case GUC_UNIT_MS:
						val *= MS_PER_H;
						break;
					case GUC_UNIT_S:
						val *= S_PER_H;
						break;
					case GUC_UNIT_MIN:
						val *= MIN_PER_H;
						break;
				}
			}
			else if (strncmp(endptr, "d", 1) == 0)
			{
				endptr += 1;
				switch (flags & GUC_UNIT_TIME)
				{
					case GUC_UNIT_MS:
						val *= MS_PER_D;
						break;
					case GUC_UNIT_S:
						val *= S_PER_D;
						break;
					case GUC_UNIT_MIN:
						val *= MIN_PER_D;
						break;
				}
			}
		}
4896

4897 4898
		/* allow whitespace after unit */
		while (isspace((unsigned char) *endptr))
4899 4900
			endptr++;

4901 4902
		if (*endptr != '\0')
			return false;		/* appropriate hint, if any, already set */
4903

4904 4905
		/* Check for overflow due to units conversion */
		if (val != (int64) ((int32) val))
4906
		{
4907 4908 4909
			if (hintmsg)
				*hintmsg = gettext_noop("Value exceeds integer range.");
			return false;
4910
		}
4911 4912
	}

4913 4914 4915 4916 4917 4918 4919 4920
	if (result)
		*result = (int) val;
	return true;
}



/*
4921 4922 4923
 * Try to parse value as a floating point number in the usual format.
 * If the string parses okay, return true, else false.
 * If okay and result is not NULL, return the value in *result.
4924
 */
4925
bool
4926 4927 4928 4929 4930
parse_real(const char *value, double *result)
{
	double		val;
	char	   *endptr;

4931 4932 4933
	if (result)
		*result = 0;			/* suppress compiler warning */

4934 4935
	errno = 0;
	val = strtod(value, &endptr);
4936
	if (endptr == value || errno == ERANGE)
4937
		return false;
4938 4939 4940 4941 4942 4943 4944

	/* allow whitespace after number */
	while (isspace((unsigned char) *endptr))
		endptr++;
	if (*endptr != '\0')
		return false;

4945 4946 4947 4948 4949 4950
	if (result)
		*result = val;
	return true;
}


4951 4952 4953 4954 4955 4956 4957 4958
/*
 * Lookup the name for an enum option with the selected value.
 * Should only ever be called with known-valid values, so throws
 * an elog(ERROR) if the enum option is not found.
 *
 * The returned string is a pointer to static data and not
 * allocated for modification.
 */
4959
const char *
4960
config_enum_lookup_by_value(struct config_enum * record, int val)
4961
{
A
Alvaro Herrera 已提交
4962 4963 4964
	const struct config_enum_entry *entry;

	for (entry = record->options; entry && entry->name; entry++)
4965 4966 4967 4968
	{
		if (entry->val == val)
			return entry->name;
	}
A
Alvaro Herrera 已提交
4969

4970 4971
	elog(ERROR, "could not find enum option %d for %s",
		 val, record->gen.name);
4972
	return NULL;				/* silence compiler */
4973 4974 4975 4976 4977 4978 4979
}


/*
 * Lookup the value for an enum option with the selected name
 * (case-insensitive).
 * If the enum option is found, sets the retval value and returns
4980
 * true. If it's not found, return FALSE and retval is set to 0.
4981
 */
4982
bool
4983
config_enum_lookup_by_name(struct config_enum * record, const char *value,
A
Alvaro Herrera 已提交
4984
						   int *retval)
4985
{
A
Alvaro Herrera 已提交
4986 4987 4988
	const struct config_enum_entry *entry;

	for (entry = record->options; entry && entry->name; entry++)
4989
	{
4990
		if (pg_strcasecmp(value, entry->name) == 0)
4991 4992 4993 4994 4995
		{
			*retval = entry->val;
			return TRUE;
		}
	}
A
Alvaro Herrera 已提交
4996 4997

	*retval = 0;
4998 4999 5000 5001 5002
	return FALSE;
}


/*
5003
 * Return a list of all available options for an enum, excluding
A
Alvaro Herrera 已提交
5004
 * hidden ones, separated by the given separator.
5005 5006
 * If prefix is non-NULL, it is added before the first enum value.
 * If suffix is non-NULL, it is added to the end of the string.
5007 5008
 */
static char *
5009
config_enum_get_options(struct config_enum * record, const char *prefix,
5010
						const char *suffix, const char *separator)
5011
{
A
Alvaro Herrera 已提交
5012
	const struct config_enum_entry *entry;
5013
	StringInfoData retstr;
A
Alvaro Herrera 已提交
5014
	int			seplen;
5015

A
Alvaro Herrera 已提交
5016 5017
	initStringInfo(&retstr);
	appendStringInfoString(&retstr, prefix);
5018

A
Alvaro Herrera 已提交
5019 5020
	seplen = strlen(separator);
	for (entry = record->options; entry && entry->name; entry++)
5021
	{
5022 5023
		if (!entry->hidden)
		{
A
Alvaro Herrera 已提交
5024 5025
			appendStringInfoString(&retstr, entry->name);
			appendBinaryStringInfo(&retstr, separator, seplen);
5026
		}
5027 5028
	}

5029
	/*
5030 5031 5032 5033 5034
	 * All the entries may have been hidden, leaving the string empty if no
	 * prefix was given. This indicates a broken GUC setup, since there is no
	 * use for an enum without any values, so we just check to make sure we
	 * don't write to invalid memory instead of actually trying to do
	 * something smart with it.
5035
	 */
A
Alvaro Herrera 已提交
5036 5037
	if (retstr.len >= seplen)
	{
5038
		/* Replace final separator */
A
Alvaro Herrera 已提交
5039 5040 5041
		retstr.data[retstr.len - seplen] = '\0';
		retstr.len -= seplen;
	}
5042

A
Alvaro Herrera 已提交
5043
	appendStringInfoString(&retstr, suffix);
5044

A
Alvaro Herrera 已提交
5045
	return retstr.data;
5046 5047
}

5048

5049
/*
5050 5051 5052 5053 5054 5055
 * Sets option `name' to given value. The value should be a string
 * which is going to be parsed and converted to the appropriate data
 * type.  The context and source parameters indicate in which context this
 * function is being called so it can apply the access restrictions
 * properly.
 *
5056 5057 5058 5059 5060 5061 5062
 * If value is NULL, set the option to its default value (normally the
 * reset_val, but if source == PGC_S_DEFAULT we instead use the boot_val).
 *
 * action indicates whether to set the value globally in the session, locally
 * to the current top transaction, or just for the duration of a function call.
 *
 * If changeVal is false then don't really set the option but do all
5063 5064 5065 5066 5067
 * the checks to see if it would work.
 *
 * If there is an error (non-existing option, invalid value) then an
 * ereport(ERROR) is thrown *unless* this is called in a context where we
 * don't want to ereport (currently, startup or SIGHUP config file reread).
5068
 * In that case we write a suitable error message via ereport(LOG) and
5069 5070 5071 5072 5073 5074
 * return false. This is working around the deficiencies in the ereport
 * mechanism, so don't blame me.  In all other cases, the function
 * returns true, including cases where the input is valid but we chose
 * not to apply it because of context or source-priority considerations.
 *
 * See also SetConfigOption for an external interface.
5075
 */
5076 5077 5078
bool
set_config_option(const char *name, const char *value,
				  GucContext context, GucSource source,
5079
				  GucAction action, bool changeVal)
5080
{
5081 5082
	struct config_generic *record;
	int			elevel;
5083
	bool		prohibitValueChange = false;
5084
	bool		makeDefault;
5085

5086 5087 5088 5089 5090 5091
	if (context == PGC_SIGHUP || source == PGC_S_DEFAULT)
	{
		/*
		 * To avoid cluttering the log, only the postmaster bleats loudly
		 * about problems with the config file.
		 */
5092
		elevel = IsUnderPostmaster ? DEBUG3 : LOG;
5093
	}
5094 5095
	else if (source == PGC_S_DATABASE || source == PGC_S_USER ||
			 source == PGC_S_DATABASE_USER)
5096
		elevel = WARNING;
5097 5098
	else
		elevel = ERROR;
5099

5100
	record = find_option(name, true, elevel);
5101 5102 5103 5104 5105 5106
	if (record == NULL)
	{
		ereport(elevel,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
			   errmsg("unrecognized configuration parameter \"%s\"", name)));
		return false;
5107 5108
	}

5109
	/*
B
Bruce Momjian 已提交
5110 5111 5112 5113
	 * If source is postgresql.conf, mark the found record with
	 * GUC_IS_IN_FILE. This is for the convenience of ProcessConfigFile.  Note
	 * that we do it even if changeVal is false, since ProcessConfigFile wants
	 * the marking to occur during its testing pass.
5114
	 */
5115 5116
	if (source == PGC_S_FILE)
		record->status |= GUC_IS_IN_FILE;
5117

5118 5119
	/*
	 * Check if the option can be set at this time. See guc.h for the precise
5120
	 * rules.
5121
	 */
5122 5123
	switch (record->context)
	{
5124
		case PGC_INTERNAL:
5125
			if (context == PGC_SIGHUP)
5126 5127 5128
			{
				/*
				 * Historically we've just silently ignored attempts to set
5129 5130
				 * PGC_INTERNAL variables from the config file.  Maybe it'd be
				 * better to use the prohibitValueChange logic for this?
5131
				 */
5132
				return true;
5133 5134
			}
			else if (context != PGC_INTERNAL)
5135
			{
5136 5137
				ereport(elevel,
						(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5138
						 errmsg("parameter \"%s\" cannot be changed",
5139
								name)));
5140 5141 5142
				return false;
			}
			break;
5143 5144
		case PGC_POSTMASTER:
			if (context == PGC_SIGHUP)
5145
			{
5146
				/*
5147 5148 5149 5150 5151 5152 5153 5154 5155 5156
				 * We are re-reading a PGC_POSTMASTER variable from
				 * postgresql.conf.  We can't change the setting, so we should
				 * give a warning if the DBA tries to change it.  However,
				 * because of variant formats, canonicalization by check
				 * hooks, etc, we can't just compare the given string directly
				 * to what's stored.  Set a flag to check below after we have
				 * the final storable value.
				 *
				 * During the "checking" pass we just do nothing, to avoid
				 * printing the warning twice.
5157
				 */
5158 5159 5160 5161
				if (!changeVal)
					return true;

				prohibitValueChange = true;
5162
			}
5163
			else if (context != PGC_POSTMASTER)
5164
			{
5165 5166
				ereport(elevel,
						(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
B
Bruce Momjian 已提交
5167 5168
						 errmsg("parameter \"%s\" cannot be changed without restarting the server",
								name)));
5169 5170 5171 5172 5173 5174
				return false;
			}
			break;
		case PGC_SIGHUP:
			if (context != PGC_SIGHUP && context != PGC_POSTMASTER)
			{
5175 5176
				ereport(elevel,
						(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
5177
						 errmsg("parameter \"%s\" cannot be changed now",
5178
								name)));
5179 5180 5181 5182
				return false;
			}

			/*
B
Bruce Momjian 已提交
5183 5184 5185 5186
			 * Hmm, the idea of the SIGHUP context is "ought to be global, but
			 * can be changed after postmaster start". But there's nothing
			 * that prevents a crafty administrator from sending SIGHUP
			 * signals to individual backends only.
5187 5188 5189 5190 5191 5192
			 */
			break;
		case PGC_BACKEND:
			if (context == PGC_SIGHUP)
			{
				/*
B
Bruce Momjian 已提交
5193 5194 5195 5196 5197 5198
				 * If a PGC_BACKEND parameter is changed in the config file,
				 * we want to accept the new value in the postmaster (whence
				 * it will propagate to subsequently-started backends), but
				 * ignore it in existing backends.	This is a tad klugy, but
				 * necessary because we don't re-read the config file during
				 * backend start.
5199 5200
				 */
				if (IsUnderPostmaster)
5201
					return true;
5202
			}
5203 5204
			else if (context != PGC_POSTMASTER && context != PGC_BACKEND &&
					 source != PGC_S_CLIENT)
5205
			{
5206 5207
				ereport(elevel,
						(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
B
Bruce Momjian 已提交
5208
						 errmsg("parameter \"%s\" cannot be set after connection start",
5209
								name)));
5210 5211 5212 5213 5214 5215
				return false;
			}
			break;
		case PGC_SUSET:
			if (context == PGC_USERSET || context == PGC_BACKEND)
			{
5216 5217
				ereport(elevel,
						(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
B
Bruce Momjian 已提交
5218
						 errmsg("permission denied to set parameter \"%s\"",
5219
								name)));
5220
				return false;
5221 5222 5223 5224 5225
			}
			break;
		case PGC_USERSET:
			/* always okay */
			break;
5226
	}
5227

5228
	/*
5229
	 * Disallow changing GUC_NOT_WHILE_SEC_REST values if we are inside a
B
Bruce Momjian 已提交
5230 5231 5232
	 * security restriction context.  We can reject this regardless of the GUC
	 * context or source, mainly because sources that it might be reasonable
	 * to override for won't be seen while inside a function.
5233
	 *
5234
	 * Note: variables marked GUC_NOT_WHILE_SEC_REST should usually be marked
5235
	 * GUC_NO_RESET_ALL as well, because ResetAllOptions() doesn't check this.
5236
	 * An exception might be made if the reset value is assumed to be "safe".
5237 5238
	 *
	 * Note: this flag is currently used for "session_authorization" and
B
Bruce Momjian 已提交
5239
	 * "role".	We need to prohibit changing these inside a local userid
5240 5241
	 * context because when we exit it, GUC won't be notified, leaving things
	 * out of sync.  (This could be fixed by forcing a new GUC nesting level,
B
Bruce Momjian 已提交
5242 5243
	 * but that would change behavior in possibly-undesirable ways.)  Also, we
	 * prohibit changing these in a security-restricted operation because
5244
	 * otherwise RESET could be used to regain the session user's privileges.
5245
	 */
5246
	if (record->flags & GUC_NOT_WHILE_SEC_REST)
5247
	{
5248 5249 5250
		if (InLocalUserIdChange())
		{
			/*
B
Bruce Momjian 已提交
5251 5252
			 * Phrasing of this error message is historical, but it's the most
			 * common case.
5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
			 */
			ereport(elevel,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
					 errmsg("cannot set parameter \"%s\" within security-definer function",
							name)));
			return false;
		}
		if (InSecurityRestrictedOperation())
		{
			ereport(elevel,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
					 errmsg("cannot set parameter \"%s\" within security-restricted operation",
							name)));
			return false;
		}
5268 5269
	}

5270
	/*
B
Bruce Momjian 已提交
5271 5272 5273 5274
	 * Should we set reset/stacked values?	(If so, the behavior is not
	 * transactional.)	This is done either when we get a default value from
	 * the database's/user's/client's default settings or when we reset a
	 * value to its default.
5275
	 */
5276 5277
	makeDefault = changeVal && (source <= PGC_S_OVERRIDE) &&
		((value != NULL) || source == PGC_S_DEFAULT);
5278 5279

	/*
5280 5281 5282 5283 5284
	 * Ignore attempted set if overridden by previously processed setting.
	 * However, if changeVal is false then plow ahead anyway since we are
	 * trying to find out if the value is potentially good, not actually use
	 * it. Also keep going if makeDefault is true, since we may want to set
	 * the reset/stacked values even if we can't set the variable itself.
5285
	 */
5286
	if (record->source > source)
5287
	{
5288
		if (changeVal && !makeDefault)
5289
		{
5290
			elog(DEBUG3, "\"%s\": setting ignored because previous source is higher priority",
5291 5292 5293
				 name);
			return true;
		}
5294
		changeVal = false;
5295 5296
	}

5297
	/*
5298
	 * Evaluate value and set variable.
5299
	 */
5300
	switch (record->vartype)
5301 5302 5303
	{
		case PGC_BOOL:
			{
B
Bruce Momjian 已提交
5304
				struct config_bool *conf = (struct config_bool *) record;
5305
				bool		newval;
5306
				void	   *newextra = NULL;
5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317

				if (value)
				{
					if (!parse_bool(value, &newval))
					{
						ereport(elevel,
								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
						  errmsg("parameter \"%s\" requires a Boolean value",
								 name)));
						return false;
					}
5318 5319 5320
					if (!call_bool_check_hook(conf, &newval, &newextra,
											  source, elevel))
						return false;
5321
				}
5322
				else if (source == PGC_S_DEFAULT)
5323
				{
5324
					newval = conf->boot_val;
5325 5326 5327 5328
					if (!call_bool_check_hook(conf, &newval, &newextra,
											  source, elevel))
						return false;
				}
5329 5330 5331
				else
				{
					newval = conf->reset_val;
5332
					newextra = conf->reset_extra;
5333 5334 5335
					source = conf->gen.reset_source;
				}

5336 5337 5338
				if (prohibitValueChange)
				{
					if (*conf->variable != newval)
5339
						ereport(elevel,
5340 5341 5342 5343 5344
								(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
								 errmsg("parameter \"%s\" cannot be changed without restarting the server",
										name)));
					return false;
				}
5345

5346 5347
				if (changeVal)
				{
5348 5349 5350 5351 5352 5353
					/* Save old value to support transaction abort */
					if (!makeDefault)
						push_old_value(&conf->gen, action);

					if (conf->assign_hook)
						(*conf->assign_hook) (newval, newextra);
5354
					*conf->variable = newval;
5355 5356
					set_extra_field(&conf->gen, &conf->gen.extra,
									newextra);
5357 5358 5359
					conf->gen.source = source;
				}
				if (makeDefault)
5360
				{
5361 5362 5363
					GucStack   *stack;

					if (conf->gen.reset_source <= source)
5364
					{
5365
						conf->reset_val = newval;
5366 5367
						set_extra_field(&conf->gen, &conf->reset_extra,
										newextra);
5368
						conf->gen.reset_source = source;
5369
					}
5370
					for (stack = conf->gen.stack; stack; stack = stack->prev)
5371
					{
5372
						if (stack->source <= source)
5373
						{
5374 5375 5376
							stack->prior.val.boolval = newval;
							set_extra_field(&conf->gen, &stack->prior.extra,
											newextra);
5377
							stack->source = source;
5378 5379
						}
					}
5380
				}
5381 5382 5383 5384

				/* Perhaps we didn't install newextra anywhere */
				if (newextra && !extra_field_used(&conf->gen, newextra))
					free(newextra);
B
Bruce Momjian 已提交
5385
				break;
5386
			}
5387 5388

		case PGC_INT:
5389
			{
B
Bruce Momjian 已提交
5390
				struct config_int *conf = (struct config_int *) record;
5391
				int			newval;
5392
				void	   *newextra = NULL;
5393

5394 5395
				if (value)
				{
5396 5397 5398
					const char *hintmsg;

					if (!parse_int(value, &newval, conf->gen.flags, &hintmsg))
5399 5400 5401
					{
						ereport(elevel,
								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
B
Bruce Momjian 已提交
5402 5403
						 errmsg("invalid value for parameter \"%s\": \"%s\"",
								name, value),
5404
								 hintmsg ? errhint("%s", _(hintmsg)) : 0));
5405 5406 5407 5408 5409 5410 5411 5412 5413 5414
						return false;
					}
					if (newval < conf->min || newval > conf->max)
					{
						ereport(elevel,
								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
								 errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)",
										newval, name, conf->min, conf->max)));
						return false;
					}
5415 5416 5417
					if (!call_int_check_hook(conf, &newval, &newextra,
											 source, elevel))
						return false;
5418
				}
5419
				else if (source == PGC_S_DEFAULT)
5420
				{
5421
					newval = conf->boot_val;
5422 5423 5424 5425
					if (!call_int_check_hook(conf, &newval, &newextra,
											 source, elevel))
						return false;
				}
5426 5427 5428
				else
				{
					newval = conf->reset_val;
5429
					newextra = conf->reset_extra;
5430 5431 5432
					source = conf->gen.reset_source;
				}

5433 5434 5435
				if (prohibitValueChange)
				{
					if (*conf->variable != newval)
5436
						ereport(elevel,
5437 5438 5439 5440 5441
								(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
								 errmsg("parameter \"%s\" cannot be changed without restarting the server",
										name)));
					return false;
				}
5442

5443 5444
				if (changeVal)
				{
5445 5446 5447 5448 5449 5450
					/* Save old value to support transaction abort */
					if (!makeDefault)
						push_old_value(&conf->gen, action);

					if (conf->assign_hook)
						(*conf->assign_hook) (newval, newextra);
5451
					*conf->variable = newval;
5452 5453
					set_extra_field(&conf->gen, &conf->gen.extra,
									newextra);
5454 5455 5456
					conf->gen.source = source;
				}
				if (makeDefault)
5457
				{
5458 5459 5460
					GucStack   *stack;

					if (conf->gen.reset_source <= source)
5461
					{
5462
						conf->reset_val = newval;
5463 5464
						set_extra_field(&conf->gen, &conf->reset_extra,
										newextra);
5465
						conf->gen.reset_source = source;
5466
					}
5467
					for (stack = conf->gen.stack; stack; stack = stack->prev)
5468
					{
5469
						if (stack->source <= source)
5470
						{
5471 5472 5473
							stack->prior.val.intval = newval;
							set_extra_field(&conf->gen, &stack->prior.extra,
											newextra);
5474
							stack->source = source;
5475 5476
						}
					}
5477
				}
5478 5479 5480 5481

				/* Perhaps we didn't install newextra anywhere */
				if (newextra && !extra_field_used(&conf->gen, newextra))
					free(newextra);
B
Bruce Momjian 已提交
5482
				break;
5483
			}
5484 5485

		case PGC_REAL:
5486
			{
B
Bruce Momjian 已提交
5487
				struct config_real *conf = (struct config_real *) record;
5488
				double		newval;
5489
				void	   *newextra = NULL;
5490

5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508
				if (value)
				{
					if (!parse_real(value, &newval))
					{
						ereport(elevel,
								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
						  errmsg("parameter \"%s\" requires a numeric value",
								 name)));
						return false;
					}
					if (newval < conf->min || newval > conf->max)
					{
						ereport(elevel,
								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
								 errmsg("%g is outside the valid range for parameter \"%s\" (%g .. %g)",
										newval, name, conf->min, conf->max)));
						return false;
					}
5509 5510 5511
					if (!call_real_check_hook(conf, &newval, &newextra,
											  source, elevel))
						return false;
5512
				}
5513
				else if (source == PGC_S_DEFAULT)
5514
				{
5515
					newval = conf->boot_val;
5516 5517 5518 5519
					if (!call_real_check_hook(conf, &newval, &newextra,
											  source, elevel))
						return false;
				}
5520 5521 5522
				else
				{
					newval = conf->reset_val;
5523
					newextra = conf->reset_extra;
5524 5525 5526
					source = conf->gen.reset_source;
				}

5527 5528 5529
				if (prohibitValueChange)
				{
					if (*conf->variable != newval)
5530
						ereport(elevel,
5531 5532 5533 5534 5535
								(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
								 errmsg("parameter \"%s\" cannot be changed without restarting the server",
										name)));
					return false;
				}
5536

5537 5538
				if (changeVal)
				{
5539 5540 5541 5542 5543 5544
					/* Save old value to support transaction abort */
					if (!makeDefault)
						push_old_value(&conf->gen, action);

					if (conf->assign_hook)
						(*conf->assign_hook) (newval, newextra);
5545
					*conf->variable = newval;
5546 5547
					set_extra_field(&conf->gen, &conf->gen.extra,
									newextra);
5548 5549 5550
					conf->gen.source = source;
				}
				if (makeDefault)
5551
				{
5552 5553 5554
					GucStack   *stack;

					if (conf->gen.reset_source <= source)
5555
					{
5556
						conf->reset_val = newval;
5557 5558
						set_extra_field(&conf->gen, &conf->reset_extra,
										newextra);
5559
						conf->gen.reset_source = source;
5560
					}
5561
					for (stack = conf->gen.stack; stack; stack = stack->prev)
5562
					{
5563
						if (stack->source <= source)
5564
						{
5565 5566 5567
							stack->prior.val.realval = newval;
							set_extra_field(&conf->gen, &stack->prior.extra,
											newextra);
5568
							stack->source = source;
5569 5570
						}
					}
5571
				}
5572 5573 5574 5575

				/* Perhaps we didn't install newextra anywhere */
				if (newextra && !extra_field_used(&conf->gen, newextra))
					free(newextra);
B
Bruce Momjian 已提交
5576
				break;
5577
			}
5578 5579 5580

		case PGC_STRING:
			{
B
Bruce Momjian 已提交
5581
				struct config_string *conf = (struct config_string *) record;
5582
				char	   *newval;
5583
				void	   *newextra = NULL;
B
Bruce Momjian 已提交
5584

5585 5586
				if (value)
				{
5587
					/*
5588 5589
					 * The value passed by the caller could be transient, so
					 * we always strdup it.
5590
					 */
5591 5592 5593
					newval = guc_strdup(elevel, value);
					if (newval == NULL)
						return false;
B
Bruce Momjian 已提交
5594

5595
					/*
5596
					 * The only built-in "parsing" check we have is to apply
B
Bruce Momjian 已提交
5597
					 * truncation if GUC_IS_NAME.
5598 5599 5600
					 */
					if (conf->gen.flags & GUC_IS_NAME)
						truncate_identifier(newval, strlen(newval), true);
5601 5602 5603 5604 5605 5606 5607

					if (!call_string_check_hook(conf, &newval, &newextra,
												source, elevel))
					{
						free(newval);
						return false;
					}
5608
				}
5609 5610
				else if (source == PGC_S_DEFAULT)
				{
5611 5612
					/* non-NULL boot_val must always get strdup'd */
					if (conf->boot_val != NULL)
5613 5614 5615 5616 5617
					{
						newval = guc_strdup(elevel, conf->boot_val);
						if (newval == NULL)
							return false;
					}
5618 5619 5620 5621 5622 5623 5624 5625 5626
					else
						newval = NULL;

					if (!call_string_check_hook(conf, &newval, &newextra,
												source, elevel))
					{
						free(newval);
						return false;
					}
5627
				}
5628
				else
5629 5630
				{
					/*
5631 5632
					 * strdup not needed, since reset_val is already under
					 * guc.c's control
5633
					 */
5634 5635
					newval = conf->reset_val;
					newextra = conf->reset_extra;
5636 5637 5638
					source = conf->gen.reset_source;
				}

5639
				if (prohibitValueChange)
5640
				{
5641 5642 5643
					/* newval shouldn't be NULL, so we're a bit sloppy here */
					if (*conf->variable == NULL || newval == NULL ||
						strcmp(*conf->variable, newval) != 0)
5644
						ereport(elevel,
5645 5646 5647 5648
								(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
								 errmsg("parameter \"%s\" cannot be changed without restarting the server",
										name)));
					return false;
5649
				}
5650

5651
				if (changeVal)
5652
				{
5653 5654 5655 5656 5657 5658
					/* Save old value to support transaction abort */
					if (!makeDefault)
						push_old_value(&conf->gen, action);

					if (conf->assign_hook)
						(*conf->assign_hook) (newval, newextra);
5659
					set_string_field(conf, conf->variable, newval);
5660 5661
					set_extra_field(&conf->gen, &conf->gen.extra,
									newextra);
5662 5663
					conf->gen.source = source;
				}
5664

5665 5666 5667 5668 5669
				if (makeDefault)
				{
					GucStack   *stack;

					if (conf->gen.reset_source <= source)
5670
					{
5671
						set_string_field(conf, &conf->reset_val, newval);
5672 5673
						set_extra_field(&conf->gen, &conf->reset_extra,
										newextra);
5674
						conf->gen.reset_source = source;
5675
					}
5676
					for (stack = conf->gen.stack; stack; stack = stack->prev)
5677
					{
5678
						if (stack->source <= source)
5679
						{
5680
							set_string_field(conf, &stack->prior.val.stringval,
5681
											 newval);
5682 5683
							set_extra_field(&conf->gen, &stack->prior.extra,
											newextra);
5684
							stack->source = source;
5685
						}
B
Bruce Momjian 已提交
5686
					}
5687
				}
5688

5689 5690
				/* Perhaps we didn't install newval anywhere */
				if (newval && !string_field_used(conf, newval))
5691
					free(newval);
5692 5693 5694
				/* Perhaps we didn't install newextra anywhere */
				if (newextra && !extra_field_used(&conf->gen, newextra))
					free(newextra);
B
Bruce Momjian 已提交
5695
				break;
5696
			}
5697

5698 5699 5700 5701
		case PGC_ENUM:
			{
				struct config_enum *conf = (struct config_enum *) record;
				int			newval;
5702
				void	   *newextra = NULL;
5703 5704 5705

				if (value)
				{
5706
					if (!config_enum_lookup_by_name(conf, value, &newval))
5707
					{
5708 5709 5710 5711
						char	   *hintmsg;

						hintmsg = config_enum_get_options(conf,
														"Available values: ",
A
Alvaro Herrera 已提交
5712
														  ".", ", ");
5713 5714 5715

						ereport(elevel,
								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5716 5717
						 errmsg("invalid value for parameter \"%s\": \"%s\"",
								name, value),
5718
								 hintmsg ? errhint("%s", _(hintmsg)) : 0));
5719 5720 5721 5722 5723

						if (hintmsg)
							pfree(hintmsg);
						return false;
					}
5724 5725 5726
					if (!call_enum_check_hook(conf, &newval, &newextra,
											  source, elevel))
						return false;
5727 5728
				}
				else if (source == PGC_S_DEFAULT)
5729
				{
5730
					newval = conf->boot_val;
5731 5732 5733 5734
					if (!call_enum_check_hook(conf, &newval, &newextra,
											  source, elevel))
						return false;
				}
5735 5736 5737
				else
				{
					newval = conf->reset_val;
5738
					newextra = conf->reset_extra;
5739 5740 5741
					source = conf->gen.reset_source;
				}

5742 5743 5744
				if (prohibitValueChange)
				{
					if (*conf->variable != newval)
5745
						ereport(elevel,
5746 5747 5748 5749 5750
								(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
								 errmsg("parameter \"%s\" cannot be changed without restarting the server",
										name)));
					return false;
				}
5751

5752
				if (changeVal)
5753
				{
5754 5755 5756 5757 5758 5759
					/* Save old value to support transaction abort */
					if (!makeDefault)
						push_old_value(&conf->gen, action);

					if (conf->assign_hook)
						(*conf->assign_hook) (newval, newextra);
5760
					*conf->variable = newval;
5761 5762
					set_extra_field(&conf->gen, &conf->gen.extra,
									newextra);
5763 5764 5765 5766 5767 5768 5769
					conf->gen.source = source;
				}
				if (makeDefault)
				{
					GucStack   *stack;

					if (conf->gen.reset_source <= source)
5770
					{
5771
						conf->reset_val = newval;
5772 5773
						set_extra_field(&conf->gen, &conf->reset_extra,
										newextra);
5774
						conf->gen.reset_source = source;
5775
					}
5776
					for (stack = conf->gen.stack; stack; stack = stack->prev)
5777
					{
5778
						if (stack->source <= source)
5779
						{
5780 5781 5782
							stack->prior.val.enumval = newval;
							set_extra_field(&conf->gen, &stack->prior.extra,
											newextra);
5783
							stack->source = source;
5784 5785 5786
						}
					}
				}
5787 5788 5789 5790

				/* Perhaps we didn't install newextra anywhere */
				if (newextra && !extra_field_used(&conf->gen, newextra))
					free(newextra);
5791 5792
				break;
			}
5793
	}
5794

5795
	if (changeVal && (record->flags & GUC_REPORT))
5796
		ReportGUCOption(record);
5797

5798 5799 5800 5801
	return true;
}


5802 5803 5804 5805 5806 5807 5808 5809 5810 5811
/*
 * Set the fields for source file and line number the setting came from.
 */
static void
set_config_sourcefile(const char *name, char *sourcefile, int sourceline)
{
	struct config_generic *record;
	int			elevel;

	/*
5812 5813
	 * To avoid cluttering the log, only the postmaster bleats loudly about
	 * problems with the config file.
5814 5815 5816 5817 5818 5819 5820 5821
	 */
	elevel = IsUnderPostmaster ? DEBUG3 : LOG;

	record = find_option(name, true, elevel);
	/* should not happen */
	if (record == NULL)
		elog(ERROR, "unrecognized configuration parameter \"%s\"", name);

5822
	sourcefile = guc_strdup(elevel, sourcefile);
5823 5824
	if (record->sourcefile)
		free(record->sourcefile);
5825
	record->sourcefile = sourcefile;
5826 5827 5828
	record->sourceline = sourceline;
}

5829 5830
/*
 * Set a config option to the given value. See also set_config_option,
B
Bruce Momjian 已提交
5831
 * this is just the wrapper to be called from outside GUC.	NB: this
5832
 * is used only for non-transactional operations.
5833 5834 5835
 *
 * Note: there is no support here for setting source file/line, as it
 * is currently not needed.
5836 5837
 */
void
5838
SetConfigOption(const char *name, const char *value,
5839
				GucContext context, GucSource source)
5840
{
5841 5842
	(void) set_config_option(name, value, context, source,
							 GUC_ACTION_SET, true);
5843 5844 5845 5846 5847
}



/*
5848 5849 5850 5851 5852
 * Fetch the current value of the option `name', as a string.
 *
 * If the option doesn't exist, return NULL if missing_ok is true (NOTE that
 * this cannot be distinguished from a string variable with a NULL value!),
 * otherwise throw an ereport and don't return.
5853
 *
5854 5855 5856 5857
 * If restrict_superuser is true, we also enforce that only superusers can
 * see GUC_SUPERUSER_ONLY variables.  This should only be passed as true
 * in user-driven calls.
 *
5858 5859 5860 5861
 * The string is *not* allocated for modification and is really only
 * valid until the next call to configuration related functions.
 */
const char *
5862
GetConfigOption(const char *name, bool missing_ok, bool restrict_superuser)
5863
{
B
Bruce Momjian 已提交
5864
	struct config_generic *record;
5865 5866
	static char buffer[256];

5867
	record = find_option(name, false, ERROR);
5868
	if (record == NULL)
5869 5870 5871
	{
		if (missing_ok)
			return NULL;
5872 5873
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
5874 5875 5876
				 errmsg("unrecognized configuration parameter \"%s\"",
						name)));
	}
5877 5878 5879
	if (restrict_superuser &&
		(record->flags & GUC_SUPERUSER_ONLY) &&
		!superuser())
5880 5881 5882
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 errmsg("must be superuser to examine \"%s\"", name)));
5883

5884
	switch (record->vartype)
5885 5886
	{
		case PGC_BOOL:
B
Bruce Momjian 已提交
5887
			return *((struct config_bool *) record)->variable ? "on" : "off";
5888

5889
		case PGC_INT:
5890 5891
			snprintf(buffer, sizeof(buffer), "%d",
					 *((struct config_int *) record)->variable);
5892 5893
			return buffer;

5894
		case PGC_REAL:
5895 5896
			snprintf(buffer, sizeof(buffer), "%g",
					 *((struct config_real *) record)->variable);
5897 5898 5899
			return buffer;

		case PGC_STRING:
B
Bruce Momjian 已提交
5900
			return *((struct config_string *) record)->variable;
5901 5902

		case PGC_ENUM:
5903
			return config_enum_lookup_by_value((struct config_enum *) record,
5904
								 *((struct config_enum *) record)->variable);
5905 5906 5907
	}
	return NULL;
}
5908

5909 5910
/*
 * Get the RESET value associated with the given option.
5911 5912 5913 5914
 *
 * Note: this is not re-entrant, due to use of static result buffer;
 * not to mention that a string variable could have its reset_val changed.
 * Beware of assuming the result value is good for very long.
5915 5916 5917
 */
const char *
GetConfigOptionResetString(const char *name)
5918
{
5919 5920
	struct config_generic *record;
	static char buffer[256];
5921

5922
	record = find_option(name, false, ERROR);
5923
	if (record == NULL)
5924 5925
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
B
Bruce Momjian 已提交
5926
			   errmsg("unrecognized configuration parameter \"%s\"", name)));
5927 5928 5929 5930
	if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 errmsg("must be superuser to examine \"%s\"", name)));
5931 5932

	switch (record->vartype)
5933 5934
	{
		case PGC_BOOL:
5935
			return ((struct config_bool *) record)->reset_val ? "on" : "off";
5936

5937
		case PGC_INT:
5938
			snprintf(buffer, sizeof(buffer), "%d",
5939 5940
					 ((struct config_int *) record)->reset_val);
			return buffer;
5941 5942

		case PGC_REAL:
5943
			snprintf(buffer, sizeof(buffer), "%g",
5944 5945
					 ((struct config_real *) record)->reset_val);
			return buffer;
5946 5947

		case PGC_STRING:
5948
			return ((struct config_string *) record)->reset_val;
5949 5950

		case PGC_ENUM:
5951
			return config_enum_lookup_by_value((struct config_enum *) record,
5952
								 ((struct config_enum *) record)->reset_val);
5953 5954 5955
	}
	return NULL;
}
5956

5957 5958 5959 5960 5961 5962 5963 5964 5965

/*
 * flatten_set_variable_args
 *		Given a parsenode List as emitted by the grammar for SET,
 *		convert to the flat string representation used by GUC.
 *
 * We need to be told the name of the variable the args are for, because
 * the flattening rules vary (ugh).
 *
5966
 * The result is NULL if args is NIL (ie, SET ... TO DEFAULT), otherwise
5967 5968
 * a palloc'd string.
 */
5969
static char *
5970 5971 5972 5973 5974
flatten_set_variable_args(const char *name, List *args)
{
	struct config_generic *record;
	int			flags;
	StringInfoData buf;
5975
	ListCell   *l;
5976

5977
	/* Fast path if just DEFAULT */
5978 5979 5980
	if (args == NIL)
		return NULL;

5981 5982 5983 5984 5985 5986 5987 5988 5989
	/*
	 * Get flags for the variable; if it's not known, use default flags.
	 * (Caller might throw error later, but not our business to do so here.)
	 */
	record = find_option(name, false, WARNING);
	if (record)
		flags = record->flags;
	else
		flags = 0;
5990 5991 5992

	/* Complain if list input and non-list variable */
	if ((flags & GUC_LIST_INPUT) == 0 &&
5993
		list_length(args) != 1)
5994 5995 5996
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("SET %s takes only one argument", name)));
5997 5998 5999

	initStringInfo(&buf);

A
Alvaro Herrera 已提交
6000 6001
	/*
	 * Each list member may be a plain A_Const node, or an A_Const within a
6002 6003
	 * TypeCast; the latter case is supported only for ConstInterval arguments
	 * (for SET TIME ZONE).
A
Alvaro Herrera 已提交
6004
	 */
6005 6006
	foreach(l, args)
	{
6007
		Node	   *arg = (Node *) lfirst(l);
6008
		char	   *val;
6009
		TypeName   *typeName = NULL;
6010
		A_Const    *con;
6011

6012
		if (l != list_head(args))
6013 6014
			appendStringInfo(&buf, ", ");

A
Alvaro Herrera 已提交
6015 6016
		if (IsA(arg, TypeCast))
		{
6017
			TypeCast   *tc = (TypeCast *) arg;
A
Alvaro Herrera 已提交
6018 6019

			arg = tc->arg;
6020
			typeName = tc->typeName;
A
Alvaro Herrera 已提交
6021 6022
		}

6023
		if (!IsA(arg, A_Const))
6024
			elog(ERROR, "unrecognized node type: %d", (int) nodeTag(arg));
A
Alvaro Herrera 已提交
6025
		con = (A_Const *) arg;
6026

A
Alvaro Herrera 已提交
6027
		switch (nodeTag(&con->val))
6028 6029
		{
			case T_Integer:
A
Alvaro Herrera 已提交
6030
				appendStringInfo(&buf, "%ld", intVal(&con->val));
6031 6032 6033
				break;
			case T_Float:
				/* represented as a string, so just copy it */
A
Alvaro Herrera 已提交
6034
				appendStringInfoString(&buf, strVal(&con->val));
6035 6036
				break;
			case T_String:
A
Alvaro Herrera 已提交
6037
				val = strVal(&con->val);
6038
				if (typeName != NULL)
6039 6040
				{
					/*
B
Bruce Momjian 已提交
6041 6042 6043
					 * Must be a ConstInterval argument for TIME ZONE. Coerce
					 * to interval and back to normalize the value and account
					 * for any typmod.
6044
					 */
6045
					Oid			typoid;
6046
					int32		typmod;
B
Bruce Momjian 已提交
6047
					Datum		interval;
B
Bruce Momjian 已提交
6048
					char	   *intervalout;
6049

P
Peter Eisentraut 已提交
6050
					typenameTypeIdAndMod(NULL, typeName, &typoid, &typmod);
6051
					Assert(typoid == INTERVALOID);
6052

6053
					interval =
B
Bruce Momjian 已提交
6054 6055 6056
						DirectFunctionCall3(interval_in,
											CStringGetDatum(val),
											ObjectIdGetDatum(InvalidOid),
6057
											Int32GetDatum(typmod));
6058 6059

					intervalout =
6060 6061
						DatumGetCString(DirectFunctionCall1(interval_out,
															interval));
6062 6063 6064 6065
					appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
				}
				else
				{
6066 6067 6068 6069
					/*
					 * Plain string literal or identifier.	For quote mode,
					 * quote it if it's not a vanilla identifier.
					 */
6070
					if (flags & GUC_LIST_QUOTE)
N
Neil Conway 已提交
6071
						appendStringInfoString(&buf, quote_identifier(val));
6072
					else
N
Neil Conway 已提交
6073
						appendStringInfoString(&buf, val);
6074 6075 6076
				}
				break;
			default:
6077
				elog(ERROR, "unrecognized node type: %d",
A
Alvaro Herrera 已提交
6078
					 (int) nodeTag(&con->val));
6079 6080
				break;
		}
6081
	}
6082 6083 6084 6085 6086 6087 6088 6089 6090

	return buf.data;
}


/*
 * SET command
 */
void
6091 6092
ExecSetVariableStmt(VariableSetStmt *stmt)
{
B
Bruce Momjian 已提交
6093
	GucAction	action = stmt->is_local ? GUC_ACTION_LOCAL : GUC_ACTION_SET;
6094

6095 6096 6097 6098 6099 6100 6101 6102
	switch (stmt->kind)
	{
		case VAR_SET_VALUE:
		case VAR_SET_CURRENT:
			set_config_option(stmt->name,
							  ExtractSetVariableArgs(stmt),
							  (superuser() ? PGC_SUSET : PGC_USERSET),
							  PGC_S_SESSION,
6103
							  action,
6104 6105 6106
							  true);
			break;
		case VAR_SET_MULTI:
B
Bruce Momjian 已提交
6107

6108
			/*
B
Bruce Momjian 已提交
6109 6110
			 * Special case for special SQL syntax that effectively sets more
			 * than one variable per statement.
6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125
			 */
			if (strcmp(stmt->name, "TRANSACTION") == 0)
			{
				ListCell   *head;

				foreach(head, stmt->args)
				{
					DefElem    *item = (DefElem *) lfirst(head);

					if (strcmp(item->defname, "transaction_isolation") == 0)
						SetPGVariable("transaction_isolation",
									  list_make1(item->arg), stmt->is_local);
					else if (strcmp(item->defname, "transaction_read_only") == 0)
						SetPGVariable("transaction_read_only",
									  list_make1(item->arg), stmt->is_local);
6126 6127 6128
					else if (strcmp(item->defname, "transaction_deferrable") == 0)
						SetPGVariable("transaction_deferrable",
									  list_make1(item->arg), stmt->is_local);
6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147
					else
						elog(ERROR, "unexpected SET TRANSACTION element: %s",
							 item->defname);
				}
			}
			else if (strcmp(stmt->name, "SESSION CHARACTERISTICS") == 0)
			{
				ListCell   *head;

				foreach(head, stmt->args)
				{
					DefElem    *item = (DefElem *) lfirst(head);

					if (strcmp(item->defname, "transaction_isolation") == 0)
						SetPGVariable("default_transaction_isolation",
									  list_make1(item->arg), stmt->is_local);
					else if (strcmp(item->defname, "transaction_read_only") == 0)
						SetPGVariable("default_transaction_read_only",
									  list_make1(item->arg), stmt->is_local);
6148 6149 6150
					else if (strcmp(item->defname, "transaction_deferrable") == 0)
						SetPGVariable("default_transaction_deferrable",
									  list_make1(item->arg), stmt->is_local);
6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165
					else
						elog(ERROR, "unexpected SET SESSION element: %s",
							 item->defname);
				}
			}
			else
				elog(ERROR, "unexpected SET MULTI element: %s",
					 stmt->name);
			break;
		case VAR_SET_DEFAULT:
		case VAR_RESET:
			set_config_option(stmt->name,
							  NULL,
							  (superuser() ? PGC_SUSET : PGC_USERSET),
							  PGC_S_SESSION,
6166
							  action,
6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201
							  true);
			break;
		case VAR_RESET_ALL:
			ResetAllOptions();
			break;
	}
}

/*
 * Get the value to assign for a VariableSetStmt, or NULL if it's RESET.
 * The result is palloc'd.
 *
 * This is exported for use by actions such as ALTER ROLE SET.
 */
char *
ExtractSetVariableArgs(VariableSetStmt *stmt)
{
	switch (stmt->kind)
	{
		case VAR_SET_VALUE:
			return flatten_set_variable_args(stmt->name, stmt->args);
		case VAR_SET_CURRENT:
			return GetConfigOptionByName(stmt->name, NULL);
		default:
			return NULL;
	}
}

/*
 * SetPGVariable - SET command exported as an easily-C-callable function.
 *
 * This provides access to SET TO value, as well as SET TO DEFAULT (expressed
 * by passing args == NIL), but not SET FROM CURRENT functionality.
 */
void
6202 6203 6204 6205 6206 6207 6208 6209 6210
SetPGVariable(const char *name, List *args, bool is_local)
{
	char	   *argstring = flatten_set_variable_args(name, args);

	/* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
	set_config_option(name,
					  argstring,
					  (superuser() ? PGC_SUSET : PGC_USERSET),
					  PGC_S_SESSION,
6211
					  is_local ? GUC_ACTION_LOCAL : GUC_ACTION_SET,
6212 6213 6214
					  true);
}

6215 6216 6217 6218 6219 6220
/*
 * SET command wrapped as a SQL callable function.
 */
Datum
set_config_by_name(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
6221 6222 6223 6224
	char	   *name;
	char	   *value;
	char	   *new_value;
	bool		is_local;
6225 6226

	if (PG_ARGISNULL(0))
6227 6228
		ereport(ERROR,
				(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6229
				 errmsg("SET requires parameter name")));
6230 6231

	/* Get the GUC variable name */
6232
	name = TextDatumGetCString(PG_GETARG_DATUM(0));
6233 6234 6235 6236 6237

	/* Get the desired value or set to NULL for a reset request */
	if (PG_ARGISNULL(1))
		value = NULL;
	else
6238
		value = TextDatumGetCString(PG_GETARG_DATUM(1));
6239 6240

	/*
B
Bruce Momjian 已提交
6241 6242
	 * Get the desired state of is_local. Default to false if provided value
	 * is NULL
6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253
	 */
	if (PG_ARGISNULL(2))
		is_local = false;
	else
		is_local = PG_GETARG_BOOL(2);

	/* Note SET DEFAULT (argstring == NULL) is equivalent to RESET */
	set_config_option(name,
					  value,
					  (superuser() ? PGC_SUSET : PGC_USERSET),
					  PGC_S_SESSION,
6254
					  is_local ? GUC_ACTION_LOCAL : GUC_ACTION_SET,
6255 6256 6257
					  true);

	/* get the new current value */
6258
	new_value = GetConfigOptionByName(name, NULL);
6259 6260

	/* Convert return string to text */
6261
	PG_RETURN_TEXT_P(cstring_to_text(new_value));
6262 6263
}

6264

6265 6266 6267 6268 6269 6270 6271 6272 6273
/*
 * Common code for DefineCustomXXXVariable subroutines: allocate the
 * new variable's config struct and fill in generic fields.
 */
static struct config_generic *
init_custom_variable(const char *name,
					 const char *short_desc,
					 const char *long_desc,
					 GucContext context,
6274
					 int flags,
6275 6276 6277 6278 6279
					 enum config_type type,
					 size_t sz)
{
	struct config_generic *gen;

6280
	/*
6281 6282 6283 6284 6285
	 * Only allow custom PGC_POSTMASTER variables to be created during shared
	 * library preload; any later than that, we can't ensure that the value
	 * doesn't change after startup.  This is a fatal elog if it happens; just
	 * erroring out isn't safe because we don't know what the calling loadable
	 * module might already have hooked into.
6286 6287 6288 6289 6290
	 */
	if (context == PGC_POSTMASTER &&
		!process_shared_preload_libraries_in_progress)
		elog(FATAL, "cannot create PGC_POSTMASTER variables after startup");

6291 6292 6293 6294 6295 6296 6297 6298
	gen = (struct config_generic *) guc_malloc(ERROR, sz);
	memset(gen, 0, sz);

	gen->name = guc_strdup(ERROR, name);
	gen->context = context;
	gen->group = CUSTOM_OPTIONS;
	gen->short_desc = short_desc;
	gen->long_desc = long_desc;
6299
	gen->flags = flags;
6300 6301 6302 6303 6304 6305 6306 6307 6308
	gen->vartype = type;

	return gen;
}

/*
 * Common code for DefineCustomXXXVariable subroutines: insert the new
 * variable into the GUC variable array, replacing any placeholder.
 */
6309
static void
B
Bruce Momjian 已提交
6310
define_custom_variable(struct config_generic * variable)
6311
{
B
Bruce Momjian 已提交
6312 6313 6314 6315
	const char *name = variable->name;
	const char **nameAddr = &name;
	const char *value;
	struct config_string *pHolder;
6316
	GucContext	phcontext;
6317
	struct config_generic **res;
B
Bruce Momjian 已提交
6318

6319 6320 6321
	/*
	 * See if there's a placeholder by the same name.
	 */
6322 6323 6324 6325 6326
	res = (struct config_generic **) bsearch((void *) &nameAddr,
											 (void *) guc_variables,
											 num_guc_variables,
											 sizeof(struct config_generic *),
											 guc_var_compare);
B
Bruce Momjian 已提交
6327
	if (res == NULL)
6328
	{
6329 6330 6331 6332 6333
		/*
		 * No placeholder to replace, so we can just add it ... but first,
		 * make sure it's initialized to its default value.
		 */
		InitializeOneGUCOption(variable);
6334
		add_guc_variable(variable, ERROR);
6335 6336 6337
		return;
	}

B
Bruce Momjian 已提交
6338 6339
	/*
	 * This better be a placeholder
6340
	 */
B
Bruce Momjian 已提交
6341
	if (((*res)->flags & GUC_CUSTOM_PLACEHOLDER) == 0)
6342 6343 6344
		ereport(ERROR,
				(errcode(ERRCODE_INTERNAL_ERROR),
				 errmsg("attempt to redefine parameter \"%s\"", name)));
6345 6346

	Assert((*res)->vartype == PGC_STRING);
6347
	pHolder = (struct config_string *) (*res);
B
Bruce Momjian 已提交
6348

6349 6350 6351 6352 6353 6354 6355
	/*
	 * First, set the variable to its default value.  We must do this even
	 * though we intend to immediately apply a new value, since it's possible
	 * that the new value is invalid.
	 */
	InitializeOneGUCOption(variable);

6356
	/*
B
Bruce Momjian 已提交
6357 6358
	 * Replace the placeholder. We aren't changing the name, so no re-sorting
	 * is necessary
6359
	 */
6360 6361
	*res = variable;

6362
	/*
6363 6364 6365
	 * Infer context for assignment based on source of existing value. We
	 * can't tell this with exact accuracy, but we can at least do something
	 * reasonable in typical cases.
6366 6367 6368 6369
	 */
	switch (pHolder->gen.source)
	{
		case PGC_S_DEFAULT:
6370
		case PGC_S_DYNAMIC_DEFAULT:
6371 6372 6373
		case PGC_S_ENV_VAR:
		case PGC_S_FILE:
		case PGC_S_ARGV:
6374

6375
			/*
6376 6377 6378
			 * If we got past the check in init_custom_variable, we can safely
			 * assume that any existing value for a PGC_POSTMASTER variable
			 * was set in postmaster context.
6379 6380 6381 6382 6383
			 */
			if (variable->context == PGC_POSTMASTER)
				phcontext = PGC_POSTMASTER;
			else
				phcontext = PGC_SIGHUP;
6384
			break;
6385

6386 6387
		case PGC_S_DATABASE:
		case PGC_S_USER:
6388
		case PGC_S_DATABASE_USER:
B
Bruce Momjian 已提交
6389

6390
			/*
B
Bruce Momjian 已提交
6391 6392 6393 6394 6395
			 * The existing value came from an ALTER ROLE/DATABASE SET
			 * command. We can assume that at the time the command was issued,
			 * we checked that the issuing user was superuser if the variable
			 * requires superuser privileges to set.  So it's safe to use
			 * SUSET context here.
6396 6397 6398 6399
			 */
			phcontext = PGC_SUSET;
			break;

6400 6401 6402
		case PGC_S_CLIENT:
		case PGC_S_SESSION:
		default:
B
Bruce Momjian 已提交
6403

6404
			/*
B
Bruce Momjian 已提交
6405 6406
			 * We must assume that the value came from an untrusted user, even
			 * if the current_user is a superuser.
6407
			 */
6408 6409 6410 6411
			phcontext = PGC_USERSET;
			break;
	}

6412
	/*
B
Bruce Momjian 已提交
6413
	 * Assign the string value stored in the placeholder to the real variable.
6414 6415 6416
	 *
	 * XXX this is not really good enough --- it should be a nontransactional
	 * assignment, since we don't want it to roll back if the current xact
6417
	 * fails later.  (Or do we?)
6418
	 */
6419 6420 6421
	value = *pHolder->variable;

	if (value)
6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432
	{
		if (set_config_option(name, value,
							  phcontext, pHolder->gen.source,
							  GUC_ACTION_SET, true))
		{
			/* Also copy over any saved source-location information */
			if (pHolder->gen.sourcefile)
				set_config_sourcefile(name, pHolder->gen.sourcefile,
									  pHolder->gen.sourceline);
		}
	}
6433

6434 6435 6436
	/*
	 * Free up as much as we conveniently can of the placeholder structure
	 * (this neglects any stack items...)
6437
	 */
6438 6439
	set_string_field(pHolder, pHolder->variable, NULL);
	set_string_field(pHolder, &pHolder->reset_val, NULL);
6440 6441 6442 6443

	free(pHolder);
}

B
Bruce Momjian 已提交
6444
void
6445
DefineCustomBoolVariable(const char *name,
B
Bruce Momjian 已提交
6446 6447 6448
						 const char *short_desc,
						 const char *long_desc,
						 bool *valueAddr,
6449
						 bool bootValue,
B
Bruce Momjian 已提交
6450
						 GucContext context,
6451
						 int flags,
6452
						 GucBoolCheckHook check_hook,
B
Bruce Momjian 已提交
6453 6454
						 GucBoolAssignHook assign_hook,
						 GucShowHook show_hook)
6455
{
6456
	struct config_bool *var;
6457

6458
	var = (struct config_bool *)
6459
		init_custom_variable(name, short_desc, long_desc, context, flags,
6460
							 PGC_BOOL, sizeof(struct config_bool));
B
Bruce Momjian 已提交
6461
	var->variable = valueAddr;
6462 6463
	var->boot_val = bootValue;
	var->reset_val = bootValue;
6464
	var->check_hook = check_hook;
6465
	var->assign_hook = assign_hook;
B
Bruce Momjian 已提交
6466
	var->show_hook = show_hook;
6467 6468 6469
	define_custom_variable(&var->gen);
}

B
Bruce Momjian 已提交
6470
void
6471
DefineCustomIntVariable(const char *name,
B
Bruce Momjian 已提交
6472 6473 6474
						const char *short_desc,
						const char *long_desc,
						int *valueAddr,
6475
						int bootValue,
6476 6477
						int minValue,
						int maxValue,
B
Bruce Momjian 已提交
6478
						GucContext context,
6479
						int flags,
6480
						GucIntCheckHook check_hook,
B
Bruce Momjian 已提交
6481 6482
						GucIntAssignHook assign_hook,
						GucShowHook show_hook)
6483
{
6484
	struct config_int *var;
6485

6486
	var = (struct config_int *)
6487
		init_custom_variable(name, short_desc, long_desc, context, flags,
6488
							 PGC_INT, sizeof(struct config_int));
B
Bruce Momjian 已提交
6489
	var->variable = valueAddr;
6490 6491
	var->boot_val = bootValue;
	var->reset_val = bootValue;
6492 6493
	var->min = minValue;
	var->max = maxValue;
6494
	var->check_hook = check_hook;
6495
	var->assign_hook = assign_hook;
B
Bruce Momjian 已提交
6496
	var->show_hook = show_hook;
6497 6498 6499
	define_custom_variable(&var->gen);
}

B
Bruce Momjian 已提交
6500
void
6501
DefineCustomRealVariable(const char *name,
B
Bruce Momjian 已提交
6502 6503 6504
						 const char *short_desc,
						 const char *long_desc,
						 double *valueAddr,
6505
						 double bootValue,
6506 6507
						 double minValue,
						 double maxValue,
B
Bruce Momjian 已提交
6508
						 GucContext context,
6509
						 int flags,
6510
						 GucRealCheckHook check_hook,
B
Bruce Momjian 已提交
6511 6512
						 GucRealAssignHook assign_hook,
						 GucShowHook show_hook)
6513
{
6514
	struct config_real *var;
6515

6516
	var = (struct config_real *)
6517
		init_custom_variable(name, short_desc, long_desc, context, flags,
6518
							 PGC_REAL, sizeof(struct config_real));
B
Bruce Momjian 已提交
6519
	var->variable = valueAddr;
6520 6521
	var->boot_val = bootValue;
	var->reset_val = bootValue;
6522 6523
	var->min = minValue;
	var->max = maxValue;
6524
	var->check_hook = check_hook;
6525
	var->assign_hook = assign_hook;
B
Bruce Momjian 已提交
6526
	var->show_hook = show_hook;
6527 6528 6529
	define_custom_variable(&var->gen);
}

B
Bruce Momjian 已提交
6530
void
6531
DefineCustomStringVariable(const char *name,
B
Bruce Momjian 已提交
6532 6533 6534
						   const char *short_desc,
						   const char *long_desc,
						   char **valueAddr,
6535
						   const char *bootValue,
B
Bruce Momjian 已提交
6536
						   GucContext context,
6537
						   int flags,
6538
						   GucStringCheckHook check_hook,
B
Bruce Momjian 已提交
6539 6540
						   GucStringAssignHook assign_hook,
						   GucShowHook show_hook)
6541
{
6542
	struct config_string *var;
6543

6544
	var = (struct config_string *)
6545
		init_custom_variable(name, short_desc, long_desc, context, flags,
6546
							 PGC_STRING, sizeof(struct config_string));
B
Bruce Momjian 已提交
6547
	var->variable = valueAddr;
6548
	var->boot_val = bootValue;
6549
	var->check_hook = check_hook;
6550
	var->assign_hook = assign_hook;
B
Bruce Momjian 已提交
6551
	var->show_hook = show_hook;
6552 6553 6554
	define_custom_variable(&var->gen);
}

6555 6556 6557 6558 6559
void
DefineCustomEnumVariable(const char *name,
						 const char *short_desc,
						 const char *long_desc,
						 int *valueAddr,
6560
						 int bootValue,
6561
						 const struct config_enum_entry * options,
6562
						 GucContext context,
6563
						 int flags,
6564
						 GucEnumCheckHook check_hook,
6565 6566 6567 6568 6569 6570
						 GucEnumAssignHook assign_hook,
						 GucShowHook show_hook)
{
	struct config_enum *var;

	var = (struct config_enum *)
6571
		init_custom_variable(name, short_desc, long_desc, context, flags,
6572 6573
							 PGC_ENUM, sizeof(struct config_enum));
	var->variable = valueAddr;
6574 6575
	var->boot_val = bootValue;
	var->reset_val = bootValue;
6576
	var->options = options;
6577
	var->check_hook = check_hook;
6578 6579 6580 6581 6582
	var->assign_hook = assign_hook;
	var->show_hook = show_hook;
	define_custom_variable(&var->gen);
}

N
Neil Conway 已提交
6583
void
B
Bruce Momjian 已提交
6584
EmitWarningsOnPlaceholders(const char *className)
6585
{
6586 6587
	int			classLen = strlen(className);
	int			i;
6588

6589
	for (i = 0; i < num_guc_variables; i++)
6590
	{
6591
		struct config_generic *var = guc_variables[i];
B
Bruce Momjian 已提交
6592 6593

		if ((var->flags & GUC_CUSTOM_PLACEHOLDER) != 0 &&
6594 6595
			strncmp(className, var->name, classLen) == 0 &&
			var->name[classLen] == GUC_QUALIFIER_SEPARATOR)
6596
		{
6597
			ereport(WARNING,
B
Bruce Momjian 已提交
6598
					(errcode(ERRCODE_UNDEFINED_OBJECT),
6599 6600
					 errmsg("unrecognized configuration parameter \"%s\"",
							var->name)));
6601 6602 6603 6604 6605
		}
	}
}


6606 6607 6608 6609
/*
 * SHOW command
 */
void
6610
GetPGVariable(const char *name, DestReceiver *dest)
6611
{
6612
	if (guc_name_compare(name, "all") == 0)
6613
		ShowAllGUCConfig(dest);
6614
	else
6615 6616 6617 6618 6619 6620 6621 6622
		ShowGUCConfigOption(name, dest);
}

TupleDesc
GetPGVariableResultDesc(const char *name)
{
	TupleDesc	tupdesc;

6623
	if (guc_name_compare(name, "all") == 0)
6624
	{
6625 6626
		/* need a tuple descriptor representing three TEXT columns */
		tupdesc = CreateTemplateTupleDesc(3, false);
6627
		TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
6628
						   TEXTOID, -1, 0);
6629
		TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
6630
						   TEXTOID, -1, 0);
6631 6632
		TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
						   TEXTOID, -1, 0);
6633 6634 6635 6636 6637 6638 6639 6640 6641 6642
	}
	else
	{
		const char *varname;

		/* Get the canonical spelling of name */
		(void) GetConfigOptionByName(name, &varname);

		/* need a tuple descriptor representing a single TEXT column */
		tupdesc = CreateTemplateTupleDesc(1, false);
6643 6644
		TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
						   TEXTOID, -1, 0);
6645 6646
	}
	return tupdesc;
6647 6648 6649 6650 6651 6652
}


/*
 * SHOW command
 */
6653
static void
6654
ShowGUCConfigOption(const char *name, DestReceiver *dest)
6655
{
6656
	TupOutputState *tstate;
B
Bruce Momjian 已提交
6657 6658 6659
	TupleDesc	tupdesc;
	const char *varname;
	char	   *value;
6660

6661 6662 6663
	/* Get the value and canonical spelling of name */
	value = GetConfigOptionByName(name, &varname);

6664
	/* need a tuple descriptor representing a single TEXT column */
6665
	tupdesc = CreateTemplateTupleDesc(1, false);
6666
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, varname,
6667
					   TEXTOID, -1, 0);
6668

6669 6670 6671 6672
	/* prepare for projection of tuples */
	tstate = begin_tup_output_tupdesc(dest, tupdesc);

	/* Send it */
6673
	do_text_output_oneline(tstate, value);
6674 6675

	end_tup_output(tstate);
6676 6677
}

6678 6679 6680
/*
 * SHOW ALL command
 */
6681
static void
6682
ShowAllGUCConfig(DestReceiver *dest)
6683
{
6684
	bool		am_superuser = superuser();
6685
	int			i;
6686
	TupOutputState *tstate;
B
Bruce Momjian 已提交
6687
	TupleDesc	tupdesc;
B
Bruce Momjian 已提交
6688 6689
	Datum		values[3];
	bool		isnull[3] = {false, false, false};
6690

6691 6692
	/* need a tuple descriptor representing three TEXT columns */
	tupdesc = CreateTemplateTupleDesc(3, false);
6693
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
6694
					   TEXTOID, -1, 0);
6695
	TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
6696
					   TEXTOID, -1, 0);
6697 6698
	TupleDescInitEntry(tupdesc, (AttrNumber) 3, "description",
					   TEXTOID, -1, 0);
B
Bruce Momjian 已提交
6699

6700 6701
	/* prepare for projection of tuples */
	tstate = begin_tup_output_tupdesc(dest, tupdesc);
6702

6703 6704
	for (i = 0; i < num_guc_variables; i++)
	{
6705
		struct config_generic *conf = guc_variables[i];
B
Bruce Momjian 已提交
6706
		char	   *setting;
6707

6708 6709
		if ((conf->flags & GUC_NO_SHOW_ALL) ||
			((conf->flags & GUC_SUPERUSER_ONLY) && !am_superuser))
6710
			continue;
6711 6712

		/* assign to the values array */
6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727
		values[0] = PointerGetDatum(cstring_to_text(conf->name));

		setting = _ShowOption(conf, true);
		if (setting)
		{
			values[1] = PointerGetDatum(cstring_to_text(setting));
			isnull[1] = false;
		}
		else
		{
			values[1] = PointerGetDatum(NULL);
			isnull[1] = true;
		}

		values[2] = PointerGetDatum(cstring_to_text(conf->short_desc));
6728 6729

		/* send it to dest */
6730
		do_tup_output(tstate, values, isnull);
6731

6732
		/* clean up */
6733 6734 6735 6736 6737 6738 6739
		pfree(DatumGetPointer(values[0]));
		if (setting)
		{
			pfree(setting);
			pfree(DatumGetPointer(values[1]));
		}
		pfree(DatumGetPointer(values[2]));
6740
	}
6741 6742

	end_tup_output(tstate);
6743
}
6744

6745
/*
6746 6747
 * Return GUC variable value by name; optionally return canonical
 * form of name.  Return value is palloc'd.
6748 6749
 */
char *
6750
GetConfigOptionByName(const char *name, const char **varname)
6751 6752 6753
{
	struct config_generic *record;

6754
	record = find_option(name, false, ERROR);
6755
	if (record == NULL)
6756 6757
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
B
Bruce Momjian 已提交
6758
			   errmsg("unrecognized configuration parameter \"%s\"", name)));
6759 6760 6761 6762
	if ((record->flags & GUC_SUPERUSER_ONLY) && !superuser())
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 errmsg("must be superuser to examine \"%s\"", name)));
6763

6764 6765 6766
	if (varname)
		*varname = record->name;

6767
	return _ShowOption(record, true);
6768 6769 6770
}

/*
6771 6772
 * Return GUC variable value by variable number; optionally return canonical
 * form of name.  Return value is palloc'd.
6773
 */
6774 6775
void
GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
6776
{
B
Bruce Momjian 已提交
6777 6778
	char		buffer[256];
	struct config_generic *conf;
6779 6780 6781 6782 6783

	/* check requested variable number valid */
	Assert((varnum >= 0) && (varnum < num_guc_variables));

	conf = guc_variables[varnum];
6784

6785
	if (noshow)
6786 6787 6788 6789 6790 6791 6792
	{
		if ((conf->flags & GUC_NO_SHOW_ALL) ||
			((conf->flags & GUC_SUPERUSER_ONLY) && !superuser()))
			*noshow = true;
		else
			*noshow = false;
	}
6793

6794 6795 6796 6797 6798 6799
	/* first get the generic attributes */

	/* name */
	values[0] = conf->name;

	/* setting : use _ShowOption in order to avoid duplicating the logic */
6800 6801 6802 6803 6804
	values[1] = _ShowOption(conf, false);

	/* unit */
	if (conf->vartype == PGC_INT)
	{
6805
		static char buf[8];
6806

6807 6808 6809 6810 6811 6812
		switch (conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
		{
			case GUC_UNIT_KB:
				values[2] = "kB";
				break;
			case GUC_UNIT_BLOCKS:
B
Bruce Momjian 已提交
6813
				snprintf(buf, sizeof(buf), "%dkB", BLCKSZ / 1024);
6814 6815 6816
				values[2] = buf;
				break;
			case GUC_UNIT_XBLOCKS:
B
Bruce Momjian 已提交
6817
				snprintf(buf, sizeof(buf), "%dkB", XLOG_BLCKSZ / 1024);
6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831
				values[2] = buf;
				break;
			case GUC_UNIT_MS:
				values[2] = "ms";
				break;
			case GUC_UNIT_S:
				values[2] = "s";
				break;
			case GUC_UNIT_MIN:
				values[2] = "min";
				break;
			default:
				values[2] = "";
				break;
6832 6833 6834 6835
		}
	}
	else
		values[2] = NULL;
6836

6837
	/* group */
6838
	values[3] = config_group_names[conf->group];
6839 6840

	/* short_desc */
6841
	values[4] = conf->short_desc;
6842 6843

	/* extra_desc */
6844
	values[5] = conf->long_desc;
6845

6846
	/* context */
6847
	values[6] = GucContext_Names[conf->context];
6848 6849

	/* vartype */
6850
	values[7] = config_type_names[conf->vartype];
6851 6852

	/* source */
6853
	values[8] = GucSource_Names[conf->source];
6854 6855 6856 6857 6858 6859

	/* now get the type specifc attributes */
	switch (conf->vartype)
	{
		case PGC_BOOL:
			{
6860 6861
				struct config_bool *lconf = (struct config_bool *) conf;

6862
				/* min_val */
6863
				values[9] = NULL;
6864 6865

				/* max_val */
6866
				values[10] = NULL;
6867 6868 6869

				/* enumvals */
				values[11] = NULL;
6870 6871 6872 6873 6874 6875

				/* boot_val */
				values[12] = pstrdup(lconf->boot_val ? "on" : "off");

				/* reset_val */
				values[13] = pstrdup(lconf->reset_val ? "on" : "off");
6876 6877 6878 6879 6880 6881 6882 6883 6884
			}
			break;

		case PGC_INT:
			{
				struct config_int *lconf = (struct config_int *) conf;

				/* min_val */
				snprintf(buffer, sizeof(buffer), "%d", lconf->min);
6885
				values[9] = pstrdup(buffer);
6886 6887 6888

				/* max_val */
				snprintf(buffer, sizeof(buffer), "%d", lconf->max);
6889
				values[10] = pstrdup(buffer);
6890 6891 6892

				/* enumvals */
				values[11] = NULL;
6893

6894 6895 6896
				/* boot_val */
				snprintf(buffer, sizeof(buffer), "%d", lconf->boot_val);
				values[12] = pstrdup(buffer);
6897

6898 6899 6900
				/* reset_val */
				snprintf(buffer, sizeof(buffer), "%d", lconf->reset_val);
				values[13] = pstrdup(buffer);
6901 6902 6903 6904 6905 6906 6907 6908 6909
			}
			break;

		case PGC_REAL:
			{
				struct config_real *lconf = (struct config_real *) conf;

				/* min_val */
				snprintf(buffer, sizeof(buffer), "%g", lconf->min);
6910
				values[9] = pstrdup(buffer);
6911 6912 6913

				/* max_val */
				snprintf(buffer, sizeof(buffer), "%g", lconf->max);
6914
				values[10] = pstrdup(buffer);
6915 6916 6917

				/* enumvals */
				values[11] = NULL;
6918

6919 6920 6921
				/* boot_val */
				snprintf(buffer, sizeof(buffer), "%g", lconf->boot_val);
				values[12] = pstrdup(buffer);
6922

6923 6924 6925
				/* reset_val */
				snprintf(buffer, sizeof(buffer), "%g", lconf->reset_val);
				values[13] = pstrdup(buffer);
6926 6927 6928 6929 6930
			}
			break;

		case PGC_STRING:
			{
6931
				struct config_string *lconf = (struct config_string *) conf;
6932

6933
				/* min_val */
6934
				values[9] = NULL;
6935 6936

				/* max_val */
6937
				values[10] = NULL;
6938 6939 6940

				/* enumvals */
				values[11] = NULL;
6941

6942 6943 6944
				/* boot_val */
				if (lconf->boot_val == NULL)
					values[12] = NULL;
6945 6946 6947
				else
					values[12] = pstrdup(lconf->boot_val);

6948 6949 6950
				/* reset_val */
				if (lconf->reset_val == NULL)
					values[13] = NULL;
6951 6952
				else
					values[13] = pstrdup(lconf->reset_val);
6953 6954 6955 6956 6957
			}
			break;

		case PGC_ENUM:
			{
6958
				struct config_enum *lconf = (struct config_enum *) conf;
6959

6960 6961 6962 6963 6964 6965 6966
				/* min_val */
				values[9] = NULL;

				/* max_val */
				values[10] = NULL;

				/* enumvals */
6967 6968 6969 6970 6971

				/*
				 * NOTE! enumvals with double quotes in them are not
				 * supported!
				 */
A
Alvaro Herrera 已提交
6972 6973
				values[11] = config_enum_get_options((struct config_enum *) conf,
													 "{\"", "\"}", "\",\"");
6974

6975
				/* boot_val */
A
Alvaro Herrera 已提交
6976
				values[12] = pstrdup(config_enum_lookup_by_value(lconf,
6977
														   lconf->boot_val));
6978

6979
				/* reset_val */
A
Alvaro Herrera 已提交
6980
				values[13] = pstrdup(config_enum_lookup_by_value(lconf,
6981
														  lconf->reset_val));
6982 6983 6984 6985 6986 6987
			}
			break;

		default:
			{
				/*
B
Bruce Momjian 已提交
6988
				 * should never get here, but in case we do, set 'em to NULL
6989 6990 6991
				 */

				/* min_val */
6992
				values[9] = NULL;
6993 6994

				/* max_val */
6995
				values[10] = NULL;
6996 6997 6998

				/* enumvals */
				values[11] = NULL;
6999

7000 7001
				/* boot_val */
				values[12] = NULL;
7002

7003 7004
				/* reset_val */
				values[13] = NULL;
7005 7006 7007
			}
			break;
	}
7008

7009 7010 7011
	/*
	 * If the setting came from a config file, set the source location. For
	 * security reasons, we don't show source file/line number for
7012 7013 7014
	 * non-superusers.
	 */
	if (conf->source == PGC_S_FILE && superuser())
7015
	{
7016
		values[14] = conf->sourcefile;
7017
		snprintf(buffer, sizeof(buffer), "%d", conf->sourceline);
7018
		values[15] = pstrdup(buffer);
7019 7020 7021
	}
	else
	{
7022 7023
		values[14] = NULL;
		values[15] = NULL;
7024
	}
7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042
}

/*
 * Return the total number of GUC variables
 */
int
GetNumConfigOptions(void)
{
	return num_guc_variables;
}

/*
 * show_config_by_name - equiv to SHOW X command but implemented as
 * a function.
 */
Datum
show_config_by_name(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
7043 7044
	char	   *varname;
	char	   *varval;
7045 7046

	/* Get the GUC variable name */
7047
	varname = TextDatumGetCString(PG_GETARG_DATUM(0));
7048 7049

	/* Get the value */
7050
	varval = GetConfigOptionByName(varname, NULL);
7051 7052

	/* Convert to text */
7053
	PG_RETURN_TEXT_P(cstring_to_text(varval));
7054 7055
}

7056 7057 7058 7059
/*
 * show_all_settings - equiv to SHOW ALL command but implemented as
 * a Table Function.
 */
7060
#define NUM_PG_SETTINGS_ATTS	16
7061

7062 7063 7064
Datum
show_all_settings(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
7065 7066 7067 7068 7069 7070
	FuncCallContext *funcctx;
	TupleDesc	tupdesc;
	int			call_cntr;
	int			max_calls;
	AttInMetadata *attinmeta;
	MemoryContext oldcontext;
7071 7072

	/* stuff done only on the first call of the function */
B
Bruce Momjian 已提交
7073 7074
	if (SRF_IS_FIRSTCALL())
	{
7075
		/* create a function context for cross-call persistence */
B
Bruce Momjian 已提交
7076
		funcctx = SRF_FIRSTCALL_INIT();
7077

B
Bruce Momjian 已提交
7078
		/*
B
Bruce Momjian 已提交
7079
		 * switch to memory context appropriate for multiple function calls
B
Bruce Momjian 已提交
7080
		 */
7081 7082
		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

7083
		/*
B
Bruce Momjian 已提交
7084 7085
		 * need a tuple descriptor representing NUM_PG_SETTINGS_ATTS columns
		 * of the appropriate types
7086 7087
		 */
		tupdesc = CreateTemplateTupleDesc(NUM_PG_SETTINGS_ATTS, false);
7088
		TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
7089
						   TEXTOID, -1, 0);
7090
		TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
7091
						   TEXTOID, -1, 0);
7092 7093 7094
		TupleDescInitEntry(tupdesc, (AttrNumber) 3, "unit",
						   TEXTOID, -1, 0);
		TupleDescInitEntry(tupdesc, (AttrNumber) 4, "category",
7095
						   TEXTOID, -1, 0);
7096
		TupleDescInitEntry(tupdesc, (AttrNumber) 5, "short_desc",
7097
						   TEXTOID, -1, 0);
7098
		TupleDescInitEntry(tupdesc, (AttrNumber) 6, "extra_desc",
7099
						   TEXTOID, -1, 0);
7100
		TupleDescInitEntry(tupdesc, (AttrNumber) 7, "context",
7101
						   TEXTOID, -1, 0);
7102
		TupleDescInitEntry(tupdesc, (AttrNumber) 8, "vartype",
7103
						   TEXTOID, -1, 0);
7104
		TupleDescInitEntry(tupdesc, (AttrNumber) 9, "source",
7105
						   TEXTOID, -1, 0);
7106
		TupleDescInitEntry(tupdesc, (AttrNumber) 10, "min_val",
7107
						   TEXTOID, -1, 0);
7108
		TupleDescInitEntry(tupdesc, (AttrNumber) 11, "max_val",
7109
						   TEXTOID, -1, 0);
7110
		TupleDescInitEntry(tupdesc, (AttrNumber) 12, "enumvals",
7111
						   TEXTARRAYOID, -1, 0);
7112 7113 7114 7115
		TupleDescInitEntry(tupdesc, (AttrNumber) 13, "boot_val",
						   TEXTOID, -1, 0);
		TupleDescInitEntry(tupdesc, (AttrNumber) 14, "reset_val",
						   TEXTOID, -1, 0);
7116
		TupleDescInitEntry(tupdesc, (AttrNumber) 15, "sourcefile",
7117
						   TEXTOID, -1, 0);
7118
		TupleDescInitEntry(tupdesc, (AttrNumber) 16, "sourceline",
7119
						   INT4OID, -1, 0);
7120 7121

		/*
B
Bruce Momjian 已提交
7122 7123
		 * Generate attribute metadata needed later to produce tuples from raw
		 * C strings
7124 7125 7126 7127 7128 7129
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

		/* total number of tuples to be returned */
		funcctx->max_calls = GetNumConfigOptions();
7130 7131

		MemoryContextSwitchTo(oldcontext);
B
Bruce Momjian 已提交
7132
	}
7133 7134

	/* stuff done on every call of the function */
B
Bruce Momjian 已提交
7135
	funcctx = SRF_PERCALL_SETUP();
7136 7137 7138 7139 7140

	call_cntr = funcctx->call_cntr;
	max_calls = funcctx->max_calls;
	attinmeta = funcctx->attinmeta;

B
Bruce Momjian 已提交
7141 7142
	if (call_cntr < max_calls)	/* do when there is more left to send */
	{
7143
		char	   *values[NUM_PG_SETTINGS_ATTS];
7144 7145 7146 7147 7148 7149 7150 7151 7152
		bool		noshow;
		HeapTuple	tuple;
		Datum		result;

		/*
		 * Get the next visible GUC variable name and value
		 */
		do
		{
7153
			GetConfigOptionByNum(call_cntr, (const char **) values, &noshow);
7154 7155 7156 7157 7158 7159 7160
			if (noshow)
			{
				/* bump the counter and get the next config setting */
				call_cntr = ++funcctx->call_cntr;

				/* make sure we haven't gone too far now */
				if (call_cntr >= max_calls)
B
Bruce Momjian 已提交
7161
					SRF_RETURN_DONE(funcctx);
7162 7163 7164 7165 7166 7167 7168
			}
		} while (noshow);

		/* build a tuple */
		tuple = BuildTupleFromCStrings(attinmeta, values);

		/* make the tuple into a datum */
7169
		result = HeapTupleGetDatum(tuple);
7170

B
Bruce Momjian 已提交
7171 7172 7173
		SRF_RETURN_NEXT(funcctx, result);
	}
	else
7174 7175
	{
		/* do when there is no more left */
B
Bruce Momjian 已提交
7176
		SRF_RETURN_DONE(funcctx);
7177
	}
7178 7179
}

7180
static char *
7181
_ShowOption(struct config_generic * record, bool use_units)
7182 7183 7184
{
	char		buffer[256];
	const char *val;
7185

7186 7187 7188 7189 7190
	switch (record->vartype)
	{
		case PGC_BOOL:
			{
				struct config_bool *conf = (struct config_bool *) record;
7191

7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206
				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else
					val = *conf->variable ? "on" : "off";
			}
			break;

		case PGC_INT:
			{
				struct config_int *conf = (struct config_int *) record;

				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else
				{
7207 7208
					/*
					 * Use int64 arithmetic to avoid overflows in units
7209
					 * conversion.
7210 7211 7212
					 */
					int64		result = *conf->variable;
					const char *unit;
7213

7214 7215
					if (use_units && result > 0 &&
						(record->flags & GUC_UNIT_MEMORY))
7216
					{
7217 7218 7219
						switch (record->flags & GUC_UNIT_MEMORY)
						{
							case GUC_UNIT_BLOCKS:
B
Bruce Momjian 已提交
7220
								result *= BLCKSZ / 1024;
7221 7222
								break;
							case GUC_UNIT_XBLOCKS:
B
Bruce Momjian 已提交
7223
								result *= XLOG_BLCKSZ / 1024;
7224 7225
								break;
						}
7226 7227 7228 7229

						if (result % KB_PER_GB == 0)
						{
							result /= KB_PER_GB;
7230
							unit = "GB";
7231 7232 7233 7234
						}
						else if (result % KB_PER_MB == 0)
						{
							result /= KB_PER_MB;
7235
							unit = "MB";
7236 7237 7238
						}
						else
						{
7239
							unit = "kB";
7240 7241
						}
					}
7242 7243
					else if (use_units && result > 0 &&
							 (record->flags & GUC_UNIT_TIME))
7244
					{
7245 7246 7247 7248 7249 7250 7251 7252 7253
						switch (record->flags & GUC_UNIT_TIME)
						{
							case GUC_UNIT_S:
								result *= MS_PER_S;
								break;
							case GUC_UNIT_MIN:
								result *= MS_PER_MIN;
								break;
						}
7254 7255 7256 7257

						if (result % MS_PER_D == 0)
						{
							result /= MS_PER_D;
7258
							unit = "d";
7259 7260 7261 7262
						}
						else if (result % MS_PER_H == 0)
						{
							result /= MS_PER_H;
7263
							unit = "h";
7264 7265 7266 7267
						}
						else if (result % MS_PER_MIN == 0)
						{
							result /= MS_PER_MIN;
7268
							unit = "min";
7269 7270 7271 7272
						}
						else if (result % MS_PER_S == 0)
						{
							result /= MS_PER_S;
7273
							unit = "s";
7274 7275 7276
						}
						else
						{
7277
							unit = "ms";
7278 7279 7280
						}
					}
					else
7281
						unit = "";
7282

7283 7284
					snprintf(buffer, sizeof(buffer), INT64_FORMAT "%s",
							 result, unit);
7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307
					val = buffer;
				}
			}
			break;

		case PGC_REAL:
			{
				struct config_real *conf = (struct config_real *) record;

				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else
				{
					snprintf(buffer, sizeof(buffer), "%g",
							 *conf->variable);
					val = buffer;
				}
			}
			break;

		case PGC_STRING:
			{
				struct config_string *conf = (struct config_string *) record;
7308

7309 7310 7311 7312 7313
				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else if (*conf->variable && **conf->variable)
					val = *conf->variable;
				else
7314
					val = "";
7315 7316
			}
			break;
7317

7318 7319 7320 7321
		case PGC_ENUM:
			{
				struct config_enum *conf = (struct config_enum *) record;

7322
				if (conf->show_hook)
7323 7324
					val = (*conf->show_hook) ();
				else
7325
					val = config_enum_lookup_by_value(conf, *conf->variable);
7326 7327 7328
			}
			break;

7329 7330 7331 7332 7333
		default:
			/* just to keep compiler quiet */
			val = "???";
			break;
	}
7334

7335
	return pstrdup(val);
7336
}
7337 7338


B
Bruce Momjian 已提交
7339
#ifdef EXEC_BACKEND
7340

B
Bruce Momjian 已提交
7341
/*
7342
 *	These routines dump out all non-default GUC options into a binary
B
Bruce Momjian 已提交
7343 7344 7345 7346 7347 7348
 *	file that is read by all exec'ed backends.  The format is:
 *
 *		variable name, string, null terminated
 *		variable value, string, null terminated
 *		variable source, integer
 */
7349
static void
7350
write_one_nondefault_variable(FILE *fp, struct config_generic * gconf)
7351 7352 7353 7354 7355 7356 7357 7358 7359 7360
{
	if (gconf->source == PGC_S_DEFAULT)
		return;

	fprintf(fp, "%s", gconf->name);
	fputc(0, fp);

	switch (gconf->vartype)
	{
		case PGC_BOOL:
7361 7362
			{
				struct config_bool *conf = (struct config_bool *) gconf;
7363

7364 7365 7366 7367 7368 7369
				if (*conf->variable)
					fprintf(fp, "true");
				else
					fprintf(fp, "false");
			}
			break;
7370 7371

		case PGC_INT:
7372 7373
			{
				struct config_int *conf = (struct config_int *) gconf;
7374

7375 7376 7377
				fprintf(fp, "%d", *conf->variable);
			}
			break;
7378 7379

		case PGC_REAL:
7380 7381
			{
				struct config_real *conf = (struct config_real *) gconf;
7382

7383 7384 7385 7386
				/* Could lose precision here? */
				fprintf(fp, "%f", *conf->variable);
			}
			break;
7387 7388

		case PGC_STRING:
7389 7390
			{
				struct config_string *conf = (struct config_string *) gconf;
7391

7392 7393 7394
				fprintf(fp, "%s", *conf->variable);
			}
			break;
7395 7396

		case PGC_ENUM:
7397 7398 7399 7400 7401 7402 7403
			{
				struct config_enum *conf = (struct config_enum *) gconf;

				fprintf(fp, "%s",
						config_enum_lookup_by_value(conf, *conf->variable));
			}
			break;
7404 7405 7406 7407 7408 7409 7410
	}

	fputc(0, fp);

	fwrite(&gconf->source, sizeof(gconf->source), 1, fp);
}

B
Bruce Momjian 已提交
7411 7412 7413
void
write_nondefault_variables(GucContext context)
{
7414 7415
	int			elevel;
	FILE	   *fp;
7416 7417
	struct config_generic *cvc_conf;
	int			i;
B
Bruce Momjian 已提交
7418 7419

	Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
7420 7421

	elevel = (context == PGC_SIGHUP) ? LOG : ERROR;
B
Bruce Momjian 已提交
7422 7423 7424 7425

	/*
	 * Open file
	 */
7426
	fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, "w");
7427 7428
	if (!fp)
	{
7429 7430
		ereport(elevel,
				(errcode_for_file_access(),
7431 7432
				 errmsg("could not write to file \"%s\": %m",
						CONFIG_EXEC_PARAMS_NEW)));
B
Bruce Momjian 已提交
7433 7434 7435
		return;
	}

7436 7437 7438 7439 7440 7441 7442 7443
	/*
	 * custom_variable_classes must be written out first; otherwise we might
	 * reject custom variable values while reading the file.
	 */
	cvc_conf = find_option("custom_variable_classes", false, ERROR);
	if (cvc_conf)
		write_one_nondefault_variable(fp, cvc_conf);

B
Bruce Momjian 已提交
7444 7445 7446 7447
	for (i = 0; i < num_guc_variables; i++)
	{
		struct config_generic *gconf = guc_variables[i];

7448 7449
		if (gconf != cvc_conf)
			write_one_nondefault_variable(fp, gconf);
B
Bruce Momjian 已提交
7450 7451
	}

7452 7453 7454 7455
	if (FreeFile(fp))
	{
		ereport(elevel,
				(errcode_for_file_access(),
7456 7457
				 errmsg("could not write to file \"%s\": %m",
						CONFIG_EXEC_PARAMS_NEW)));
7458 7459 7460
		return;
	}

7461
	/*
B
Bruce Momjian 已提交
7462 7463
	 * Put new file in place.  This could delay on Win32, but we don't hold
	 * any exclusive locks.
7464
	 */
7465
	rename(CONFIG_EXEC_PARAMS_NEW, CONFIG_EXEC_PARAMS);
B
Bruce Momjian 已提交
7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476
}


/*
 *	Read string, including null byte from file
 *
 *	Return NULL on EOF and nothing read
 */
static char *
read_string_with_null(FILE *fp)
{
7477 7478 7479 7480
	int			i = 0,
				ch,
				maxlen = 256;
	char	   *str = NULL;
B
Bruce Momjian 已提交
7481 7482 7483 7484 7485 7486 7487 7488

	do
	{
		if ((ch = fgetc(fp)) == EOF)
		{
			if (i == 0)
				return NULL;
			else
7489
				elog(FATAL, "invalid format of exec config params file");
B
Bruce Momjian 已提交
7490 7491
		}
		if (i == 0)
7492
			str = guc_malloc(FATAL, maxlen);
B
Bruce Momjian 已提交
7493
		else if (i == maxlen)
7494
			str = guc_realloc(FATAL, str, maxlen *= 2);
B
Bruce Momjian 已提交
7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508
		str[i++] = ch;
	} while (ch != 0);

	return str;
}


/*
 *	This routine loads a previous postmaster dump of its non-default
 *	settings.
 */
void
read_nondefault_variables(void)
{
7509 7510 7511 7512
	FILE	   *fp;
	char	   *varname,
			   *varvalue;
	int			varsource;
B
Bruce Momjian 已提交
7513 7514 7515 7516

	/*
	 * Open file
	 */
7517
	fp = AllocateFile(CONFIG_EXEC_PARAMS, "r");
7518 7519 7520 7521
	if (!fp)
	{
		/* File not found is fine */
		if (errno != ENOENT)
7522 7523
			ereport(FATAL,
					(errcode_for_file_access(),
7524 7525
					 errmsg("could not read from file \"%s\": %m",
							CONFIG_EXEC_PARAMS)));
B
Bruce Momjian 已提交
7526
		return;
7527
	}
B
Bruce Momjian 已提交
7528

7529
	for (;;)
B
Bruce Momjian 已提交
7530
	{
7531 7532
		struct config_generic *record;

B
Bruce Momjian 已提交
7533 7534 7535
		if ((varname = read_string_with_null(fp)) == NULL)
			break;

7536
		if ((record = find_option(varname, true, FATAL)) == NULL)
B
Bruce Momjian 已提交
7537
			elog(FATAL, "failed to locate variable %s in exec config params file", varname);
B
Bruce Momjian 已提交
7538
		if ((varvalue = read_string_with_null(fp)) == NULL)
7539
			elog(FATAL, "invalid format of exec config params file");
7540
		if (fread(&varsource, sizeof(varsource), 1, fp) == 0)
7541
			elog(FATAL, "invalid format of exec config params file");
B
Bruce Momjian 已提交
7542

7543
		(void) set_config_option(varname, varvalue, record->context,
7544
								 varsource, GUC_ACTION_SET, true);
B
Bruce Momjian 已提交
7545 7546 7547 7548 7549 7550
		free(varname);
		free(varvalue);
	}

	FreeFile(fp);
}
B
Bruce Momjian 已提交
7551
#endif   /* EXEC_BACKEND */
B
Bruce Momjian 已提交
7552 7553


7554 7555 7556 7557 7558 7559 7560 7561
/*
 * A little "long argument" simulation, although not quite GNU
 * compliant. Takes a string of the form "some-option=some value" and
 * returns name = "some_option" and value = "some value" in malloc'ed
 * storage. Note that '-' is converted to '_' in the option name. If
 * there is no '=' in the input string then value will be NULL.
 */
void
B
Bruce Momjian 已提交
7562
ParseLongOption(const char *string, char **name, char **value)
7563
{
B
Bruce Momjian 已提交
7564 7565
	size_t		equal_pos;
	char	   *cp;
7566 7567 7568 7569 7570 7571 7572 7573 7574

	AssertArg(string);
	AssertArg(name);
	AssertArg(value);

	equal_pos = strcspn(string, "=");

	if (string[equal_pos] == '=')
	{
7575
		*name = guc_malloc(FATAL, equal_pos + 1);
7576
		strlcpy(*name, string, equal_pos + 1);
7577

7578
		*value = guc_strdup(FATAL, &string[equal_pos + 1]);
7579
	}
B
Bruce Momjian 已提交
7580
	else
7581
	{
7582
		/* no equal sign in string */
7583
		*name = guc_strdup(FATAL, string);
7584 7585 7586
		*value = NULL;
	}

B
Bruce Momjian 已提交
7587
	for (cp = *name; *cp; cp++)
7588 7589 7590
		if (*cp == '-')
			*cp = '_';
}
7591 7592


7593
/*
7594
 * Handle options fetched from pg_db_role_setting.setconfig,
B
Bruce Momjian 已提交
7595
 * pg_proc.proconfig, etc.	Caller must specify proper context/source/action.
7596
 *
7597
 * The array parameter must be an array of TEXT (it must not be NULL).
7598
 */
7599
void
7600
ProcessGUCArray(ArrayType *array,
7601
				GucContext context, GucSource source, GucAction action)
7602
{
B
Bruce Momjian 已提交
7603
	int			i;
7604

7605
	Assert(array != NULL);
7606 7607 7608
	Assert(ARR_ELEMTYPE(array) == TEXTOID);
	Assert(ARR_NDIM(array) == 1);
	Assert(ARR_LBOUND(array)[0] == 1);
7609 7610 7611 7612 7613 7614 7615 7616 7617 7618

	for (i = 1; i <= ARR_DIMS(array)[0]; i++)
	{
		Datum		d;
		bool		isnull;
		char	   *s;
		char	   *name;
		char	   *value;

		d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
7619 7620 7621 7622
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
7623 7624 7625 7626 7627
					  &isnull);

		if (isnull)
			continue;

7628
		s = TextDatumGetCString(d);
7629

7630 7631 7632
		ParseLongOption(s, &name, &value);
		if (!value)
		{
7633 7634
			ereport(WARNING,
					(errcode(ERRCODE_SYNTAX_ERROR),
7635 7636
					 errmsg("could not parse setting for parameter \"%s\"",
							name)));
7637
			free(name);
B
Bruce Momjian 已提交
7638
			continue;
7639 7640
		}

7641
		(void) set_config_option(name, value, context, source, action, true);
7642 7643 7644 7645

		free(name);
		if (value)
			free(value);
7646
		pfree(s);
7647 7648 7649 7650
	}
}


7651 7652 7653 7654
/*
 * Add an entry to an option array.  The array parameter may be NULL
 * to indicate the current table entry is NULL.
 */
7655 7656 7657
ArrayType *
GUCArrayAdd(ArrayType *array, const char *name, const char *value)
{
7658
	struct config_generic *record;
7659 7660 7661 7662 7663 7664 7665
	Datum		datum;
	char	   *newval;
	ArrayType  *a;

	Assert(name);
	Assert(value);

7666 7667
	/* test if the option is valid and we're allowed to set it */
	(void) validate_option_array_item(name, value, false);
7668

7669 7670 7671 7672
	/* normalize name (converts obsolete GUC names to modern spellings) */
	record = find_option(name, false, WARNING);
	if (record)
		name = record->name;
7673

7674
	/* build new item for array */
7675 7676
	newval = palloc(strlen(name) + 1 + strlen(value) + 1);
	sprintf(newval, "%s=%s", name, value);
7677
	datum = CStringGetTextDatum(newval);
B
Bruce Momjian 已提交
7678

7679 7680
	if (array)
	{
B
Bruce Momjian 已提交
7681 7682 7683
		int			index;
		bool		isnull;
		int			i;
7684

7685 7686 7687 7688
		Assert(ARR_ELEMTYPE(array) == TEXTOID);
		Assert(ARR_NDIM(array) == 1);
		Assert(ARR_LBOUND(array)[0] == 1);

B
Bruce Momjian 已提交
7689
		index = ARR_DIMS(array)[0] + 1; /* add after end */
7690 7691 7692 7693 7694 7695 7696

		for (i = 1; i <= ARR_DIMS(array)[0]; i++)
		{
			Datum		d;
			char	   *current;

			d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
7697 7698 7699 7700
						  -1 /* varlenarray */ ,
						  -1 /* TEXT's typlen */ ,
						  false /* TEXT's typbyval */ ,
						  'i' /* TEXT's typalign */ ,
7701
						  &isnull);
7702 7703
			if (isnull)
				continue;
7704
			current = TextDatumGetCString(d);
7705 7706

			/* check for match up through and including '=' */
B
Bruce Momjian 已提交
7707
			if (strncmp(current, newval, strlen(name) + 1) == 0)
7708 7709 7710 7711 7712 7713
			{
				index = i;
				break;
			}
		}

7714 7715
		a = array_set(array, 1, &index,
					  datum,
7716 7717
					  false,
					  -1 /* varlena array */ ,
B
Bruce Momjian 已提交
7718 7719
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
7720
					  'i' /* TEXT's typalign */ );
7721 7722
	}
	else
7723 7724 7725
		a = construct_array(&datum, 1,
							TEXTOID,
							-1, false, 'i');
7726 7727 7728 7729 7730

	return a;
}


7731 7732 7733 7734 7735
/*
 * Delete an entry from an option array.  The array parameter may be NULL
 * to indicate the current table entry is NULL.  Also, if the return value
 * is NULL then a null should be stored.
 */
7736 7737 7738
ArrayType *
GUCArrayDelete(ArrayType *array, const char *name)
{
7739
	struct config_generic *record;
B
Bruce Momjian 已提交
7740 7741 7742
	ArrayType  *newarray;
	int			i;
	int			index;
7743 7744 7745

	Assert(name);

7746 7747
	/* test if the option is valid and we're allowed to set it */
	(void) validate_option_array_item(name, NULL, false);
7748

7749 7750 7751 7752
	/* normalize name (converts obsolete GUC names to modern spellings) */
	record = find_option(name, false, WARNING);
	if (record)
		name = record->name;
7753

7754 7755 7756 7757 7758
	/* if array is currently null, then surely nothing to delete */
	if (!array)
		return NULL;

	newarray = NULL;
7759 7760 7761 7762 7763 7764 7765 7766 7767
	index = 1;

	for (i = 1; i <= ARR_DIMS(array)[0]; i++)
	{
		Datum		d;
		char	   *val;
		bool		isnull;

		d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
7768 7769 7770 7771
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
7772
					  &isnull);
7773 7774
		if (isnull)
			continue;
7775
		val = TextDatumGetCString(d);
7776

7777
		/* ignore entry if it's what we want to delete */
B
Bruce Momjian 已提交
7778
		if (strncmp(val, name, strlen(name)) == 0
7779 7780 7781
			&& val[strlen(name)] == '=')
			continue;

7782 7783 7784 7785
		/* else add it to the output array */
		if (newarray)
			newarray = array_set(newarray, 1, &index,
								 d,
7786
								 false,
7787 7788 7789
								 -1 /* varlenarray */ ,
								 -1 /* TEXT's typlen */ ,
								 false /* TEXT's typbyval */ ,
7790
								 'i' /* TEXT's typalign */ );
7791 7792 7793 7794 7795
		else
			newarray = construct_array(&d, 1,
									   TEXTOID,
									   -1, false, 'i');

7796 7797 7798 7799 7800
		index++;
	}

	return newarray;
}
7801

7802

7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818
/*
 * Given a GUC array, delete all settings from it that our permission
 * level allows: if superuser, delete them all; if regular user, only
 * those that are PGC_USERSET
 */
ArrayType *
GUCArrayReset(ArrayType *array)
{
	ArrayType  *newarray;
	int			i;
	int			index;

	/* if array is currently null, nothing to do */
	if (!array)
		return NULL;

7819
	/* if we're superuser, we can delete everything, so just do it */
7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845
	if (superuser())
		return NULL;

	newarray = NULL;
	index = 1;

	for (i = 1; i <= ARR_DIMS(array)[0]; i++)
	{
		Datum		d;
		char	   *val;
		char	   *eqsgn;
		bool		isnull;

		d = array_ref(array, 1, &i,
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
					  &isnull);
		if (isnull)
			continue;
		val = TextDatumGetCString(d);

		eqsgn = strchr(val, '=');
		*eqsgn = '\0';

7846 7847
		/* skip if we have permission to delete it */
		if (validate_option_array_item(val, NULL, true))
7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870
			continue;

		/* else add it to the output array */
		if (newarray)
			newarray = array_set(newarray, 1, &index,
								 d,
								 false,
								 -1 /* varlenarray */ ,
								 -1 /* TEXT's typlen */ ,
								 false /* TEXT's typbyval */ ,
								 'i' /* TEXT's typalign */ );
		else
			newarray = construct_array(&d, 1,
									   TEXTOID,
									   -1, false, 'i');

		index++;
		pfree(val);
	}

	return newarray;
}

7871 7872 7873 7874
/*
 * Validate a proposed option setting for GUCArrayAdd/Delete/Reset.
 *
 * name is the option name.  value is the proposed value for the Add case,
B
Bruce Momjian 已提交
7875
 * or NULL for the Delete/Reset cases.	If skipIfNoPermissions is true, it's
7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896
 * not an error to have no permissions to set the option.
 *
 * Returns TRUE if OK, FALSE if skipIfNoPermissions is true and user does not
 * have permission to change this option (all other error cases result in an
 * error being thrown).
 */
static bool
validate_option_array_item(const char *name, const char *value,
						   bool skipIfNoPermissions)

{
	struct config_generic *gconf;

	/*
	 * There are three cases to consider:
	 *
	 * name is a known GUC variable.  Check the value normally, check
	 * permissions normally (ie, allow if variable is USERSET, or if it's
	 * SUSET and user is superuser).
	 *
	 * name is not known, but exists or can be created as a placeholder
B
Bruce Momjian 已提交
7897 7898 7899 7900 7901
	 * (implying it has a prefix listed in custom_variable_classes). We allow
	 * this case if you're a superuser, otherwise not.  Superusers are assumed
	 * to know what they're doing.  We can't allow it for other users, because
	 * when the placeholder is resolved it might turn out to be a SUSET
	 * variable; define_custom_variable assumes we checked that.
7902 7903
	 *
	 * name is not known and can't be created as a placeholder.  Throw error,
B
Bruce Momjian 已提交
7904 7905 7906 7907 7908 7909
	 * unless skipIfNoPermissions is true, in which case return FALSE. (It's
	 * tempting to allow this case to superusers, if the name is qualified but
	 * not listed in custom_variable_classes.  That would ease restoring of
	 * dumps containing ALTER ROLE/DATABASE SET.  However, it's not clear that
	 * this usage justifies such a loss of error checking. You can always fix
	 * custom_variable_classes before you restore.)
7910 7911 7912 7913 7914 7915 7916 7917 7918
	 */
	gconf = find_option(name, true, WARNING);
	if (!gconf)
	{
		/* not known, failed to make a placeholder */
		if (skipIfNoPermissions)
			return false;
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
B
Bruce Momjian 已提交
7919
			   errmsg("unrecognized configuration parameter \"%s\"", name)));
7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938
	}

	if (gconf->flags & GUC_CUSTOM_PLACEHOLDER)
	{
		/*
		 * We cannot do any meaningful check on the value, so only permissions
		 * are useful to check.
		 */
		if (superuser())
			return true;
		if (skipIfNoPermissions)
			return false;
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 errmsg("permission denied to set parameter \"%s\"", name)));
	}

	/* manual permissions check so we can avoid an error being thrown */
	if (gconf->context == PGC_USERSET)
B
Bruce Momjian 已提交
7939
		 /* ok */ ;
7940
	else if (gconf->context == PGC_SUSET && superuser())
B
Bruce Momjian 已提交
7941
		 /* ok */ ;
7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953
	else if (skipIfNoPermissions)
		return false;
	/* if a permissions error should be thrown, let set_config_option do it */

	/* test for permissions and valid option value */
	set_config_option(name, value,
					  superuser() ? PGC_SUSET : PGC_USERSET,
					  PGC_S_TEST, GUC_ACTION_SET, false);

	return true;
}

7954 7955

/*
7956 7957 7958 7959
 * Called by check_hooks that want to override the normal
 * ERRCODE_INVALID_PARAMETER_VALUE SQLSTATE for check hook failures.
 *
 * Note that GUC_check_errmsg() etc are just macros that result in a direct
7960
 * assignment to the associated variables.	That is ugly, but forced by the
7961
 * limitations of C's macro mechanisms.
7962
 */
7963 7964 7965 7966 7967
void
GUC_check_errcode(int sqlerrcode)
{
	GUC_check_errcode_value = sqlerrcode;
}
7968

7969 7970 7971 7972 7973 7974 7975 7976

/*
 * Convenience functions to manage calling a variable's check_hook.
 * These mostly take care of the protocol for letting check hooks supply
 * portions of the error report on failure.
 */

static bool
7977
call_bool_check_hook(struct config_bool * conf, bool *newval, void **extra,
7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994
					 GucSource source, int elevel)
{
	/* Quick success if no hook */
	if (!conf->check_hook)
		return true;

	/* Reset variables that might be set by hook */
	GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
	GUC_check_errmsg_string = NULL;
	GUC_check_errdetail_string = NULL;
	GUC_check_errhint_string = NULL;

	if (!(*conf->check_hook) (newval, extra, source))
	{
		ereport(elevel,
				(errcode(GUC_check_errcode_value),
				 GUC_check_errmsg_string ?
7995
				 errmsg_internal("%s", GUC_check_errmsg_string) :
7996 7997 7998
				 errmsg("invalid value for parameter \"%s\": %d",
						conf->gen.name, (int) *newval),
				 GUC_check_errdetail_string ?
7999
				 errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010
				 GUC_check_errhint_string ?
				 errhint("%s", GUC_check_errhint_string) : 0));
		/* Flush any strings created in ErrorContext */
		FlushErrorState();
		return false;
	}

	return true;
}

static bool
8011
call_int_check_hook(struct config_int * conf, int *newval, void **extra,
8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028
					GucSource source, int elevel)
{
	/* Quick success if no hook */
	if (!conf->check_hook)
		return true;

	/* Reset variables that might be set by hook */
	GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
	GUC_check_errmsg_string = NULL;
	GUC_check_errdetail_string = NULL;
	GUC_check_errhint_string = NULL;

	if (!(*conf->check_hook) (newval, extra, source))
	{
		ereport(elevel,
				(errcode(GUC_check_errcode_value),
				 GUC_check_errmsg_string ?
8029
				 errmsg_internal("%s", GUC_check_errmsg_string) :
8030 8031 8032
				 errmsg("invalid value for parameter \"%s\": %d",
						conf->gen.name, *newval),
				 GUC_check_errdetail_string ?
8033
				 errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044
				 GUC_check_errhint_string ?
				 errhint("%s", GUC_check_errhint_string) : 0));
		/* Flush any strings created in ErrorContext */
		FlushErrorState();
		return false;
	}

	return true;
}

static bool
8045
call_real_check_hook(struct config_real * conf, double *newval, void **extra,
8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062
					 GucSource source, int elevel)
{
	/* Quick success if no hook */
	if (!conf->check_hook)
		return true;

	/* Reset variables that might be set by hook */
	GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
	GUC_check_errmsg_string = NULL;
	GUC_check_errdetail_string = NULL;
	GUC_check_errhint_string = NULL;

	if (!(*conf->check_hook) (newval, extra, source))
	{
		ereport(elevel,
				(errcode(GUC_check_errcode_value),
				 GUC_check_errmsg_string ?
8063
				 errmsg_internal("%s", GUC_check_errmsg_string) :
8064 8065 8066
				 errmsg("invalid value for parameter \"%s\": %g",
						conf->gen.name, *newval),
				 GUC_check_errdetail_string ?
8067
				 errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078
				 GUC_check_errhint_string ?
				 errhint("%s", GUC_check_errhint_string) : 0));
		/* Flush any strings created in ErrorContext */
		FlushErrorState();
		return false;
	}

	return true;
}

static bool
8079
call_string_check_hook(struct config_string * conf, char **newval, void **extra,
8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096
					   GucSource source, int elevel)
{
	/* Quick success if no hook */
	if (!conf->check_hook)
		return true;

	/* Reset variables that might be set by hook */
	GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
	GUC_check_errmsg_string = NULL;
	GUC_check_errdetail_string = NULL;
	GUC_check_errhint_string = NULL;

	if (!(*conf->check_hook) (newval, extra, source))
	{
		ereport(elevel,
				(errcode(GUC_check_errcode_value),
				 GUC_check_errmsg_string ?
8097
				 errmsg_internal("%s", GUC_check_errmsg_string) :
8098 8099 8100
				 errmsg("invalid value for parameter \"%s\": \"%s\"",
						conf->gen.name, *newval ? *newval : ""),
				 GUC_check_errdetail_string ?
8101
				 errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112
				 GUC_check_errhint_string ?
				 errhint("%s", GUC_check_errhint_string) : 0));
		/* Flush any strings created in ErrorContext */
		FlushErrorState();
		return false;
	}

	return true;
}

static bool
8113
call_enum_check_hook(struct config_enum * conf, int *newval, void **extra,
8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130
					 GucSource source, int elevel)
{
	/* Quick success if no hook */
	if (!conf->check_hook)
		return true;

	/* Reset variables that might be set by hook */
	GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
	GUC_check_errmsg_string = NULL;
	GUC_check_errdetail_string = NULL;
	GUC_check_errhint_string = NULL;

	if (!(*conf->check_hook) (newval, extra, source))
	{
		ereport(elevel,
				(errcode(GUC_check_errcode_value),
				 GUC_check_errmsg_string ?
8131
				 errmsg_internal("%s", GUC_check_errmsg_string) :
8132 8133 8134 8135
				 errmsg("invalid value for parameter \"%s\": \"%s\"",
						conf->gen.name,
						config_enum_lookup_by_value(conf, *newval)),
				 GUC_check_errdetail_string ?
8136
				 errdetail_internal("%s", GUC_check_errdetail_string) : 0,
8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153
				 GUC_check_errhint_string ?
				 errhint("%s", GUC_check_errhint_string) : 0));
		/* Flush any strings created in ErrorContext */
		FlushErrorState();
		return false;
	}

	return true;
}


/*
 * check_hook, assign_hook and show_hook subroutines
 */

static bool
check_log_destination(char **newval, void **extra, GucSource source)
8154
{
B
Bruce Momjian 已提交
8155 8156 8157 8158
	char	   *rawstring;
	List	   *elemlist;
	ListCell   *l;
	int			newlogdest = 0;
8159
	int		   *myextra;
B
Bruce Momjian 已提交
8160

8161
	/* Need a modifiable copy of string */
8162
	rawstring = pstrdup(*newval);
8163 8164

	/* Parse string into list of identifiers */
B
Bruce Momjian 已提交
8165
	if (!SplitIdentifierString(rawstring, ',', &elemlist))
8166 8167
	{
		/* syntax error in list */
8168
		GUC_check_errdetail("List syntax is invalid.");
8169
		pfree(rawstring);
8170
		list_free(elemlist);
8171
		return false;
8172 8173 8174 8175
	}

	foreach(l, elemlist)
	{
B
Bruce Momjian 已提交
8176 8177 8178
		char	   *tok = (char *) lfirst(l);

		if (pg_strcasecmp(tok, "stderr") == 0)
8179
			newlogdest |= LOG_DESTINATION_STDERR;
8180
		else if (pg_strcasecmp(tok, "csvlog") == 0)
B
Bruce Momjian 已提交
8181
			newlogdest |= LOG_DESTINATION_CSVLOG;
8182
#ifdef HAVE_SYSLOG
B
Bruce Momjian 已提交
8183
		else if (pg_strcasecmp(tok, "syslog") == 0)
8184 8185 8186
			newlogdest |= LOG_DESTINATION_SYSLOG;
#endif
#ifdef WIN32
B
Bruce Momjian 已提交
8187
		else if (pg_strcasecmp(tok, "eventlog") == 0)
8188 8189
			newlogdest |= LOG_DESTINATION_EVENTLOG;
#endif
B
Bruce Momjian 已提交
8190 8191
		else
		{
8192
			GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
8193
			pfree(rawstring);
8194
			list_free(elemlist);
8195
			return false;
8196 8197 8198 8199
		}
	}

	pfree(rawstring);
8200
	list_free(elemlist);
8201

8202 8203 8204 8205 8206
	myextra = (int *) guc_malloc(ERROR, sizeof(int));
	*myextra = newlogdest;
	*extra = (void *) myextra;

	return true;
8207 8208
}

8209 8210 8211 8212 8213 8214 8215 8216
static void
assign_log_destination(const char *newval, void *extra)
{
	Log_destination = *((int *) extra);
}

static void
assign_syslog_facility(int newval, void *extra)
8217
{
8218
#ifdef HAVE_SYSLOG
8219 8220
	set_syslog_parameters(syslog_ident_str ? syslog_ident_str : "postgres",
						  newval);
8221 8222
#endif
	/* Without syslog support, just ignore it */
8223
}
8224

8225 8226
static void
assign_syslog_ident(const char *newval, void *extra)
8227
{
8228
#ifdef HAVE_SYSLOG
8229
	set_syslog_parameters(newval, syslog_facility);
8230 8231
#endif
	/* Without syslog support, it will always be set to "none", so ignore */
8232
}
8233 8234


8235 8236
static void
assign_session_replication_role(int newval, void *extra)
8237
{
8238 8239 8240 8241
	/*
	 * Must flush the plan cache when changing replication role; but don't
	 * flush unnecessarily.
	 */
8242
	if (SessionReplicationRole != newval)
8243
		ResetPlanCache();
8244 8245
}

8246 8247
static bool
check_temp_buffers(int *newval, void **extra, GucSource source)
8248 8249
{
	/*
8250
	 * Once local buffers have been initialized, it's too late to change this.
8251
	 */
8252 8253
	if (NLocBuffer && NLocBuffer != *newval)
	{
8254
		GUC_check_errdetail("\"temp_buffers\" cannot be changed after any temporary tables have been accessed in the session.");
8255 8256 8257
		return false;
	}
	return true;
8258 8259
}

8260
static bool
8261
check_phony_autocommit(bool *newval, void **extra, GucSource source)
8262
{
8263
	if (!*newval)
8264
	{
8265 8266
		GUC_check_errcode(ERRCODE_FEATURE_NOT_SUPPORTED);
		GUC_check_errmsg("SET AUTOCOMMIT TO OFF is no longer supported");
8267 8268 8269 8270 8271
		return false;
	}
	return true;
}

8272 8273
static bool
check_custom_variable_classes(char **newval, void **extra, GucSource source)
8274
{
B
Bruce Momjian 已提交
8275 8276
	/*
	 * Check syntax. newval must be a comma separated list of identifiers.
8277
	 * Whitespace is allowed but removed from the result.
8278
	 */
B
Bruce Momjian 已提交
8279
	bool		hasSpaceAfterToken = false;
8280
	const char *cp = *newval;
B
Bruce Momjian 已提交
8281
	int			symLen = 0;
8282
	char		c;
8283 8284
	StringInfoData buf;

8285 8286 8287 8288
	/* Default NULL is OK */
	if (cp == NULL)
		return true;

8289
	initStringInfo(&buf);
8290
	while ((c = *cp++) != '\0')
8291
	{
8292
		if (isspace((unsigned char) c))
8293
		{
B
Bruce Momjian 已提交
8294
			if (symLen > 0)
8295 8296 8297 8298
				hasSpaceAfterToken = true;
			continue;
		}

B
Bruce Momjian 已提交
8299
		if (c == ',')
8300
		{
8301
			if (symLen > 0)		/* terminate identifier */
8302 8303
			{
				appendStringInfoChar(&buf, ',');
8304
				symLen = 0;
8305
			}
8306
			hasSpaceAfterToken = false;
8307 8308 8309
			continue;
		}

8310
		if (hasSpaceAfterToken || !(isalnum((unsigned char) c) || c == '_'))
8311
		{
B
Bruce Momjian 已提交
8312
			/*
8313 8314
			 * Syntax error due to token following space after token or
			 * non-identifier character
8315
			 */
8316
			pfree(buf.data);
8317
			return false;
8318
		}
8319
		appendStringInfoChar(&buf, c);
8320 8321 8322
		symLen++;
	}

8323
	/* Remove stray ',' at end */
B
Bruce Momjian 已提交
8324
	if (symLen == 0 && buf.len > 0)
8325
		buf.data[--buf.len] = '\0';
8326

8327
	/* GUC wants the result malloc'd */
8328 8329
	free(*newval);
	*newval = guc_strdup(LOG, buf.data);
8330 8331

	pfree(buf.data);
8332
	return true;
8333
}
8334

8335
static bool
8336
check_debug_assertions(bool *newval, void **extra, GucSource source)
8337 8338
{
#ifndef USE_ASSERT_CHECKING
8339
	if (*newval)
8340
	{
8341
		GUC_check_errmsg("assertion checking is not supported by this build");
8342 8343
		return false;
	}
8344 8345 8346 8347
#endif
	return true;
}

8348
static bool
8349
check_bonjour(bool *newval, void **extra, GucSource source)
8350 8351
{
#ifndef USE_BONJOUR
8352
	if (*newval)
8353
	{
8354
		GUC_check_errmsg("Bonjour is not supported by this build");
8355 8356 8357 8358 8359 8360
		return false;
	}
#endif
	return true;
}

8361
static bool
8362
check_ssl(bool *newval, void **extra, GucSource source)
8363 8364
{
#ifndef USE_SSL
8365
	if (*newval)
8366
	{
8367
		GUC_check_errmsg("SSL is not supported by this build");
8368 8369
		return false;
	}
8370 8371 8372 8373
#endif
	return true;
}

8374
static bool
8375
check_stage_log_stats(bool *newval, void **extra, GucSource source)
8376
{
8377
	if (*newval && log_statement_stats)
8378
	{
8379 8380
		GUC_check_errdetail("Cannot enable parameter when \"log_statement_stats\" is true.");
		return false;
8381 8382 8383 8384 8385
	}
	return true;
}

static bool
8386
check_log_stats(bool *newval, void **extra, GucSource source)
8387
{
8388
	if (*newval &&
8389
		(log_parser_stats || log_planner_stats || log_executor_stats))
8390
	{
8391 8392 8393 8394
		GUC_check_errdetail("Cannot enable \"log_statement_stats\" when "
							"\"log_parser_stats\", \"log_planner_stats\", "
							"or \"log_executor_stats\" is true.");
		return false;
8395 8396 8397 8398
	}
	return true;
}

8399 8400
static bool
check_canonical_path(char **newval, void **extra, GucSource source)
8401
{
8402
	/*
8403 8404 8405
	 * Since canonicalize_path never enlarges the string, we can just modify
	 * newval in-place.  But watch out for NULL, which is the default value
	 * for external_pid_file.
8406 8407 8408 8409
	 */
	if (*newval)
		canonicalize_path(*newval);
	return true;
8410 8411
}

8412 8413
static bool
check_timezone_abbreviations(char **newval, void **extra, GucSource source)
8414
{
8415
	/*
8416 8417 8418 8419 8420 8421
	 * The boot_val given above for timezone_abbreviations is NULL. When we
	 * see this we just do nothing.  If this value isn't overridden from the
	 * config file then pg_timezone_abbrev_initialize() will eventually
	 * replace it with "Default".  This hack has two purposes: to avoid
	 * wasting cycles loading values that might soon be overridden from the
	 * config file, and to avoid trying to read the timezone abbrev files
B
Bruce Momjian 已提交
8422 8423
	 * during InitializeGUCOptions().  The latter doesn't work in an
	 * EXEC_BACKEND subprocess because my_exec_path hasn't been set yet and so
8424
	 * we can't locate PGSHAREDIR.
8425
	 */
8426
	if (*newval == NULL)
8427
	{
8428 8429
		Assert(source == PGC_S_DEFAULT);
		return true;
8430 8431
	}

8432 8433
	/* OK, load the file and produce a malloc'd TimeZoneAbbrevTable */
	*extra = load_tzoffsets(*newval);
8434

8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449
	/* tzparser.c returns NULL on failure, reporting via GUC_check_errmsg */
	if (!*extra)
		return false;

	return true;
}

static void
assign_timezone_abbreviations(const char *newval, void *extra)
{
	/* Do nothing for the boot_val default of NULL */
	if (!extra)
		return;

	InstallTimeZoneAbbrevs((TimeZoneAbbrevTable *) extra);
8450 8451
}

8452 8453 8454 8455 8456
/*
 * pg_timezone_abbrev_initialize --- set default value if not done already
 *
 * This is called after initial loading of postgresql.conf.  If no
 * timezone_abbreviations setting was found therein, select default.
8457
 * If a non-default value is already installed, nothing will happen.
8458 8459 8460 8461
 */
void
pg_timezone_abbrev_initialize(void)
{
8462 8463
	SetConfigOption("timezone_abbreviations", "Default",
					PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT);
8464 8465
}

8466 8467 8468
static const char *
show_archive_command(void)
{
8469
	if (XLogArchivingActive())
8470 8471 8472 8473 8474
		return XLogArchiveCommand;
	else
		return "(disabled)";
}

8475 8476
static void
assign_tcp_keepalives_idle(int newval, void *extra)
8477
{
8478
	/*
8479 8480 8481
	 * The kernel API provides no way to test a value without setting it; and
	 * once we set it we might fail to unset it.  So there seems little point
	 * in fully implementing the check-then-assign GUC API for these
8482 8483 8484
	 * variables.  Instead we just do the assignment on demand.  pqcomm.c
	 * reports any problems via elog(LOG).
	 *
8485 8486 8487
	 * This approach means that the GUC value might have little to do with the
	 * actual kernel value, so we use a show_hook that retrieves the kernel
	 * value rather than trusting GUC's copy.
8488 8489
	 */
	(void) pq_setkeepalivesidle(newval, MyProcPort);
8490 8491 8492 8493 8494
}

static const char *
show_tcp_keepalives_idle(void)
{
8495
	/* See comments in assign_tcp_keepalives_idle */
8496 8497 8498
	static char nbuf[16];

	snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
8499 8500 8501
	return nbuf;
}

8502 8503
static void
assign_tcp_keepalives_interval(int newval, void *extra)
8504
{
8505 8506
	/* See comments in assign_tcp_keepalives_idle */
	(void) pq_setkeepalivesinterval(newval, MyProcPort);
8507 8508 8509 8510 8511
}

static const char *
show_tcp_keepalives_interval(void)
{
8512
	/* See comments in assign_tcp_keepalives_idle */
8513 8514 8515
	static char nbuf[16];

	snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
8516 8517 8518
	return nbuf;
}

8519 8520
static void
assign_tcp_keepalives_count(int newval, void *extra)
8521
{
8522 8523
	/* See comments in assign_tcp_keepalives_idle */
	(void) pq_setkeepalivescount(newval, MyProcPort);
8524 8525 8526 8527 8528
}

static const char *
show_tcp_keepalives_count(void)
{
8529
	/* See comments in assign_tcp_keepalives_idle */
8530 8531 8532
	static char nbuf[16];

	snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
8533 8534
	return nbuf;
}
8535

8536
static bool
8537
check_maxconnections(int *newval, void **extra, GucSource source)
8538
{
8539
	if (*newval + autovacuum_max_workers + 1 > MAX_BACKENDS)
8540
		return false;
8541 8542 8543
	return true;
}

8544 8545 8546 8547 8548 8549
static void
assign_maxconnections(int newval, void *extra)
{
	MaxBackends = newval + autovacuum_max_workers + 1;
}

8550
static bool
8551
check_autovacuum_max_workers(int *newval, void **extra, GucSource source)
8552
{
8553
	if (MaxConnections + *newval + 1 > MAX_BACKENDS)
8554
		return false;
8555 8556
	return true;
}
8557

8558 8559 8560 8561 8562 8563
static void
assign_autovacuum_max_workers(int newval, void *extra)
{
	MaxBackends = MaxConnections + newval + 1;
}

8564
static bool
8565
check_effective_io_concurrency(int *newval, void **extra, GucSource source)
8566 8567 8568 8569 8570 8571 8572 8573
{
#ifdef USE_PREFETCH
	double		new_prefetch_pages = 0.0;
	int			i;

	/*----------
	 * The user-visible GUC parameter is the number of drives (spindles),
	 * which we need to translate to a number-of-pages-to-prefetch target.
8574 8575
	 * The target value is stashed in *extra and then assigned to the actual
	 * variable by assign_effective_io_concurrency.
8576 8577 8578 8579 8580
	 *
	 * The expected number of prefetch pages needed to keep N drives busy is:
	 *
	 * drives |   I/O requests
	 * -------+----------------
8581 8582 8583 8584 8585
	 *		1 |   1
	 *		2 |   2/1 + 2/2 = 3
	 *		3 |   3/1 + 3/2 + 3/3 = 5 1/2
	 *		4 |   4/1 + 4/2 + 4/3 + 4/4 = 8 1/3
	 *		n |   n * H(n)
8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599
	 *
	 * This is called the "coupon collector problem" and H(n) is called the
	 * harmonic series.  This could be approximated by n * ln(n), but for
	 * reasonable numbers of drives we might as well just compute the series.
	 *
	 * Alternatively we could set the target to the number of pages necessary
	 * so that the expected number of active spindles is some arbitrary
	 * percentage of the total.  This sounds the same but is actually slightly
	 * different.  The result ends up being ln(1-P)/ln((n-1)/n) where P is
	 * that desired fraction.
	 *
	 * Experimental results show that both of these formulas aren't aggressive
	 * enough, but we don't really have any better proposals.
	 *
8600
	 * Note that if *newval = 0 (disabled), we must set target = 0.
8601 8602 8603
	 *----------
	 */

8604 8605
	for (i = 1; i <= *newval; i++)
		new_prefetch_pages += (double) *newval / (double) i;
8606 8607 8608 8609

	/* This range check shouldn't fail, but let's be paranoid */
	if (new_prefetch_pages >= 0.0 && new_prefetch_pages < (double) INT_MAX)
	{
8610
		int		   *myextra = (int *) guc_malloc(ERROR, sizeof(int));
8611 8612 8613 8614

		*myextra = (int) rint(new_prefetch_pages);
		*extra = (void *) myextra;

8615 8616 8617 8618 8619 8620
		return true;
	}
	else
		return false;
#else
	return true;
8621
#endif   /* USE_PREFETCH */
8622 8623
}

8624 8625
static void
assign_effective_io_concurrency(int newval, void *extra)
8626
{
8627 8628 8629 8630
#ifdef USE_PREFETCH
	target_prefetch_pages = *((int *) extra);
#endif   /* USE_PREFETCH */
}
8631

8632 8633 8634 8635 8636 8637
static void
assign_pgstat_temp_directory(const char *newval, void *extra)
{
	/* check_canonical_path already canonicalized newval for us */
	char	   *tname;
	char	   *fname;
8638

8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649
	tname = guc_malloc(ERROR, strlen(newval) + 12);		/* /pgstat.tmp */
	sprintf(tname, "%s/pgstat.tmp", newval);
	fname = guc_malloc(ERROR, strlen(newval) + 13);		/* /pgstat.stat */
	sprintf(fname, "%s/pgstat.stat", newval);

	if (pgstat_stat_tmpname)
		free(pgstat_stat_tmpname);
	pgstat_stat_tmpname = tname;
	if (pgstat_stat_filename)
		free(pgstat_stat_filename);
	pgstat_stat_filename = fname;
8650 8651
}

8652 8653
static bool
check_application_name(char **newval, void **extra, GucSource source)
8654
{
8655 8656
	/* Only allow clean ASCII chars in the application name */
	char	   *p;
8657

8658 8659 8660 8661 8662
	for (p = *newval; *p; p++)
	{
		if (*p < 32 || *p > 126)
			*p = '?';
	}
8663

8664 8665
	return true;
}
8666

8667 8668 8669 8670 8671
static void
assign_application_name(const char *newval, void *extra)
{
	/* Update the pg_stat_activity view */
	pgstat_report_appname(newval);
8672 8673
}

8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691
static const char *
show_unix_socket_permissions(void)
{
	static char buf[8];

	snprintf(buf, sizeof(buf), "%04o", Unix_socket_permissions);
	return buf;
}

static const char *
show_log_file_mode(void)
{
	static char buf[8];

	snprintf(buf, sizeof(buf), "%04o", Log_file_mode);
	return buf;
}

8692
#include "guc-file.c"