guc.c 67.3 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.92 2002/09/02 05:42:54 momjian 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 "funcapi.h"
33
#include "libpq/auth.h"
34
#include "libpq/pqcomm.h"
35
#include "mb/pg_wchar.h"
36 37 38 39 40 41
#include "miscadmin.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "parser/parse_expr.h"
42
#include "storage/fd.h"
43 44
#include "storage/freespace.h"
#include "storage/lock.h"
45 46
#include "storage/proc.h"
#include "tcop/tcopprot.h"
47 48
#include "utils/array.h"
#include "utils/builtins.h"
49
#include "utils/datetime.h"
50
#include "utils/elog.h"
51
#include "utils/pg_locale.h"
52
#include "pgstat.h"
53 54


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

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

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

74 75 76
static const char *assign_msglvl(int *var, const char *newval,
									bool doit, bool interactive);

77 78 79
/*
 * Debugging options
 */
80
#ifdef USE_ASSERT_CHECKING
B
Bruce Momjian 已提交
81
bool		assert_enabled = true;
82
#endif
83 84
bool		Log_statement = false;
bool		Log_duration = false;
B
Bruce Momjian 已提交
85 86 87 88
bool		Debug_print_plan = false;
bool		Debug_print_parse = false;
bool		Debug_print_rewritten = false;
bool		Debug_pretty_print = false;
89

B
Bruce Momjian 已提交
90 91 92
bool		Show_parser_stats = false;
bool		Show_planner_stats = false;
bool		Show_executor_stats = false;
93
bool		Show_statement_stats = false;	/* this is sort of all three above
B
Bruce Momjian 已提交
94 95
										 * together */
bool		Show_btree_build_stats = false;
96

97 98
bool		Explain_pretty_print = true;

B
Bruce Momjian 已提交
99
bool		SQL_inheritance = true;
100

101 102
bool		Australian_timezones = false;

103
bool		Password_encryption = true;
104

105 106 107 108 109 110 111 112 113 114 115 116 117
int			log_min_error_statement;
char		*log_min_error_statement_str = NULL;
const char	log_min_error_statement_str_default[] = "error";

int         server_min_messages;
char       *server_min_messages_str = NULL;
const char  server_min_messages_str_default[] = "notice";

int         client_min_messages;
char       *client_min_messages_str = NULL;
const char  client_min_messages_str_default[] = "notice";


118
#ifndef PG_KRB_SRVTAB
B
Bruce Momjian 已提交
119
#define PG_KRB_SRVTAB ""
120 121
#endif

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/*
 * 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);

139

140 141
/*
 * Declarations for GUC tables
142 143
 *
 * See src/backend/utils/misc/README for design notes.
144
 */
145 146
enum config_type
{
147 148 149 150
	PGC_BOOL,
	PGC_INT,
	PGC_REAL,
	PGC_STRING
151 152
};

153
/* Generic fields applicable to all types of variables */
154 155
struct config_generic
{
156 157 158 159 160 161 162 163 164 165 166
	/* 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 */
167 168
};

169 170 171 172 173 174 175 176 177 178 179 180
/* 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 */
181 182 183

struct config_bool
{
184 185 186
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
187
	bool	   *variable;
188 189 190 191 192 193
	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;
194 195 196 197
};

struct config_int
{
198 199 200
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
201
	int		   *variable;
202
	int			reset_val;
B
Bruce Momjian 已提交
203 204
	int			min;
	int			max;
205 206 207 208 209
	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;
210 211 212 213
};

struct config_real
{
214 215 216
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
217
	double	   *variable;
218
	double		reset_val;
B
Bruce Momjian 已提交
219 220
	double		min;
	double		max;
221 222 223 224 225
	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;
226 227 228 229
};

struct config_string
{
230 231 232
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all are constants) */
B
Bruce Momjian 已提交
233
	char	  **variable;
234 235 236 237 238 239 240
	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;
241 242
};

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/* 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)


283

284 285 286 287 288 289 290 291 292 293 294 295 296 297
/*
 * 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.
 *
298
 * 5. Add it to src/backend/utils/misc/postgresql.conf.sample.
299
 *
300
 * 6. Add it to src/bin/psql/tab-complete.c, if it's a USERSET option.
301
 *
302
 * 7. Don't forget to document the option.
303 304
 */

305

306
/******** option records follow ********/
307 308

static struct config_bool
B
Bruce Momjian 已提交
309
			ConfigureNamesBool[] =
310
{
311
	{
312 313
		{ "enable_seqscan", PGC_USERSET }, &enable_seqscan,
		true, NULL, NULL
314
	},
315
	{
316 317
		{ "enable_indexscan", PGC_USERSET }, &enable_indexscan,
		true, NULL, NULL
318 319
	},
	{
320 321
		{ "enable_tidscan", PGC_USERSET }, &enable_tidscan,
		true, NULL, NULL
322 323
	},
	{
324 325
		{ "enable_sort", PGC_USERSET }, &enable_sort,
		true, NULL, NULL
326 327
	},
	{
328 329
		{ "enable_nestloop", PGC_USERSET }, &enable_nestloop,
		true, NULL, NULL
330 331
	},
	{
332 333
		{ "enable_mergejoin", PGC_USERSET }, &enable_mergejoin,
		true, NULL, NULL
334 335
	},
	{
336 337
		{ "enable_hashjoin", PGC_USERSET }, &enable_hashjoin,
		true, NULL, NULL
338 339
	},
	{
340 341
		{ "geqo", PGC_USERSET }, &enable_geqo,
		true, NULL, NULL
342 343 344
	},

	{
345 346
		{ "tcpip_socket", PGC_POSTMASTER }, &NetServer,
		false, NULL, NULL
347 348
	},
	{
349 350
		{ "ssl", PGC_POSTMASTER }, &EnableSSL,
		false, NULL, NULL
351 352
	},
	{
353 354
		{ "fsync", PGC_SIGHUP }, &enableFsync,
		true, NULL, NULL
355 356
	},
	{
357 358
		{ "silent_mode", PGC_POSTMASTER }, &SilentMode,
		false, NULL, NULL
359 360 361
	},

	{
362 363
		{ "log_connections", PGC_BACKEND }, &Log_connections,
		false, NULL, NULL
364 365
	},
	{
366 367
		{ "log_timestamp", PGC_SIGHUP }, &Log_timestamp,
		false, NULL, NULL
368 369
	},
	{
370 371
		{ "log_pid", PGC_SIGHUP }, &Log_pid,
		false, NULL, NULL
372
	},
373

374
#ifdef USE_ASSERT_CHECKING
375
	{
376 377
		{ "debug_assertions", PGC_USERSET }, &assert_enabled,
		true, NULL, NULL
378
	},
379 380
#endif

381
	{
382 383 384 385 386
		{ "log_statement", PGC_USERSET }, &Log_statement,
		false, NULL, NULL
	},
	{
		{ "log_duration", PGC_USERSET }, &Log_duration,
387
		false, NULL, NULL
388 389
	},
	{
390 391
		{ "debug_print_parse", PGC_USERSET }, &Debug_print_parse,
		false, NULL, NULL
392 393
	},
	{
394 395
		{ "debug_print_rewritten", PGC_USERSET }, &Debug_print_rewritten,
		false, NULL, NULL
396 397
	},
	{
398 399
		{ "debug_print_plan", PGC_USERSET }, &Debug_print_plan,
		false, NULL, NULL
400 401
	},
	{
402 403
		{ "debug_pretty_print", PGC_USERSET }, &Debug_pretty_print,
		false, NULL, NULL
404
	},
405

406
	{
407 408
		{ "show_parser_stats", PGC_USERSET }, &Show_parser_stats,
		false, NULL, NULL
409 410
	},
	{
411 412
		{ "show_planner_stats", PGC_USERSET }, &Show_planner_stats,
		false, NULL, NULL
413 414
	},
	{
415 416
		{ "show_executor_stats", PGC_USERSET }, &Show_executor_stats,
		false, NULL, NULL
417 418
	},
	{
419
		{ "show_statement_stats", PGC_USERSET }, &Show_statement_stats,
420
		false, NULL, NULL
421
	},
422
#ifdef BTREE_BUILD_STATS
423
	{
424 425
		{ "show_btree_build_stats", PGC_SUSET }, &Show_btree_build_stats,
		false, NULL, NULL
426
	},
427 428
#endif

429
	{
430 431
		{ "explain_pretty_print", PGC_USERSET }, &Explain_pretty_print,
		true, NULL, NULL
432 433
	},

434
	{
435 436
		{ "stats_start_collector", PGC_POSTMASTER }, &pgstat_collect_startcollector,
		true, NULL, NULL
437 438
	},
	{
439 440
		{ "stats_reset_on_server_start", PGC_POSTMASTER }, &pgstat_collect_resetonpmstart,
		true, NULL, NULL
441 442
	},
	{
443 444
		{ "stats_command_string", PGC_SUSET }, &pgstat_collect_querystring,
		false, NULL, NULL
445 446
	},
	{
447 448
		{ "stats_row_level", PGC_SUSET }, &pgstat_collect_tuplelevel,
		false, NULL, NULL
449 450
	},
	{
451 452
		{ "stats_block_level", PGC_SUSET }, &pgstat_collect_blocklevel,
		false, NULL, NULL
453
	},
454

455
	{
456 457
		{ "trace_notify", PGC_USERSET }, &Trace_notify,
		false, NULL, NULL
458
	},
459 460

#ifdef LOCK_DEBUG
461
	{
462 463
		{ "trace_locks", PGC_SUSET }, &Trace_locks,
		false, NULL, NULL
464 465
	},
	{
466 467
		{ "trace_userlocks", PGC_SUSET }, &Trace_userlocks,
		false, NULL, NULL
468 469
	},
	{
470 471
		{ "trace_lwlocks", PGC_SUSET }, &Trace_lwlocks,
		false, NULL, NULL
472 473
	},
	{
474 475
		{ "debug_deadlocks", PGC_SUSET }, &Debug_deadlocks,
		false, NULL, NULL
476
	},
477 478
#endif

479
	{
480 481
		{ "hostname_lookup", PGC_SIGHUP }, &HostnameLookup,
		false, NULL, NULL
482 483
	},
	{
484 485
		{ "show_source_port", PGC_SIGHUP }, &ShowPortNumber,
		false, NULL, NULL
486
	},
487

488
	{
489 490
		{ "sql_inheritance", PGC_USERSET }, &SQL_inheritance,
		true, NULL, NULL
491 492
	},
	{
493 494
		{ "australian_timezones", PGC_USERSET }, &Australian_timezones,
		false, ClearDateCache, NULL
495 496
	},
	{
497 498
		{ "fixbtree", PGC_POSTMASTER }, &FixBTree,
		true, NULL, NULL
499 500
	},
	{
501
		{ "password_encryption", PGC_USERSET }, &Password_encryption,
502
		true, NULL, NULL
503 504
	},
	{
505 506
		{ "transform_null_equals", PGC_USERSET }, &Transform_null_equals,
		false, NULL, NULL
507
	},
508 509 510 511
	{
		{ "db_user_namespace", PGC_SIGHUP }, &Db_user_namespace,
		false, NULL, NULL
	},
512 513 514 515
	{
		{ "autocommit", PGC_USERSET }, &autocommit, 
		true, NULL, NULL
	},
516

517
	{
518
		{ NULL, 0 }, NULL, false, NULL, NULL
519
	}
520 521 522 523
};


static struct config_int
B
Bruce Momjian 已提交
524
			ConfigureNamesInt[] =
525
{
526 527 528 529
	{
		{ "default_statistics_target", PGC_USERSET }, &default_statistics_target,
		10, 1, 1000, NULL, NULL
	},
530
	{
531
		{ "geqo_threshold", PGC_USERSET }, &geqo_rels,
532
		DEFAULT_GEQO_RELS, 2, INT_MAX, NULL, NULL
533
	},
534
	{
535
		{ "geqo_pool_size", PGC_USERSET }, &Geqo_pool_size,
536
		DEFAULT_GEQO_POOL_SIZE, 0, MAX_GEQO_POOL_SIZE, NULL, NULL
537 538
	},
	{
539
		{ "geqo_effort", PGC_USERSET }, &Geqo_effort,
540
		1, 1, INT_MAX, NULL, NULL
541 542
	},
	{
543
		{ "geqo_generations", PGC_USERSET }, &Geqo_generations,
544
		0, 0, INT_MAX, NULL, NULL
545 546
	},
	{
547
		{ "geqo_random_seed", PGC_USERSET }, &Geqo_random_seed,
548
		-1, INT_MIN, INT_MAX, NULL, NULL
549 550 551
	},

	{
552
		{ "deadlock_timeout", PGC_POSTMASTER }, &DeadlockTimeout,
553
		1000, 0, INT_MAX, NULL, NULL
554
	},
555

T
Tatsuo Ishii 已提交
556
#ifdef HAVE_SYSLOG
557
	{
558
		{ "syslog", PGC_SIGHUP }, &Use_syslog,
559
		0, 0, 2, NULL, NULL
560
	},
561 562 563
#endif

	/*
B
Bruce Momjian 已提交
564 565
	 * Note: There is some postprocessing done in PostmasterMain() to make
	 * sure the buffers are at least twice the number of backends, so the
566 567 568
	 * constraints here are partially unused. Similarly, the superuser
	 * reserved number is checked to ensure it is less than the max
	 * backends number.
569
	 */
570
	{
571
		{ "max_connections", PGC_POSTMASTER }, &MaxBackends,
572
		DEF_MAXBACKENDS, 1, INT_MAX, NULL, NULL
573
	},
574

575 576 577 578 579
	{
		{ "superuser_reserved_connections", PGC_POSTMASTER }, &ReservedBackends,
		2, 0, INT_MAX, NULL, NULL
	},

580
	{
581
		{ "shared_buffers", PGC_POSTMASTER }, &NBuffers,
582
		DEF_NBUFFERS, 16, INT_MAX, NULL, NULL
583
	},
584

585
	{
586
		{ "port", PGC_POSTMASTER }, &PostPortNumber,
587
		DEF_PGPORT, 1, 65535, NULL, NULL
588
	},
589

590
	{
591
		{ "unix_socket_permissions", PGC_POSTMASTER }, &Unix_socket_permissions,
592
		0777, 0000, 0777, NULL, NULL
593
	},
594

595
	{
596
		{ "sort_mem", PGC_USERSET }, &SortMem,
597
		1024, 8 * BLCKSZ / 1024, INT_MAX, NULL, NULL
598
	},
599

600
	{
601
		{ "vacuum_mem", PGC_USERSET }, &VacuumMem,
602
		8192, 1024, INT_MAX, NULL, NULL
603
	},
604

605
	{
606
		{ "max_files_per_process", PGC_BACKEND }, &max_files_per_process,
607
		1000, 25, INT_MAX, NULL, NULL
608
	},
609

610
#ifdef LOCK_DEBUG
611
	{
612
		{ "trace_lock_oidmin", PGC_SUSET }, &Trace_lock_oidmin,
613
		BootstrapObjectIdData, 1, INT_MAX, NULL, NULL
614 615
	},
	{
616
		{ "trace_lock_table", PGC_SUSET }, &Trace_lock_table,
617
		0, 0, INT_MAX, NULL, NULL
618
	},
619
#endif
620
	{
621
		{ "max_expr_depth", PGC_USERSET }, &max_expr_depth,
622
		DEFAULT_MAX_EXPR_DEPTH, 10, INT_MAX, NULL, NULL
623
	},
624

625 626 627 628 629
	{
		{ "statement_timeout", PGC_USERSET }, &StatementTimeout,
		0, 0, INT_MAX, NULL, NULL
	},

630
	{
631
		{ "max_fsm_relations", PGC_POSTMASTER }, &MaxFSMRelations,
632
		100, 10, INT_MAX, NULL, NULL
633 634
	},
	{
635
		{ "max_fsm_pages", PGC_POSTMASTER }, &MaxFSMPages,
636
		10000, 1000, INT_MAX, NULL, NULL
637
	},
638

639
	{
640
		{ "max_locks_per_transaction", PGC_POSTMASTER }, &max_locks_per_xact,
641
		64, 10, INT_MAX, NULL, NULL
642
	},
643

644
	{
645
		{ "authentication_timeout", PGC_SIGHUP }, &AuthenticationTimeout,
646
		60, 1, 600, NULL, NULL
647
	},
648

649
	{
650
		{ "pre_auth_delay", PGC_SIGHUP }, &PreAuthDelay,
651
		0, 0, 60, NULL, NULL
652
	},
653

654
	{
655
		{ "checkpoint_segments", PGC_SIGHUP }, &CheckPointSegments,
656
		3, 1, INT_MAX, NULL, NULL
657
	},
T
Tom Lane 已提交
658

659
	{
660
		{ "checkpoint_timeout", PGC_SIGHUP }, &CheckPointTimeout,
661
		300, 30, 3600, NULL, NULL
662
	},
V
Vadim B. Mikheev 已提交
663

664
	{
665
		{ "wal_buffers", PGC_POSTMASTER }, &XLOGbuffers,
666
		8, 4, INT_MAX, NULL, NULL
667
	},
V
Vadim B. Mikheev 已提交
668

669
	{
670
		{ "wal_debug", PGC_SUSET }, &XLOG_DEBUG,
671
		0, 0, 16, NULL, NULL
672
	},
V
Vadim B. Mikheev 已提交
673

674
	{
675
		{ "commit_delay", PGC_USERSET }, &CommitDelay,
676
		0, 0, 100000, NULL, NULL
677
	},
V
Vadim B. Mikheev 已提交
678

679
	{
680
		{ "commit_siblings", PGC_USERSET }, &CommitSiblings,
681
		5, 1, 1000, NULL, NULL
682
	},
683

684
	{
685
		{ NULL, 0 }, NULL, 0, 0, 0, NULL, NULL
686
	}
687 688 689 690
};


static struct config_real
B
Bruce Momjian 已提交
691
			ConfigureNamesReal[] =
692
{
693
	{
694
		{ "effective_cache_size", PGC_USERSET }, &effective_cache_size,
695
		DEFAULT_EFFECTIVE_CACHE_SIZE, 0, DBL_MAX, NULL, NULL
696
	},
697
	{
698
		{ "random_page_cost", PGC_USERSET }, &random_page_cost,
699
		DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX, NULL, NULL
700 701
	},
	{
702
		{ "cpu_tuple_cost", PGC_USERSET }, &cpu_tuple_cost,
703
		DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX, NULL, NULL
704 705
	},
	{
706
		{ "cpu_index_tuple_cost", PGC_USERSET }, &cpu_index_tuple_cost,
707
		DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX, NULL, NULL
708 709
	},
	{
710
		{ "cpu_operator_cost", PGC_USERSET }, &cpu_operator_cost,
711
		DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX, NULL, NULL
712 713 714
	},

	{
715
		{ "geqo_selection_bias", PGC_USERSET }, &Geqo_selection_bias,
716 717
		DEFAULT_GEQO_SELECTION_BIAS, MIN_GEQO_SELECTION_BIAS,
		MAX_GEQO_SELECTION_BIAS, NULL, NULL
718 719 720
	},

	{
721 722 723 724 725 726 727
		{ "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
728
	}
729 730 731 732
};


static struct config_string
B
Bruce Momjian 已提交
733
			ConfigureNamesString[] =
734
{
735
	{
736 737
		{ "client_encoding", PGC_USERSET }, &client_encoding_string,
		"SQL_ASCII", assign_client_encoding, NULL
738 739
	},

740
	{
741 742
		{ "client_min_messages", PGC_USERSET }, &client_min_messages_str,
		client_min_messages_str_default, assign_client_min_messages, NULL
743
	},
744

745 746 747 748 749
	{
		{ "log_min_error_statement", PGC_USERSET }, &log_min_error_statement_str,
		log_min_error_statement_str_default, assign_min_error_statement, NULL
	},

750
	{
751 752
		{ "DateStyle", PGC_USERSET, GUC_LIST_INPUT }, &datestyle_string,
		"ISO, US", assign_datestyle, show_datestyle
753
	},
754

755
	{
756 757
		{ "default_transaction_isolation", PGC_USERSET }, &default_iso_level_string,
		"read committed", assign_defaultxactisolevel, NULL
758 759
	},

760
	{
761 762 763 764 765 766
		{ "dynamic_library_path", PGC_SUSET }, &Dynamic_library_path,
		"$libdir", NULL, NULL
	},

	{
		{ "krb_server_keyfile", PGC_POSTMASTER }, &pg_krb_server_keyfile,
767
		PG_KRB_SRVTAB, NULL, NULL
768
	},
769

770
	{
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		{ "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
788 789 790
	},

	{
791 792 793
		{ "search_path", PGC_USERSET, GUC_LIST_INPUT | GUC_LIST_QUOTE },
		&namespace_search_path,
		"$user,public", assign_search_path, NULL
794 795 796
	},

	{
797 798
		{ "server_encoding", PGC_USERSET }, &server_encoding_string,
		"SQL_ASCII", assign_server_encoding, show_server_encoding
799 800 801
	},

	{
802 803
		{ "server_min_messages", PGC_USERSET }, &server_min_messages_str,
		server_min_messages_str_default, assign_server_min_messages, NULL
804 805
	},

806
	{
807 808 809
		{ "session_authorization", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL },
		&session_authorization_string,
		NULL, assign_session_authorization, show_session_authorization
810 811
	},

T
Tatsuo Ishii 已提交
812
#ifdef HAVE_SYSLOG
813
	{
814 815
		{ "syslog_facility", PGC_POSTMASTER }, &Syslog_facility,
		"LOCAL0", assign_facility, NULL
816 817
	},
	{
818
		{ "syslog_ident", PGC_POSTMASTER }, &Syslog_ident,
819
		"postgres", NULL, NULL
820
	},
821
#endif
822

823
	{
824 825 826 827 828 829 830 831 832 833 834 835
		{ "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,
836
		"", NULL, NULL
837
	},
838

839
	{
840
		{ "unix_socket_directory", PGC_POSTMASTER }, &UnixSocketDir,
841
		"", NULL, NULL
842
	},
843

844
	{
845
		{ "virtual_host", PGC_POSTMASTER }, &VirtualHost,
846
		"", NULL, NULL
847
	},
848

849
	{
850 851
		{ "wal_sync_method", PGC_SIGHUP }, &XLOG_sync_method,
		XLOG_sync_method_default, assign_xlog_sync_method, NULL
852
	},
853

854
	{
855
		{ NULL, 0 }, NULL, NULL, NULL, NULL
856
	}
857 858 859 860 861 862
};

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


/*
863
 * Actual lookup of variables is done through this single, sorted array.
864
 */
865 866
static struct config_generic **guc_variables;
static int num_guc_variables;
867

868
static bool guc_dirty;			/* TRUE if need to do commit/abort work */
869

870
static char *guc_string_workspace; /* for avoiding memory leaks */
871

872

873
static int guc_var_compare(const void *a, const void *b);
874
static char *_ShowOption(struct config_generic *record);
875 876 877


/*
878 879 880
 * 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).
881
 */
882 883
static void
build_guc_variables(void)
884
{
885 886
	int			num_vars = 0;
	struct config_generic **guc_vars;
B
Bruce Momjian 已提交
887
	int			i;
888

889
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
890 891 892
	{
		struct config_bool *conf = &ConfigureNamesBool[i];

893 894 895
		/* Rather than requiring vartype to be filled in by hand, do this: */
		conf->gen.vartype = PGC_BOOL;
		num_vars++;
896
	}
897

898
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
899 900 901
	{
		struct config_int *conf = &ConfigureNamesInt[i];

902 903
		conf->gen.vartype = PGC_INT;
		num_vars++;
904
	}
905

906
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
907 908 909
	{
		struct config_real *conf = &ConfigureNamesReal[i];

910 911
		conf->gen.vartype = PGC_REAL;
		num_vars++;
912
	}
913

914
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
915
	{
916
		struct config_string *conf = &ConfigureNamesString[i];
917

918 919
		conf->gen.vartype = PGC_STRING;
		num_vars++;
920 921
	}

922 923 924 925
	guc_vars = (struct config_generic **)
		malloc(num_vars * sizeof(struct config_generic *));
	if (!guc_vars)
		elog(PANIC, "out of memory");
926

927
	num_vars = 0;
928

929 930
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesBool[i].gen;
931

932 933
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesInt[i].gen;
934

935 936
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesReal[i].gen;
937

938 939
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
		guc_vars[num_vars++] = & ConfigureNamesString[i].gen;
940

941 942
	qsort((void *) guc_vars, num_vars, sizeof(struct config_generic *),
		  guc_var_compare);
943

944 945 946 947
	if (guc_variables)
		free(guc_variables);
	guc_variables = guc_vars;
	num_guc_variables = num_vars;
948 949 950 951
}


/*
952 953
 * Look up option NAME. If it exists, return a pointer to its record,
 * else return NULL.
954
 */
955 956
static struct config_generic *
find_option(const char *name)
957
{
958 959
	const char **key = &name;
	struct config_generic **res;
960

961
	Assert(name);
962

963 964 965 966 967 968 969 970 971 972 973 974 975
	/*
	 * 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;
}
976 977 978


/*
979
 * comparator for qsorting and bsearching guc_variables array
980
 */
981 982
static int
guc_var_compare(const void *a, const void *b)
983
{
984 985
	struct config_generic *confa = *(struct config_generic **) a;
	struct config_generic *confb = *(struct config_generic **) b;
986 987
	const char *namea;
	const char *nameb;
988

989 990 991
	/*
	 * The temptation to use strcasecmp() here must be resisted, because
	 * the array ordering has to remain stable across setlocale() calls.
992
	 * So, build our own with a simple ASCII-only downcasing.
993
	 */
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	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;
1007
	}
1008 1009 1010 1011 1012
	if (*namea)
		return 1;				/* a is longer */
	if (*nameb)
		return -1;				/* b is longer */
	return 0;
1013
}
1014 1015 1016


/*
1017
 * Initialize GUC options during program startup.
1018
 */
1019 1020
void
InitializeGUCOptions(void)
1021
{
1022 1023
	int			i;
	char	   *env;
1024

1025 1026 1027 1028
	/*
	 * Build sorted array of all GUC variables.
	 */
	build_guc_variables();
1029

1030
	/*
1031 1032 1033 1034 1035 1036
	 * 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...
1037
	 */
1038
	for (i = 0; i < num_guc_variables; i++)
1039
	{
1040
		struct config_generic *gconf = guc_variables[i];
1041

1042 1043 1044 1045 1046 1047 1048 1049 1050
		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:
1051
			{
1052 1053 1054 1055
				struct config_bool *conf = (struct config_bool *) gconf;

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (conf->reset_val, true, false))
1056 1057
						fprintf(stderr, "Failed to initialize %s to %d\n",
								conf->gen.name, (int) conf->reset_val);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
				*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))
1070 1071
						fprintf(stderr, "Failed to initialize %s to %d\n",
								conf->gen.name, conf->reset_val);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
				*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))
1084 1085
						fprintf(stderr, "Failed to initialize %s to %g\n",
								conf->gen.name, conf->reset_val);
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
				*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)
					{
1118 1119
						fprintf(stderr, "Failed to initialize %s to '%s'\n",
								conf->gen.name, str);
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 1212 1213 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
					}
					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;
				}

1250 1251
				/* We need not strdup here */
				str = conf->reset_val;
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 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637

				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;
1638 1639 1640 1641 1642
			}
			break;
		case PGC_USERSET:
			/* always okay */
			break;
1643
	}
1644

1645
	/* Should we report errors interactively? */
1646
	interactive = (source >= PGC_S_SESSION);
1647 1648 1649 1650 1651
	/*
	 * Should we set reset/session values?  (If so, the behavior is not
	 * transactional.)
	 */
	makeDefault = DoIt && (source <= PGC_S_OVERRIDE) && (value != NULL);
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663

	/*
	 * 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)
		{
1664
			elog(DEBUG2, "%s: setting ignored because previous source is higher priority",
1665 1666 1667 1668 1669 1670
				 name);
			return true;
		}
		DoIt = false;			/* we won't change the variable itself */
	}

1671 1672 1673
	/*
	 * Evaluate value and set variable
	 */
1674
	switch (record->vartype)
1675 1676 1677
	{
		case PGC_BOOL:
			{
B
Bruce Momjian 已提交
1678
				struct config_bool *conf = (struct config_bool *) record;
1679
				bool		newval;
B
Bruce Momjian 已提交
1680 1681

				if (value)
1682
				{
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
					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 已提交
1695

1696 1697
				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
B
Bruce Momjian 已提交
1698
					{
1699 1700
						elog(elevel, "invalid value for option '%s': %d",
							 name, (int) newval);
B
Bruce Momjian 已提交
1701 1702
						return false;
					}
1703 1704 1705

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1706
					if (DoIt)
1707
					{
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
						*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;
1735
					}
1736
				}
B
Bruce Momjian 已提交
1737
				break;
1738
			}
1739 1740

		case PGC_INT:
1741
			{
B
Bruce Momjian 已提交
1742
				struct config_int *conf = (struct config_int *) record;
1743
				int			newval;
1744

B
Bruce Momjian 已提交
1745
				if (value)
1746
				{
1747
					if (!parse_int(value, &newval))
B
Bruce Momjian 已提交
1748
					{
1749 1750
						elog(elevel, "option '%s' expects an integer value",
							 name);
B
Bruce Momjian 已提交
1751 1752
						return false;
					}
1753
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1754 1755 1756
					{
						elog(elevel, "option '%s' value %d is outside"
							 " of permissible range [%d .. %d]",
1757
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1758 1759
						return false;
					}
1760 1761 1762 1763 1764 1765 1766 1767 1768
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1769 1770
					{
						elog(elevel, "invalid value for option '%s': %d",
1771
							 name, newval);
1772 1773
						return false;
					}
1774 1775 1776

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1777
					if (DoIt)
1778
					{
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
						*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;
1806
					}
1807
				}
B
Bruce Momjian 已提交
1808
				break;
1809
			}
1810 1811

		case PGC_REAL:
1812
			{
B
Bruce Momjian 已提交
1813
				struct config_real *conf = (struct config_real *) record;
1814
				double		newval;
1815

B
Bruce Momjian 已提交
1816
				if (value)
1817
				{
1818
					if (!parse_real(value, &newval))
B
Bruce Momjian 已提交
1819
					{
1820 1821
						elog(elevel, "option '%s' expects a real number",
							 name);
B
Bruce Momjian 已提交
1822 1823
						return false;
					}
1824
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1825 1826 1827
					{
						elog(elevel, "option '%s' value %g is outside"
							 " of permissible range [%g .. %g]",
1828
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1829 1830
						return false;
					}
1831 1832 1833 1834 1835 1836 1837 1838 1839
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1840 1841
					{
						elog(elevel, "invalid value for option '%s': %g",
1842
							 name, newval);
1843 1844
						return false;
					}
1845 1846 1847

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1848
					if (DoIt)
1849
					{
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
						*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;
1877
					}
1878
				}
B
Bruce Momjian 已提交
1879
				break;
1880
			}
1881 1882 1883

		case PGC_STRING:
			{
B
Bruce Momjian 已提交
1884
				struct config_string *conf = (struct config_string *) record;
1885
				char	   *newval;
B
Bruce Momjian 已提交
1886 1887

				if (value)
1888
				{
1889 1890 1891 1892 1893 1894 1895 1896 1897
					newval = strdup(value);
					if (newval == NULL)
					{
						elog(elevel, "out of memory");
						return false;
					}
				}
				else if (conf->reset_val)
				{
1898 1899 1900 1901 1902
					/*
					 * We could possibly avoid strdup here, but easier to
					 * make this case work the same as the normal assignment
					 * case.
					 */
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
					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 已提交
1933
					{
1934
						free(newval);
1935
						elog(elevel, "invalid value for option '%s': '%s'",
1936
							 name, value ? value : "");
B
Bruce Momjian 已提交
1937 1938
						return false;
					}
1939
					else if (hookresult != newval)
B
Bruce Momjian 已提交
1940
					{
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
						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 已提交
1955

1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
				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 已提交
1966
						{
1967 1968
							SET_STRING_RESET_VAL(conf, newval);
							conf->gen.reset_source = source;
B
Bruce Momjian 已提交
1969
						}
1970
						if (conf->gen.session_source <= source)
1971
						{
1972 1973
							SET_STRING_SESSION_VAL(conf, newval);
							conf->gen.session_source = source;
1974
						}
1975 1976 1977 1978 1979
						/* Perhaps we didn't install newval anywhere */
						if (newval != *conf->variable &&
							newval != conf->session_val &&
							newval != conf->reset_val)
							free(newval);
B
Bruce Momjian 已提交
1980
					}
1981
					else if (isLocal)
1982
					{
1983 1984
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
1985
					}
1986
					else
1987
					{
1988 1989 1990 1991
						SET_STRING_TENTATIVE_VAL(conf, newval);
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
1992
					}
1993 1994 1995 1996
				}
				else
				{
					free(newval);
1997
				}
B
Bruce Momjian 已提交
1998
				break;
1999
			}
2000
	}
2001

2002 2003 2004 2005 2006 2007 2008
	return true;
}



/*
 * Set a config option to the given value. See also set_config_option,
2009 2010
 * this is just the wrapper to be called from outside GUC.  NB: this
 * is used only for non-transactional operations.
2011 2012
 */
void
2013
SetConfigOption(const char *name, const char *value,
2014
				GucContext context, GucSource source)
2015
{
2016
	(void) set_config_option(name, value, context, source, false, true);
2017 2018 2019 2020 2021
}



/*
2022 2023
 * Fetch the current value of the option `name'. If the option doesn't exist,
 * throw an elog and don't return.
2024 2025 2026 2027 2028
 *
 * 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 已提交
2029
GetConfigOption(const char *name)
2030
{
B
Bruce Momjian 已提交
2031
	struct config_generic *record;
2032 2033
	static char buffer[256];

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

2038
	switch (record->vartype)
2039 2040
	{
		case PGC_BOOL:
B
Bruce Momjian 已提交
2041
			return *((struct config_bool *) record)->variable ? "on" : "off";
2042

2043
		case PGC_INT:
2044 2045
			snprintf(buffer, sizeof(buffer), "%d",
					 *((struct config_int *) record)->variable);
2046 2047
			return buffer;

2048
		case PGC_REAL:
2049 2050
			snprintf(buffer, sizeof(buffer), "%g",
					 *((struct config_real *) record)->variable);
2051 2052 2053
			return buffer;

		case PGC_STRING:
B
Bruce Momjian 已提交
2054
			return *((struct config_string *) record)->variable;
2055 2056 2057
	}
	return NULL;
}
2058

2059 2060 2061 2062 2063
/*
 * Get the RESET value associated with the given option.
 */
const char *
GetConfigOptionResetString(const char *name)
2064
{
2065 2066
	struct config_generic *record;
	static char buffer[256];
2067

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

	switch (record->vartype)
2073 2074
	{
		case PGC_BOOL:
2075
			return ((struct config_bool *) record)->reset_val ? "on" : "off";
2076

2077
		case PGC_INT:
2078
			snprintf(buffer, sizeof(buffer), "%d",
2079 2080
					 ((struct config_int *) record)->reset_val);
			return buffer;
2081 2082

		case PGC_REAL:
2083
			snprintf(buffer, sizeof(buffer), "%g",
2084 2085
					 ((struct config_real *) record)->reset_val);
			return buffer;
2086 2087

		case PGC_STRING:
2088 2089 2090 2091
			return ((struct config_string *) record)->reset_val;
	}
	return NULL;
}
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 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191


/*
 * 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;
		}
2192
	}
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214

	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);
}

2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
/*
 * 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 */
2231
	name = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2232 2233 2234 2235 2236

	/* Get the desired value or set to NULL for a reset request */
	if (PG_ARGISNULL(1))
		value = NULL;
	else
2237
		value = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(1)));
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256

	/*
	 * 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 */
2257
	new_value = GetConfigOptionByName(name, NULL);
2258 2259 2260 2261 2262 2263 2264 2265

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

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

2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
/*
 * 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)
{
2302 2303 2304
	TupOutputState *tstate;
	TupleDesc		tupdesc;
	CommandDest		dest = whereToSendOutput;
2305
	const char	   *varname;
2306
	char		   *value;
2307

2308 2309 2310
	/* Get the value and canonical spelling of name */
	value = GetConfigOptionByName(name, &varname);

2311
	/* need a tuple descriptor representing a single TEXT column */
2312
	tupdesc = CreateTemplateTupleDesc(1, false);
2313
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, (char *) varname,
2314
					   TEXTOID, -1, 0, false);
2315

2316 2317 2318 2319
	/* prepare for projection of tuples */
	tstate = begin_tup_output_tupdesc(dest, tupdesc);

	/* Send it */
2320
	do_text_output_oneline(tstate, value);
2321 2322

	end_tup_output(tstate);
2323 2324
}

2325 2326 2327
/*
 * SHOW ALL command
 */
2328 2329 2330 2331
void
ShowAllGUCConfig(void)
{
	int			i;
2332 2333 2334 2335 2336 2337
	TupOutputState *tstate;
	TupleDesc		tupdesc;
	CommandDest		dest = whereToSendOutput;
	char		  *values[2];

	/* need a tuple descriptor representing two TEXT columns */
2338
	tupdesc = CreateTemplateTupleDesc(2, false);
2339 2340 2341 2342 2343 2344 2345
	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);
2346

2347 2348
	for (i = 0; i < num_guc_variables; i++)
	{
2349 2350 2351 2352
		struct config_generic *conf = guc_variables[i];

		if (conf->flags & GUC_NO_SHOW_ALL)
			continue;
2353 2354

		/* assign to the values array */
2355 2356
		values[0] = (char *) conf->name;
		values[1] = _ShowOption(conf);
2357 2358 2359

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

2361 2362 2363
		/* clean up */
		if (values[1] != NULL)
			pfree(values[1]);
2364
	}
2365 2366

	end_tup_output(tstate);
2367
}
2368

2369
/*
2370 2371
 * Return GUC variable value by name; optionally return canonical
 * form of name.  Return value is palloc'd.
2372 2373
 */
char *
2374
GetConfigOptionByName(const char *name, const char **varname)
2375 2376 2377 2378 2379 2380 2381
{
	struct config_generic *record;

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

2382 2383 2384
	if (varname)
		*varname = record->name;

2385 2386 2387 2388
	return _ShowOption(record);
}

/*
2389 2390
 * Return GUC variable value by variable number; optionally return canonical
 * form of name.  Return value is palloc'd.
2391 2392
 */
char *
2393
GetConfigOptionByNum(int varnum, const char **varname, bool *noshow)
2394
{
2395 2396 2397 2398 2399 2400
	struct config_generic *conf;

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

	conf = guc_variables[varnum];
2401

2402 2403
	if (varname)
		*varname = conf->name;
2404

2405 2406 2407
	if (noshow)
		*noshow = (conf->flags & GUC_NO_SHOW_ALL) ? true : false;

2408
	return _ShowOption(conf);
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
}

/*
 * 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 */
2432
	varname = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2433 2434

	/* Get the value */
2435
	varval = GetConfigOptionByName(varname, NULL);
2436 2437 2438 2439 2440 2441 2442 2443

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

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

2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
/*
 * show_all_settings - equiv to SHOW ALL command but implemented as
 * a Table Function.
 */
Datum
show_all_settings(PG_FUNCTION_ARGS)
{
	FuncCallContext	   *funcctx;
	TupleDesc			tupdesc;
	int					call_cntr;
	int					max_calls;
	TupleTableSlot	   *slot;
	AttInMetadata	   *attinmeta;
2457
	MemoryContext		oldcontext;
2458 2459 2460 2461 2462 2463 2464

	/* stuff done only on the first call of the function */
 	if(SRF_IS_FIRSTCALL())
 	{
		/* create a function context for cross-call persistence */
 		funcctx = SRF_FIRSTCALL_INIT();

2465 2466 2467
		/* switch to memory context appropriate for multiple function calls */
		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

2468
		/* need a tuple descriptor representing two TEXT columns */
2469
		tupdesc = CreateTemplateTupleDesc(2, false);
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
		TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
						   TEXTOID, -1, 0, false);
		TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
						   TEXTOID, -1, 0, false);

		/* allocate a slot for a tuple with this tupdesc */
		slot = TupleDescGetSlot(tupdesc);

		/* assign slot to function context */
		funcctx->slot = slot;

		/*
		 * Generate attribute metadata needed later to produce tuples from raw
		 * C strings
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

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

		MemoryContextSwitchTo(oldcontext);
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
    }

	/* stuff done on every call of the function */
 	funcctx = SRF_PERCALL_SETUP();

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

 	if (call_cntr < max_calls)	/* do when there is more left to send */
 	{
2504
		char	   *values[2];
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
		char	   *varname;
		char	   *varval;
		bool		noshow;
		HeapTuple	tuple;
		Datum		result;

		/*
		 * Get the next visible GUC variable name and value
		 */
		do
		{
2516 2517 2518
			varval = GetConfigOptionByNum(call_cntr,
										  (const char **) &varname,
										  &noshow);
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
			if (noshow)
			{
				/* varval is a palloc'd copy, so free it */
				if (varval != NULL)
					pfree(varval);

				/* 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)
			 		SRF_RETURN_DONE(funcctx);
			}
		} while (noshow);

		/*
		 * Prepare a values array for storage in our slot.
		 * This should be an array of C strings which will
		 * be processed later by the appropriate "in" functions.
		 */
2539 2540
		values[0] = varname;
		values[1] = varval;
2541 2542 2543 2544 2545 2546 2547 2548 2549

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

		/* make the tuple into a datum */
		result = TupleGetDatum(slot, tuple);

		/* Clean up */
		if (varval != NULL)
2550
			pfree(varval);
2551 2552 2553 2554 2555 2556 2557 2558 2559

 		SRF_RETURN_NEXT(funcctx, result);
 	}
 	else	/* do when there is no more left */
 	{
 		SRF_RETURN_DONE(funcctx);
 	}
}

2560
static char *
2561 2562 2563 2564
_ShowOption(struct config_generic *record)
{
	char		buffer[256];
	const char *val;
2565

2566 2567 2568 2569 2570
	switch (record->vartype)
	{
		case PGC_BOOL:
			{
				struct config_bool *conf = (struct config_bool *) record;
2571

2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
				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;
2612

2613 2614 2615 2616 2617 2618 2619 2620
				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else if (*conf->variable && **conf->variable)
					val = *conf->variable;
				else
					val = "unset";
			}
			break;
2621

2622 2623 2624 2625 2626
		default:
			/* just to keep compiler quiet */
			val = "???";
			break;
	}
2627

2628
	return pstrdup(val);
2629
}
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639


/*
 * 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 已提交
2640
ParseLongOption(const char *string, char **name, char **value)
2641
{
B
Bruce Momjian 已提交
2642 2643
	size_t		equal_pos;
	char	   *cp;
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662

	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 已提交
2663
	else
2664
	{
2665
		/* no equal sign in string */
2666 2667 2668 2669 2670 2671
		*name = strdup(string);
		if (!*name)
			elog(FATAL, "out of memory");
		*value = NULL;
	}

B
Bruce Momjian 已提交
2672
	for (cp = *name; *cp; cp++)
2673 2674 2675
		if (*cp == '-')
			*cp = '_';
}
2676 2677 2678



T
Tatsuo Ishii 已提交
2679
#ifdef HAVE_SYSLOG
2680

2681 2682
static const char *
assign_facility(const char *facility, bool doit, bool interactive)
2683
{
B
Bruce Momjian 已提交
2684
	if (strcasecmp(facility, "LOCAL0") == 0)
2685
		return facility;
B
Bruce Momjian 已提交
2686
	if (strcasecmp(facility, "LOCAL1") == 0)
2687
		return facility;
B
Bruce Momjian 已提交
2688
	if (strcasecmp(facility, "LOCAL2") == 0)
2689
		return facility;
B
Bruce Momjian 已提交
2690
	if (strcasecmp(facility, "LOCAL3") == 0)
2691
		return facility;
B
Bruce Momjian 已提交
2692
	if (strcasecmp(facility, "LOCAL4") == 0)
2693
		return facility;
B
Bruce Momjian 已提交
2694
	if (strcasecmp(facility, "LOCAL5") == 0)
2695
		return facility;
B
Bruce Momjian 已提交
2696
	if (strcasecmp(facility, "LOCAL6") == 0)
2697
		return facility;
B
Bruce Momjian 已提交
2698
	if (strcasecmp(facility, "LOCAL7") == 0)
2699 2700
		return facility;
	return NULL;
2701
}
2702

2703
#endif
2704 2705


2706 2707
static const char *
assign_defaultxactisolevel(const char *newval, bool doit, bool interactive)
2708
{
2709 2710 2711 2712
	if (strcasecmp(newval, "serializable") == 0)
		{ if (doit) DefaultXactIsoLevel = XACT_SERIALIZABLE; }
	else if (strcasecmp(newval, "read committed") == 0)
		{ if (doit) DefaultXactIsoLevel = XACT_READ_COMMITTED; }
2713
	else
2714 2715
		return NULL;
	return newval;
2716
}
2717 2718


2719 2720
/*
 * Handle options fetched from pg_database.datconfig or pg_shadow.useconfig.
2721
 * The array parameter must be an array of TEXT.
2722
 */
2723 2724 2725 2726 2727
void
ProcessGUCArray(ArrayType *array, GucSource source)
{
	int		i;

2728
	Assert(array != NULL);
2729 2730 2731
	Assert(ARR_ELEMTYPE(array) == TEXTOID);
	Assert(ARR_NDIM(array) == 1);
	Assert(ARR_LBOUND(array)[0] == 1);
2732
	Assert(source == PGC_S_DATABASE || source == PGC_S_USER);
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743

	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,
					  -1 /*varlenarray*/,
2744 2745 2746
					  -1 /*TEXT's typlen*/,
					  false /*TEXT's typbyval*/,
					  'i' /*TEXT's typalign*/,
2747 2748 2749 2750 2751 2752
					  &isnull);

		if (isnull)
			continue;

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

2754 2755 2756
		ParseLongOption(s, &name, &value);
		if (!value)
		{
2757
			elog(WARNING, "cannot parse setting \"%s\"", name);
2758
			free(name);
B
Bruce Momjian 已提交
2759
			continue;
2760 2761
		}

2762 2763 2764 2765 2766
		/*
		 * 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.
		 */
2767
		SetConfigOption(name, value, PGC_SUSET, source);
2768 2769 2770 2771

		free(name);
		if (value)
			free(value);
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
	}
}



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,
2790
					  PGC_S_SESSION, false, false);
2791 2792 2793 2794

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

2796 2797 2798 2799 2800 2801
	if (array)
	{
		int		index;
		bool	isnull;
		int		i;

2802 2803 2804 2805
		Assert(ARR_ELEMTYPE(array) == TEXTOID);
		Assert(ARR_NDIM(array) == 1);
		Assert(ARR_LBOUND(array)[0] == 1);

2806 2807 2808 2809 2810 2811 2812 2813 2814
		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,
						  -1 /*varlenarray*/,
2815 2816 2817
						  -1 /*TEXT's typlen*/,
						  false /*TEXT's typbyval*/,
						  'i' /*TEXT's typalign*/,
2818
						  &isnull);
2819 2820
			if (isnull)
				continue;
2821 2822 2823 2824 2825 2826 2827 2828 2829
			current = DatumGetCString(DirectFunctionCall1(textout, d));
			if (strncmp(current, newval, strlen(name) + 1)==0)
			{
				index = i;
				break;
			}
		}

		isnull = false;
2830 2831 2832 2833 2834 2835 2836
		a = array_set(array, 1, &index,
					  datum,
					  -1 /*varlenarray*/,
					  -1 /*TEXT's typlen*/,
					  false /*TEXT's typbyval*/,
					  'i' /*TEXT's typalign*/,
					  &isnull);
2837 2838
	}
	else
2839 2840 2841
		a = construct_array(&datum, 1,
							TEXTOID,
							-1, false, 'i');
2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860

	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,
2861
					  PGC_S_SESSION, false, false);
2862

2863 2864 2865
	newarray = construct_array(NULL, 0,
							   TEXTOID,
							   -1, false, 'i');
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
	index = 1;

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

		d = array_ref(array, 1, &i,
					  -1 /*varlenarray*/,
2876 2877 2878
					  -1 /*TEXT's typlen*/,
					  false /*TEXT's typbyval*/,
					  'i' /*TEXT's typalign*/,
2879
					  &isnull);
2880 2881
		if (isnull)
			continue;
2882 2883 2884 2885 2886 2887 2888
		val = DatumGetCString(DirectFunctionCall1(textout, d));

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

		isnull = false;
2889 2890 2891 2892 2893 2894 2895
		newarray = array_set(newarray, 1, &index,
							 d,
							 -1 /*varlenarray*/,
							 -1 /*TEXT's typlen*/,
							 false /*TEXT's typbyval*/,
							 'i' /*TEXT's typalign*/,
							 &isnull);
2896 2897 2898 2899 2900
		index++;
	}

	return newarray;
}
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951

const char *
assign_server_min_messages(const char *newval,
						   bool doit, bool interactive)
{
	return(assign_msglvl(&server_min_messages,newval,doit,interactive));
}

const char *
assign_client_min_messages(const char *newval,
						   bool doit, bool interactive)
{
	return(assign_msglvl(&client_min_messages,newval,doit,interactive));
}

const char *
assign_min_error_statement(const char *newval, bool doit, bool interactive)
{
	return(assign_msglvl(&log_min_error_statement,newval,doit,interactive));
}

static const char *
assign_msglvl(int *var, const char *newval, bool doit, bool interactive)
{
	if (strcasecmp(newval, "debug") == 0)
		{ if (doit) (*var) = DEBUG1; }
	else if (strcasecmp(newval, "debug5") == 0)
		{ if (doit) (*var) = DEBUG5; }
	else if (strcasecmp(newval, "debug4") == 0)
		{ if (doit) (*var) = DEBUG4; }
	else if (strcasecmp(newval, "debug3") == 0)
		{ if (doit) (*var) = DEBUG3; }
	else if (strcasecmp(newval, "debug2") == 0)
		{ if (doit) (*var) = DEBUG2; }
	else if (strcasecmp(newval, "debug1") == 0)
		{ if (doit) (*var) = DEBUG1; }
	else if (strcasecmp(newval, "log") == 0)
		{ if (doit) (*var) = LOG; }
	else if (strcasecmp(newval, "info") == 0)
		{ if (doit) (*var) = INFO; }
	else if (strcasecmp(newval, "notice") == 0)
		{ if (doit) (*var) = NOTICE; }
	else if (strcasecmp(newval, "warning") == 0)
		{ if (doit) (*var) = WARNING; }
	else if (strcasecmp(newval, "error") == 0)
		{ if (doit) (*var) = ERROR; }
	else
		return NULL;			/* fail */
	return newval;				/* OK */
}