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