@@ -557,6 +557,12 @@ StartupCommitTs(void)
557
557
TransactionId xid = ShmemVariableCache -> nextXid ;
558
558
int pageno = TransactionIdToCTsPage (xid );
559
559
560
+ if (track_commit_timestamp )
561
+ {
562
+ ActivateCommitTs ();
563
+ return ;
564
+ }
565
+
560
566
LWLockAcquire (CommitTsControlLock , LW_EXCLUSIVE );
561
567
562
568
/*
@@ -569,16 +575,33 @@ StartupCommitTs(void)
569
575
570
576
/*
571
577
* This must be called ONCE during postmaster or standalone-backend startup,
572
- * when commit timestamp is enabled. Must be called after recovery has
573
- * finished.
578
+ * when commit timestamp is enabled, after recovery has finished.
579
+ */
580
+ void
581
+ CompleteCommitTsInitialization (void )
582
+ {
583
+ if (!track_commit_timestamp )
584
+ DeactivateCommitTs (true);
585
+ }
586
+
587
+ /*
588
+ * Activate this module whenever necessary.
589
+ * This must happen during postmaster or standalong-backend startup,
590
+ * or during WAL replay anytime the track_commit_timestamp setting is
591
+ * changed in the master.
592
+ *
593
+ * The reason why this SLRU needs separate activation/deactivation functions is
594
+ * that it can be enabled/disabled during start and the activation/deactivation
595
+ * on master is propagated to slave via replay. Other SLRUs don't have this
596
+ * property and they can be just initialized during normal startup.
574
597
*
575
598
* This is in charge of creating the currently active segment, if it's not
576
599
* already there. The reason for this is that the server might have been
577
600
* running with this module disabled for a while and thus might have skipped
578
601
* the normal creation point.
579
602
*/
580
603
void
581
- CompleteCommitTsInitialization (void )
604
+ ActivateCommitTs (void )
582
605
{
583
606
TransactionId xid = ShmemVariableCache -> nextXid ;
584
607
int pageno = TransactionIdToCTsPage (xid );
@@ -590,22 +613,6 @@ CompleteCommitTsInitialization(void)
590
613
CommitTsCtl -> shared -> latest_page_number = pageno ;
591
614
LWLockRelease (CommitTsControlLock );
592
615
593
- /*
594
- * If this module is not currently enabled, make sure we don't hand back
595
- * possibly-invalid data; also remove segments of old data.
596
- */
597
- if (!track_commit_timestamp )
598
- {
599
- LWLockAcquire (CommitTsLock , LW_EXCLUSIVE );
600
- ShmemVariableCache -> oldestCommitTs = InvalidTransactionId ;
601
- ShmemVariableCache -> newestCommitTs = InvalidTransactionId ;
602
- LWLockRelease (CommitTsLock );
603
-
604
- TruncateCommitTs (ReadNewTransactionId ());
605
-
606
- return ;
607
- }
608
-
609
616
/*
610
617
* If CommitTs is enabled, but it wasn't in the previous server run, we
611
618
* need to set the oldest and newest values to the next Xid; that way, we
@@ -640,6 +647,37 @@ CompleteCommitTsInitialization(void)
640
647
}
641
648
}
642
649
650
+ /*
651
+ * Deactivate this module.
652
+ *
653
+ * This must be called when the track_commit_timestamp parameter is turned off.
654
+ * This happens during postmaster or standalone-backend startup, or during WAL
655
+ * replay.
656
+ *
657
+ * Resets CommitTs into invalid state to make sure we don't hand back
658
+ * possibly-invalid data; also removes segments of old data.
659
+ */
660
+ void
661
+ DeactivateCommitTs (bool do_wal )
662
+ {
663
+ TransactionId xid = ShmemVariableCache -> nextXid ;
664
+ int pageno = TransactionIdToCTsPage (xid );
665
+
666
+ /*
667
+ * Re-Initialize our idea of the latest page number.
668
+ */
669
+ LWLockAcquire (CommitTsControlLock , LW_EXCLUSIVE );
670
+ CommitTsCtl -> shared -> latest_page_number = pageno ;
671
+ LWLockRelease (CommitTsControlLock );
672
+
673
+ LWLockAcquire (CommitTsLock , LW_EXCLUSIVE );
674
+ ShmemVariableCache -> oldestCommitTs = InvalidTransactionId ;
675
+ ShmemVariableCache -> newestCommitTs = InvalidTransactionId ;
676
+ LWLockRelease (CommitTsLock );
677
+
678
+ TruncateCommitTs (ReadNewTransactionId (), do_wal );
679
+ }
680
+
643
681
/*
644
682
* This must be called ONCE during postmaster or standalone-backend shutdown
645
683
*/
@@ -705,7 +743,7 @@ ExtendCommitTs(TransactionId newestXact)
705
743
* Note that we don't need to flush XLOG here.
706
744
*/
707
745
void
708
- TruncateCommitTs (TransactionId oldestXact )
746
+ TruncateCommitTs (TransactionId oldestXact , bool do_wal )
709
747
{
710
748
int cutoffPage ;
711
749
@@ -721,7 +759,8 @@ TruncateCommitTs(TransactionId oldestXact)
721
759
return ; /* nothing to remove */
722
760
723
761
/* Write XLOG record */
724
- WriteTruncateXlogRec (cutoffPage );
762
+ if (do_wal )
763
+ WriteTruncateXlogRec (cutoffPage );
725
764
726
765
/* Now we can remove the old CommitTs segment(s) */
727
766
SimpleLruTruncate (CommitTsCtl , cutoffPage );
0 commit comments