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

#include "postgres.h"

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

#include "utils/guc.h"

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


59 60 61 62
#ifndef PG_KRB_SRVTAB
#define PG_KRB_SRVTAB ""
#endif

B
Bruce Momjian 已提交
63 64 65
#ifdef EXEC_BACKEND
#define CONFIG_EXEC_PARAMS "global/config_exec_params"
#endif
66 67

/* XXX these should appear in other modules' header files */
68
extern bool Log_connections;
69 70
extern int	PreAuthDelay;
extern int	AuthenticationTimeout;
B
Bruce Momjian 已提交
71
extern int	CheckPointTimeout;
72
extern bool autocommit;
B
Bruce Momjian 已提交
73 74
extern int	CommitDelay;
extern int	CommitSiblings;
75
extern char *preload_libraries_string;
76

T
Tatsuo Ishii 已提交
77
#ifdef HAVE_SYSLOG
78
extern char *Syslog_facility;
79
extern char *Syslog_ident;
80

81
static const char *assign_facility(const char *facility,
B
Bruce Momjian 已提交
82
				bool doit, bool interactive);
83
#endif
84

85 86 87 88 89 90 91 92
static const char *assign_defaultxactisolevel(const char *newval,
						   bool doit, bool interactive);
static const char *assign_log_min_messages(const char *newval,
						   bool doit, bool interactive);
static const char *assign_client_min_messages(const char *newval,
						   bool doit, bool interactive);
static const char *assign_min_error_statement(const char *newval, bool doit,
						   bool interactive);
93
static const char *assign_msglvl(int *var, const char *newval,
B
Bruce Momjian 已提交
94
			  bool doit, bool interactive);
95

96

97 98 99
/*
 * Debugging options
 */
100
#ifdef USE_ASSERT_CHECKING
B
Bruce Momjian 已提交
101
bool		assert_enabled = true;
102
#endif
B
Rename:  
Bruce Momjian 已提交
103 104
bool		log_statement = false;
bool		log_duration = false;
B
Bruce Momjian 已提交
105 106 107 108
bool		Debug_print_plan = false;
bool		Debug_print_parse = false;
bool		Debug_print_rewritten = false;
bool		Debug_pretty_print = false;
109
bool		Explain_pretty_print = true;
110

B
Rename:  
Bruce Momjian 已提交
111 112 113 114
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 已提交
115
												 * three above together */
116
bool		log_btree_build_stats = false;
117

B
Bruce Momjian 已提交
118
bool		SQL_inheritance = true;
119

120 121
bool		Australian_timezones = false;

122
bool		Password_encryption = true;
123

124
int			log_min_error_statement = PANIC;
125
int			log_min_messages = NOTICE;
126
int			client_min_messages = NOTICE;
127 128


129 130 131 132 133
/*
 * 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.
 */
134 135 136
static char *log_min_error_statement_str;
static char *log_min_messages_str;
static char *client_min_messages_str;
137 138 139 140
static double phony_random_seed;
static char *client_encoding_string;
static char *datestyle_string;
static char *default_iso_level_string;
141 142
static char *locale_collate;
static char *locale_ctype;
143
static char *regex_flavor_string;
144
static char *server_encoding_string;
145
static char *server_version_string;
146 147 148 149
static char *session_authorization_string;
static char *timezone_string;
static char *XactIsoLevel_string;

150

151 152
/*
 * Declarations for GUC tables
153 154
 *
 * See src/backend/utils/misc/README for design notes.
155
 */
156 157
enum config_type
{
158 159 160 161
	PGC_BOOL,
	PGC_INT,
	PGC_REAL,
	PGC_STRING
162 163
};

164
/* Generic fields applicable to all types of variables */
165 166
struct config_generic
{
167 168 169 170 171 172 173 174
	/* 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 已提交
175 176
	GucSource	session_source; /* source of the session_value */
	GucSource	tentative_source;		/* source of the tentative_value */
177
	GucSource	source;			/* source of the current actual value */
178 179
};

180
/* bit values in flags field */
B
Bruce Momjian 已提交
181 182 183 184
#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 */
185
#define GUC_REPORT			0x0010		/* auto-report changes to client */
186 187

/* bit values in status field */
B
Bruce Momjian 已提交
188 189
#define GUC_HAVE_TENTATIVE	0x0001		/* tentative value is defined */
#define GUC_HAVE_LOCAL		0x0002		/* a SET LOCAL has been executed */
190 191 192


/* GUC records for specific variable types */
193 194 195

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

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

struct config_real
{
226 227 228
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all but reset_val are constants) */
B
Bruce Momjian 已提交
229
	double	   *variable;
230
	double		reset_val;
B
Bruce Momjian 已提交
231 232
	double		min;
	double		max;
233 234 235 236 237
	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;
238 239 240 241
};

struct config_string
{
242 243 244
	struct config_generic gen;
	/* these fields must be set correctly in initial value: */
	/* (all are constants) */
B
Bruce Momjian 已提交
245
	char	  **variable;
246 247 248 249 250 251 252
	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;
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 283 284 285 286 287 288 289 290 291 292 293 294
/* 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)


295

296 297 298 299 300 301 302 303 304 305 306 307 308 309
/*
 * 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.
 *
310
 * 5. Add it to src/backend/utils/misc/postgresql.conf.sample.
311
 *
312
 * 6. Add it to src/bin/psql/tab-complete.c, if it's a USERSET option.
313
 *
314
 * 7. Don't forget to document the option.
315 316
 */

317

318
/******** option records follow ********/
319 320

static struct config_bool
B
Bruce Momjian 已提交
321
			ConfigureNamesBool[] =
322
{
323
	{
B
Bruce Momjian 已提交
324
		{"enable_seqscan", PGC_USERSET}, &enable_seqscan,
325
		true, NULL, NULL
326
	},
327
	{
B
Bruce Momjian 已提交
328
		{"enable_indexscan", PGC_USERSET}, &enable_indexscan,
329
		true, NULL, NULL
330 331
	},
	{
B
Bruce Momjian 已提交
332
		{"enable_tidscan", PGC_USERSET}, &enable_tidscan,
333
		true, NULL, NULL
334 335
	},
	{
B
Bruce Momjian 已提交
336
		{"enable_sort", PGC_USERSET}, &enable_sort,
337
		true, NULL, NULL
338
	},
339 340 341 342
	{
		{"enable_hashagg", PGC_USERSET}, &enable_hashagg,
		true, NULL, NULL
	},
343
	{
B
Bruce Momjian 已提交
344
		{"enable_nestloop", PGC_USERSET}, &enable_nestloop,
345
		true, NULL, NULL
346 347
	},
	{
B
Bruce Momjian 已提交
348
		{"enable_mergejoin", PGC_USERSET}, &enable_mergejoin,
349
		true, NULL, NULL
350 351
	},
	{
B
Bruce Momjian 已提交
352
		{"enable_hashjoin", PGC_USERSET}, &enable_hashjoin,
353
		true, NULL, NULL
354 355
	},
	{
B
Bruce Momjian 已提交
356
		{"geqo", PGC_USERSET}, &enable_geqo,
357
		true, NULL, NULL
358 359 360
	},

	{
B
Bruce Momjian 已提交
361
		{"tcpip_socket", PGC_POSTMASTER}, &NetServer,
362
		false, NULL, NULL
363 364
	},
	{
B
Bruce Momjian 已提交
365
		{"ssl", PGC_POSTMASTER}, &EnableSSL,
366
		false, NULL, NULL
367 368
	},
	{
B
Bruce Momjian 已提交
369
		{"fsync", PGC_SIGHUP}, &enableFsync,
370
		true, NULL, NULL
371
	},
372 373 374 375
	{
		{"zero_damaged_pages", PGC_SUSET}, &zero_damaged_pages,
		false, NULL, NULL
	},
376
	{
B
Bruce Momjian 已提交
377
		{"silent_mode", PGC_POSTMASTER}, &SilentMode,
378
		false, NULL, NULL
379 380 381
	},

	{
B
Bruce Momjian 已提交
382
		{"log_connections", PGC_BACKEND}, &Log_connections,
383
		false, NULL, NULL
384 385
	},
	{
B
Bruce Momjian 已提交
386
		{"log_timestamp", PGC_SIGHUP}, &Log_timestamp,
387
		false, NULL, NULL
388 389
	},
	{
B
Bruce Momjian 已提交
390
		{"log_pid", PGC_SIGHUP}, &Log_pid,
391
		false, NULL, NULL
392
	},
393

394
#ifdef USE_ASSERT_CHECKING
395
	{
B
Bruce Momjian 已提交
396
		{"debug_assertions", PGC_USERSET}, &assert_enabled,
397
		true, NULL, NULL
398
	},
399 400
#endif

401
	{
B
Rename:  
Bruce Momjian 已提交
402
		{"log_statement", PGC_USERSET}, &log_statement,
403 404 405
		false, NULL, NULL
	},
	{
B
Rename:  
Bruce Momjian 已提交
406
		{"log_duration", PGC_USERSET}, &log_duration,
407
		false, NULL, NULL
408 409
	},
	{
B
Bruce Momjian 已提交
410
		{"debug_print_parse", PGC_USERSET}, &Debug_print_parse,
411
		false, NULL, NULL
412 413
	},
	{
B
Bruce Momjian 已提交
414
		{"debug_print_rewritten", PGC_USERSET}, &Debug_print_rewritten,
415
		false, NULL, NULL
416 417
	},
	{
B
Bruce Momjian 已提交
418
		{"debug_print_plan", PGC_USERSET}, &Debug_print_plan,
419
		false, NULL, NULL
420 421
	},
	{
B
Bruce Momjian 已提交
422
		{"debug_pretty_print", PGC_USERSET}, &Debug_pretty_print,
423
		false, NULL, NULL
424
	},
425

426
	{
B
Rename:  
Bruce Momjian 已提交
427
		{"log_parser_stats", PGC_USERSET}, &log_parser_stats,
428
		false, NULL, NULL
429 430
	},
	{
B
Rename:  
Bruce Momjian 已提交
431
		{"log_planner_stats", PGC_USERSET}, &log_planner_stats,
432
		false, NULL, NULL
433 434
	},
	{
B
Rename:  
Bruce Momjian 已提交
435
		{"log_executor_stats", PGC_USERSET}, &log_executor_stats,
436
		false, NULL, NULL
437 438
	},
	{
B
Rename:  
Bruce Momjian 已提交
439
		{"log_statement_stats", PGC_USERSET}, &log_statement_stats,
440
		false, NULL, NULL
441
	},
442
#ifdef BTREE_BUILD_STATS
443
	{
444
		{"log_btree_build_stats", PGC_SUSET}, &log_btree_build_stats,
445
		false, NULL, NULL
446
	},
447 448
#endif

449
	{
B
Bruce Momjian 已提交
450
		{"explain_pretty_print", PGC_USERSET}, &Explain_pretty_print,
451
		true, NULL, NULL
452 453
	},

454
	{
B
Bruce Momjian 已提交
455
		{"stats_start_collector", PGC_POSTMASTER}, &pgstat_collect_startcollector,
456
		true, NULL, NULL
457 458
	},
	{
B
Bruce Momjian 已提交
459
		{"stats_reset_on_server_start", PGC_POSTMASTER}, &pgstat_collect_resetonpmstart,
460
		true, NULL, NULL
461 462
	},
	{
B
Bruce Momjian 已提交
463
		{"stats_command_string", PGC_SUSET}, &pgstat_collect_querystring,
464
		false, NULL, NULL
465 466
	},
	{
B
Bruce Momjian 已提交
467
		{"stats_row_level", PGC_SUSET}, &pgstat_collect_tuplelevel,
468
		false, NULL, NULL
469 470
	},
	{
B
Bruce Momjian 已提交
471
		{"stats_block_level", PGC_SUSET}, &pgstat_collect_blocklevel,
472
		false, NULL, NULL
473
	},
474

475
	{
B
Bruce Momjian 已提交
476
		{"trace_notify", PGC_USERSET}, &Trace_notify,
477
		false, NULL, NULL
478
	},
479 480

#ifdef LOCK_DEBUG
481
	{
B
Bruce Momjian 已提交
482
		{"trace_locks", PGC_SUSET}, &Trace_locks,
483
		false, NULL, NULL
484 485
	},
	{
B
Bruce Momjian 已提交
486
		{"trace_userlocks", PGC_SUSET}, &Trace_userlocks,
487
		false, NULL, NULL
488 489
	},
	{
B
Bruce Momjian 已提交
490
		{"trace_lwlocks", PGC_SUSET}, &Trace_lwlocks,
491
		false, NULL, NULL
492 493
	},
	{
B
Bruce Momjian 已提交
494
		{"debug_deadlocks", PGC_SUSET}, &Debug_deadlocks,
495
		false, NULL, NULL
496
	},
497 498
#endif

499
	{
500
		{"log_hostname", PGC_SIGHUP}, &log_hostname,
501
		false, NULL, NULL
502 503
	},
	{
504
		{"log_source_port", PGC_SIGHUP}, &LogSourcePort,
505
		false, NULL, NULL
506
	},
507

508
	{
B
Bruce Momjian 已提交
509
		{"sql_inheritance", PGC_USERSET}, &SQL_inheritance,
510
		true, NULL, NULL
511 512
	},
	{
B
Bruce Momjian 已提交
513
		{"australian_timezones", PGC_USERSET}, &Australian_timezones,
514
		false, ClearDateCache, NULL
515 516
	},
	{
B
Bruce Momjian 已提交
517
		{"password_encryption", PGC_USERSET}, &Password_encryption,
518
		true, NULL, NULL
519 520
	},
	{
B
Bruce Momjian 已提交
521
		{"transform_null_equals", PGC_USERSET}, &Transform_null_equals,
522
		false, NULL, NULL
523
	},
524
	{
B
Bruce Momjian 已提交
525
		{"db_user_namespace", PGC_SIGHUP}, &Db_user_namespace,
526 527
		false, NULL, NULL
	},
528
	{
B
Bruce Momjian 已提交
529
		{"autocommit", PGC_USERSET}, &autocommit,
530 531
		true, NULL, NULL
	},
532 533 534 535 536 537 538 539
	{
		{"default_transaction_read_only", PGC_USERSET}, &DefaultXactReadOnly,
		false, NULL, NULL
	},
	{
		{"transaction_read_only", PGC_USERSET, GUC_NO_RESET_ALL}, &XactReadOnly,
		false, NULL, NULL
	},
540

541
	{
B
Bruce Momjian 已提交
542
		{NULL, 0}, NULL, false, NULL, NULL
543
	}
544 545 546 547
};


static struct config_int
B
Bruce Momjian 已提交
548
			ConfigureNamesInt[] =
549
{
550
	{
B
Bruce Momjian 已提交
551
		{"default_statistics_target", PGC_USERSET}, &default_statistics_target,
552 553
		10, 1, 1000, NULL, NULL
	},
554
	{
555 556 557 558 559 560 561 562 563 564
		{"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
565
	},
566
	{
B
Bruce Momjian 已提交
567
		{"geqo_pool_size", PGC_USERSET}, &Geqo_pool_size,
568
		DEFAULT_GEQO_POOL_SIZE, 0, MAX_GEQO_POOL_SIZE, NULL, NULL
569 570
	},
	{
B
Bruce Momjian 已提交
571
		{"geqo_effort", PGC_USERSET}, &Geqo_effort,
572
		1, 1, INT_MAX, NULL, NULL
573 574
	},
	{
B
Bruce Momjian 已提交
575
		{"geqo_generations", PGC_USERSET}, &Geqo_generations,
576
		0, 0, INT_MAX, NULL, NULL
577 578
	},
	{
B
Bruce Momjian 已提交
579
		{"geqo_random_seed", PGC_USERSET}, &Geqo_random_seed,
580
		-1, INT_MIN, INT_MAX, NULL, NULL
581 582 583
	},

	{
584
		{"deadlock_timeout", PGC_SIGHUP}, &DeadlockTimeout,
585
		1000, 0, INT_MAX, NULL, NULL
586
	},
587

T
Tatsuo Ishii 已提交
588
#ifdef HAVE_SYSLOG
589
	{
B
Bruce Momjian 已提交
590
		{"syslog", PGC_SIGHUP}, &Use_syslog,
591
		0, 0, 2, NULL, NULL
592
	},
593 594 595
#endif

	/*
B
Bruce Momjian 已提交
596 597
	 * Note: There is some postprocessing done in PostmasterMain() to make
	 * sure the buffers are at least twice the number of backends, so the
598 599 600
	 * constraints here are partially unused. Similarly, the superuser
	 * reserved number is checked to ensure it is less than the max
	 * backends number.
601
	 */
602
	{
B
Bruce Momjian 已提交
603
		{"max_connections", PGC_POSTMASTER}, &MaxBackends,
604
		DEF_MAXBACKENDS, 1, INT_MAX, NULL, NULL
605
	},
606

607
	{
B
Bruce Momjian 已提交
608
		{"superuser_reserved_connections", PGC_POSTMASTER}, &ReservedBackends,
609 610 611
		2, 0, INT_MAX, NULL, NULL
	},

612
	{
B
Bruce Momjian 已提交
613
		{"shared_buffers", PGC_POSTMASTER}, &NBuffers,
614
		DEF_NBUFFERS, 16, INT_MAX, NULL, NULL
615
	},
616

617
	{
B
Bruce Momjian 已提交
618
		{"port", PGC_POSTMASTER}, &PostPortNumber,
619
		DEF_PGPORT, 1, 65535, NULL, NULL
620
	},
621

622
	{
B
Bruce Momjian 已提交
623
		{"unix_socket_permissions", PGC_POSTMASTER}, &Unix_socket_permissions,
624
		0777, 0000, 0777, NULL, NULL
625
	},
626

627
	{
B
Bruce Momjian 已提交
628
		{"sort_mem", PGC_USERSET}, &SortMem,
629
		1024, 8 * BLCKSZ / 1024, INT_MAX, NULL, NULL
630
	},
631

632
	{
B
Bruce Momjian 已提交
633
		{"vacuum_mem", PGC_USERSET}, &VacuumMem,
634
		8192, 1024, INT_MAX, NULL, NULL
635
	},
636

637
	{
B
Bruce Momjian 已提交
638
		{"max_files_per_process", PGC_BACKEND}, &max_files_per_process,
639
		1000, 25, INT_MAX, NULL, NULL
640
	},
641

642
#ifdef LOCK_DEBUG
643
	{
B
Bruce Momjian 已提交
644
		{"trace_lock_oidmin", PGC_SUSET}, &Trace_lock_oidmin,
645
		BootstrapObjectIdData, 1, INT_MAX, NULL, NULL
646 647
	},
	{
B
Bruce Momjian 已提交
648
		{"trace_lock_table", PGC_SUSET}, &Trace_lock_table,
649
		0, 0, INT_MAX, NULL, NULL
650
	},
651
#endif
652
	{
B
Bruce Momjian 已提交
653
		{"max_expr_depth", PGC_USERSET}, &max_expr_depth,
654
		DEFAULT_MAX_EXPR_DEPTH, 10, INT_MAX, NULL, NULL
655
	},
656

657
	{
B
Bruce Momjian 已提交
658
		{"statement_timeout", PGC_USERSET}, &StatementTimeout,
659 660 661
		0, 0, INT_MAX, NULL, NULL
	},

662
	{
B
Bruce Momjian 已提交
663
		{"max_fsm_relations", PGC_POSTMASTER}, &MaxFSMRelations,
664
		1000, 100, INT_MAX, NULL, NULL
665 666
	},
	{
B
Bruce Momjian 已提交
667
		{"max_fsm_pages", PGC_POSTMASTER}, &MaxFSMPages,
668
		20000, 1000, INT_MAX, NULL, NULL
669
	},
670

671
	{
B
Bruce Momjian 已提交
672
		{"max_locks_per_transaction", PGC_POSTMASTER}, &max_locks_per_xact,
673
		64, 10, INT_MAX, NULL, NULL
674
	},
675

676
	{
B
Bruce Momjian 已提交
677
		{"authentication_timeout", PGC_SIGHUP}, &AuthenticationTimeout,
678
		60, 1, 600, NULL, NULL
679
	},
680

681
	{
B
Bruce Momjian 已提交
682
		{"pre_auth_delay", PGC_SIGHUP}, &PreAuthDelay,
683
		0, 0, 60, NULL, NULL
684
	},
685

686
	{
B
Bruce Momjian 已提交
687
		{"checkpoint_segments", PGC_SIGHUP}, &CheckPointSegments,
688
		3, 1, INT_MAX, NULL, NULL
689
	},
T
Tom Lane 已提交
690

691
	{
B
Bruce Momjian 已提交
692
		{"checkpoint_timeout", PGC_SIGHUP}, &CheckPointTimeout,
693
		300, 30, 3600, NULL, NULL
694
	},
V
Vadim B. Mikheev 已提交
695

696 697 698 699 700
	{
		{"checkpoint_warning", PGC_SIGHUP}, &CheckPointWarning,
		30, 0, INT_MAX, NULL, NULL
	},

701
	{
B
Bruce Momjian 已提交
702
		{"wal_buffers", PGC_POSTMASTER}, &XLOGbuffers,
703
		8, 4, INT_MAX, NULL, NULL
704
	},
V
Vadim B. Mikheev 已提交
705

706
	{
B
Bruce Momjian 已提交
707
		{"wal_debug", PGC_SUSET}, &XLOG_DEBUG,
708
		0, 0, 16, NULL, NULL
709
	},
V
Vadim B. Mikheev 已提交
710

711
	{
B
Bruce Momjian 已提交
712
		{"commit_delay", PGC_USERSET}, &CommitDelay,
713
		0, 0, 100000, NULL, NULL
714
	},
V
Vadim B. Mikheev 已提交
715

716
	{
B
Bruce Momjian 已提交
717
		{"commit_siblings", PGC_USERSET}, &CommitSiblings,
718
		5, 1, 1000, NULL, NULL
719
	},
720

721 722 723 724 725
	{
		{"extra_float_digits", PGC_USERSET}, &extra_float_digits,
		0, -15, 2, NULL, NULL
	},

726
	{
B
Bruce Momjian 已提交
727
		{NULL, 0}, NULL, 0, 0, 0, NULL, NULL
728
	}
729 730 731 732
};


static struct config_real
B
Bruce Momjian 已提交
733
			ConfigureNamesReal[] =
734
{
735
	{
B
Bruce Momjian 已提交
736
		{"effective_cache_size", PGC_USERSET}, &effective_cache_size,
737
		DEFAULT_EFFECTIVE_CACHE_SIZE, 0, DBL_MAX, NULL, NULL
738
	},
739
	{
B
Bruce Momjian 已提交
740
		{"random_page_cost", PGC_USERSET}, &random_page_cost,
741
		DEFAULT_RANDOM_PAGE_COST, 0, DBL_MAX, NULL, NULL
742 743
	},
	{
B
Bruce Momjian 已提交
744
		{"cpu_tuple_cost", PGC_USERSET}, &cpu_tuple_cost,
745
		DEFAULT_CPU_TUPLE_COST, 0, DBL_MAX, NULL, NULL
746 747
	},
	{
B
Bruce Momjian 已提交
748
		{"cpu_index_tuple_cost", PGC_USERSET}, &cpu_index_tuple_cost,
749
		DEFAULT_CPU_INDEX_TUPLE_COST, 0, DBL_MAX, NULL, NULL
750 751
	},
	{
B
Bruce Momjian 已提交
752
		{"cpu_operator_cost", PGC_USERSET}, &cpu_operator_cost,
753
		DEFAULT_CPU_OPERATOR_COST, 0, DBL_MAX, NULL, NULL
754 755 756
	},

	{
B
Bruce Momjian 已提交
757
		{"geqo_selection_bias", PGC_USERSET}, &Geqo_selection_bias,
758 759
		DEFAULT_GEQO_SELECTION_BIAS, MIN_GEQO_SELECTION_BIAS,
		MAX_GEQO_SELECTION_BIAS, NULL, NULL
760 761 762
	},

	{
B
Bruce Momjian 已提交
763
		{"seed", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL},
764 765 766 767 768
		&phony_random_seed,
		0.5, 0.0, 1.0, assign_random_seed, show_random_seed
	},

	{
B
Bruce Momjian 已提交
769
		{NULL, 0}, NULL, 0.0, 0.0, 0.0, NULL, NULL
770
	}
771 772 773 774
};


static struct config_string
B
Bruce Momjian 已提交
775
			ConfigureNamesString[] =
776
{
777
	{
778 779
		{"client_encoding", PGC_USERSET, GUC_REPORT},
		&client_encoding_string,
780
		"SQL_ASCII", assign_client_encoding, NULL
781 782
	},

783
	{
B
Bruce Momjian 已提交
784
		{"client_min_messages", PGC_USERSET}, &client_min_messages_str,
785
		"notice", assign_client_min_messages, NULL
786
	},
787

788
	{
B
Bruce Momjian 已提交
789
		{"log_min_error_statement", PGC_USERSET}, &log_min_error_statement_str,
790
		"panic", assign_min_error_statement, NULL
791 792
	},

793
	{
794 795
		{"DateStyle", PGC_USERSET, GUC_LIST_INPUT | GUC_REPORT},
		&datestyle_string,
796
		"ISO, US", assign_datestyle, show_datestyle
797
	},
798

799
	{
B
Bruce Momjian 已提交
800
		{"default_transaction_isolation", PGC_USERSET}, &default_iso_level_string,
801
		"read committed", assign_defaultxactisolevel, NULL
802 803
	},

804
	{
B
Bruce Momjian 已提交
805
		{"dynamic_library_path", PGC_SUSET}, &Dynamic_library_path,
806 807 808 809
		"$libdir", NULL, NULL
	},

	{
B
Bruce Momjian 已提交
810
		{"krb_server_keyfile", PGC_POSTMASTER}, &pg_krb_server_keyfile,
811
		PG_KRB_SRVTAB, NULL, NULL
812
	},
813

814 815
	/* See main.c about why defaults for LC_foo are not all alike */

816 817 818 819 820 821 822 823 824 825
	{
		{"lc_collate", PGC_INTERNAL}, &locale_collate,
		"C", NULL, NULL
	},

	{
		{"lc_ctype", PGC_INTERNAL}, &locale_ctype,
		"C", NULL, NULL
	},

826
	{
B
Bruce Momjian 已提交
827
		{"lc_messages", PGC_SUSET}, &locale_messages,
828 829 830 831
		"", locale_messages_assign, NULL
	},

	{
B
Bruce Momjian 已提交
832
		{"lc_monetary", PGC_USERSET}, &locale_monetary,
833
		"C", locale_monetary_assign, NULL
834 835 836
	},

	{
B
Bruce Momjian 已提交
837
		{"lc_numeric", PGC_USERSET}, &locale_numeric,
838
		"C", locale_numeric_assign, NULL
839 840 841
	},

	{
B
Bruce Momjian 已提交
842
		{"lc_time", PGC_USERSET}, &locale_time,
843
		"C", locale_time_assign, NULL
844 845
	},

846 847 848 849 850 851
	{
		{"preload_libraries", PGC_POSTMASTER, GUC_LIST_INPUT | GUC_LIST_QUOTE},
		&preload_libraries_string,
		"", NULL, NULL
	},

852 853 854 855 856
	{
		{"regex_flavor", PGC_USERSET}, &regex_flavor_string,
		"advanced", assign_regex_flavor, NULL
	},

857
	{
B
Bruce Momjian 已提交
858
		{"search_path", PGC_USERSET, GUC_LIST_INPUT | GUC_LIST_QUOTE},
859 860
		&namespace_search_path,
		"$user,public", assign_search_path, NULL
861 862 863
	},

	{
864 865 866 867 868 869 870 871 872
		{"server_encoding", PGC_INTERNAL, GUC_REPORT},
		&server_encoding_string,
		"SQL_ASCII", NULL, NULL
	},

	{
		{"server_version", PGC_INTERNAL, GUC_REPORT},
		&server_version_string,
		PG_VERSION, NULL, NULL
873 874 875
	},

	{
876
		{"log_min_messages", PGC_USERSET}, &log_min_messages_str,
877
		"notice", assign_log_min_messages, NULL
878 879
	},

880
	{
B
Bruce Momjian 已提交
881
		{"session_authorization", PGC_USERSET, GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL},
882 883
		&session_authorization_string,
		NULL, assign_session_authorization, show_session_authorization
884 885
	},

T
Tatsuo Ishii 已提交
886
#ifdef HAVE_SYSLOG
887
	{
B
Bruce Momjian 已提交
888
		{"syslog_facility", PGC_POSTMASTER}, &Syslog_facility,
889
		"LOCAL0", assign_facility, NULL
890 891
	},
	{
B
Bruce Momjian 已提交
892
		{"syslog_ident", PGC_POSTMASTER}, &Syslog_ident,
893
		"postgres", NULL, NULL
894
	},
895
#endif
896

897
	{
B
Bruce Momjian 已提交
898
		{"TimeZone", PGC_USERSET}, &timezone_string,
899 900 901 902
		"UNKNOWN", assign_timezone, show_timezone
	},

	{
903
		{"transaction_isolation", PGC_USERSET, GUC_NO_RESET_ALL},
904 905 906 907 908
		&XactIsoLevel_string,
		NULL, assign_XactIsoLevel, show_XactIsoLevel
	},

	{
B
Bruce Momjian 已提交
909
		{"unix_socket_group", PGC_POSTMASTER}, &Unix_socket_group,
910
		"", NULL, NULL
911
	},
912

913
	{
B
Bruce Momjian 已提交
914
		{"unix_socket_directory", PGC_POSTMASTER}, &UnixSocketDir,
915
		"", NULL, NULL
916
	},
917

918
	{
B
Bruce Momjian 已提交
919
		{"virtual_host", PGC_POSTMASTER}, &VirtualHost,
920
		"", NULL, NULL
921
	},
922

923
	{
B
Bruce Momjian 已提交
924
		{"wal_sync_method", PGC_SIGHUP}, &XLOG_sync_method,
925
		XLOG_sync_method_default, assign_xlog_sync_method, NULL
926
	},
927

928
	{
B
Bruce Momjian 已提交
929
		{NULL, 0}, NULL, NULL, NULL, NULL
930
	}
931 932 933 934 935 936
};

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


/*
937
 * Actual lookup of variables is done through this single, sorted array.
938
 */
939
static struct config_generic **guc_variables;
B
Bruce Momjian 已提交
940
static int	num_guc_variables;
941

942
static bool guc_dirty;			/* TRUE if need to do commit/abort work */
943

944 945
static bool reporting_enabled;	/* TRUE to enable GUC_REPORT */

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

948

B
Bruce Momjian 已提交
949
static int	guc_var_compare(const void *a, const void *b);
950
static void ReportGUCOption(struct config_generic *record);
B
Bruce Momjian 已提交
951
static char *_ShowOption(struct config_generic * record);
952 953 954


/*
B
Bruce Momjian 已提交
955
 * Build the sorted array.	This is split out so that it could be
956 957
 * re-executed after startup (eg, we could allow loadable modules to
 * add vars, and then we'd need to re-sort).
958
 */
959 960
static void
build_guc_variables(void)
961
{
962 963
	int			num_vars = 0;
	struct config_generic **guc_vars;
B
Bruce Momjian 已提交
964
	int			i;
965

966
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
967 968 969
	{
		struct config_bool *conf = &ConfigureNamesBool[i];

970 971 972
		/* Rather than requiring vartype to be filled in by hand, do this: */
		conf->gen.vartype = PGC_BOOL;
		num_vars++;
973
	}
974

975
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
976 977 978
	{
		struct config_int *conf = &ConfigureNamesInt[i];

979 980
		conf->gen.vartype = PGC_INT;
		num_vars++;
981
	}
982

983
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
984 985 986
	{
		struct config_real *conf = &ConfigureNamesReal[i];

987 988
		conf->gen.vartype = PGC_REAL;
		num_vars++;
989
	}
990

991
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
992
	{
993
		struct config_string *conf = &ConfigureNamesString[i];
994

995 996
		conf->gen.vartype = PGC_STRING;
		num_vars++;
997 998
	}

999 1000 1001 1002
	guc_vars = (struct config_generic **)
		malloc(num_vars * sizeof(struct config_generic *));
	if (!guc_vars)
		elog(PANIC, "out of memory");
1003

1004
	num_vars = 0;
1005

1006
	for (i = 0; ConfigureNamesBool[i].gen.name; i++)
B
Bruce Momjian 已提交
1007
		guc_vars[num_vars++] = &ConfigureNamesBool[i].gen;
1008

1009
	for (i = 0; ConfigureNamesInt[i].gen.name; i++)
B
Bruce Momjian 已提交
1010
		guc_vars[num_vars++] = &ConfigureNamesInt[i].gen;
1011

1012
	for (i = 0; ConfigureNamesReal[i].gen.name; i++)
B
Bruce Momjian 已提交
1013
		guc_vars[num_vars++] = &ConfigureNamesReal[i].gen;
1014

1015
	for (i = 0; ConfigureNamesString[i].gen.name; i++)
B
Bruce Momjian 已提交
1016
		guc_vars[num_vars++] = &ConfigureNamesString[i].gen;
1017

1018 1019
	qsort((void *) guc_vars, num_vars, sizeof(struct config_generic *),
		  guc_var_compare);
1020

1021 1022 1023 1024
	if (guc_variables)
		free(guc_variables);
	guc_variables = guc_vars;
	num_guc_variables = num_vars;
1025 1026 1027 1028
}


/*
1029 1030
 * Look up option NAME. If it exists, return a pointer to its record,
 * else return NULL.
1031
 */
1032 1033
static struct config_generic *
find_option(const char *name)
1034
{
1035 1036
	const char **key = &name;
	struct config_generic **res;
1037

1038
	Assert(name);
1039

1040 1041 1042 1043
	/*
	 * by equating const char ** with struct config_generic *, we are
	 * assuming the name field is first in config_generic.
	 */
B
Bruce Momjian 已提交
1044 1045 1046 1047 1048
	res = (struct config_generic **) bsearch((void *) &key,
											 (void *) guc_variables,
											 num_guc_variables,
										 sizeof(struct config_generic *),
											 guc_var_compare);
1049 1050 1051 1052
	if (res)
		return *res;
	return NULL;
}
1053 1054 1055


/*
1056
 * comparator for qsorting and bsearching guc_variables array
1057
 */
1058 1059
static int
guc_var_compare(const void *a, const void *b)
1060
{
1061 1062
	struct config_generic *confa = *(struct config_generic **) a;
	struct config_generic *confb = *(struct config_generic **) b;
1063 1064
	const char *namea;
	const char *nameb;
1065

1066 1067 1068
	/*
	 * The temptation to use strcasecmp() here must be resisted, because
	 * the array ordering has to remain stable across setlocale() calls.
1069
	 * So, build our own with a simple ASCII-only downcasing.
1070
	 */
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	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;
1084
	}
1085 1086 1087 1088 1089
	if (*namea)
		return 1;				/* a is longer */
	if (*nameb)
		return -1;				/* b is longer */
	return 0;
1090
}
1091 1092 1093


/*
1094
 * Initialize GUC options during program startup.
1095
 */
1096 1097
void
InitializeGUCOptions(void)
1098
{
1099 1100
	int			i;
	char	   *env;
1101

1102 1103 1104 1105
	/*
	 * Build sorted array of all GUC variables.
	 */
	build_guc_variables();
1106

1107
	/*
1108 1109 1110 1111 1112 1113
	 * 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...
1114
	 */
1115
	for (i = 0; i < num_guc_variables; i++)
1116
	{
1117
		struct config_generic *gconf = guc_variables[i];
1118

1119 1120 1121 1122 1123 1124 1125 1126 1127
		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 已提交
1128 1129
				{
					struct config_bool *conf = (struct config_bool *) gconf;
1130

B
Bruce Momjian 已提交
1131 1132 1133 1134 1135 1136 1137 1138
					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;
				}
1139
			case PGC_INT:
B
Bruce Momjian 已提交
1140 1141
				{
					struct config_int *conf = (struct config_int *) gconf;
1142

B
Bruce Momjian 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
					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;
				}
1153 1154
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
					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;
1165 1166
					break;
				}
B
Bruce Momjian 已提交
1167
			case PGC_STRING:
1168
				{
B
Bruce Momjian 已提交
1169 1170 1171 1172 1173 1174 1175
					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;
1176

B
Bruce Momjian 已提交
1177
					if (conf->boot_val == NULL)
1178
					{
B
Bruce Momjian 已提交
1179 1180
						/* Cannot set value yet */
						break;
1181
					}
B
Bruce Momjian 已提交
1182 1183 1184 1185 1186 1187 1188

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

					if (conf->assign_hook)
1189
					{
B
Bruce Momjian 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
						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;
						}
1209
					}
B
Bruce Momjian 已提交
1210 1211 1212
					*conf->variable = str;
					conf->session_val = str;
					break;
1213 1214 1215 1216 1217 1218
				}
		}
	}

	guc_dirty = false;

1219 1220
	reporting_enabled = false;

1221 1222 1223
	guc_string_workspace = NULL;

	/*
1224
	 * Prevent any attempt to override the transaction modes from
1225 1226
	 * non-interactive sources.
	 */
1227 1228 1229
	SetConfigOption("transaction_isolation", "default",
					PGC_POSTMASTER, PGC_S_OVERRIDE);
	SetConfigOption("transaction_read_only", "no",
1230 1231 1232 1233
					PGC_POSTMASTER, PGC_S_OVERRIDE);

	/*
	 * For historical reasons, some GUC parameters can receive defaults
B
Bruce Momjian 已提交
1234
	 * from environment variables.	Process those settings.
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	 */

	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 已提交
1280 1281
				{
					struct config_bool *conf = (struct config_bool *) gconf;
1282

B
Bruce Momjian 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
					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;
				}
1294
			case PGC_INT:
B
Bruce Momjian 已提交
1295 1296
				{
					struct config_int *conf = (struct config_int *) gconf;
1297

B
Bruce Momjian 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
					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;
				}
1309 1310
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
					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;
1322 1323
					break;
				}
B
Bruce Momjian 已提交
1324 1325 1326 1327
			case PGC_STRING:
				{
					struct config_string *conf = (struct config_string *) gconf;
					char	   *str;
1328

B
Bruce Momjian 已提交
1329 1330 1331 1332 1333
					if (conf->reset_val == NULL)
					{
						/* Nothing to reset to, as yet; so do nothing */
						break;
					}
1334

B
Bruce Momjian 已提交
1335 1336
					/* We need not strdup here */
					str = conf->reset_val;
1337

B
Bruce Momjian 已提交
1338
					if (conf->assign_hook)
1339
					{
B
Bruce Momjian 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
						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;
						}
1353 1354
					}

B
Bruce Momjian 已提交
1355 1356 1357 1358 1359 1360 1361 1362
					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;
				}
1363
		}
1364 1365 1366

		if (gconf->flags & GUC_REPORT)
			ReportGUCOption(gconf);
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
	}
}


/*
 * 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];
1393
		bool	changed;
1394 1395 1396 1397 1398

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

1399 1400
		changed = false;

1401 1402 1403 1404
		switch (gconf->vartype)
		{
			case PGC_BOOL:
				{
B
Bruce Momjian 已提交
1405
					struct config_bool *conf = (struct config_bool *) gconf;
1406

B
Bruce Momjian 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
					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;
1420
						changed = true;
B
Bruce Momjian 已提交
1421 1422 1423 1424
					}
					conf->gen.source = conf->gen.session_source;
					conf->gen.status = 0;
					break;
1425 1426 1427
				}
			case PGC_INT:
				{
B
Bruce Momjian 已提交
1428
					struct config_int *conf = (struct config_int *) gconf;
1429

B
Bruce Momjian 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
					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;
1443
						changed = true;
B
Bruce Momjian 已提交
1444 1445 1446 1447
					}
					conf->gen.source = conf->gen.session_source;
					conf->gen.status = 0;
					break;
1448 1449 1450
				}
			case PGC_REAL:
				{
B
Bruce Momjian 已提交
1451
					struct config_real *conf = (struct config_real *) gconf;
1452

B
Bruce Momjian 已提交
1453 1454 1455 1456 1457
					if (isCommit && (conf->gen.status & GUC_HAVE_TENTATIVE))
					{
						conf->session_val = conf->tentative_val;
						conf->gen.session_source = conf->gen.tentative_source;
					}
1458

B
Bruce Momjian 已提交
1459 1460 1461 1462 1463 1464 1465
					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;
1466
						changed = true;
B
Bruce Momjian 已提交
1467 1468 1469 1470
					}
					conf->gen.source = conf->gen.session_source;
					conf->gen.status = 0;
					break;
1471
				}
B
Bruce Momjian 已提交
1472
			case PGC_STRING:
1473
				{
B
Bruce Momjian 已提交
1474
					struct config_string *conf = (struct config_string *) gconf;
1475

B
Bruce Momjian 已提交
1476 1477 1478 1479 1480 1481 1482 1483
					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);
1484

B
Bruce Momjian 已提交
1485
					if (*conf->variable != conf->session_val)
1486
					{
B
Bruce Momjian 已提交
1487
						char	   *str = conf->session_val;
1488

B
Bruce Momjian 已提交
1489
						if (conf->assign_hook)
1490
						{
B
Bruce Momjian 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
							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);
							}
1505 1506
						}

B
Bruce Momjian 已提交
1507
						SET_STRING_VARIABLE(conf, str);
1508
						changed = true;
B
Bruce Momjian 已提交
1509 1510 1511 1512
					}
					conf->gen.source = conf->gen.session_source;
					conf->gen.status = 0;
					break;
1513 1514
				}
		}
1515 1516 1517

		if (changed && (gconf->flags & GUC_REPORT))
			ReportGUCOption(gconf);
1518 1519 1520 1521 1522 1523
	}

	guc_dirty = false;
}


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
/*
 * Start up automatic reporting of changes to variables marked GUC_REPORT.
 * This is executed at completion of backend startup.
 */
void
BeginReportingGUCOptions(void)
{
	int			i;

	/*
	 * Don't do anything unless talking to an interactive frontend of
	 * protocol 3.0 or later.
	 */
	if (whereToSendOutput != Remote ||
		PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
		return;

	reporting_enabled = true;

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

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

/*
 * ReportGUCOption: if appropriate, transmit option value to frontend
 */
static void
ReportGUCOption(struct config_generic *record)
{
	if (reporting_enabled && (record->flags & GUC_REPORT))
	{
		char	*val = _ShowOption(record);
		StringInfoData msgbuf;

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

		pfree(val);
	}
}


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 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 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 1735 1736 1737 1738 1739
/*
 * 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)
	{
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
		case PGC_INTERNAL:
			if (context == PGC_SIGHUP)
				return true;
			if (context != PGC_INTERNAL)
			{
				elog(elevel, "'%s' cannot be changed",
					 name);
				return false;
			}
			break;
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 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
		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;
1800 1801 1802 1803 1804
			}
			break;
		case PGC_USERSET:
			/* always okay */
			break;
1805
	}
1806

1807
	/* Should we report errors interactively? */
1808
	interactive = (source >= PGC_S_SESSION);
B
Bruce Momjian 已提交
1809

1810
	/*
B
Bruce Momjian 已提交
1811
	 * Should we set reset/session values?	(If so, the behavior is not
1812 1813 1814
	 * transactional.)
	 */
	makeDefault = DoIt && (source <= PGC_S_OVERRIDE) && (value != NULL);
1815 1816 1817

	/*
	 * Ignore attempted set if overridden by previously processed setting.
B
Bruce Momjian 已提交
1818 1819 1820 1821 1822
	 * 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.
1823 1824 1825 1826 1827
	 */
	if (record->source > source)
	{
		if (DoIt && !makeDefault)
		{
1828
			elog(DEBUG2, "%s: setting ignored because previous source is higher priority",
1829 1830 1831 1832 1833 1834
				 name);
			return true;
		}
		DoIt = false;			/* we won't change the variable itself */
	}

1835 1836 1837
	/*
	 * Evaluate value and set variable
	 */
1838
	switch (record->vartype)
1839 1840 1841
	{
		case PGC_BOOL:
			{
B
Bruce Momjian 已提交
1842
				struct config_bool *conf = (struct config_bool *) record;
1843
				bool		newval;
B
Bruce Momjian 已提交
1844 1845

				if (value)
1846
				{
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
					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 已提交
1859

1860 1861
				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
B
Bruce Momjian 已提交
1862
					{
1863 1864
						elog(elevel, "invalid value for option '%s': %d",
							 name, (int) newval);
B
Bruce Momjian 已提交
1865 1866
						return false;
					}
1867 1868 1869

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1870
					if (DoIt)
1871
					{
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
						*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;
1899
					}
1900
				}
B
Bruce Momjian 已提交
1901
				break;
1902
			}
1903 1904

		case PGC_INT:
1905
			{
B
Bruce Momjian 已提交
1906
				struct config_int *conf = (struct config_int *) record;
1907
				int			newval;
1908

B
Bruce Momjian 已提交
1909
				if (value)
1910
				{
1911
					if (!parse_int(value, &newval))
B
Bruce Momjian 已提交
1912
					{
1913 1914
						elog(elevel, "option '%s' expects an integer value",
							 name);
B
Bruce Momjian 已提交
1915 1916
						return false;
					}
1917
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1918 1919 1920
					{
						elog(elevel, "option '%s' value %d is outside"
							 " of permissible range [%d .. %d]",
1921
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1922 1923
						return false;
					}
1924 1925 1926 1927 1928 1929 1930 1931 1932
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
1933 1934
					{
						elog(elevel, "invalid value for option '%s': %d",
1935
							 name, newval);
1936 1937
						return false;
					}
1938 1939 1940

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
1941
					if (DoIt)
1942
					{
1943 1944 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
						*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;
1970
					}
1971
				}
B
Bruce Momjian 已提交
1972
				break;
1973
			}
1974 1975

		case PGC_REAL:
1976
			{
B
Bruce Momjian 已提交
1977
				struct config_real *conf = (struct config_real *) record;
1978
				double		newval;
1979

B
Bruce Momjian 已提交
1980
				if (value)
1981
				{
1982
					if (!parse_real(value, &newval))
B
Bruce Momjian 已提交
1983
					{
1984 1985
						elog(elevel, "option '%s' expects a real number",
							 name);
B
Bruce Momjian 已提交
1986 1987
						return false;
					}
1988
					if (newval < conf->min || newval > conf->max)
B
Bruce Momjian 已提交
1989 1990 1991
					{
						elog(elevel, "option '%s' value %g is outside"
							 " of permissible range [%g .. %g]",
1992
							 name, newval, conf->min, conf->max);
B
Bruce Momjian 已提交
1993 1994
						return false;
					}
1995 1996 1997 1998 1999 2000 2001 2002 2003
				}
				else
				{
					newval = conf->reset_val;
					source = conf->gen.reset_source;
				}

				if (conf->assign_hook)
					if (!(*conf->assign_hook) (newval, DoIt, interactive))
2004 2005
					{
						elog(elevel, "invalid value for option '%s': %g",
2006
							 name, newval);
2007 2008
						return false;
					}
2009 2010 2011

				if (DoIt || makeDefault)
				{
B
Bruce Momjian 已提交
2012
					if (DoIt)
2013
					{
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
						*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;
2041
					}
2042
				}
B
Bruce Momjian 已提交
2043
				break;
2044
			}
2045 2046 2047

		case PGC_STRING:
			{
B
Bruce Momjian 已提交
2048
				struct config_string *conf = (struct config_string *) record;
2049
				char	   *newval;
B
Bruce Momjian 已提交
2050 2051

				if (value)
2052
				{
2053 2054 2055 2056 2057 2058 2059 2060 2061
					newval = strdup(value);
					if (newval == NULL)
					{
						elog(elevel, "out of memory");
						return false;
					}
				}
				else if (conf->reset_val)
				{
2062 2063
					/*
					 * We could possibly avoid strdup here, but easier to
B
Bruce Momjian 已提交
2064 2065
					 * make this case work the same as the normal
					 * assignment case.
2066
					 */
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
					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 已提交
2091
					const char *hookresult;
2092 2093 2094 2095 2096

					hookresult = (*conf->assign_hook) (newval,
													   DoIt, interactive);
					guc_string_workspace = NULL;
					if (hookresult == NULL)
B
Bruce Momjian 已提交
2097
					{
2098
						free(newval);
2099
						elog(elevel, "invalid value for option '%s': '%s'",
2100
							 name, value ? value : "");
B
Bruce Momjian 已提交
2101 2102
						return false;
					}
2103
					else if (hookresult != newval)
B
Bruce Momjian 已提交
2104
					{
2105
						free(newval);
B
Bruce Momjian 已提交
2106

2107
						/*
B
Bruce Momjian 已提交
2108 2109 2110 2111 2112 2113 2114
						 * 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.
2115 2116 2117 2118 2119 2120
						 */
						newval = (char *) hookresult;
					}
				}

				guc_string_workspace = NULL;
B
Bruce Momjian 已提交
2121

2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
				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 已提交
2132
						{
2133 2134
							SET_STRING_RESET_VAL(conf, newval);
							conf->gen.reset_source = source;
B
Bruce Momjian 已提交
2135
						}
2136
						if (conf->gen.session_source <= source)
2137
						{
2138 2139
							SET_STRING_SESSION_VAL(conf, newval);
							conf->gen.session_source = source;
2140
						}
2141 2142 2143 2144 2145
						/* Perhaps we didn't install newval anywhere */
						if (newval != *conf->variable &&
							newval != conf->session_val &&
							newval != conf->reset_val)
							free(newval);
B
Bruce Momjian 已提交
2146
					}
2147
					else if (isLocal)
2148
					{
2149 2150
						conf->gen.status |= GUC_HAVE_LOCAL;
						guc_dirty = true;
2151
					}
2152
					else
2153
					{
2154 2155 2156 2157
						SET_STRING_TENTATIVE_VAL(conf, newval);
						conf->gen.tentative_source = source;
						conf->gen.status |= GUC_HAVE_TENTATIVE;
						guc_dirty = true;
2158
					}
2159 2160 2161
				}
				else
					free(newval);
B
Bruce Momjian 已提交
2162
				break;
2163
			}
2164
	}
2165

2166 2167 2168
	if (DoIt && (record->flags & GUC_REPORT))
			ReportGUCOption(record);

2169 2170 2171 2172 2173 2174 2175
	return true;
}



/*
 * Set a config option to the given value. See also set_config_option,
B
Bruce Momjian 已提交
2176
 * this is just the wrapper to be called from outside GUC.	NB: this
2177
 * is used only for non-transactional operations.
2178 2179
 */
void
2180
SetConfigOption(const char *name, const char *value,
2181
				GucContext context, GucSource source)
2182
{
2183
	(void) set_config_option(name, value, context, source, false, true);
2184 2185 2186 2187 2188
}



/*
2189 2190
 * Fetch the current value of the option `name'. If the option doesn't exist,
 * throw an elog and don't return.
2191 2192 2193 2194 2195
 *
 * 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 已提交
2196
GetConfigOption(const char *name)
2197
{
B
Bruce Momjian 已提交
2198
	struct config_generic *record;
2199 2200
	static char buffer[256];

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

2205
	switch (record->vartype)
2206 2207
	{
		case PGC_BOOL:
B
Bruce Momjian 已提交
2208
			return *((struct config_bool *) record)->variable ? "on" : "off";
2209

2210
		case PGC_INT:
2211 2212
			snprintf(buffer, sizeof(buffer), "%d",
					 *((struct config_int *) record)->variable);
2213 2214
			return buffer;

2215
		case PGC_REAL:
2216 2217
			snprintf(buffer, sizeof(buffer), "%g",
					 *((struct config_real *) record)->variable);
2218 2219 2220
			return buffer;

		case PGC_STRING:
B
Bruce Momjian 已提交
2221
			return *((struct config_string *) record)->variable;
2222 2223 2224
	}
	return NULL;
}
2225

2226 2227 2228 2229 2230
/*
 * Get the RESET value associated with the given option.
 */
const char *
GetConfigOptionResetString(const char *name)
2231
{
2232 2233
	struct config_generic *record;
	static char buffer[256];
2234

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

	switch (record->vartype)
2240 2241
	{
		case PGC_BOOL:
2242
			return ((struct config_bool *) record)->reset_val ? "on" : "off";
2243

2244
		case PGC_INT:
2245
			snprintf(buffer, sizeof(buffer), "%d",
2246 2247
					 ((struct config_int *) record)->reset_val);
			return buffer;
2248 2249

		case PGC_REAL:
2250
			snprintf(buffer, sizeof(buffer), "%g",
2251 2252
					 ((struct config_real *) record)->reset_val);
			return buffer;
2253 2254

		case PGC_STRING:
2255 2256 2257 2258
			return ((struct config_string *) record)->reset_val;
	}
	return NULL;
}
2259

2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278


/*
 * 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 已提交
2279
	List	   *l;
2280

2281 2282 2283 2284
	/*
	 * Fast path if just DEFAULT.  We do not check the variable name in
	 * this case --- necessary for RESET ALL to work correctly.
	 */
2285 2286 2287
	if (args == NIL)
		return NULL;

2288
	/* Else get flags for the variable */
2289 2290
	record = find_option(name);
	if (record == NULL)
2291 2292 2293
		elog(ERROR, "'%s' is not a valid option name", name);

	flags = record->flags;
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330

	/* 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 已提交
2331 2332
					Datum		interval;
					char	   *intervalout;
2333 2334 2335 2336 2337

					interval =
						DirectFunctionCall3(interval_in,
											CStringGetDatum(val),
											ObjectIdGetDatum(InvalidOid),
B
Bruce Momjian 已提交
2338
								   Int32GetDatum(arg->typename->typmod));
2339 2340 2341 2342

					intervalout =
						DatumGetCString(DirectFunctionCall3(interval_out,
															interval,
B
Bruce Momjian 已提交
2343 2344
											ObjectIdGetDatum(InvalidOid),
													 Int32GetDatum(-1)));
2345 2346 2347 2348 2349
					appendStringInfo(&buf, "INTERVAL '%s'", intervalout);
				}
				else
				{
					/*
B
Bruce Momjian 已提交
2350 2351
					 * Plain string literal or identifier.	For quote
					 * mode, quote it if it's not a vanilla identifier.
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
					 */
					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;
		}
2363
	}
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385

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

2386 2387 2388 2389 2390 2391
/*
 * SET command wrapped as a SQL callable function.
 */
Datum
set_config_by_name(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
2392 2393 2394 2395 2396
	char	   *name;
	char	   *value;
	char	   *new_value;
	bool		is_local;
	text	   *result_text;
2397 2398 2399 2400 2401

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

	/* Get the GUC variable name */
2402
	name = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2403 2404 2405 2406 2407

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

	/*
B
Bruce Momjian 已提交
2411 2412
	 * Get the desired state of is_local. Default to false if provided
	 * value is NULL
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
	 */
	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 */
2428
	new_value = GetConfigOptionByName(name, NULL);
2429 2430 2431 2432 2433 2434 2435 2436

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

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

2437 2438 2439 2440
/*
 * SHOW command
 */
void
2441
GetPGVariable(const char *name, DestReceiver *dest)
2442 2443
{
	if (strcasecmp(name, "all") == 0)
2444
		ShowAllGUCConfig(dest);
2445
	else
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
		ShowGUCConfigOption(name, dest);
}

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

	if (strcasecmp(name, "all") == 0)
	{
		/* need a tuple descriptor representing two TEXT columns */
		tupdesc = CreateTemplateTupleDesc(2, false);
		TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
						   TEXTOID, -1, 0, false);
		TupleDescInitEntry(tupdesc, (AttrNumber) 2, "setting",
						   TEXTOID, -1, 0, false);
	}
	else
	{
		const char *varname;

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

		/* need a tuple descriptor representing a single TEXT column */
		tupdesc = CreateTemplateTupleDesc(1, false);
		TupleDescInitEntry(tupdesc, (AttrNumber) 1, (char *) varname,
						   TEXTOID, -1, 0, false);
	}
	return tupdesc;
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
}

/*
 * 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
2500
ShowGUCConfigOption(const char *name, DestReceiver *dest)
2501
{
2502
	TupOutputState *tstate;
B
Bruce Momjian 已提交
2503 2504 2505
	TupleDesc	tupdesc;
	const char *varname;
	char	   *value;
2506

2507 2508 2509
	/* Get the value and canonical spelling of name */
	value = GetConfigOptionByName(name, &varname);

2510
	/* need a tuple descriptor representing a single TEXT column */
2511
	tupdesc = CreateTemplateTupleDesc(1, false);
2512
	TupleDescInitEntry(tupdesc, (AttrNumber) 1, (char *) varname,
2513
					   TEXTOID, -1, 0, false);
2514

2515 2516 2517 2518
	/* prepare for projection of tuples */
	tstate = begin_tup_output_tupdesc(dest, tupdesc);

	/* Send it */
2519
	do_text_output_oneline(tstate, value);
2520 2521

	end_tup_output(tstate);
2522 2523
}

2524 2525 2526
/*
 * SHOW ALL command
 */
2527
void
2528
ShowAllGUCConfig(DestReceiver *dest)
2529 2530
{
	int			i;
2531
	TupOutputState *tstate;
B
Bruce Momjian 已提交
2532 2533
	TupleDesc	tupdesc;
	char	   *values[2];
2534 2535

	/* need a tuple descriptor representing two TEXT columns */
2536
	tupdesc = CreateTemplateTupleDesc(2, false);
2537 2538 2539 2540 2541 2542 2543
	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);
2544

2545 2546
	for (i = 0; i < num_guc_variables; i++)
	{
2547 2548 2549 2550
		struct config_generic *conf = guc_variables[i];

		if (conf->flags & GUC_NO_SHOW_ALL)
			continue;
2551 2552

		/* assign to the values array */
2553 2554
		values[0] = (char *) conf->name;
		values[1] = _ShowOption(conf);
2555 2556 2557

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

2559 2560 2561
		/* clean up */
		if (values[1] != NULL)
			pfree(values[1]);
2562
	}
2563 2564

	end_tup_output(tstate);
2565
}
2566

2567
/*
2568 2569
 * Return GUC variable value by name; optionally return canonical
 * form of name.  Return value is palloc'd.
2570 2571
 */
char *
2572
GetConfigOptionByName(const char *name, const char **varname)
2573 2574 2575 2576 2577 2578 2579
{
	struct config_generic *record;

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

2580 2581 2582
	if (varname)
		*varname = record->name;

2583 2584 2585 2586
	return _ShowOption(record);
}

/*
2587 2588
 * Return GUC variable value by variable number; optionally return canonical
 * form of name.  Return value is palloc'd.
2589 2590
 */
char *
2591
GetConfigOptionByNum(int varnum, const char **varname, bool *noshow)
2592
{
2593 2594 2595 2596 2597 2598
	struct config_generic *conf;

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

	conf = guc_variables[varnum];
2599

2600 2601
	if (varname)
		*varname = conf->name;
2602

2603 2604 2605
	if (noshow)
		*noshow = (conf->flags & GUC_NO_SHOW_ALL) ? true : false;

2606
	return _ShowOption(conf);
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
}

/*
 * 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 已提交
2625 2626 2627
	char	   *varname;
	char	   *varval;
	text	   *result_text;
2628 2629

	/* Get the GUC variable name */
2630
	varname = DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(0)));
2631 2632

	/* Get the value */
2633
	varval = GetConfigOptionByName(varname, NULL);
2634 2635 2636 2637 2638 2639 2640 2641

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

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

2642 2643 2644 2645 2646 2647 2648
/*
 * show_all_settings - equiv to SHOW ALL command but implemented as
 * a Table Function.
 */
Datum
show_all_settings(PG_FUNCTION_ARGS)
{
B
Bruce Momjian 已提交
2649 2650 2651 2652 2653 2654 2655
	FuncCallContext *funcctx;
	TupleDesc	tupdesc;
	int			call_cntr;
	int			max_calls;
	TupleTableSlot *slot;
	AttInMetadata *attinmeta;
	MemoryContext oldcontext;
2656 2657

	/* stuff done only on the first call of the function */
B
Bruce Momjian 已提交
2658 2659
	if (SRF_IS_FIRSTCALL())
	{
2660
		/* create a function context for cross-call persistence */
B
Bruce Momjian 已提交
2661
		funcctx = SRF_FIRSTCALL_INIT();
2662

B
Bruce Momjian 已提交
2663 2664 2665 2666
		/*
		 * switch to memory context appropriate for multiple function
		 * calls
		 */
2667 2668
		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

2669
		/* need a tuple descriptor representing two TEXT columns */
2670
		tupdesc = CreateTemplateTupleDesc(2, false);
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
		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 已提交
2683 2684
		 * Generate attribute metadata needed later to produce tuples from
		 * raw C strings
2685 2686 2687 2688 2689 2690
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

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

		MemoryContextSwitchTo(oldcontext);
B
Bruce Momjian 已提交
2693
	}
2694 2695

	/* stuff done on every call of the function */
B
Bruce Momjian 已提交
2696
	funcctx = SRF_PERCALL_SETUP();
2697 2698 2699 2700 2701 2702

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

B
Bruce Momjian 已提交
2703 2704
	if (call_cntr < max_calls)	/* do when there is more left to send */
	{
2705
		char	   *values[2];
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
		char	   *varname;
		char	   *varval;
		bool		noshow;
		HeapTuple	tuple;
		Datum		result;

		/*
		 * Get the next visible GUC variable name and value
		 */
		do
		{
2717 2718 2719
			varval = GetConfigOptionByNum(call_cntr,
										  (const char **) &varname,
										  &noshow);
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
			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 已提交
2731
					SRF_RETURN_DONE(funcctx);
2732 2733 2734 2735
			}
		} while (noshow);

		/*
B
Bruce Momjian 已提交
2736 2737 2738
		 * 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.
2739
		 */
2740 2741
		values[0] = varname;
		values[1] = varval;
2742 2743 2744 2745 2746 2747 2748 2749 2750

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

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

		/* Clean up */
		if (varval != NULL)
2751
			pfree(varval);
2752

B
Bruce Momjian 已提交
2753 2754 2755
		SRF_RETURN_NEXT(funcctx, result);
	}
	else
2756 2757
	{
		/* do when there is no more left */
B
Bruce Momjian 已提交
2758
		SRF_RETURN_DONE(funcctx);
2759
	}
2760 2761
}

2762
static char *
B
Bruce Momjian 已提交
2763
_ShowOption(struct config_generic * record)
2764 2765 2766
{
	char		buffer[256];
	const char *val;
2767

2768 2769 2770 2771 2772
	switch (record->vartype)
	{
		case PGC_BOOL:
			{
				struct config_bool *conf = (struct config_bool *) record;
2773

2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
				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;
2814

2815 2816 2817 2818 2819 2820 2821 2822
				if (conf->show_hook)
					val = (*conf->show_hook) ();
				else if (*conf->variable && **conf->variable)
					val = *conf->variable;
				else
					val = "unset";
			}
			break;
2823

2824 2825 2826 2827 2828
		default:
			/* just to keep compiler quiet */
			val = "???";
			break;
	}
2829

2830
	return pstrdup(val);
2831
}
2832 2833


B
Bruce Momjian 已提交
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 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 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
#ifdef EXEC_BACKEND
/*
 *	This routine dumps out all non-default GUC options into a binary
 *	file that is read by all exec'ed backends.  The format is:
 *
 *		variable name, string, null terminated
 *		variable value, string, null terminated
 *		variable source, integer
 */
void
write_nondefault_variables(GucContext context)
{
	int i;
	char *new_filename, *filename;
	int elevel;
	FILE *fp;

	Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
	Assert(DataDir);
	elevel = (context == PGC_SIGHUP) ? DEBUG3 : ERROR;

	/*
	 * Open file
	 */
	new_filename = malloc(strlen(DataDir) + strlen(CONFIG_EXEC_PARAMS) +
							strlen(".new") + 2);
	filename = malloc(strlen(DataDir) + strlen(CONFIG_EXEC_PARAMS) + 2);
	if (new_filename == NULL || filename == NULL)
	{
		elog(elevel, "out of memory");
		return;
	}
	sprintf(new_filename, "%s/" CONFIG_EXEC_PARAMS ".new", DataDir);
	sprintf(filename, "%s/" CONFIG_EXEC_PARAMS, DataDir);

    fp = AllocateFile(new_filename, "w");
    if (!fp)
    {
 		free(new_filename);
		free(filename);
		elog(elevel, "could not write exec config params file `"
					CONFIG_EXEC_PARAMS "': %s", strerror(errno));
		return;
	}

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

		if (gconf->source != PGC_S_DEFAULT)
		{
			fprintf(fp, "%s", gconf->name);
			fputc(0, fp);

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

						if (*conf->variable == 0)
							fprintf(fp, "false");
						else
							fprintf(fp, "true");
					}
					break;

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

						fprintf(fp, "%d", *conf->variable);
					}
					break;

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

						/* Could lose precision here? */
						fprintf(fp, "%f", *conf->variable);
					}
					break;

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

						fprintf(fp, "%s", *conf->variable);
					}
					break;
			}

			fputc(0, fp);

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

	FreeFile(fp);
	/* Put new file in place, this could delay on Win32 */
	rename(new_filename, filename);
	free(new_filename);
	free(filename);
	return;
}


/*
 *	Read string, including null byte from file
 *
 *	Return NULL on EOF and nothing read
 */
static char *
read_string_with_null(FILE *fp)
{
	int i = 0, ch, maxlen = 256;
	char *str = NULL;

	do
	{
		if ((ch = fgetc(fp)) == EOF)
		{
			if (i == 0)
				return NULL;
			else
				elog(FATAL, "Invalid format of exec config params file");
		}
		if (i == 0)
			str = malloc(maxlen);
		else if (i == maxlen)
			str = realloc(str, maxlen *= 2);
		str[i++] = ch;
	} while (ch != 0);

	return str;
}


/*
 *	This routine loads a previous postmaster dump of its non-default
 *	settings.
 */
void
read_nondefault_variables(void)
{
	char *filename;
	FILE *fp;
	char *varname, *varvalue;
	int varsource;

	Assert(DataDir);

	/*
	 * Open file
	 */
	filename = malloc(strlen(DataDir) + strlen(CONFIG_EXEC_PARAMS) + 2);
	if (filename == NULL)
	{
		elog(ERROR, "out of memory");
		return;
	}
	sprintf(filename, "%s/" CONFIG_EXEC_PARAMS, DataDir);

    fp = AllocateFile(filename, "r");
    if (!fp)
    {
		free(filename);
        /* File not found is fine */
        if (errno != ENOENT)
            elog(FATAL, "could not read exec config params file `"
					CONFIG_EXEC_PARAMS "': %s", strerror(errno));
		return;
    }

    while (1)
	{
		if ((varname = read_string_with_null(fp)) == NULL)
			break;

		if ((varvalue = read_string_with_null(fp)) == NULL)
			elog(FATAL, "Invalid format of exec config params file");
 		if (fread(&varsource, sizeof(varsource), 1, fp) == 0)
			elog(FATAL, "Invalid format of exec config params file");

		(void) set_config_option(varname, varvalue, PGC_POSTMASTER,
				varsource, false, true);
		free(varname);
		free(varvalue);
	}

	FreeFile(fp);
	free(filename);
	return;
}
#endif


3032 3033 3034 3035 3036 3037 3038 3039
/*
 * 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 已提交
3040
ParseLongOption(const char *string, char **name, char **value)
3041
{
B
Bruce Momjian 已提交
3042 3043
	size_t		equal_pos;
	char	   *cp;
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062

	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 已提交
3063
	else
3064
	{
3065
		/* no equal sign in string */
3066 3067 3068 3069 3070 3071
		*name = strdup(string);
		if (!*name)
			elog(FATAL, "out of memory");
		*value = NULL;
	}

B
Bruce Momjian 已提交
3072
	for (cp = *name; *cp; cp++)
3073 3074 3075
		if (*cp == '-')
			*cp = '_';
}
3076 3077


3078 3079
/*
 * Handle options fetched from pg_database.datconfig or pg_shadow.useconfig.
3080
 * The array parameter must be an array of TEXT (it must not be NULL).
3081
 */
3082 3083 3084
void
ProcessGUCArray(ArrayType *array, GucSource source)
{
B
Bruce Momjian 已提交
3085
	int			i;
3086

3087
	Assert(array != NULL);
3088 3089 3090
	Assert(ARR_ELEMTYPE(array) == TEXTOID);
	Assert(ARR_NDIM(array) == 1);
	Assert(ARR_LBOUND(array)[0] == 1);
3091
	Assert(source == PGC_S_DATABASE || source == PGC_S_USER);
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101

	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 已提交
3102 3103 3104 3105
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
3106 3107 3108 3109 3110 3111
					  &isnull);

		if (isnull)
			continue;

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

3113 3114 3115
		ParseLongOption(s, &name, &value);
		if (!value)
		{
3116
			elog(WARNING, "cannot parse setting \"%s\"", name);
3117
			free(name);
B
Bruce Momjian 已提交
3118
			continue;
3119 3120
		}

3121
		/*
B
Bruce Momjian 已提交
3122 3123
		 * We process all these options at SUSET level.  We assume that
		 * the right to insert an option into pg_database or pg_shadow was
3124 3125
		 * checked when it was inserted.
		 */
3126
		SetConfigOption(name, value, PGC_SUSET, source);
3127 3128 3129 3130

		free(name);
		if (value)
			free(value);
3131 3132 3133 3134
	}
}


3135 3136 3137 3138
/*
 * Add an entry to an option array.  The array parameter may be NULL
 * to indicate the current table entry is NULL.
 */
3139 3140 3141
ArrayType *
GUCArrayAdd(ArrayType *array, const char *name, const char *value)
{
3142
	const char *varname;
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
	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,
3153
					  PGC_S_SESSION, false, false);
3154

3155 3156 3157 3158
	/* convert name to canonical spelling, so we can use plain strcmp */
	(void) GetConfigOptionByName(name, &varname);
	name = varname;

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

3163 3164
	if (array)
	{
B
Bruce Momjian 已提交
3165 3166 3167
		int			index;
		bool		isnull;
		int			i;
3168

3169 3170 3171 3172
		Assert(ARR_ELEMTYPE(array) == TEXTOID);
		Assert(ARR_NDIM(array) == 1);
		Assert(ARR_LBOUND(array)[0] == 1);

B
Bruce Momjian 已提交
3173
		index = ARR_DIMS(array)[0] + 1; /* add after end */
3174 3175 3176 3177 3178 3179 3180

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

			d = array_ref(array, 1, &i,
B
Bruce Momjian 已提交
3181 3182 3183 3184
						  -1 /* varlenarray */ ,
						  -1 /* TEXT's typlen */ ,
						  false /* TEXT's typbyval */ ,
						  'i' /* TEXT's typalign */ ,
3185
						  &isnull);
3186 3187
			if (isnull)
				continue;
3188
			current = DatumGetCString(DirectFunctionCall1(textout, d));
B
Bruce Momjian 已提交
3189
			if (strncmp(current, newval, strlen(name) + 1) == 0)
3190 3191 3192 3193 3194 3195 3196
			{
				index = i;
				break;
			}
		}

		isnull = false;
3197 3198
		a = array_set(array, 1, &index,
					  datum,
B
Bruce Momjian 已提交
3199 3200 3201 3202
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
3203
					  &isnull);
3204 3205
	}
	else
3206 3207 3208
		a = construct_array(&datum, 1,
							TEXTOID,
							-1, false, 'i');
3209 3210 3211 3212 3213

	return a;
}


3214 3215 3216 3217 3218
/*
 * 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.
 */
3219 3220 3221
ArrayType *
GUCArrayDelete(ArrayType *array, const char *name)
{
3222
	const char *varname;
B
Bruce Momjian 已提交
3223 3224 3225
	ArrayType  *newarray;
	int			i;
	int			index;
3226 3227 3228 3229 3230 3231

	Assert(name);

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

3234 3235 3236 3237
	/* convert name to canonical spelling, so we can use plain strcmp */
	(void) GetConfigOptionByName(name, &varname);
	name = varname;

3238 3239 3240 3241 3242
	/* if array is currently null, then surely nothing to delete */
	if (!array)
		return NULL;

	newarray = NULL;
3243 3244 3245 3246 3247 3248 3249 3250 3251
	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 已提交
3252 3253 3254 3255
					  -1 /* varlenarray */ ,
					  -1 /* TEXT's typlen */ ,
					  false /* TEXT's typbyval */ ,
					  'i' /* TEXT's typalign */ ,
3256
					  &isnull);
3257 3258
		if (isnull)
			continue;
3259 3260
		val = DatumGetCString(DirectFunctionCall1(textout, d));

3261
		/* ignore entry if it's what we want to delete */
B
Bruce Momjian 已提交
3262
		if (strncmp(val, name, strlen(name)) == 0
3263 3264 3265
			&& val[strlen(name)] == '=')
			continue;

3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
		/* 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');

3283 3284 3285 3286 3287
		index++;
	}

	return newarray;
}
3288

3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338

/*
 * assign_hook subroutines
 */

#ifdef HAVE_SYSLOG

static const char *
assign_facility(const char *facility, bool doit, bool interactive)
{
	if (strcasecmp(facility, "LOCAL0") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL1") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL2") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL3") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL4") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL5") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL6") == 0)
		return facility;
	if (strcasecmp(facility, "LOCAL7") == 0)
		return facility;
	return NULL;
}
#endif


static const char *
assign_defaultxactisolevel(const char *newval, bool doit, bool interactive)
{
	if (strcasecmp(newval, "serializable") == 0)
	{
		if (doit)
			DefaultXactIsoLevel = XACT_SERIALIZABLE;
	}
	else if (strcasecmp(newval, "read committed") == 0)
	{
		if (doit)
			DefaultXactIsoLevel = XACT_READ_COMMITTED;
	}
	else
		return NULL;
	return newval;
}

static const char *
3339
assign_log_min_messages(const char *newval,
3340 3341
						   bool doit, bool interactive)
{
3342
	return (assign_msglvl(&log_min_messages, newval, doit, interactive));
3343 3344
}

3345
static const char *
3346 3347 3348
assign_client_min_messages(const char *newval,
						   bool doit, bool interactive)
{
B
Bruce Momjian 已提交
3349
	return (assign_msglvl(&client_min_messages, newval, doit, interactive));
3350 3351
}

3352
static const char *
3353 3354
assign_min_error_statement(const char *newval, bool doit, bool interactive)
{
B
Bruce Momjian 已提交
3355
	return (assign_msglvl(&log_min_error_statement, newval, doit, interactive));
3356 3357 3358 3359 3360 3361
}

static const char *
assign_msglvl(int *var, const char *newval, bool doit, bool interactive)
{
	if (strcasecmp(newval, "debug") == 0)
B
Bruce Momjian 已提交
3362 3363 3364 3365
	{
		if (doit)
			(*var) = DEBUG1;
	}
3366
	else if (strcasecmp(newval, "debug5") == 0)
B
Bruce Momjian 已提交
3367 3368 3369 3370
	{
		if (doit)
			(*var) = DEBUG5;
	}
3371
	else if (strcasecmp(newval, "debug4") == 0)
B
Bruce Momjian 已提交
3372 3373 3374 3375
	{
		if (doit)
			(*var) = DEBUG4;
	}
3376
	else if (strcasecmp(newval, "debug3") == 0)
B
Bruce Momjian 已提交
3377 3378 3379 3380
	{
		if (doit)
			(*var) = DEBUG3;
	}
3381
	else if (strcasecmp(newval, "debug2") == 0)
B
Bruce Momjian 已提交
3382 3383 3384 3385
	{
		if (doit)
			(*var) = DEBUG2;
	}
3386
	else if (strcasecmp(newval, "debug1") == 0)
B
Bruce Momjian 已提交
3387 3388 3389 3390
	{
		if (doit)
			(*var) = DEBUG1;
	}
3391
	else if (strcasecmp(newval, "log") == 0)
B
Bruce Momjian 已提交
3392 3393 3394 3395
	{
		if (doit)
			(*var) = LOG;
	}
3396
	else if (strcasecmp(newval, "info") == 0)
B
Bruce Momjian 已提交
3397 3398 3399 3400
	{
		if (doit)
			(*var) = INFO;
	}
3401
	else if (strcasecmp(newval, "notice") == 0)
B
Bruce Momjian 已提交
3402 3403 3404 3405
	{
		if (doit)
			(*var) = NOTICE;
	}
3406
	else if (strcasecmp(newval, "warning") == 0)
B
Bruce Momjian 已提交
3407 3408 3409 3410
	{
		if (doit)
			(*var) = WARNING;
	}
3411
	else if (strcasecmp(newval, "error") == 0)
B
Bruce Momjian 已提交
3412 3413 3414 3415
	{
		if (doit)
			(*var) = ERROR;
	}
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
	/* 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;
	}
3427 3428 3429 3430
	else
		return NULL;			/* fail */
	return newval;				/* OK */
}
3431

3432

3433
#include "guc-file.c"
B
Bruce Momjian 已提交
3434