59 #define DIRECTORY_LOCK_FILE "postmaster.pid"
125 _setmode(fileno(stderr), _O_BINARY);
205 elog(
FATAL,
"%s: could not locate my own executable path",
251 const char *backendDesc =
"unknown process type";
256 backendDesc =
"not initialized";
259 backendDesc =
"autovacuum launcher";
262 backendDesc =
"autovacuum worker";
265 backendDesc =
"client backend";
268 backendDesc =
"background worker";
271 backendDesc =
"background writer";
274 backendDesc =
"checkpointer";
277 backendDesc =
"startup";
280 backendDesc =
"walreceiver";
283 backendDesc =
"walsender";
286 backendDesc =
"walwriter";
289 backendDesc =
"archiver";
292 backendDesc =
"logger";
320 struct stat stat_buf;
329 errmsg(
"data directory \"%s\" does not exist",
334 errmsg(
"could not read permissions of directory \"%s\": %m",
341 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
342 errmsg(
"specified data directory \"%s\" is not a directory",
354 #if !defined(WIN32) && !defined(__CYGWIN__)
355 if (stat_buf.
st_uid != geteuid())
357 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
358 errmsg(
"data directory \"%s\" has wrong ownership",
360 errhint(
"The server must be started by the user that owns the data directory.")));
374 #if !defined(WIN32) && !defined(__CYGWIN__)
377 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
378 errmsg(
"data directory \"%s\" has invalid permissions",
380 errdetail(
"Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).")));
395 #if !defined(WIN32) && !defined(__CYGWIN__)
438 errmsg(
"could not change directory to \"%s\": %m",
663 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
664 errmsg(
"cannot set parameter \"%s\" within security-restricted operation",
718 if (rolename != NULL)
723 (
errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
724 errmsg(
"role \"%s\" does not exist", rolename)));
731 (
errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
732 errmsg(
"role with OID %u does not exist", roleid)));
737 rname =
NameStr(rform->rolname);
759 if (!rform->rolcanlogin)
761 (
errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
762 errmsg(
"role \"%s\" is not permitted to log in",
775 if (rform->rolconnlimit >= 0 &&
779 (
errcode(ERRCODE_TOO_MANY_CONNECTIONS),
780 errmsg(
"too many connections for role \"%s\"",
839 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
840 errmsg(
"permission denied to set session authorization")));
922 (
errcode(ERRCODE_UNDEFINED_OBJECT),
923 errmsg(
"invalid role OID: %u", roleid)));
960 char *curfile = (
char *)
lfirst(l);
977 (
errmsg(
"database system is shut down")));
990 const char *socketDir,
991 bool isDDLock,
const char *refName)
1025 my_p_pid = getppid();
1035 envvar = getenv(
"PG_GRANDPARENT_PID");
1037 my_gp_pid = atoi(envvar);
1046 for (ntries = 0;; ntries++)
1061 if ((errno != EEXIST && errno != EACCES) || ntries > 100)
1064 errmsg(
"could not create lock file \"%s\": %m",
1074 if (errno == ENOENT)
1078 errmsg(
"could not open lock file \"%s\": %m",
1082 if ((
len =
read(
fd, buffer,
sizeof(buffer) - 1)) < 0)
1085 errmsg(
"could not read lock file \"%s\": %m",
1093 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
1095 errhint(
"Either another server is starting, or the lock file is the remnant of a previous server startup crash.")));
1099 encoded_pid = atoi(buffer);
1102 other_pid = (pid_t) (encoded_pid < 0 ? -encoded_pid : encoded_pid);
1105 elog(
FATAL,
"bogus data in lock file \"%s\": \"%s\"",
1130 if (other_pid != my_pid && other_pid != my_p_pid &&
1131 other_pid != my_gp_pid)
1133 if (
kill(other_pid, 0) == 0 ||
1134 (errno != ESRCH && errno != EPERM))
1138 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
1139 errmsg(
"lock file \"%s\" already exists",
1143 errhint(
"Is another postgres (PID %d) running in data directory \"%s\"?",
1144 (
int) other_pid, refName) :
1145 errhint(
"Is another postmaster (PID %d) running in data directory \"%s\"?",
1146 (
int) other_pid, refName)) :
1148 errhint(
"Is another postgres (PID %d) using socket file \"%s\"?",
1149 (
int) other_pid, refName) :
1150 errhint(
"Is another postmaster (PID %d) using socket file \"%s\"?",
1151 (
int) other_pid, refName))));
1175 if ((ptr = strchr(ptr,
'\n')) == NULL)
1181 sscanf(ptr,
"%lu %lu", &id1, &id2) == 2)
1185 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
1186 errmsg(
"pre-existing shared memory block (key %lu, ID %lu) is still in use",
1188 errhint(
"Terminate any old server processes associated with data directory \"%s\".",
1201 errmsg(
"could not remove old lock file \"%s\": %m",
1203 errhint(
"The file seems accidentally left over, but "
1204 "it could not be removed. Please remove the file "
1205 "by hand and try again.")));
1214 snprintf(buffer,
sizeof(buffer),
"%d\n%s\n%ld\n%d\n%s\n",
1215 amPostmaster ? (
int) my_pid : -((
int) my_pid),
1225 if (isDDLock && !amPostmaster)
1226 strlcat(buffer,
"\n",
sizeof(buffer));
1230 if (
write(
fd, buffer, strlen(buffer)) != strlen(buffer))
1232 int save_errno = errno;
1237 errno = save_errno ? save_errno : ENOSPC;
1247 int save_errno = errno;
1259 int save_errno = errno;
1304 const char *socketDir)
1308 snprintf(lockfile,
sizeof(lockfile),
"%s.lock", socketfile);
1309 CreateLockFile(lockfile, amPostmaster, socketDir,
false, socketfile);
1327 char *socketLockFile = (
char *)
lfirst(l);
1334 (void) utime(socketLockFile, NULL);
1357 char srcbuffer[BLCKSZ];
1358 char destbuffer[BLCKSZ];
1365 errmsg(
"could not open file \"%s\": %m",
1370 len =
read(
fd, srcbuffer,
sizeof(srcbuffer) - 1);
1376 errmsg(
"could not read from file \"%s\": %m",
1381 srcbuffer[
len] =
'\0';
1388 for (lineno = 1; lineno < target_line; lineno++)
1390 char *eol = strchr(srcptr,
'\n');
1396 memcpy(destbuffer, srcbuffer, srcptr - srcbuffer);
1397 destptr = destbuffer + (srcptr - srcbuffer);
1403 for (; lineno < target_line; lineno++)
1405 if (destptr < destbuffer +
sizeof(destbuffer))
1412 snprintf(destptr, destbuffer +
sizeof(destbuffer) - destptr,
"%s\n",
str);
1413 destptr += strlen(destptr);
1418 if ((srcptr = strchr(srcptr,
'\n')) != NULL)
1421 snprintf(destptr, destbuffer +
sizeof(destbuffer) - destptr,
"%s",
1429 len = strlen(destbuffer);
1440 errmsg(
"could not write to file \"%s\": %m",
1451 errmsg(
"could not write to file \"%s\": %m",
1459 errmsg(
"could not write to file \"%s\": %m",
1482 char buffer[BLCKSZ];
1499 errmsg(
"could not open file \"%s\": %m",
1506 errmsg(
"could not open file \"%s\": %m; continuing anyway",
1512 len =
read(
fd, buffer,
sizeof(buffer) - 1);
1518 errmsg(
"could not read from file \"%s\": %m",
1525 file_pid = atol(buffer);
1526 if (file_pid == getpid())
1531 (
errmsg(
"lock file \"%s\" contains wrong PID: %ld instead of %ld",
1557 char file_version_string[64];
1558 const char *my_version_string = PG_VERSION;
1560 my_major = strtol(my_version_string, &endptr, 10);
1562 snprintf(full_path,
sizeof(full_path),
"%s/PG_VERSION", path);
1567 if (errno == ENOENT)
1569 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1570 errmsg(
"\"%s\" is not a valid data directory",
1572 errdetail(
"File \"%s\" is missing.", full_path)));
1576 errmsg(
"could not open file \"%s\": %m", full_path)));
1579 file_version_string[0] =
'\0';
1580 ret = fscanf(file,
"%63s", file_version_string);
1581 file_major = strtol(file_version_string, &endptr, 10);
1583 if (ret != 1 || endptr == file_version_string)
1585 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1586 errmsg(
"\"%s\" is not a valid data directory",
1588 errdetail(
"File \"%s\" does not contain valid data.",
1590 errhint(
"You might need to initdb.")));
1594 if (my_major != file_major)
1596 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1597 errmsg(
"database files are incompatible with server"),
1598 errdetail(
"The data directory was initialized by PostgreSQL version %s, "
1599 "which is not compatible with this version %s.",
1600 file_version_string, my_version_string)));
1636 if (libraries == NULL || libraries[0] ==
'\0')
1640 rawstring =
pstrdup(libraries);
1649 (
errcode(ERRCODE_SYNTAX_ERROR),
1650 errmsg(
"invalid list syntax in parameter \"%s\"",
1655 foreach(l, elemlist)
1659 char *expanded = NULL;
1686 "shared_preload_libraries",
1699 "session_preload_libraries",
1702 "local_preload_libraries",
1727 bindtextdomain(domain, locale_path);
1728 pg_bind_textdomain_codeset(domain);
bool IsAutoVacuumWorkerProcess(void)
#define AssertState(condition)
#define OidIsValid(objectId)
#define AssertArg(condition)
int find_my_exec(const char *argv0, char *retpath)
elog(ERROR, "%s: %s", p2, msg)
void load_file(const char *filename, bool restricted)
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
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)
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 InitStandaloneProcess(const char *argv0)
void InitializeSessionUserIdStandalone(void)
void AddToDataDirLockFile(int target_line, const char *str)
static bool AuthenticatedUserIsSuperuser
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 int SecurityRestrictionContext
Oid GetSessionUserId(void)
void SetCurrentRoleId(Oid roleid, bool is_superuser)
Oid GetAuthenticatedUserId(void)
bool InLocalUserIdChange(void)
void SetDatabasePath(const char *path)
void InitPostmasterChild(void)
void InitializeSessionUserId(const char *rolename, Oid roleid)
char * shared_preload_libraries_string
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)
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)
static void static void status(const char *fmt,...) pg_attribute_printf(1
#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)
ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset)
size_t strlcat(char *dst, const char *src, size_t siz)
pg_stack_base_t set_stack_base(void)
#define ObjectIdGetDatum(X)
#define PointerGetDatum(X)
void InitProcessGlobals(void)
WaitEventSet * FeBeWaitSet
static int fd(const char *x, int i)
int CountUserBackends(Oid roleid)
char * psprintf(const char *fmt,...)
void pgwin32_signal_initialize(void)
pqsigfunc pqsignal(int signum, pqsigfunc handler)
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)
@ WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE
@ WAIT_EVENT_LOCK_FILE_CREATE_SYNC
@ WAIT_EVENT_LOCK_FILE_CREATE_READ
@ WAIT_EVENT_LOCK_FILE_CREATE_WRITE
@ WAIT_EVENT_LOCK_FILE_RECHECKDATADIR_READ
@ WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ
@ WAIT_EVENT_LOCK_FILE_ADDTODATADIR_SYNC
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)