guc.c 69.5 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.113 2003/01/28 18:04:02 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 "funcapi.h"
33
#include "libpq/auth.h"
34
#include "libpq/pqcomm.h"
35
#include "mb/pg_wchar.h"
36 37 38 39
#include "miscadmin.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/paths.h"
40
#include "optimizer/prep.h"
41
#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;
B
Bruce Momjian 已提交
59
extern int	CheckPointTimeout;
60
extern bool autocommit;
B
Bruce Momjian 已提交
61 62
extern int	CommitDelay;
extern int	CommitSiblings;
63 64
extern bool FixBTree;

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

69
static const char *assign_facility(const char *facility,
B
Bruce Momjian 已提交
70
				bool doit, bool interactive);
71
#endif
72

73
static const char *assign_msglvl(int *var, const char *newval,
B
Bruce Momjian 已提交
74
			  bool doit, bool interactive);
75

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

B
Rename:  
Bruce Momjian 已提交
89 90 91 92
bool		log_parser_stats = false;
bool		log_planner_stats = false;
bool		log_executor_stats = false;
bool		log_statement_stats = false;		/* this is sort of all
B
Bruce Momjian 已提交
93
												 * three above together */
94
bool		log_btree_build_stats = false;
95

96 97
bool		Explain_pretty_print = true;

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

100 101
bool		Australian_timezones = false;

102
bool		Password_encryption = true;
103

104
int			log_min_error_statement = PANIC;
B
Bruce Momjian 已提交
105
char	   *log_min_error_statement_str = NULL;
106
const char	log_min_error_statement_str_default[] = "panic";
107

108 109 110
int			log_min_messages = NOTICE;
char	   *log_min_messages_str = NULL;
const char	log_min_messages_str_default[] = "notice";
111

112
int			client_min_messages = NOTICE;
B
Bruce Momjian 已提交
113 114
char	   *client_min_messages_str = NULL;
const char	client_min_messages_str_default[] = "notice";
115 116


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

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/*
 * 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,
B
Bruce Momjian 已提交
136
						   bool doit, bool interactive);
137

138

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

152
/* Generic fields applicable to all types of variables */
153 154
struct config_generic
{
155 156 157 158 159 160 161 162
	/* 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 */
B
Bruce Momjian 已提交
163 164
	GucSource	session_source; /* source of the session_value */
	GucSource	tentative_source;		/* source of the tentative_value */
165
	GucSource	source;			/* source of the current actual value */
166 167
};

168
/* bit values in flags field */
B
Bruce Momjian 已提交
169 170 171 172
#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 */
173 174

/* bit values in status field */
B
Bruce Momjian 已提交
175 176
#define GUC_HAVE_TENTATIVE	0x0001		/* tentative value is defined */
#define GUC_HAVE_LOCAL		0x0002		/* a SET LOCAL has been executed */
177 178 179


/* GUC records for specific variable types */
180 181 182

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

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

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

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


282

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

304

305
/******** option records follow ********/
306 307

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

	{
B
Bruce Momjian 已提交
348
		{"tcpip_socket", PGC_POSTMASTER}, &NetServer,
349
		false, NULL, NULL
350 351
	},
	{
B
Bruce Momjian 已提交
352
		{"ssl", PGC_POSTMASTER}, &EnableSSL,
353
		false, NULL, NULL
354 355
	},
	{
B
Bruce Momjian 已提交
356
		{"fsync", PGC_SIGHUP}, &enableFsync,
357
		true, NULL, NULL
358 359
	},
	{
B
Bruce Momjian 已提交
360
		{"silent_mode", PGC_POSTMASTER}, &SilentMode,
361
		false, NULL, NULL
362 363 364
	},

	{
B
Bruce Momjian 已提交
365
		{"log_connections", PGC_BACKEND}, &Log_connections,
366
		false, NULL, NULL
367 368
	},
	{
B
Bruce Momjian 已提交
369
		{"log_timestamp", PGC_SIGHUP}, &Log_timestamp,
370
		false, NULL, NULL
371 372
	},
	{
B
Bruce Momjian 已提交
373
		{"log_pid", PGC_SIGHUP}, &Log_pid,
374
		false, NULL, NULL
375
	},
376

377
#ifdef USE_ASSERT_CHECKING
378
	{
B
Bruce Momjian 已提交
379
		{"debug_assertions", PGC_USERSET}, &assert_enabled,
380
		true, NULL, NULL
381
	},
382 383
#endif

384
	{
B
Rename:  
Bruce Momjian 已提交
385
		{"log_statement", PGC_USERSET}, &log_statement,
386 387 388
		false, NULL, NULL
	},
	{
B
Rename:  
Bruce Momjian 已提交
389
		{"log_duration", PGC_USERSET}, &log_duration,
390
		false, NULL, NULL
391 392
	},
	{
B
Bruce Momjian 已提交
393
		{"debug_print_parse", PGC_USERSET}, &Debug_print_parse,
394
		false, NULL, NULL
395 396
	},
	{
B
Bruce Momjian 已提交
397
		{"debug_print_rewritten", PGC_USERSET}, &Debug_print_rewritten,
398
		false, NULL, NULL
399 400
	},
	{
B
Bruce Momjian 已提交
401
		{"debug_print_plan", PGC_USERSET}, &Debug_print_plan,
402
		false, NULL, NULL
403 404
	},
	{
B
Bruce Momjian 已提交
405
		{"debug_pretty_print", PGC_USERSET}, &Debug_pretty_print,
406
		false, NULL, NULL
407
	},
408

409
	{
B
Rename:  
Bruce Momjian 已提交
410
		{"log_parser_stats", PGC_USERSET}, &log_parser_stats,
411
		false, NULL, NULL
412 413
	},
	{
B
Rename:  
Bruce Momjian 已提交
414
		{"log_planner_stats", PGC_USERSET}, &log_planner_stats,
415
		false, NULL, NULL
416 417
	},
	{
B
Rename:  
Bruce Momjian 已提交
418
		{"log_executor_stats", PGC_USERSET}, &log_executor_stats,
419
		false, NULL, NULL
420 421
	},
	{
B
Rename:  
Bruce Momjian 已提交
422
		{"log_statement_stats", PGC_USERSET}, &log_statement_stats,
423
		false, NULL, NULL
424
	},
425
#ifdef BTREE_BUILD_STATS
426
	{
427
		{"log_btree_build_stats", PGC_SUSET}, &log_btree_build_stats,
428
		false, NULL, NULL
429
	},
430 431
#endif

432
	{
B
Bruce Momjian 已提交
433
		{"explain_pretty_print", PGC_USERSET}, &Explain_pretty_print,
434
		true, NULL, NULL
435 436
	},

437
	{
B
Bruce Momjian 已提交
438
		{"stats_start_collector", PGC_POSTMASTER}, &pgstat_collect_startcollector,
439
		true, NULL, NULL
440 441
	},
	{
B
Bruce Momjian 已提交
442
		{"stats_reset_on_server_start", PGC_POSTMASTER}, &pgstat_collect_resetonpmstart,
443
		true, NULL, NULL
444 445
	},
	{
B
Bruce Momjian 已提交
446
		{"stats_command_string", PGC_SUSET}, &pgstat_collect_querystring,
447
		false, NULL, NULL
448 449
	},
	{
B
Bruce Momjian 已提交
450
		{"stats_row_level", PGC_SUSET}, &pgstat_collect_tuplelevel,
451
		false, NULL, NULL
452 453
	},
	{
B
Bruce Momjian 已提交
454
		{"stats_block_level", PGC_SUSET}, &pgstat_collect_blocklevel,
455
		false, NULL, NULL
456
	},
457

458
	{
B
Bruce Momjian 已提交
459
		{"trace_notify", PGC_USERSET}, &Trace_notify,
460
		false, NULL, NULL
461
	},
462 463

#ifdef LOCK_DEBUG
464
	{
B
Bruce Momjian 已提交
465
		{"trace_locks", PGC_SUSET}, &Trace_locks,
466
		false, NULL, NULL
467 468
	},
	{
B
Bruce Momjian 已提交
469
		{"trace_userlocks", PGC_SUSET}, &Trace_userlocks,
470
		false, NULL, NULL
471 472
	},
	{
B
Bruce Momjian 已提交
473
		{"trace_lwlocks", PGC_SUSET}, &Trace_lwlocks,
474
		false, NULL, NULL
475 476
	},
	{
B
Bruce Momjian 已提交
477
		{"debug_deadlocks", PGC_SUSET}, &Debug_deadlocks,
478
		false, NULL, NULL
479
	},
480 481
#endif

482
	{
483
		{"log_hostname", PGC_SIGHUP}, &log_hostname,
484
		false, NULL, NULL
485 486
	},
	{
487
		{"log_source_port", PGC_SIGHUP}, &LogSourcePort,
488
		false, NULL, NULL
489
	},
490

491
	{
B
Bruce Momjian 已提交
492
		{"sql_inheritance", PGC_USERSET}, &SQL_inheritance,
493
		true, NULL, NULL
494 495
	},
	{
B
Bruce Momjian 已提交
496
		{"australian_timezones", PGC_USERSET}, &Australian_timezones,
497
		false, ClearDateCache, NULL
498 499
	},
	{
B
Bruce Momjian 已提交
500
		{"fixbtree", PGC_POSTMASTER}, &FixBTree,
501
		true, NULL, NULL
502 503
	},
	{
B
Bruce Momjian 已提交
504
		{"password_encryption", PGC_USERSET}, &Password_encryption,
505
		true, NULL, NULL
506 507
	},
	{
B
Bruce Momjian 已提交
508
		{"transform_null_equals", PGC_USERSET}, &Transform_null_equals,
509
		false, NULL, NULL
510
	},
511
	{
B
Bruce Momjian 已提交
512
		{"db_user_namespace", PGC_SIGHUP}, &Db_user_namespace,
513 514
		false, NULL, NULL
	},
515
	{
B
Bruce Momjian 已提交
516
		{"autocommit", PGC_USERSET}, &autocommit,
517 518
		true, NULL, NULL
	},
519 520 521 522 523 524 525 526
	{
		{"default_transaction_read_only", PGC_USERSET}, &DefaultXactReadOnly,
		false, NULL, NULL
	},
	{
		{"transaction_read_only", PGC_USERSET, GUC_NO_RESET_ALL}, &XactReadOnly,
		false, NULL, NULL
	},
527

528
	{
B
Bruce Momjian 已提交
529
		{NULL, 0}, NULL, false, NULL, NULL
530
	}
531 532 533 534
};


static struct config_int
B
Bruce Momjian 已提交
535
			ConfigureNamesInt[] =
536
{
537
	{
B
Bruce Momjian 已提交
538
		{"default_statistics_target", PGC_USERSET}, &default_statistics_target,
539 540
		10, 1, 1000, NULL, NULL
	},
541
	{
542 543 544 545 546 547 548 549 550 551
		{"from_collapse_limit", PGC_USERSET}, &from_collapse_limit,
		8, 1, INT_MAX, NULL, NULL
	},
	{
		{"join_collapse_limit", PGC_USERSET}, &join_collapse_limit,
		8, 1, INT_MAX, NULL, NULL
	},
	{
		{"geqo_threshold", PGC_USERSET}, &geqo_threshold,
		11, 2, INT_MAX, NULL, NULL
552
	},
553
	{
B
Bruce Momjian 已提交
554
		{"geqo_pool_size", PGC_USERSET}, &Geqo_pool_size,
555
		DEFAULT_GEQO_POOL_SIZE, 0, MAX_GEQO_POOL_SIZE, NULL, NULL
556 557
	},
	{
B
Bruce Momjian 已提交
558
		{"geqo_effort", PGC_USERSET}, &Geqo_effort,
559
		1, 1, INT_MAX, NULL, NULL
560 561
	},
	{
B
Bruce Momjian 已提交
562
		{"geqo_generations", PGC_USERSET}, &Geqo_generations,
563
		0, 0, INT_MAX, NULL, NULL
564 565
	},
	{
B
Bruce Momjian 已提交
566
		{"geqo_random_seed", PGC_USERSET}, &Geqo_random_seed,
567
		-1, INT_MIN, INT_MAX, NULL, NULL
568 569 570
	},

	{
B
Bruce Momjian 已提交
571
		{"deadlock_timeout", PGC_POSTMASTER}, &DeadlockTimeout,
572
		1000, 0, INT_MAX, NULL, NULL
573
	},
574

T
Tatsuo Ishii 已提交
575
#ifdef HAVE_SYSLOG
576
	{
B
Bruce Momjian 已提交
577
		{"syslog", PGC_SIGHUP}, &Use_syslog,
578
		0, 0, 2, NULL, NULL
579
	},
580 581 582
#endif

	/*
B
Bruce Momjian 已提交
583 584
	 * Note: There is some postprocessing done in PostmasterMain() to make
	 * sure the buffers are at least twice the number of backends, so the
585 586 587
	 * constraints here are partially unused. Similarly, the superuser
	 * reserved number is checked to ensure it is less than the max
	 * backends number.
588
	 */
589
	{
B
Bruce Momjian 已提交
590
		{"max_connections", PGC_POSTMASTER}, &MaxBackends,
591
		DEF_MAXBACKENDS, 1, INT_MAX, NULL, NULL
592
	},
593

594
	{
B
Bruce Momjian 已提交
595
		{"superuser_reserved_connections", PGC_POSTMASTER}, &ReservedBackends,
596 597 598
		2, 0, INT_MAX, NULL, NULL
	},

599
	{
B
Bruce Momjian 已提交
600
		{"shared_buffers", PGC_POSTMASTER}, &NBuffers,
601
		DEF_NBUFFERS, 16, INT_MAX, NULL, NULL
602
	},
603

604
	{
B
Bruce Momjian 已提交
605
		{"port", PGC_POSTMASTER}, &PostPortNumber,
606
		DEF_PGPORT, 1, 65535, NULL, NULL
607
	},
608

609
	{
B
Bruce Momjian 已提交
610
		{"unix_socket_permissions", PGC_POSTMASTER}, &Unix_socket_permissions,
611
		0777, 0000, 0777, NULL, NULL
612
	},
613

614
	{
B
Bruce Momjian 已提交
615
		{"sort_mem", PGC_USERSET}, &SortMem,
616
		1024, 8 * BLCKSZ / 1024, INT_MAX, NULL, NULL
617
	},
618

619
	{
B
Bruce Momjian 已提交
620
		{"vacuum_mem", PGC_USERSET}, &VacuumMem,
621
		8192, 1024, INT_MAX, NULL, NULL
622
	},
623

624
	{
B
Bruce Momjian 已提交
625
		{"max_files_per_process", PGC_BACKEND}, &max_files_per_process,
626
		1000, 25, INT_MAX, NULL, NULL
627
	},
628

629
#ifdef LOCK_DEBUG
630
	{
B
Bruce Momjian 已提交
631
		{"trace_lock_oidmin", PGC_SUSET}, &Trace_lock_oidmin,
632
		BootstrapObjectIdData, 1, INT_MAX, NULL, NULL
633 634
	},
	{
B
Bruce Momjian 已提交
635
		{"trace_lock_table", PGC_SUSET}, &Trace_lock_table,
636
		0, 0, INT_MAX, NULL, NULL
637
	},
638
#endif
639
	{
B
Bruce Momjian 已提交
640
		{"max_expr_depth", PGC_USERSET}, &max_expr_depth,
641
		DEFAULT_MAX_EXPR_DEPTH, 10, INT_MAX, NULL, NULL
642
	},
643

644
	{
B
Bruce Momjian 已提交
645
		{"statement_timeout", PGC_USERSET}, &StatementTimeout,
646 647 648
		0, 0, INT_MAX, NULL, NULL
	},

649
	{
B
Bruce Momjian 已提交
650
		{"max_fsm_relations", PGC_POSTMASTER}, &MaxFSMRelations,
651
		1000, 10, INT_MAX, NULL, NULL
652 653
	},
	{
B
Bruce Momjian 已提交
654
		{"max_fsm_pages", PGC_POSTMASTER}, &MaxFSMPages,
655
		10000, 1000, INT_MAX, NULL, NULL
656
	},
657

658
	{
B
Bruce Momjian 已提交
659
		{"max_locks_per_transaction", PGC_POSTMASTER}, &max_locks_per_xact,
660
		64, 10, INT_MAX, NULL, NULL
661
	},
662

663
	{
B
Bruce Momjian 已提交
664
		{"authentication_timeout", PGC_SIGHUP}, &AuthenticationTimeout,
665
		60, 1, 600, NULL, NULL
666
	},
667

668
	{
B
Bruce Momjian 已提交
669
		{"pre_auth_delay", PGC_SIGHUP}, &PreAuthDelay,
670
		0, 0, 60, NULL, NULL
671
	},
672

673
	{
B
Bruce Momjian 已提交
674
		{"checkpoint_segments", PGC_SIGHUP}, &CheckPointSegments,
675
		3, 1, INT_MAX, NULL, NULL
676
	},
T
Tom Lane 已提交
677

678
	{
B
Bruce Momjian 已提交
679
		{"checkpoint_timeout", PGC_SIGHUP}, &CheckPointTimeout,
680
		300, 30, 3600, NULL, NULL
681
	},
V
Vadim B. Mikheev 已提交
682

683 684 685 686 687
	{
		{"checkpoint_warning", PGC_SIGHUP}, &CheckPointWarning,
		30, 0, INT_MAX, NULL, NULL
	},

688
	{
B
Bruce Momjian 已提交
689
		{"wal_buffers", PGC_POSTMASTER}, &XLOGbuffers,
690
		8, 4, INT_MAX, NULL, NULL
691
	},
V
Vadim B. Mikheev 已提交
692

693
	{
B
Bruce Momjian 已提交
694
		{"wal_debug", PGC_SUSET}, &XLOG_DEBUG,
695
		0, 0, 16, NULL, NULL
696
	},
V
Vadim B. Mikheev 已提交
697

698
	{
B
Bruce Momjian 已提交
699
		{"commit_delay", PGC_USERSET}, &CommitDelay,
700
		0, 0, 100000, NULL, NULL
701
	},
V
Vadim B. Mikheev 已提交
702

703
	{
B
Bruce Momjian 已提交
704
		{"commit_siblings", PGC_USERSET}, &CommitSiblings,
705
		5, 1, 1000, NULL, NULL
706
	},
707

708 709 710 711 712
	{
		{"extra_float_digits", PGC_USERSET}, &extra_float_digits,
		0, -15, 2, NULL, NULL
	},

713
	{
B
Bruce Momjian 已提交
714
		{NULL, 0}, NULL, 0, 0, 0, NULL, NULL
715
	}
716 717 718 719
};


static struct config_real
B
Bruce Momjian 已提交
720
			ConfigureNamesReal[] =
721
{
722
	{
B
Bruce Momjian 已提交
723
		{"effective_cache_size", PGC_USERSET}, &effective_cache_size,
724
		DEFAULT_EFFECTIVE_CACHE_SIZE, 0, DBL_MAX, NULL, NULL
725
	},
726
	{
B
Bruce Momjian 已提交
727
		{"random_page_cost", PGC_USERSET}, &random_page_cost,
728
		DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX, NULL, NULL
729 730
	},
	{
B
Bruce Momjian 已提交
731
		{"cpu_tuple_cost", PGC_USERSET}, &cpu_tuple_cost,
732
		DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX, NULL, NULL
733 734
	},
	{
B
Bruce Momjian 已提交
735
		{"cpu_index_tuple_cost", PGC_USERSET}, &cpu_index_tuple_cost,
736
		DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX, NULL, NULL
737 738
	},
	{
B
Bruce Momjian 已提交
739
		{"cpu_operator_cost", PGC_USERSET}, &cpu_operator_cost,
740
		DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX, NULL, NULL
741 742 743
	},

	{
B
Bruce Momjian 已提交
744
		{"geqo_selection_bias", PGC_USERSET}, &Geqo_selection_bias,
745 746
		DEFAULT_GEQO_SELECTION_BIAS, MIN_GEQO_SELECTION_BIAS,
		MAX_GEQO_SELECTION_BIAS, NULL, NULL
747 748 749
	},

	{
B
Bruce Momjian 已提交
750
		{"seed", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL},
751 752 753 754 755
		&phony_random_seed,
		0.5, 0.0, 1.0, assign_random_seed, show_random_seed
	},

	{
B
Bruce Momjian 已提交
756
		{NULL, 0}, NULL, 0.0, 0.0, 0.0, NULL, NULL
757
	}
758 759 760 761
};


static struct config_string
B
Bruce Momjian 已提交
762
			ConfigureNamesString[] =
763
{
764
	{
B
Bruce Momjian 已提交
765
		{"client_encoding", PGC_USERSET}, &client_encoding_string,
766
		"SQL_ASCII", assign_client_encoding, NULL
767 768
	},

769
	{
B
Bruce Momjian 已提交
770
		{"client_min_messages", PGC_USERSET}, &client_min_messages_str,
771
		client_min_messages_str_default, assign_client_min_messages, NULL
772
	},
773

774
	{
B
Bruce Momjian 已提交
775
		{"log_min_error_statement", PGC_USERSET}, &log_min_error_statement_str,
776 777 778
		log_min_error_statement_str_default, assign_min_error_statement, NULL
	},

779
	{
B
Bruce Momjian 已提交
780
		{"DateStyle", PGC_USERSET, GUC_LIST_INPUT}, &datestyle_string,
781
		"ISO, US", assign_datestyle, show_datestyle
782
	},
783

784
	{
B
Bruce Momjian 已提交
785
		{"default_transaction_isolation", PGC_USERSET}, &default_iso_level_string,
786
		"read committed", assign_defaultxactisolevel, NULL
787 788
	},

789
	{
B
Bruce Momjian 已提交
790
		{"dynamic_library_path", PGC_SUSET}, &Dynamic_library_path,
791 792 793 794
		"$libdir", NULL, NULL
	},

	{
B
Bruce Momjian 已提交
795
		{"krb_server_keyfile", PGC_POSTMASTER}, &pg_krb_server_keyfile,
796
		PG_KRB_SRVTAB, NULL, NULL
797
	},
798

799 800
	/* See main.c about why defaults for LC_foo are not all alike */

801
	{
B
Bruce Momjian 已提交
802
		{"lc_messages", PGC_SUSET}, &locale_messages,
803 804 805 806
		"", locale_messages_assign, NULL
	},

	{
B
Bruce Momjian 已提交
807
		{"lc_monetary", PGC_USERSET}, &locale_monetary,
808
		"C", locale_monetary_assign, NULL
809 810 811
	},

	{
B
Bruce Momjian 已提交
812
		{"lc_numeric", PGC_USERSET}, &locale_numeric,
813
		"C", locale_numeric_assign, NULL
814 815 816
	},

	{
B
Bruce Momjian 已提交
817
		{"lc_time", PGC_USERSET}, &locale_time,
818
		"C", locale_time_assign, NULL
819 820 821
	},

	{
B
Bruce Momjian 已提交
822
		{"search_path", PGC_USERSET, GUC_LIST_INPUT | GUC_LIST_QUOTE},
823 824
		&namespace_search_path,
		"$user,public", assign_search_path, NULL
825 826 827
	},

	{
B
Bruce Momjian 已提交
828
		{"server_encoding", PGC_USERSET}, &server_encoding_string,
829
		"SQL_ASCII", assign_server_encoding, show_server_encoding
830 831 832
	},

	{
833 834
		{"log_min_messages", PGC_USERSET}, &log_min_messages_str,
		log_min_messages_str_default, assign_log_min_messages, NULL
835 836
	},

837
	{
B
Bruce Momjian 已提交
838
		{"session_authorization", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL},
839 840
		&session_authorization_string,
		NULL, assign_session_authorization, show_session_authorization
841 842
	},

T
Tatsuo Ishii 已提交
843
#ifdef HAVE_SYSLOG
844
	{
B
Bruce Momjian 已提交
845
		{"syslog_facility", PGC_POSTMASTER}, &Syslog_facility,
846
		"LOCAL0", assign_facility, NULL
847 848
	},
	{
B
Bruce Momjian 已提交
849
		{"syslog_ident", PGC_POSTMASTER}, &Syslog_ident,
850
		"postgres", NULL, NULL
851
	},
852
#endif
853

854
	{
B
Bruce Momjian 已提交
855
		{"TimeZone", PGC_USERSET}, &timezone_string,
856 857 858 859
		"UNKNOWN", assign_timezone, show_timezone
	},

	{
860
		{"transaction_isolation", PGC_USERSET, GUC_NO_RESET_ALL},
861 862 863 864 865
		&XactIsoLevel_string,
		NULL, assign_XactIsoLevel, show_XactIsoLevel
	},

	{
B
Bruce Momjian 已提交
866
		{"unix_socket_group", PGC_POSTMASTER}, &Unix_socket_group,
867
		"", NULL, NULL
868
	},
869

870
	{
B
Bruce Momjian 已提交
871
		{"unix_socket_directory", PGC_POSTMASTER}, &UnixSocketDir,
872
		"", NULL, NULL
873
	},
874

875
	{
B
Bruce Momjian 已提交
876
		{"virtual_host", PGC_POSTMASTER}, &VirtualHost,
877
		"", NULL, NULL
878
	},
879

880
	{
B
Bruce Momjian 已提交
881
		{"wal_sync_method", PGC_SIGHUP}, &XLOG_sync_method,
882
		XLOG_sync_method_default, assign_xlog_sync_method, NULL
883
	},
884

885
	{
B
Bruce Momjian 已提交
886
		{NULL, 0}, NULL, NULL, NULL, NULL
887
	}
888 889 890 891 892 893
};

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


/*
894
 * Actual lookup of variables is done through this single, sorted array.
895
 */
896
static struct config_generic **guc_variables;
B
Bruce Momjian 已提交
897
static int	num_guc_variables;
898

899
static bool guc_dirty;			/* TRUE if need to do commit/abort work */
900

B
Bruce Momjian 已提交
901
static char *guc_string_workspace;		/* for avoiding memory leaks */
902

903

B
Bruce Momjian 已提交
904 905
static int	guc_var_compare(const void *a, const void *b);
static char *_ShowOption(struct config_generic * record);
906 907 908


/*
B
Bruce Momjian 已提交
909
 * Build the sorted array.	This is split out so that it could be
910 911
 * re-executed after startup (eg, we could allow loadable modules to
 * add vars, and then we'd need to re-sort).
912
 */
913 914
static void
build_guc_variables(void)
915
{
916 917
	int			num_vars = 0;
	struct config_generic **guc_vars;
B
Bruce Momjian 已提交
918
	int			i;
919

920
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
921 922 923
	{
		struct config_bool *conf = &ConfigureNamesBool[i];

924 925 926
		/* Rather than requiring vartype to be filled in by hand, do this: */
		conf->gen.vartype = PGC_BOOL;
		num_vars++;
927
	}
928

929
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
930 931 932
	{
		struct config_int *conf = &ConfigureNamesInt[i];

933 934
		conf->gen.vartype = PGC_INT;
		num_vars++;
935
	}
936

937
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
938 939 940
	{
		struct config_real *conf = &ConfigureNamesReal[i];

941 942
		conf->gen.vartype = PGC_REAL;
		num_vars++;
943
	}
944

945
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
946
	{
947
		struct config_string *conf = &ConfigureNamesString[i];
948

949 950
		conf->gen.vartype = PGC_STRING;
		num_vars++;
951 952
	}

953 954 955 956
	guc_vars = (struct config_generic **)
		malloc(num_vars * sizeof(struct config_generic *));
	if (!guc_vars)
		elog(PANIC, "out of memory");
957

958
	num_vars = 0;
959

960
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
B
Bruce Momjian 已提交
961
		guc_vars[num_vars++] = &ConfigureNamesBool[i].gen;
962

963
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
B
Bruce Momjian 已提交
964
		guc_vars[num_vars++] = &ConfigureNamesInt[i].gen;
965

966
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
B
Bruce Momjian 已提交
967
		guc_vars[num_vars++] = &ConfigureNamesReal[i].gen;
968

969
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
B
Bruce Momjian 已提交
970
		guc_vars[num_vars++] = &ConfigureNamesString[i].gen;
971

972 973
	qsort((void *) guc_vars, num_vars, sizeof(struct config_generic *),
		  guc_var_compare);
974

975 976 977 978
	if (guc_variables)
		free(guc_variables);
	guc_variables = guc_vars;
	num_guc_variables = num_vars;
979 980 981 982
}


/*
983 984
 * Look up option NAME. If it exists, return a pointer to its record,
 * else return NULL.
985
 */
986 987
static struct config_generic *
find_option(const char *name)
988
{
989 990
	const char **key = &name;
	struct config_generic **res;
991

992
	Assert(name);
993

994 995 996 997
	/*
	 * by equating const char ** with struct config_generic *, we are
	 * assuming the name field is first in config_generic.
	 */
B
Bruce Momjian 已提交
998 999 1000 1001 1002
	res = (struct config_generic **) bsearch((void *) &key,
											 (void *) guc_variables,
											 num_guc_variables,
										 sizeof(struct config_generic *),
											 guc_var_compare);
1003 1004 1005 1006
	if (res)
		return *res;
	return NULL;
}
1007 1008 1009


/*
1010
 * comparator for qsorting and bsearching guc_variables array
1011
 */
1012 1013
static int
guc_var_compare(const void *a, const void *b)
1014
{
1015 1016
	struct config_generic *confa = *(struct config_generic **) a;
	struct config_generic *confb = *(struct config_generic **) b;
1017 1018
	const char *namea;
	const char *nameb;
1019

1020 1021 1022
	/*
	 * The temptation to use strcasecmp() here must be resisted, because
	 * the array ordering has to remain stable across setlocale() calls.
1023
	 * So, build our own with a simple ASCII-only downcasing.
1024
	 */
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	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;
1038
	}
1039 1040 1041 1042 1043
	if (*namea)
		return 1;				/* a is longer */
	if (*nameb)
		return -1;				/* b is longer */
	return 0;
1044
}
1045 1046 1047


/*
1048
 * Initialize GUC options during program startup.
1049
 */
1050 1051
void
InitializeGUCOptions(void)
1052
{
1053 1054
	int			i;
	char	   *env;
1055

1056 1057 1058 1059
	/*
	 * Build sorted array of all GUC variables.
	 */
	build_guc_variables();
1060

1061
	/*
1062 1063 1064 1065 1066 1067
	 * 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...
1068
	 */
1069
	for (i = 0; i < num_guc_variables; i++)
1070
	{
1071
		struct config_generic *gconf = guc_variables[i];
1072

1073 1074 1075 1076 1077 1078 1079 1080 1081
		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:
B
Bruce Momjian 已提交
1082 1083
				{
					struct config_bool *conf = (struct config_bool *) gconf;
1084

B
Bruce Momjian 已提交
1085 1086 1087 1088 1089 1090 1091 1092
					if (conf->assign_hook)
						if (!(*conf->assign_hook) (conf->reset_val, true, false))
							fprintf(stderr, "Failed to initialize %s to %d\n",
								  conf->gen.name, (int) conf->reset_val);
					*conf->variable = conf->reset_val;
					conf->session_val = conf->reset_val;
					break;
				}
1093
			case PGC_INT:
B
Bruce Momjian 已提交
1094 1095
				{
					struct config_int *conf = (struct config_int *) gconf;
1096

B
Bruce Momjian 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
					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))
							fprintf(stderr, "Failed to initialize %s to %d\n",
									conf->gen.name, conf->reset_val);
					*conf->variable = conf->reset_val;
					conf->session_val = conf->reset_val;
					break;
				}
1107 1108
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
					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))
							fprintf(stderr, "Failed to initialize %s to %g\n",
									conf->gen.name, conf->reset_val);
					*conf->variable = conf->reset_val;
					conf->session_val = conf->reset_val;
1119 1120
					break;
				}
B
Bruce Momjian 已提交
1121
			case PGC_STRING:
1122
				{
B
Bruce Momjian 已提交
1123 1124 1125 1126 1127 1128 1129
					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;
1130

B
Bruce Momjian 已提交
1131
					if (conf->boot_val == NULL)
1132
					{
B
Bruce Momjian 已提交
1133 1134
						/* Cannot set value yet */
						break;
1135
					}
B
Bruce Momjian 已提交
1136 1137 1138 1139 1140 1141 1142

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

					if (conf->assign_hook)
1143
					{
B
Bruce Momjian 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
						const char *newstr;

						newstr = (*conf->assign_hook) (str, true, false);
						if (newstr == NULL)
						{
							fprintf(stderr, "Failed to initialize %s to '%s'\n",
									conf->gen.name, str);
						}
						else if (newstr != str)
						{
							free(str);

							/*
							 * See notes in set_config_option about
							 * casting
							 */
							str = (char *) newstr;
							conf->reset_val = str;
						}
1163
					}
B
Bruce Momjian 已提交
1164 1165 1166
					*conf->variable = str;
					conf->session_val = str;
					break;
1167 1168 1169 1170 1171 1172 1173 1174 1175
				}
		}
	}

	guc_dirty = false;

	guc_string_workspace = NULL;

	/*
1176
	 * Prevent any attempt to override the transaction modes from
1177 1178
	 * non-interactive sources.
	 */
1179 1180 1181
	SetConfigOption("transaction_isolation", "default",
					PGC_POSTMASTER, PGC_S_OVERRIDE);
	SetConfigOption("transaction_read_only", "no",
1182 1183 1184 1185
					PGC_POSTMASTER, PGC_S_OVERRIDE);

	/*
	 * For historical reasons, some GUC parameters can receive defaults
B
Bruce Momjian 已提交
1186
	 * from environment variables.	Process those settings.
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
	 */

	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:
B
Bruce Momjian 已提交
1232 1233
				{
					struct config_bool *conf = (struct config_bool *) gconf;
1234

B
Bruce Momjian 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
					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;
				}
1246
			case PGC_INT:
B
Bruce Momjian 已提交
1247 1248
				{
					struct config_int *conf = (struct config_int *) gconf;
1249

B
Bruce Momjian 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
					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;
				}
1261 1262
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
					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;
1274 1275
					break;
				}
B
Bruce Momjian 已提交
1276 1277 1278 1279
			case PGC_STRING:
				{
					struct config_string *conf = (struct config_string *) gconf;
					char	   *str;
1280

B
Bruce Momjian 已提交
1281 1282 1283 1284 1285
					if (conf->reset_val == NULL)
					{
						/* Nothing to reset to, as yet; so do nothing */
						break;
					}
1286

B
Bruce Momjian 已提交
1287 1288
					/* We need not strdup here */
					str = conf->reset_val;
1289

B
Bruce Momjian 已提交
1290
					if (conf->assign_hook)
1291
					{
B
Bruce Momjian 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
						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;
						}
1305 1306
					}

B
Bruce Momjian 已提交
1307 1308 1309 1310 1311 1312 1313 1314
					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;
				}
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
		}
	}
}


/*
 * 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:
				{
B
Bruce Momjian 已提交
1351
					struct config_bool *conf = (struct config_bool *) gconf;
1352

B
Bruce Momjian 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
					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;
1370 1371 1372
				}
			case PGC_INT:
				{
B
Bruce Momjian 已提交
1373
					struct config_int *conf = (struct config_int *) gconf;
1374

B
Bruce Momjian 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
					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;
1392 1393 1394
				}
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
1395
					struct config_real *conf = (struct config_real *) gconf;
1396

B
Bruce Momjian 已提交
1397 1398 1399 1400 1401
					if (isCommit && (conf->gen.status & GUC_HAVE_TENTATIVE))
					{
						conf->session_val = conf->tentative_val;
						conf->gen.session_source = conf->gen.tentative_source;
					}
1402

B
Bruce Momjian 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
					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;
1414
				}
B
Bruce Momjian 已提交
1415
			case PGC_STRING:
1416
				{
B
Bruce Momjian 已提交
1417
					struct config_string *conf = (struct config_string *) gconf;
1418

B
Bruce Momjian 已提交
1419 1420 1421 1422 1423 1424 1425 1426
					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);
1427

B
Bruce Momjian 已提交
1428
					if (*conf->variable != conf->session_val)
1429
					{
B
Bruce Momjian 已提交
1430
						char	   *str = conf->session_val;
1431

B
Bruce Momjian 已提交
1432
						if (conf->assign_hook)
1433
						{
B
Bruce Momjian 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
							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);
							}
1448 1449
						}

B
Bruce Momjian 已提交
1450 1451 1452 1453 1454
						SET_STRING_VARIABLE(conf, str);
					}
					conf->gen.source = conf->gen.session_source;
					conf->gen.status = 0;
					break;
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 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
				}
		}
	}

	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;
1679 1680 1681 1682 1683
			}
			break;
		case PGC_USERSET:
			/* always okay */
			break;
1684
	}
1685

1686
	/* Should we report errors interactively? */
1687
	interactive = (source >= PGC_S_SESSION);
B
Bruce Momjian 已提交
1688

1689
	/*
B
Bruce Momjian 已提交
1690
	 * Should we set reset/session values?	(If so, the behavior is not
1691 1692 1693
	 * transactional.)
	 */
	makeDefault = DoIt && (source <= PGC_S_OVERRIDE) && (value != NULL);
1694 1695 1696

	/*
	 * Ignore attempted set if overridden by previously processed setting.
B
Bruce Momjian 已提交
1697 1698 1699 1700 1701
	 * 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.
1702 1703 1704 1705 1706
	 */
	if (record->source > source)
	{
		if (DoIt && !makeDefault)
		{
1707
			elog(DEBUG2, "%s: setting ignored because previous source is higher priority",
1708 1709 1710 1711 1712 1713
				 name);
			return true;
		}
		DoIt = false;			/* we won't change the variable itself */
	}

1714 1715 1716
	/*
	 * Evaluate value and set variable
	 */
1717
	switch (record->vartype)
1718 1719 1720
	{
		case PGC_BOOL:
			{
B
Bruce Momjian 已提交
1721
				struct config_bool *conf = (struct config_bool *) record;
1722
				bool		newval;
B
Bruce Momjian 已提交
1723 1724

				if (value)
1725
				{
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
					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 已提交
1738

1739 1740
				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
B
Bruce Momjian 已提交
1741
					{
1742 1743
						elog(elevel, "invalid value for option '%s': %d",
							 name, (int) newval);
B
Bruce Momjian 已提交
1744 1745
						return false;
					}
1746 1747 1748

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1749
					if (DoIt)
1750
					{
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
						*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;
1778
					}
1779
				}
B
Bruce Momjian 已提交
1780
				break;
1781
			}
1782 1783

		case PGC_INT:
1784
			{
B
Bruce Momjian 已提交
1785
				struct config_int *conf = (struct config_int *) record;
1786
				int			newval;
1787

B
Bruce Momjian 已提交
1788
				if (value)
1789
				{
1790
					if (!parse_int(value, &newval))
B
Bruce Momjian 已提交
1791
					{
1792 1793
						elog(elevel, "option '%s' expects an integer value",
							 name);
B
Bruce Momjian 已提交
1794 1795
						return false;
					}
1796
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1797 1798 1799
					{
						elog(elevel, "option '%s' value %d is outside"
							 " of permissible range [%d .. %d]",
1800
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1801 1802
						return false;
					}
1803 1804 1805 1806 1807 1808 1809 1810 1811
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1812 1813
					{
						elog(elevel, "invalid value for option '%s': %d",
1814
							 name, newval);
1815 1816
						return false;
					}
1817 1818 1819

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1820
					if (DoIt)
1821
					{
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
						*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;
1849
					}
1850
				}
B
Bruce Momjian 已提交
1851
				break;
1852
			}
1853 1854

		case PGC_REAL:
1855
			{
B
Bruce Momjian 已提交
1856
				struct config_real *conf = (struct config_real *) record;
1857
				double		newval;
1858

B
Bruce Momjian 已提交
1859
				if (value)
1860
				{
1861
					if (!parse_real(value, &newval))
B
Bruce Momjian 已提交
1862
					{
1863 1864
						elog(elevel, "option '%s' expects a real number",
							 name);
B
Bruce Momjian 已提交
1865 1866
						return false;
					}
1867
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1868 1869 1870
					{
						elog(elevel, "option '%s' value %g is outside"
							 " of permissible range [%g .. %g]",
1871
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1872 1873
						return false;
					}
1874 1875 1876 1877 1878 1879 1880 1881 1882
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1883 1884
					{
						elog(elevel, "invalid value for option '%s': %g",
1885
							 name, newval);
1886 1887
						return false;
					}
1888 1889 1890

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1891
					if (DoIt)
1892
					{
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
						*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;
1920
					}
1921
				}
B
Bruce Momjian 已提交
1922
				break;
1923
			}
1924 1925 1926

		case PGC_STRING:
			{
B
Bruce Momjian 已提交
1927
				struct config_string *conf = (struct config_string *) record;
1928
				char	   *newval;
B
Bruce Momjian 已提交
1929 1930

				if (value)
1931
				{
1932 1933 1934 1935 1936 1937 1938 1939 1940
					newval = strdup(value);
					if (newval == NULL)
					{
						elog(elevel, "out of memory");
						return false;
					}
				}
				else if (conf->reset_val)
				{
1941 1942
					/*
					 * We could possibly avoid strdup here, but easier to
B
Bruce Momjian 已提交
1943 1944
					 * make this case work the same as the normal
					 * assignment case.
1945
					 */
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
					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)
				{
B
Bruce Momjian 已提交
1970
					const char *hookresult;
1971 1972 1973 1974 1975

					hookresult = (*conf->assign_hook) (newval,
													   DoIt, interactive);
					guc_string_workspace = NULL;
					if (hookresult == NULL)
B
Bruce Momjian 已提交
1976
					{
1977
						free(newval);
1978
						elog(elevel, "invalid value for option '%s': '%s'",
1979
							 name, value ? value : "");
B
Bruce Momjian 已提交
1980 1981
						return false;
					}
1982
					else if (hookresult != newval)
B
Bruce Momjian 已提交
1983
					{
1984
						free(newval);
B
Bruce Momjian 已提交
1985

1986
						/*
B
Bruce Momjian 已提交
1987 1988 1989 1990 1991 1992 1993
						 * 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.
1994 1995 1996 1997 1998 1999
						 */
						newval = (char *) hookresult;
					}
				}

				guc_string_workspace = NULL;
B
Bruce Momjian 已提交
2000

2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
				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 已提交
2011
						{
2012 2013
							SET_STRING_RESET_VAL(conf, newval);
							conf->gen.reset_source = source;
B
Bruce Momjian 已提交
2014
						}
2015
						if (conf->gen.session_source <= source)
2016
						{
2017 2018
							SET_STRING_SESSION_VAL(conf, newval);
							conf->gen.session_source = source;
2019
						}
2020 2021 2022 2023 2024
						/* Perhaps we didn't install newval anywhere */
						if (newval != *conf->variable &&
							newval != conf->session_val &&
							newval != conf->reset_val)
							free(newval);
B
Bruce Momjian 已提交
2025
					}
2026
					else if (isLocal)
2027
					{
2028 2029
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
2030
					}
2031
					else
2032
					{
2033 2034 2035 2036
						SET_STRING_TENTATIVE_VAL(conf, newval);
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
2037
					}
2038 2039 2040
				}
				else
					free(newval);
B
Bruce Momjian 已提交
2041
				break;
2042
			}
2043
	}
2044

2045 2046 2047 2048 2049 2050 2051
	return true;
}



/*
 * Set a config option to the given value. See also set_config_option,
B
Bruce Momjian 已提交
2052
 * this is just the wrapper to be called from outside GUC.	NB: this
2053
 * is used only for non-transactional operations.
2054 2055
 */
void
2056
SetConfigOption(const char *name, const char *value,
2057
				GucContext context, GucSource source)
2058
{
2059
	(void) set_config_option(name, value, context, source, false, true);
2060 2061 2062 2063 2064
}



/*
2065 2066
 * Fetch the current value of the option `name'. If the option doesn't exist,
 * throw an elog and don't return.
2067 2068 2069 2070 2071
 *
 * 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 已提交
2072
GetConfigOption(const char *name)
2073
{
B
Bruce Momjian 已提交
2074
	struct config_generic *record;
2075 2076
	static char buffer[256];

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

2081
	switch (record->vartype)
2082 2083
	{
		case PGC_BOOL:
B
Bruce Momjian 已提交
2084
			return *((struct config_bool *) record)->variable ? "on" : "off";
2085

2086
		case PGC_INT:
2087 2088
			snprintf(buffer, sizeof(buffer), "%d",
					 *((struct config_int *) record)->variable);
2089 2090
			return buffer;

2091
		case PGC_REAL:
2092 2093
			snprintf(buffer, sizeof(buffer), "%g",
					 *((struct config_real *) record)->variable);
2094 2095 2096
			return buffer;

		case PGC_STRING:
B
Bruce Momjian 已提交
2097
			return *((struct config_string *) record)->variable;
2098 2099 2100
	}
	return NULL;
}
2101

2102 2103 2104 2105 2106
/*
 * Get the RESET value associated with the given option.
 */
const char *
GetConfigOptionResetString(const char *name)
2107
{
2108 2109
	struct config_generic *record;
	static char buffer[256];
2110

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

	switch (record->vartype)
2116 2117
	{
		case PGC_BOOL:
2118
			return ((struct config_bool *) record)->reset_val ? "on" : "off";
2119

2120
		case PGC_INT:
2121
			snprintf(buffer, sizeof(buffer), "%d",
2122 2123
					 ((struct config_int *) record)->reset_val);
			return buffer;
2124 2125

		case PGC_REAL:
2126
			snprintf(buffer, sizeof(buffer), "%g",
2127 2128
					 ((struct config_real *) record)->reset_val);
			return buffer;
2129 2130

		case PGC_STRING:
2131 2132 2133 2134
			return ((struct config_string *) record)->reset_val;
	}
	return NULL;
}
2135

2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154


/*
 * 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;
B
Bruce Momjian 已提交
2155
	List	   *l;
2156

2157 2158 2159 2160
	/*
	 * Fast path if just DEFAULT.  We do not check the variable name in
	 * this case --- necessary for RESET ALL to work correctly.
	 */
2161 2162 2163
	if (args == NIL)
		return NULL;

2164
	/* Else get flags for the variable */
2165 2166
	record = find_option(name);
	if (record == NULL)
2167 2168 2169
		elog(ERROR, "'%s' is not a valid option name", name);

	flags = record->flags;
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206

	/* 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.
					 */
B
Bruce Momjian 已提交
2207 2208
					Datum		interval;
					char	   *intervalout;
2209 2210 2211 2212 2213

					interval =
						DirectFunctionCall3(interval_in,
											CStringGetDatum(val),
											ObjectIdGetDatum(InvalidOid),
B
Bruce Momjian 已提交
2214
								   Int32GetDatum(arg->typename->typmod));
2215 2216 2217 2218

					intervalout =
						DatumGetCString(DirectFunctionCall3(interval_out,
															interval,
B
Bruce Momjian 已提交
2219 2220
											ObjectIdGetDatum(InvalidOid),
													 Int32GetDatum(-1)));
2221 2222 2223 2224 2225
					appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
				}
				else
				{
					/*
B
Bruce Momjian 已提交
2226 2227
					 * Plain string literal or identifier.	For quote
					 * mode, quote it if it's not a vanilla identifier.
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
					 */
					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;
		}
2239
	}
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261

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

2262 2263 2264 2265 2266 2267
/*
 * SET command wrapped as a SQL callable function.
 */
Datum
set_config_by_name(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
2268 2269 2270 2271 2272
	char	   *name;
	char	   *value;
	char	   *new_value;
	bool		is_local;
	text	   *result_text;
2273 2274 2275 2276 2277

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

	/* Get the GUC variable name */
2278
	name = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2279 2280 2281 2282 2283

	/* Get the desired value or set to NULL for a reset request */
	if (PG_ARGISNULL(1))
		value = NULL;
	else
2284
		value = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(1)));
2285 2286

	/*
B
Bruce Momjian 已提交
2287 2288
	 * Get the desired state of is_local. Default to false if provided
	 * value is NULL
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
	 */
	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 */
2304
	new_value = GetConfigOptionByName(name, NULL);
2305 2306 2307 2308 2309 2310 2311 2312

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

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

2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
/*
 * 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)
{
2349
	TupOutputState *tstate;
B
Bruce Momjian 已提交
2350 2351 2352 2353
	TupleDesc	tupdesc;
	CommandDest dest = whereToSendOutput;
	const char *varname;
	char	   *value;
2354

2355 2356 2357
	/* Get the value and canonical spelling of name */
	value = GetConfigOptionByName(name, &varname);

2358
	/* need a tuple descriptor representing a single TEXT column */
2359
	tupdesc = CreateTemplateTupleDesc(1, false);
2360
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, (char *) varname,
2361
					   TEXTOID, -1, 0, false);
2362

2363 2364 2365 2366
	/* prepare for projection of tuples */
	tstate = begin_tup_output_tupdesc(dest, tupdesc);

	/* Send it */
2367
	do_text_output_oneline(tstate, value);
2368 2369

	end_tup_output(tstate);
2370 2371
}

2372 2373 2374
/*
 * SHOW ALL command
 */
2375 2376 2377 2378
void
ShowAllGUCConfig(void)
{
	int			i;
2379
	TupOutputState *tstate;
B
Bruce Momjian 已提交
2380 2381 2382
	TupleDesc	tupdesc;
	CommandDest dest = whereToSendOutput;
	char	   *values[2];
2383 2384

	/* need a tuple descriptor representing two TEXT columns */
2385
	tupdesc = CreateTemplateTupleDesc(2, false);
2386 2387 2388 2389 2390 2391 2392
	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);
2393

2394 2395
	for (i = 0; i < num_guc_variables; i++)
	{
2396 2397 2398 2399
		struct config_generic *conf = guc_variables[i];

		if (conf->flags & GUC_NO_SHOW_ALL)
			continue;
2400 2401

		/* assign to the values array */
2402 2403
		values[0] = (char *) conf->name;
		values[1] = _ShowOption(conf);
2404 2405 2406

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

2408 2409 2410
		/* clean up */
		if (values[1] != NULL)
			pfree(values[1]);
2411
	}
2412 2413

	end_tup_output(tstate);
2414
}
2415

2416
/*
2417 2418
 * Return GUC variable value by name; optionally return canonical
 * form of name.  Return value is palloc'd.
2419 2420
 */
char *
2421
GetConfigOptionByName(const char *name, const char **varname)
2422 2423 2424 2425 2426 2427 2428
{
	struct config_generic *record;

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

2429 2430 2431
	if (varname)
		*varname = record->name;

2432 2433 2434 2435
	return _ShowOption(record);
}

/*
2436 2437
 * Return GUC variable value by variable number; optionally return canonical
 * form of name.  Return value is palloc'd.
2438 2439
 */
char *
2440
GetConfigOptionByNum(int varnum, const char **varname, bool *noshow)
2441
{
2442 2443 2444 2445 2446 2447
	struct config_generic *conf;

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

	conf = guc_variables[varnum];
2448

2449 2450
	if (varname)
		*varname = conf->name;
2451

2452 2453 2454
	if (noshow)
		*noshow = (conf->flags & GUC_NO_SHOW_ALL) ? true : false;

2455
	return _ShowOption(conf);
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
}

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

/*
 * show_config_by_name - equiv to SHOW X command but implemented as
 * a function.
 */
Datum
show_config_by_name(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
2474 2475 2476
	char	   *varname;
	char	   *varval;
	text	   *result_text;
2477 2478

	/* Get the GUC variable name */
2479
	varname = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2480 2481

	/* Get the value */
2482
	varval = GetConfigOptionByName(varname, NULL);
2483 2484 2485 2486 2487 2488 2489 2490

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

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

2491 2492 2493 2494 2495 2496 2497
/*
 * show_all_settings - equiv to SHOW ALL command but implemented as
 * a Table Function.
 */
Datum
show_all_settings(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
2498 2499 2500 2501 2502 2503 2504
	FuncCallContext *funcctx;
	TupleDesc	tupdesc;
	int			call_cntr;
	int			max_calls;
	TupleTableSlot *slot;
	AttInMetadata *attinmeta;
	MemoryContext oldcontext;
2505 2506

	/* stuff done only on the first call of the function */
B
Bruce Momjian 已提交
2507 2508
	if (SRF_IS_FIRSTCALL())
	{
2509
		/* create a function context for cross-call persistence */
B
Bruce Momjian 已提交
2510
		funcctx = SRF_FIRSTCALL_INIT();
2511

B
Bruce Momjian 已提交
2512 2513 2514 2515
		/*
		 * switch to memory context appropriate for multiple function
		 * calls
		 */
2516 2517
		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

2518
		/* need a tuple descriptor representing two TEXT columns */
2519
		tupdesc = CreateTemplateTupleDesc(2, false);
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
		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;

		/*
B
Bruce Momjian 已提交
2532 2533
		 * Generate attribute metadata needed later to produce tuples from
		 * raw C strings
2534 2535 2536 2537 2538 2539
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

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

		MemoryContextSwitchTo(oldcontext);
B
Bruce Momjian 已提交
2542
	}
2543 2544

	/* stuff done on every call of the function */
B
Bruce Momjian 已提交
2545
	funcctx = SRF_PERCALL_SETUP();
2546 2547 2548 2549 2550 2551

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

B
Bruce Momjian 已提交
2552 2553
	if (call_cntr < max_calls)	/* do when there is more left to send */
	{
2554
		char	   *values[2];
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
		char	   *varname;
		char	   *varval;
		bool		noshow;
		HeapTuple	tuple;
		Datum		result;

		/*
		 * Get the next visible GUC variable name and value
		 */
		do
		{
2566 2567 2568
			varval = GetConfigOptionByNum(call_cntr,
										  (const char **) &varname,
										  &noshow);
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
			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)
B
Bruce Momjian 已提交
2580
					SRF_RETURN_DONE(funcctx);
2581 2582 2583 2584
			}
		} while (noshow);

		/*
B
Bruce Momjian 已提交
2585 2586 2587
		 * 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.
2588
		 */
2589 2590
		values[0] = varname;
		values[1] = varval;
2591 2592 2593 2594 2595 2596 2597 2598 2599

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

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

		/* Clean up */
		if (varval != NULL)
2600
			pfree(varval);
2601

B
Bruce Momjian 已提交
2602 2603 2604 2605 2606
		SRF_RETURN_NEXT(funcctx, result);
	}
	else
/* do when there is no more left */
		SRF_RETURN_DONE(funcctx);
2607 2608
}

2609
static char *
B
Bruce Momjian 已提交
2610
_ShowOption(struct config_generic * record)
2611 2612 2613
{
	char		buffer[256];
	const char *val;
2614

2615 2616 2617 2618 2619
	switch (record->vartype)
	{
		case PGC_BOOL:
			{
				struct config_bool *conf = (struct config_bool *) record;
2620

2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 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
				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;
2661

2662 2663 2664 2665 2666 2667 2668 2669
				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else if (*conf->variable && **conf->variable)
					val = *conf->variable;
				else
					val = "unset";
			}
			break;
2670

2671 2672 2673 2674 2675
		default:
			/* just to keep compiler quiet */
			val = "???";
			break;
	}
2676

2677
	return pstrdup(val);
2678
}
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688


/*
 * 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 已提交
2689
ParseLongOption(const char *string, char **name, char **value)
2690
{
B
Bruce Momjian 已提交
2691 2692
	size_t		equal_pos;
	char	   *cp;
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711

	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 已提交
2712
	else
2713
	{
2714
		/* no equal sign in string */
2715 2716 2717 2718 2719 2720
		*name = strdup(string);
		if (!*name)
			elog(FATAL, "out of memory");
		*value = NULL;
	}

B
Bruce Momjian 已提交
2721
	for (cp = *name; *cp; cp++)
2722 2723 2724
		if (*cp == '-')
			*cp = '_';
}
2725 2726 2727



T
Tatsuo Ishii 已提交
2728
#ifdef HAVE_SYSLOG
2729

2730 2731
static const char *
assign_facility(const char *facility, bool doit, bool interactive)
2732
{
B
Bruce Momjian 已提交
2733
	if (strcasecmp(facility, "LOCAL0") == 0)
2734
		return facility;
B
Bruce Momjian 已提交
2735
	if (strcasecmp(facility, "LOCAL1") == 0)
2736
		return facility;
B
Bruce Momjian 已提交
2737
	if (strcasecmp(facility, "LOCAL2") == 0)
2738
		return facility;
B
Bruce Momjian 已提交
2739
	if (strcasecmp(facility, "LOCAL3") == 0)
2740
		return facility;
B
Bruce Momjian 已提交
2741
	if (strcasecmp(facility, "LOCAL4") == 0)
2742
		return facility;
B
Bruce Momjian 已提交
2743
	if (strcasecmp(facility, "LOCAL5") == 0)
2744
		return facility;
B
Bruce Momjian 已提交
2745
	if (strcasecmp(facility, "LOCAL6") == 0)
2746
		return facility;
B
Bruce Momjian 已提交
2747
	if (strcasecmp(facility, "LOCAL7") == 0)
2748 2749
		return facility;
	return NULL;
2750
}
2751
#endif
2752 2753


2754 2755
static const char *
assign_defaultxactisolevel(const char *newval, bool doit, bool interactive)
2756
{
2757
	if (strcasecmp(newval, "serializable") == 0)
B
Bruce Momjian 已提交
2758 2759 2760 2761
	{
		if (doit)
			DefaultXactIsoLevel = XACT_SERIALIZABLE;
	}
2762
	else if (strcasecmp(newval, "read committed") == 0)
B
Bruce Momjian 已提交
2763 2764 2765 2766
	{
		if (doit)
			DefaultXactIsoLevel = XACT_READ_COMMITTED;
	}
2767
	else
2768 2769
		return NULL;
	return newval;
2770
}
2771 2772


2773 2774
/*
 * Handle options fetched from pg_database.datconfig or pg_shadow.useconfig.
2775
 * The array parameter must be an array of TEXT (it must not be NULL).
2776
 */
2777 2778 2779
void
ProcessGUCArray(ArrayType *array, GucSource source)
{
B
Bruce Momjian 已提交
2780
	int			i;
2781

2782
	Assert(array != NULL);
2783 2784 2785
	Assert(ARR_ELEMTYPE(array) == TEXTOID);
	Assert(ARR_NDIM(array) == 1);
	Assert(ARR_LBOUND(array)[0] == 1);
2786
	Assert(source == PGC_S_DATABASE || source == PGC_S_USER);
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796

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

		d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
2797 2798 2799 2800
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
2801 2802 2803 2804 2805 2806
					  &isnull);

		if (isnull)
			continue;

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

2808 2809 2810
		ParseLongOption(s, &name, &value);
		if (!value)
		{
2811
			elog(WARNING, "cannot parse setting \"%s\"", name);
2812
			free(name);
B
Bruce Momjian 已提交
2813
			continue;
2814 2815
		}

2816
		/*
B
Bruce Momjian 已提交
2817 2818
		 * We process all these options at SUSET level.  We assume that
		 * the right to insert an option into pg_database or pg_shadow was
2819 2820
		 * checked when it was inserted.
		 */
2821
		SetConfigOption(name, value, PGC_SUSET, source);
2822 2823 2824 2825

		free(name);
		if (value)
			free(value);
2826 2827 2828 2829
	}
}


2830 2831 2832 2833
/*
 * Add an entry to an option array.  The array parameter may be NULL
 * to indicate the current table entry is NULL.
 */
2834 2835 2836
ArrayType *
GUCArrayAdd(ArrayType *array, const char *name, const char *value)
{
2837
	const char *varname;
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
	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,
2848
					  PGC_S_SESSION, false, false);
2849

2850 2851 2852 2853
	/* convert name to canonical spelling, so we can use plain strcmp */
	(void) GetConfigOptionByName(name, &varname);
	name = varname;

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

2858 2859
	if (array)
	{
B
Bruce Momjian 已提交
2860 2861 2862
		int			index;
		bool		isnull;
		int			i;
2863

2864 2865 2866 2867
		Assert(ARR_ELEMTYPE(array) == TEXTOID);
		Assert(ARR_NDIM(array) == 1);
		Assert(ARR_LBOUND(array)[0] == 1);

B
Bruce Momjian 已提交
2868
		index = ARR_DIMS(array)[0] + 1; /* add after end */
2869 2870 2871 2872 2873 2874 2875

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

			d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
2876 2877 2878 2879
						  -1 /* varlenarray */ ,
						  -1 /* TEXT's typlen */ ,
						  false /* TEXT's typbyval */ ,
						  'i' /* TEXT's typalign */ ,
2880
						  &isnull);
2881 2882
			if (isnull)
				continue;
2883
			current = DatumGetCString(DirectFunctionCall1(textout, d));
B
Bruce Momjian 已提交
2884
			if (strncmp(current, newval, strlen(name) + 1) == 0)
2885 2886 2887 2888 2889 2890 2891
			{
				index = i;
				break;
			}
		}

		isnull = false;
2892 2893
		a = array_set(array, 1, &index,
					  datum,
B
Bruce Momjian 已提交
2894 2895 2896 2897
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
2898
					  &isnull);
2899 2900
	}
	else
2901 2902 2903
		a = construct_array(&datum, 1,
							TEXTOID,
							-1, false, 'i');
2904 2905 2906 2907 2908

	return a;
}


2909 2910 2911 2912 2913
/*
 * Delete an entry from an option array.  The array parameter may be NULL
 * to indicate the current table entry is NULL.  Also, if the return value
 * is NULL then a null should be stored.
 */
2914 2915 2916
ArrayType *
GUCArrayDelete(ArrayType *array, const char *name)
{
2917
	const char *varname;
B
Bruce Momjian 已提交
2918 2919 2920
	ArrayType  *newarray;
	int			i;
	int			index;
2921 2922 2923 2924 2925 2926

	Assert(name);

	/* test if the option is valid */
	set_config_option(name, NULL,
					  superuser() ? PGC_SUSET : PGC_USERSET,
2927
					  PGC_S_SESSION, false, false);
2928

2929 2930 2931 2932
	/* convert name to canonical spelling, so we can use plain strcmp */
	(void) GetConfigOptionByName(name, &varname);
	name = varname;

2933 2934 2935 2936 2937
	/* if array is currently null, then surely nothing to delete */
	if (!array)
		return NULL;

	newarray = NULL;
2938 2939 2940 2941 2942 2943 2944 2945 2946
	index = 1;

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

		d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
2947 2948 2949 2950
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
2951
					  &isnull);
2952 2953
		if (isnull)
			continue;
2954 2955
		val = DatumGetCString(DirectFunctionCall1(textout, d));

2956
		/* ignore entry if it's what we want to delete */
B
Bruce Momjian 已提交
2957
		if (strncmp(val, name, strlen(name)) == 0
2958 2959 2960
			&& val[strlen(name)] == '=')
			continue;

2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977
		/* else add it to the output array */
		if (newarray)
		{
			isnull = false;
			newarray = array_set(newarray, 1, &index,
								 d,
								 -1 /* varlenarray */ ,
								 -1 /* TEXT's typlen */ ,
								 false /* TEXT's typbyval */ ,
								 'i' /* TEXT's typalign */ ,
								 &isnull);
		}
		else
			newarray = construct_array(&d, 1,
									   TEXTOID,
									   -1, false, 'i');

2978 2979 2980 2981 2982
		index++;
	}

	return newarray;
}
2983 2984

const char *
2985
assign_log_min_messages(const char *newval,
2986 2987
						   bool doit, bool interactive)
{
2988
	return (assign_msglvl(&log_min_messages, newval, doit, interactive));
2989 2990 2991 2992 2993 2994
}

const char *
assign_client_min_messages(const char *newval,
						   bool doit, bool interactive)
{
B
Bruce Momjian 已提交
2995
	return (assign_msglvl(&client_min_messages, newval, doit, interactive));
2996 2997 2998 2999 3000
}

const char *
assign_min_error_statement(const char *newval, bool doit, bool interactive)
{
B
Bruce Momjian 已提交
3001
	return (assign_msglvl(&log_min_error_statement, newval, doit, interactive));
3002 3003 3004 3005 3006 3007
}

static const char *
assign_msglvl(int *var, const char *newval, bool doit, bool interactive)
{
	if (strcasecmp(newval, "debug") == 0)
B
Bruce Momjian 已提交
3008 3009 3010 3011
	{
		if (doit)
			(*var) = DEBUG1;
	}
3012
	else if (strcasecmp(newval, "debug5") == 0)
B
Bruce Momjian 已提交
3013 3014 3015 3016
	{
		if (doit)
			(*var) = DEBUG5;
	}
3017
	else if (strcasecmp(newval, "debug4") == 0)
B
Bruce Momjian 已提交
3018 3019 3020 3021
	{
		if (doit)
			(*var) = DEBUG4;
	}
3022
	else if (strcasecmp(newval, "debug3") == 0)
B
Bruce Momjian 已提交
3023 3024 3025 3026
	{
		if (doit)
			(*var) = DEBUG3;
	}
3027
	else if (strcasecmp(newval, "debug2") == 0)
B
Bruce Momjian 已提交
3028 3029 3030 3031
	{
		if (doit)
			(*var) = DEBUG2;
	}
3032
	else if (strcasecmp(newval, "debug1") == 0)
B
Bruce Momjian 已提交
3033 3034 3035 3036
	{
		if (doit)
			(*var) = DEBUG1;
	}
3037
	else if (strcasecmp(newval, "log") == 0)
B
Bruce Momjian 已提交
3038 3039 3040 3041
	{
		if (doit)
			(*var) = LOG;
	}
3042
	else if (strcasecmp(newval, "info") == 0)
B
Bruce Momjian 已提交
3043 3044 3045 3046
	{
		if (doit)
			(*var) = INFO;
	}
3047
	else if (strcasecmp(newval, "notice") == 0)
B
Bruce Momjian 已提交
3048 3049 3050 3051
	{
		if (doit)
			(*var) = NOTICE;
	}
3052
	else if (strcasecmp(newval, "warning") == 0)
B
Bruce Momjian 已提交
3053 3054 3055 3056
	{
		if (doit)
			(*var) = WARNING;
	}
3057
	else if (strcasecmp(newval, "error") == 0)
B
Bruce Momjian 已提交
3058 3059 3060 3061
	{
		if (doit)
			(*var) = ERROR;
	}
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
	/* We allow FATAL/PANIC for client-side messages too. */
	else if (strcasecmp(newval, "fatal") == 0)
	{
		if (doit)
			(*var) = FATAL;
	}
	else if (strcasecmp(newval, "panic") == 0)
	{
		if (doit)
			(*var) = PANIC;
	}
3073 3074 3075 3076
	else
		return NULL;			/* fail */
	return newval;				/* OK */
}
3077 3078

#include "guc-file.c"