Skip to content

Commit b223d2d

Browse files
committed
PG-1437 Fix race condition in the event trigger
If we look up something about a relation in the command start event trigger we need to hold onto that lock until the end of the command and not release it so no other commd is able to change the data we looked at between now and when the DDL command itself actually locks the relation.
1 parent 21a3794 commit b223d2d

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

contrib/pg_tde/src/pg_tde_event_capture.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
122122
if (IsA(parsetree, IndexStmt))
123123
{
124124
IndexStmt *stmt = (IndexStmt *) parsetree;
125-
Oid relationId = RangeVarGetRelid(stmt->relation, NoLock, true);
125+
Oid relationId = RangeVarGetRelid(stmt->relation, AccessShareLock, true);
126126

127127
validateCurrentEventTriggerState(true);
128128
tdeCurrentCreateEvent.tid = GetCurrentFullTransactionId();
@@ -132,16 +132,16 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
132132

133133
if (relationId != InvalidOid)
134134
{
135-
LOCKMODE lockmode = AccessShareLock; /* TODO. Verify lock mode? */
136-
Relation rel = table_open(relationId, lockmode);
135+
Relation rel = table_open(relationId, NoLock);
137136

138137
if (rel->rd_rel->relam == get_tde_table_am_oid())
139138
{
140139
/* We are creating the index on encrypted table */
141140
/* set the global state */
142141
tdeCurrentCreateEvent.encryptMode = true;
143142
}
144-
table_close(rel, lockmode);
143+
144+
table_close(rel, NoLock);
145145

146146
if (tdeCurrentCreateEvent.encryptMode)
147147
{
@@ -181,7 +181,7 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
181181
{
182182
AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
183183
ListCell *lcmd;
184-
Oid relationId = RangeVarGetRelid(stmt->relation, NoLock, true);
184+
Oid relationId = RangeVarGetRelid(stmt->relation, AccessShareLock, true);
185185

186186
validateCurrentEventTriggerState(true);
187187
tdeCurrentCreateEvent.tid = GetCurrentFullTransactionId();
@@ -216,8 +216,7 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
216216

217217
if (relationId != InvalidOid)
218218
{
219-
LOCKMODE lockmode = AccessShareLock;
220-
Relation rel = relation_open(relationId, lockmode);
219+
Relation rel = relation_open(relationId, NoLock);
221220

222221
if (rel->rd_rel->relam == get_tde_table_am_oid())
223222
{
@@ -227,7 +226,8 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
227226
*/
228227
tdeCurrentCreateEvent.encryptMode = true;
229228
}
230-
relation_close(rel, lockmode);
229+
230+
relation_close(rel, NoLock);
231231

232232
if (tdeCurrentCreateEvent.encryptMode)
233233
{

0 commit comments

Comments
 (0)