Refactor error handling

pull/7254/head
EmelSimsek 2023-10-17 10:28:28 +03:00 committed by Emel Şimşek
parent c26415ce48
commit b14c5a0308
1 changed files with 24 additions and 4 deletions

View File

@ -359,8 +359,14 @@ CitusMaintenanceDaemonMain(Datum main_arg)
if (!myDbData) if (!myDbData)
{ {
LWLockRelease(&MaintenanceDaemonControl->lock); /*
return; * When the database crashes, background workers are restarted, but
* the state in shared memory is lost. In that case, we exit and
* wait for a session to call InitializeMaintenanceDaemonBackend
* to properly add it to the hash.
*/
proc_exit(0);
} }
if (!found) if (!found)
@ -371,6 +377,7 @@ CitusMaintenanceDaemonMain(Datum main_arg)
} }
before_shmem_exit(MaintenanceDaemonShmemExit, ObjectIdGetDatum(MyDatabaseId)); before_shmem_exit(MaintenanceDaemonShmemExit, ObjectIdGetDatum(MyDatabaseId));
databaseOid = MyDatabaseId; databaseOid = MyDatabaseId;
myDbData->userOid = GetSessionUserId(); myDbData->userOid = GetSessionUserId();
} }
@ -381,7 +388,8 @@ CitusMaintenanceDaemonMain(Datum main_arg)
myDbData = (MaintenanceDaemonDBData *) myDbData = (MaintenanceDaemonDBData *)
hash_search(MaintenanceDaemonDBHash, &databaseOid, hash_search(MaintenanceDaemonDBHash, &databaseOid,
HASH_FIND, NULL); HASH_FIND, NULL);
if (!myDbData || myDbData->workerPid != 0)
if (!myDbData)
{ {
/* /*
* When the database crashes, background workers are restarted, but * When the database crashes, background workers are restarted, but
@ -390,9 +398,21 @@ CitusMaintenanceDaemonMain(Datum main_arg)
* to properly add it to the hash. * to properly add it to the hash.
*/ */
LWLockRelease(&MaintenanceDaemonControl->lock);
proc_exit(0); proc_exit(0);
} }
if (myDbData->workerPid != 0)
{
/*
* Another maintenance daemon is running. This usually happens because
* postgres restarts the daemon after an non-zero exit, and
* InitializeMaintenanceDaemonBackend started one before postgres did.
* In that case, the first one stays and the last one exits.
*/
proc_exit(0);
}
before_shmem_exit(MaintenanceDaemonShmemExit, main_arg); before_shmem_exit(MaintenanceDaemonShmemExit, main_arg);
BackgroundWorkerInitializeConnectionByOid(databaseOid, myDbData->userOid, 0); BackgroundWorkerInitializeConnectionByOid(databaseOid, myDbData->userOid, 0);