59 #define DIRECTORY_LOCK_FILE "postmaster.pid"
125 _setmode(fileno(stderr), _O_BINARY);
162 sigprocmask(SIG_SETMASK, &
BlockSig, NULL);
172 errmsg_internal(
"could not set postmaster death monitoring pipe to FD_CLOEXEC mode: %m")));
207 sigprocmask(SIG_SETMASK, &
BlockSig, NULL);
213 elog(
FATAL,
"%s: could not locate my own executable path",
266 const char *backendDesc =
"unknown process type";
271 backendDesc =
"not initialized";
274 backendDesc =
"archiver";
277 backendDesc =
"autovacuum launcher";
280 backendDesc =
"autovacuum worker";
283 backendDesc =
"client backend";
286 backendDesc =
"background worker";
289 backendDesc =
"background writer";
292 backendDesc =
"checkpointer";
295 backendDesc =
"logger";
298 backendDesc =
"standalone backend";
301 backendDesc =
"startup";
304 backendDesc =
"walreceiver";
307 backendDesc =
"walsender";
310 backendDesc =
"walwriter";
338 struct stat stat_buf;
347 errmsg(
"data directory \"%s\" does not exist",
352 errmsg(
"could not read permissions of directory \"%s\": %m",
359 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
360 errmsg(
"specified data directory \"%s\" is not a directory",
372 #if !defined(WIN32) && !defined(__CYGWIN__)
373 if (stat_buf.
st_uid != geteuid())
375 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
376 errmsg(
"data directory \"%s\" has wrong ownership",
378 errhint(
"The server must be started by the user that owns the data directory.")));
392 #if !defined(WIN32) && !defined(__CYGWIN__)
395 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
396 errmsg(
"data directory \"%s\" has invalid permissions",
398 errdetail(
"Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).")));
413 #if !defined(WIN32) && !defined(__CYGWIN__)
456 errmsg(
"could not change directory to \"%s\": %m",
691 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
692 errmsg(
"cannot set parameter \"%s\" within security-restricted operation",
751 if (rolename != NULL)
756 (
errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
757 errmsg(
"role \"%s\" does not exist", rolename)));
764 (
errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
765 errmsg(
"role with OID %u does not exist", roleid)));
770 rname =
NameStr(rform->rolname);
792 if (!bypass_login_check && !rform->rolcanlogin)
794 (
errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
795 errmsg(
"role \"%s\" is not permitted to log in",
808 if (rform->rolconnlimit >= 0 &&
812 (
errcode(ERRCODE_TOO_MANY_CONNECTIONS),
813 errmsg(
"too many connections for role \"%s\"",
984 (
errcode(ERRCODE_UNDEFINED_OBJECT),
985 errmsg(
"invalid role OID: %u", roleid)));
1048 Assert(maxsize >=
sizeof(serialized));
1049 memcpy(start_address, &serialized,
sizeof(serialized));
1051 maxsize -=
sizeof(serialized);
1052 start_address +=
sizeof(serialized);
1058 memcpy(start_address,
1073 memcpy(&serialized, conninfo,
sizeof(serialized));
1083 authn_id = conninfo +
sizeof(serialized);
1115 char *curfile = (
char *)
lfirst(l);
1132 (
errmsg(
"database system is shut down")));
1145 const char *socketDir,
1146 bool isDDLock,
const char *refName)
1180 my_p_pid = getppid();
1190 envvar = getenv(
"PG_GRANDPARENT_PID");
1192 my_gp_pid = atoi(envvar);
1201 for (ntries = 0;; ntries++)
1216 if ((errno != EEXIST && errno != EACCES) || ntries > 100)
1219 errmsg(
"could not create lock file \"%s\": %m",
1229 if (errno == ENOENT)
1233 errmsg(
"could not open lock file \"%s\": %m",
1237 if ((
len =
read(
fd, buffer,
sizeof(buffer) - 1)) < 0)
1240 errmsg(
"could not read lock file \"%s\": %m",
1248 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
1250 errhint(
"Either another server is starting, or the lock file is the remnant of a previous server startup crash.")));
1254 encoded_pid = atoi(buffer);
1257 other_pid = (pid_t) (encoded_pid < 0 ? -encoded_pid : encoded_pid);
1260 elog(
FATAL,
"bogus data in lock file \"%s\": \"%s\"",
1285 if (other_pid != my_pid && other_pid != my_p_pid &&
1286 other_pid != my_gp_pid)
1288 if (
kill(other_pid, 0) == 0 ||
1289 (errno != ESRCH && errno != EPERM))
1293 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
1294 errmsg(
"lock file \"%s\" already exists",
1298 errhint(
"Is another postgres (PID %d) running in data directory \"%s\"?",
1299 (
int) other_pid, refName) :
1300 errhint(
"Is another postmaster (PID %d) running in data directory \"%s\"?",
1301 (
int) other_pid, refName)) :
1303 errhint(
"Is another postgres (PID %d) using socket file \"%s\"?",
1304 (
int) other_pid, refName) :
1305 errhint(
"Is another postmaster (PID %d) using socket file \"%s\"?",
1306 (
int) other_pid, refName))));
1330 if ((ptr = strchr(ptr,
'\n')) == NULL)
1336 sscanf(ptr,
"%lu %lu", &id1, &id2) == 2)
1340 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
1341 errmsg(
"pre-existing shared memory block (key %lu, ID %lu) is still in use",
1343 errhint(
"Terminate any old server processes associated with data directory \"%s\".",
1356 errmsg(
"could not remove old lock file \"%s\": %m",
1358 errhint(
"The file seems accidentally left over, but "
1359 "it could not be removed. Please remove the file "
1360 "by hand and try again.")));
1369 snprintf(buffer,
sizeof(buffer),
"%d\n%s\n%ld\n%d\n%s\n",
1370 amPostmaster ? (
int) my_pid : -((
int) my_pid),
1380 if (isDDLock && !amPostmaster)
1381 strlcat(buffer,
"\n",
sizeof(buffer));
1385 if (
write(
fd, buffer, strlen(buffer)) != strlen(buffer))
1387 int save_errno = errno;
1392 errno = save_errno ? save_errno : ENOSPC;
1402 int save_errno = errno;
1414 int save_errno = errno;
1459 const char *socketDir)
1463 snprintf(lockfile,
sizeof(lockfile),
"%s.lock", socketfile);
1464 CreateLockFile(lockfile, amPostmaster, socketDir,
false, socketfile);
1482 char *socketLockFile = (
char *)
lfirst(l);
1489 (void) utime(socketLockFile, NULL);
1512 char srcbuffer[BLCKSZ];
1513 char destbuffer[BLCKSZ];
1520 errmsg(
"could not open file \"%s\": %m",
1525 len =
read(
fd, srcbuffer,
sizeof(srcbuffer) - 1);
1531 errmsg(
"could not read from file \"%s\": %m",
1536 srcbuffer[
len] =
'\0';
1543 for (lineno = 1; lineno < target_line; lineno++)
1545 char *eol = strchr(srcptr,
'\n');
1551 memcpy(destbuffer, srcbuffer, srcptr - srcbuffer);
1552 destptr = destbuffer + (srcptr - srcbuffer);
1558 for (; lineno < target_line; lineno++)
1560 if (destptr < destbuffer +
sizeof(destbuffer))
1567 snprintf(destptr, destbuffer +
sizeof(destbuffer) - destptr,
"%s\n",
str);
1568 destptr += strlen(destptr);
1573 if ((srcptr = strchr(srcptr,
'\n')) != NULL)
1576 snprintf(destptr, destbuffer +
sizeof(destbuffer) - destptr,
"%s",
1584 len = strlen(destbuffer);
1595 errmsg(
"could not write to file \"%s\": %m",
1606 errmsg(
"could not write to file \"%s\": %m",
1614 errmsg(
"could not write to file \"%s\": %m",
1637 char buffer[BLCKSZ];
1654 errmsg(
"could not open file \"%s\": %m",
1661 errmsg(
"could not open file \"%s\": %m; continuing anyway",
1667 len =
read(
fd, buffer,
sizeof(buffer) - 1);
1673 errmsg(
"could not read from file \"%s\": %m",
1680 file_pid = atol(buffer);
1681 if (file_pid == getpid())
1686 (
errmsg(
"lock file \"%s\" contains wrong PID: %ld instead of %ld",
1712 char file_version_string[64];
1713 const char *my_version_string = PG_VERSION;
1715 my_major = strtol(my_version_string, &endptr, 10);
1717 snprintf(full_path,
sizeof(full_path),
"%s/PG_VERSION", path);
1722 if (errno == ENOENT)
1724 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1725 errmsg(
"\"%s\" is not a valid data directory",
1727 errdetail(
"File \"%s\" is missing.", full_path)));
1731 errmsg(
"could not open file \"%s\": %m", full_path)));
1734 file_version_string[0] =
'\0';
1735 ret = fscanf(file,
"%63s", file_version_string);
1736 file_major = strtol(file_version_string, &endptr, 10);
1738 if (ret != 1 || endptr == file_version_string)
1740 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1741 errmsg(
"\"%s\" is not a valid data directory",
1743 errdetail(
"File \"%s\" does not contain valid data.",
1745 errhint(
"You might need to initdb.")));
1749 if (my_major != file_major)
1751 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1752 errmsg(
"database files are incompatible with server"),
1753 errdetail(
"The data directory was initialized by PostgreSQL version %s, "
1754 "which is not compatible with this version %s.",
1755 file_version_string, my_version_string)));
1791 if (libraries == NULL || libraries[0] ==
'\0')
1795 rawstring =
pstrdup(libraries);
1804 (
errcode(ERRCODE_SYNTAX_ERROR),
1805 errmsg(
"invalid list syntax in parameter \"%s\"",
1810 foreach(l, elemlist)
1814 char *expanded = NULL;
1841 "shared_preload_libraries",
1854 "session_preload_libraries",
1857 "local_preload_libraries",
1882 bindtextdomain(domain, locale_path);
1883 pg_bind_textdomain_codeset(domain);
bool IsAutoVacuumWorkerProcess(void)
#define CStringGetTextDatum(s)
#define OidIsValid(objectId)
int find_my_exec(const char *argv0, char *retpath)
elog(ERROR, "%s: %s", p2, msg)
void load_file(const char *filename, bool restricted)
int errcode_for_socket_access(void)
int errmsg_internal(const char *fmt,...)
int errcode_for_file_access(void)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
FILE * AllocateFile(const char *name, const char *mode)
void SetDataDirectoryCreatePerm(int dataDirMode)
#define PG_MODE_MASK_GROUP
#define PG_RETURN_DATUM(x)
char pkglib_path[MAXPGPATH]
bool IsPostmasterEnvironment
char my_exec_path[MAXPGPATH]
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
#define HeapTupleIsValid(tuple)
void SignalHandlerForCrashExit(SIGNAL_ARGS)
void AcceptInvalidationMessages(void)
void on_proc_exit(pg_on_exit_callback function, Datum arg)
void InitializeLatchWaitSet(void)
void InitializeLatchSupport(void)
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
void SetLatch(Latch *latch)
void InitLatch(Latch *latch)
#define FeBeWaitSetLatchPos
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lcons(void *datum, List *list)
void list_free_deep(List *list)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext TopMemoryContext
char * MemoryContextStrdup(MemoryContext context, const char *string)
#define IsBootstrapProcessingMode()
#define SECURITY_NOFORCE_RLS
#define SECURITY_RESTRICTED_OPERATION
#define SECURITY_LOCAL_USERID_CHANGE
void(* shmem_request_hook_type)(void)
struct SerializedClientConnectionInfo SerializedClientConnectionInfo
void ChangeToDataDir(void)
char * GetUserNameFromId(Oid roleid, bool noerr)
bool process_shared_preload_libraries_done
void process_shmem_requests(void)
static void SetOuterUserId(Oid userid)
void InitializeSessionUserId(const char *rolename, Oid roleid, bool bypass_login_check)
void InitStandaloneProcess(const char *argv0)
void SerializeClientConnectionInfo(Size maxsize, char *start_address)
void InitializeSystemUser(const char *authn_id, const char *auth_method)
void InitializeSessionUserIdStandalone(void)
void AddToDataDirLockFile(int target_line, const char *str)
void InitProcessLocalLatch(void)
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
void SetSessionAuthorization(Oid userid, bool is_superuser)
void process_session_preload_libraries(void)
static bool SessionUserIsSuperuser
bool process_shmem_requests_in_progress
bool InSecurityRestrictedOperation(void)
#define DIRECTORY_LOCK_FILE
static const char * SystemUser
static int SecurityRestrictionContext
Size EstimateClientConnectionInfoSpace(void)
const char * GetSystemUser(void)
Oid GetSessionUserId(void)
void SetCurrentRoleId(Oid roleid, bool is_superuser)
Oid GetAuthenticatedUserId(void)
bool InLocalUserIdChange(void)
Datum system_user(PG_FUNCTION_ARGS)
void SetDatabasePath(const char *path)
void InitPostmasterChild(void)
char * shared_preload_libraries_string
ClientConnectionInfo MyClientConnectionInfo
void process_shared_preload_libraries(void)
char * session_preload_libraries_string
static void SetSessionUserId(Oid userid, bool is_superuser)
shmem_request_hook_type shmem_request_hook
char * local_preload_libraries_string
static Latch LocalLatchData
const char * GetBackendTypeDesc(BackendType backendType)
void TouchSocketLockFiles(void)
void RestoreClientConnectionInfo(char *conninfo)
static bool SetRoleIsActive
bool InNoForceRLSOperation(void)
static Oid AuthenticatedUserId
static void load_libraries(const char *libraries, const char *gucname, bool restricted)
Oid GetCurrentRoleId(void)
static void UnlinkLockFiles(int status, Datum arg)
void SwitchToSharedLatch(void)
BackendType MyBackendType
void GetUserIdAndContext(Oid *userid, bool *sec_def_context)
void SetDataDir(const char *dir)
void SetUserIdAndContext(Oid userid, bool sec_def_context)
bool process_shared_preload_libraries_in_progress
void pg_bindtextdomain(const char *domain)
bool has_rolreplication(Oid roleid)
static void CreateLockFile(const char *filename, bool amPostmaster, const char *socketDir, bool isDDLock, const char *refName)
void ValidatePgVersion(const char *path)
void SetUserIdAndSecContext(Oid userid, int sec_context)
bool RecheckDataDirLockFile(void)
void CreateDataDirLockFile(bool amPostmaster)
void SwitchBackToLocalLatch(void)
void CreateSocketLockFile(const char *socketfile, bool amPostmaster, const char *socketDir)
FormData_pg_authid * Form_pg_authid
static bool is_superuser(Archive *fout)
#define LOCK_FILE_LINE_SHMEM_KEY
void PostmasterDeathSignalInit(void)
void get_pkglib_path(const char *my_exec_path, char *ret_path)
void get_locale_path(const char *my_exec_path, char *ret_path)
char * make_absolute_path(const char *path)
char * first_dir_separator(const char *filename)
pqsigfunc pqsignal(int signo, pqsigfunc func)
size_t strlcat(char *dst, const char *src, size_t siz)
pg_stack_base_t set_stack_base(void)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
void InitProcessGlobals(void)
int postmaster_alive_fds[2]
#define POSTMASTER_FD_WATCH
WaitEventSet * FeBeWaitSet
static int fd(const char *x, int i)
int CountUserBackends(Oid roleid)
char * psprintf(const char *fmt,...)
Size add_size(Size s1, Size s2)
void pgwin32_signal_initialize(void)
bool superuser_arg(Oid roleid)
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
bool SplitDirectoriesString(char *rawstring, char separator, List **namelist)
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)