Skip to content

Commit

Permalink
Fix assigning subtransaction logical xids
Browse files Browse the repository at this point in the history
  • Loading branch information
akorotkov committed Nov 29, 2024
1 parent 18c749d commit b3555db
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
23 changes: 23 additions & 0 deletions expected/subtransactions.out
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,29 @@ FETCH FROM abc;
-------
(0 rows)

ROLLBACK;
-- Check for logical xid overflow
BEGIN;
DO $$
BEGIN
FOR i IN 1..1000 LOOP
BEGIN
RAISE reading_sql_data_not_permitted;
EXCEPTION WHEN reading_sql_data_not_permitted THEN END;
END LOOP;
END;$$;
ROLLBACK;
BEGIN;
DO $$
BEGIN
FOR i IN 1..100000 LOOP
BEGIN
RAISE reading_sql_data_not_permitted;
EXCEPTION WHEN reading_sql_data_not_permitted THEN END;
END LOOP;
END;$$;
ERROR: not enough logical xids
CONTEXT: PL/pgSQL function inline_code_block line 4 during statement block entry
ROLLBACK;
DROP EXTENSION orioledb CASCADE;
NOTICE: drop cascades to table o_subtrans
Expand Down
22 changes: 22 additions & 0 deletions sql/subtransactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,28 @@ ROLLBACK TO s1;
FETCH FROM abc;
ROLLBACK;

-- Check for logical xid overflow
BEGIN;
DO $$
BEGIN
FOR i IN 1..1000 LOOP
BEGIN
RAISE reading_sql_data_not_permitted;
EXCEPTION WHEN reading_sql_data_not_permitted THEN END;
END LOOP;
END;$$;
ROLLBACK;
BEGIN;
DO $$
BEGIN
FOR i IN 1..100000 LOOP
BEGIN
RAISE reading_sql_data_not_permitted;
EXCEPTION WHEN reading_sql_data_not_permitted THEN END;
END LOOP;
END;$$;
ROLLBACK;

DROP EXTENSION orioledb CASCADE;
DROP SCHEMA subtransactions CASCADE;
RESET search_path;
7 changes: 6 additions & 1 deletion src/transam/oxid.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ acquire_logical_xid(void)
if (nloops > 16)
elog(ERROR, "not enough logical xids");
}
continue;
}
bitnum = pg_ceil_log2_32(bit);
Assert(bit == (1 << bitnum));
Expand Down Expand Up @@ -312,6 +313,10 @@ release_logical_xid(TransactionId xid)
void
assign_subtransaction_logical_xid(void)
{
TransactionId nextLogicalXid;

nextLogicalXid = acquire_logical_xid();

if (TransactionIdIsValid(logicalXid))
{
MemoryContext mcxt = MemoryContextSwitchTo(TopMemoryContext);
Expand All @@ -324,7 +329,7 @@ assign_subtransaction_logical_xid(void)
MemoryContextSwitchTo(mcxt);
}

logicalXid = acquire_logical_xid();
logicalXid = nextLogicalXid;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/transam/undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1565,9 +1565,9 @@ undo_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
{
case SUBXACT_EVENT_START_SUB:
(void) get_current_oxid();
add_subxact_undo_item(parentSubid);
prentLogicalXid = get_current_logical_xid();
assign_subtransaction_logical_xid();
add_subxact_undo_item(parentSubid);
add_savepoint_wal_record(parentSubid, prentLogicalXid);
break;
case SUBXACT_EVENT_COMMIT_SUB:
Expand Down

0 comments on commit b3555db

Please sign in to comment.