PostgreSQL Source Code  git master
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/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/pg_authid.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 "postmaster/autovacuum.h"
#include "postmaster/postmaster.h"
#include "replication/walsender.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/procarray.h"
#include "storage/procsignal.h"
#include "storage/proc.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
#include "storage/sync.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.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 InitCommunication (void)
 
static void ShutdownPostgres (int code, Datum arg)
 
static void StatementTimeoutHandler (void)
 
static void LockTimeoutHandler (void)
 
static void IdleInTransactionSessionTimeoutHandler (void)
 
static bool ThereIsAtLeastOneRole (void)
 
static void process_startup_options (Port *port, bool am_superuser)
 
static void process_settings (Oid databaseid, Oid roleid)
 
void pg_split_opts (char **argv, int *argcp, const char *optstr)
 
void InitializeMaxBackends (void)
 
void BaseInit (void)
 
void InitPostgres (const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
 

Function Documentation

◆ BaseInit()

void BaseInit ( void  )

Definition at line 550 of file postinit.c.

References DebugFileOpen(), InitBufferPoolAccess(), InitCommunication(), InitFileAccess(), InitSync(), and smgrinit().

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMain(), PostgresMain(), and StartBackgroundWorker().

551 {
552  /*
553  * Attach to shared memory and semaphores, and initialize our
554  * input/output/debugging file descriptors.
555  */
557  DebugFileOpen();
558 
559  /* Do local initialization of file, storage and buffer managers */
560  InitFileAccess();
561  InitSync();
562  smgrinit();
564 }
static void InitCommunication(void)
Definition: postinit.c:440
void InitBufferPoolAccess(void)
Definition: bufmgr.c:2445
void DebugFileOpen(void)
Definition: elog.c:1845
void smgrinit(void)
Definition: smgr.c:110
void InitSync(void)
Definition: sync.c:106
void InitFileAccess(void)
Definition: fd.c:786

◆ CheckMyDatabase()

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

Definition at line 317 of file postinit.c.

References ACL_CONNECT, ACLCHECK_OK, check_strxfrm_bug(), CountDBConnections(), DATABASEOID, elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, FATAL, GetDatabaseEncodingName(), GETSTRUCT, GetUserId(), HeapTupleIsValid, IsAutoVacuumWorkerProcess(), IsUnderPostmaster, MyDatabaseId, NameStr, ObjectIdGetDatum, pg_database_aclcheck(), pg_perm_setlocale(), PGC_BACKEND, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, ReleaseSysCache(), SearchSysCache1(), SetConfigOption(), and SetDatabaseEncoding().

Referenced by InitPostgres().

318 {
319  HeapTuple tup;
320  Form_pg_database dbform;
321  char *collate;
322  char *ctype;
323 
324  /* Fetch our pg_database row normally, via syscache */
326  if (!HeapTupleIsValid(tup))
327  elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
328  dbform = (Form_pg_database) GETSTRUCT(tup);
329 
330  /* This recheck is strictly paranoia */
331  if (strcmp(name, NameStr(dbform->datname)) != 0)
332  ereport(FATAL,
333  (errcode(ERRCODE_UNDEFINED_DATABASE),
334  errmsg("database \"%s\" has disappeared from pg_database",
335  name),
336  errdetail("Database OID %u now seems to belong to \"%s\".",
337  MyDatabaseId, NameStr(dbform->datname))));
338 
339  /*
340  * Check permissions to connect to the database.
341  *
342  * These checks are not enforced when in standalone mode, so that there is
343  * a way to recover from disabling all access to all databases, for
344  * example "UPDATE pg_database SET datallowconn = false;".
345  *
346  * We do not enforce them for autovacuum worker processes either.
347  */
349  {
350  /*
351  * Check that the database is currently allowing connections.
352  */
353  if (!dbform->datallowconn && !override_allow_connections)
354  ereport(FATAL,
355  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
356  errmsg("database \"%s\" is not currently accepting connections",
357  name)));
358 
359  /*
360  * Check privilege to connect to the database. (The am_superuser test
361  * is redundant, but since we have the flag, might as well check it
362  * and save a few cycles.)
363  */
364  if (!am_superuser &&
367  ereport(FATAL,
368  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
369  errmsg("permission denied for database \"%s\"", name),
370  errdetail("User does not have CONNECT privilege.")));
371 
372  /*
373  * Check connection limit for this database.
374  *
375  * There is a race condition here --- we create our PGPROC before
376  * checking for other PGPROCs. If two backends did this at about the
377  * same time, they might both think they were over the limit, while
378  * ideally one should succeed and one fail. Getting that to work
379  * exactly seems more trouble than it is worth, however; instead we
380  * just document that the connection limit is approximate.
381  */
382  if (dbform->datconnlimit >= 0 &&
383  !am_superuser &&
384  CountDBConnections(MyDatabaseId) > dbform->datconnlimit)
385  ereport(FATAL,
386  (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
387  errmsg("too many connections for database \"%s\"",
388  name)));
389  }
390 
391  /*
392  * OK, we're golden. Next to-do item is to save the encoding info out of
393  * the pg_database tuple.
394  */
395  SetDatabaseEncoding(dbform->encoding);
396  /* Record it as a GUC internal option, too */
397  SetConfigOption("server_encoding", GetDatabaseEncodingName(),
399  /* If we have no other source of client_encoding, use server encoding */
400  SetConfigOption("client_encoding", GetDatabaseEncodingName(),
402 
403  /* assign locale variables */
404  collate = NameStr(dbform->datcollate);
405  ctype = NameStr(dbform->datctype);
406 
407  if (pg_perm_setlocale(LC_COLLATE, collate) == NULL)
408  ereport(FATAL,
409  (errmsg("database locale is incompatible with operating system"),
410  errdetail("The database was initialized with LC_COLLATE \"%s\", "
411  " which is not recognized by setlocale().", collate),
412  errhint("Recreate the database with another locale or install the missing locale.")));
413 
414  if (pg_perm_setlocale(LC_CTYPE, ctype) == NULL)
415  ereport(FATAL,
416  (errmsg("database locale is incompatible with operating system"),
417  errdetail("The database was initialized with LC_CTYPE \"%s\", "
418  " which is not recognized by setlocale().", ctype),
419  errhint("Recreate the database with another locale or install the missing locale.")));
420 
421  /* Make the locale settings visible as GUC variables, too */
422  SetConfigOption("lc_collate", collate, PGC_INTERNAL, PGC_S_OVERRIDE);
423  SetConfigOption("lc_ctype", ctype, PGC_INTERNAL, PGC_S_OVERRIDE);
424 
426 
427  ReleaseSysCache(tup);
428 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Oid GetUserId(void)
Definition: miscinit.c:380
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
int errcode(int sqlerrcode)
Definition: elog.c:570
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:156
int CountDBConnections(Oid databaseid)
Definition: procarray.c:2786
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
void check_strxfrm_bug(void)
Definition: pg_locale.c:1056
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7487
bool IsUnderPostmaster
Definition: globals.c:109
int errdetail(const char *fmt,...)
Definition: elog.c:860
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3278
#define ereport(elevel, rest)
Definition: elog.h:141
#define ACL_CONNECT
Definition: parsenodes.h:86
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void SetDatabaseEncoding(int encoding)
Definition: mbutils.c:890
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Oid MyDatabaseId
Definition: globals.c:85
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4643
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1002
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609

◆ GetDatabaseTuple()

static HeapTuple GetDatabaseTuple ( const char *  dbname)
static

Definition at line 97 of file postinit.c.

References AccessShareLock, BTEqualStrategyNumber, criticalSharedRelcachesBuilt, CStringGetDatum, DatabaseNameIndexId, heap_copytuple(), HeapTupleIsValid, sort-test::key, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by InitPostgres().

98 {
99  HeapTuple tuple;
100  Relation relation;
101  SysScanDesc scan;
102  ScanKeyData key[1];
103 
104  /*
105  * form a scan key
106  */
107  ScanKeyInit(&key[0],
108  Anum_pg_database_datname,
109  BTEqualStrategyNumber, F_NAMEEQ,
111 
112  /*
113  * Open pg_database and fetch a tuple. Force heap scan if we haven't yet
114  * built the critical shared relcache entries (i.e., we're starting up
115  * without a shared relcache cache file).
116  */
117  relation = table_open(DatabaseRelationId, AccessShareLock);
118  scan = systable_beginscan(relation, DatabaseNameIndexId,
120  NULL,
121  1, key);
122 
123  tuple = systable_getnext(scan);
124 
125  /* Must copy tuple before releasing buffer */
126  if (HeapTupleIsValid(tuple))
127  tuple = heap_copytuple(tuple);
128 
129  /* all done */
130  systable_endscan(scan);
131  table_close(relation, AccessShareLock);
132 
133  return tuple;
134 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define AccessShareLock
Definition: lockdefs.h:36
bool criticalSharedRelcachesBuilt
Definition: relcache.c:147
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define CStringGetDatum(X)
Definition: postgres.h:578
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
char * dbname
Definition: streamutil.c:52
#define DatabaseNameIndexId
Definition: indexing.h:144
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetDatabaseTupleByOid()

static HeapTuple GetDatabaseTupleByOid ( Oid  dboid)
static

Definition at line 140 of file postinit.c.

References AccessShareLock, BTEqualStrategyNumber, criticalSharedRelcachesBuilt, DatabaseOidIndexId, heap_copytuple(), HeapTupleIsValid, sort-test::key, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by InitPostgres().

141 {
142  HeapTuple tuple;
143  Relation relation;
144  SysScanDesc scan;
145  ScanKeyData key[1];
146 
147  /*
148  * form a scan key
149  */
150  ScanKeyInit(&key[0],
151  Anum_pg_database_oid,
152  BTEqualStrategyNumber, F_OIDEQ,
153  ObjectIdGetDatum(dboid));
154 
155  /*
156  * Open pg_database and fetch a tuple. Force heap scan if we haven't yet
157  * built the critical shared relcache entries (i.e., we're starting up
158  * without a shared relcache cache file).
159  */
160  relation = table_open(DatabaseRelationId, AccessShareLock);
161  scan = systable_beginscan(relation, DatabaseOidIndexId,
163  NULL,
164  1, key);
165 
166  tuple = systable_getnext(scan);
167 
168  /* Must copy tuple before releasing buffer */
169  if (HeapTupleIsValid(tuple))
170  tuple = heap_copytuple(tuple);
171 
172  /* all done */
173  systable_endscan(scan);
174  table_close(relation, AccessShareLock);
175 
176  return tuple;
177 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define AccessShareLock
Definition: lockdefs.h:36
bool criticalSharedRelcachesBuilt
Definition: relcache.c:147
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define DatabaseOidIndexId
Definition: indexing.h:146
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ IdleInTransactionSessionTimeoutHandler()

static void IdleInTransactionSessionTimeoutHandler ( void  )
static

Definition at line 1239 of file postinit.c.

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

Referenced by InitPostgres().

1240 {
1242  InterruptPending = true;
1243  SetLatch(MyLatch);
1244 }
void SetLatch(Latch *latch)
Definition: latch.c:436
volatile sig_atomic_t IdleInTransactionSessionTimeoutPending
Definition: globals.c:34
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
struct Latch * MyLatch
Definition: globals.c:54

◆ InitCommunication()

static void InitCommunication ( void  )
static

Definition at line 440 of file postinit.c.

References CreateSharedMemoryAndSemaphores(), IsUnderPostmaster, and PostPortNumber.

Referenced by BaseInit().

441 {
442  /*
443  * initialize shared memory and semaphores appropriately.
444  */
445  if (!IsUnderPostmaster) /* postmaster already did this */
446  {
447  /*
448  * We're running a postgres bootstrap process or a standalone backend.
449  * Though we won't listen on PostPortNumber, use it to select a shmem
450  * key. This increases the chance of detecting a leftover live
451  * backend of this DataDir.
452  */
454  }
455 }
void CreateSharedMemoryAndSemaphores(int port)
Definition: ipci.c:94
int PostPortNumber
Definition: postmaster.c:197
bool IsUnderPostmaster
Definition: globals.c:109

◆ InitializeMaxBackends()

void InitializeMaxBackends ( void  )

Definition at line 528 of file postinit.c.

References Assert, autovacuum_max_workers, elog, ERROR, MAX_BACKENDS, max_wal_senders, max_worker_processes, MaxBackends, and MaxConnections.

Referenced by AuxiliaryProcessMain(), PostgresMain(), and PostmasterMain().

529 {
530  Assert(MaxBackends == 0);
531 
532  /* the extra unit accounts for the autovacuum launcher */
535 
536  /* internal error because the values were all checked previously */
538  elog(ERROR, "too many backends configured");
539 }
#define ERROR
Definition: elog.h:43
#define MAX_BACKENDS
Definition: postmaster.h:75
int MaxBackends
Definition: globals.c:135
int max_wal_senders
Definition: walsender.c:120
int MaxConnections
Definition: globals.c:132
int autovacuum_max_workers
Definition: autovacuum.c:114
#define Assert(condition)
Definition: c.h:732
#define elog(elevel,...)
Definition: elog.h:226
int max_worker_processes
Definition: globals.c:133

◆ InitPostgres()

void InitPostgres ( const char *  in_dbname,
Oid  dboid,
const char *  username,
Oid  useroid,
char *  out_dbname,
bool  override_allow_connections 
)

Definition at line 592 of file postinit.c.

References am_db_walsender, am_walsender, Assert, before_shmem_exit(), CAC_WAITBACKUP, Port::canAcceptConnections, CheckDeadLockAlert(), CheckMyDatabase(), CommitTransactionCommand(), CreateAuxProcessResourceOwner(), CurrentResourceOwner, PGPROC::databaseId, dbname, DEADLOCK_TIMEOUT, DEBUG3, elog, EnablePortalManager(), ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg(), FATAL, GetDatabasePath(), GetDatabaseTuple(), GetDatabaseTupleByOid(), GetSessionUserId(), GETSTRUCT, GetTransactionSnapshot(), GetUserId(), has_rolreplication(), HaveNFreeProcs(), HeapTupleIsValid, IDLE_IN_TRANSACTION_SESSION_TIMEOUT, IdleInTransactionSessionTimeoutHandler(), InitBufferPoolBackend(), InitCatalogCache(), initialize_acl(), InitializeClientEncoding(), InitializeSearchPath(), InitializeSession(), InitializeSessionUserId(), InitializeSessionUserIdStandalone(), InitPlanCache(), InitProcessPhase2(), InvalidateCatalogSnapshot(), InvalidBackendId, IsAutoVacuumLauncherProcess(), IsAutoVacuumWorkerProcess(), IsBackgroundWorker, IsBinaryUpgrade, IsBootstrapProcessingMode, IsUnderPostmaster, LOCK_TIMEOUT, LockSharedObject(), LockTimeoutHandler(), MaxBackends, MyBackendId, MyDatabaseId, MyDatabaseTableSpace, MyProc, MyProcPort, NAMEDATALEN, NameStr, OidIsValid, on_shmem_exit(), PerformAuthentication(), pg_usleep(), pgstat_bestart(), pgstat_initialize(), PostAuthDelay, process_settings(), process_startup_options(), ProcSignalInit(), RecoveryInProgress(), RegisterTimeout(), RelationCacheInitialize(), RelationCacheInitializePhase2(), RelationCacheInitializePhase3(), ReleaseAuxProcessResources(), ReservedBackends, RowExclusiveLock, SetCurrentStatementStartTimestamp(), SetDatabasePath(), SharedInvalBackendInit(), ShutdownPostgres(), ShutdownXLOG(), StartTransactionCommand(), StartupXLOG(), STATEMENT_TIMEOUT, StatementTimeoutHandler(), strlcpy(), superuser(), ThereIsAtLeastOneRole(), ValidatePgVersion(), WARNING, XACT_READ_COMMITTED, and XactIsoLevel.

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

594 {
595  bool bootstrap = IsBootstrapProcessingMode();
596  bool am_superuser;
597  char *fullpath;
598  char dbname[NAMEDATALEN];
599 
600  elog(DEBUG3, "InitPostgres");
601 
602  /*
603  * Add my PGPROC struct to the ProcArray.
604  *
605  * Once I have done this, I am visible to other backends!
606  */
608 
609  /*
610  * Initialize my entry in the shared-invalidation manager's array of
611  * per-backend data.
612  *
613  * Sets up MyBackendId, a unique backend identifier.
614  */
616 
617  SharedInvalBackendInit(false);
618 
619  if (MyBackendId > MaxBackends || MyBackendId <= 0)
620  elog(FATAL, "bad backend ID: %d", MyBackendId);
621 
622  /* Now that we have a BackendId, we can participate in ProcSignal */
624 
625  /*
626  * Also set up timeout handlers needed for backend operation. We need
627  * these in every case except bootstrap.
628  */
629  if (!bootstrap)
630  {
636  }
637 
638  /*
639  * bufmgr needs another initialization call too
640  */
642 
643  /*
644  * Initialize local process's access to XLOG.
645  */
646  if (IsUnderPostmaster)
647  {
648  /*
649  * The postmaster already started the XLOG machinery, but we need to
650  * call InitXLOGAccess(), if the system isn't in hot-standby mode.
651  * This is handled by calling RecoveryInProgress and ignoring the
652  * result.
653  */
654  (void) RecoveryInProgress();
655  }
656  else
657  {
658  /*
659  * We are either a bootstrap process or a standalone backend. Either
660  * way, start up the XLOG machinery, and register to have it closed
661  * down at exit.
662  *
663  * We don't yet have an aux-process resource owner, but StartupXLOG
664  * and ShutdownXLOG will need one. Hence, create said resource owner
665  * (and register a callback to clean it up after ShutdownXLOG runs).
666  */
668 
669  StartupXLOG();
670  /* Release (and warn about) any buffer pins leaked in StartupXLOG */
672  /* Reset CurrentResourceOwner to nothing for the moment */
673  CurrentResourceOwner = NULL;
674 
676  }
677 
678  /*
679  * Initialize the relation cache and the system catalog caches. Note that
680  * no catalog access happens here; we only set up the hashtable structure.
681  * We must do this before starting a transaction because transaction abort
682  * would try to touch these hashtables.
683  */
686  InitPlanCache();
687 
688  /* Initialize portal manager */
690 
691  /* Initialize stats collection --- must happen before first xact */
692  if (!bootstrap)
694 
695  /*
696  * Load relcache entries for the shared system catalogs. This must create
697  * at least entries for pg_database and catalogs used for authentication.
698  */
700 
701  /*
702  * Set up process-exit callback to do pre-shutdown cleanup. This is the
703  * first before_shmem_exit callback we register; thus, this will be the
704  * last thing we do before low-level modules like the buffer manager begin
705  * to close down. We need to have this in place before we begin our first
706  * transaction --- if we fail during the initialization transaction, as is
707  * entirely possible, we need the AbortTransaction call to clean up.
708  */
710 
711  /* The autovacuum launcher is done here */
713  {
714  /* report this backend in the PgBackendStatus array */
715  pgstat_bestart();
716 
717  return;
718  }
719 
720  /*
721  * Start a new transaction here before first access to db, and get a
722  * snapshot. We don't have a use for the snapshot itself, but we're
723  * interested in the secondary effect that it sets RecentGlobalXmin. (This
724  * is critical for anything that reads heap pages, because HOT may decide
725  * to prune them even if the process doesn't attempt to modify any
726  * tuples.)
727  */
728  if (!bootstrap)
729  {
730  /* statement_timestamp must be set for timeouts to work correctly */
733 
734  /*
735  * transaction_isolation will have been set to the default by the
736  * above. If the default is "serializable", and we are in hot
737  * standby, we will fail if we don't change it to something lower.
738  * Fortunately, "read committed" is plenty good enough.
739  */
741 
742  (void) GetTransactionSnapshot();
743  }
744 
745  /*
746  * Perform client authentication if necessary, then figure out our
747  * postgres user ID, and see if we are a superuser.
748  *
749  * In standalone mode and in autovacuum worker processes, we use a fixed
750  * ID, otherwise we figure it out from the authenticated user name.
751  */
752  if (bootstrap || IsAutoVacuumWorkerProcess())
753  {
755  am_superuser = true;
756  }
757  else if (!IsUnderPostmaster)
758  {
760  am_superuser = true;
761  if (!ThereIsAtLeastOneRole())
763  (errcode(ERRCODE_UNDEFINED_OBJECT),
764  errmsg("no roles are defined in this database system"),
765  errhint("You should immediately run CREATE USER \"%s\" SUPERUSER;.",
766  username != NULL ? username : "postgres")));
767  }
768  else if (IsBackgroundWorker)
769  {
770  if (username == NULL && !OidIsValid(useroid))
771  {
773  am_superuser = true;
774  }
775  else
776  {
778  am_superuser = superuser();
779  }
780  }
781  else
782  {
783  /* normal multiuser case */
784  Assert(MyProcPort != NULL);
787  am_superuser = superuser();
788  }
789 
790  /*
791  * If we're trying to shut down, only superusers can connect, and new
792  * replication connections are not allowed.
793  */
794  if ((!am_superuser || am_walsender) &&
795  MyProcPort != NULL &&
797  {
798  if (am_walsender)
799  ereport(FATAL,
800  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
801  errmsg("new replication connections are not allowed during database shutdown")));
802  else
803  ereport(FATAL,
804  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
805  errmsg("must be superuser to connect during database shutdown")));
806  }
807 
808  /*
809  * Binary upgrades only allowed super-user connections
810  */
811  if (IsBinaryUpgrade && !am_superuser)
812  {
813  ereport(FATAL,
814  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
815  errmsg("must be superuser to connect in binary upgrade mode")));
816  }
817 
818  /*
819  * The last few connection slots are reserved for superusers. Replication
820  * connections are drawn from slots reserved with max_wal_senders and not
821  * limited by max_connections or superuser_reserved_connections.
822  */
823  if (!am_superuser && !am_walsender &&
824  ReservedBackends > 0 &&
826  ereport(FATAL,
827  (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
828  errmsg("remaining connection slots are reserved for non-replication superuser connections")));
829 
830  /* Check replication permissions needed for walsender processes. */
831  if (am_walsender)
832  {
833  Assert(!bootstrap);
834 
835  if (!superuser() && !has_rolreplication(GetUserId()))
836  ereport(FATAL,
837  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
838  errmsg("must be superuser or replication role to start walsender")));
839  }
840 
841  /*
842  * If this is a plain walsender only supporting physical replication, we
843  * don't want to connect to any particular database. Just finish the
844  * backend startup by processing any options from the startup packet, and
845  * we're done.
846  */
848  {
849  /* process any options passed in the startup packet */
850  if (MyProcPort != NULL)
851  process_startup_options(MyProcPort, am_superuser);
852 
853  /* Apply PostAuthDelay as soon as we've read all options */
854  if (PostAuthDelay > 0)
855  pg_usleep(PostAuthDelay * 1000000L);
856 
857  /* initialize client encoding */
859 
860  /* report this backend in the PgBackendStatus array */
861  pgstat_bestart();
862 
863  /* close the transaction we started above */
865 
866  return;
867  }
868 
869  /*
870  * Set up the global variables holding database id and default tablespace.
871  * But note we won't actually try to touch the database just yet.
872  *
873  * We take a shortcut in the bootstrap case, otherwise we have to look up
874  * the db's entry in pg_database.
875  */
876  if (bootstrap)
877  {
878  MyDatabaseId = TemplateDbOid;
879  MyDatabaseTableSpace = DEFAULTTABLESPACE_OID;
880  }
881  else if (in_dbname != NULL)
882  {
883  HeapTuple tuple;
884  Form_pg_database dbform;
885 
886  tuple = GetDatabaseTuple(in_dbname);
887  if (!HeapTupleIsValid(tuple))
888  ereport(FATAL,
889  (errcode(ERRCODE_UNDEFINED_DATABASE),
890  errmsg("database \"%s\" does not exist", in_dbname)));
891  dbform = (Form_pg_database) GETSTRUCT(tuple);
892  MyDatabaseId = dbform->oid;
893  MyDatabaseTableSpace = dbform->dattablespace;
894  /* take database name from the caller, just for paranoia */
895  strlcpy(dbname, in_dbname, sizeof(dbname));
896  }
897  else if (OidIsValid(dboid))
898  {
899  /* caller specified database by OID */
900  HeapTuple tuple;
901  Form_pg_database dbform;
902 
903  tuple = GetDatabaseTupleByOid(dboid);
904  if (!HeapTupleIsValid(tuple))
905  ereport(FATAL,
906  (errcode(ERRCODE_UNDEFINED_DATABASE),
907  errmsg("database %u does not exist", dboid)));
908  dbform = (Form_pg_database) GETSTRUCT(tuple);
909  MyDatabaseId = dbform->oid;
910  MyDatabaseTableSpace = dbform->dattablespace;
911  Assert(MyDatabaseId == dboid);
912  strlcpy(dbname, NameStr(dbform->datname), sizeof(dbname));
913  /* pass the database name back to the caller */
914  if (out_dbname)
915  strcpy(out_dbname, dbname);
916  }
917  else
918  {
919  /*
920  * If this is a background worker not bound to any particular
921  * database, we're done now. Everything that follows only makes sense
922  * if we are bound to a specific database. We do need to close the
923  * transaction we started before returning.
924  */
925  if (!bootstrap)
926  {
927  pgstat_bestart();
929  }
930  return;
931  }
932 
933  /*
934  * Now, take a writer's lock on the database we are trying to connect to.
935  * If there is a concurrently running DROP DATABASE on that database, this
936  * will block us until it finishes (and has committed its update of
937  * pg_database).
938  *
939  * Note that the lock is not held long, only until the end of this startup
940  * transaction. This is OK since we will advertise our use of the
941  * database in the ProcArray before dropping the lock (in fact, that's the
942  * next thing to do). Anyone trying a DROP DATABASE after this point will
943  * see us in the array once they have the lock. Ordering is important for
944  * this because we don't want to advertise ourselves as being in this
945  * database until we have the lock; otherwise we create what amounts to a
946  * deadlock with CountOtherDBBackends().
947  *
948  * Note: use of RowExclusiveLock here is reasonable because we envision
949  * our session as being a concurrent writer of the database. If we had a
950  * way of declaring a session as being guaranteed-read-only, we could use
951  * AccessShareLock for such sessions and thereby not conflict against
952  * CREATE DATABASE.
953  */
954  if (!bootstrap)
955  LockSharedObject(DatabaseRelationId, MyDatabaseId, 0,
957 
958  /*
959  * Now we can mark our PGPROC entry with the database ID.
960  *
961  * We assume this is an atomic store so no lock is needed; though actually
962  * things would work fine even if it weren't atomic. Anyone searching the
963  * ProcArray for this database's ID should hold the database lock, so they
964  * would not be executing concurrently with this store. A process looking
965  * for another database's ID could in theory see a chance match if it read
966  * a partially-updated databaseId value; but as long as all such searches
967  * wait and retry, as in CountOtherDBBackends(), they will certainly see
968  * the correct value on their next try.
969  */
971 
972  /*
973  * We established a catalog snapshot while reading pg_authid and/or
974  * pg_database; but until we have set up MyDatabaseId, we won't react to
975  * incoming sinval messages for unshared catalogs, so we won't realize it
976  * if the snapshot has been invalidated. Assume it's no good anymore.
977  */
979 
980  /*
981  * Recheck pg_database to make sure the target database hasn't gone away.
982  * If there was a concurrent DROP DATABASE, this ensures we will die
983  * cleanly without creating a mess.
984  */
985  if (!bootstrap)
986  {
987  HeapTuple tuple;
988 
989  tuple = GetDatabaseTuple(dbname);
990  if (!HeapTupleIsValid(tuple) ||
991  MyDatabaseId != ((Form_pg_database) GETSTRUCT(tuple))->oid ||
992  MyDatabaseTableSpace != ((Form_pg_database) GETSTRUCT(tuple))->dattablespace)
993  ereport(FATAL,
994  (errcode(ERRCODE_UNDEFINED_DATABASE),
995  errmsg("database \"%s\" does not exist", dbname),
996  errdetail("It seems to have just been dropped or renamed.")));
997  }
998 
999  /*
1000  * Now we should be able to access the database directory safely. Verify
1001  * it's there and looks reasonable.
1002  */
1004 
1005  if (!bootstrap)
1006  {
1007  if (access(fullpath, F_OK) == -1)
1008  {
1009  if (errno == ENOENT)
1010  ereport(FATAL,
1011  (errcode(ERRCODE_UNDEFINED_DATABASE),
1012  errmsg("database \"%s\" does not exist",
1013  dbname),
1014  errdetail("The database subdirectory \"%s\" is missing.",
1015  fullpath)));
1016  else
1017  ereport(FATAL,
1019  errmsg("could not access directory \"%s\": %m",
1020  fullpath)));
1021  }
1022 
1023  ValidatePgVersion(fullpath);
1024  }
1025 
1026  SetDatabasePath(fullpath);
1027 
1028  /*
1029  * It's now possible to do real access to the system catalogs.
1030  *
1031  * Load relcache entries for the system catalogs. This must create at
1032  * least the minimum set of "nailed-in" cache entries.
1033  */
1035 
1036  /* set up ACL framework (so CheckMyDatabase can check permissions) */
1037  initialize_acl();
1038 
1039  /*
1040  * Re-read the pg_database row for our database, check permissions and set
1041  * up database-specific GUC settings. We can't do this until all the
1042  * database-access infrastructure is up. (Also, it wants to know if the
1043  * user is a superuser, so the above stuff has to happen first.)
1044  */
1045  if (!bootstrap)
1046  CheckMyDatabase(dbname, am_superuser, override_allow_connections);
1047 
1048  /*
1049  * Now process any command-line switches and any additional GUC variable
1050  * settings passed in the startup packet. We couldn't do this before
1051  * because we didn't know if client is a superuser.
1052  */
1053  if (MyProcPort != NULL)
1054  process_startup_options(MyProcPort, am_superuser);
1055 
1056  /* Process pg_db_role_setting options */
1058 
1059  /* Apply PostAuthDelay as soon as we've read all options */
1060  if (PostAuthDelay > 0)
1061  pg_usleep(PostAuthDelay * 1000000L);
1062 
1063  /*
1064  * Initialize various default states that can't be set up until we've
1065  * selected the active user and gotten the right GUC settings.
1066  */
1067 
1068  /* set default namespace search path */
1070 
1071  /* initialize client encoding */
1073 
1074  /* Initialize this backend's session state. */
1076 
1077  /* report this backend in the PgBackendStatus array */
1078  if (!bootstrap)
1079  pgstat_bestart();
1080 
1081  /* close the transaction we started above */
1082  if (!bootstrap)
1084 }
int errhint(const char *fmt,...)
Definition: elog.c:974
BackendId MyBackendId
Definition: globals.c:81
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
struct Port * MyProcPort
Definition: globals.c:43
CAC_state canAcceptConnections
Definition: libpq-be.h:133
void SharedInvalBackendInit(bool sendOnly)
Definition: sinvaladt.c:258
#define DEBUG3
Definition: elog.h:23
Oid GetUserId(void)
Definition: miscinit.c:380
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
PGPROC * MyProc
Definition: proc.c:68
static HeapTuple GetDatabaseTuple(const char *dbname)
Definition: postinit.c:97
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void CommitTransactionCommand(void)
Definition: xact.c:2895
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:855
void InitializeSession(void)
Definition: session.c:54
void ValidatePgVersion(const char *path)
Definition: miscinit.c:1454
void RelationCacheInitializePhase2(void)
Definition: relcache.c:3617
static void PerformAuthentication(Port *port)
Definition: postinit.c:186
static void LockTimeoutHandler(void)
Definition: postinit.c:1229
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
void initialize_acl(void)
Definition: acl.c:4689
bool RecoveryInProgress(void)
Definition: xlog.c:7898
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
#define OidIsValid(objectId)
Definition: c.h:638
Oid GetSessionUserId(void)
Definition: miscinit.c:414
bool HaveNFreeProcs(int n)
Definition: proc.c:672
static void CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections)
Definition: postinit.c:317
bool IsBinaryUpgrade
Definition: globals.c:110
Oid MyDatabaseTableSpace
Definition: globals.c:87
bool IsBackgroundWorker
Definition: globals.c:111
void RelationCacheInitializePhase3(void)
Definition: relcache.c:3676
#define NAMEDATALEN
bool am_walsender
Definition: walsender.c:114
void pg_usleep(long microsec)
Definition: signal.c:53
int ReservedBackends
Definition: postmaster.c:214
#define FATAL
Definition: elog.h:52
void InitializeSearchPath(void)
Definition: namespace.c:4247
int MaxBackends
Definition: globals.c:135
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
bool IsUnderPostmaster
Definition: globals.c:109
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:860
int errcode_for_file_access(void)
Definition: elog.c:593
static void IdleInTransactionSessionTimeoutHandler(void)
Definition: postinit.c:1239
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
Oid databaseId
Definition: proc.h:114
void pgstat_initialize(void)
Definition: pgstat.c:2815
static bool ThereIsAtLeastOneRole(void)
Definition: postinit.c:1250
void StartupXLOG(void)
Definition: xlog.c:6195
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3278
#define ereport(elevel, rest)
Definition: elog.h:141
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:512
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:107
void SetDatabasePath(const char *path)
Definition: miscinit.c:86
#define WARNING
Definition: elog.h:40
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:8294
static HeapTuple GetDatabaseTupleByOid(Oid dboid)
Definition: postinit.c:140
#define InvalidBackendId
Definition: backendid.h:23
bool am_db_walsender
Definition: walsender.c:117
Oid MyDatabaseId
Definition: globals.c:85
int PostAuthDelay
Definition: postgres.c:101
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1004
void EnablePortalManager(void)
Definition: portalmem.c:104
static void ShutdownPostgres(int code, Datum arg)
Definition: postinit.c:1190
static char * username
Definition: initdb.c:133
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:373
bool IsAutoVacuumLauncherProcess(void)
Definition: autovacuum.c:3272
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
void InitializeSessionUserIdStandalone(void)
Definition: miscinit.c:682
void StartTransactionCommand(void)
Definition: xact.c:2794
void CreateAuxProcessResourceOwner(void)
Definition: resowner.c:834
#define XACT_READ_COMMITTED
Definition: xact.h:37
void InitializeSessionUserId(const char *rolename, Oid roleid)
Definition: miscinit.c:579
char * dbname
Definition: streamutil.c:52
static void process_startup_options(Port *port, bool am_superuser)
Definition: postinit.c:1091
int XactIsoLevel
Definition: xact.c:75
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:561
void InitializeClientEncoding(void)
Definition: mbutils.c:274
void InitPlanCache(void)
Definition: plancache.c:127
void InitProcessPhase2(void)
Definition: proc.c:481
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374
void RelationCacheInitialize(void)
Definition: relcache.c:3580
void ProcSignalInit(int pss_idx)
Definition: procsignal.c:105
void InitCatalogCache(void)
Definition: syscache.c:1010
static void StatementTimeoutHandler(void)
Definition: postinit.c:1207
void SetCurrentStatementStartTimestamp(void)
Definition: xact.c:818
int errmsg(const char *fmt,...)
Definition: elog.c:784
void pgstat_bestart(void)
Definition: pgstat.c:2857
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
static void process_settings(Oid databaseid, Oid roleid)
Definition: postinit.c:1156
void CheckDeadLockAlert(void)
Definition: proc.c:1776
void InitBufferPoolBackend(void)
Definition: bufmgr.c:2469

◆ LockTimeoutHandler()

static void LockTimeoutHandler ( void  )
static

Definition at line 1229 of file postinit.c.

References kill, and MyProcPid.

Referenced by InitPostgres().

1230 {
1231 #ifdef HAVE_SETSID
1232  /* try to signal whole process group */
1233  kill(-MyProcPid, SIGINT);
1234 #endif
1235  kill(MyProcPid, SIGINT);
1236 }
int MyProcPid
Definition: globals.c:40
#define kill(pid, sig)
Definition: win32_port.h:435

◆ PerformAuthentication()

static void PerformAuthentication ( Port port)
static

Definition at line 186 of file postinit.c.

References _, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, am_walsender, Port::application_name, AuthenticationTimeout, be_tls_get_cipher(), be_tls_get_cipher_bits(), be_tls_get_compression(), be_tls_get_version(), ClientAuthentication(), ClientAuthInProgress, Port::database_name, disable_timeout(), enable_timeout_after(), ereport, errmsg(), FATAL, load_hba(), load_ident(), LOG, Log_connections, PostmasterContext, set_ps_display(), Port::ssl_in_use, STATEMENT_TIMEOUT, TopMemoryContext, and Port::user_name.

Referenced by InitPostgres().

187 {
188  /* This should be set already, but let's make sure */
189  ClientAuthInProgress = true; /* limit visibility of log messages */
190 
191  /*
192  * In EXEC_BACKEND case, we didn't inherit the contents of pg_hba.conf
193  * etcetera from the postmaster, and have to load them ourselves.
194  *
195  * FIXME: [fork/exec] Ugh. Is there a way around this overhead?
196  */
197 #ifdef EXEC_BACKEND
198 
199  /*
200  * load_hba() and load_ident() want to work within the PostmasterContext,
201  * so create that if it doesn't exist (which it won't). We'll delete it
202  * again later, in PostgresMain.
203  */
204  if (PostmasterContext == NULL)
206  "Postmaster",
208 
209  if (!load_hba())
210  {
211  /*
212  * It makes no sense to continue if we fail to load the HBA file,
213  * since there is no way to connect to the database in this case.
214  */
215  ereport(FATAL,
216  (errmsg("could not load pg_hba.conf")));
217  }
218 
219  if (!load_ident())
220  {
221  /*
222  * It is ok to continue if we fail to load the IDENT file, although it
223  * means that you cannot log in using any of the authentication
224  * methods that need a user name mapping. load_ident() already logged
225  * the details of error to the log.
226  */
227  }
228 #endif
229 
230  /*
231  * Set up a timeout in case a buggy or malicious client fails to respond
232  * during authentication. Since we're inside a transaction and might do
233  * database access, we have to use the statement_timeout infrastructure.
234  */
236 
237  /*
238  * Now perform authentication exchange.
239  */
240  ClientAuthentication(port); /* might not return, if failure */
241 
242  /*
243  * Done with authentication. Disable the timeout, and log if needed.
244  */
246 
247  if (Log_connections)
248  {
249  if (am_walsender)
250  {
251 #ifdef USE_SSL
252  if (port->ssl_in_use)
253  ereport(LOG,
254  (port->application_name != NULL
255  ? errmsg("replication connection authorized: user=%s application_name=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
256  port->user_name,
257  port->application_name,
258  be_tls_get_version(port),
259  be_tls_get_cipher(port),
261  be_tls_get_compression(port) ? _("on") : _("off"))
262  : errmsg("replication connection authorized: user=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
263  port->user_name,
264  be_tls_get_version(port),
265  be_tls_get_cipher(port),
267  be_tls_get_compression(port) ? _("on") : _("off"))));
268  else
269 #endif
270  ereport(LOG,
271  (port->application_name != NULL
272  ? errmsg("replication connection authorized: user=%s application_name=%s",
273  port->user_name,
274  port->application_name)
275  : errmsg("replication connection authorized: user=%s",
276  port->user_name)));
277  }
278  else
279  {
280 #ifdef USE_SSL
281  if (port->ssl_in_use)
282  ereport(LOG,
283  (port->application_name != NULL
284  ? errmsg("connection authorized: user=%s database=%s application_name=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
285  port->user_name, port->database_name, port->application_name,
286  be_tls_get_version(port),
287  be_tls_get_cipher(port),
289  be_tls_get_compression(port) ? _("on") : _("off"))
290  : errmsg("connection authorized: user=%s database=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)",
291  port->user_name, port->database_name,
292  be_tls_get_version(port),
293  be_tls_get_cipher(port),
295  be_tls_get_compression(port) ? _("on") : _("off"))));
296  else
297 #endif
298  ereport(LOG,
299  (port->application_name != NULL
300  ? errmsg("connection authorized: user=%s database=%s application_name=%s",
301  port->user_name, port->database_name, port->application_name)
302  : errmsg("connection authorized: user=%s database=%s",
303  port->user_name, port->database_name)));
304  }
305  }
306 
307  set_ps_display("startup", false);
308 
309  ClientAuthInProgress = false; /* client_min_messages is active now */
310 }
#define AllocSetContextCreate
Definition: memutils.h:169
void ClientAuthentication(Port *port)
Definition: auth.c:343
void set_ps_display(const char *activity, bool force)
Definition: ps_status.c:331
bool ssl_in_use
Definition: libpq-be.h:190
#define LOG
Definition: elog.h:26
bool Log_connections
Definition: postmaster.c:242
bool load_ident(void)
Definition: hba.c:2979
int AuthenticationTimeout
Definition: postmaster.c:239
bool ClientAuthInProgress
Definition: postmaster.c:352
bool am_walsender
Definition: walsender.c:114
bool be_tls_get_compression(Port *port)
#define FATAL
Definition: elog.h:52
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:191
bool load_hba(void)
Definition: hba.c:2183
char * user_name
Definition: libpq-be.h:141
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
int be_tls_get_cipher_bits(Port *port)
void enable_timeout_after(TimeoutId id, int delay_ms)
Definition: timeout.c:428
char * application_name
Definition: libpq-be.h:150
const char * be_tls_get_version(Port *port)
int errmsg(const char *fmt,...)
Definition: elog.c:784
const char * be_tls_get_cipher(Port *port)
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition: timeout.c:525
char * database_name
Definition: libpq-be.h:140
#define _(x)
Definition: elog.c:84
MemoryContext PostmasterContext
Definition: mcxt.c:46

◆ pg_split_opts()

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

Definition at line 469 of file postinit.c.

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

Referenced by BackendRun(), and process_startup_options().

470 {
471  StringInfoData s;
472 
473  initStringInfo(&s);
474 
475  while (*optstr)
476  {
477  bool last_was_escape = false;
478 
479  resetStringInfo(&s);
480 
481  /* skip over leading space */
482  while (isspace((unsigned char) *optstr))
483  optstr++;
484 
485  if (*optstr == '\0')
486  break;
487 
488  /*
489  * Parse a single option, stopping at the first space, unless it's
490  * escaped.
491  */
492  while (*optstr)
493  {
494  if (isspace((unsigned char) *optstr) && !last_was_escape)
495  break;
496 
497  if (!last_was_escape && *optstr == '\\')
498  last_was_escape = true;
499  else
500  {
501  last_was_escape = false;
502  appendStringInfoChar(&s, *optstr);
503  }
504 
505  optstr++;
506  }
507 
508  /* now store the option in the next argv[] position */
509  argv[(*argcp)++] = pstrdup(s.data);
510  }
511 
512  pfree(s.data);
513 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void pfree(void *pointer)
Definition: mcxt.c:1031
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:175
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46

◆ process_settings()

static void process_settings ( Oid  databaseid,
Oid  roleid 
)
static

Definition at line 1156 of file postinit.c.

References AccessShareLock, ApplySetting(), 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().

1157 {
1158  Relation relsetting;
1159  Snapshot snapshot;
1160 
1161  if (!IsUnderPostmaster)
1162  return;
1163 
1164  relsetting = table_open(DbRoleSettingRelationId, AccessShareLock);
1165 
1166  /* read all the settings under the same snapshot for efficiency */
1167  snapshot = RegisterSnapshot(GetCatalogSnapshot(DbRoleSettingRelationId));
1168 
1169  /* Later settings are ignored if set earlier. */
1170  ApplySetting(snapshot, databaseid, roleid, relsetting, PGC_S_DATABASE_USER);
1171  ApplySetting(snapshot, InvalidOid, roleid, relsetting, PGC_S_USER);
1172  ApplySetting(snapshot, databaseid, InvalidOid, relsetting, PGC_S_DATABASE);
1173  ApplySetting(snapshot, InvalidOid, InvalidOid, relsetting, PGC_S_GLOBAL);
1174 
1175  UnregisterSnapshot(snapshot);
1176  table_close(relsetting, AccessShareLock);
1177 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:865
Snapshot GetCatalogSnapshot(Oid relid)
Definition: snapmgr.c:442
#define AccessShareLock
Definition: lockdefs.h:36
bool IsUnderPostmaster
Definition: globals.c:109
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:907
#define InvalidOid
Definition: postgres_ext.h:36
void ApplySetting(Snapshot snapshot, Oid databaseid, Oid roleid, Relation relsetting, GucSource source)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ process_startup_options()

static void process_startup_options ( Port port,
bool  am_superuser 
)
static

Definition at line 1091 of file postinit.c.

References Assert, av, Port::cmdline_options, Port::guc_options, lfirst, list_head(), lnext(), name, palloc(), pg_split_opts(), PGC_BACKEND, PGC_S_CLIENT, PGC_SU_BACKEND, process_postgres_switches(), SetConfigOption(), and value.

Referenced by InitPostgres().

1092 {
1093  GucContext gucctx;
1094  ListCell *gucopts;
1095 
1096  gucctx = am_superuser ? PGC_SU_BACKEND : PGC_BACKEND;
1097 
1098  /*
1099  * First process any command-line switches that were included in the
1100  * startup packet, if we are in a regular backend.
1101  */
1102  if (port->cmdline_options != NULL)
1103  {
1104  /*
1105  * The maximum possible number of commandline arguments that could
1106  * come from port->cmdline_options is (strlen + 1) / 2; see
1107  * pg_split_opts().
1108  */
1109  char **av;
1110  int maxac;
1111  int ac;
1112 
1113  maxac = 2 + (strlen(port->cmdline_options) + 1) / 2;
1114 
1115  av = (char **) palloc(maxac * sizeof(char *));
1116  ac = 0;
1117 
1118  av[ac++] = "postgres";
1119 
1120  pg_split_opts(av, &ac, port->cmdline_options);
1121 
1122  av[ac] = NULL;
1123 
1124  Assert(ac < maxac);
1125 
1126  (void) process_postgres_switches(ac, av, gucctx, NULL);
1127  }
1128 
1129  /*
1130  * Process any additional GUC variable settings passed in startup packet.
1131  * These are handled exactly like command-line variables.
1132  */
1133  gucopts = list_head(port->guc_options);
1134  while (gucopts)
1135  {
1136  char *name;
1137  char *value;
1138 
1139  name = lfirst(gucopts);
1140  gucopts = lnext(port->guc_options, gucopts);
1141 
1142  value = lfirst(gucopts);
1143  gucopts = lnext(port->guc_options, gucopts);
1144 
1145  SetConfigOption(name, value, gucctx, PGC_S_CLIENT);
1146  }
1147 }
GucContext
Definition: guc.h:68
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
static struct @144 value
List * guc_options
Definition: libpq-be.h:143
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7487
void pg_split_opts(char **argv, int *argcp, const char *optstr)
Definition: postinit.c:469
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
struct @18::@19 av[32]
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:924
char * cmdline_options
Definition: libpq-be.h:142
void process_postgres_switches(int argc, char *argv[], GucContext ctx, const char **dbname)
Definition: postgres.c:3468

◆ ShutdownPostgres()

static void ShutdownPostgres ( int  code,
Datum  arg 
)
static

Definition at line 1190 of file postinit.c.

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

Referenced by InitPostgres().

1191 {
1192  /* Make sure we've killed any active transaction */
1194 
1195  /*
1196  * User locks are not released by transaction end, so be sure to release
1197  * them explicitly.
1198  */
1200 }
void AbortOutOfAnyTransaction(void)
Definition: xact.c:4505
#define USER_LOCKMETHOD
Definition: lock.h:129
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition: lock.c:2089

◆ StatementTimeoutHandler()

static void StatementTimeoutHandler ( void  )
static

Definition at line 1207 of file postinit.c.

References ClientAuthInProgress, kill, MyProcPid, and sig.

Referenced by InitPostgres().

1208 {
1209  int sig = SIGINT;
1210 
1211  /*
1212  * During authentication the timeout is used to deal with
1213  * authentication_timeout - we want to quit in response to such timeouts.
1214  */
1216  sig = SIGTERM;
1217 
1218 #ifdef HAVE_SETSID
1219  /* try to signal whole process group */
1220  kill(-MyProcPid, sig);
1221 #endif
1222  kill(MyProcPid, sig);
1223 }
int MyProcPid
Definition: globals.c:40
#define kill(pid, sig)
Definition: win32_port.h:435
bool ClientAuthInProgress
Definition: postmaster.c:352
static int sig
Definition: pg_ctl.c:84

◆ ThereIsAtLeastOneRole()

static bool ThereIsAtLeastOneRole ( void  )
static

Definition at line 1250 of file postinit.c.

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

Referenced by InitPostgres().

1251 {
1252  Relation pg_authid_rel;
1253  TableScanDesc scan;
1254  bool result;
1255 
1256  pg_authid_rel = table_open(AuthIdRelationId, AccessShareLock);
1257 
1258  scan = table_beginscan_catalog(pg_authid_rel, 0, NULL);
1259  result = (heap_getnext(scan, ForwardScanDirection) != NULL);
1260 
1261  table_endscan(scan);
1262  table_close(pg_authid_rel, AccessShareLock);
1263 
1264  return result;
1265 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:98
#define AccessShareLock
Definition: lockdefs.h:36
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1290
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39