Skip to content

Commit b860395

Browse files
authored
[OSS-ONLY] Fix: Preventing locking in case the object to be locked belongs to an ENR temp table. (babelfish-for-postgresql#687)
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 Signed-off-by: Ayush Shah <ayushdsh@amazon.com>
1 parent b852e45 commit b860395

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ LockAcquireExtended(const LOCKTAG *locktag,
850850
LWLock *partitionLock;
851851
bool found_conflict;
852852
bool log_lock = false;
853+
bool is_temp_relation_lock = pltsql_get_tsql_enr_from_oid_hook && locktag->locktag_type == LOCKTAG_RELATION &&
854+
(*pltsql_get_tsql_enr_from_oid_hook)(locktag->locktag_field2);
855+
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);
853856

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

870-
if (pltsql_get_tsql_enr_from_oid_hook && locktag->locktag_type == LOCKTAG_RELATION &&
871-
(*pltsql_get_tsql_enr_from_oid_hook)(locktag->locktag_field2))
873+
if (is_temp_relation_lock || is_temp_object_lock)
872874
{
873875
/*
874876
* 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)