guc.c 61.2 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
 * $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.78 2002/08/07 17:26:24 tgl Exp $
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright 2000 by PostgreSQL Global Development Group
 * Written by Peter Eisentraut <peter_e@gmx.net>.
 *--------------------------------------------------------------------
 */

#include "postgres.h"

#include <errno.h>
#include <float.h>
#include <limits.h>
#include <unistd.h>

#include "utils/guc.h"

24
#include "access/xlog.h"
25
#include "catalog/namespace.h"
26
#include "catalog/pg_type.h"
27
#include "commands/async.h"
28
#include "commands/variable.h"
29
#include "commands/vacuum.h"
30
#include "executor/executor.h"
31
#include "fmgr.h"
32
#include "libpq/auth.h"
33
#include "libpq/pqcomm.h"
34
#include "mb/pg_wchar.h"
35 36 37 38 39 40
#include "miscadmin.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "parser/parse_expr.h"
41
#include "storage/fd.h"
42 43
#include "storage/freespace.h"
#include "storage/lock.h"
44 45
#include "storage/proc.h"
#include "tcop/tcopprot.h"
46 47
#include "utils/array.h"
#include "utils/builtins.h"
48
#include "utils/datetime.h"
49
#include "utils/elog.h"
50
#include "utils/pg_locale.h"
51
#include "pgstat.h"
52 53


54
/* XXX these should be in other modules' header files */
55
extern bool Log_connections;
56 57
extern int	PreAuthDelay;
extern int	AuthenticationTimeout;
58
extern int	StatementTimeout;
B
Bruce Momjian 已提交
59 60 61
extern int	CheckPointTimeout;
extern int	CommitDelay;
extern int	CommitSiblings;
62 63
extern bool FixBTree;

T
Tatsuo Ishii 已提交
64
#ifdef HAVE_SYSLOG
65
extern char *Syslog_facility;
66
extern char *Syslog_ident;
67

68 69 70
static const char *assign_facility(const char *facility,
								   bool doit, bool interactive);
#endif
71

72 73 74
/*
 * Debugging options
 */
75
#ifdef USE_ASSERT_CHECKING
B
Bruce Momjian 已提交
76
bool		assert_enabled = true;
77
#endif
B
Bruce Momjian 已提交
78 79 80 81 82
bool		Debug_print_query = false;
bool		Debug_print_plan = false;
bool		Debug_print_parse = false;
bool		Debug_print_rewritten = false;
bool		Debug_pretty_print = false;
83

B
Bruce Momjian 已提交
84 85 86 87 88 89
bool		Show_parser_stats = false;
bool		Show_planner_stats = false;
bool		Show_executor_stats = false;
bool		Show_query_stats = false;	/* this is sort of all three above
										 * together */
bool		Show_btree_build_stats = false;
90

91 92
bool		Explain_pretty_print = true;

B
Bruce Momjian 已提交
93
bool		SQL_inheritance = true;
94

95 96
bool		Australian_timezones = false;

97 98
bool		Password_encryption = false;

99
#ifndef PG_KRB_SRVTAB
B
Bruce Momjian 已提交
100
#define PG_KRB_SRVTAB ""
101 102
#endif

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/*
 * 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.
 */
static double phony_random_seed;
static char *client_encoding_string;
static char *datestyle_string;
static char *default_iso_level_string;
static char *server_encoding_string;
static char *session_authorization_string;
static char *timezone_string;
static char *XactIsoLevel_string;

static const char *assign_defaultxactisolevel(const char *newval,
											  bool doit, bool interactive);

120

121 122
/*
 * Declarations for GUC tables
123 124
 *
 * See src/backend/utils/misc/README for design notes.
125
 */
126 127
enum config_type
{
128 129 130 131
	PGC_BOOL,
	PGC_INT,
	PGC_REAL,
	PGC_STRING
132 133
};

134
/* Generic fields applicable to all types of variables */
135 136
struct config_generic
{
137 138 139 140 141 142 143 144 145 146 147
	/* constant fields, must be set correctly in initial value: */
	const char *name;			/* name of variable - MUST BE FIRST */
	GucContext	context;		/* context required to set the variable */
	int			flags;			/* flag bits, see below */
	/* variable fields, initialized at runtime: */
	enum config_type vartype;	/* type of variable (set only at startup) */
	int			status;			/* status bits, see below */
	GucSource	reset_source;	/* source of the reset_value */
	GucSource	session_source;	/* source of the session_value */
	GucSource	tentative_source; /* source of the tentative_value */
	GucSource	source;			/* source of the current actual value */
148 149
};

150 151 152 153 154 155 156 157 158 159 160 161
/* bit values in flags field */
#define GUC_LIST_INPUT		0x0001	/* input can be list format */
#define GUC_LIST_QUOTE		0x0002	/* double-quote list elements */
#define GUC_NO_SHOW_ALL		0x0004	/* exclude from SHOW ALL */
#define GUC_NO_RESET_ALL	0x0008	/* exclude from RESET ALL */

/* bit values in status field */
#define GUC_HAVE_TENTATIVE	0x0001	/* tentative value is defined */
#define GUC_HAVE_LOCAL		0x0002	/* a SET LOCAL has been executed */


/* GUC records for specific variable types */
162 163 164

struct config_bool
{
165 166 167
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
168
	bool	   *variable;
169 170 171 172 173 174
	bool		reset_val;
	bool		(*assign_hook) (bool newval, bool doit, bool interactive);
	const char *(*show_hook) (void);
	/* variable fields, initialized at runtime: */
	bool		session_val;
	bool		tentative_val;
175 176 177 178
};

struct config_int
{
179 180 181
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
182
	int		   *variable;
183
	int			reset_val;
B
Bruce Momjian 已提交
184 185
	int			min;
	int			max;
186 187 188 189 190
	bool		(*assign_hook) (int newval, bool doit, bool interactive);
	const char *(*show_hook) (void);
	/* variable fields, initialized at runtime: */
	int			session_val;
	int			tentative_val;
191 192 193 194
};

struct config_real
{
195 196 197
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
198
	double	   *variable;
199
	double		reset_val;
B
Bruce Momjian 已提交
200 201
	double		min;
	double		max;
202 203 204 205 206
	bool		(*assign_hook) (double newval, bool doit, bool interactive);
	const char *(*show_hook) (void);
	/* variable fields, initialized at runtime: */
	double		session_val;
	double		tentative_val;
207 208 209 210
};

struct config_string
{
211 212 213
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all are constants) */
B
Bruce Momjian 已提交
214
	char	  **variable;
215 216 217 218 219 220 221
	const char *boot_val;
	const char *(*assign_hook) (const char *newval, bool doit, bool interactive);
	const char *(*show_hook) (void);
	/* variable fields, initialized at runtime: */
	char	   *reset_val;
	char	   *session_val;
	char	   *tentative_val;
222 223
};

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/* Macros for freeing malloc'd pointers only if appropriate to do so */
/* Some of these tests are probably redundant, but be safe ... */
#define SET_STRING_VARIABLE(rec, newval) \
	do { \
		if (*(rec)->variable && \
			*(rec)->variable != (rec)->reset_val && \
			*(rec)->variable != (rec)->session_val && \
			*(rec)->variable != (rec)->tentative_val) \
			free(*(rec)->variable); \
		*(rec)->variable = (newval); \
	} while (0)
#define SET_STRING_RESET_VAL(rec, newval) \
	do { \
		if ((rec)->reset_val && \
			(rec)->reset_val != *(rec)->variable && \
			(rec)->reset_val != (rec)->session_val && \
			(rec)->reset_val != (rec)->tentative_val) \
			free((rec)->reset_val); \
		(rec)->reset_val = (newval); \
	} while (0)
#define SET_STRING_SESSION_VAL(rec, newval) \
	do { \
		if ((rec)->session_val && \
			(rec)->session_val != *(rec)->variable && \
			(rec)->session_val != (rec)->reset_val && \
			(rec)->session_val != (rec)->tentative_val) \
			free((rec)->session_val); \
		(rec)->session_val = (newval); \
	} while (0)
#define SET_STRING_TENTATIVE_VAL(rec, newval) \
	do { \
		if ((rec)->tentative_val && \
			(rec)->tentative_val != *(rec)->variable && \
			(rec)->tentative_val != (rec)->reset_val && \
			(rec)->tentative_val != (rec)->session_val) \
			free((rec)->tentative_val); \
		(rec)->tentative_val = (newval); \
	} while (0)


264

265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*
 * TO ADD AN OPTION:
 *
 * 1. Declare a global variable of type bool, int, double, or char*
 * and make use of it.
 *
 * 2. Decide at what times it's safe to set the option. See guc.h for
 * details.
 *
 * 3. Decide on a name, a default value, upper and lower bounds (if
 * applicable), etc.
 *
 * 4. Add a record below.
 *
279
 * 5. Add it to src/backend/utils/misc/postgresql.conf.sample.
280
 *
281
 * 6. Add it to src/bin/psql/tab-complete.c, if it's a USERSET option.
282
 *
283
 * 7. Don't forget to document the option.
284 285
 */

286

287
/******** option records follow ********/
288 289

static struct config_bool
B
Bruce Momjian 已提交
290
			ConfigureNamesBool[] =
291
{
292
	{
293 294
		{ "enable_seqscan", PGC_USERSET }, &enable_seqscan,
		true, NULL, NULL
295
	},
296
	{
297 298
		{ "enable_indexscan", PGC_USERSET }, &enable_indexscan,
		true, NULL, NULL
299 300
	},
	{
301 302
		{ "enable_tidscan", PGC_USERSET }, &enable_tidscan,
		true, NULL, NULL
303 304
	},
	{
305 306
		{ "enable_sort", PGC_USERSET }, &enable_sort,
		true, NULL, NULL
307 308
	},
	{
309 310
		{ "enable_nestloop", PGC_USERSET }, &enable_nestloop,
		true, NULL, NULL
311 312
	},
	{
313 314
		{ "enable_mergejoin", PGC_USERSET }, &enable_mergejoin,
		true, NULL, NULL
315 316
	},
	{
317 318
		{ "enable_hashjoin", PGC_USERSET }, &enable_hashjoin,
		true, NULL, NULL
319 320
	},
	{
321 322
		{ "geqo", PGC_USERSET }, &enable_geqo,
		true, NULL, NULL
323 324 325
	},

	{
326 327
		{ "tcpip_socket", PGC_POSTMASTER }, &NetServer,
		false, NULL, NULL
328 329
	},
	{
330 331
		{ "ssl", PGC_POSTMASTER }, &EnableSSL,
		false, NULL, NULL
332 333
	},
	{
334 335
		{ "fsync", PGC_SIGHUP }, &enableFsync,
		true, NULL, NULL
336 337
	},
	{
338 339
		{ "silent_mode", PGC_POSTMASTER }, &SilentMode,
		false, NULL, NULL
340 341 342
	},

	{
343 344
		{ "log_connections", PGC_BACKEND }, &Log_connections,
		false, NULL, NULL
345 346
	},
	{
347 348
		{ "log_timestamp", PGC_SIGHUP }, &Log_timestamp,
		false, NULL, NULL
349 350
	},
	{
351 352
		{ "log_pid", PGC_SIGHUP }, &Log_pid,
		false, NULL, NULL
353
	},
354

355
#ifdef USE_ASSERT_CHECKING
356
	{
357 358
		{ "debug_assertions", PGC_USERSET }, &assert_enabled,
		true, NULL, NULL
359
	},
360 361
#endif

362
	{
363 364
		{ "debug_print_query", PGC_USERSET }, &Debug_print_query,
		false, NULL, NULL
365 366
	},
	{
367 368
		{ "debug_print_parse", PGC_USERSET }, &Debug_print_parse,
		false, NULL, NULL
369 370
	},
	{
371 372
		{ "debug_print_rewritten", PGC_USERSET }, &Debug_print_rewritten,
		false, NULL, NULL
373 374
	},
	{
375 376
		{ "debug_print_plan", PGC_USERSET }, &Debug_print_plan,
		false, NULL, NULL
377 378
	},
	{
379 380
		{ "debug_pretty_print", PGC_USERSET }, &Debug_pretty_print,
		false, NULL, NULL
381
	},
382

383
	{
384 385
		{ "show_parser_stats", PGC_USERSET }, &Show_parser_stats,
		false, NULL, NULL
386 387
	},
	{
388 389
		{ "show_planner_stats", PGC_USERSET }, &Show_planner_stats,
		false, NULL, NULL
390 391
	},
	{
392 393
		{ "show_executor_stats", PGC_USERSET }, &Show_executor_stats,
		false, NULL, NULL
394 395
	},
	{
396 397
		{ "show_query_stats", PGC_USERSET }, &Show_query_stats,
		false, NULL, NULL
398
	},
399
#ifdef BTREE_BUILD_STATS
400
	{
401 402
		{ "show_btree_build_stats", PGC_SUSET }, &Show_btree_build_stats,
		false, NULL, NULL
403
	},
404 405
#endif

406
	{
407 408
		{ "explain_pretty_print", PGC_USERSET }, &Explain_pretty_print,
		true, NULL, NULL
409 410
	},

411
	{
412 413
		{ "stats_start_collector", PGC_POSTMASTER }, &pgstat_collect_startcollector,
		true, NULL, NULL
414 415
	},
	{
416 417
		{ "stats_reset_on_server_start", PGC_POSTMASTER }, &pgstat_collect_resetonpmstart,
		true, NULL, NULL
418 419
	},
	{
420 421
		{ "stats_command_string", PGC_SUSET }, &pgstat_collect_querystring,
		false, NULL, NULL
422 423
	},
	{
424 425
		{ "stats_row_level", PGC_SUSET }, &pgstat_collect_tuplelevel,
		false, NULL, NULL
426 427
	},
	{
428 429
		{ "stats_block_level", PGC_SUSET }, &pgstat_collect_blocklevel,
		false, NULL, NULL
430
	},
431

432
	{
433 434
		{ "trace_notify", PGC_USERSET }, &Trace_notify,
		false, NULL, NULL
435
	},
436 437

#ifdef LOCK_DEBUG
438
	{
439 440
		{ "trace_locks", PGC_SUSET }, &Trace_locks,
		false, NULL, NULL
441 442
	},
	{
443 444
		{ "trace_userlocks", PGC_SUSET }, &Trace_userlocks,
		false, NULL, NULL
445 446
	},
	{
447 448
		{ "trace_lwlocks", PGC_SUSET }, &Trace_lwlocks,
		false, NULL, NULL
449 450
	},
	{
451 452
		{ "debug_deadlocks", PGC_SUSET }, &Debug_deadlocks,
		false, NULL, NULL
453
	},
454 455
#endif

456
	{
457 458
		{ "hostname_lookup", PGC_SIGHUP }, &HostnameLookup,
		false, NULL, NULL
459 460
	},
	{
461 462
		{ "show_source_port", PGC_SIGHUP }, &ShowPortNumber,
		false, NULL, NULL
463
	},
464

465
	{
466 467
		{ "sql_inheritance", PGC_USERSET }, &SQL_inheritance,
		true, NULL, NULL
468 469
	},
	{
470 471
		{ "australian_timezones", PGC_USERSET }, &Australian_timezones,
		false, ClearDateCache, NULL
472 473
	},
	{
474 475
		{ "fixbtree", PGC_POSTMASTER }, &FixBTree,
		true, NULL, NULL
476 477
	},
	{
478 479
		{ "password_encryption", PGC_USERSET }, &Password_encryption,
		false, NULL, NULL
480 481
	},
	{
482 483
		{ "transform_null_equals", PGC_USERSET }, &Transform_null_equals,
		false, NULL, NULL
484
	},
485

486
	{
487
		{ NULL, 0 }, NULL, false, NULL, NULL
488
	}
489 490 491 492
};


static struct config_int
B
Bruce Momjian 已提交
493
			ConfigureNamesInt[] =
494
{
495 496 497 498
	{
		{ "default_statistics_target", PGC_USERSET }, &default_statistics_target,
		10, 1, 1000, NULL, NULL
	},
499
	{
500
		{ "geqo_threshold", PGC_USERSET }, &geqo_rels,
501
		DEFAULT_GEQO_RELS, 2, INT_MAX, NULL, NULL
502
	},
503
	{
504
		{ "geqo_pool_size", PGC_USERSET }, &Geqo_pool_size,
505
		DEFAULT_GEQO_POOL_SIZE, 0, MAX_GEQO_POOL_SIZE, NULL, NULL
506 507
	},
	{
508
		{ "geqo_effort", PGC_USERSET }, &Geqo_effort,
509
		1, 1, INT_MAX, NULL, NULL
510 511
	},
	{
512
		{ "geqo_generations", PGC_USERSET }, &Geqo_generations,
513
		0, 0, INT_MAX, NULL, NULL
514 515
	},
	{
516
		{ "geqo_random_seed", PGC_USERSET }, &Geqo_random_seed,
517
		-1, INT_MIN, INT_MAX, NULL, NULL
518 519 520
	},

	{
521
		{ "deadlock_timeout", PGC_POSTMASTER }, &DeadlockTimeout,
522
		1000, 0, INT_MAX, NULL, NULL
523
	},
524

T
Tatsuo Ishii 已提交
525
#ifdef HAVE_SYSLOG
526
	{
527
		{ "syslog", PGC_SIGHUP }, &Use_syslog,
528
		0, 0, 2, NULL, NULL
529
	},
530 531 532
#endif

	/*
B
Bruce Momjian 已提交
533 534 535
	 * Note: There is some postprocessing done in PostmasterMain() to make
	 * sure the buffers are at least twice the number of backends, so the
	 * constraints here are partially unused.
536
	 */
537
	{
538
		{ "max_connections", PGC_POSTMASTER }, &MaxBackends,
539
		DEF_MAXBACKENDS, 1, INT_MAX, NULL, NULL
540
	},
541

542
	{
543
		{ "shared_buffers", PGC_POSTMASTER }, &NBuffers,
544
		DEF_NBUFFERS, 16, INT_MAX, NULL, NULL
545
	},
546

547
	{
548
		{ "port", PGC_POSTMASTER }, &PostPortNumber,
549
		DEF_PGPORT, 1, 65535, NULL, NULL
550
	},
551

552
	{
553
		{ "unix_socket_permissions", PGC_POSTMASTER }, &Unix_socket_permissions,
554
		0777, 0000, 0777, NULL, NULL
555
	},
556

557
	{
558
		{ "sort_mem", PGC_USERSET }, &SortMem,
559
		512, 4 * BLCKSZ / 1024, INT_MAX, NULL, NULL
560
	},
561

562
	{
563
		{ "vacuum_mem", PGC_USERSET }, &VacuumMem,
564
		8192, 1024, INT_MAX, NULL, NULL
565
	},
566

567
	{
568
		{ "max_files_per_process", PGC_BACKEND }, &max_files_per_process,
569
		1000, 25, INT_MAX, NULL, NULL
570
	},
571

572
#ifdef LOCK_DEBUG
573
	{
574
		{ "trace_lock_oidmin", PGC_SUSET }, &Trace_lock_oidmin,
575
		BootstrapObjectIdData, 1, INT_MAX, NULL, NULL
576 577
	},
	{
578
		{ "trace_lock_table", PGC_SUSET }, &Trace_lock_table,
579
		0, 0, INT_MAX, NULL, NULL
580
	},
581
#endif
582
	{
583
		{ "max_expr_depth", PGC_USERSET }, &max_expr_depth,
584
		DEFAULT_MAX_EXPR_DEPTH, 10, INT_MAX, NULL, NULL
585
	},
586

587 588 589 590 591
	{
		{ "statement_timeout", PGC_USERSET }, &StatementTimeout,
		0, 0, INT_MAX, NULL, NULL
	},

592
	{
593
		{ "max_fsm_relations", PGC_POSTMASTER }, &MaxFSMRelations,
594
		100, 10, INT_MAX, NULL, NULL
595 596
	},
	{
597
		{ "max_fsm_pages", PGC_POSTMASTER }, &MaxFSMPages,
598
		10000, 1000, INT_MAX, NULL, NULL
599
	},
600

601
	{
602
		{ "max_locks_per_transaction", PGC_POSTMASTER }, &max_locks_per_xact,
603
		64, 10, INT_MAX, NULL, NULL
604
	},
605

606
	{
607
		{ "authentication_timeout", PGC_SIGHUP }, &AuthenticationTimeout,
608
		60, 1, 600, NULL, NULL
609
	},
610

611
	{
612
		{ "pre_auth_delay", PGC_SIGHUP }, &PreAuthDelay,
613
		0, 0, 60, NULL, NULL
614
	},
615

616
	{
617
		{ "checkpoint_segments", PGC_SIGHUP }, &CheckPointSegments,
618
		3, 1, INT_MAX, NULL, NULL
619
	},
T
Tom Lane 已提交
620

621
	{
622
		{ "checkpoint_timeout", PGC_SIGHUP }, &CheckPointTimeout,
623
		300, 30, 3600, NULL, NULL
624
	},
V
Vadim B. Mikheev 已提交
625

626
	{
627
		{ "wal_buffers", PGC_POSTMASTER }, &XLOGbuffers,
628
		8, 4, INT_MAX, NULL, NULL
629
	},
V
Vadim B. Mikheev 已提交
630

631
	{
632
		{ "wal_files", PGC_SIGHUP }, &XLOGfiles,
633
		0, 0, 64, NULL, NULL
634
	},
635

636
	{
637
		{ "wal_debug", PGC_SUSET }, &XLOG_DEBUG,
638
		0, 0, 16, NULL, NULL
639
	},
V
Vadim B. Mikheev 已提交
640

641
	{
642
		{ "commit_delay", PGC_USERSET }, &CommitDelay,
643
		0, 0, 100000, NULL, NULL
644
	},
V
Vadim B. Mikheev 已提交
645

646
	{
647
		{ "commit_siblings", PGC_USERSET }, &CommitSiblings,
648
		5, 1, 1000, NULL, NULL
649
	},
650

651
	{
652
		{ NULL, 0 }, NULL, 0, 0, 0, NULL, NULL
653
	}
654 655 656 657
};


static struct config_real
B
Bruce Momjian 已提交
658
			ConfigureNamesReal[] =
659
{
660
	{
661
		{ "effective_cache_size", PGC_USERSET }, &effective_cache_size,
662
		DEFAULT_EFFECTIVE_CACHE_SIZE, 0, DBL_MAX, NULL, NULL
663
	},
664
	{
665
		{ "random_page_cost", PGC_USERSET }, &random_page_cost,
666
		DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX, NULL, NULL
667 668
	},
	{
669
		{ "cpu_tuple_cost", PGC_USERSET }, &cpu_tuple_cost,
670
		DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX, NULL, NULL
671 672
	},
	{
673
		{ "cpu_index_tuple_cost", PGC_USERSET }, &cpu_index_tuple_cost,
674
		DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX, NULL, NULL
675 676
	},
	{
677
		{ "cpu_operator_cost", PGC_USERSET }, &cpu_operator_cost,
678
		DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX, NULL, NULL
679 680 681
	},

	{
682
		{ "geqo_selection_bias", PGC_USERSET }, &Geqo_selection_bias,
683 684
		DEFAULT_GEQO_SELECTION_BIAS, MIN_GEQO_SELECTION_BIAS,
		MAX_GEQO_SELECTION_BIAS, NULL, NULL
685 686 687
	},

	{
688 689 690 691 692 693 694
		{ "seed", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL },
		&phony_random_seed,
		0.5, 0.0, 1.0, assign_random_seed, show_random_seed
	},

	{
		{ NULL, 0 }, NULL, 0.0, 0.0, 0.0, NULL, NULL
695
	}
696 697 698 699
};


static struct config_string
B
Bruce Momjian 已提交
700
			ConfigureNamesString[] =
701
{
702
	{
703 704
		{ "client_encoding", PGC_USERSET }, &client_encoding_string,
		"SQL_ASCII", assign_client_encoding, NULL
705 706
	},

707
	{
708 709
		{ "client_min_messages", PGC_USERSET }, &client_min_messages_str,
		client_min_messages_str_default, assign_client_min_messages, NULL
710
	},
711

712
	{
713 714
		{ "DateStyle", PGC_USERSET, GUC_LIST_INPUT }, &datestyle_string,
		"ISO, US", assign_datestyle, show_datestyle
715
	},
716

717
	{
718 719
		{ "default_transaction_isolation", PGC_USERSET }, &default_iso_level_string,
		"read committed", assign_defaultxactisolevel, NULL
720 721
	},

722
	{
723 724 725 726 727 728
		{ "dynamic_library_path", PGC_SUSET }, &Dynamic_library_path,
		"$libdir", NULL, NULL
	},

	{
		{ "krb_server_keyfile", PGC_POSTMASTER }, &pg_krb_server_keyfile,
729
		PG_KRB_SRVTAB, NULL, NULL
730
	},
731

732
	{
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
		{ "lc_messages", PGC_SUSET }, &locale_messages,
		"", locale_messages_assign, NULL
	},

	{
		{ "lc_monetary", PGC_USERSET }, &locale_monetary,
		"", locale_monetary_assign, NULL
	},

	{
		{ "lc_numeric", PGC_USERSET }, &locale_numeric,
		"", locale_numeric_assign, NULL
	},

	{
		{ "lc_time", PGC_USERSET }, &locale_time,
		"", locale_time_assign, NULL
750 751 752
	},

	{
753 754 755
		{ "search_path", PGC_USERSET, GUC_LIST_INPUT | GUC_LIST_QUOTE },
		&namespace_search_path,
		"$user,public", assign_search_path, NULL
756 757 758
	},

	{
759 760
		{ "server_encoding", PGC_USERSET }, &server_encoding_string,
		"SQL_ASCII", assign_server_encoding, show_server_encoding
761 762 763
	},

	{
764 765
		{ "server_min_messages", PGC_USERSET }, &server_min_messages_str,
		server_min_messages_str_default, assign_server_min_messages, NULL
766 767
	},

768
	{
769 770 771
		{ "session_authorization", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL },
		&session_authorization_string,
		NULL, assign_session_authorization, show_session_authorization
772 773
	},

T
Tatsuo Ishii 已提交
774
#ifdef HAVE_SYSLOG
775
	{
776 777
		{ "syslog_facility", PGC_POSTMASTER }, &Syslog_facility,
		"LOCAL0", assign_facility, NULL
778 779
	},
	{
780
		{ "syslog_ident", PGC_POSTMASTER }, &Syslog_ident,
781
		"postgres", NULL, NULL
782
	},
783
#endif
784

785
	{
786 787 788 789 790 791 792 793 794 795 796 797
		{ "TimeZone", PGC_USERSET }, &timezone_string,
		"UNKNOWN", assign_timezone, show_timezone
	},

	{
		{ "TRANSACTION ISOLATION LEVEL", PGC_USERSET, GUC_NO_RESET_ALL },
		&XactIsoLevel_string,
		NULL, assign_XactIsoLevel, show_XactIsoLevel
	},

	{
		{ "unix_socket_group", PGC_POSTMASTER }, &Unix_socket_group,
798
		"", NULL, NULL
799
	},
800

801
	{
802
		{ "unix_socket_directory", PGC_POSTMASTER }, &UnixSocketDir,
803
		"", NULL, NULL
804
	},
805

806
	{
807
		{ "virtual_host", PGC_POSTMASTER }, &VirtualHost,
808
		"", NULL, NULL
809
	},
810

811
	{
812 813
		{ "wal_sync_method", PGC_SIGHUP }, &XLOG_sync_method,
		XLOG_sync_method_default, assign_xlog_sync_method, NULL
814
	},
815

816
	{
817
		{ NULL, 0 }, NULL, NULL, NULL, NULL
818
	}
819 820 821 822 823 824
};

/******** end of options list ********/


/*
825
 * Actual lookup of variables is done through this single, sorted array.
826
 */
827 828
static struct config_generic **guc_variables;
static int num_guc_variables;
829

830
static bool guc_dirty;			/* TRUE if need to do commit/abort work */
831

832
static char *guc_string_workspace; /* for avoiding memory leaks */
833

834

835
static int guc_var_compare(const void *a, const void *b);
836
static char *_ShowOption(struct config_generic *record);
837 838 839


/*
840 841 842
 * Build the sorted array.  This is split out so that it could be
 * re-executed after startup (eg, we could allow loadable modules to
 * add vars, and then we'd need to re-sort).
843
 */
844 845
static void
build_guc_variables(void)
846
{
847 848
	int			num_vars = 0;
	struct config_generic **guc_vars;
B
Bruce Momjian 已提交
849
	int			i;
850

851
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
852 853 854
	{
		struct config_bool *conf = &ConfigureNamesBool[i];

855 856 857
		/* Rather than requiring vartype to be filled in by hand, do this: */
		conf->gen.vartype = PGC_BOOL;
		num_vars++;
858
	}
859

860
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
861 862 863
	{
		struct config_int *conf = &ConfigureNamesInt[i];

864 865
		conf->gen.vartype = PGC_INT;
		num_vars++;
866
	}
867

868
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
869 870 871
	{
		struct config_real *conf = &ConfigureNamesReal[i];

872 873
		conf->gen.vartype = PGC_REAL;
		num_vars++;
874
	}
875

876
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
877
	{
878
		struct config_string *conf = &ConfigureNamesString[i];
879

880 881
		conf->gen.vartype = PGC_STRING;
		num_vars++;
882 883
	}

884 885 886 887
	guc_vars = (struct config_generic **)
		malloc(num_vars * sizeof(struct config_generic *));
	if (!guc_vars)
		elog(PANIC, "out of memory");
888

889
	num_vars = 0;
890

891 892
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesBool[i].gen;
893

894 895
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesInt[i].gen;
896

897 898
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesReal[i].gen;
899

900 901
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesString[i].gen;
902

903 904
	qsort((void *) guc_vars, num_vars, sizeof(struct config_generic *),
		  guc_var_compare);
905

906 907 908 909
	if (guc_variables)
		free(guc_variables);
	guc_variables = guc_vars;
	num_guc_variables = num_vars;
910 911 912 913
}


/*
914 915
 * Look up option NAME. If it exists, return a pointer to its record,
 * else return NULL.
916
 */
917 918
static struct config_generic *
find_option(const char *name)
919
{
920 921
	const char **key = &name;
	struct config_generic **res;
922

923
	Assert(name);
924

925 926 927 928 929 930 931 932 933 934 935 936 937
	/*
	 * by equating const char ** with struct config_generic *, we are
	 * assuming the name field is first in config_generic.
	 */
	res = (struct config_generic**) bsearch((void *) &key,
											(void *) guc_variables,
											num_guc_variables,
											sizeof(struct config_generic *),
											guc_var_compare);
	if (res)
		return *res;
	return NULL;
}
938 939 940


/*
941
 * comparator for qsorting and bsearching guc_variables array
942
 */
943 944
static int
guc_var_compare(const void *a, const void *b)
945
{
946 947
	struct config_generic *confa = *(struct config_generic **) a;
	struct config_generic *confb = *(struct config_generic **) b;
948 949
	const char *namea;
	const char *nameb;
950

951 952 953
	/*
	 * The temptation to use strcasecmp() here must be resisted, because
	 * the array ordering has to remain stable across setlocale() calls.
954
	 * So, build our own with a simple ASCII-only downcasing.
955
	 */
956 957 958 959 960 961 962 963 964 965 966 967 968
	namea = confa->name;
	nameb = confb->name;
	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;
969
	}
970 971 972 973 974
	if (*namea)
		return 1;				/* a is longer */
	if (*nameb)
		return -1;				/* b is longer */
	return 0;
975
}
976 977 978


/*
979
 * Initialize GUC options during program startup.
980
 */
981 982
void
InitializeGUCOptions(void)
983
{
984 985
	int			i;
	char	   *env;
986

987 988 989 990
	/*
	 * Build sorted array of all GUC variables.
	 */
	build_guc_variables();
991

992
	/*
993 994 995 996 997 998
	 * Load all variables with their compiled-in defaults, and initialize
	 * status fields as needed.
	 *
	 * Note: any errors here are reported with plain ol' printf, since we
	 * shouldn't assume that elog will work before we've initialized its
	 * config variables.  An error here would be unexpected anyway...
999
	 */
1000
	for (i = 0; i < num_guc_variables; i++)
1001
	{
1002
		struct config_generic *gconf = guc_variables[i];
1003

1004 1005 1006 1007 1008 1009 1010 1011 1012
		gconf->status = 0;
		gconf->reset_source = PGC_S_DEFAULT;
		gconf->session_source = PGC_S_DEFAULT;
		gconf->tentative_source = PGC_S_DEFAULT;
		gconf->source = PGC_S_DEFAULT;

		switch (gconf->vartype)
		{
			case PGC_BOOL:
1013
			{
1014 1015 1016 1017
				struct config_bool *conf = (struct config_bool *) gconf;

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, false))
1018 1019
						fprintf(stderr, "Failed to initialize %s to %d\n",
								conf->gen.name, (int) conf->reset_val);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
				*conf->variable = conf->reset_val;
				conf->session_val = conf->reset_val;
				break;
			}
			case PGC_INT:
			{
				struct config_int *conf = (struct config_int *) gconf;

				Assert(conf->reset_val >= conf->min);
				Assert(conf->reset_val <= conf->max);
				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, false))
1032 1033
						fprintf(stderr, "Failed to initialize %s to %d\n",
								conf->gen.name, conf->reset_val);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
				*conf->variable = conf->reset_val;
				conf->session_val = conf->reset_val;
				break;
			}
			case PGC_REAL:
			{
				struct config_real *conf = (struct config_real *) gconf;

				Assert(conf->reset_val >= conf->min);
				Assert(conf->reset_val <= conf->max);
				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, false))
1046 1047
						fprintf(stderr, "Failed to initialize %s to %g\n",
								conf->gen.name, conf->reset_val);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
				*conf->variable = conf->reset_val;
				conf->session_val = conf->reset_val;
				break;
			}
			case PGC_STRING:
			{
				struct config_string *conf = (struct config_string *) gconf;
				char	   *str;

				*conf->variable = NULL;
				conf->reset_val = NULL;
				conf->session_val = NULL;
				conf->tentative_val = NULL;

				if (conf->boot_val == NULL)
				{
					/* Cannot set value yet */
					break;
				}

				str = strdup(conf->boot_val);
				if (str == NULL)
					elog(PANIC, "out of memory");
				conf->reset_val = str;

				if (conf->assign_hook)
				{
					const char   *newstr;

					newstr = (*conf->assign_hook) (str, true, false);
					if (newstr == NULL)
					{
1080 1081
						fprintf(stderr, "Failed to initialize %s to '%s'\n",
								conf->gen.name, str);
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
					}
					else if (newstr != str)
					{
						free(str);
						/* See notes in set_config_option about casting */
						str = (char *) newstr;
						conf->reset_val = str;
					}
				}
				*conf->variable = str;
				conf->session_val = str;
				break;
			}
		}
	}

	guc_dirty = false;

	guc_string_workspace = NULL;

	/*
	 * Prevent any attempt to override TRANSACTION ISOLATION LEVEL from
	 * non-interactive sources.
	 */
	SetConfigOption("TRANSACTION ISOLATION LEVEL", "default",
					PGC_POSTMASTER, PGC_S_OVERRIDE);

	/*
	 * For historical reasons, some GUC parameters can receive defaults
	 * from environment variables.  Process those settings.
	 */

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

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

	env = getenv("TZ");
	if (env != NULL)
		SetConfigOption("timezone", env, PGC_POSTMASTER, PGC_S_ENV_VAR);

	env = getenv("PGCLIENTENCODING");
	if (env != NULL)
		SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
}


/*
 * 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 */
		if (gconf->context != PGC_SUSET && gconf->context != PGC_USERSET)
			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;

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

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, true))
						elog(ERROR, "Failed to reset %s", conf->gen.name);
				*conf->variable = conf->reset_val;
				conf->tentative_val = conf->reset_val;
				conf->gen.source = conf->gen.reset_source;
				conf->gen.tentative_source = conf->gen.reset_source;
				conf->gen.status |= GUC_HAVE_TENTATIVE;
				guc_dirty = true;
				break;
			}
			case PGC_INT:
			{
				struct config_int *conf = (struct config_int *) gconf;

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, true))
						elog(ERROR, "Failed to reset %s", conf->gen.name);
				*conf->variable = conf->reset_val;
				conf->tentative_val = conf->reset_val;
				conf->gen.source = conf->gen.reset_source;
				conf->gen.tentative_source = conf->gen.reset_source;
				conf->gen.status |= GUC_HAVE_TENTATIVE;
				guc_dirty = true;
				break;
			}
			case PGC_REAL:
			{
				struct config_real *conf = (struct config_real *) gconf;

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, true))
						elog(ERROR, "Failed to reset %s", conf->gen.name);
				*conf->variable = conf->reset_val;
				conf->tentative_val = conf->reset_val;
				conf->gen.source = conf->gen.reset_source;
				conf->gen.tentative_source = conf->gen.reset_source;
				conf->gen.status |= GUC_HAVE_TENTATIVE;
				guc_dirty = true;
				break;
			}
			case PGC_STRING:
			{
				struct config_string *conf = (struct config_string *) gconf;
				char	   *str;

				if (conf->reset_val == NULL)
				{
					/* Nothing to reset to, as yet; so do nothing */
					break;
				}

1212 1213
				/* We need not strdup here */
				str = conf->reset_val;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

				if (conf->assign_hook)
				{
					const char   *newstr;

					newstr = (*conf->assign_hook) (str, true, true);
					if (newstr == NULL)
						elog(ERROR, "Failed to reset %s", conf->gen.name);
					else if (newstr != str)
					{
						/* See notes in set_config_option about casting */
						str = (char *) newstr;
					}
				}

				SET_STRING_VARIABLE(conf, str);
				SET_STRING_TENTATIVE_VAL(conf, str);
				conf->gen.source = conf->gen.reset_source;
				conf->gen.tentative_source = conf->gen.reset_source;
				conf->gen.status |= GUC_HAVE_TENTATIVE;
				guc_dirty = true;
				break;
			}
		}
	}
}


/*
 * Do GUC processing at transaction commit or abort.
 */
void
AtEOXact_GUC(bool isCommit)
{
	int			i;

	/* Quick exit if nothing's changed in this transaction */
	if (!guc_dirty)
		return;

	/* Prevent memory leak if elog during an assign_hook */
	if (guc_string_workspace)
	{
		free(guc_string_workspace);
		guc_string_workspace = NULL;
	}

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

		/* Skip if nothing's happened to this var in this transaction */
		if (gconf->status == 0)
			continue;

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

				if (isCommit && (conf->gen.status & GUC_HAVE_TENTATIVE))
				{
					conf->session_val = conf->tentative_val;
					conf->gen.session_source = conf->gen.tentative_source;
				}

				if (*conf->variable != conf->session_val)
				{
					if (conf->assign_hook)
						if (!(*conf->assign_hook) (conf->session_val,
												   true, false))
							elog(LOG, "Failed to commit %s", conf->gen.name);
					*conf->variable = conf->session_val;
				}
				conf->gen.source = conf->gen.session_source;
				conf->gen.status = 0;
				break;
			}
			case PGC_INT:
			{
				struct config_int *conf = (struct config_int *) gconf;

				if (isCommit && (conf->gen.status & GUC_HAVE_TENTATIVE))
				{
					conf->session_val = conf->tentative_val;
					conf->gen.session_source = conf->gen.tentative_source;
				}

				if (*conf->variable != conf->session_val)
				{
					if (conf->assign_hook)
						if (!(*conf->assign_hook) (conf->session_val,
												   true, false))
							elog(LOG, "Failed to commit %s", conf->gen.name);
					*conf->variable = conf->session_val;
				}
				conf->gen.source = conf->gen.session_source;
				conf->gen.status = 0;
				break;
			}
			case PGC_REAL:
			{
				struct config_real *conf = (struct config_real *) gconf;

				if (isCommit && (conf->gen.status & GUC_HAVE_TENTATIVE))
				{
					conf->session_val = conf->tentative_val;
					conf->gen.session_source = conf->gen.tentative_source;
				}

				if (*conf->variable != conf->session_val)
				{
					if (conf->assign_hook)
						if (!(*conf->assign_hook) (conf->session_val,
												   true, false))
							elog(LOG, "Failed to commit %s", conf->gen.name);
					*conf->variable = conf->session_val;
				}
				conf->gen.source = conf->gen.session_source;
				conf->gen.status = 0;
				break;
			}
			case PGC_STRING:
			{
				struct config_string *conf = (struct config_string *) gconf;

				if (isCommit && (conf->gen.status & GUC_HAVE_TENTATIVE))
				{
					SET_STRING_SESSION_VAL(conf, conf->tentative_val);
					conf->gen.session_source = conf->gen.tentative_source;
					conf->tentative_val = NULL;	/* transfer ownership */
				}
				else
				{
					SET_STRING_TENTATIVE_VAL(conf, NULL);
				}

				if (*conf->variable != conf->session_val)
				{
					char	   *str = conf->session_val;

					if (conf->assign_hook)
					{
						const char   *newstr;

						newstr = (*conf->assign_hook) (str, true, false);
						if (newstr == NULL)
							elog(LOG, "Failed to commit %s", conf->gen.name);
						else if (newstr != str)
						{
							/* See notes in set_config_option about casting */
							str = (char *) newstr;
							SET_STRING_SESSION_VAL(conf, str);
						}
					}

					SET_STRING_VARIABLE(conf, str);
				}
				conf->gen.source = conf->gen.session_source;
				conf->gen.status = 0;
				break;
			}
		}
	}

	guc_dirty = false;
}


/*
 * Try to interpret value as boolean value.  Valid values are: true,
 * false, yes, no, on, off, 1, 0.  If the string parses okay, return
 * true, else false.  If result is not NULL, return the parsing result
 * there.
 */
static bool
parse_bool(const char *value, bool *result)
{
	size_t		len = strlen(value);

	if (strncasecmp(value, "true", len) == 0)
	{
		if (result)
			*result = true;
	}
	else if (strncasecmp(value, "false", len) == 0)
	{
		if (result)
			*result = false;
	}

	else if (strncasecmp(value, "yes", len) == 0)
	{
		if (result)
			*result = true;
	}
	else if (strncasecmp(value, "no", len) == 0)
	{
		if (result)
			*result = false;
	}

	else if (strcasecmp(value, "on") == 0)
	{
		if (result)
			*result = true;
	}
	else if (strcasecmp(value, "off") == 0)
	{
		if (result)
			*result = false;
	}

	else if (strcasecmp(value, "1") == 0)
	{
		if (result)
			*result = true;
	}
	else if (strcasecmp(value, "0") == 0)
	{
		if (result)
			*result = false;
	}

	else
		return false;
	return true;
}



/*
 * Try to parse value as an integer.  The accepted formats are the
 * usual decimal, octal, or hexadecimal formats.  If the string parses
 * okay, return true, else false.  If result is not NULL, return the
 * value there.
 */
static bool
parse_int(const char *value, int *result)
{
	long		val;
	char	   *endptr;

	errno = 0;
	val = strtol(value, &endptr, 0);
	if (endptr == value || *endptr != '\0' || errno == ERANGE
#ifdef HAVE_LONG_INT_64
	/* if long > 32 bits, check for overflow of int4 */
		|| val != (long) ((int32) val)
#endif
		)
		return false;
	if (result)
		*result = (int) val;
	return true;
}



/*
 * Try to parse value as a floating point constant in the usual
 * format.	If the value parsed okay return true, else false.  If
 * result is not NULL, return the semantic value there.
 */
static bool
parse_real(const char *value, double *result)
{
	double		val;
	char	   *endptr;

	errno = 0;
	val = strtod(value, &endptr);
	if (endptr == value || *endptr != '\0' || errno == ERANGE)
		return false;
	if (result)
		*result = val;
	return true;
}



/*
 * 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.
 *
 * If value is NULL, set the option to its default value. If the
 * parameter DoIt is false then don't really set the option but do all
 * the checks to see if it would work.
 *
 * If there is an error (non-existing option, invalid value) then an
 * elog(ERROR) is thrown *unless* this is called in a context where we
 * don't want to elog (currently, startup or SIGHUP config file reread).
 * In that case we write a suitable error message via elog(DEBUG) and
 * return false. This is working around the deficiencies in the elog
 * 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.
 */
bool
set_config_option(const char *name, const char *value,
				  GucContext context, GucSource source,
				  bool isLocal, bool DoIt)
{
	struct config_generic *record;
	int			elevel;
	bool		interactive;
	bool		makeDefault;

	if (context == PGC_SIGHUP || source == PGC_S_DEFAULT)
		elevel = DEBUG1;
	else if (source == PGC_S_DATABASE || source == PGC_S_USER)
		elevel = INFO;
	else
		elevel = ERROR;

	record = find_option(name);
	if (record == NULL)
	{
		elog(elevel, "'%s' is not a valid option name", name);
		return false;
	}

	/*
	 * Check if the option can be set at this time. See guc.h for the
	 * precise rules. Note that we don't want to throw errors if we're in
	 * the SIGHUP context. In that case we just ignore the attempt and
	 * return true.
	 */
	switch (record->context)
	{
		case PGC_POSTMASTER:
			if (context == PGC_SIGHUP)
				return true;
			if (context != PGC_POSTMASTER)
			{
				elog(elevel, "'%s' cannot be changed after server start",
					 name);
				return false;
			}
			break;
		case PGC_SIGHUP:
			if (context != PGC_SIGHUP && context != PGC_POSTMASTER)
			{
				elog(elevel, "'%s' cannot be changed now", name);
				return false;
			}

			/*
			 * 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.
			 */
			break;
		case PGC_BACKEND:
			if (context == PGC_SIGHUP)
			{
				/*
				 * 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.
				 */
				if (IsUnderPostmaster)
					return true;
			}
			else if (context != PGC_BACKEND && context != PGC_POSTMASTER)
			{
				elog(elevel, "'%s' cannot be set after connection start",
					 name);
				return false;
			}
			break;
		case PGC_SUSET:
			if (context == PGC_USERSET || context == PGC_BACKEND)
			{
				elog(elevel, "'%s': permission denied", name);
				return false;
1600 1601 1602 1603 1604
			}
			break;
		case PGC_USERSET:
			/* always okay */
			break;
1605
	}
1606

1607
	/* Should we report errors interactively? */
1608
	interactive = (source >= PGC_S_SESSION);
1609 1610 1611 1612 1613
	/*
	 * Should we set reset/session values?  (If so, the behavior is not
	 * transactional.)
	 */
	makeDefault = DoIt && (source <= PGC_S_OVERRIDE) && (value != NULL);
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

	/*
	 * Ignore attempted set if overridden by previously processed setting.
	 * However, if DoIt 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/session values even if we can't set the variable itself.
	 */
	if (record->source > source)
	{
		if (DoIt && !makeDefault)
		{
1626
			elog(DEBUG2, "%s: setting ignored because previous source is higher priority",
1627 1628 1629 1630 1631 1632
				 name);
			return true;
		}
		DoIt = false;			/* we won't change the variable itself */
	}

1633 1634 1635
	/*
	 * Evaluate value and set variable
	 */
1636
	switch (record->vartype)
1637 1638 1639
	{
		case PGC_BOOL:
			{
B
Bruce Momjian 已提交
1640
				struct config_bool *conf = (struct config_bool *) record;
1641
				bool		newval;
B
Bruce Momjian 已提交
1642 1643

				if (value)
1644
				{
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
					if (!parse_bool(value, &newval))
					{
						elog(elevel, "option '%s' requires a boolean value",
							 name);
						return false;
					}
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}
B
Bruce Momjian 已提交
1657

1658 1659
				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
B
Bruce Momjian 已提交
1660
					{
1661 1662
						elog(elevel, "invalid value for option '%s': %d",
							 name, (int) newval);
B
Bruce Momjian 已提交
1663 1664
						return false;
					}
1665 1666 1667

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1668
					if (DoIt)
1669
					{
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
						*conf->variable = newval;
						conf->gen.source = source;
					}
					if (makeDefault)
					{
						if (conf->gen.reset_source <= source)
						{
							conf->reset_val = newval;
							conf->gen.reset_source = source;
						}
						if (conf->gen.session_source <= source)
						{
							conf->session_val = newval;
							conf->gen.session_source = source;
						}
					}
					else if (isLocal)
					{
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
					}
					else
					{
						conf->tentative_val = newval;
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
1697
					}
1698
				}
B
Bruce Momjian 已提交
1699
				break;
1700
			}
1701 1702

		case PGC_INT:
1703
			{
B
Bruce Momjian 已提交
1704
				struct config_int *conf = (struct config_int *) record;
1705
				int			newval;
1706

B
Bruce Momjian 已提交
1707
				if (value)
1708
				{
1709
					if (!parse_int(value, &newval))
B
Bruce Momjian 已提交
1710
					{
1711 1712
						elog(elevel, "option '%s' expects an integer value",
							 name);
B
Bruce Momjian 已提交
1713 1714
						return false;
					}
1715
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1716 1717 1718
					{
						elog(elevel, "option '%s' value %d is outside"
							 " of permissible range [%d .. %d]",
1719
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1720 1721
						return false;
					}
1722 1723 1724 1725 1726 1727 1728 1729 1730
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1731 1732
					{
						elog(elevel, "invalid value for option '%s': %d",
1733
							 name, newval);
1734 1735
						return false;
					}
1736 1737 1738

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1739
					if (DoIt)
1740
					{
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
						*conf->variable = newval;
						conf->gen.source = source;
					}
					if (makeDefault)
					{
						if (conf->gen.reset_source <= source)
						{
							conf->reset_val = newval;
							conf->gen.reset_source = source;
						}
						if (conf->gen.session_source <= source)
						{
							conf->session_val = newval;
							conf->gen.session_source = source;
						}
					}
					else if (isLocal)
					{
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
					}
					else
					{
						conf->tentative_val = newval;
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
1768
					}
1769
				}
B
Bruce Momjian 已提交
1770
				break;
1771
			}
1772 1773

		case PGC_REAL:
1774
			{
B
Bruce Momjian 已提交
1775
				struct config_real *conf = (struct config_real *) record;
1776
				double		newval;
1777

B
Bruce Momjian 已提交
1778
				if (value)
1779
				{
1780
					if (!parse_real(value, &newval))
B
Bruce Momjian 已提交
1781
					{
1782 1783
						elog(elevel, "option '%s' expects a real number",
							 name);
B
Bruce Momjian 已提交
1784 1785
						return false;
					}
1786
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1787 1788 1789
					{
						elog(elevel, "option '%s' value %g is outside"
							 " of permissible range [%g .. %g]",
1790
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1791 1792
						return false;
					}
1793 1794 1795 1796 1797 1798 1799 1800 1801
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1802 1803
					{
						elog(elevel, "invalid value for option '%s': %g",
1804
							 name, newval);
1805 1806
						return false;
					}
1807 1808 1809

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1810
					if (DoIt)
1811
					{
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
						*conf->variable = newval;
						conf->gen.source = source;
					}
					if (makeDefault)
					{
						if (conf->gen.reset_source <= source)
						{
							conf->reset_val = newval;
							conf->gen.reset_source = source;
						}
						if (conf->gen.session_source <= source)
						{
							conf->session_val = newval;
							conf->gen.session_source = source;
						}
					}
					else if (isLocal)
					{
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
					}
					else
					{
						conf->tentative_val = newval;
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
1839
					}
1840
				}
B
Bruce Momjian 已提交
1841
				break;
1842
			}
1843 1844 1845

		case PGC_STRING:
			{
B
Bruce Momjian 已提交
1846
				struct config_string *conf = (struct config_string *) record;
1847
				char	   *newval;
B
Bruce Momjian 已提交
1848 1849

				if (value)
1850
				{
1851 1852 1853 1854 1855 1856 1857 1858 1859
					newval = strdup(value);
					if (newval == NULL)
					{
						elog(elevel, "out of memory");
						return false;
					}
				}
				else if (conf->reset_val)
				{
1860 1861 1862 1863 1864
					/*
					 * We could possibly avoid strdup here, but easier to
					 * make this case work the same as the normal assignment
					 * case.
					 */
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
					newval = strdup(conf->reset_val);
					if (newval == NULL)
					{
						elog(elevel, "out of memory");
						return false;
					}
					source = conf->gen.reset_source;
				}
				else
				{
					/* Nothing to reset to, as yet; so do nothing */
					break;
				}

				/*
				 * Remember string in workspace, so that we can free it
				 * and avoid a permanent memory leak if hook elogs.
				 */
				if (guc_string_workspace)
					free(guc_string_workspace);
				guc_string_workspace = newval;

				if (conf->assign_hook)
				{
					const char   *hookresult;

					hookresult = (*conf->assign_hook) (newval,
													   DoIt, interactive);
					guc_string_workspace = NULL;
					if (hookresult == NULL)
B
Bruce Momjian 已提交
1895
					{
1896
						free(newval);
1897
						elog(elevel, "invalid value for option '%s': '%s'",
1898
							 name, value ? value : "");
B
Bruce Momjian 已提交
1899 1900
						return false;
					}
1901
					else if (hookresult != newval)
B
Bruce Momjian 已提交
1902
					{
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
						free(newval);
						/*
						 * Having to cast away const here is annoying, but the
						 * alternative is to declare assign_hooks as returning
						 * char*, which would mean they'd have to cast away
						 * const, or as both taking and returning char*, which
						 * doesn't seem attractive either --- we don't want
						 * them to scribble on the passed str.
						 */
						newval = (char *) hookresult;
					}
				}

				guc_string_workspace = NULL;
B
Bruce Momjian 已提交
1917

1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
				if (DoIt || makeDefault)
				{
					if (DoIt)
					{
						SET_STRING_VARIABLE(conf, newval);
						conf->gen.source = source;
					}
					if (makeDefault)
					{
						if (conf->gen.reset_source <= source)
B
Bruce Momjian 已提交
1928
						{
1929 1930
							SET_STRING_RESET_VAL(conf, newval);
							conf->gen.reset_source = source;
B
Bruce Momjian 已提交
1931
						}
1932
						if (conf->gen.session_source <= source)
1933
						{
1934 1935
							SET_STRING_SESSION_VAL(conf, newval);
							conf->gen.session_source = source;
1936
						}
1937 1938 1939 1940 1941
						/* Perhaps we didn't install newval anywhere */
						if (newval != *conf->variable &&
							newval != conf->session_val &&
							newval != conf->reset_val)
							free(newval);
B
Bruce Momjian 已提交
1942
					}
1943
					else if (isLocal)
1944
					{
1945 1946
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
1947
					}
1948
					else
1949
					{
1950 1951 1952 1953
						SET_STRING_TENTATIVE_VAL(conf, newval);
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
1954
					}
1955 1956 1957 1958
				}
				else
				{
					free(newval);
1959
				}
B
Bruce Momjian 已提交
1960
				break;
1961
			}
1962
	}
1963

1964 1965 1966 1967 1968 1969 1970
	return true;
}



/*
 * Set a config option to the given value. See also set_config_option,
1971 1972
 * this is just the wrapper to be called from outside GUC.  NB: this
 * is used only for non-transactional operations.
1973 1974
 */
void
1975
SetConfigOption(const char *name, const char *value,
1976
				GucContext context, GucSource source)
1977
{
1978
	(void) set_config_option(name, value, context, source, false, true);
1979 1980 1981 1982 1983
}



/*
1984 1985
 * Fetch the current value of the option `name'. If the option doesn't exist,
 * throw an elog and don't return.
1986 1987 1988 1989 1990
 *
 * The string is *not* allocated for modification and is really only
 * valid until the next call to configuration related functions.
 */
const char *
B
Bruce Momjian 已提交
1991
GetConfigOption(const char *name)
1992
{
B
Bruce Momjian 已提交
1993
	struct config_generic *record;
1994 1995
	static char buffer[256];

1996 1997
	record = find_option(name);
	if (record == NULL)
1998
		elog(ERROR, "Option '%s' is not recognized", name);
1999

2000
	switch (record->vartype)
2001 2002
	{
		case PGC_BOOL:
B
Bruce Momjian 已提交
2003
			return *((struct config_bool *) record)->variable ? "on" : "off";
2004

2005
		case PGC_INT:
2006 2007
			snprintf(buffer, sizeof(buffer), "%d",
					 *((struct config_int *) record)->variable);
2008 2009
			return buffer;

2010
		case PGC_REAL:
2011 2012
			snprintf(buffer, sizeof(buffer), "%g",
					 *((struct config_real *) record)->variable);
2013 2014 2015
			return buffer;

		case PGC_STRING:
B
Bruce Momjian 已提交
2016
			return *((struct config_string *) record)->variable;
2017 2018 2019
	}
	return NULL;
}
2020

2021 2022 2023 2024 2025
/*
 * Get the RESET value associated with the given option.
 */
const char *
GetConfigOptionResetString(const char *name)
2026
{
2027 2028
	struct config_generic *record;
	static char buffer[256];
2029

2030 2031 2032 2033 2034
	record = find_option(name);
	if (record == NULL)
		elog(ERROR, "Option '%s' is not recognized", name);

	switch (record->vartype)
2035 2036
	{
		case PGC_BOOL:
2037
			return ((struct config_bool *) record)->reset_val ? "on" : "off";
2038

2039
		case PGC_INT:
2040
			snprintf(buffer, sizeof(buffer), "%d",
2041 2042
					 ((struct config_int *) record)->reset_val);
			return buffer;
2043 2044

		case PGC_REAL:
2045
			snprintf(buffer, sizeof(buffer), "%g",
2046 2047
					 ((struct config_real *) record)->reset_val);
			return buffer;
2048 2049

		case PGC_STRING:
2050 2051 2052 2053
			return ((struct config_string *) record)->reset_val;
	}
	return NULL;
}
2054

2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153


/*
 * 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).
 *
 * The result is NULL if input is NIL (ie, SET ... TO DEFAULT), otherwise
 * a palloc'd string.
 */
char *
flatten_set_variable_args(const char *name, List *args)
{
	struct config_generic *record;
	int			flags;
	StringInfoData buf;
	List		*l;

	/* Fast path if just DEFAULT */
	if (args == NIL)
		return NULL;

	record = find_option(name);
	if (record == NULL)
		flags = 0;				/* default assumptions */
	else
		flags = record->flags;

	/* Complain if list input and non-list variable */
	if ((flags & GUC_LIST_INPUT) == 0 &&
		lnext(args) != NIL)
		elog(ERROR, "SET %s takes only one argument", name);

	initStringInfo(&buf);

	foreach(l, args)
	{
		A_Const    *arg = (A_Const *) lfirst(l);
		char	   *val;

		if (l != args)
			appendStringInfo(&buf, ", ");

		if (!IsA(arg, A_Const))
			elog(ERROR, "flatten_set_variable_args: unexpected input");

		switch (nodeTag(&arg->val))
		{
			case T_Integer:
				appendStringInfo(&buf, "%ld", intVal(&arg->val));
				break;
			case T_Float:
				/* represented as a string, so just copy it */
				appendStringInfo(&buf, "%s", strVal(&arg->val));
				break;
			case T_String:
				val = strVal(&arg->val);
				if (arg->typename != NULL)
				{
					/*
					 * Must be a ConstInterval argument for TIME ZONE.
					 * Coerce to interval and back to normalize the value
					 * and account for any typmod.
					 */
					Datum	interval;
					char   *intervalout;

					interval =
						DirectFunctionCall3(interval_in,
											CStringGetDatum(val),
											ObjectIdGetDatum(InvalidOid),
											Int32GetDatum(arg->typename->typmod));

					intervalout =
						DatumGetCString(DirectFunctionCall3(interval_out,
															interval,
															ObjectIdGetDatum(InvalidOid),
															Int32GetDatum(-1)));
					appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
				}
				else
				{
					/*
					 * Plain string literal or identifier.  For quote mode,
					 * quote it if it's not a vanilla identifier.
					 */
					if (flags & GUC_LIST_QUOTE)
						appendStringInfo(&buf, "%s", quote_identifier(val));
					else
						appendStringInfo(&buf, "%s", val);
				}
				break;
			default:
				elog(ERROR, "flatten_set_variable_args: unexpected input");
				break;
		}
2154
	}
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176

	return buf.data;
}


/*
 * SET command
 */
void
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,
					  is_local,
					  true);
}

2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
/*
 * SET command wrapped as a SQL callable function.
 */
Datum
set_config_by_name(PG_FUNCTION_ARGS)
{
	char   *name;
	char   *value;
	char   *new_value;
	bool	is_local;
	text   *result_text;

	if (PG_ARGISNULL(0))
		elog(ERROR, "SET variable name is required");

	/* Get the GUC variable name */
2193
	name = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2194 2195 2196 2197 2198

	/* Get the desired value or set to NULL for a reset request */
	if (PG_ARGISNULL(1))
		value = NULL;
	else
2199
		value = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(1)));
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218

	/*
	 * Get the desired state of is_local. Default to false
	 * if provided value is NULL
	 */
	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,
					  is_local,
					  true);

	/* get the new current value */
2219
	new_value = GetConfigOptionByName(name, NULL);
2220 2221 2222 2223 2224 2225 2226 2227

	/* Convert return string to text */
	result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(new_value)));

	/* return it */
	PG_RETURN_TEXT_P(result_text);
}

2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
/*
 * SHOW command
 */
void
GetPGVariable(const char *name)
{
	if (strcasecmp(name, "all") == 0)
		ShowAllGUCConfig();
	else
		ShowGUCConfigOption(name);
}

/*
 * RESET command
 */
void
ResetPGVariable(const char *name)
{
	if (strcasecmp(name, "all") == 0)
		ResetAllOptions();
	else
		set_config_option(name,
						  NULL,
						  (superuser() ? PGC_SUSET : PGC_USERSET),
						  PGC_S_SESSION,
						  false,
						  true);
}


/*
 * SHOW command
 */
void
ShowGUCConfigOption(const char *name)
{
2264 2265 2266
	TupOutputState *tstate;
	TupleDesc		tupdesc;
	CommandDest		dest = whereToSendOutput;
2267
	const char	   *varname;
2268
	char		   *value;
2269

2270 2271 2272
	/* Get the value and canonical spelling of name */
	value = GetConfigOptionByName(name, &varname);

2273
	/* need a tuple descriptor representing a single TEXT column */
2274
	tupdesc = CreateTemplateTupleDesc(1, WITHOUTOID);
2275
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, (char *) varname,
2276
					   TEXTOID, -1, 0, false);
2277

2278 2279 2280 2281
	/* prepare for projection of tuples */
	tstate = begin_tup_output_tupdesc(dest, tupdesc);

	/* Send it */
2282
	PROJECT_LINE_OF_TEXT(tstate, value);
2283 2284

	end_tup_output(tstate);
2285 2286
}

2287 2288 2289
/*
 * SHOW ALL command
 */
2290 2291 2292 2293
void
ShowAllGUCConfig(void)
{
	int			i;
2294 2295 2296 2297 2298 2299
	TupOutputState *tstate;
	TupleDesc		tupdesc;
	CommandDest		dest = whereToSendOutput;
	char		  *values[2];

	/* need a tuple descriptor representing two TEXT columns */
2300
	tupdesc = CreateTemplateTupleDesc(2, WITHOUTOID);
2301 2302 2303 2304 2305 2306 2307
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
					   TEXTOID, -1, 0, false);
	TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
					   TEXTOID, -1, 0, false);

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

2309 2310
	for (i = 0; i < num_guc_variables; i++)
	{
2311 2312 2313 2314
		struct config_generic *conf = guc_variables[i];

		if (conf->flags & GUC_NO_SHOW_ALL)
			continue;
2315 2316

		/* assign to the values array */
2317 2318
		values[0] = (char *) conf->name;
		values[1] = _ShowOption(conf);
2319 2320 2321

		/* send it to dest */
		do_tup_output(tstate, values);
2322

2323 2324 2325
		/* clean up */
		if (values[1] != NULL)
			pfree(values[1]);
2326
	}
2327 2328

	end_tup_output(tstate);
2329
}
2330

2331
/*
2332 2333
 * Return GUC variable value by name; optionally return canonical
 * form of name.  Return value is palloc'd.
2334 2335
 */
char *
2336
GetConfigOptionByName(const char *name, const char **varname)
2337 2338 2339 2340 2341 2342 2343
{
	struct config_generic *record;

	record = find_option(name);
	if (record == NULL)
		elog(ERROR, "Option '%s' is not recognized", name);

2344 2345 2346
	if (varname)
		*varname = record->name;

2347 2348 2349 2350
	return _ShowOption(record);
}

/*
2351 2352
 * Return GUC variable value by variable number; optionally return canonical
 * form of name.  Return value is palloc'd.
2353 2354
 */
char *
2355
GetConfigOptionByNum(int varnum, const char **varname, bool *noshow)
2356
{
2357 2358 2359 2360 2361 2362
	struct config_generic *conf;

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

	conf = guc_variables[varnum];
2363

2364 2365
	if (varname)
		*varname = conf->name;
2366

2367 2368 2369
	if (noshow)
		*noshow = (conf->flags & GUC_NO_SHOW_ALL) ? true : false;

2370
	return _ShowOption(conf);
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
}

/*
 * 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)
{
	char   *varname;
	char   *varval;
	text   *result_text;

	/* Get the GUC variable name */
2394
	varname = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2395 2396

	/* Get the value */
2397
	varval = GetConfigOptionByName(varname, NULL);
2398 2399 2400 2401 2402 2403 2404 2405 2406

	/* Convert to text */
	result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(varval)));

	/* return it */
	PG_RETURN_TEXT_P(result_text);
}

static char *
2407 2408 2409 2410
_ShowOption(struct config_generic *record)
{
	char		buffer[256];
	const char *val;
2411

2412 2413 2414 2415 2416
	switch (record->vartype)
	{
		case PGC_BOOL:
			{
				struct config_bool *conf = (struct config_bool *) record;
2417

2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
				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
				{
					snprintf(buffer, sizeof(buffer), "%d",
							 *conf->variable);
					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;
2458

2459 2460 2461 2462 2463 2464 2465 2466
				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else if (*conf->variable && **conf->variable)
					val = *conf->variable;
				else
					val = "unset";
			}
			break;
2467

2468 2469 2470 2471 2472
		default:
			/* just to keep compiler quiet */
			val = "???";
			break;
	}
2473

2474
	return pstrdup(val);
2475
}
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485


/*
 * 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 已提交
2486
ParseLongOption(const char *string, char **name, char **value)
2487
{
B
Bruce Momjian 已提交
2488 2489
	size_t		equal_pos;
	char	   *cp;
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508

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

	equal_pos = strcspn(string, "=");

	if (string[equal_pos] == '=')
	{
		*name = malloc(equal_pos + 1);
		if (!*name)
			elog(FATAL, "out of memory");
		strncpy(*name, string, equal_pos);
		(*name)[equal_pos] = '\0';

		*value = strdup(&string[equal_pos + 1]);
		if (!*value)
			elog(FATAL, "out of memory");
	}
B
Bruce Momjian 已提交
2509
	else
2510
	{
2511
		/* no equal sign in string */
2512 2513 2514 2515 2516 2517
		*name = strdup(string);
		if (!*name)
			elog(FATAL, "out of memory");
		*value = NULL;
	}

B
Bruce Momjian 已提交
2518
	for (cp = *name; *cp; cp++)
2519 2520 2521
		if (*cp == '-')
			*cp = '_';
}
2522 2523 2524



T
Tatsuo Ishii 已提交
2525
#ifdef HAVE_SYSLOG
2526

2527 2528
static const char *
assign_facility(const char *facility, bool doit, bool interactive)
2529
{
B
Bruce Momjian 已提交
2530
	if (strcasecmp(facility, "LOCAL0") == 0)
2531
		return facility;
B
Bruce Momjian 已提交
2532
	if (strcasecmp(facility, "LOCAL1") == 0)
2533
		return facility;
B
Bruce Momjian 已提交
2534
	if (strcasecmp(facility, "LOCAL2") == 0)
2535
		return facility;
B
Bruce Momjian 已提交
2536
	if (strcasecmp(facility, "LOCAL3") == 0)
2537
		return facility;
B
Bruce Momjian 已提交
2538
	if (strcasecmp(facility, "LOCAL4") == 0)
2539
		return facility;
B
Bruce Momjian 已提交
2540
	if (strcasecmp(facility, "LOCAL5") == 0)
2541
		return facility;
B
Bruce Momjian 已提交
2542
	if (strcasecmp(facility, "LOCAL6") == 0)
2543
		return facility;
B
Bruce Momjian 已提交
2544
	if (strcasecmp(facility, "LOCAL7") == 0)
2545 2546
		return facility;
	return NULL;
2547
}
2548

2549
#endif
2550 2551


2552 2553
static const char *
assign_defaultxactisolevel(const char *newval, bool doit, bool interactive)
2554
{
2555 2556 2557 2558
	if (strcasecmp(newval, "serializable") == 0)
		{ if (doit) DefaultXactIsoLevel = XACT_SERIALIZABLE; }
	else if (strcasecmp(newval, "read committed") == 0)
		{ if (doit) DefaultXactIsoLevel = XACT_READ_COMMITTED; }
2559
	else
2560 2561
		return NULL;
	return newval;
2562
}
2563 2564


2565 2566 2567
/*
 * Handle options fetched from pg_database.datconfig or pg_shadow.useconfig.
 */
2568 2569 2570 2571 2572
void
ProcessGUCArray(ArrayType *array, GucSource source)
{
	int		i;

2573 2574
	Assert(array != NULL);
	Assert(source == PGC_S_DATABASE || source == PGC_S_USER);
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593

	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,
					  false /*notbyvalue*/,
					  -1 /*varlenelem*/,
					  -1 /*varlenarray*/,
					  &isnull);

		if (isnull)
			continue;

		s = DatumGetCString(DirectFunctionCall1(textout, d));
2594

2595 2596 2597
		ParseLongOption(s, &name, &value);
		if (!value)
		{
2598
			elog(WARNING, "cannot parse setting \"%s\"", name);
2599
			free(name);
B
Bruce Momjian 已提交
2600
			continue;
2601 2602
		}

2603 2604 2605 2606 2607
		/*
		 * We process all these options at SUSET level.  We assume that the
		 * right to insert an option into pg_database or pg_shadow was
		 * checked when it was inserted.
		 */
2608
		SetConfigOption(name, value, PGC_SUSET, source);
2609 2610 2611 2612

		free(name);
		if (value)
			free(value);
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
	}
}



ArrayType *
GUCArrayAdd(ArrayType *array, const char *name, const char *value)
{
	Datum		datum;
	char	   *newval;
	ArrayType  *a;

	Assert(name);
	Assert(value);

	/* test if the option is valid */
	set_config_option(name, value,
					  superuser() ? PGC_SUSET : PGC_USERSET,
2631
					  PGC_S_SESSION, false, false);
2632 2633 2634 2635

	newval = palloc(strlen(name) + 1 + strlen(value) + 1);
	sprintf(newval, "%s=%s", name, value);
	datum = DirectFunctionCall1(textin, CStringGetDatum(newval));
B
Bruce Momjian 已提交
2636

2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
	if (array)
	{
		int		index;
		bool	isnull;
		int		i;

		index = ARR_DIMS(array)[0] + 1;	/* add after end */

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

			d = array_ref(array, 1, &i,
						  false /*notbyvalue*/,
						  -1 /*varlenelem*/,
						  -1 /*varlenarray*/,
						  &isnull);
			current = DatumGetCString(DirectFunctionCall1(textout, d));
			if (strncmp(current, newval, strlen(name) + 1)==0)
			{
				index = i;
				break;
			}
		}

		isnull = false;
		a = array_set(array, 1, &index, datum, false/*notbyval*/, -1, -1, &isnull);
	}
	else
		a = construct_array(&datum, 1, false, -1, 'i');

	return a;
}



ArrayType *
GUCArrayDelete(ArrayType *array, const char *name)
{
	ArrayType *newarray;
	int i;
	int index;

	Assert(name);
	Assert(array);

	/* test if the option is valid */
	set_config_option(name, NULL,
					  superuser() ? PGC_SUSET : PGC_USERSET,
2687
					  PGC_S_SESSION, false, false);
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715

	newarray = construct_array(NULL, 0, false, -1, 'i');
	index = 1;

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

		d = array_ref(array, 1, &i,
					  false /*notbyvalue*/,
					  -1 /*varlenelem*/,
					  -1 /*varlenarray*/,
					  &isnull);
		val = DatumGetCString(DirectFunctionCall1(textout, d));

		if (strncmp(val, name, strlen(name))==0
			&& val[strlen(name)] == '=')
			continue;

		isnull = false;
		newarray = array_set(newarray, 1, &index, d, false/*notbyval*/, -1, -1, &isnull);
		index++;
	}

	return newarray;
}