shengyi.wang
2022-07-27 10:58:47 +08:00
parent 004f671b4d
commit 10a9cdeec7

View File

@ -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.
*