1 /*-------------------------------------------------------------------------
2  *
3  * postinit.c
4  *	  postgres initialization utilities
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/utils/init/postinit.c
12  *
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17 
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #include "access/genam.h"
23 #include "access/heapam.h"
24 #include "access/htup_details.h"
25 #include "access/session.h"
26 #include "access/sysattr.h"
27 #include "access/tableam.h"
28 #include "access/xact.h"
29 #include "access/xlog.h"
30 #include "catalog/catalog.h"
31 #include "catalog/indexing.h"
32 #include "catalog/namespace.h"
33 #include "catalog/pg_authid.h"
34 #include "catalog/pg_database.h"
35 #include "catalog/pg_db_role_setting.h"
36 #include "catalog/pg_tablespace.h"
37 #include "libpq/auth.h"
38 #include "libpq/libpq-be.h"
39 #include "mb/pg_wchar.h"
40 #include "miscadmin.h"
41 #include "pgstat.h"
42 #include "postmaster/autovacuum.h"
43 #include "postmaster/postmaster.h"
44 #include "replication/walsender.h"
45 #include "storage/bufmgr.h"
46 #include "storage/fd.h"
47 #include "storage/ipc.h"
48 #include "storage/lmgr.h"
49 #include "storage/procarray.h"
50 #include "storage/procsignal.h"
51 #include "storage/proc.h"
52 #include "storage/sinvaladt.h"
53 #include "storage/smgr.h"
54 #include "storage/sync.h"
55 #include "tcop/tcopprot.h"
56 #include "utils/acl.h"
57 #include "utils/fmgroids.h"
58 #include "utils/guc.h"
59 #include "utils/memutils.h"
60 #include "utils/pg_locale.h"
61 #include "utils/portal.h"
62 #include "utils/ps_status.h"
63 #include "utils/snapmgr.h"
64 #include "utils/syscache.h"
65 #include "utils/timeout.h"
66 
67 
68 static HeapTuple GetDatabaseTuple(const char *dbname);
69 static HeapTuple GetDatabaseTupleByOid(Oid dboid);
70 static void PerformAuthentication(Port *port);
71 static void CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections);
72 static void InitCommunication(void);
73 static void ShutdownPostgres(int code, Datum arg);
74 static void StatementTimeoutHandler(void);
75 static void LockTimeoutHandler(void);
76 static void IdleInTransactionSessionTimeoutHandler(void);
77 static bool ThereIsAtLeastOneRole(void);
78 static void process_startup_options(Port *port, bool am_superuser);
79 static void process_settings(Oid databaseid, Oid roleid);
80 
81 
82 /*** InitPostgres support ***/
83 
84 
85 /*
86  * GetDatabaseTuple -- fetch the pg_database row for a database
87  *
88  * This is used during backend startup when we don't yet have any access to
89  * system catalogs in general.  In the worst case, we can seqscan pg_database
90  * using nothing but the hard-wired descriptor that relcache.c creates for
91  * pg_database.  In more typical cases, relcache.c was able to load
92  * descriptors for both pg_database and its indexes from the shared relcache
93  * cache file, and so we can do an indexscan.  criticalSharedRelcachesBuilt
94  * tells whether we got the cached descriptors.
95  */
96 static HeapTuple
GetDatabaseTuple(const char * dbname)97 GetDatabaseTuple(const char *dbname)
98 {
99 	HeapTuple	tuple;
100 	Relation	relation;
101 	SysScanDesc scan;
102 	ScanKeyData key[1];
103 
104 	/*
105 	 * form a scan key
106 	 */
107 	ScanKeyInit(&key[0],
108 				Anum_pg_database_datname,
109 				BTEqualStrategyNumber, F_NAMEEQ,
110 				CStringGetDatum(dbname));
111 
112 	/*
113 	 * Open pg_database and fetch a tuple.  Force heap scan if we haven't yet
114 	 * built the critical shared relcache entries (i.e., we're starting up
115 	 * without a shared relcache cache file).
116 	 */
117 	relation = table_open(DatabaseRelationId, AccessShareLock);
118 	scan = systable_beginscan(relation, DatabaseNameIndexId,
119 							  criticalSharedRelcachesBuilt,
120 							  NULL,
121 							  1, key);
122 
123 	tuple = systable_getnext(scan);
124 
125 	/* Must copy tuple before releasing buffer */
126 	if (HeapTupleIsValid(tuple))
127 		tuple = heap_copytuple(tuple);
128 
129 	/* all done */
130 	systable_endscan(scan);
131 	table_close(relation, AccessShareLock);
132 
133 	return tuple;
134 }
135 
136 /*
137  * GetDatabaseTupleByOid -- as above, but search by database OID
138  */
139 static HeapTuple
GetDatabaseTupleByOid(Oid dboid)140 GetDatabaseTupleByOid(Oid dboid)
141 {
142 	HeapTuple	tuple;
143 	Relation	relation;
144 	SysScanDesc scan;
145 	ScanKeyData key[1];
146 
147 	/*
148 	 * form a scan key
149 	 */
150 	ScanKeyInit(&key[0],
151 				Anum_pg_database_oid,
152 				BTEqualStrategyNumber, F_OIDEQ,
153 				ObjectIdGetDatum(dboid));
154 
155 	/*
156 	 * Open pg_database and fetch a tuple.  Force heap scan if we haven't yet
157 	 * built the critical shared relcache entries (i.e., we're starting up
158 	 * without a shared relcache cache file).
159 	 */
160 	relation = table_open(DatabaseRelationId, AccessShareLock);
161 	scan = systable_beginscan(relation, DatabaseOidIndexId,
162 							  criticalSharedRelcachesBuilt,
163 							  NULL,
164 							  1, key);
165 
166 	tuple = systable_getnext(scan);
167 
168 	/* Must copy tuple before releasing buffer */
169 	if (HeapTupleIsValid(tuple))
170 		tuple = heap_copytuple(tuple);
171 
172 	/* all done */
173 	systable_endscan(scan);
174 	table_close(relation, AccessShareLock);
175 
176 	return tuple;
177 }
178 
179 
180 /*
181  * PerformAuthentication -- authenticate a remote client
182  *
183  * returns: nothing.  Will not return at all if there's any failure.
184  */
185 static void
PerformAuthentication(Port * port)186 PerformAuthentication(Port *port)
187 {
188 	/* This should be set already, but let's make sure */
189 	ClientAuthInProgress = true;	/* limit visibility of log messages */
190 
191 	/*
192 	 * In EXEC_BACKEND case, we didn't inherit the contents of pg_hba.conf
193 	 * etcetera from the postmaster, and have to load them ourselves.
194 	 *
195 	 * FIXME: [fork/exec] Ugh.  Is there a way around this overhead?
196 	 */
197 #ifdef EXEC_BACKEND
198 
199 	/*
200 	 * load_hba() and load_ident() want to work within the PostmasterContext,
201 	 * so create that if it doesn't exist (which it won't).  We'll delete it
202 	 * again later, in PostgresMain.
203 	 */
204 	if (PostmasterContext == NULL)
205 		PostmasterContext = AllocSetContextCreate(TopMemoryContext,
206 												  "Postmaster",
207 												  ALLOCSET_DEFAULT_SIZES);
208 
209 	if (!load_hba())
210 	{
211 		/*
212 		 * It makes no sense to continue if we fail to load the HBA file,
213 		 * since there is no way to connect to the database in this case.
214 		 */
215 		ereport(FATAL,
216 				(errmsg("could not load pg_hba.conf")));
217 	}
218 
219 	if (!load_ident())
220 	{
221 		/*
222 		 * It is ok to continue if we fail to load the IDENT file, although it
223 		 * means that you cannot log in using any of the authentication
224 		 * methods that need a user name mapping. load_ident() already logged
225 		 * the details of error to the log.
226 		 */
227 	}
228 #endif
229 
230 	/*
231 	 * Set up a timeout in case a buggy or malicious client fails to respond
232 	 * during authentication.  Since we're inside a transaction and might do
233 	 * database access, we have to use the statement_timeout infrastructure.
234 	 */
235 	enable_timeout_after(STATEMENT_TIMEOUT, AuthenticationTimeout * 1000);
236 
237 	/*
238 	 * Now perform authentication exchange.
239 	 */
240 	ClientAuthentication(port); /* might not return, if failure */
241 
242 	/*
243 	 * Done with authentication.  Disable the timeout, and log if needed.
244 	 */
245 	disable_timeout(STATEMENT_TIMEOUT, false);
246 
247 	if (Log_connections)
248 	{
249 		StringInfoData logmsg;
250 
251 		initStringInfo(&logmsg);
252 		if (am_walsender)
253 			appendStringInfo(&logmsg, _("replication connection authorized: user=%s"),
254 							 port->user_name);
255 		else
256 			appendStringInfo(&logmsg, _("connection authorized: user=%s"),
257 							 port->user_name);
258 		if (!am_walsender)
259 			appendStringInfo(&logmsg, _(" database=%s"), port->database_name);
260 
261 		if (port->application_name != NULL)
262 			appendStringInfo(&logmsg, _(" application_name=%s"),
263 							 port->application_name);
264 
265 #ifdef USE_SSL
266 		if (port->ssl_in_use)
267 			appendStringInfo(&logmsg, _(" SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)"),
268 							 be_tls_get_version(port),
269 							 be_tls_get_cipher(port),
270 							 be_tls_get_cipher_bits(port),
271 							 be_tls_get_compression(port) ? _("on") : _("off"));
272 #endif
273 #ifdef ENABLE_GSS
274 		if (port->gss)
275 		{
276 			const char *princ = be_gssapi_get_princ(port);
277 
278 			if (princ)
279 				appendStringInfo(&logmsg,
280 								 _(" GSS (authenticated=%s, encrypted=%s, principal=%s)"),
281 								 be_gssapi_get_auth(port) ? _("yes") : _("no"),
282 								 be_gssapi_get_enc(port) ? _("yes") : _("no"),
283 								 princ);
284 			else
285 				appendStringInfo(&logmsg,
286 								 _(" GSS (authenticated=%s, encrypted=%s)"),
287 								 be_gssapi_get_auth(port) ? _("yes") : _("no"),
288 								 be_gssapi_get_enc(port) ? _("yes") : _("no"));
289 		}
290 #endif
291 
292 		ereport(LOG, errmsg_internal("%s", logmsg.data));
293 		pfree(logmsg.data);
294 	}
295 
296 	set_ps_display("startup", false);
297 
298 	ClientAuthInProgress = false;	/* client_min_messages is active now */
299 }
300 
301 
302 /*
303  * CheckMyDatabase -- fetch information from the pg_database entry for our DB
304  */
305 static void
CheckMyDatabase(const char * name,bool am_superuser,bool override_allow_connections)306 CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections)
307 {
308 	HeapTuple	tup;
309 	Form_pg_database dbform;
310 	char	   *collate;
311 	char	   *ctype;
312 
313 	/* Fetch our pg_database row normally, via syscache */
314 	tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
315 	if (!HeapTupleIsValid(tup))
316 		elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
317 	dbform = (Form_pg_database) GETSTRUCT(tup);
318 
319 	/* This recheck is strictly paranoia */
320 	if (strcmp(name, NameStr(dbform->datname)) != 0)
321 		ereport(FATAL,
322 				(errcode(ERRCODE_UNDEFINED_DATABASE),
323 				 errmsg("database \"%s\" has disappeared from pg_database",
324 						name),
325 				 errdetail("Database OID %u now seems to belong to \"%s\".",
326 						   MyDatabaseId, NameStr(dbform->datname))));
327 
328 	/*
329 	 * Check permissions to connect to the database.
330 	 *
331 	 * These checks are not enforced when in standalone mode, so that there is
332 	 * a way to recover from disabling all access to all databases, for
333 	 * example "UPDATE pg_database SET datallowconn = false;".
334 	 *
335 	 * We do not enforce them for autovacuum worker processes either.
336 	 */
337 	if (IsUnderPostmaster && !IsAutoVacuumWorkerProcess())
338 	{
339 		/*
340 		 * Check that the database is currently allowing connections.
341 		 */
342 		if (!dbform->datallowconn && !override_allow_connections)
343 			ereport(FATAL,
344 					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
345 					 errmsg("database \"%s\" is not currently accepting connections",
346 							name)));
347 
348 		/*
349 		 * Check privilege to connect to the database.  (The am_superuser test
350 		 * is redundant, but since we have the flag, might as well check it
351 		 * and save a few cycles.)
352 		 */
353 		if (!am_superuser &&
354 			pg_database_aclcheck(MyDatabaseId, GetUserId(),
355 								 ACL_CONNECT) != ACLCHECK_OK)
356 			ereport(FATAL,
357 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
358 					 errmsg("permission denied for database \"%s\"", name),
359 					 errdetail("User does not have CONNECT privilege.")));
360 
361 		/*
362 		 * Check connection limit for this database.
363 		 *
364 		 * There is a race condition here --- we create our PGPROC before
365 		 * checking for other PGPROCs.  If two backends did this at about the
366 		 * same time, they might both think they were over the limit, while
367 		 * ideally one should succeed and one fail.  Getting that to work
368 		 * exactly seems more trouble than it is worth, however; instead we
369 		 * just document that the connection limit is approximate.
370 		 */
371 		if (dbform->datconnlimit >= 0 &&
372 			!am_superuser &&
373 			CountDBConnections(MyDatabaseId) > dbform->datconnlimit)
374 			ereport(FATAL,
375 					(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
376 					 errmsg("too many connections for database \"%s\"",
377 							name)));
378 	}
379 
380 	/*
381 	 * OK, we're golden.  Next to-do item is to save the encoding info out of
382 	 * the pg_database tuple.
383 	 */
384 	SetDatabaseEncoding(dbform->encoding);
385 	/* Record it as a GUC internal option, too */
386 	SetConfigOption("server_encoding", GetDatabaseEncodingName(),
387 					PGC_INTERNAL, PGC_S_OVERRIDE);
388 	/* If we have no other source of client_encoding, use server encoding */
389 	SetConfigOption("client_encoding", GetDatabaseEncodingName(),
390 					PGC_BACKEND, PGC_S_DYNAMIC_DEFAULT);
391 
392 	/* assign locale variables */
393 	collate = NameStr(dbform->datcollate);
394 	ctype = NameStr(dbform->datctype);
395 
396 	if (pg_perm_setlocale(LC_COLLATE, collate) == NULL)
397 		ereport(FATAL,
398 				(errmsg("database locale is incompatible with operating system"),
399 				 errdetail("The database was initialized with LC_COLLATE \"%s\", "
400 						   " which is not recognized by setlocale().", collate),
401 				 errhint("Recreate the database with another locale or install the missing locale.")));
402 
403 	if (pg_perm_setlocale(LC_CTYPE, ctype) == NULL)
404 		ereport(FATAL,
405 				(errmsg("database locale is incompatible with operating system"),
406 				 errdetail("The database was initialized with LC_CTYPE \"%s\", "
407 						   " which is not recognized by setlocale().", ctype),
408 				 errhint("Recreate the database with another locale or install the missing locale.")));
409 
410 	/* Make the locale settings visible as GUC variables, too */
411 	SetConfigOption("lc_collate", collate, PGC_INTERNAL, PGC_S_OVERRIDE);
412 	SetConfigOption("lc_ctype", ctype, PGC_INTERNAL, PGC_S_OVERRIDE);
413 
414 	check_strxfrm_bug();
415 
416 	ReleaseSysCache(tup);
417 }
418 
419 
420 
421 /* --------------------------------
422  *		InitCommunication
423  *
424  *		This routine initializes stuff needed for ipc, locking, etc.
425  *		it should be called something more informative.
426  * --------------------------------
427  */
428 static void
InitCommunication(void)429 InitCommunication(void)
430 {
431 	/*
432 	 * initialize shared memory and semaphores appropriately.
433 	 */
434 	if (!IsUnderPostmaster)		/* postmaster already did this */
435 	{
436 		/*
437 		 * We're running a postgres bootstrap process or a standalone backend.
438 		 * Though we won't listen on PostPortNumber, use it to select a shmem
439 		 * key.  This increases the chance of detecting a leftover live
440 		 * backend of this DataDir.
441 		 */
442 		CreateSharedMemoryAndSemaphores(PostPortNumber);
443 	}
444 }
445 
446 
447 /*
448  * pg_split_opts -- split a string of options and append it to an argv array
449  *
450  * The caller is responsible for ensuring the argv array is large enough.  The
451  * maximum possible number of arguments added by this routine is
452  * (strlen(optstr) + 1) / 2.
453  *
454  * Because some option values can contain spaces we allow escaping using
455  * backslashes, with \\ representing a literal backslash.
456  */
457 void
pg_split_opts(char ** argv,int * argcp,const char * optstr)458 pg_split_opts(char **argv, int *argcp, const char *optstr)
459 {
460 	StringInfoData s;
461 
462 	initStringInfo(&s);
463 
464 	while (*optstr)
465 	{
466 		bool		last_was_escape = false;
467 
468 		resetStringInfo(&s);
469 
470 		/* skip over leading space */
471 		while (isspace((unsigned char) *optstr))
472 			optstr++;
473 
474 		if (*optstr == '\0')
475 			break;
476 
477 		/*
478 		 * Parse a single option, stopping at the first space, unless it's
479 		 * escaped.
480 		 */
481 		while (*optstr)
482 		{
483 			if (isspace((unsigned char) *optstr) && !last_was_escape)
484 				break;
485 
486 			if (!last_was_escape && *optstr == '\\')
487 				last_was_escape = true;
488 			else
489 			{
490 				last_was_escape = false;
491 				appendStringInfoChar(&s, *optstr);
492 			}
493 
494 			optstr++;
495 		}
496 
497 		/* now store the option in the next argv[] position */
498 		argv[(*argcp)++] = pstrdup(s.data);
499 	}
500 
501 	pfree(s.data);
502 }
503 
504 /*
505  * Initialize MaxBackends value from config options.
506  *
507  * This must be called after modules have had the chance to register background
508  * workers in shared_preload_libraries, and before shared memory size is
509  * determined.
510  *
511  * Note that in EXEC_BACKEND environment, the value is passed down from
512  * postmaster to subprocesses via BackendParameters in SubPostmasterMain; only
513  * postmaster itself and processes not under postmaster control should call
514  * this.
515  */
516 void
InitializeMaxBackends(void)517 InitializeMaxBackends(void)
518 {
519 	Assert(MaxBackends == 0);
520 
521 	/* the extra unit accounts for the autovacuum launcher */
522 	MaxBackends = MaxConnections + autovacuum_max_workers + 1 +
523 		max_worker_processes + max_wal_senders;
524 
525 	/* internal error because the values were all checked previously */
526 	if (MaxBackends > MAX_BACKENDS)
527 		elog(ERROR, "too many backends configured");
528 }
529 
530 /*
531  * Early initialization of a backend (either standalone or under postmaster).
532  * This happens even before InitPostgres.
533  *
534  * This is separate from InitPostgres because it is also called by auxiliary
535  * processes, such as the background writer process, which may not call
536  * InitPostgres at all.
537  */
538 void
BaseInit(void)539 BaseInit(void)
540 {
541 	/*
542 	 * Attach to shared memory and semaphores, and initialize our
543 	 * input/output/debugging file descriptors.
544 	 */
545 	InitCommunication();
546 	DebugFileOpen();
547 
548 	/* Do local initialization of file, storage and buffer managers */
549 	InitFileAccess();
550 	InitSync();
551 	smgrinit();
552 	InitBufferPoolAccess();
553 }
554 
555 
556 /* --------------------------------
557  * InitPostgres
558  *		Initialize POSTGRES.
559  *
560  * The database can be specified by name, using the in_dbname parameter, or by
561  * OID, using the dboid parameter.  In the latter case, the actual database
562  * name can be returned to the caller in out_dbname.  If out_dbname isn't
563  * NULL, it must point to a buffer of size NAMEDATALEN.
564  *
565  * Similarly, the username can be passed by name, using the username parameter,
566  * or by OID using the useroid parameter.
567  *
568  * In bootstrap mode no parameters are used.  The autovacuum launcher process
569  * doesn't use any parameters either, because it only goes far enough to be
570  * able to read pg_database; it doesn't connect to any particular database.
571  * In walsender mode only username is used.
572  *
573  * As of PostgreSQL 8.2, we expect InitProcess() was already called, so we
574  * already have a PGPROC struct ... but it's not completely filled in yet.
575  *
576  * Note:
577  *		Be very careful with the order of calls in the InitPostgres function.
578  * --------------------------------
579  */
580 void
InitPostgres(const char * in_dbname,Oid dboid,const char * username,Oid useroid,char * out_dbname,bool override_allow_connections)581 InitPostgres(const char *in_dbname, Oid dboid, const char *username,
582 			 Oid useroid, char *out_dbname, bool override_allow_connections)
583 {
584 	bool		bootstrap = IsBootstrapProcessingMode();
585 	bool		am_superuser;
586 	char	   *fullpath;
587 	char		dbname[NAMEDATALEN];
588 
589 	elog(DEBUG3, "InitPostgres");
590 
591 	/*
592 	 * Add my PGPROC struct to the ProcArray.
593 	 *
594 	 * Once I have done this, I am visible to other backends!
595 	 */
596 	InitProcessPhase2();
597 
598 	/*
599 	 * Initialize my entry in the shared-invalidation manager's array of
600 	 * per-backend data.
601 	 *
602 	 * Sets up MyBackendId, a unique backend identifier.
603 	 */
604 	MyBackendId = InvalidBackendId;
605 
606 	SharedInvalBackendInit(false);
607 
608 	if (MyBackendId > MaxBackends || MyBackendId <= 0)
609 		elog(FATAL, "bad backend ID: %d", MyBackendId);
610 
611 	/* Now that we have a BackendId, we can participate in ProcSignal */
612 	ProcSignalInit(MyBackendId);
613 
614 	/*
615 	 * Also set up timeout handlers needed for backend operation.  We need
616 	 * these in every case except bootstrap.
617 	 */
618 	if (!bootstrap)
619 	{
620 		RegisterTimeout(DEADLOCK_TIMEOUT, CheckDeadLockAlert);
621 		RegisterTimeout(STATEMENT_TIMEOUT, StatementTimeoutHandler);
622 		RegisterTimeout(LOCK_TIMEOUT, LockTimeoutHandler);
623 		RegisterTimeout(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
624 						IdleInTransactionSessionTimeoutHandler);
625 	}
626 
627 	/*
628 	 * bufmgr needs another initialization call too
629 	 */
630 	InitBufferPoolBackend();
631 
632 	/*
633 	 * Initialize local process's access to XLOG.
634 	 */
635 	if (IsUnderPostmaster)
636 	{
637 		/*
638 		 * The postmaster already started the XLOG machinery, but we need to
639 		 * call InitXLOGAccess(), if the system isn't in hot-standby mode.
640 		 * This is handled by calling RecoveryInProgress and ignoring the
641 		 * result.
642 		 */
643 		(void) RecoveryInProgress();
644 	}
645 	else
646 	{
647 		/*
648 		 * We are either a bootstrap process or a standalone backend. Either
649 		 * way, start up the XLOG machinery, and register to have it closed
650 		 * down at exit.
651 		 *
652 		 * We don't yet have an aux-process resource owner, but StartupXLOG
653 		 * and ShutdownXLOG will need one.  Hence, create said resource owner
654 		 * (and register a callback to clean it up after ShutdownXLOG runs).
655 		 */
656 		CreateAuxProcessResourceOwner();
657 
658 		StartupXLOG();
659 		/* Release (and warn about) any buffer pins leaked in StartupXLOG */
660 		ReleaseAuxProcessResources(true);
661 		/* Reset CurrentResourceOwner to nothing for the moment */
662 		CurrentResourceOwner = NULL;
663 
664 		on_shmem_exit(ShutdownXLOG, 0);
665 	}
666 
667 	/*
668 	 * Initialize the relation cache and the system catalog caches.  Note that
669 	 * no catalog access happens here; we only set up the hashtable structure.
670 	 * We must do this before starting a transaction because transaction abort
671 	 * would try to touch these hashtables.
672 	 */
673 	RelationCacheInitialize();
674 	InitCatalogCache();
675 	InitPlanCache();
676 
677 	/* Initialize portal manager */
678 	EnablePortalManager();
679 
680 	/* Initialize stats collection --- must happen before first xact */
681 	if (!bootstrap)
682 		pgstat_initialize();
683 
684 	/*
685 	 * Load relcache entries for the shared system catalogs.  This must create
686 	 * at least entries for pg_database and catalogs used for authentication.
687 	 */
688 	RelationCacheInitializePhase2();
689 
690 	/*
691 	 * Set up process-exit callback to do pre-shutdown cleanup.  This is the
692 	 * first before_shmem_exit callback we register; thus, this will be the
693 	 * last thing we do before low-level modules like the buffer manager begin
694 	 * to close down.  We need to have this in place before we begin our first
695 	 * transaction --- if we fail during the initialization transaction, as is
696 	 * entirely possible, we need the AbortTransaction call to clean up.
697 	 */
698 	before_shmem_exit(ShutdownPostgres, 0);
699 
700 	/* The autovacuum launcher is done here */
701 	if (IsAutoVacuumLauncherProcess())
702 	{
703 		/* report this backend in the PgBackendStatus array */
704 		pgstat_bestart();
705 
706 		return;
707 	}
708 
709 	/*
710 	 * Start a new transaction here before first access to db, and get a
711 	 * snapshot.  We don't have a use for the snapshot itself, but we're
712 	 * interested in the secondary effect that it sets RecentGlobalXmin. (This
713 	 * is critical for anything that reads heap pages, because HOT may decide
714 	 * to prune them even if the process doesn't attempt to modify any
715 	 * tuples.)
716 	 */
717 	if (!bootstrap)
718 	{
719 		/* statement_timestamp must be set for timeouts to work correctly */
720 		SetCurrentStatementStartTimestamp();
721 		StartTransactionCommand();
722 
723 		/*
724 		 * transaction_isolation will have been set to the default by the
725 		 * above.  If the default is "serializable", and we are in hot
726 		 * standby, we will fail if we don't change it to something lower.
727 		 * Fortunately, "read committed" is plenty good enough.
728 		 */
729 		XactIsoLevel = XACT_READ_COMMITTED;
730 
731 		(void) GetTransactionSnapshot();
732 	}
733 
734 	/*
735 	 * Perform client authentication if necessary, then figure out our
736 	 * postgres user ID, and see if we are a superuser.
737 	 *
738 	 * In standalone mode and in autovacuum worker processes, we use a fixed
739 	 * ID, otherwise we figure it out from the authenticated user name.
740 	 */
741 	if (bootstrap || IsAutoVacuumWorkerProcess())
742 	{
743 		InitializeSessionUserIdStandalone();
744 		am_superuser = true;
745 	}
746 	else if (!IsUnderPostmaster)
747 	{
748 		InitializeSessionUserIdStandalone();
749 		am_superuser = true;
750 		if (!ThereIsAtLeastOneRole())
751 			ereport(WARNING,
752 					(errcode(ERRCODE_UNDEFINED_OBJECT),
753 					 errmsg("no roles are defined in this database system"),
754 					 errhint("You should immediately run CREATE USER \"%s\" SUPERUSER;.",
755 							 username != NULL ? username : "postgres")));
756 	}
757 	else if (IsBackgroundWorker)
758 	{
759 		if (username == NULL && !OidIsValid(useroid))
760 		{
761 			InitializeSessionUserIdStandalone();
762 			am_superuser = true;
763 		}
764 		else
765 		{
766 			InitializeSessionUserId(username, useroid);
767 			am_superuser = superuser();
768 		}
769 	}
770 	else
771 	{
772 		/* normal multiuser case */
773 		Assert(MyProcPort != NULL);
774 		PerformAuthentication(MyProcPort);
775 		InitializeSessionUserId(username, useroid);
776 		am_superuser = superuser();
777 	}
778 
779 	/*
780 	 * If we're trying to shut down, only superusers can connect, and new
781 	 * replication connections are not allowed.
782 	 */
783 	if ((!am_superuser || am_walsender) &&
784 		MyProcPort != NULL &&
785 		MyProcPort->canAcceptConnections == CAC_SUPERUSER)
786 	{
787 		if (am_walsender)
788 			ereport(FATAL,
789 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
790 					 errmsg("new replication connections are not allowed during database shutdown")));
791 		else
792 			ereport(FATAL,
793 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
794 					 errmsg("must be superuser to connect during database shutdown")));
795 	}
796 
797 	/*
798 	 * Binary upgrades only allowed super-user connections
799 	 */
800 	if (IsBinaryUpgrade && !am_superuser)
801 	{
802 		ereport(FATAL,
803 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
804 				 errmsg("must be superuser to connect in binary upgrade mode")));
805 	}
806 
807 	/*
808 	 * The last few connection slots are reserved for superusers.  Replication
809 	 * connections are drawn from slots reserved with max_wal_senders and not
810 	 * limited by max_connections or superuser_reserved_connections.
811 	 */
812 	if (!am_superuser && !am_walsender &&
813 		ReservedBackends > 0 &&
814 		!HaveNFreeProcs(ReservedBackends))
815 		ereport(FATAL,
816 				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
817 				 errmsg("remaining connection slots are reserved for non-replication superuser connections")));
818 
819 	/* Check replication permissions needed for walsender processes. */
820 	if (am_walsender)
821 	{
822 		Assert(!bootstrap);
823 
824 		if (!superuser() && !has_rolreplication(GetUserId()))
825 			ereport(FATAL,
826 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
827 					 errmsg("must be superuser or replication role to start walsender")));
828 	}
829 
830 	/*
831 	 * If this is a plain walsender only supporting physical replication, we
832 	 * don't want to connect to any particular database. Just finish the
833 	 * backend startup by processing any options from the startup packet, and
834 	 * we're done.
835 	 */
836 	if (am_walsender && !am_db_walsender)
837 	{
838 		/* process any options passed in the startup packet */
839 		if (MyProcPort != NULL)
840 			process_startup_options(MyProcPort, am_superuser);
841 
842 		/* Apply PostAuthDelay as soon as we've read all options */
843 		if (PostAuthDelay > 0)
844 			pg_usleep(PostAuthDelay * 1000000L);
845 
846 		/* initialize client encoding */
847 		InitializeClientEncoding();
848 
849 		/* report this backend in the PgBackendStatus array */
850 		pgstat_bestart();
851 
852 		/* close the transaction we started above */
853 		CommitTransactionCommand();
854 
855 		return;
856 	}
857 
858 	/*
859 	 * Set up the global variables holding database id and default tablespace.
860 	 * But note we won't actually try to touch the database just yet.
861 	 *
862 	 * We take a shortcut in the bootstrap case, otherwise we have to look up
863 	 * the db's entry in pg_database.
864 	 */
865 	if (bootstrap)
866 	{
867 		MyDatabaseId = TemplateDbOid;
868 		MyDatabaseTableSpace = DEFAULTTABLESPACE_OID;
869 	}
870 	else if (in_dbname != NULL)
871 	{
872 		HeapTuple	tuple;
873 		Form_pg_database dbform;
874 
875 		tuple = GetDatabaseTuple(in_dbname);
876 		if (!HeapTupleIsValid(tuple))
877 			ereport(FATAL,
878 					(errcode(ERRCODE_UNDEFINED_DATABASE),
879 					 errmsg("database \"%s\" does not exist", in_dbname)));
880 		dbform = (Form_pg_database) GETSTRUCT(tuple);
881 		MyDatabaseId = dbform->oid;
882 		MyDatabaseTableSpace = dbform->dattablespace;
883 		/* take database name from the caller, just for paranoia */
884 		strlcpy(dbname, in_dbname, sizeof(dbname));
885 	}
886 	else if (OidIsValid(dboid))
887 	{
888 		/* caller specified database by OID */
889 		HeapTuple	tuple;
890 		Form_pg_database dbform;
891 
892 		tuple = GetDatabaseTupleByOid(dboid);
893 		if (!HeapTupleIsValid(tuple))
894 			ereport(FATAL,
895 					(errcode(ERRCODE_UNDEFINED_DATABASE),
896 					 errmsg("database %u does not exist", dboid)));
897 		dbform = (Form_pg_database) GETSTRUCT(tuple);
898 		MyDatabaseId = dbform->oid;
899 		MyDatabaseTableSpace = dbform->dattablespace;
900 		Assert(MyDatabaseId == dboid);
901 		strlcpy(dbname, NameStr(dbform->datname), sizeof(dbname));
902 		/* pass the database name back to the caller */
903 		if (out_dbname)
904 			strcpy(out_dbname, dbname);
905 	}
906 	else
907 	{
908 		/*
909 		 * If this is a background worker not bound to any particular
910 		 * database, we're done now.  Everything that follows only makes sense
911 		 * if we are bound to a specific database.  We do need to close the
912 		 * transaction we started before returning.
913 		 */
914 		if (!bootstrap)
915 		{
916 			pgstat_bestart();
917 			CommitTransactionCommand();
918 		}
919 		return;
920 	}
921 
922 	/*
923 	 * Now, take a writer's lock on the database we are trying to connect to.
924 	 * If there is a concurrently running DROP DATABASE on that database, this
925 	 * will block us until it finishes (and has committed its update of
926 	 * pg_database).
927 	 *
928 	 * Note that the lock is not held long, only until the end of this startup
929 	 * transaction.  This is OK since we will advertise our use of the
930 	 * database in the ProcArray before dropping the lock (in fact, that's the
931 	 * next thing to do).  Anyone trying a DROP DATABASE after this point will
932 	 * see us in the array once they have the lock.  Ordering is important for
933 	 * this because we don't want to advertise ourselves as being in this
934 	 * database until we have the lock; otherwise we create what amounts to a
935 	 * deadlock with CountOtherDBBackends().
936 	 *
937 	 * Note: use of RowExclusiveLock here is reasonable because we envision
938 	 * our session as being a concurrent writer of the database.  If we had a
939 	 * way of declaring a session as being guaranteed-read-only, we could use
940 	 * AccessShareLock for such sessions and thereby not conflict against
941 	 * CREATE DATABASE.
942 	 */
943 	if (!bootstrap)
944 		LockSharedObject(DatabaseRelationId, MyDatabaseId, 0,
945 						 RowExclusiveLock);
946 
947 	/*
948 	 * Now we can mark our PGPROC entry with the database ID.
949 	 *
950 	 * We assume this is an atomic store so no lock is needed; though actually
951 	 * things would work fine even if it weren't atomic.  Anyone searching the
952 	 * ProcArray for this database's ID should hold the database lock, so they
953 	 * would not be executing concurrently with this store.  A process looking
954 	 * for another database's ID could in theory see a chance match if it read
955 	 * a partially-updated databaseId value; but as long as all such searches
956 	 * wait and retry, as in CountOtherDBBackends(), they will certainly see
957 	 * the correct value on their next try.
958 	 */
959 	MyProc->databaseId = MyDatabaseId;
960 
961 	/*
962 	 * We established a catalog snapshot while reading pg_authid and/or
963 	 * pg_database; but until we have set up MyDatabaseId, we won't react to
964 	 * incoming sinval messages for unshared catalogs, so we won't realize it
965 	 * if the snapshot has been invalidated.  Assume it's no good anymore.
966 	 */
967 	InvalidateCatalogSnapshot();
968 
969 	/*
970 	 * Recheck pg_database to make sure the target database hasn't gone away.
971 	 * If there was a concurrent DROP DATABASE, this ensures we will die
972 	 * cleanly without creating a mess.
973 	 */
974 	if (!bootstrap)
975 	{
976 		HeapTuple	tuple;
977 
978 		tuple = GetDatabaseTuple(dbname);
979 		if (!HeapTupleIsValid(tuple) ||
980 			MyDatabaseId != ((Form_pg_database) GETSTRUCT(tuple))->oid ||
981 			MyDatabaseTableSpace != ((Form_pg_database) GETSTRUCT(tuple))->dattablespace)
982 			ereport(FATAL,
983 					(errcode(ERRCODE_UNDEFINED_DATABASE),
984 					 errmsg("database \"%s\" does not exist", dbname),
985 					 errdetail("It seems to have just been dropped or renamed.")));
986 	}
987 
988 	/*
989 	 * Now we should be able to access the database directory safely. Verify
990 	 * it's there and looks reasonable.
991 	 */
992 	fullpath = GetDatabasePath(MyDatabaseId, MyDatabaseTableSpace);
993 
994 	if (!bootstrap)
995 	{
996 		if (access(fullpath, F_OK) == -1)
997 		{
998 			if (errno == ENOENT)
999 				ereport(FATAL,
1000 						(errcode(ERRCODE_UNDEFINED_DATABASE),
1001 						 errmsg("database \"%s\" does not exist",
1002 								dbname),
1003 						 errdetail("The database subdirectory \"%s\" is missing.",
1004 								   fullpath)));
1005 			else
1006 				ereport(FATAL,
1007 						(errcode_for_file_access(),
1008 						 errmsg("could not access directory \"%s\": %m",
1009 								fullpath)));
1010 		}
1011 
1012 		ValidatePgVersion(fullpath);
1013 	}
1014 
1015 	SetDatabasePath(fullpath);
1016 
1017 	/*
1018 	 * It's now possible to do real access to the system catalogs.
1019 	 *
1020 	 * Load relcache entries for the system catalogs.  This must create at
1021 	 * least the minimum set of "nailed-in" cache entries.
1022 	 */
1023 	RelationCacheInitializePhase3();
1024 
1025 	/* set up ACL framework (so CheckMyDatabase can check permissions) */
1026 	initialize_acl();
1027 
1028 	/*
1029 	 * Re-read the pg_database row for our database, check permissions and set
1030 	 * up database-specific GUC settings.  We can't do this until all the
1031 	 * database-access infrastructure is up.  (Also, it wants to know if the
1032 	 * user is a superuser, so the above stuff has to happen first.)
1033 	 */
1034 	if (!bootstrap)
1035 		CheckMyDatabase(dbname, am_superuser, override_allow_connections);
1036 
1037 	/*
1038 	 * Now process any command-line switches and any additional GUC variable
1039 	 * settings passed in the startup packet.   We couldn't do this before
1040 	 * because we didn't know if client is a superuser.
1041 	 */
1042 	if (MyProcPort != NULL)
1043 		process_startup_options(MyProcPort, am_superuser);
1044 
1045 	/* Process pg_db_role_setting options */
1046 	process_settings(MyDatabaseId, GetSessionUserId());
1047 
1048 	/* Apply PostAuthDelay as soon as we've read all options */
1049 	if (PostAuthDelay > 0)
1050 		pg_usleep(PostAuthDelay * 1000000L);
1051 
1052 	/*
1053 	 * Initialize various default states that can't be set up until we've
1054 	 * selected the active user and gotten the right GUC settings.
1055 	 */
1056 
1057 	/* set default namespace search path */
1058 	InitializeSearchPath();
1059 
1060 	/* initialize client encoding */
1061 	InitializeClientEncoding();
1062 
1063 	/* Initialize this backend's session state. */
1064 	InitializeSession();
1065 
1066 	/* report this backend in the PgBackendStatus array */
1067 	if (!bootstrap)
1068 		pgstat_bestart();
1069 
1070 	/* close the transaction we started above */
1071 	if (!bootstrap)
1072 		CommitTransactionCommand();
1073 }
1074 
1075 /*
1076  * Process any command-line switches and any additional GUC variable
1077  * settings passed in the startup packet.
1078  */
1079 static void
process_startup_options(Port * port,bool am_superuser)1080 process_startup_options(Port *port, bool am_superuser)
1081 {
1082 	GucContext	gucctx;
1083 	ListCell   *gucopts;
1084 
1085 	gucctx = am_superuser ? PGC_SU_BACKEND : PGC_BACKEND;
1086 
1087 	/*
1088 	 * First process any command-line switches that were included in the
1089 	 * startup packet, if we are in a regular backend.
1090 	 */
1091 	if (port->cmdline_options != NULL)
1092 	{
1093 		/*
1094 		 * The maximum possible number of commandline arguments that could
1095 		 * come from port->cmdline_options is (strlen + 1) / 2; see
1096 		 * pg_split_opts().
1097 		 */
1098 		char	  **av;
1099 		int			maxac;
1100 		int			ac;
1101 
1102 		maxac = 2 + (strlen(port->cmdline_options) + 1) / 2;
1103 
1104 		av = (char **) palloc(maxac * sizeof(char *));
1105 		ac = 0;
1106 
1107 		av[ac++] = "postgres";
1108 
1109 		pg_split_opts(av, &ac, port->cmdline_options);
1110 
1111 		av[ac] = NULL;
1112 
1113 		Assert(ac < maxac);
1114 
1115 		(void) process_postgres_switches(ac, av, gucctx, NULL);
1116 	}
1117 
1118 	/*
1119 	 * Process any additional GUC variable settings passed in startup packet.
1120 	 * These are handled exactly like command-line variables.
1121 	 */
1122 	gucopts = list_head(port->guc_options);
1123 	while (gucopts)
1124 	{
1125 		char	   *name;
1126 		char	   *value;
1127 
1128 		name = lfirst(gucopts);
1129 		gucopts = lnext(gucopts);
1130 
1131 		value = lfirst(gucopts);
1132 		gucopts = lnext(gucopts);
1133 
1134 		SetConfigOption(name, value, gucctx, PGC_S_CLIENT);
1135 	}
1136 }
1137 
1138 /*
1139  * Load GUC settings from pg_db_role_setting.
1140  *
1141  * We try specific settings for the database/role combination, as well as
1142  * general for this database and for this user.
1143  */
1144 static void
process_settings(Oid databaseid,Oid roleid)1145 process_settings(Oid databaseid, Oid roleid)
1146 {
1147 	Relation	relsetting;
1148 	Snapshot	snapshot;
1149 
1150 	if (!IsUnderPostmaster)
1151 		return;
1152 
1153 	relsetting = table_open(DbRoleSettingRelationId, AccessShareLock);
1154 
1155 	/* read all the settings under the same snapshot for efficiency */
1156 	snapshot = RegisterSnapshot(GetCatalogSnapshot(DbRoleSettingRelationId));
1157 
1158 	/* Later settings are ignored if set earlier. */
1159 	ApplySetting(snapshot, databaseid, roleid, relsetting, PGC_S_DATABASE_USER);
1160 	ApplySetting(snapshot, InvalidOid, roleid, relsetting, PGC_S_USER);
1161 	ApplySetting(snapshot, databaseid, InvalidOid, relsetting, PGC_S_DATABASE);
1162 	ApplySetting(snapshot, InvalidOid, InvalidOid, relsetting, PGC_S_GLOBAL);
1163 
1164 	UnregisterSnapshot(snapshot);
1165 	table_close(relsetting, AccessShareLock);
1166 }
1167 
1168 /*
1169  * Backend-shutdown callback.  Do cleanup that we want to be sure happens
1170  * before all the supporting modules begin to nail their doors shut via
1171  * their own callbacks.
1172  *
1173  * User-level cleanup, such as temp-relation removal and UNLISTEN, happens
1174  * via separate callbacks that execute before this one.  We don't combine the
1175  * callbacks because we still want this one to happen if the user-level
1176  * cleanup fails.
1177  */
1178 static void
ShutdownPostgres(int code,Datum arg)1179 ShutdownPostgres(int code, Datum arg)
1180 {
1181 	/* Make sure we've killed any active transaction */
1182 	AbortOutOfAnyTransaction();
1183 
1184 	/*
1185 	 * User locks are not released by transaction end, so be sure to release
1186 	 * them explicitly.
1187 	 */
1188 	LockReleaseAll(USER_LOCKMETHOD, true);
1189 }
1190 
1191 
1192 /*
1193  * STATEMENT_TIMEOUT handler: trigger a query-cancel interrupt.
1194  */
1195 static void
StatementTimeoutHandler(void)1196 StatementTimeoutHandler(void)
1197 {
1198 	int			sig = SIGINT;
1199 
1200 	/*
1201 	 * During authentication the timeout is used to deal with
1202 	 * authentication_timeout - we want to quit in response to such timeouts.
1203 	 */
1204 	if (ClientAuthInProgress)
1205 		sig = SIGTERM;
1206 
1207 #ifdef HAVE_SETSID
1208 	/* try to signal whole process group */
1209 	kill(-MyProcPid, sig);
1210 #endif
1211 	kill(MyProcPid, sig);
1212 }
1213 
1214 /*
1215  * LOCK_TIMEOUT handler: trigger a query-cancel interrupt.
1216  */
1217 static void
LockTimeoutHandler(void)1218 LockTimeoutHandler(void)
1219 {
1220 #ifdef HAVE_SETSID
1221 	/* try to signal whole process group */
1222 	kill(-MyProcPid, SIGINT);
1223 #endif
1224 	kill(MyProcPid, SIGINT);
1225 }
1226 
1227 static void
IdleInTransactionSessionTimeoutHandler(void)1228 IdleInTransactionSessionTimeoutHandler(void)
1229 {
1230 	IdleInTransactionSessionTimeoutPending = true;
1231 	InterruptPending = true;
1232 	SetLatch(MyLatch);
1233 }
1234 
1235 /*
1236  * Returns true if at least one role is defined in this database cluster.
1237  */
1238 static bool
ThereIsAtLeastOneRole(void)1239 ThereIsAtLeastOneRole(void)
1240 {
1241 	Relation	pg_authid_rel;
1242 	TableScanDesc scan;
1243 	bool		result;
1244 
1245 	pg_authid_rel = table_open(AuthIdRelationId, AccessShareLock);
1246 
1247 	scan = table_beginscan_catalog(pg_authid_rel, 0, NULL);
1248 	result = (heap_getnext(scan, ForwardScanDirection) != NULL);
1249 
1250 	table_endscan(scan);
1251 	table_close(pg_authid_rel, AccessShareLock);
1252 
1253 	return result;
1254 }
1255