Skip to content

Commit bf82f43

Browse files
committed
Followup fixes for transaction_timeout
Don't deal with transaction timeout in PostgresMain(). Instead, release transaction timeout activated by StartTransaction() in CommitTransaction()/AbortTransaction()/PrepareTransaction(). Deal with both enabling and disabling transaction timeout in assign_transaction_timeout(). Also, remove potentially flaky timeouts-long isolation test, which has no guarantees to pass on slow/busy machines. Reported-by: Andres Freund Discussion: https://postgr.es/m/20240215230856.pc6k57tqxt7fhldm%40awork3.anarazel.de
1 parent 51efe38 commit bf82f43

File tree

4 files changed

+23
-56
lines changed

4 files changed

+23
-56
lines changed

src/backend/access/transam/xact.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,10 @@ CommitTransaction(void)
22622262
s->state = TRANS_COMMIT;
22632263
s->parallelModeLevel = 0;
22642264

2265+
/* Disable transaction timeout */
2266+
if (TransactionTimeout > 0)
2267+
disable_timeout(TRANSACTION_TIMEOUT, false);
2268+
22652269
if (!is_parallel_worker)
22662270
{
22672271
/*
@@ -2535,6 +2539,10 @@ PrepareTransaction(void)
25352539
*/
25362540
s->state = TRANS_PREPARE;
25372541

2542+
/* Disable transaction timeout */
2543+
if (TransactionTimeout > 0)
2544+
disable_timeout(TRANSACTION_TIMEOUT, false);
2545+
25382546
prepared_at = GetCurrentTimestamp();
25392547

25402548
/*
@@ -2707,6 +2715,10 @@ AbortTransaction(void)
27072715
/* Prevent cancel/die interrupt while cleaning up */
27082716
HOLD_INTERRUPTS();
27092717

2718+
/* Disable transaction timeout */
2719+
if (TransactionTimeout > 0)
2720+
disable_timeout(TRANSACTION_TIMEOUT, false);
2721+
27102722
/* Make sure we have a valid memory context and resource owner */
27112723
AtAbort_Memory();
27122724
AtAbort_ResourceOwner();

src/backend/tcop/postgres.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3647,9 +3647,17 @@ check_log_stats(bool *newval, void **extra, GucSource source)
36473647
void
36483648
assign_transaction_timeout(int newval, void *extra)
36493649
{
3650-
if (TransactionTimeout <= 0 &&
3651-
get_timeout_active(TRANSACTION_TIMEOUT))
3652-
disable_timeout(TRANSACTION_TIMEOUT, false);
3650+
if (IsTransactionState())
3651+
{
3652+
/*
3653+
* If transaction_timeout GUC has changes within the transaction block
3654+
* enable or disable the timer correspondingly.
3655+
*/
3656+
if (newval > 0 && !get_timeout_active(TRANSACTION_TIMEOUT))
3657+
enable_timeout_after(TRANSACTION_TIMEOUT, newval);
3658+
else if (newval <= 0 && get_timeout_active(TRANSACTION_TIMEOUT))
3659+
disable_timeout(TRANSACTION_TIMEOUT, false);
3660+
}
36533661
}
36543662

36553663

@@ -4510,11 +4518,6 @@ PostgresMain(const char *dbname, const char *username)
45104518
enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
45114519
IdleInTransactionSessionTimeout);
45124520
}
4513-
4514-
/* Schedule or reschedule transaction timeout */
4515-
if (TransactionTimeout > 0 && !get_timeout_active(TRANSACTION_TIMEOUT))
4516-
enable_timeout_after(TRANSACTION_TIMEOUT,
4517-
TransactionTimeout);
45184521
}
45194522
else if (IsTransactionOrTransactionBlock())
45204523
{
@@ -4529,11 +4532,6 @@ PostgresMain(const char *dbname, const char *username)
45294532
enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
45304533
IdleInTransactionSessionTimeout);
45314534
}
4532-
4533-
/* Schedule or reschedule transaction timeout */
4534-
if (TransactionTimeout > 0 && !get_timeout_active(TRANSACTION_TIMEOUT))
4535-
enable_timeout_after(TRANSACTION_TIMEOUT,
4536-
TransactionTimeout);
45374535
}
45384536
else
45394537
{
@@ -4586,13 +4584,6 @@ PostgresMain(const char *dbname, const char *username)
45864584
enable_timeout_after(IDLE_SESSION_TIMEOUT,
45874585
IdleSessionTimeout);
45884586
}
4589-
4590-
/*
4591-
* If GUC is changed then it's handled in
4592-
* assign_transaction_timeout().
4593-
*/
4594-
if (TransactionTimeout > 0 && get_timeout_active(TRANSACTION_TIMEOUT))
4595-
disable_timeout(TRANSACTION_TIMEOUT, false);
45964587
}
45974588

45984589
/* Report any recently-changed GUC options */

src/test/isolation/isolation_schedule

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ test: sequence-ddl
8989
test: async-notify
9090
test: vacuum-no-cleanup-lock
9191
test: timeouts
92-
test: timeouts-long
9392
test: vacuum-concurrent-drop
9493
test: vacuum-conflict
9594
test: vacuum-skip-locked

src/test/isolation/specs/timeouts-long.spec

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)