From 10a9cdeec75d772a353d42cfde59f4642d917511 Mon Sep 17 00:00:00 2001 From: "shengyi.wang" Date: Wed, 27 Jul 2022 10:58:47 +0800 Subject: [PATCH 1/2] link https://gitee.com/opengauss/openGauss-server/issues/I5AZHL --- src/common/backend/utils/init/postinit.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/common/backend/utils/init/postinit.cpp b/src/common/backend/utils/init/postinit.cpp index 14b5fc6ee..a2c7fb244 100644 --- a/src/common/backend/utils/init/postinit.cpp +++ b/src/common/backend/utils/init/postinit.cpp @@ -2463,10 +2463,10 @@ void PostgresInitializer::SetDatabaseByName() ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", m_indbname))); } dbform = (Form_pg_database)GETSTRUCT(tuple); - u_sess->proc_cxt.MyDatabaseId = HeapTupleGetOid(tuple); + // Do not set u_sess->proc_cxt.MyDatabaseId before LockDatabase. Other processes will assume that the session is connected to this database. + m_dboid = HeapTupleGetOid(tuple); u_sess->proc_cxt.MyDatabaseTableSpace = dbform->dattablespace; /* take database name from the caller, just for paranoia */ - m_dboid = u_sess->proc_cxt.MyDatabaseId; strlcpy(m_dbname, m_indbname, sizeof(m_dbname)); if (EnableLocalSysCache()) { t_thrd.lsc_cxt.lsc->InitSessionDatabase(m_dboid, m_dbname, dbform->dattablespace); @@ -2491,9 +2491,8 @@ void PostgresInitializer::SetDatabaseByOid() ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database %u does not exist", m_dboid))); } dbform = (Form_pg_database)GETSTRUCT(tuple); - u_sess->proc_cxt.MyDatabaseId = HeapTupleGetOid(tuple); + // Do not set u_sess->proc_cxt.MyDatabaseId before LockDatabase. Other processes will assume that the session is connected to this database. u_sess->proc_cxt.MyDatabaseTableSpace = dbform->dattablespace; - Assert(u_sess->proc_cxt.MyDatabaseId == m_dboid); strlcpy(m_dbname, NameStr(dbform->datname), sizeof(m_dbname)); if (EnableLocalSysCache()) { t_thrd.lsc_cxt.lsc->InitSessionDatabase(m_dboid, m_dbname, dbform->dattablespace); @@ -2524,7 +2523,10 @@ void PostgresInitializer::LockDatabase() * CREATE DATABASE. */ - LockSharedObject(DatabaseRelationId, u_sess->proc_cxt.MyDatabaseId, 0, RowExclusiveLock); + LockSharedObject(DatabaseRelationId, m_dboid, 0, RowExclusiveLock); + + // Set u_sess->proc_cxt.MyDatabaseId here. It's safe. + u_sess->proc_cxt.MyDatabaseId = m_dboid; /* * Now we can mark our PGPROC entry with the database ID. * From 94fc1b50a1daa583b5a3ec982ddbe5c089c235dd Mon Sep 17 00:00:00 2001 From: "shengyi.wang" Date: Thu, 28 Jul 2022 11:10:12 +0800 Subject: [PATCH 2/2] link #I5AZHL --- src/common/backend/utils/init/postinit.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/common/backend/utils/init/postinit.cpp b/src/common/backend/utils/init/postinit.cpp index a2c7fb244..ef424754e 100644 --- a/src/common/backend/utils/init/postinit.cpp +++ b/src/common/backend/utils/init/postinit.cpp @@ -2463,7 +2463,8 @@ void PostgresInitializer::SetDatabaseByName() ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", m_indbname))); } dbform = (Form_pg_database)GETSTRUCT(tuple); - // Do not set u_sess->proc_cxt.MyDatabaseId before LockDatabase. Other processes will assume that the session is connected to this database. + // Do not set u_sess->proc_cxt.MyDatabaseId before LockDatabase. + // Other processes will assume that the session is connected to this database. m_dboid = HeapTupleGetOid(tuple); u_sess->proc_cxt.MyDatabaseTableSpace = dbform->dattablespace; /* take database name from the caller, just for paranoia */ @@ -2491,7 +2492,8 @@ void PostgresInitializer::SetDatabaseByOid() ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database %u does not exist", m_dboid))); } dbform = (Form_pg_database)GETSTRUCT(tuple); - // Do not set u_sess->proc_cxt.MyDatabaseId before LockDatabase. Other processes will assume that the session is connected to this database. + // Do not set u_sess->proc_cxt.MyDatabaseId before LockDatabase. + // Other processes will assume that the session is connected to this database. u_sess->proc_cxt.MyDatabaseTableSpace = dbform->dattablespace; strlcpy(m_dbname, NameStr(dbform->datname), sizeof(m_dbname)); if (EnableLocalSysCache()) { @@ -2524,7 +2526,6 @@ void PostgresInitializer::LockDatabase() */ LockSharedObject(DatabaseRelationId, m_dboid, 0, RowExclusiveLock); - // Set u_sess->proc_cxt.MyDatabaseId here. It's safe. u_sess->proc_cxt.MyDatabaseId = m_dboid; /*