Skip to content

Commit 8a35ab3

Browse files
Jason TengANJU BHARTI
authored andcommitted
BABEL: Skip locking the tuple for ENR-related tuple modifications.
Commit 09f0820095ea8b7c6ca4269454a94695c8421628 introduced locking for individual tuples during certain operations. However, Babelfish introduced the concept of ENR-only relations, which store all catalog tuples in their own local cache and not in the physical catalogs. As these relations and their catalog tuples are completely session-local, there is no need to acquire locks on tuples for these relations (and it would just lead to a crash anyways since the tuples do not have an underlying TID to lock against). Signed-off-by: Jason Teng <jasonten@amazon.com>
1 parent dec0d7e commit 8a35ab3

6 files changed

Lines changed: 86 additions & 21 deletions

File tree

src/backend/catalog/aclchk.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "nodes/makefuncs.h"
7676
#include "parser/parse_func.h"
7777
#include "parser/parse_type.h"
78+
#include "parser/parser.h"
7879
#include "storage/lmgr.h"
7980
#include "utils/acl.h"
8081
#include "utils/aclchk_internal.h"
@@ -1813,8 +1814,12 @@ ExecGrant_Relation(InternalGrant *istmt)
18131814
Oid ownerId;
18141815
HeapTuple tuple;
18151816
ListCell *cell_colprivs;
1817+
bool is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, relOid, ENR_TSQL_TEMP));
18161818

1817-
tuple = SearchSysCacheLocked1(RELOID, ObjectIdGetDatum(relOid));
1819+
if (is_enr)
1820+
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
1821+
else
1822+
tuple = SearchSysCacheLocked1(RELOID, ObjectIdGetDatum(relOid));
18181823
if (!HeapTupleIsValid(tuple))
18191824
elog(ERROR, "cache lookup failed for relation %u", relOid);
18201825
pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
@@ -2031,7 +2036,8 @@ ExecGrant_Relation(InternalGrant *istmt)
20312036
values, nulls, replaces);
20322037

20332038
CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2034-
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
2039+
if (!is_enr)
2040+
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
20352041

20362042
/* Update initial privileges for extensions */
20372043
recordExtensionInitPriv(relOid, RelationRelationId, 0, new_acl);
@@ -2044,7 +2050,7 @@ ExecGrant_Relation(InternalGrant *istmt)
20442050

20452051
pfree(new_acl);
20462052
}
2047-
else
2053+
else if (!is_enr)
20482054
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
20492055

20502056
/*
@@ -2158,11 +2164,17 @@ ExecGrant_common(InternalGrant *istmt, Oid classid, AclMode default_privs,
21582164
int nnewmembers;
21592165
Oid *oldmembers;
21602166
Oid *newmembers;
2167+
bool is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, objectid, ENR_TSQL_TEMP));
2168+
21612169

2162-
tuple = SearchSysCacheLocked1(cacheid, ObjectIdGetDatum(objectid));
2170+
if (is_enr)
2171+
tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
2172+
else
2173+
tuple = SearchSysCacheLocked1(cacheid, ObjectIdGetDatum(objectid));
21632174
if (!HeapTupleIsValid(tuple))
21642175
elog(ERROR, "cache lookup failed for %s %u", get_object_class_descr(classid), objectid);
21652176

2177+
21662178
/*
21672179
* Additional object-type-specific checks
21682180
*/
@@ -2240,7 +2252,8 @@ ExecGrant_common(InternalGrant *istmt, Oid classid, AclMode default_privs,
22402252
nulls, replaces);
22412253

22422254
CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2243-
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
2255+
if (!is_enr)
2256+
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
22442257

22452258
/* Update initial privileges for extensions */
22462259
recordExtensionInitPriv(objectid, classid, 0, new_acl);

src/backend/commands/dbcommands.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "common/file_perm.h"
5252
#include "mb/pg_wchar.h"
5353
#include "miscadmin.h"
54+
#include "parser/parser.h"
5455
#include "pgstat.h"
5556
#include "postmaster/bgwriter.h"
5657
#include "replication/slot.h"
@@ -1908,6 +1909,7 @@ RenameDatabase(const char *oldname, const char *newname)
19081909
int notherbackends;
19091910
int npreparedxacts;
19101911
ObjectAddress address;
1912+
bool is_enr = false;
19111913

19121914
/*
19131915
* Look up the target database's OID, and get exclusive lock on it. We
@@ -1975,13 +1977,18 @@ RenameDatabase(const char *oldname, const char *newname)
19751977
errdetail_busy_db(notherbackends, npreparedxacts)));
19761978

19771979
/* rename */
1978-
newtup = SearchSysCacheLockedCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
1980+
is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, db_id, ENR_TSQL_TEMP));
1981+
if (is_enr)
1982+
newtup = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
1983+
else
1984+
newtup = SearchSysCacheLockedCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
19791985
if (!HeapTupleIsValid(newtup))
19801986
elog(ERROR, "cache lookup failed for database %u", db_id);
19811987
otid = newtup->t_self;
19821988
namestrcpy(&(((Form_pg_database) GETSTRUCT(newtup))->datname), newname);
19831989
CatalogTupleUpdate(rel, &otid, newtup);
1984-
UnlockTuple(rel, &otid, InplaceUpdateTupleLock);
1990+
if (!is_enr)
1991+
UnlockTuple(rel, &otid, InplaceUpdateTupleLock);
19851992

19861993
InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0);
19871994

src/backend/commands/indexcmds.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "parser/parse_coerce.h"
5353
#include "parser/parse_oper.h"
5454
#include "parser/parse_utilcmd.h"
55+
#include "parser/parser.h"
5556
#include "partitioning/partdesc.h"
5657
#include "pgstat.h"
5758
#include "rewrite/rewriteManip.h"
@@ -68,6 +69,7 @@
6869
#include "utils/memutils.h"
6970
#include "utils/partcache.h"
7071
#include "utils/pg_rusage.h"
72+
#include "utils/queryenvironment.h"
7173
#include "utils/regproc.h"
7274
#include "utils/snapmgr.h"
7375
#include "utils/syscache.h"
@@ -4577,16 +4579,21 @@ update_relispartition(Oid relationId, bool newval)
45774579
HeapTuple tup;
45784580
Relation classRel;
45794581
ItemPointerData otid;
4582+
bool is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, relationId, ENR_TSQL_TEMP));
45804583

45814584
classRel = table_open(RelationRelationId, RowExclusiveLock);
4582-
tup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(relationId));
4585+
if (is_enr)
4586+
tup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
4587+
else
4588+
tup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(relationId));
45834589
if (!HeapTupleIsValid(tup))
45844590
elog(ERROR, "cache lookup failed for relation %u", relationId);
45854591
otid = tup->t_self;
45864592
Assert(((Form_pg_class) GETSTRUCT(tup))->relispartition != newval);
45874593
((Form_pg_class) GETSTRUCT(tup))->relispartition = newval;
45884594
CatalogTupleUpdate(classRel, &otid, tup);
4589-
UnlockTuple(classRel, &otid, InplaceUpdateTupleLock);
4595+
if (!is_enr)
4596+
UnlockTuple(classRel, &otid, InplaceUpdateTupleLock);
45904597
heap_freetuple(tup);
45914598
table_close(classRel, RowExclusiveLock);
45924599
}

src/backend/commands/tablecmds.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,13 +3819,17 @@ SetRelationTableSpace(Relation rel,
38193819
ItemPointerData otid;
38203820
Form_pg_class rd_rel;
38213821
Oid reloid = RelationGetRelid(rel);
3822+
bool is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, reloid, ENR_TSQL_TEMP));
38223823

38233824
Assert(CheckRelationTableSpaceMove(rel, newTableSpaceId));
38243825

38253826
/* Get a modifiable copy of the relation's pg_class row. */
38263827
pg_class = table_open(RelationRelationId, RowExclusiveLock);
38273828

3828-
tuple = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(reloid));
3829+
if (is_enr)
3830+
tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(reloid));
3831+
else
3832+
tuple = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(reloid));
38293833
if (!HeapTupleIsValid(tuple))
38303834
elog(ERROR, "cache lookup failed for relation %u", reloid);
38313835
otid = tuple->t_self;
@@ -3837,7 +3841,8 @@ SetRelationTableSpace(Relation rel,
38373841
if (RelFileNumberIsValid(newRelFilenumber))
38383842
rd_rel->relfilenode = newRelFilenumber;
38393843
CatalogTupleUpdate(pg_class, &otid, tuple);
3840-
UnlockTuple(pg_class, &otid, InplaceUpdateTupleLock);
3844+
if (!is_enr)
3845+
UnlockTuple(pg_class, &otid, InplaceUpdateTupleLock);
38413846

38423847
/*
38433848
* Record dependency on tablespace. This is only required for relations
@@ -4339,6 +4344,7 @@ RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bo
43394344
HeapTuple reltup;
43404345
Form_pg_class relform;
43414346
Oid namespaceId;
4347+
bool is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, myrelid, ENR_TSQL_TEMP));
43424348

43434349
/*
43444350
* Grab a lock on the target relation, which we will NOT release until end
@@ -4358,7 +4364,10 @@ RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bo
43584364
*/
43594365
relrelation = table_open(RelationRelationId, RowExclusiveLock);
43604366

4361-
reltup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(myrelid));
4367+
if (is_enr)
4368+
reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(myrelid));
4369+
else
4370+
reltup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(myrelid));
43624371
if (!HeapTupleIsValid(reltup)) /* shouldn't happen */
43634372
elog(ERROR, "cache lookup failed for relation %u", myrelid);
43644373
otid = reltup->t_self;
@@ -4387,7 +4396,8 @@ RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bo
43874396
namestrcpy(&(relform->relname), newrelname);
43884397

43894398
CatalogTupleUpdate(relrelation, &otid, reltup);
4390-
UnlockTuple(relrelation, &otid, InplaceUpdateTupleLock);
4399+
if (!is_enr)
4400+
UnlockTuple(relrelation, &otid, InplaceUpdateTupleLock);
43914401

43924402
InvokeObjectPostAlterHookArg(RelationRelationId, myrelid, 0,
43934403
InvalidOid, is_internal);
@@ -16751,6 +16761,7 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation,
1675116761
bool repl_null[Natts_pg_class];
1675216762
bool repl_repl[Natts_pg_class];
1675316763
const char *const validnsps[] = HEAP_RELOPT_NAMESPACES;
16764+
bool is_enr = false;
1675416765

1675516766
if (defList == NIL && operation != AT_ReplaceRelOptions)
1675616767
return; /* nothing to do */
@@ -16759,7 +16770,11 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation,
1675916770

1676016771
/* Fetch heap tuple */
1676116772
relid = RelationGetRelid(rel);
16762-
tuple = SearchSysCacheLocked1(RELOID, ObjectIdGetDatum(relid));
16773+
is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, relid, ENR_TSQL_TEMP));
16774+
if (is_enr)
16775+
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
16776+
else
16777+
tuple = SearchSysCacheLocked1(RELOID, ObjectIdGetDatum(relid));
1676316778
if (!HeapTupleIsValid(tuple))
1676416779
elog(ERROR, "cache lookup failed for relation %u", relid);
1676516780

@@ -16866,7 +16881,8 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation,
1686616881
repl_val, repl_null, repl_repl);
1686716882

1686816883
CatalogTupleUpdate(pgclass, &newtuple->t_self, newtuple);
16869-
UnlockTuple(pgclass, &tuple->t_self, InplaceUpdateTupleLock);
16884+
if (!is_enr)
16885+
UnlockTuple(pgclass, &tuple->t_self, InplaceUpdateTupleLock);
1687016886

1687116887
InvokeObjectPostAlterHook(RelationRelationId, RelationGetRelid(rel), 0);
1687216888

@@ -19156,9 +19172,13 @@ AlterRelationNamespaceInternal(Relation classRel, Oid relOid,
1915619172
Form_pg_class classForm;
1915719173
ObjectAddress thisobj;
1915819174
bool already_done = false;
19175+
bool is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, relOid, ENR_TSQL_TEMP));
1915919176

1916019177
/* no rel lock for relkind=c so use LOCKTAG_TUPLE */
19161-
classTup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(relOid));
19178+
if (is_enr)
19179+
classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
19180+
else
19181+
classTup = SearchSysCacheLockedCopy1(RELOID, ObjectIdGetDatum(relOid));
1916219182
if (!HeapTupleIsValid(classTup))
1916319183
elog(ERROR, "cache lookup failed for relation %u", relOid);
1916419184
classForm = (Form_pg_class) GETSTRUCT(classTup);
@@ -19192,7 +19212,8 @@ AlterRelationNamespaceInternal(Relation classRel, Oid relOid,
1919219212
classForm->relnamespace = newNspOid;
1919319213

1919419214
CatalogTupleUpdate(classRel, &otid, classTup);
19195-
UnlockTuple(classRel, &otid, InplaceUpdateTupleLock);
19215+
if (!is_enr)
19216+
UnlockTuple(classRel, &otid, InplaceUpdateTupleLock);
1919619217

1919719218

1919819219
/* Update dependency on schema if caller said so */
@@ -19205,7 +19226,7 @@ AlterRelationNamespaceInternal(Relation classRel, Oid relOid,
1920519226
elog(ERROR, "could not change schema dependency for relation \"%s\"",
1920619227
NameStr(classForm->relname));
1920719228
}
19208-
else
19229+
else if (!is_enr)
1920919230
UnlockTuple(classRel, &classTup->t_self, InplaceUpdateTupleLock);
1921019231
if (!already_done)
1921119232
{

src/backend/utils/cache/relcache.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "nodes/makefuncs.h"
7373
#include "nodes/nodeFuncs.h"
7474
#include "optimizer/optimizer.h"
75+
#include "parser/parser.h"
7576
#include "pgstat.h"
7677
#include "rewrite/rewriteDefine.h"
7778
#include "rewrite/rowsecurity.h"
@@ -3781,6 +3782,7 @@ RelationSetNewRelfilenumber(Relation relation, char persistence)
37813782
MultiXactId minmulti = InvalidMultiXactId;
37823783
TransactionId freezeXid = InvalidTransactionId;
37833784
RelFileLocator newrlocator;
3785+
bool is_enr = false;
37843786

37853787
if (!IsBinaryUpgrade)
37863788
{
@@ -3818,8 +3820,13 @@ RelationSetNewRelfilenumber(Relation relation, char persistence)
38183820
*/
38193821
pg_class = table_open(RelationRelationId, RowExclusiveLock);
38203822

3821-
tuple = SearchSysCacheLockedCopy1(RELOID,
3822-
ObjectIdGetDatum(RelationGetRelid(relation)));
3823+
is_enr = (sql_dialect == SQL_DIALECT_TSQL && get_ENR_withoid(currentQueryEnv, RelationGetRelid(relation), ENR_TSQL_TEMP));
3824+
if (is_enr)
3825+
tuple = SearchSysCacheCopy1(RELOID,
3826+
ObjectIdGetDatum(RelationGetRelid(relation)));
3827+
else
3828+
tuple = SearchSysCacheLockedCopy1(RELOID,
3829+
ObjectIdGetDatum(RelationGetRelid(relation)));
38233830
if (!HeapTupleIsValid(tuple))
38243831
elog(ERROR, "could not find tuple for relation %u",
38253832
RelationGetRelid(relation));
@@ -3947,7 +3954,8 @@ RelationSetNewRelfilenumber(Relation relation, char persistence)
39473954
CatalogTupleUpdate(pg_class, &otid, tuple);
39483955
}
39493956

3950-
UnlockTuple(pg_class, &otid, InplaceUpdateTupleLock);
3957+
if (!is_enr)
3958+
UnlockTuple(pg_class, &otid, InplaceUpdateTupleLock);
39513959
heap_freetuple(tuple);
39523960

39533961
table_close(pg_class, RowExclusiveLock);

src/backend/utils/cache/syscache.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ ReleaseSysCache(HeapTuple tuple)
308308
*
309309
* The returned tuple may be the subject of an uncommitted update, so this
310310
* doesn't prevent the "tuple concurrently updated" error.
311+
*
312+
* Note: For Babelfish, this function should not be used if the target tuple is
313+
* for an ENR entry, as there is no physical tid for ENR catalog tuples (since ENR
314+
* entries hold all catalog data internally in their cache). Use SearchSysCache1() instead
315+
* to look up tuples for catalogs for ENR entries, and also skip the UnlockTuple() call
316+
* in such cases.
311317
*/
312318
HeapTuple
313319
SearchSysCacheLocked1(int cacheId,
@@ -425,6 +431,9 @@ SearchSysCacheCopy(int cacheId,
425431
* Meld SearchSysCacheLocked1 with SearchSysCacheCopy(). After the
426432
* caller's heap_update(), it should UnlockTuple(InplaceUpdateTupleLock) and
427433
* heap_freetuple().
434+
*
435+
* See SearchSysCacheLocked1() for notes about ENR entries (do not use this
436+
* function for tuples related to ENR entries).
428437
*/
429438
HeapTuple
430439
SearchSysCacheLockedCopy1(int cacheId,

0 commit comments

Comments
 (0)