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