Skip to content

Commit 50c00bb

Browse files
authored
[OSS-ONLY] Fix: Preventing locking in case the object to be locked belongs to an ENR temp table. (#687) (#691)
Previously, objects belonging to ENR temp tables were still unnecessarily being locked leading to contention during concurrent temp table operations. These changes add a hook which checks given an object and catalog id whether the object belongs to an ENR temp table, and if so, skip putting a lock for it. Task : BABEL-5971 (cherry picked from commit b860395) Signed-off-by: Ayush Shah <ayushdsh@amazon.com>
1 parent ee6b1b1 commit 50c00bb

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/backend/storage/lmgr/lock.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,9 @@ LockAcquireExtended(const LOCKTAG *locktag,
862862
LWLock *partitionLock;
863863
bool found_conflict;
864864
bool log_lock = false;
865+
bool is_temp_relation_lock = pltsql_get_tsql_enr_from_oid_hook && locktag->locktag_type == LOCKTAG_RELATION &&
866+
(*pltsql_get_tsql_enr_from_oid_hook)(locktag->locktag_field2);
867+
bool is_temp_object_lock = find_object_in_enr_hook && locktag->locktag_type == LOCKTAG_OBJECT && (*find_object_in_enr_hook) (locktag->locktag_field2, locktag->locktag_field3);
865868

866869
if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
867870
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
@@ -879,8 +882,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
879882
lockMethodTable->lockModeNames[lockmode]),
880883
errhint("Only RowExclusiveLock or less can be acquired on database objects during recovery.")));
881884

882-
if (pltsql_get_tsql_enr_from_oid_hook && locktag->locktag_type == LOCKTAG_RELATION &&
883-
(*pltsql_get_tsql_enr_from_oid_hook)(locktag->locktag_field2))
885+
if (is_temp_relation_lock || is_temp_object_lock)
884886
{
885887
/*
886888
* Normally, opening a relation with AccessExclusiveLock will automatically trigger for an XID

src/backend/utils/misc/queryenvironment.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,7 @@
5656
#define NUM_ENR_CATALOGS 11
5757

5858
pltsql_get_tsql_enr_from_oid_hook_type pltsql_get_tsql_enr_from_oid_hook = NULL;
59-
60-
/*
61-
* Private state of a query environment.
62-
*/
63-
struct QueryEnvironment
64-
{
65-
List *namedRelList;
66-
List *dropped_namedRelList;
67-
List *savedCatcacheMessages;
68-
struct QueryEnvironment *parentEnv;
69-
MemoryContext memctx;
70-
};
59+
find_object_in_enr_hook_type find_object_in_enr_hook = NULL;
7160

7261
/*
7362
* This list must match ENRCatalogTupleType in queryenvironment.h.

src/include/utils/queryenvironment.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ typedef enum EphemeralNameRelationType
2828
ENR_TSQL_TEMP /* Temp table created in procedure/function */
2929
} EphemeralNameRelationType;
3030

31+
/*
32+
* Private state of a query environment.
33+
*/
34+
struct QueryEnvironment
35+
{
36+
List *namedRelList;
37+
List *dropped_namedRelList;
38+
List *savedCatcacheMessages;
39+
struct QueryEnvironment *parentEnv;
40+
MemoryContext memctx;
41+
};
42+
3143
typedef enum ENRCatalogTupleType
3244
{
3345
ENR_CATTUP_CLASS = 0,
@@ -171,4 +183,7 @@ extern bool has_existing_enr_relations(void);
171183
typedef EphemeralNamedRelation (*pltsql_get_tsql_enr_from_oid_hook_type) (Oid oid);
172184
extern PGDLLIMPORT pltsql_get_tsql_enr_from_oid_hook_type pltsql_get_tsql_enr_from_oid_hook;
173185

186+
typedef EphemeralNamedRelation (*find_object_in_enr_hook_type) (Oid catalog_oid, Oid object_id);
187+
extern PGDLLIMPORT find_object_in_enr_hook_type find_object_in_enr_hook;
188+
174189
#endif /* QUERYENVIRONMENT_H */

0 commit comments

Comments
 (0)