PostgreSQL Source Code git master
Loading...
Searching...
No Matches
postinit.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/session.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "catalog/namespace.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_database.h"
#include "catalog/pg_db_role_setting.h"
#include "catalog/pg_tablespace.h"
#include "libpq/auth.h"
#include "libpq/libpq-be.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "postmaster/autovacuum.h"
#include "postmaster/postmaster.h"
#include "replication/slot.h"
#include "replication/slotsync.h"
#include "replication/walsender.h"
#include "storage/aio_subsys.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/procnumber.h"
#include "storage/procsignal.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
#include "storage/sync.h"
#include "tcop/backend_startup.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc_hooks.h"
#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/portal.h"
#include "utils/ps_status.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/timeout.h"
Include dependency graph for postinit.c:

Go to the source code of this file.

Functions

static HeapTuple GetDatabaseTuple (const char *dbname)
 
static HeapTuple GetDatabaseTupleByOid (Oid dboid)
 
static void PerformAuthentication (Port *port)
 
static void CheckMyDatabase (const char *name, bool am_superuser, bool override_allow_connections)
 
static void ShutdownPostgres (int code, Datum arg)
 
static void StatementTimeoutHandler (void)
 
static void LockTimeoutHandler (void)
 
static void IdleInTransactionSessionTimeoutHandler (void)
 
static void TransactionTimeoutHandler (void)
 
static void IdleSessionTimeoutHandler (void)
 
static void IdleStatsUpdateTimeoutHandler (void)
 
static void ClientCheckTimeoutHandler (void)
 
static bool ThereIsAtLeastOneRole (void)
 
static void process_startup_options (Port *port, bool am_superuser)
 
static void process_settings (Oid databaseid, Oid roleid)
 
static void EmitConnectionWarnings (void)
 
void pg_split_opts (char **argv, int *argcp, const char *optstr)
 
void InitializeMaxBackends (void)
 
void InitializeFastPathLocks (void)
 
void BaseInit (void)
 
void InitPostgres (const char *in_dbname, Oid dboid, const char *username, Oid useroid, uint32 flags, char *out_dbname)
 
void StoreConnectionWarning (char *msg, char *detail)
 

Variables

static bool ConnectionWarningsEmitted
 
static ListConnectionWarningMessages
 
static ListConnectionWarningDetails
 

Function Documentation

◆ BaseInit()

void BaseInit ( void  )

Definition at line 616 of file postinit.c.

617{
618 Assert(MyProc != NULL);
619
620 /*
621 * Initialize our input/output/debugging file descriptors.
622 */
624
625 /*
626 * Initialize file access. Done early so other subsystems can access
627 * files.
628 */
630
631 /*
632 * Initialize statistics reporting. This needs to happen early to ensure
633 * that pgstat's shutdown callback runs after the shutdown callbacks of
634 * all subsystems that can produce stats (like e.g. transaction commits
635 * can).
636 */
638
639 /*
640 * Initialize AIO before infrastructure that might need to actually
641 * execute AIO.
642 */
644
645 /* Do local initialization of storage and buffer managers */
646 InitSync();
647 smgrinit();
649
650 /*
651 * Initialize temporary file access after pgstat, so that the temporary
652 * file shutdown hook can report temporary file statistics.
653 */
655
656 /*
657 * Initialize local buffers for WAL record construction, in case we ever
658 * try to insert XLOG.
659 */
661
662 /* Initialize lock manager's local structs */
664
665 /*
666 * Initialize replication slots after pgstat. The exit hook might need to
667 * drop ephemeral slots, which in turn triggers stats reporting.
668 */
670}
void pgaio_init_backend(void)
Definition aio_init.c:238
void InitBufferManagerAccess(void)
Definition bufmgr.c:4225
#define Assert(condition)
Definition c.h:943
void DebugFileOpen(void)
Definition elog.c:2307
void InitFileAccess(void)
Definition fd.c:904
void InitTemporaryFileAccess(void)
Definition fd.c:934
void InitLockManagerAccess(void)
Definition lock.c:502
void pgstat_initialize(void)
Definition pgstat.c:669
static int fb(int x)
void ReplicationSlotInitialize(void)
Definition slot.c:240
void smgrinit(void)
Definition smgr.c:188
PGPROC * MyProc
Definition proc.c:71
void InitSync(void)
Definition sync.c:125
void InitXLogInsert(void)

References Assert, DebugFileOpen(), fb(), InitBufferManagerAccess(), InitFileAccess(), InitLockManagerAccess(), InitSync(), InitTemporaryFileAccess(), InitXLogInsert(), MyProc, pgaio_init_backend(), pgstat_initialize(), ReplicationSlotInitialize(), and smgrinit().

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMainCommon(), BackgroundWorkerMain(), BootstrapModeMain(), PostgresMain(), and ReplSlotSyncWorkerMain().

◆ CheckMyDatabase()

static void CheckMyDatabase ( const char name,
bool  am_superuser,
bool  override_allow_connections 
)
static

Definition at line 332 of file postinit.c.

333{
336 Datum datum;
337 bool isnull;
338 char *collate;
339 char *ctype;
340
341 /* Fetch our pg_database row normally, via syscache */
343 if (!HeapTupleIsValid(tup))
344 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
346
347 /* This recheck is strictly paranoia */
348 if (strcmp(name, NameStr(dbform->datname)) != 0)
351 errmsg("database \"%s\" has disappeared from pg_database",
352 name),
353 errdetail("Database OID %u now seems to belong to \"%s\".",
354 MyDatabaseId, NameStr(dbform->datname))));
355
356 /*
357 * Check permissions to connect to the database.
358 *
359 * These checks are not enforced when in standalone mode, so that there is
360 * a way to recover from disabling all access to all databases, for
361 * example "UPDATE pg_database SET datallowconn = false;".
362 */
364 {
365 /*
366 * Check that the database is currently allowing connections.
367 * (Background processes can override this test and the next one by
368 * setting override_allow_connections.)
369 */
370 if (!dbform->datallowconn && !override_allow_connections)
373 errmsg("database \"%s\" is not currently accepting connections",
374 name)));
375
376 /*
377 * Check privilege to connect to the database. (The am_superuser test
378 * is redundant, but since we have the flag, might as well check it
379 * and save a few cycles.)
380 */
386 errmsg("permission denied for database \"%s\"", name),
387 errdetail("User does not have CONNECT privilege.")));
388
389 /*
390 * Check connection limit for this database. We enforce the limit
391 * only for regular backends, since other process types have their own
392 * PGPROC pools.
393 *
394 * There is a race condition here --- we create our PGPROC before
395 * checking for other PGPROCs. If two backends did this at about the
396 * same time, they might both think they were over the limit, while
397 * ideally one should succeed and one fail. Getting that to work
398 * exactly seems more trouble than it is worth, however; instead we
399 * just document that the connection limit is approximate.
400 */
401 if (dbform->datconnlimit >= 0 &&
403 !am_superuser &&
404 CountDBConnections(MyDatabaseId) > dbform->datconnlimit)
407 errmsg("too many connections for database \"%s\"",
408 name)));
409 }
410
411 /*
412 * OK, we're golden. Next to-do item is to save the encoding info out of
413 * the pg_database tuple.
414 */
415 SetDatabaseEncoding(dbform->encoding);
416 /* Record it as a GUC internal option, too */
417 SetConfigOption("server_encoding", GetDatabaseEncodingName(),
419 /* If we have no other source of client_encoding, use server encoding */
420 SetConfigOption("client_encoding", GetDatabaseEncodingName(),
422
423 /* assign locale variables */
425 collate = TextDatumGetCString(datum);
427 ctype = TextDatumGetCString(datum);
428
429 /*
430 * Historically, we set LC_COLLATE from datcollate, as well. That's no
431 * longer necessary because all collation behavior is handled through
432 * pg_locale_t.
433 */
434
435 if (pg_perm_setlocale(LC_CTYPE, ctype) == NULL)
437 (errmsg("database locale is incompatible with operating system"),
438 errdetail("The database was initialized with LC_CTYPE \"%s\", "
439 " which is not recognized by setlocale().", ctype),
440 errhint("Recreate the database with another locale or install the missing locale.")));
441
443
444 /*
445 * Check collation version. See similar code in
446 * pg_newlocale_from_collation(). Note that here we warn instead of error
447 * in any case, so that we don't prevent connecting.
448 */
450 &isnull);
451 if (!isnull)
452 {
453 char *actual_versionstr;
454 char *collversionstr;
455 char *locale;
456
458
459 if (dbform->datlocprovider == COLLPROVIDER_LIBC)
460 locale = collate;
461 else
462 {
464 locale = TextDatumGetCString(datum);
465 }
466
467 actual_versionstr = get_collation_actual_version(dbform->datlocprovider, locale);
469 /* should not happen */
471 "database \"%s\" has no actual collation version, but a version was recorded",
472 name);
475 (errmsg("database \"%s\" has a collation version mismatch",
476 name),
477 errdetail("The database was created using collation version %s, "
478 "but the operating system provides version %s.",
480 errhint("Rebuild all objects in this database that use the default collation and run "
481 "ALTER DATABASE %s REFRESH COLLATION VERSION, "
482 "or build PostgreSQL with the right library version.",
484 }
485
487}
@ ACLCHECK_OK
Definition acl.h:184
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition aclchk.c:3880
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:835
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define FATAL
Definition elog.h:42
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
bool IsUnderPostmaster
Definition globals.c:122
Oid MyDatabaseId
Definition globals.c:96
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4234
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_INTERNAL
Definition guc.h:73
@ PGC_BACKEND
Definition guc.h:77
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
const char * GetDatabaseEncodingName(void)
Definition mbutils.c:1395
void SetDatabaseEncoding(int encoding)
Definition mbutils.c:1290
#define AmRegularBackendProcess()
Definition miscadmin.h:396
Oid GetUserId(void)
Definition miscinit.c:470
static char * errmsg
#define ACL_CONNECT
Definition parsenodes.h:87
END_CATALOG_STRUCT typedef FormData_pg_database * Form_pg_database
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition pg_locale.c:1247
char * pg_perm_setlocale(int category, const char *locale)
Definition pg_locale.c:172
void init_database_collation(void)
Definition pg_locale.c:1131
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
int CountDBConnections(Oid databaseid)
Definition procarray.c:3671
const char * quote_identifier(const char *ident)
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:626
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
const char * name

References ACL_CONNECT, ACLCHECK_OK, AmRegularBackendProcess, CountDBConnections(), elog, ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, FATAL, fb(), Form_pg_database, get_collation_actual_version(), GetDatabaseEncodingName(), GETSTRUCT(), GetUserId(), HeapTupleIsValid, init_database_collation(), IsUnderPostmaster, MyDatabaseId, name, NameStr, object_aclcheck(), ObjectIdGetDatum(), pg_perm_setlocale(), PGC_BACKEND, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, quote_identifier(), ReleaseSysCache(), SearchSysCache1(), SetConfigOption(), SetDatabaseEncoding(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), TextDatumGetCString, and WARNING.

Referenced by InitPostgres().

◆ ClientCheckTimeoutHandler()

static void ClientCheckTimeoutHandler ( void  )
static

Definition at line 1472 of file postinit.c.

1473{
1475 InterruptPending = true;
1477}
volatile sig_atomic_t InterruptPending
Definition globals.c:32
struct Latch * MyLatch
Definition globals.c:65
volatile sig_atomic_t CheckClientConnectionPending
Definition globals.c:35
void SetLatch(Latch *latch)
Definition latch.c:290

References CheckClientConnectionPending, InterruptPending, MyLatch, and SetLatch().

Referenced by InitPostgres().

◆ EmitConnectionWarnings()

static void EmitConnectionWarnings ( void  )
static

Definition at line 1533 of file postinit.c.

1534{
1537
1539 elog(ERROR, "EmitConnectionWarnings() called more than once");
1540 else
1542
1545 {
1547 (errmsg("%s", (char *) lfirst(lc_msg)),
1548 errdetail("%s", (char *) lfirst(lc_detail))));
1549 }
1550
1553}
void list_free_deep(List *list)
Definition list.c:1560
#define lfirst(lc)
Definition pg_list.h:172
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
static List * ConnectionWarningDetails
Definition postinit.c:79
static bool ConnectionWarningsEmitted
Definition postinit.c:75
static List * ConnectionWarningMessages
Definition postinit.c:78

References ConnectionWarningDetails, ConnectionWarningMessages, ConnectionWarningsEmitted, elog, ereport, errdetail(), errmsg, ERROR, fb(), forboth, lfirst, list_free_deep(), and WARNING.

Referenced by InitPostgres().

◆ GetDatabaseTuple()

static HeapTuple GetDatabaseTuple ( const char dbname)
static

Definition at line 114 of file postinit.c.

115{
116 HeapTuple tuple;
117 Relation relation;
118 SysScanDesc scan;
119 ScanKeyData key[1];
120
121 /*
122 * form a scan key
123 */
124 ScanKeyInit(&key[0],
128
129 /*
130 * Open pg_database and fetch a tuple. Force heap scan if we haven't yet
131 * built the critical shared relcache entries (i.e., we're starting up
132 * without a shared relcache cache file).
133 */
137 NULL,
138 1, key);
139
140 tuple = systable_getnext(scan);
141
142 /* Must copy tuple before releasing buffer */
143 if (HeapTupleIsValid(tuple))
144 tuple = heap_copytuple(tuple);
145
146 /* all done */
147 systable_endscan(scan);
148 table_close(relation, AccessShareLock);
149
150 return tuple;
151}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:612
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:523
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
HeapTuple heap_copytuple(HeapTuple tuple)
Definition heaptuple.c:686
#define AccessShareLock
Definition lockdefs.h:36
static Datum CStringGetDatum(const char *X)
Definition postgres.h:383
bool criticalSharedRelcachesBuilt
Definition relcache.c:148
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
char * dbname
Definition streamutil.c:49
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References AccessShareLock, BTEqualStrategyNumber, criticalSharedRelcachesBuilt, CStringGetDatum(), dbname, fb(), heap_copytuple(), HeapTupleIsValid, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by InitPostgres().

◆ GetDatabaseTupleByOid()

static HeapTuple GetDatabaseTupleByOid ( Oid  dboid)
static

Definition at line 157 of file postinit.c.

158{
159 HeapTuple tuple;
160 Relation relation;
161 SysScanDesc scan;
162 ScanKeyData key[1];
163
164 /*
165 * form a scan key
166 */
167 ScanKeyInit(&key[0],
170 ObjectIdGetDatum(dboid));
171
172 /*
173 * Open pg_database and fetch a tuple. Force heap scan if we haven't yet
174 * built the critical shared relcache entries (i.e., we're starting up
175 * without a shared relcache cache file).
176 */
178 scan = systable_beginscan(relation, DatabaseOidIndexId,
180 NULL,
181 1, key);
182
183 tuple = systable_getnext(scan);
184
185 /* Must copy tuple before releasing buffer */
186 if (HeapTupleIsValid(tuple))
187 tuple = heap_copytuple(tuple);
188
189 /* all done */
190 systable_endscan(scan);
191 table_close(relation, AccessShareLock);
192
193 return tuple;
194}

References AccessShareLock, BTEqualStrategyNumber, criticalSharedRelcachesBuilt, fb(), heap_copytuple(), HeapTupleIsValid, ObjectIdGetDatum(), ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by InitPostgres().

◆ IdleInTransactionSessionTimeoutHandler()

static void IdleInTransactionSessionTimeoutHandler ( void  )
static

Definition at line 1448 of file postinit.c.

1449{
1451 InterruptPending = true;
1453}
volatile sig_atomic_t IdleInTransactionSessionTimeoutPending
Definition globals.c:37

References IdleInTransactionSessionTimeoutPending, InterruptPending, MyLatch, and SetLatch().

Referenced by InitPostgres().

◆ IdleSessionTimeoutHandler()

static void IdleSessionTimeoutHandler ( void  )
static

Definition at line 1456 of file postinit.c.

1457{
1459 InterruptPending = true;
1461}
volatile sig_atomic_t IdleSessionTimeoutPending
Definition globals.c:39

References IdleSessionTimeoutPending, InterruptPending, MyLatch, and SetLatch().

Referenced by InitPostgres().

◆ IdleStatsUpdateTimeoutHandler()

static void IdleStatsUpdateTimeoutHandler ( void  )
static

Definition at line 1464 of file postinit.c.

1465{
1467 InterruptPending = true;
1469}
volatile sig_atomic_t IdleStatsUpdateTimeoutPending
Definition globals.c:42

References IdleStatsUpdateTimeoutPending, InterruptPending, MyLatch, and SetLatch().

Referenced by InitPostgres().

◆ InitializeFastPathLocks()

void InitializeFastPathLocks ( void  )

Definition at line 584 of file postinit.c.

585{
586 /* Should be initialized only once. */
588
589 /*
590 * Based on the max_locks_per_transaction GUC, as that's a good indicator
591 * of the expected number of locks, figure out the value for
592 * FastPathLockGroupsPerBackend. This must be a power-of-two. We cap the
593 * value at FP_LOCK_GROUPS_PER_BACKEND_MAX and insist the value is at
594 * least 1.
595 *
596 * The default max_locks_per_transaction = 128 means 8 groups by default.
597 */
601
602 /* Validate we did get a power-of-two */
605}
#define Min(x, y)
Definition c.h:1091
#define Max(x, y)
Definition c.h:1085
int max_locks_per_xact
Definition lock.c:56
int FastPathLockGroupsPerBackend
Definition lock.c:205
static uint32 pg_nextpower2_32(uint32 num)
#define FP_LOCK_GROUPS_PER_BACKEND_MAX
Definition proc.h:95
#define FP_LOCK_SLOTS_PER_GROUP
Definition proc.h:96

References Assert, FastPathLockGroupsPerBackend, FP_LOCK_GROUPS_PER_BACKEND_MAX, FP_LOCK_SLOTS_PER_GROUP, Max, max_locks_per_xact, Min, and pg_nextpower2_32().

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), and PostmasterMain().

◆ InitializeMaxBackends()

void InitializeMaxBackends ( void  )

Definition at line 559 of file postinit.c.

560{
561 Assert(MaxBackends == 0);
562
563 /* Note that this does not include "auxiliary" processes */
566
570 errmsg("too many server processes configured"),
571 errdetail("\"max_connections\" (%d) plus \"autovacuum_worker_slots\" (%d) plus \"max_worker_processes\" (%d) plus \"max_wal_senders\" (%d) must be less than %d.",
575}
int autovacuum_worker_slots
Definition autovacuum.c:124
int MaxConnections
Definition globals.c:145
int MaxBackends
Definition globals.c:149
int max_worker_processes
Definition globals.c:146
#define NUM_SPECIAL_WORKER_PROCS
Definition proc.h:514
#define MAX_BACKENDS
Definition procnumber.h:39
int max_wal_senders
Definition walsender.c:141

References Assert, autovacuum_worker_slots, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), MAX_BACKENDS, max_wal_senders, max_worker_processes, MaxBackends, MaxConnections, and NUM_SPECIAL_WORKER_PROCS.

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), and PostmasterMain().

◆ InitPostgres()

void InitPostgres ( const char in_dbname,
Oid  dboid,
const char username,
Oid  useroid,
uint32  flags,
char out_dbname 
)

Definition at line 716 of file postinit.c.

720{
722 bool am_superuser;
723 char *fullpath;
724 char dbname[NAMEDATALEN];
725 int nfree = 0;
726
727 elog(DEBUG3, "InitPostgres");
728
729 /*
730 * Add my PGPROC struct to the ProcArray.
731 *
732 * Once I have done this, I am visible to other backends!
733 */
735
736 /* Initialize status reporting */
738
739 /*
740 * And initialize an entry in the PgBackendStatus array. That way, if
741 * LWLocks or third-party authentication should happen to hang, it is
742 * possible to retrieve some information about what is going on.
743 */
744 if (!bootstrap)
745 {
747 INJECTION_POINT("init-pre-auth", NULL);
748 }
749
750 /*
751 * Initialize my entry in the shared-invalidation manager's array of
752 * per-backend data.
753 */
755
756 /*
757 * Prevent consuming interrupts between setting ProcSignalInit and setting
758 * the initial local data checksum value. If a barrier is emitted, and
759 * absorbed, before local cached state is initialized the state transition
760 * can be invalid.
761 */
763
765
766 /*
767 * Initialize a local cache of the data_checksum_version, to be updated by
768 * the procsignal-based barriers.
769 *
770 * This intentionally happens after initializing the procsignal, otherwise
771 * we might miss a state change. This means we can get a barrier for the
772 * state we've just initialized.
773 *
774 * The postmaster (which is what gets forked into the new child process)
775 * does not handle barriers, therefore it may not have the current value
776 * of LocalDataChecksumVersion value (it'll have the value read from the
777 * control file, which may be arbitrarily old).
778 *
779 * NB: Even if the postmaster handled barriers, the value might still be
780 * stale, as it might have changed after this process forked.
781 */
783
785
786 /*
787 * Also set up timeout handlers needed for backend operation. We need
788 * these in every case except bootstrap.
789 */
790 if (!bootstrap)
791 {
802 }
803
804 /*
805 * If this is either a bootstrap process or a standalone backend, start up
806 * the XLOG machinery, and register to have it closed down at exit. In
807 * other cases, the startup process is responsible for starting up the
808 * XLOG machinery, and the checkpointer for closing it down.
809 */
811 {
812 /*
813 * We don't yet have an aux-process resource owner, but StartupXLOG
814 * and ShutdownXLOG will need one. Hence, create said resource owner
815 * (and register a callback to clean it up after ShutdownXLOG runs).
816 */
818
819 StartupXLOG();
820 /* Release (and warn about) any buffer pins leaked in StartupXLOG */
822 /* Reset CurrentResourceOwner to nothing for the moment */
824
825 /*
826 * Use before_shmem_exit() so that ShutdownXLOG() can rely on DSM
827 * segments etc to work (which in turn is required for pgstats).
828 */
831 }
832
833 /*
834 * Initialize the process-local logical info WAL logging state.
835 *
836 * This must be called after ProcSignalInit() so that the process can
837 * participate in procsignal-based barriers that update this state.
838 * Furthermore, in !IsUnderPostmaster cases, this must occur after
839 * StartupXLOG() where the shared state is first established.
840 */
842
843 /*
844 * Initialize the relation cache and the system catalog caches. Note that
845 * no catalog access happens here; we only set up the hashtable structure.
846 * We must do this before starting a transaction because transaction abort
847 * would try to touch these hashtables.
848 */
852
853 /* Initialize portal manager */
855
856 /*
857 * Load relcache entries for the shared system catalogs. This must create
858 * at least entries for pg_database and catalogs used for authentication.
859 */
861
862 /*
863 * Set up process-exit callback to do pre-shutdown cleanup. This is one
864 * of the first before_shmem_exit callbacks we register; thus, this will
865 * be one of the last things we do before low-level modules like the
866 * buffer manager begin to close down. We need to have this in place
867 * before we begin our first transaction --- if we fail during the
868 * initialization transaction, as is entirely possible, we need the
869 * AbortTransaction call to clean up.
870 */
872
873 /* The autovacuum launcher is done here */
875 {
876 /* fill in the remainder of this entry in the PgBackendStatus array */
878
879 return;
880 }
881
882 /*
883 * Start a new transaction here before first access to db.
884 */
885 if (!bootstrap)
886 {
887 /* statement_timestamp must be set for timeouts to work correctly */
890
891 /*
892 * transaction_isolation will have been set to the default by the
893 * above. If the default is "serializable", and we are in hot
894 * standby, we will fail if we don't change it to something lower.
895 * Fortunately, "read committed" is plenty good enough.
896 */
898 }
899
900 /*
901 * Perform client authentication if necessary, then figure out our
902 * postgres user ID, and see if we are a superuser.
903 *
904 * In standalone mode, autovacuum worker processes and slot sync worker
905 * process, we use a fixed ID, otherwise we figure it out from the
906 * authenticated user name.
907 */
909 {
911 am_superuser = true;
912 }
913 else if (!IsUnderPostmaster)
914 {
916 am_superuser = true;
920 errmsg("no roles are defined in this database system"),
921 errhint("You should immediately run CREATE USER \"%s\" SUPERUSER;.",
922 username != NULL ? username : "postgres")));
923 }
925 {
926 if (username == NULL && !OidIsValid(useroid))
927 {
929 am_superuser = true;
930 }
931 else
932 {
934 (flags & INIT_PG_OVERRIDE_ROLE_LOGIN) != 0);
936 }
937 }
938 else
939 {
940 /* normal multiuser case */
944 /* ensure that auth_method is actually valid, aka authn_id is not NULL */
949 }
950
951 /* Report any SSL/GSS details for the session. */
952 if (MyProcPort != NULL)
953 {
955
957 }
958
959 /*
960 * Binary upgrades only allowed super-user connections
961 */
963 {
966 errmsg("must be superuser to connect in binary upgrade mode")));
967 }
968
969 /*
970 * The last few regular connection slots are reserved for superusers and
971 * roles with privileges of pg_use_reserved_connections. We do not apply
972 * these limits to background processes, since they all have their own
973 * pools of PGPROC slots.
974 *
975 * Note: At this point, the new backend has already claimed a proc struct,
976 * so we must check whether the number of free slots is strictly less than
977 * the reserved connection limits.
978 */
982 {
986 errmsg("remaining connection slots are reserved for roles with the %s attribute",
987 "SUPERUSER")));
988
992 errmsg("remaining connection slots are reserved for roles with privileges of the \"%s\" role",
993 "pg_use_reserved_connections")));
994 }
995
996 /* Check replication permissions needed for walsender processes. */
997 if (am_walsender)
998 {
1000
1002 ereport(FATAL,
1004 errmsg("permission denied to start WAL sender"),
1005 errdetail("Only roles with the %s attribute may start a WAL sender process.",
1006 "REPLICATION")));
1007 }
1008
1009 /*
1010 * If this is a plain walsender only supporting physical replication, we
1011 * don't want to connect to any particular database. Just finish the
1012 * backend startup by processing any options from the startup packet, and
1013 * we're done.
1014 */
1016 {
1017 /* process any options passed in the startup packet */
1018 if (MyProcPort != NULL)
1020
1021 /* Apply PostAuthDelay as soon as we've read all options */
1022 if (PostAuthDelay > 0)
1023 pg_usleep(PostAuthDelay * 1000000L);
1024
1025 /* initialize client encoding */
1027
1028 /* fill in the remainder of this entry in the PgBackendStatus array */
1030
1031 /* close the transaction we started above */
1033
1034 /* send any WARNINGs we've accumulated during initialization */
1036
1037 return;
1038 }
1039
1040 /*
1041 * Set up the global variables holding database id and default tablespace.
1042 * But note we won't actually try to touch the database just yet.
1043 *
1044 * We take a shortcut in the bootstrap case, otherwise we have to look up
1045 * the db's entry in pg_database.
1046 */
1047 if (bootstrap)
1048 {
1049 dboid = Template1DbOid;
1051 }
1052 else if (in_dbname != NULL)
1053 {
1054 HeapTuple tuple;
1056
1057 tuple = GetDatabaseTuple(in_dbname);
1058 if (!HeapTupleIsValid(tuple))
1059 ereport(FATAL,
1061 errmsg("database \"%s\" does not exist", in_dbname)));
1063 dboid = dbform->oid;
1064 }
1065 else if (!OidIsValid(dboid))
1066 {
1067 /*
1068 * If this is a background worker not bound to any particular
1069 * database, we're done now. Everything that follows only makes sense
1070 * if we are bound to a specific database. We do need to close the
1071 * transaction we started before returning.
1072 */
1073 if (!bootstrap)
1074 {
1077 }
1078 return;
1079 }
1080
1081 /*
1082 * Now, take a writer's lock on the database we are trying to connect to.
1083 * If there is a concurrently running DROP DATABASE on that database, this
1084 * will block us until it finishes (and has committed its update of
1085 * pg_database).
1086 *
1087 * Note that the lock is not held long, only until the end of this startup
1088 * transaction. This is OK since we will advertise our use of the
1089 * database in the ProcArray before dropping the lock (in fact, that's the
1090 * next thing to do). Anyone trying a DROP DATABASE after this point will
1091 * see us in the array once they have the lock. Ordering is important for
1092 * this because we don't want to advertise ourselves as being in this
1093 * database until we have the lock; otherwise we create what amounts to a
1094 * deadlock with CountOtherDBBackends().
1095 *
1096 * Note: use of RowExclusiveLock here is reasonable because we envision
1097 * our session as being a concurrent writer of the database. If we had a
1098 * way of declaring a session as being guaranteed-read-only, we could use
1099 * AccessShareLock for such sessions and thereby not conflict against
1100 * CREATE DATABASE.
1101 */
1102 if (!bootstrap)
1104
1105 /*
1106 * Recheck pg_database to make sure the target database hasn't gone away.
1107 * If there was a concurrent DROP DATABASE, this ensures we will die
1108 * cleanly without creating a mess.
1109 */
1110 if (!bootstrap)
1111 {
1112 HeapTuple tuple;
1114
1115 tuple = GetDatabaseTupleByOid(dboid);
1116 if (HeapTupleIsValid(tuple))
1118
1119 if (!HeapTupleIsValid(tuple) ||
1120 (in_dbname && namestrcmp(&datform->datname, in_dbname)))
1121 {
1122 if (in_dbname)
1123 ereport(FATAL,
1125 errmsg("database \"%s\" does not exist", in_dbname),
1126 errdetail("It seems to have just been dropped or renamed.")));
1127 else
1128 ereport(FATAL,
1130 errmsg("database %u does not exist", dboid)));
1131 }
1132
1133 strlcpy(dbname, NameStr(datform->datname), sizeof(dbname));
1134
1136 {
1137 ereport(FATAL,
1139 errmsg("cannot connect to invalid database \"%s\"", dbname),
1140 errhint("Use DROP DATABASE to drop invalid databases."));
1141 }
1142
1143 MyDatabaseTableSpace = datform->dattablespace;
1144 MyDatabaseHasLoginEventTriggers = datform->dathasloginevt;
1145 /* pass the database name back to the caller */
1146 if (out_dbname)
1148 }
1149
1150 /*
1151 * Now that we rechecked, we are certain to be connected to a database and
1152 * thus can set MyDatabaseId.
1153 *
1154 * It is important that MyDatabaseId only be set once we are sure that the
1155 * target database can no longer be concurrently dropped or renamed. For
1156 * example, without this guarantee, pgstat_update_dbstats() could create
1157 * entries for databases that were just dropped in the pgstat shutdown
1158 * callback, which could confuse other code paths like the autovacuum
1159 * scheduler.
1160 */
1161 MyDatabaseId = dboid;
1162
1163 /*
1164 * Now we can mark our PGPROC entry with the database ID.
1165 *
1166 * We assume this is an atomic store so no lock is needed; though actually
1167 * things would work fine even if it weren't atomic. Anyone searching the
1168 * ProcArray for this database's ID should hold the database lock, so they
1169 * would not be executing concurrently with this store. A process looking
1170 * for another database's ID could in theory see a chance match if it read
1171 * a partially-updated databaseId value; but as long as all such searches
1172 * wait and retry, as in CountOtherDBBackends(), they will certainly see
1173 * the correct value on their next try.
1174 */
1176
1177 /*
1178 * We established a catalog snapshot while reading pg_authid and/or
1179 * pg_database; but until we have set up MyDatabaseId, we won't react to
1180 * incoming sinval messages for unshared catalogs, so we won't realize it
1181 * if the snapshot has been invalidated. Assume it's no good anymore.
1182 */
1184
1185 /*
1186 * Now we should be able to access the database directory safely. Verify
1187 * it's there and looks reasonable.
1188 */
1190
1191 if (!bootstrap)
1192 {
1193 if (access(fullpath, F_OK) == -1)
1194 {
1195 if (errno == ENOENT)
1196 ereport(FATAL,
1198 errmsg("database \"%s\" does not exist",
1199 dbname),
1200 errdetail("The database subdirectory \"%s\" is missing.",
1201 fullpath)));
1202 else
1203 ereport(FATAL,
1205 errmsg("could not access directory \"%s\": %m",
1206 fullpath)));
1207 }
1208
1209 ValidatePgVersion(fullpath);
1210 }
1211
1212 SetDatabasePath(fullpath);
1213 pfree(fullpath);
1214
1215 /*
1216 * It's now possible to do real access to the system catalogs.
1217 *
1218 * Load relcache entries for the system catalogs. This must create at
1219 * least the minimum set of "nailed-in" cache entries.
1220 */
1222
1223 /* set up ACL framework (so CheckMyDatabase can check permissions) */
1225
1226 /*
1227 * Re-read the pg_database row for our database, check permissions and set
1228 * up database-specific GUC settings. We can't do this until all the
1229 * database-access infrastructure is up. (Also, it wants to know if the
1230 * user is a superuser, so the above stuff has to happen first.)
1231 */
1232 if (!bootstrap)
1234 (flags & INIT_PG_OVERRIDE_ALLOW_CONNS) != 0);
1235
1236 /*
1237 * Now process any command-line switches and any additional GUC variable
1238 * settings passed in the startup packet. We couldn't do this before
1239 * because we didn't know if client is a superuser.
1240 */
1241 if (MyProcPort != NULL)
1243
1244 /* Process pg_db_role_setting options */
1246
1247 /* Apply PostAuthDelay as soon as we've read all options */
1248 if (PostAuthDelay > 0)
1249 pg_usleep(PostAuthDelay * 1000000L);
1250
1251 /*
1252 * Initialize various default states that can't be set up until we've
1253 * selected the active user and gotten the right GUC settings.
1254 */
1255
1256 /* set default namespace search path */
1258
1259 /* initialize client encoding */
1261
1262 /* Initialize this backend's session state. */
1264
1265 /*
1266 * If this is an interactive session, load any libraries that should be
1267 * preloaded at backend start. Since those are determined by GUCs, this
1268 * can't happen until GUC settings are complete, but we want it to happen
1269 * during the initial transaction in case anything that requires database
1270 * access needs to be done.
1271 */
1272 if ((flags & INIT_PG_LOAD_SESSION_LIBS) != 0)
1274
1275 /* fill in the remainder of this entry in the PgBackendStatus array */
1276 if (!bootstrap)
1278
1279 /* close the transaction we started above */
1280 if (!bootstrap)
1282
1283 /* send any WARNINGs we've accumulated during initialization */
1285}
void initialize_acl(void)
Definition acl.c:5069
bool has_privs_of_role(Oid member, Oid role)
Definition acl.c:5314
void pgstat_bestart_security(void)
void pgstat_bestart_initial(void)
void pgstat_beinit(void)
void pgstat_bestart_final(void)
#define OidIsValid(objectId)
Definition c.h:858
bool database_is_invalid_form(Form_pg_database datform)
int errcode_for_file_access(void)
Definition elog.c:898
#define DEBUG3
Definition elog.h:29
bool MyDatabaseHasLoginEventTriggers
Definition globals.c:100
int MyCancelKeyLength
Definition globals.c:55
bool IsBinaryUpgrade
Definition globals.c:123
Oid MyDatabaseTableSpace
Definition globals.c:98
uint8 MyCancelKey[MAX_CANCEL_KEY_LENGTH]
Definition globals.c:54
struct Port * MyProcPort
Definition globals.c:53
const char * hba_authname(UserAuth auth_method)
Definition hba.c:2948
static char * username
Definition initdb.c:153
#define INJECTION_POINT(name, arg)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1088
#define RowExclusiveLock
Definition lockdefs.h:38
void InitializeProcessXLogLogicalInfo(void)
Definition logicalctl.c:174
void InitializeClientEncoding(void)
Definition mbutils.c:290
void pfree(void *pointer)
Definition mcxt.c:1619
#define RESUME_INTERRUPTS()
Definition miscadmin.h:138
#define IsBootstrapProcessingMode()
Definition miscadmin.h:495
#define INIT_PG_LOAD_SESSION_LIBS
Definition miscadmin.h:517
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:398
#define AmBackgroundWorkerProcess()
Definition miscadmin.h:399
#define AmLogicalSlotSyncWorkerProcess()
Definition miscadmin.h:401
#define HOLD_INTERRUPTS()
Definition miscadmin.h:136
#define AmAutoVacuumLauncherProcess()
Definition miscadmin.h:397
#define INIT_PG_OVERRIDE_ROLE_LOGIN
Definition miscadmin.h:519
#define AmDataChecksumsWorkerProcess()
Definition miscadmin.h:410
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition miscadmin.h:518
void InitializeSessionUserId(const char *rolename, Oid roleid, bool bypass_login_check)
Definition miscinit.c:711
void InitializeSystemUser(const char *authn_id, const char *auth_method)
Definition miscinit.c:876
void InitializeSessionUserIdStandalone(void)
Definition miscinit.c:841
void process_session_preload_libraries(void)
Definition miscinit.c:1867
Oid GetSessionUserId(void)
Definition miscinit.c:509
void SetDatabasePath(const char *path)
Definition miscinit.c:284
ClientConnectionInfo MyClientConnectionInfo
Definition miscinit.c:1020
bool has_rolreplication(Oid roleid)
Definition miscinit.c:689
void ValidatePgVersion(const char *path)
Definition miscinit.c:1720
int namestrcmp(Name name, const char *str)
Definition name.c:247
void InitializeSearchPath(void)
Definition namespace.c:4808
#define NAMEDATALEN
void pgstat_before_server_shutdown(int code, Datum arg)
Definition pgstat.c:590
void InitPlanCache(void)
Definition plancache.c:148
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
void EnablePortalManager(void)
Definition portalmem.c:106
int PostAuthDelay
Definition postgres.c:105
static void ShutdownPostgres(int code, Datum arg)
Definition postinit.c:1391
static void IdleInTransactionSessionTimeoutHandler(void)
Definition postinit.c:1448
static void LockTimeoutHandler(void)
Definition postinit.c:1430
static void IdleStatsUpdateTimeoutHandler(void)
Definition postinit.c:1464
static void process_settings(Oid databaseid, Oid roleid)
Definition postinit.c:1357
static void IdleSessionTimeoutHandler(void)
Definition postinit.c:1456
static void process_startup_options(Port *port, bool am_superuser)
Definition postinit.c:1292
static void StatementTimeoutHandler(void)
Definition postinit.c:1408
static void EmitConnectionWarnings(void)
Definition postinit.c:1533
static void CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections)
Definition postinit.c:332
static bool ThereIsAtLeastOneRole(void)
Definition postinit.c:1483
static void PerformAuthentication(Port *port)
Definition postinit.c:203
static void ClientCheckTimeoutHandler(void)
Definition postinit.c:1472
static HeapTuple GetDatabaseTuple(const char *dbname)
Definition postinit.c:114
static HeapTuple GetDatabaseTupleByOid(Oid dboid)
Definition postinit.c:157
static void TransactionTimeoutHandler(void)
Definition postinit.c:1440
int ReservedConnections
Definition postmaster.c:231
int SuperuserReservedConnections
Definition postmaster.c:230
short access
void ProcSignalInit(const uint8 *cancel_key, int cancel_key_len)
Definition procsignal.c:170
void RelationCacheInitializePhase3(void)
Definition relcache.c:4109
void RelationCacheInitialize(void)
Definition relcache.c:4004
void RelationCacheInitializePhase2(void)
Definition relcache.c:4050
char * GetDatabasePath(Oid dbOid, Oid spcOid)
Definition relpath.c:110
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1016
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
void CreateAuxProcessResourceOwner(void)
Definition resowner.c:996
void InitializeSession(void)
Definition session.c:54
void pg_usleep(long microsec)
Definition signal.c:53
void SharedInvalBackendInit(bool sendOnly)
Definition sinvaladt.c:274
void InvalidateCatalogSnapshot(void)
Definition snapmgr.c:455
bool HaveNFreeProcs(int n, int *nfree)
Definition proc.c:787
void CheckDeadLockAlert(void)
Definition proc.c:1914
void InitProcessPhase2(void)
Definition proc.c:583
const char * authn_id
Definition libpq-be.h:99
Oid databaseId
Definition proc.h:201
bool superuser(void)
Definition superuser.c:47
void InitCatalogCache(void)
Definition syscache.c:111
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition timeout.c:505
@ IDLE_SESSION_TIMEOUT
Definition timeout.h:35
@ IDLE_IN_TRANSACTION_SESSION_TIMEOUT
Definition timeout.h:33
@ LOCK_TIMEOUT
Definition timeout.h:28
@ STATEMENT_TIMEOUT
Definition timeout.h:29
@ DEADLOCK_TIMEOUT
Definition timeout.h:27
@ TRANSACTION_TIMEOUT
Definition timeout.h:34
@ IDLE_STATS_UPDATE_TIMEOUT
Definition timeout.h:36
@ CLIENT_CONNECTION_CHECK_TIMEOUT
Definition timeout.h:37
bool am_walsender
Definition walsender.c:135
bool am_db_walsender
Definition walsender.c:138
void StartTransactionCommand(void)
Definition xact.c:3109
int XactIsoLevel
Definition xact.c:81
void SetCurrentStatementStartTimestamp(void)
Definition xact.c:916
void CommitTransactionCommand(void)
Definition xact.c:3207
#define XACT_READ_COMMITTED
Definition xact.h:37
void StartupXLOG(void)
Definition xlog.c:5847
void ShutdownXLOG(int code, Datum arg)
Definition xlog.c:7100
void InitLocalDataChecksumState(void)
Definition xlog.c:4962

References am_db_walsender, am_walsender, AmAutoVacuumLauncherProcess, AmAutoVacuumWorkerProcess, AmBackgroundWorkerProcess, AmDataChecksumsWorkerProcess, AmLogicalSlotSyncWorkerProcess, AmRegularBackendProcess, Assert, ClientConnectionInfo::auth_method, ClientConnectionInfo::authn_id, before_shmem_exit(), CheckDeadLockAlert(), CheckMyDatabase(), CLIENT_CONNECTION_CHECK_TIMEOUT, ClientCheckTimeoutHandler(), CommitTransactionCommand(), CreateAuxProcessResourceOwner(), CurrentResourceOwner, database_is_invalid_form(), PGPROC::databaseId, dbname, DEADLOCK_TIMEOUT, DEBUG3, elog, EmitConnectionWarnings(), EnablePortalManager(), ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg, FATAL, fb(), Form_pg_database, GetDatabasePath(), GetDatabaseTuple(), GetDatabaseTupleByOid(), GetSessionUserId(), GETSTRUCT(), GetUserId(), has_privs_of_role(), has_rolreplication(), HaveNFreeProcs(), hba_authname(), HeapTupleIsValid, HOLD_INTERRUPTS, IDLE_IN_TRANSACTION_SESSION_TIMEOUT, IDLE_SESSION_TIMEOUT, IDLE_STATS_UPDATE_TIMEOUT, IdleInTransactionSessionTimeoutHandler(), IdleSessionTimeoutHandler(), IdleStatsUpdateTimeoutHandler(), INIT_PG_LOAD_SESSION_LIBS, INIT_PG_OVERRIDE_ALLOW_CONNS, INIT_PG_OVERRIDE_ROLE_LOGIN, InitCatalogCache(), initialize_acl(), InitializeClientEncoding(), InitializeProcessXLogLogicalInfo(), InitializeSearchPath(), InitializeSession(), InitializeSessionUserId(), InitializeSessionUserIdStandalone(), InitializeSystemUser(), InitLocalDataChecksumState(), InitPlanCache(), InitProcessPhase2(), INJECTION_POINT, InvalidateCatalogSnapshot(), IsBinaryUpgrade, IsBootstrapProcessingMode, IsUnderPostmaster, LOCK_TIMEOUT, LockSharedObject(), LockTimeoutHandler(), MyCancelKey, MyCancelKeyLength, MyClientConnectionInfo, MyDatabaseHasLoginEventTriggers, MyDatabaseId, MyDatabaseTableSpace, MyProc, MyProcPort, NAMEDATALEN, NameStr, namestrcmp(), OidIsValid, PerformAuthentication(), pfree(), pg_usleep(), pgstat_before_server_shutdown(), pgstat_beinit(), pgstat_bestart_final(), pgstat_bestart_initial(), pgstat_bestart_security(), PostAuthDelay, process_session_preload_libraries(), process_settings(), process_startup_options(), ProcSignalInit(), RegisterTimeout(), RelationCacheInitialize(), RelationCacheInitializePhase2(), RelationCacheInitializePhase3(), ReleaseAuxProcessResources(), ReservedConnections, RESUME_INTERRUPTS, RowExclusiveLock, SetCurrentStatementStartTimestamp(), SetDatabasePath(), SharedInvalBackendInit(), ShutdownPostgres(), ShutdownXLOG(), StartTransactionCommand(), StartupXLOG(), STATEMENT_TIMEOUT, StatementTimeoutHandler(), strlcpy(), superuser(), SuperuserReservedConnections, ThereIsAtLeastOneRole(), TRANSACTION_TIMEOUT, TransactionTimeoutHandler(), username, ValidatePgVersion(), WARNING, XACT_READ_COMMITTED, and XactIsoLevel.

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), BackgroundWorkerInitializeConnection(), BackgroundWorkerInitializeConnectionByOid(), BootstrapModeMain(), PostgresMain(), and ReplSlotSyncWorkerMain().

◆ LockTimeoutHandler()

static void LockTimeoutHandler ( void  )
static

Definition at line 1430 of file postinit.c.

1431{
1432#ifdef HAVE_SETSID
1433 /* try to signal whole process group */
1435#endif
1437}
int MyProcPid
Definition globals.c:49
#define kill(pid, sig)
Definition win32_port.h:490

References fb(), kill, and MyProcPid.

Referenced by InitPostgres().

◆ PerformAuthentication()

static void PerformAuthentication ( Port port)
static

Definition at line 203 of file postinit.c.

204{
205 /* This should be set already, but let's make sure */
206 ClientAuthInProgress = true; /* limit visibility of log messages */
207
208 /*
209 * In EXEC_BACKEND case, we didn't inherit the contents of pg_hba.conf
210 * etcetera from the postmaster, and have to load them ourselves.
211 *
212 * FIXME: [fork/exec] Ugh. Is there a way around this overhead?
213 */
214#ifdef EXEC_BACKEND
215
216 /*
217 * load_hba() and load_ident() want to work within the PostmasterContext,
218 * so create that if it doesn't exist (which it won't). We'll delete it
219 * again later, in PostgresMain.
220 */
221 if (PostmasterContext == NULL)
223 "Postmaster",
225
226 if (!load_hba())
227 {
228 /*
229 * It makes no sense to continue if we fail to load the HBA file,
230 * since there is no way to connect to the database in this case.
231 */
233 /* translator: %s is a configuration file */
234 (errmsg("could not load %s", HbaFileName)));
235 }
236
237 if (!load_ident())
238 {
239 /*
240 * It is ok to continue if we fail to load the IDENT file, although it
241 * means that you cannot log in using any of the authentication
242 * methods that need a user name mapping. load_ident() already logged
243 * the details of error to the log.
244 */
245 }
246#endif
247
248 /* Capture authentication start time for logging */
250
251 /*
252 * Set up a timeout in case a buggy or malicious client fails to respond
253 * during authentication. Since we're inside a transaction and might do
254 * database access, we have to use the statement_timeout infrastructure.
255 */
257
258 /*
259 * Now perform authentication exchange.
260 */
261 set_ps_display("authentication");
262 ClientAuthentication(port); /* might not return, if failure */
263
264 /*
265 * Done with authentication. Disable the timeout, and log if needed.
266 */
268
269 /* Capture authentication end time for logging */
271
273 {
275
277 if (am_walsender)
278 appendStringInfo(&logmsg, _("replication connection authorized: user=%s"),
279 port->user_name);
280 else
281 appendStringInfo(&logmsg, _("connection authorized: user=%s"),
282 port->user_name);
283 if (!am_walsender)
284 appendStringInfo(&logmsg, _(" database=%s"), port->database_name);
285
286 if (port->application_name != NULL)
287 appendStringInfo(&logmsg, _(" application_name=%s"),
288 port->application_name);
289
290#ifdef USE_SSL
291 if (port->ssl_in_use)
292 appendStringInfo(&logmsg, _(" SSL enabled (protocol=%s, cipher=%s, bits=%d)"),
296#endif
297#ifdef ENABLE_GSS
298 if (port->gss)
299 {
300 const char *princ = be_gssapi_get_princ(port);
301
302 if (princ)
304 _(" GSS (authenticated=%s, encrypted=%s, delegated_credentials=%s, principal=%s)"),
305 be_gssapi_get_auth(port) ? _("yes") : _("no"),
306 be_gssapi_get_enc(port) ? _("yes") : _("no"),
307 be_gssapi_get_delegation(port) ? _("yes") : _("no"),
308 princ);
309 else
311 _(" GSS (authenticated=%s, encrypted=%s, delegated_credentials=%s)"),
312 be_gssapi_get_auth(port) ? _("yes") : _("no"),
313 be_gssapi_get_enc(port) ? _("yes") : _("no"),
314 be_gssapi_get_delegation(port) ? _("yes") : _("no"));
315 }
316#endif
317
318 ereport(LOG, errmsg_internal("%s", logmsg.data));
319 pfree(logmsg.data);
320 }
321
322 set_ps_display("startup");
323
324 ClientAuthInProgress = false; /* client_min_messages is active now */
325}
void ClientAuthentication(Port *port)
Definition auth.c:374
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1649
uint32 log_connections
ConnectionTiming conn_timing
@ LOG_CONNECTION_AUTHORIZATION
bool be_gssapi_get_auth(Port *port)
bool be_gssapi_get_enc(Port *port)
const char * be_gssapi_get_princ(Port *port)
bool be_gssapi_get_delegation(Port *port)
const char * be_tls_get_version(Port *port)
int be_tls_get_cipher_bits(Port *port)
const char * be_tls_get_cipher(Port *port)
#define _(x)
Definition elog.c:96
#define LOG
Definition elog.h:32
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
char * HbaFileName
Definition guc_tables.c:584
bool load_ident(void)
Definition hba.c:2846
bool load_hba(void)
Definition hba.c:2452
MemoryContext TopMemoryContext
Definition mcxt.c:167
MemoryContext PostmasterContext
Definition mcxt.c:169
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
static int port
Definition pg_regress.c:117
bool ClientAuthInProgress
Definition postmaster.c:374
int AuthenticationTimeout
Definition postmaster.c:242
static void set_ps_display(const char *activity)
Definition ps_status.h:40
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
TimestampTz auth_start
void enable_timeout_after(TimeoutId id, int delay_ms)
Definition timeout.c:560
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition timeout.c:685

References _, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, am_walsender, appendStringInfo(), ConnectionTiming::auth_end, ConnectionTiming::auth_start, AuthenticationTimeout, be_gssapi_get_auth(), be_gssapi_get_delegation(), be_gssapi_get_enc(), be_gssapi_get_princ(), be_tls_get_cipher(), be_tls_get_cipher_bits(), be_tls_get_version(), ClientAuthentication(), ClientAuthInProgress, conn_timing, disable_timeout(), enable_timeout_after(), ereport, errmsg, errmsg_internal(), FATAL, fb(), GetCurrentTimestamp(), HbaFileName, initStringInfo(), load_hba(), load_ident(), LOG, LOG_CONNECTION_AUTHORIZATION, log_connections, pfree(), port, PostmasterContext, set_ps_display(), STATEMENT_TIMEOUT, and TopMemoryContext.

Referenced by InitPostgres().

◆ pg_split_opts()

void pg_split_opts ( char **  argv,
int argcp,
const char optstr 
)

Definition at line 501 of file postinit.c.

502{
504
505 initStringInfo(&s);
506
507 while (*optstr)
508 {
509 bool last_was_escape = false;
510
511 resetStringInfo(&s);
512
513 /* skip over leading space */
514 while (isspace((unsigned char) *optstr))
515 optstr++;
516
517 if (*optstr == '\0')
518 break;
519
520 /*
521 * Parse a single option, stopping at the first space, unless it's
522 * escaped.
523 */
524 while (*optstr)
525 {
526 if (isspace((unsigned char) *optstr) && !last_was_escape)
527 break;
528
529 if (!last_was_escape && *optstr == '\\')
530 last_was_escape = true;
531 else
532 {
533 last_was_escape = false;
535 }
536
537 optstr++;
538 }
539
540 /* now store the option in the next argv[] position */
541 argv[(*argcp)++] = pstrdup(s.data);
542 }
543
544 pfree(s.data);
545}
char * pstrdup(const char *in)
Definition mcxt.c:1910
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242

References appendStringInfoChar(), StringInfoData::data, fb(), initStringInfo(), pfree(), pstrdup(), and resetStringInfo().

Referenced by process_startup_options().

◆ process_settings()

static void process_settings ( Oid  databaseid,
Oid  roleid 
)
static

Definition at line 1357 of file postinit.c.

1358{
1360 Snapshot snapshot;
1361
1362 if (!IsUnderPostmaster)
1363 return;
1364
1366
1367 /* read all the settings under the same snapshot for efficiency */
1369
1370 /* Later settings are ignored if set earlier. */
1372 ApplySetting(snapshot, InvalidOid, roleid, relsetting, PGC_S_USER);
1375
1376 UnregisterSnapshot(snapshot);
1378}
@ PGC_S_GLOBAL
Definition guc.h:118
@ PGC_S_DATABASE
Definition guc.h:119
@ PGC_S_DATABASE_USER
Definition guc.h:121
@ PGC_S_USER
Definition guc.h:120
void ApplySetting(Snapshot snapshot, Oid databaseid, Oid roleid, Relation relsetting, GucSource source)
#define InvalidOid
Snapshot GetCatalogSnapshot(Oid relid)
Definition snapmgr.c:385
void UnregisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:866
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:824

References AccessShareLock, ApplySetting(), fb(), GetCatalogSnapshot(), InvalidOid, IsUnderPostmaster, PGC_S_DATABASE, PGC_S_DATABASE_USER, PGC_S_GLOBAL, PGC_S_USER, RegisterSnapshot(), table_close(), table_open(), and UnregisterSnapshot().

Referenced by InitPostgres().

◆ process_startup_options()

static void process_startup_options ( Port port,
bool  am_superuser 
)
static

Definition at line 1292 of file postinit.c.

1293{
1296
1298
1299 /*
1300 * First process any command-line switches that were included in the
1301 * startup packet, if we are in a regular backend.
1302 */
1303 if (port->cmdline_options != NULL)
1304 {
1305 /*
1306 * The maximum possible number of commandline arguments that could
1307 * come from port->cmdline_options is (strlen + 1) / 2; see
1308 * pg_split_opts().
1309 */
1310 char **av;
1311 int maxac;
1312 int ac;
1313
1314 maxac = 2 + (strlen(port->cmdline_options) + 1) / 2;
1315
1316 av = palloc_array(char *, maxac);
1317 ac = 0;
1318
1319 av[ac++] = "postgres";
1320
1321 pg_split_opts(av, &ac, port->cmdline_options);
1322
1323 av[ac] = NULL;
1324
1325 Assert(ac < maxac);
1326
1328 }
1329
1330 /*
1331 * Process any additional GUC variable settings passed in startup packet.
1332 * These are handled exactly like command-line variables.
1333 */
1334 gucopts = list_head(port->guc_options);
1335 while (gucopts)
1336 {
1337 char *name;
1338 char *value;
1339
1340 name = lfirst(gucopts);
1341 gucopts = lnext(port->guc_options, gucopts);
1342
1343 value = lfirst(gucopts);
1344 gucopts = lnext(port->guc_options, gucopts);
1345
1347 }
1348}
#define palloc_array(type, count)
Definition fe_memutils.h:91
@ PGC_S_CLIENT
Definition guc.h:122
GucContext
Definition guc.h:72
@ PGC_SU_BACKEND
Definition guc.h:76
static struct @177 value
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
void process_postgres_switches(int argc, char *argv[], GucContext ctx, const char **dbname)
Definition postgres.c:3875
void pg_split_opts(char **argv, int *argcp, const char *optstr)
Definition postinit.c:501
struct @10::@11 av[32]

References Assert, av, fb(), lfirst, list_head(), lnext(), name, palloc_array, pg_split_opts(), PGC_BACKEND, PGC_S_CLIENT, PGC_SU_BACKEND, port, process_postgres_switches(), SetConfigOption(), and value.

Referenced by InitPostgres().

◆ ShutdownPostgres()

static void ShutdownPostgres ( int  code,
Datum  arg 
)
static

Definition at line 1391 of file postinit.c.

1392{
1393 /* Make sure we've killed any active transaction */
1395
1396 /*
1397 * User locks are not released by transaction end, so be sure to release
1398 * them explicitly.
1399 */
1401}
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition lock.c:2315
#define USER_LOCKMETHOD
Definition locktag.h:26
void AbortOutOfAnyTransaction(void)
Definition xact.c:4913

References AbortOutOfAnyTransaction(), LockReleaseAll(), and USER_LOCKMETHOD.

Referenced by InitPostgres().

◆ StatementTimeoutHandler()

static void StatementTimeoutHandler ( void  )
static

Definition at line 1408 of file postinit.c.

1409{
1410 int sig = SIGINT;
1411
1412 /*
1413 * During authentication the timeout is used to deal with
1414 * authentication_timeout - we want to quit in response to such timeouts.
1415 */
1417 sig = SIGTERM;
1418
1419#ifdef HAVE_SETSID
1420 /* try to signal whole process group */
1421 kill(-MyProcPid, sig);
1422#endif
1423 kill(MyProcPid, sig);
1424}
static int sig
Definition pg_ctl.c:81

References ClientAuthInProgress, fb(), kill, MyProcPid, and sig.

Referenced by InitPostgres().

◆ StoreConnectionWarning()

void StoreConnectionWarning ( char msg,
char detail 
)

Definition at line 1508 of file postinit.c.

1509{
1510 MemoryContext oldcontext;
1511
1512 Assert(msg);
1513 Assert(detail);
1514
1516 elog(ERROR, "StoreConnectionWarning() called after EmitConnectionWarnings()");
1517
1519
1522
1523 MemoryContextSwitchTo(oldcontext);
1524}
List * lappend(List *list, void *datum)
Definition list.c:339
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138

References Assert, ConnectionWarningDetails, ConnectionWarningMessages, ConnectionWarningsEmitted, elog, ERROR, lappend(), MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by get_role_password(), and md5_crypt_verify().

◆ ThereIsAtLeastOneRole()

static bool ThereIsAtLeastOneRole ( void  )
static

Definition at line 1483 of file postinit.c.

1484{
1486 TableScanDesc scan;
1487 bool result;
1488
1490
1493
1494 table_endscan(scan);
1496
1497 return result;
1498}
uint32 result
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1435
@ ForwardScanDirection
Definition sdir.h:28
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061

References AccessShareLock, fb(), ForwardScanDirection, heap_getnext(), result, table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by InitPostgres().

◆ TransactionTimeoutHandler()

static void TransactionTimeoutHandler ( void  )
static

Definition at line 1440 of file postinit.c.

1441{
1443 InterruptPending = true;
1445}
volatile sig_atomic_t TransactionTimeoutPending
Definition globals.c:38

References InterruptPending, MyLatch, SetLatch(), and TransactionTimeoutPending.

Referenced by InitPostgres().

Variable Documentation

◆ ConnectionWarningDetails

List* ConnectionWarningDetails
static

Definition at line 79 of file postinit.c.

Referenced by EmitConnectionWarnings(), and StoreConnectionWarning().

◆ ConnectionWarningMessages

List* ConnectionWarningMessages
static

Definition at line 78 of file postinit.c.

Referenced by EmitConnectionWarnings(), and StoreConnectionWarning().

◆ ConnectionWarningsEmitted

bool ConnectionWarningsEmitted
static

Definition at line 75 of file postinit.c.

Referenced by EmitConnectionWarnings(), and StoreConnectionWarning().