LCOV - differential code coverage report
Current view: top level - src/backend/storage/ipc - standby.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 91.6 % 391 358 33 358
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 31 31 31
Baseline: lcov-20250906-005545-baseline Branches: 69.4 % 222 154 68 154
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 6 6 6
(360..) days: 91.4 % 385 352 33 352
Function coverage date bins:
(360..) days: 100.0 % 31 31 31
Branch coverage date bins:
(360..) days: 69.4 % 222 154 68 154

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * standby.c
                                  4                 :                :  *    Misc functions used in Hot Standby mode.
                                  5                 :                :  *
                                  6                 :                :  *  All functions for handling RM_STANDBY_ID, which relate to
                                  7                 :                :  *  AccessExclusiveLocks and starting snapshots for Hot Standby mode.
                                  8                 :                :  *  Plus conflict recovery processing.
                                  9                 :                :  *
                                 10                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 11                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 12                 :                :  *
                                 13                 :                :  * IDENTIFICATION
                                 14                 :                :  *    src/backend/storage/ipc/standby.c
                                 15                 :                :  *
                                 16                 :                :  *-------------------------------------------------------------------------
                                 17                 :                :  */
                                 18                 :                : #include "postgres.h"
                                 19                 :                : #include "access/transam.h"
                                 20                 :                : #include "access/twophase.h"
                                 21                 :                : #include "access/xact.h"
                                 22                 :                : #include "access/xloginsert.h"
                                 23                 :                : #include "access/xlogrecovery.h"
                                 24                 :                : #include "access/xlogutils.h"
                                 25                 :                : #include "miscadmin.h"
                                 26                 :                : #include "pgstat.h"
                                 27                 :                : #include "replication/slot.h"
                                 28                 :                : #include "storage/bufmgr.h"
                                 29                 :                : #include "storage/proc.h"
                                 30                 :                : #include "storage/procarray.h"
                                 31                 :                : #include "storage/sinvaladt.h"
                                 32                 :                : #include "storage/standby.h"
                                 33                 :                : #include "utils/hsearch.h"
                                 34                 :                : #include "utils/injection_point.h"
                                 35                 :                : #include "utils/ps_status.h"
                                 36                 :                : #include "utils/timeout.h"
                                 37                 :                : #include "utils/timestamp.h"
                                 38                 :                : 
                                 39                 :                : /* User-settable GUC parameters */
                                 40                 :                : int         max_standby_archive_delay = 30 * 1000;
                                 41                 :                : int         max_standby_streaming_delay = 30 * 1000;
                                 42                 :                : bool        log_recovery_conflict_waits = false;
                                 43                 :                : 
                                 44                 :                : /*
                                 45                 :                :  * Keep track of all the exclusive locks owned by original transactions.
                                 46                 :                :  * For each known exclusive lock, there is a RecoveryLockEntry in the
                                 47                 :                :  * RecoveryLockHash hash table.  All RecoveryLockEntrys belonging to a
                                 48                 :                :  * given XID are chained together so that we can find them easily.
                                 49                 :                :  * For each original transaction that is known to have any such locks,
                                 50                 :                :  * there is a RecoveryLockXidEntry in the RecoveryLockXidHash hash table,
                                 51                 :                :  * which stores the head of the chain of its locks.
                                 52                 :                :  */
                                 53                 :                : typedef struct RecoveryLockEntry
                                 54                 :                : {
                                 55                 :                :     xl_standby_lock key;        /* hash key: xid, dbOid, relOid */
                                 56                 :                :     struct RecoveryLockEntry *next; /* chain link */
                                 57                 :                : } RecoveryLockEntry;
                                 58                 :                : 
                                 59                 :                : typedef struct RecoveryLockXidEntry
                                 60                 :                : {
                                 61                 :                :     TransactionId xid;          /* hash key -- must be first */
                                 62                 :                :     struct RecoveryLockEntry *head; /* chain head */
                                 63                 :                : } RecoveryLockXidEntry;
                                 64                 :                : 
                                 65                 :                : static HTAB *RecoveryLockHash = NULL;
                                 66                 :                : static HTAB *RecoveryLockXidHash = NULL;
                                 67                 :                : 
                                 68                 :                : /* Flags set by timeout handlers */
                                 69                 :                : static volatile sig_atomic_t got_standby_deadlock_timeout = false;
                                 70                 :                : static volatile sig_atomic_t got_standby_delay_timeout = false;
                                 71                 :                : static volatile sig_atomic_t got_standby_lock_timeout = false;
                                 72                 :                : 
                                 73                 :                : static void ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
                                 74                 :                :                                                    ProcSignalReason reason,
                                 75                 :                :                                                    uint32 wait_event_info,
                                 76                 :                :                                                    bool report_waiting);
                                 77                 :                : static void SendRecoveryConflictWithBufferPin(ProcSignalReason reason);
                                 78                 :                : static XLogRecPtr LogCurrentRunningXacts(RunningTransactions CurrRunningXacts);
                                 79                 :                : static void LogAccessExclusiveLocks(int nlocks, xl_standby_lock *locks);
                                 80                 :                : static const char *get_recovery_conflict_desc(ProcSignalReason reason);
                                 81                 :                : 
                                 82                 :                : /*
                                 83                 :                :  * InitRecoveryTransactionEnvironment
                                 84                 :                :  *      Initialize tracking of our primary's in-progress transactions.
                                 85                 :                :  *
                                 86                 :                :  * We need to issue shared invalidations and hold locks. Holding locks
                                 87                 :                :  * means others may want to wait on us, so we need to make a lock table
                                 88                 :                :  * vxact entry like a real transaction. We could create and delete
                                 89                 :                :  * lock table entries for each transaction but its simpler just to create
                                 90                 :                :  * one permanent entry and leave it there all the time. Locks are then
                                 91                 :                :  * acquired and released as needed. Yes, this means you can see the
                                 92                 :                :  * Startup process in pg_locks once we have run this.
                                 93                 :                :  */
                                 94                 :                : void
 5740 simon@2ndQuadrant.co       95                 :CBC         100 : InitRecoveryTransactionEnvironment(void)
                                 96                 :                : {
                                 97                 :                :     VirtualTransactionId vxid;
                                 98                 :                :     HASHCTL     hash_ctl;
                                 99                 :                : 
 1066 tgl@sss.pgh.pa.us         100         [ -  + ]:            100 :     Assert(RecoveryLockHash == NULL);   /* don't run this twice */
                                101                 :                : 
                                102                 :                :     /*
                                103                 :                :      * Initialize the hash tables for tracking the locks held by each
                                104                 :                :      * transaction.
                                105                 :                :      */
                                106                 :            100 :     hash_ctl.keysize = sizeof(xl_standby_lock);
                                107                 :            100 :     hash_ctl.entrysize = sizeof(RecoveryLockEntry);
                                108                 :            100 :     RecoveryLockHash = hash_create("RecoveryLockHash",
                                109                 :                :                                    64,
                                110                 :                :                                    &hash_ctl,
                                111                 :                :                                    HASH_ELEM | HASH_BLOBS);
 2629 tmunro@postgresql.or      112                 :            100 :     hash_ctl.keysize = sizeof(TransactionId);
 1066 tgl@sss.pgh.pa.us         113                 :            100 :     hash_ctl.entrysize = sizeof(RecoveryLockXidEntry);
                                114                 :            100 :     RecoveryLockXidHash = hash_create("RecoveryLockXidHash",
                                115                 :                :                                       64,
                                116                 :                :                                       &hash_ctl,
                                117                 :                :                                       HASH_ELEM | HASH_BLOBS);
                                118                 :                : 
                                119                 :                :     /*
                                120                 :                :      * Initialize shared invalidation management for Startup process, being
                                121                 :                :      * careful to register ourselves as a sendOnly process so we don't need to
                                122                 :                :      * read messages, nor will we get signaled when the queue starts filling
                                123                 :                :      * up.
                                124                 :                :      */
 5740 simon@2ndQuadrant.co      125                 :            100 :     SharedInvalBackendInit(true);
                                126                 :                : 
                                127                 :                :     /*
                                128                 :                :      * Lock a virtual transaction id for Startup process.
                                129                 :                :      *
                                130                 :                :      * We need to do GetNextLocalTransactionId() because
                                131                 :                :      * SharedInvalBackendInit() leaves localTransactionId invalid and the lock
                                132                 :                :      * manager doesn't like that at all.
                                133                 :                :      *
                                134                 :                :      * Note that we don't need to run XactLockTableInsert() because nobody
                                135                 :                :      * needs to wait on xids. That sounds a little strange, but table locks
                                136                 :                :      * are held by vxids and row level locks are held by xids. All queries
                                137                 :                :      * hold AccessShareLocks so never block while we write or lock new rows.
                                138                 :                :      */
  552 heikki.linnakangas@i      139                 :            100 :     MyProc->vxid.procNumber = MyProcNumber;
                                140                 :            100 :     vxid.procNumber = MyProcNumber;
 5740 simon@2ndQuadrant.co      141                 :            100 :     vxid.localTransactionId = GetNextLocalTransactionId();
                                142                 :            100 :     VirtualXactLockTableInsert(vxid);
                                143                 :                : 
                                144                 :            100 :     standbyState = STANDBY_INITIALIZED;
                                145                 :            100 : }
                                146                 :                : 
                                147                 :                : /*
                                148                 :                :  * ShutdownRecoveryTransactionEnvironment
                                149                 :                :  *      Shut down transaction tracking
                                150                 :                :  *
                                151                 :                :  * Prepare to switch from hot standby mode to normal operation. Shut down
                                152                 :                :  * recovery-time transaction tracking.
                                153                 :                :  *
                                154                 :                :  * This must be called even in shutdown of startup process if transaction
                                155                 :                :  * tracking has been initialized. Otherwise some locks the tracked
                                156                 :                :  * transactions were holding will not be released and may interfere with
                                157                 :                :  * the processes still running (but will exit soon later) at the exit of
                                158                 :                :  * startup process.
                                159                 :                :  */
                                160                 :                : void
                                161                 :            147 : ShutdownRecoveryTransactionEnvironment(void)
                                162                 :                : {
                                163                 :                :     /*
                                164                 :                :      * Do nothing if RecoveryLockHash is NULL because that means that
                                165                 :                :      * transaction tracking has not yet been initialized or has already been
                                166                 :                :      * shut down.  This makes it safe to have possibly-redundant calls of this
                                167                 :                :      * function during process exit.
                                168                 :                :      */
 1066 tgl@sss.pgh.pa.us         169         [ +  + ]:            147 :     if (RecoveryLockHash == NULL)
 1614 fujii@postgresql.org      170                 :             47 :         return;
                                171                 :                : 
                                172                 :                :     /* Mark all tracked in-progress transactions as finished. */
 5740 simon@2ndQuadrant.co      173                 :            100 :     ExpireAllKnownAssignedTransactionIds();
                                174                 :                : 
                                175                 :                :     /* Release all locks the tracked transactions were holding */
                                176                 :            100 :     StandbyReleaseAllLocks();
                                177                 :                : 
                                178                 :                :     /* Destroy the lock hash tables. */
 1066 tgl@sss.pgh.pa.us         179                 :            100 :     hash_destroy(RecoveryLockHash);
                                180                 :            100 :     hash_destroy(RecoveryLockXidHash);
                                181                 :            100 :     RecoveryLockHash = NULL;
                                182                 :            100 :     RecoveryLockXidHash = NULL;
                                183                 :                : 
                                184                 :                :     /* Cleanup our VirtualTransaction */
 4664 simon@2ndQuadrant.co      185                 :            100 :     VirtualXactLockTableCleanup();
                                186                 :                : }
                                187                 :                : 
                                188                 :                : 
                                189                 :                : /*
                                190                 :                :  * -----------------------------------------------------
                                191                 :                :  *      Standby wait timers and backend cancel logic
                                192                 :                :  * -----------------------------------------------------
                                193                 :                :  */
                                194                 :                : 
                                195                 :                : /*
                                196                 :                :  * Determine the cutoff time at which we want to start canceling conflicting
                                197                 :                :  * transactions.  Returns zero (a time safely in the past) if we are willing
                                198                 :                :  * to wait forever.
                                199                 :                :  */
                                200                 :                : static TimestampTz
 5544 tgl@sss.pgh.pa.us         201                 :             28 : GetStandbyLimitTime(void)
                                202                 :                : {
                                203                 :                :     TimestampTz rtime;
                                204                 :                :     bool        fromStream;
                                205                 :                : 
                                206                 :                :     /*
                                207                 :                :      * The cutoff time is the last WAL data receipt time plus the appropriate
                                208                 :                :      * delay variable.  Delay of -1 means wait forever.
                                209                 :                :      */
                                210                 :             28 :     GetXLogReceiptTime(&rtime, &fromStream);
                                211         [ +  - ]:             28 :     if (fromStream)
                                212                 :                :     {
                                213         [ -  + ]:             28 :         if (max_standby_streaming_delay < 0)
 5544 tgl@sss.pgh.pa.us         214                 :UBC           0 :             return 0;           /* wait forever */
 5544 tgl@sss.pgh.pa.us         215                 :CBC          28 :         return TimestampTzPlusMilliseconds(rtime, max_standby_streaming_delay);
                                216                 :                :     }
                                217                 :                :     else
                                218                 :                :     {
 5544 tgl@sss.pgh.pa.us         219         [ #  # ]:UBC           0 :         if (max_standby_archive_delay < 0)
                                220                 :              0 :             return 0;           /* wait forever */
                                221                 :              0 :         return TimestampTzPlusMilliseconds(rtime, max_standby_archive_delay);
                                222                 :                :     }
                                223                 :                : }
                                224                 :                : 
                                225                 :                : #define STANDBY_INITIAL_WAIT_US  1000
                                226                 :                : static int  standbyWait_us = STANDBY_INITIAL_WAIT_US;
                                227                 :                : 
                                228                 :                : /*
                                229                 :                :  * Standby wait logic for ResolveRecoveryConflictWithVirtualXIDs.
                                230                 :                :  * We wait here for a while then return. If we decide we can't wait any
                                231                 :                :  * more then we return true, if we can wait some more return false.
                                232                 :                :  */
                                233                 :                : static bool
 1982 fujii@postgresql.org      234                 :CBC          15 : WaitExceedsMaxStandbyDelay(uint32 wait_event_info)
                                235                 :                : {
                                236                 :                :     TimestampTz ltime;
                                237                 :                : 
 3145 simon@2ndQuadrant.co      238         [ -  + ]:             15 :     CHECK_FOR_INTERRUPTS();
                                239                 :                : 
                                240                 :                :     /* Are we past the limit time? */
 5544 tgl@sss.pgh.pa.us         241                 :             15 :     ltime = GetStandbyLimitTime();
                                242   [ +  -  +  + ]:             15 :     if (ltime && GetCurrentTimestamp() >= ltime)
 5740 simon@2ndQuadrant.co      243                 :              3 :         return true;
                                244                 :                : 
                                245                 :                :     /*
                                246                 :                :      * Sleep a bit (this is essential to avoid busy-waiting).
                                247                 :                :      */
 1982 fujii@postgresql.org      248                 :             12 :     pgstat_report_wait_start(wait_event_info);
 5740 simon@2ndQuadrant.co      249                 :             12 :     pg_usleep(standbyWait_us);
 1982 fujii@postgresql.org      250                 :             12 :     pgstat_report_wait_end();
                                251                 :                : 
                                252                 :                :     /*
                                253                 :                :      * Progressively increase the sleep times, but not to more than 1s, since
                                254                 :                :      * pg_usleep isn't interruptible on some platforms.
                                255                 :                :      */
 5740 simon@2ndQuadrant.co      256                 :             12 :     standbyWait_us *= 2;
                                257         [ -  + ]:             12 :     if (standbyWait_us > 1000000)
 5740 simon@2ndQuadrant.co      258                 :UBC           0 :         standbyWait_us = 1000000;
                                259                 :                : 
 5740 simon@2ndQuadrant.co      260                 :CBC          12 :     return false;
                                261                 :                : }
                                262                 :                : 
                                263                 :                : /*
                                264                 :                :  * Log the recovery conflict.
                                265                 :                :  *
                                266                 :                :  * wait_start is the timestamp when the caller started to wait.
                                267                 :                :  * now is the timestamp when this function has been called.
                                268                 :                :  * wait_list is the list of virtual transaction ids assigned to
                                269                 :                :  * conflicting processes. still_waiting indicates whether
                                270                 :                :  * the startup process is still waiting for the recovery conflict
                                271                 :                :  * to be resolved or not.
                                272                 :                :  */
                                273                 :                : void
 1702 fujii@postgresql.org      274                 :             10 : LogRecoveryConflict(ProcSignalReason reason, TimestampTz wait_start,
                                275                 :                :                     TimestampTz now, VirtualTransactionId *wait_list,
                                276                 :                :                     bool still_waiting)
                                277                 :                : {
                                278                 :                :     long        secs;
                                279                 :                :     int         usecs;
                                280                 :                :     long        msecs;
                                281                 :                :     StringInfoData buf;
                                282                 :             10 :     int         nprocs = 0;
                                283                 :                : 
                                284                 :                :     /*
                                285                 :                :      * There must be no conflicting processes when the recovery conflict has
                                286                 :                :      * already been resolved.
                                287                 :                :      */
 1697                           288   [ +  +  -  + ]:             10 :     Assert(still_waiting || wait_list == NULL);
                                289                 :                : 
 1702                           290                 :             10 :     TimestampDifference(wait_start, now, &secs, &usecs);
                                291                 :             10 :     msecs = secs * 1000 + usecs / 1000;
                                292                 :             10 :     usecs = usecs % 1000;
                                293                 :                : 
                                294         [ +  + ]:             10 :     if (wait_list)
                                295                 :                :     {
                                296                 :                :         VirtualTransactionId *vxids;
                                297                 :                : 
                                298                 :                :         /* Construct a string of list of the conflicting processes */
                                299                 :              3 :         vxids = wait_list;
                                300         [ +  + ]:              6 :         while (VirtualTransactionIdIsValid(*vxids))
                                301                 :                :         {
  552 heikki.linnakangas@i      302                 :              3 :             PGPROC     *proc = ProcNumberGetProc(vxids->procNumber);
                                303                 :                : 
                                304                 :                :             /* proc can be NULL if the target backend is not active */
 1702 fujii@postgresql.org      305         [ +  - ]:              3 :             if (proc)
                                306                 :                :             {
                                307         [ +  - ]:              3 :                 if (nprocs == 0)
                                308                 :                :                 {
                                309                 :              3 :                     initStringInfo(&buf);
                                310                 :              3 :                     appendStringInfo(&buf, "%d", proc->pid);
                                311                 :                :                 }
                                312                 :                :                 else
 1702 fujii@postgresql.org      313                 :UBC           0 :                     appendStringInfo(&buf, ", %d", proc->pid);
                                314                 :                : 
 1702 fujii@postgresql.org      315                 :CBC           3 :                 nprocs++;
                                316                 :                :             }
                                317                 :                : 
                                318                 :              3 :             vxids++;
                                319                 :                :         }
                                320                 :                :     }
                                321                 :                : 
                                322                 :                :     /*
                                323                 :                :      * If wait_list is specified, report the list of PIDs of active
                                324                 :                :      * conflicting backends in a detail message. Note that if all the backends
                                325                 :                :      * in the list are not active, no detail message is logged.
                                326                 :                :      */
 1697                           327         [ +  + ]:             10 :     if (still_waiting)
                                328                 :                :     {
                                329   [ +  -  +  + ]:              5 :         ereport(LOG,
                                330                 :                :                 errmsg("recovery still waiting after %ld.%03d ms: %s",
                                331                 :                :                        msecs, usecs, get_recovery_conflict_desc(reason)),
                                332                 :                :                 nprocs > 0 ? errdetail_log_plural("Conflicting process: %s.",
                                333                 :                :                                                   "Conflicting processes: %s.",
                                334                 :                :                                                   nprocs, buf.data) : 0);
                                335                 :                :     }
                                336                 :                :     else
                                337                 :                :     {
                                338         [ +  - ]:              5 :         ereport(LOG,
                                339                 :                :                 errmsg("recovery finished waiting after %ld.%03d ms: %s",
                                340                 :                :                        msecs, usecs, get_recovery_conflict_desc(reason)));
                                341                 :                :     }
                                342                 :                : 
 1702                           343         [ +  + ]:             10 :     if (nprocs > 0)
                                344                 :              3 :         pfree(buf.data);
                                345                 :             10 : }
                                346                 :                : 
                                347                 :                : /*
                                348                 :                :  * This is the main executioner for any query backend that conflicts with
                                349                 :                :  * recovery processing. Judgement has already been passed on it within
                                350                 :                :  * a specific rmgr. Here we just issue the orders to the procs. The procs
                                351                 :                :  * then throw the required error as instructed.
                                352                 :                :  *
                                353                 :                :  * If report_waiting is true, "waiting" is reported in PS display and the
                                354                 :                :  * wait for recovery conflict is reported in the log, if necessary. If
                                355                 :                :  * the caller is responsible for reporting them, report_waiting should be
                                356                 :                :  * false. Otherwise, both the caller and this function report the same
                                357                 :                :  * thing unexpectedly.
                                358                 :                :  */
                                359                 :                : static void
 5740 simon@2ndQuadrant.co      360                 :          13584 : ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
                                361                 :                :                                        ProcSignalReason reason, uint32 wait_event_info,
                                362                 :                :                                        bool report_waiting)
                                363                 :                : {
 2006 fujii@postgresql.org      364                 :          13584 :     TimestampTz waitStart = 0;
  929 drowley@postgresql.o      365                 :          13584 :     bool        waiting = false;
 1702 fujii@postgresql.org      366                 :          13584 :     bool        logged_recovery_conflict = false;
                                367                 :                : 
                                368                 :                :     /* Fast exit, to avoid a kernel call if there's no work to be done. */
 5377 rhaas@postgresql.org      369         [ +  + ]:          13584 :     if (!VirtualTransactionIdIsValid(*waitlist))
                                370                 :          13581 :         return;
                                371                 :                : 
                                372                 :                :     /* Set the wait start timestamp for reporting */
 1702 fujii@postgresql.org      373   [ +  +  -  +  :              3 :     if (report_waiting && (log_recovery_conflict_waits || update_process_title))
                                              -  - ]
 2006                           374                 :              2 :         waitStart = GetCurrentTimestamp();
                                375                 :                : 
 5377 rhaas@postgresql.org      376         [ +  + ]:              6 :     while (VirtualTransactionIdIsValid(*waitlist))
                                377                 :                :     {
                                378                 :                :         /* reset standbyWait_us for each xact we wait for */
 5740 simon@2ndQuadrant.co      379                 :              3 :         standbyWait_us = STANDBY_INITIAL_WAIT_US;
                                380                 :                : 
                                381                 :                :         /* wait until the virtual xid is gone */
 5147 rhaas@postgresql.org      382         [ +  + ]:             18 :         while (!VirtualXactLock(*waitlist, false))
                                383                 :                :         {
                                384                 :                :             /* Is it time to kill it? */
 1982 fujii@postgresql.org      385         [ +  + ]:             15 :             if (WaitExceedsMaxStandbyDelay(wait_event_info))
                                386                 :                :             {
                                387                 :                :                 pid_t       pid;
                                388                 :                : 
                                389                 :                :                 /*
                                390                 :                :                  * Now find out who to throw out of the balloon.
                                391                 :                :                  */
 5740 simon@2ndQuadrant.co      392         [ -  + ]:              3 :                 Assert(VirtualTransactionIdIsValid(*waitlist));
 5712                           393                 :              3 :                 pid = CancelVirtualTransaction(*waitlist, reason);
                                394                 :                : 
                                395                 :                :                 /*
                                396                 :                :                  * Wait a little bit for it to die so that we avoid flooding
                                397                 :                :                  * an unresponsive backend when system is heavily loaded.
                                398                 :                :                  */
 5740                           399         [ +  - ]:              3 :                 if (pid != 0)
 5606 tgl@sss.pgh.pa.us         400                 :              3 :                     pg_usleep(5000L);
                                401                 :                :             }
                                402                 :                : 
  929 drowley@postgresql.o      403   [ +  +  +  +  :             15 :             if (waitStart != 0 && (!logged_recovery_conflict || !waiting))
                                              +  - ]
                                404                 :                :             {
 1702 fujii@postgresql.org      405                 :             14 :                 TimestampTz now = 0;
                                406                 :                :                 bool        maybe_log_conflict;
                                407                 :                :                 bool        maybe_update_title;
                                408                 :                : 
                                409   [ +  -  +  + ]:             14 :                 maybe_log_conflict = (log_recovery_conflict_waits && !logged_recovery_conflict);
  929 drowley@postgresql.o      410   [ +  -  +  - ]:             14 :                 maybe_update_title = (update_process_title && !waiting);
                                411                 :                : 
                                412                 :                :                 /* Get the current timestamp if not report yet */
 1702 fujii@postgresql.org      413   [ +  +  +  - ]:             14 :                 if (maybe_log_conflict || maybe_update_title)
                                414                 :             14 :                     now = GetCurrentTimestamp();
                                415                 :                : 
                                416                 :                :                 /*
                                417                 :                :                  * Report via ps if we have been waiting for more than 500
                                418                 :                :                  * msec (should that be configurable?)
                                419                 :                :                  */
                                420   [ +  -  -  + ]:             28 :                 if (maybe_update_title &&
                                421                 :             14 :                     TimestampDifferenceExceeds(waitStart, now, 500))
                                422                 :                :                 {
  929 drowley@postgresql.o      423                 :UBC           0 :                     set_ps_display_suffix("waiting");
                                424                 :              0 :                     waiting = true;
                                425                 :                :                 }
                                426                 :                : 
                                427                 :                :                 /*
                                428                 :                :                  * Emit the log message if the startup process is waiting
                                429                 :                :                  * longer than deadlock_timeout for recovery conflict.
                                430                 :                :                  */
 1702 fujii@postgresql.org      431   [ +  +  +  + ]:CBC          22 :                 if (maybe_log_conflict &&
                                432                 :              8 :                     TimestampDifferenceExceeds(waitStart, now, DeadlockTimeout))
                                433                 :                :                 {
 1697                           434                 :              2 :                     LogRecoveryConflict(reason, waitStart, now, waitlist, true);
 1702                           435                 :              2 :                     logged_recovery_conflict = true;
                                436                 :                :                 }
                                437                 :                :             }
                                438                 :                :         }
                                439                 :                : 
                                440                 :                :         /* The virtual transaction is gone now, wait for the next one */
 5740 simon@2ndQuadrant.co      441                 :              3 :         waitlist++;
                                442                 :                :     }
                                443                 :                : 
                                444                 :                :     /*
                                445                 :                :      * Emit the log message if recovery conflict was resolved but the startup
                                446                 :                :      * process waited longer than deadlock_timeout for it.
                                447                 :                :      */
 1697 fujii@postgresql.org      448         [ +  + ]:              3 :     if (logged_recovery_conflict)
                                449                 :              2 :         LogRecoveryConflict(reason, waitStart, GetCurrentTimestamp(),
                                450                 :                :                             NULL, false);
                                451                 :                : 
                                452                 :                :     /* reset ps display to remove the suffix if we added one */
  929 drowley@postgresql.o      453         [ -  + ]:              3 :     if (waiting)
  929 drowley@postgresql.o      454                 :UBC           0 :         set_ps_display_remove_suffix();
                                455                 :                : 
                                456                 :                : }
                                457                 :                : 
                                458                 :                : /*
                                459                 :                :  * Generate whatever recovery conflicts are needed to eliminate snapshots that
                                460                 :                :  * might see XIDs <= snapshotConflictHorizon as still running.
                                461                 :                :  *
                                462                 :                :  * snapshotConflictHorizon cutoffs are our standard approach to generating
                                463                 :                :  * granular recovery conflicts.  Note that InvalidTransactionId values are
                                464                 :                :  * interpreted as "definitely don't need any conflicts" here, which is a
                                465                 :                :  * general convention that WAL records can (and often do) depend on.
                                466                 :                :  */
                                467                 :                : void
 1024 pg@bowt.ie                468                 :CBC       19137 : ResolveRecoveryConflictWithSnapshot(TransactionId snapshotConflictHorizon,
                                469                 :                :                                     bool isCatalogRel,
                                470                 :                :                                     RelFileLocator locator)
                                471                 :                : {
                                472                 :                :     VirtualTransactionId *backends;
                                473                 :                : 
                                474                 :                :     /*
                                475                 :                :      * If we get passed InvalidTransactionId then we do nothing (no conflict).
                                476                 :                :      *
                                477                 :                :      * This can happen when replaying already-applied WAL records after a
                                478                 :                :      * standby crash or restart, or when replaying an XLOG_HEAP2_VISIBLE
                                479                 :                :      * record that marks as frozen a page which was already all-visible.  It's
                                480                 :                :      * also quite common with records generated during index deletion
                                481                 :                :      * (original execution of the deletion can reason that a recovery conflict
                                482                 :                :      * which is sufficient for the deletion operation must take place before
                                483                 :                :      * replay of the deletion record itself).
                                484                 :                :      */
                                485         [ +  + ]:          19137 :     if (!TransactionIdIsValid(snapshotConflictHorizon))
 5409 simon@2ndQuadrant.co      486                 :           5555 :         return;
                                487                 :                : 
  978 pg@bowt.ie                488         [ -  + ]:          13582 :     Assert(TransactionIdIsNormal(snapshotConflictHorizon));
 1024                           489                 :          13582 :     backends = GetConflictingVirtualXIDs(snapshotConflictHorizon,
                                490                 :                :                                          locator.dbOid);
 5714 simon@2ndQuadrant.co      491                 :          13582 :     ResolveRecoveryConflictWithVirtualXIDs(backends,
                                492                 :                :                                            PROCSIG_RECOVERY_CONFLICT_SNAPSHOT,
                                493                 :                :                                            WAIT_EVENT_RECOVERY_CONFLICT_SNAPSHOT,
                                494                 :                :                                            true);
                                495                 :                : 
                                496                 :                :     /*
                                497                 :                :      * Note that WaitExceedsMaxStandbyDelay() is not taken into account here
                                498                 :                :      * (as opposed to ResolveRecoveryConflictWithVirtualXIDs() above). That
                                499                 :                :      * seems OK, given that this kind of conflict should not normally be
                                500                 :                :      * reached, e.g. due to using a physical replication slot.
                                501                 :                :      */
  883 andres@anarazel.de        502   [ -  +  -  - ]:          13582 :     if (wal_level >= WAL_LEVEL_LOGICAL && isCatalogRel)
  883 andres@anarazel.de        503                 :UBC           0 :         InvalidateObsoleteReplicationSlots(RS_INVAL_HORIZON, 0, locator.dbOid,
                                504                 :                :                                            snapshotConflictHorizon);
                                505                 :                : }
                                506                 :                : 
                                507                 :                : /*
                                508                 :                :  * Variant of ResolveRecoveryConflictWithSnapshot that works with
                                509                 :                :  * FullTransactionId values
                                510                 :                :  */
                                511                 :                : void
 1024 pg@bowt.ie                512                 :CBC          90 : ResolveRecoveryConflictWithSnapshotFullXid(FullTransactionId snapshotConflictHorizon,
                                513                 :                :                                            bool isCatalogRel,
                                514                 :                :                                            RelFileLocator locator)
                                515                 :                : {
                                516                 :                :     /*
                                517                 :                :      * ResolveRecoveryConflictWithSnapshot operates on 32-bit TransactionIds,
                                518                 :                :      * so truncate the logged FullTransactionId.  If the logged value is very
                                519                 :                :      * old, so that XID wrap-around already happened on it, there can't be any
                                520                 :                :      * snapshots that still see it.
                                521                 :                :      */
 1655                           522                 :             90 :     FullTransactionId nextXid = ReadNextFullTransactionId();
                                523                 :                :     uint64      diff;
                                524                 :                : 
                                525                 :             90 :     diff = U64FromFullTransactionId(nextXid) -
 1024                           526                 :             90 :         U64FromFullTransactionId(snapshotConflictHorizon);
 1655                           527         [ +  - ]:             90 :     if (diff < MaxTransactionId / 2)
                                528                 :                :     {
                                529                 :                :         TransactionId truncated;
                                530                 :                : 
 1024                           531                 :             90 :         truncated = XidFromFullTransactionId(snapshotConflictHorizon);
  883 andres@anarazel.de        532                 :             90 :         ResolveRecoveryConflictWithSnapshot(truncated,
                                533                 :                :                                             isCatalogRel,
                                534                 :                :                                             locator);
                                535                 :                :     }
 1655 pg@bowt.ie                536                 :             90 : }
                                537                 :                : 
                                538                 :                : void
 5714 simon@2ndQuadrant.co      539                 :              1 : ResolveRecoveryConflictWithTablespace(Oid tsid)
                                540                 :                : {
                                541                 :                :     VirtualTransactionId *temp_file_users;
                                542                 :                : 
                                543                 :                :     /*
                                544                 :                :      * Standby users may be currently using this tablespace for their
                                545                 :                :      * temporary files. We only care about current users because
                                546                 :                :      * temp_tablespace parameter will just ignore tablespaces that no longer
                                547                 :                :      * exist.
                                548                 :                :      *
                                549                 :                :      * Ask everybody to cancel their queries immediately so we can ensure no
                                550                 :                :      * temp files remain and we can remove the tablespace. Nuke the entire
                                551                 :                :      * site from orbit, it's the only way to be sure.
                                552                 :                :      *
                                553                 :                :      * XXX: We could work out the pids of active backends using this
                                554                 :                :      * tablespace by examining the temp filenames in the directory. We would
                                555                 :                :      * then convert the pids into VirtualXIDs before attempting to cancel
                                556                 :                :      * them.
                                557                 :                :      *
                                558                 :                :      * We don't wait for commit because drop tablespace is non-transactional.
                                559                 :                :      */
                                560                 :              1 :     temp_file_users = GetConflictingVirtualXIDs(InvalidTransactionId,
                                561                 :                :                                                 InvalidOid);
                                562                 :              1 :     ResolveRecoveryConflictWithVirtualXIDs(temp_file_users,
                                563                 :                :                                            PROCSIG_RECOVERY_CONFLICT_TABLESPACE,
                                564                 :                :                                            WAIT_EVENT_RECOVERY_CONFLICT_TABLESPACE,
                                565                 :                :                                            true);
                                566                 :              1 : }
                                567                 :                : 
                                568                 :                : void
                                569                 :             12 : ResolveRecoveryConflictWithDatabase(Oid dbid)
                                570                 :                : {
                                571                 :                :     /*
                                572                 :                :      * We don't do ResolveRecoveryConflictWithVirtualXIDs() here since that
                                573                 :                :      * only waits for transactions and completely idle sessions would block
                                574                 :                :      * us. This is rare enough that we do this as simply as possible: no wait,
                                575                 :                :      * just force them off immediately.
                                576                 :                :      *
                                577                 :                :      * No locking is required here because we already acquired
                                578                 :                :      * AccessExclusiveLock. Anybody trying to connect while we do this will
                                579                 :                :      * block during InitPostgres() and then disconnect when they see the
                                580                 :                :      * database has been removed.
                                581                 :                :      */
                                582         [ +  + ]:             13 :     while (CountDBBackends(dbid) > 0)
                                583                 :                :     {
 5686                           584                 :              1 :         CancelDBBackends(dbid, PROCSIG_RECOVERY_CONFLICT_DATABASE, true);
                                585                 :                : 
                                586                 :                :         /*
                                587                 :                :          * Wait awhile for them to die so that we avoid flooding an
                                588                 :                :          * unresponsive backend when system is heavily loaded.
                                589                 :                :          */
 5714                           590                 :              1 :         pg_usleep(10000);
                                591                 :                :     }
                                592                 :             12 : }
                                593                 :                : 
                                594                 :                : /*
                                595                 :                :  * ResolveRecoveryConflictWithLock is called from ProcSleep()
                                596                 :                :  * to resolve conflicts with other backends holding relation locks.
                                597                 :                :  *
                                598                 :                :  * The WaitLatch sleep normally done in ProcSleep()
                                599                 :                :  * (when not InHotStandby) is performed here, for code clarity.
                                600                 :                :  *
                                601                 :                :  * We either resolve conflicts immediately or set a timeout to wake us at
                                602                 :                :  * the limit of our patience.
                                603                 :                :  *
                                604                 :                :  * Resolve conflicts by canceling to all backends holding a conflicting
                                605                 :                :  * lock.  As we are already queued to be granted the lock, no new lock
                                606                 :                :  * requests conflicting with ours will be granted in the meantime.
                                607                 :                :  *
                                608                 :                :  * We also must check for deadlocks involving the Startup process and
                                609                 :                :  * hot-standby backend processes. If deadlock_timeout is reached in
                                610                 :                :  * this function, all the backends holding the conflicting locks are
                                611                 :                :  * requested to check themselves for deadlocks.
                                612                 :                :  *
                                613                 :                :  * logging_conflict should be true if the recovery conflict has not been
                                614                 :                :  * logged yet even though logging is enabled. After deadlock_timeout is
                                615                 :                :  * reached and the request for deadlock check is sent, we wait again to
                                616                 :                :  * be signaled by the release of the lock if logging_conflict is false.
                                617                 :                :  * Otherwise we return without waiting again so that the caller can report
                                618                 :                :  * the recovery conflict. In this case, then, this function is called again
                                619                 :                :  * with logging_conflict=false (because the recovery conflict has already
                                620                 :                :  * been logged) and we will wait again for the lock to be released.
                                621                 :                :  */
                                622                 :                : void
 1702 fujii@postgresql.org      623                 :              4 : ResolveRecoveryConflictWithLock(LOCKTAG locktag, bool logging_conflict)
                                624                 :                : {
                                625                 :                :     TimestampTz ltime;
                                626                 :                :     TimestampTz now;
                                627                 :                : 
 3467 simon@2ndQuadrant.co      628         [ -  + ]:              4 :     Assert(InHotStandby);
                                629                 :                : 
                                630                 :              4 :     ltime = GetStandbyLimitTime();
 1664 fujii@postgresql.org      631                 :              4 :     now = GetCurrentTimestamp();
                                632                 :                : 
                                633                 :                :     /*
                                634                 :                :      * Update waitStart if first time through after the startup process
                                635                 :                :      * started waiting for the lock. It should not be updated every time
                                636                 :                :      * ResolveRecoveryConflictWithLock() is called during the wait.
                                637                 :                :      *
                                638                 :                :      * Use the current time obtained for comparison with ltime as waitStart
                                639                 :                :      * (i.e., the time when this process started waiting for the lock). Since
                                640                 :                :      * getting the current time newly can cause overhead, we reuse the
                                641                 :                :      * already-obtained time to avoid that overhead.
                                642                 :                :      *
                                643                 :                :      * Note that waitStart is updated without holding the lock table's
                                644                 :                :      * partition lock, to avoid the overhead by additional lock acquisition.
                                645                 :                :      * This can cause "waitstart" in pg_locks to become NULL for a very short
                                646                 :                :      * period of time after the wait started even though "granted" is false.
                                647                 :                :      * This is OK in practice because we can assume that users are likely to
                                648                 :                :      * look at "waitstart" when waiting for the lock for a long time.
                                649                 :                :      */
                                650         [ +  + ]:              4 :     if (pg_atomic_read_u64(&MyProc->waitStart) == 0)
                                651                 :              1 :         pg_atomic_write_u64(&MyProc->waitStart, now);
                                652                 :                : 
                                653   [ +  +  +  - ]:              4 :     if (now >= ltime && ltime != 0)
 3467 simon@2ndQuadrant.co      654                 :              1 :     {
                                655                 :                :         /*
                                656                 :                :          * We're already behind, so clear a path as quickly as possible.
                                657                 :                :          */
                                658                 :                :         VirtualTransactionId *backends;
                                659                 :                : 
 2349 alvherre@alvh.no-ip.      660                 :              1 :         backends = GetLockConflicts(&locktag, AccessExclusiveLock, NULL);
                                661                 :                : 
                                662                 :                :         /*
                                663                 :                :          * Prevent ResolveRecoveryConflictWithVirtualXIDs() from reporting
                                664                 :                :          * "waiting" in PS display by disabling its argument report_waiting
                                665                 :                :          * because the caller, WaitOnLock(), has already reported that.
                                666                 :                :          */
 5714 simon@2ndQuadrant.co      667                 :              1 :         ResolveRecoveryConflictWithVirtualXIDs(backends,
                                668                 :                :                                                PROCSIG_RECOVERY_CONFLICT_LOCK,
 1982 fujii@postgresql.org      669                 :              1 :                                                PG_WAIT_LOCK | locktag.locktag_type,
                                670                 :                :                                                false);
                                671                 :                :     }
                                672                 :                :     else
                                673                 :                :     {
                                674                 :                :         /*
                                675                 :                :          * Wait (or wait again) until ltime, and check for deadlocks as well
                                676                 :                :          * if we will be waiting longer than deadlock_timeout
                                677                 :                :          */
                                678                 :                :         EnableTimeoutParams timeouts[2];
 1704                           679                 :              3 :         int         cnt = 0;
                                680                 :                : 
                                681         [ +  - ]:              3 :         if (ltime != 0)
                                682                 :                :         {
                                683                 :              3 :             got_standby_lock_timeout = false;
                                684                 :              3 :             timeouts[cnt].id = STANDBY_LOCK_TIMEOUT;
                                685                 :              3 :             timeouts[cnt].type = TMPARAM_AT;
                                686                 :              3 :             timeouts[cnt].fin_time = ltime;
                                687                 :              3 :             cnt++;
                                688                 :                :         }
                                689                 :                : 
                                690                 :              3 :         got_standby_deadlock_timeout = false;
                                691                 :              3 :         timeouts[cnt].id = STANDBY_DEADLOCK_TIMEOUT;
                                692                 :              3 :         timeouts[cnt].type = TMPARAM_AFTER;
                                693                 :              3 :         timeouts[cnt].delay_ms = DeadlockTimeout;
                                694                 :              3 :         cnt++;
                                695                 :                : 
                                696                 :              3 :         enable_timeouts(timeouts, cnt);
                                697                 :                :     }
                                698                 :                : 
                                699                 :                :     /* Wait to be signaled by the release of the Relation Lock */
 3258 rhaas@postgresql.org      700                 :              4 :     ProcWaitForSignal(PG_WAIT_LOCK | locktag.locktag_type);
                                701                 :                : 
                                702                 :                :     /*
                                703                 :                :      * Exit if ltime is reached. Then all the backends holding conflicting
                                704                 :                :      * locks will be canceled in the next ResolveRecoveryConflictWithLock()
                                705                 :                :      * call.
                                706                 :                :      */
 1704 fujii@postgresql.org      707         [ -  + ]:              4 :     if (got_standby_lock_timeout)
 1704 fujii@postgresql.org      708                 :UBC           0 :         goto cleanup;
                                709                 :                : 
 1704 fujii@postgresql.org      710         [ +  + ]:CBC           4 :     if (got_standby_deadlock_timeout)
                                711                 :                :     {
                                712                 :                :         VirtualTransactionId *backends;
                                713                 :                : 
                                714                 :              2 :         backends = GetLockConflicts(&locktag, AccessExclusiveLock, NULL);
                                715                 :                : 
                                716                 :                :         /* Quick exit if there's no work to be done */
                                717         [ -  + ]:              2 :         if (!VirtualTransactionIdIsValid(*backends))
 1704 fujii@postgresql.org      718                 :UBC           0 :             goto cleanup;
                                719                 :                : 
                                720                 :                :         /*
                                721                 :                :          * Send signals to all the backends holding the conflicting locks, to
                                722                 :                :          * ask them to check themselves for deadlocks.
                                723                 :                :          */
 1704 fujii@postgresql.org      724         [ +  + ]:CBC           4 :         while (VirtualTransactionIdIsValid(*backends))
                                725                 :                :         {
                                726                 :              2 :             SignalVirtualTransaction(*backends,
                                727                 :                :                                      PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK,
                                728                 :                :                                      false);
                                729                 :              2 :             backends++;
                                730                 :                :         }
                                731                 :                : 
                                732                 :                :         /*
                                733                 :                :          * Exit if the recovery conflict has not been logged yet even though
                                734                 :                :          * logging is enabled, so that the caller can log that. Then
                                735                 :                :          * RecoveryConflictWithLock() is called again and we will wait again
                                736                 :                :          * for the lock to be released.
                                737                 :                :          */
 1702                           738         [ +  + ]:              2 :         if (logging_conflict)
                                739                 :              1 :             goto cleanup;
                                740                 :                : 
                                741                 :                :         /*
                                742                 :                :          * Wait again here to be signaled by the release of the Relation Lock,
                                743                 :                :          * to prevent the subsequent RecoveryConflictWithLock() from causing
                                744                 :                :          * deadlock_timeout and sending a request for deadlocks check again.
                                745                 :                :          * Otherwise the request continues to be sent every deadlock_timeout
                                746                 :                :          * until the relation locks are released or ltime is reached.
                                747                 :                :          */
 1704                           748                 :              1 :         got_standby_deadlock_timeout = false;
                                749                 :              1 :         ProcWaitForSignal(PG_WAIT_LOCK | locktag.locktag_type);
                                750                 :                :     }
                                751                 :                : 
                                752                 :              2 : cleanup:
                                753                 :                : 
                                754                 :                :     /*
                                755                 :                :      * Clear any timeout requests established above.  We assume here that the
                                756                 :                :      * Startup process doesn't have any other outstanding timeouts than those
                                757                 :                :      * used by this function. If that stops being true, we could cancel the
                                758                 :                :      * timeouts individually, but that'd be slower.
                                759                 :                :      */
 3467 simon@2ndQuadrant.co      760                 :              4 :     disable_all_timeouts(false);
 1704 fujii@postgresql.org      761                 :              4 :     got_standby_lock_timeout = false;
                                762                 :              4 :     got_standby_deadlock_timeout = false;
 5714 simon@2ndQuadrant.co      763                 :              4 : }
                                764                 :                : 
                                765                 :                : /*
                                766                 :                :  * ResolveRecoveryConflictWithBufferPin is called from LockBufferForCleanup()
                                767                 :                :  * to resolve conflicts with other backends holding buffer pins.
                                768                 :                :  *
                                769                 :                :  * The ProcWaitForSignal() sleep normally done in LockBufferForCleanup()
                                770                 :                :  * (when not InHotStandby) is performed here, for code clarity.
                                771                 :                :  *
                                772                 :                :  * We either resolve conflicts immediately or set a timeout to wake us at
                                773                 :                :  * the limit of our patience.
                                774                 :                :  *
                                775                 :                :  * Resolve conflicts by sending a PROCSIG signal to all backends to check if
                                776                 :                :  * they hold one of the buffer pins that is blocking Startup process. If so,
                                777                 :                :  * those backends will take an appropriate error action, ERROR or FATAL.
                                778                 :                :  *
                                779                 :                :  * We also must check for deadlocks.  Deadlocks occur because if queries
                                780                 :                :  * wait on a lock, that must be behind an AccessExclusiveLock, which can only
                                781                 :                :  * be cleared if the Startup process replays a transaction completion record.
                                782                 :                :  * If Startup process is also waiting then that is a deadlock. The deadlock
                                783                 :                :  * can occur if the query is waiting and then the Startup sleeps, or if
                                784                 :                :  * Startup is sleeping and the query waits on a lock. We protect against
                                785                 :                :  * only the former sequence here, the latter sequence is checked prior to
                                786                 :                :  * the query sleeping, in CheckRecoveryConflictDeadlock().
                                787                 :                :  *
                                788                 :                :  * Deadlocks are extremely rare, and relatively expensive to check for,
                                789                 :                :  * so we don't do a deadlock check right away ... only if we have had to wait
                                790                 :                :  * at least deadlock_timeout.
                                791                 :                :  */
                                792                 :                : void
 5705                           793                 :              9 : ResolveRecoveryConflictWithBufferPin(void)
                                794                 :                : {
                                795                 :                :     TimestampTz ltime;
                                796                 :                : 
                                797         [ -  + ]:              9 :     Assert(InHotStandby);
                                798                 :                : 
 5544 tgl@sss.pgh.pa.us         799                 :              9 :     ltime = GetStandbyLimitTime();
                                800                 :                : 
 1704 fujii@postgresql.org      801   [ +  +  +  - ]:              9 :     if (GetCurrentTimestamp() >= ltime && ltime != 0)
                                802                 :                :     {
                                803                 :                :         /*
                                804                 :                :          * We're already behind, so clear a path as quickly as possible.
                                805                 :                :          */
 5544 tgl@sss.pgh.pa.us         806                 :              1 :         SendRecoveryConflictWithBufferPin(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
                                807                 :                :     }
                                808                 :                :     else
                                809                 :                :     {
                                810                 :                :         /*
                                811                 :                :          * Wake up at ltime, and check for deadlocks as well if we will be
                                812                 :                :          * waiting longer than deadlock_timeout
                                813                 :                :          */
                                814                 :                :         EnableTimeoutParams timeouts[2];
 1704 fujii@postgresql.org      815                 :              8 :         int         cnt = 0;
                                816                 :                : 
                                817         [ +  - ]:              8 :         if (ltime != 0)
                                818                 :                :         {
                                819                 :              8 :             timeouts[cnt].id = STANDBY_TIMEOUT;
                                820                 :              8 :             timeouts[cnt].type = TMPARAM_AT;
                                821                 :              8 :             timeouts[cnt].fin_time = ltime;
                                822                 :              8 :             cnt++;
                                823                 :                :         }
                                824                 :                : 
                                825                 :              8 :         got_standby_deadlock_timeout = false;
                                826                 :              8 :         timeouts[cnt].id = STANDBY_DEADLOCK_TIMEOUT;
                                827                 :              8 :         timeouts[cnt].type = TMPARAM_AFTER;
                                828                 :              8 :         timeouts[cnt].delay_ms = DeadlockTimeout;
                                829                 :              8 :         cnt++;
                                830                 :                : 
                                831                 :              8 :         enable_timeouts(timeouts, cnt);
                                832                 :                :     }
                                833                 :                : 
                                834                 :                :     /*
                                835                 :                :      * Wait to be signaled by UnpinBuffer() or for the wait to be interrupted
                                836                 :                :      * by one of the timeouts established above.
                                837                 :                :      *
                                838                 :                :      * We assume that only UnpinBuffer() and the timeout requests established
                                839                 :                :      * above can wake us up here. WakeupRecovery() called by walreceiver or
                                840                 :                :      * SIGHUP signal handler, etc cannot do that because it uses the different
                                841                 :                :      * latch from that ProcWaitForSignal() waits on.
                                842                 :                :      */
  796 michael@paquier.xyz       843                 :              9 :     ProcWaitForSignal(WAIT_EVENT_BUFFER_PIN);
                                844                 :                : 
 1223 andres@anarazel.de        845         [ +  + ]:              9 :     if (got_standby_delay_timeout)
                                846                 :              1 :         SendRecoveryConflictWithBufferPin(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
                                847         [ +  + ]:              8 :     else if (got_standby_deadlock_timeout)
                                848                 :                :     {
                                849                 :                :         /*
                                850                 :                :          * Send out a request for hot-standby backends to check themselves for
                                851                 :                :          * deadlocks.
                                852                 :                :          *
                                853                 :                :          * XXX The subsequent ResolveRecoveryConflictWithBufferPin() will wait
                                854                 :                :          * to be signaled by UnpinBuffer() again and send a request for
                                855                 :                :          * deadlocks check if deadlock_timeout happens. This causes the
                                856                 :                :          * request to continue to be sent every deadlock_timeout until the
                                857                 :                :          * buffer is unpinned or ltime is reached. This would increase the
                                858                 :                :          * workload in the startup process and backends. In practice it may
                                859                 :                :          * not be so harmful because the period that the buffer is kept pinned
                                860                 :                :          * is basically no so long. But we should fix this?
                                861                 :                :          */
 1212 alvherre@alvh.no-ip.      862                 :              5 :         SendRecoveryConflictWithBufferPin(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
                                863                 :                :     }
                                864                 :                : 
                                865                 :                :     /*
                                866                 :                :      * Clear any timeout requests established above.  We assume here that the
                                867                 :                :      * Startup process doesn't have any other timeouts than what this function
                                868                 :                :      * uses.  If that stops being true, we could cancel the timeouts
                                869                 :                :      * individually, but that'd be slower.
                                870                 :                :      */
 4800                           871                 :              9 :     disable_all_timeouts(false);
 1223 andres@anarazel.de        872                 :              9 :     got_standby_delay_timeout = false;
 1704 fujii@postgresql.org      873                 :              9 :     got_standby_deadlock_timeout = false;
 5705 simon@2ndQuadrant.co      874                 :              9 : }
                                875                 :                : 
                                876                 :                : static void
 5684                           877                 :              7 : SendRecoveryConflictWithBufferPin(ProcSignalReason reason)
                                878                 :                : {
                                879   [ +  +  -  + ]:              7 :     Assert(reason == PROCSIG_RECOVERY_CONFLICT_BUFFERPIN ||
                                880                 :                :            reason == PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
                                881                 :                : 
                                882                 :                :     /*
                                883                 :                :      * We send signal to all backends to ask them if they are holding the
                                884                 :                :      * buffer pin which is delaying the Startup process. We must not set the
                                885                 :                :      * conflict flag yet, since most backends will be innocent. Let the
                                886                 :                :      * SIGUSR1 handling in each backend decide their own fate.
                                887                 :                :      */
                                888                 :              7 :     CancelDBBackends(InvalidOid, reason, false);
 5705                           889                 :              7 : }
                                890                 :                : 
                                891                 :                : /*
                                892                 :                :  * In Hot Standby perform early deadlock detection.  We abort the lock
                                893                 :                :  * wait if we are about to sleep while holding the buffer pin that Startup
                                894                 :                :  * process is waiting for.
                                895                 :                :  *
                                896                 :                :  * Note: this code is pessimistic, because there is no way for it to
                                897                 :                :  * determine whether an actual deadlock condition is present: the lock we
                                898                 :                :  * need to wait for might be unrelated to any held by the Startup process.
                                899                 :                :  * Sooner or later, this mechanism should get ripped out in favor of somehow
                                900                 :                :  * accounting for buffer locks in DeadLockCheck().  However, errors here
                                901                 :                :  * seem to be very low-probability in practice, so for now it's not worth
                                902                 :                :  * the trouble.
                                903                 :                :  */
                                904                 :                : void
 5149 tgl@sss.pgh.pa.us         905                 :              1 : CheckRecoveryConflictDeadlock(void)
                                906                 :                : {
                                907         [ -  + ]:              1 :     Assert(!InRecovery);        /* do not call in Startup process */
                                908                 :                : 
 5697 simon@2ndQuadrant.co      909         [ +  - ]:              1 :     if (!HoldingBufferPinThatDelaysRecovery())
                                910                 :              1 :         return;
                                911                 :                : 
                                912                 :                :     /*
                                913                 :                :      * Error message should match ProcessInterrupts() but we avoid calling
                                914                 :                :      * that because we aren't handling an interrupt at this point. Note that
                                915                 :                :      * we only cancel the current transaction here, so if we are in a
                                916                 :                :      * subtransaction and the pin is held by a parent, then the Startup
                                917                 :                :      * process will continue to wait even though we have avoided deadlock.
                                918                 :                :      */
 5697 simon@2ndQuadrant.co      919         [ #  # ]:UBC           0 :     ereport(ERROR,
                                920                 :                :             (errcode(ERRCODE_T_R_DEADLOCK_DETECTED),
                                921                 :                :              errmsg("canceling statement due to conflict with recovery"),
                                922                 :                :              errdetail("User transaction caused buffer deadlock with recovery.")));
                                923                 :                : }
                                924                 :                : 
                                925                 :                : 
                                926                 :                : /* --------------------------------
                                927                 :                :  *      timeout handler routines
                                928                 :                :  * --------------------------------
                                929                 :                :  */
                                930                 :                : 
                                931                 :                : /*
                                932                 :                :  * StandbyDeadLockHandler() will be called if STANDBY_DEADLOCK_TIMEOUT is
                                933                 :                :  * exceeded.
                                934                 :                :  */
                                935                 :                : void
 4800 alvherre@alvh.no-ip.      936                 :CBC           7 : StandbyDeadLockHandler(void)
                                937                 :                : {
 1704 fujii@postgresql.org      938                 :              7 :     got_standby_deadlock_timeout = true;
 4800 alvherre@alvh.no-ip.      939                 :              7 : }
                                940                 :                : 
                                941                 :                : /*
                                942                 :                :  * StandbyTimeoutHandler() will be called if STANDBY_TIMEOUT is exceeded.
                                943                 :                :  */
                                944                 :                : void
                                945                 :              1 : StandbyTimeoutHandler(void)
                                946                 :                : {
 1223 andres@anarazel.de        947                 :              1 :     got_standby_delay_timeout = true;
 4800 alvherre@alvh.no-ip.      948                 :              1 : }
                                949                 :                : 
                                950                 :                : /*
                                951                 :                :  * StandbyLockTimeoutHandler() will be called if STANDBY_LOCK_TIMEOUT is exceeded.
                                952                 :                :  */
                                953                 :                : void
 3467 simon@2ndQuadrant.co      954                 :              1 : StandbyLockTimeoutHandler(void)
                                955                 :                : {
 1704 fujii@postgresql.org      956                 :              1 :     got_standby_lock_timeout = true;
 3467 simon@2ndQuadrant.co      957                 :              1 : }
                                958                 :                : 
                                959                 :                : /*
                                960                 :                :  * -----------------------------------------------------
                                961                 :                :  * Locking in Recovery Mode
                                962                 :                :  * -----------------------------------------------------
                                963                 :                :  *
                                964                 :                :  * All locks are held by the Startup process using a single virtual
                                965                 :                :  * transaction. This implementation is both simpler and in some senses,
                                966                 :                :  * more correct. The locks held mean "some original transaction held
                                967                 :                :  * this lock, so query access is not allowed at this time". So the Startup
                                968                 :                :  * process is the proxy by which the original locks are implemented.
                                969                 :                :  *
                                970                 :                :  * We only keep track of AccessExclusiveLocks, which are only ever held by
                                971                 :                :  * one transaction on one relation.
                                972                 :                :  *
                                973                 :                :  * We keep a table of known locks in the RecoveryLockHash hash table.
                                974                 :                :  * The point of that table is to let us efficiently de-duplicate locks,
                                975                 :                :  * which is important because checkpoints will re-report the same locks
                                976                 :                :  * already held.  There is also a RecoveryLockXidHash table with one entry
                                977                 :                :  * per xid, which allows us to efficiently find all the locks held by a
                                978                 :                :  * given original transaction.
                                979                 :                :  *
                                980                 :                :  * We use session locks rather than normal locks so we don't need
                                981                 :                :  * ResourceOwners.
                                982                 :                :  */
                                983                 :                : 
                                984                 :                : 
                                985                 :                : void
 5740                           986                 :          25540 : StandbyAcquireAccessExclusiveLock(TransactionId xid, Oid dbOid, Oid relOid)
                                987                 :                : {
                                988                 :                :     RecoveryLockXidEntry *xidentry;
                                989                 :                :     RecoveryLockEntry *lockentry;
                                990                 :                :     xl_standby_lock key;
                                991                 :                :     LOCKTAG     locktag;
                                992                 :                :     bool        found;
                                993                 :                : 
                                994                 :                :     /* Already processed? */
 4975                           995   [ +  -  +  + ]:          51080 :     if (!TransactionIdIsValid(xid) ||
                                996         [ -  + ]:          51056 :         TransactionIdDidCommit(xid) ||
                                997                 :          25516 :         TransactionIdDidAbort(xid))
 5740                           998                 :             24 :         return;
                                999                 :                : 
  635 michael@paquier.xyz      1000         [ -  + ]:          25516 :     elog(DEBUG4, "adding recovery lock: db %u rel %u", dbOid, relOid);
                               1001                 :                : 
                               1002                 :                :     /* dbOid is InvalidOid when we are locking a shared relation. */
 5740 simon@2ndQuadrant.co     1003         [ -  + ]:          25516 :     Assert(OidIsValid(relOid));
                               1004                 :                : 
                               1005                 :                :     /* Create a hash entry for this xid, if we don't have one already. */
 1066 tgl@sss.pgh.pa.us        1006                 :          25516 :     xidentry = hash_search(RecoveryLockXidHash, &xid, HASH_ENTER, &found);
 2629 tmunro@postgresql.or     1007         [ +  + ]:          25516 :     if (!found)
                               1008                 :                :     {
 1066 tgl@sss.pgh.pa.us        1009         [ -  + ]:          10415 :         Assert(xidentry->xid == xid);    /* dynahash should have set this */
                               1010                 :          10415 :         xidentry->head = NULL;
                               1011                 :                :     }
                               1012                 :                : 
                               1013                 :                :     /* Create a hash entry for this lock, unless we have one already. */
                               1014                 :          25516 :     key.xid = xid;
                               1015                 :          25516 :     key.dbOid = dbOid;
                               1016                 :          25516 :     key.relOid = relOid;
                               1017                 :          25516 :     lockentry = hash_search(RecoveryLockHash, &key, HASH_ENTER, &found);
                               1018         [ +  + ]:          25516 :     if (!found)
                               1019                 :                :     {
                               1020                 :                :         /* It's new, so link it into the XID's list ... */
                               1021                 :          24247 :         lockentry->next = xidentry->head;
                               1022                 :          24247 :         xidentry->head = lockentry;
                               1023                 :                : 
                               1024                 :                :         /* ... and acquire the lock locally. */
                               1025                 :          24247 :         SET_LOCKTAG_RELATION(locktag, dbOid, relOid);
                               1026                 :                : 
                               1027                 :          24247 :         (void) LockAcquire(&locktag, AccessExclusiveLock, true, false);
                               1028                 :                :     }
                               1029                 :                : }
                               1030                 :                : 
                               1031                 :                : /*
                               1032                 :                :  * Release all the locks associated with this RecoveryLockXidEntry.
                               1033                 :                :  */
                               1034                 :                : static void
                               1035                 :          10415 : StandbyReleaseXidEntryLocks(RecoveryLockXidEntry *xidentry)
                               1036                 :                : {
                               1037                 :                :     RecoveryLockEntry *entry;
                               1038                 :                :     RecoveryLockEntry *next;
                               1039                 :                : 
                               1040         [ +  + ]:          34662 :     for (entry = xidentry->head; entry != NULL; entry = next)
                               1041                 :                :     {
                               1042                 :                :         LOCKTAG     locktag;
                               1043                 :                : 
  635 michael@paquier.xyz      1044         [ -  + ]:          24247 :         elog(DEBUG4,
                               1045                 :                :              "releasing recovery lock: xid %u db %u rel %u",
                               1046                 :                :              entry->key.xid, entry->key.dbOid, entry->key.relOid);
                               1047                 :                :         /* Release the lock ... */
 1066 tgl@sss.pgh.pa.us        1048                 :          24247 :         SET_LOCKTAG_RELATION(locktag, entry->key.dbOid, entry->key.relOid);
 2629 tmunro@postgresql.or     1049         [ -  + ]:          24247 :         if (!LockRelease(&locktag, AccessExclusiveLock, true))
                               1050                 :                :         {
 2629 tmunro@postgresql.or     1051         [ #  # ]:UBC           0 :             elog(LOG,
                               1052                 :                :                  "RecoveryLockHash contains entry for lock no longer recorded by lock manager: xid %u database %u relation %u",
                               1053                 :                :                  entry->key.xid, entry->key.dbOid, entry->key.relOid);
                               1054                 :              0 :             Assert(false);
                               1055                 :                :         }
                               1056                 :                :         /* ... and remove the per-lock hash entry */
 1066 tgl@sss.pgh.pa.us        1057                 :CBC       24247 :         next = entry->next;
                               1058                 :          24247 :         hash_search(RecoveryLockHash, entry, HASH_REMOVE, NULL);
                               1059                 :                :     }
                               1060                 :                : 
                               1061                 :          10415 :     xidentry->head = NULL;       /* just for paranoia */
 2629 tmunro@postgresql.or     1062                 :          10415 : }
                               1063                 :                : 
                               1064                 :                : /*
                               1065                 :                :  * Release locks for specific XID, or all locks if it's InvalidXid.
                               1066                 :                :  */
                               1067                 :                : static void
                               1068                 :          11092 : StandbyReleaseLocks(TransactionId xid)
                               1069                 :                : {
                               1070                 :                :     RecoveryLockXidEntry *entry;
                               1071                 :                : 
                               1072         [ +  - ]:          11092 :     if (TransactionIdIsValid(xid))
                               1073                 :                :     {
 1066 tgl@sss.pgh.pa.us        1074         [ +  + ]:          11092 :         if ((entry = hash_search(RecoveryLockXidHash, &xid, HASH_FIND, NULL)))
                               1075                 :                :         {
                               1076                 :          10415 :             StandbyReleaseXidEntryLocks(entry);
                               1077                 :          10415 :             hash_search(RecoveryLockXidHash, entry, HASH_REMOVE, NULL);
                               1078                 :                :         }
                               1079                 :                :     }
                               1080                 :                :     else
 2629 tmunro@postgresql.or     1081                 :UBC           0 :         StandbyReleaseAllLocks();
 5740 simon@2ndQuadrant.co     1082                 :CBC       11092 : }
                               1083                 :                : 
                               1084                 :                : /*
                               1085                 :                :  * Release locks for a transaction tree, starting at xid down, from
                               1086                 :                :  * RecoveryLockXidHash.
                               1087                 :                :  *
                               1088                 :                :  * Called during WAL replay of COMMIT/ROLLBACK when in hot standby mode,
                               1089                 :                :  * to remove any AccessExclusiveLocks requested by a transaction.
                               1090                 :                :  */
                               1091                 :                : void
                               1092                 :          10591 : StandbyReleaseLockTree(TransactionId xid, int nsubxids, TransactionId *subxids)
                               1093                 :                : {
                               1094                 :                :     int         i;
                               1095                 :                : 
                               1096                 :          10591 :     StandbyReleaseLocks(xid);
                               1097                 :                : 
                               1098         [ +  + ]:          11092 :     for (i = 0; i < nsubxids; i++)
                               1099                 :            501 :         StandbyReleaseLocks(subxids[i]);
                               1100                 :          10591 : }
                               1101                 :                : 
                               1102                 :                : /*
                               1103                 :                :  * Called at end of recovery and when we see a shutdown checkpoint.
                               1104                 :                :  */
                               1105                 :                : void
 4975                          1106                 :            100 : StandbyReleaseAllLocks(void)
                               1107                 :                : {
                               1108                 :                :     HASH_SEQ_STATUS status;
                               1109                 :                :     RecoveryLockXidEntry *entry;
                               1110                 :                : 
  635 michael@paquier.xyz      1111         [ +  + ]:            100 :     elog(DEBUG2, "release all standby locks");
                               1112                 :                : 
 1066 tgl@sss.pgh.pa.us        1113                 :            100 :     hash_seq_init(&status, RecoveryLockXidHash);
 2629 tmunro@postgresql.or     1114         [ -  + ]:            100 :     while ((entry = hash_seq_search(&status)))
                               1115                 :                :     {
 1066 tgl@sss.pgh.pa.us        1116                 :UBC           0 :         StandbyReleaseXidEntryLocks(entry);
                               1117                 :              0 :         hash_search(RecoveryLockXidHash, entry, HASH_REMOVE, NULL);
                               1118                 :                :     }
 4975 simon@2ndQuadrant.co     1119                 :CBC         100 : }
                               1120                 :                : 
                               1121                 :                : /*
                               1122                 :                :  * StandbyReleaseOldLocks
                               1123                 :                :  *      Release standby locks held by top-level XIDs that aren't running,
                               1124                 :                :  *      as long as they're not prepared transactions.
                               1125                 :                :  *
                               1126                 :                :  * This is needed to prune the locks of crashed transactions, which didn't
                               1127                 :                :  * write an ABORT/COMMIT record.
                               1128                 :                :  */
                               1129                 :                : void
 2639                          1130                 :            735 : StandbyReleaseOldLocks(TransactionId oldxid)
                               1131                 :                : {
                               1132                 :                :     HASH_SEQ_STATUS status;
                               1133                 :                :     RecoveryLockXidEntry *entry;
                               1134                 :                : 
 1066 tgl@sss.pgh.pa.us        1135                 :            735 :     hash_seq_init(&status, RecoveryLockXidHash);
 2629 tmunro@postgresql.or     1136         [ +  + ]:           1038 :     while ((entry = hash_seq_search(&status)))
                               1137                 :                :     {
                               1138         [ -  + ]:            303 :         Assert(TransactionIdIsValid(entry->xid));
                               1139                 :                : 
                               1140                 :                :         /* Skip if prepared transaction. */
                               1141         [ -  + ]:            303 :         if (StandbyTransactionIdIsPrepared(entry->xid))
 2629 tmunro@postgresql.or     1142                 :UBC           0 :             continue;
                               1143                 :                : 
                               1144                 :                :         /* Skip if >= oldxid. */
 2629 tmunro@postgresql.or     1145         [ +  - ]:CBC         303 :         if (!TransactionIdPrecedes(entry->xid, oldxid))
                               1146                 :            303 :             continue;
                               1147                 :                : 
                               1148                 :                :         /* Remove all locks and hash table entry. */
 1066 tgl@sss.pgh.pa.us        1149                 :UBC           0 :         StandbyReleaseXidEntryLocks(entry);
                               1150                 :              0 :         hash_search(RecoveryLockXidHash, entry, HASH_REMOVE, NULL);
                               1151                 :                :     }
 5740 simon@2ndQuadrant.co     1152                 :CBC         735 : }
                               1153                 :                : 
                               1154                 :                : /*
                               1155                 :                :  * --------------------------------------------------------------------
                               1156                 :                :  *      Recovery handling for Rmgr RM_STANDBY_ID
                               1157                 :                :  *
                               1158                 :                :  * These record types will only be created if XLogStandbyInfoActive()
                               1159                 :                :  * --------------------------------------------------------------------
                               1160                 :                :  */
                               1161                 :                : 
                               1162                 :                : void
 3943 heikki.linnakangas@i     1163                 :          25863 : standby_redo(XLogReaderState *record)
                               1164                 :                : {
                               1165                 :          25863 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
                               1166                 :                : 
                               1167                 :                :     /* Backup blocks are not used in standby records */
                               1168         [ -  + ]:          25863 :     Assert(!XLogRecHasAnyBlockRefs(record));
                               1169                 :                : 
                               1170                 :                :     /* Do nothing if we're not in hot standby mode */
 5740 simon@2ndQuadrant.co     1171         [ +  + ]:          25863 :     if (standbyState == STANDBY_DISABLED)
                               1172                 :            149 :         return;
                               1173                 :                : 
                               1174         [ +  + ]:          25714 :     if (info == XLOG_STANDBY_LOCK)
                               1175                 :                :     {
                               1176                 :          24405 :         xl_standby_locks *xlrec = (xl_standby_locks *) XLogRecGetData(record);
                               1177                 :                :         int         i;
                               1178                 :                : 
                               1179         [ +  + ]:          49945 :         for (i = 0; i < xlrec->nlocks; i++)
                               1180                 :          25540 :             StandbyAcquireAccessExclusiveLock(xlrec->locks[i].xid,
                               1181                 :                :                                               xlrec->locks[i].dbOid,
                               1182                 :                :                                               xlrec->locks[i].relOid);
                               1183                 :                :     }
                               1184         [ +  + ]:           1309 :     else if (info == XLOG_RUNNING_XACTS)
                               1185                 :                :     {
                               1186                 :            681 :         xl_running_xacts *xlrec = (xl_running_xacts *) XLogRecGetData(record);
                               1187                 :                :         RunningTransactionsData running;
                               1188                 :                : 
                               1189                 :            681 :         running.xcnt = xlrec->xcnt;
 4661                          1190                 :            681 :         running.subxcnt = xlrec->subxcnt;
  436 heikki.linnakangas@i     1191                 :            681 :         running.subxid_status = xlrec->subxid_overflow ? SUBXIDS_MISSING : SUBXIDS_IN_ARRAY;
 5740 simon@2ndQuadrant.co     1192                 :            681 :         running.nextXid = xlrec->nextXid;
 5594                          1193                 :            681 :         running.latestCompletedXid = xlrec->latestCompletedXid;
 5740                          1194                 :            681 :         running.oldestRunningXid = xlrec->oldestRunningXid;
                               1195                 :            681 :         running.xids = xlrec->xids;
                               1196                 :                : 
                               1197                 :            681 :         ProcArrayApplyRecoveryInfo(&running);
                               1198                 :                : 
                               1199                 :                :         /*
                               1200                 :                :          * The startup process currently has no convenient way to schedule
                               1201                 :                :          * stats to be reported. XLOG_RUNNING_XACTS records issued at a
                               1202                 :                :          * regular cadence, making this a convenient location to report stats.
                               1203                 :                :          * While these records aren't generated with wal_level=minimal, stats
                               1204                 :                :          * also cannot be accessed during WAL replay.
                               1205                 :                :          */
  817 andres@anarazel.de       1206                 :            681 :         pgstat_report_stat(true);
                               1207                 :                :     }
 3423                          1208         [ +  - ]:            628 :     else if (info == XLOG_INVALIDATIONS)
                               1209                 :                :     {
                               1210                 :            628 :         xl_invalidations *xlrec = (xl_invalidations *) XLogRecGetData(record);
                               1211                 :                : 
                               1212                 :            628 :         ProcessCommittedInvalidationMessages(xlrec->msgs,
                               1213                 :                :                                              xlrec->nmsgs,
                               1214                 :            628 :                                              xlrec->relcacheInitFileInval,
                               1215                 :                :                                              xlrec->dbId,
                               1216                 :                :                                              xlrec->tsId);
                               1217                 :                :     }
                               1218                 :                :     else
 4963 tgl@sss.pgh.pa.us        1219         [ #  # ]:UBC           0 :         elog(PANIC, "standby_redo: unknown op code %u", info);
                               1220                 :                : }
                               1221                 :                : 
                               1222                 :                : /*
                               1223                 :                :  * Log details of the current snapshot to WAL. This allows the snapshot state
                               1224                 :                :  * to be reconstructed on the standby and for logical decoding.
                               1225                 :                :  *
                               1226                 :                :  * This is used for Hot Standby as follows:
                               1227                 :                :  *
                               1228                 :                :  * We can move directly to STANDBY_SNAPSHOT_READY at startup if we
                               1229                 :                :  * start from a shutdown checkpoint because we know nothing was running
                               1230                 :                :  * at that time and our recovery snapshot is known empty. In the more
                               1231                 :                :  * typical case of an online checkpoint we need to jump through a few
                               1232                 :                :  * hoops to get a correct recovery snapshot and this requires a two or
                               1233                 :                :  * sometimes a three stage process.
                               1234                 :                :  *
                               1235                 :                :  * The initial snapshot must contain all running xids and all current
                               1236                 :                :  * AccessExclusiveLocks at a point in time on the standby. Assembling
                               1237                 :                :  * that information while the server is running requires many and
                               1238                 :                :  * various LWLocks, so we choose to derive that information piece by
                               1239                 :                :  * piece and then re-assemble that info on the standby. When that
                               1240                 :                :  * information is fully assembled we move to STANDBY_SNAPSHOT_READY.
                               1241                 :                :  *
                               1242                 :                :  * Since locking on the primary when we derive the information is not
                               1243                 :                :  * strict, we note that there is a time window between the derivation and
                               1244                 :                :  * writing to WAL of the derived information. That allows race conditions
                               1245                 :                :  * that we must resolve, since xids and locks may enter or leave the
                               1246                 :                :  * snapshot during that window. This creates the issue that an xid or
                               1247                 :                :  * lock may start *after* the snapshot has been derived yet *before* the
                               1248                 :                :  * snapshot is logged in the running xacts WAL record. We resolve this by
                               1249                 :                :  * starting to accumulate changes at a point just prior to when we derive
                               1250                 :                :  * the snapshot on the primary, then ignore duplicates when we later apply
                               1251                 :                :  * the snapshot from the running xacts record. This is implemented during
                               1252                 :                :  * CreateCheckPoint() where we use the logical checkpoint location as
                               1253                 :                :  * our starting point and then write the running xacts record immediately
                               1254                 :                :  * before writing the main checkpoint WAL record. Since we always start
                               1255                 :                :  * up from a checkpoint and are immediately at our starting point, we
                               1256                 :                :  * unconditionally move to STANDBY_INITIALIZED. After this point we
                               1257                 :                :  * must do 4 things:
                               1258                 :                :  *  * move shared nextXid forwards as we see new xids
                               1259                 :                :  *  * extend the clog and subtrans with each new xid
                               1260                 :                :  *  * keep track of uncommitted known assigned xids
                               1261                 :                :  *  * keep track of uncommitted AccessExclusiveLocks
                               1262                 :                :  *
                               1263                 :                :  * When we see a commit/abort we must remove known assigned xids and locks
                               1264                 :                :  * from the completing transaction. Attempted removals that cannot locate
                               1265                 :                :  * an entry are expected and must not cause an error when we are in state
                               1266                 :                :  * STANDBY_INITIALIZED. This is implemented in StandbyReleaseLocks() and
                               1267                 :                :  * KnownAssignedXidsRemove().
                               1268                 :                :  *
                               1269                 :                :  * Later, when we apply the running xact data we must be careful to ignore
                               1270                 :                :  * transactions already committed, since those commits raced ahead when
                               1271                 :                :  * making WAL entries.
                               1272                 :                :  *
                               1273                 :                :  * For logical decoding only the running xacts information is needed;
                               1274                 :                :  * there's no need to look at the locking information, but it's logged anyway,
                               1275                 :                :  * as there's no independent knob to just enable logical decoding. For
                               1276                 :                :  * details of how this is used, check snapbuild.c's introductory comment.
                               1277                 :                :  *
                               1278                 :                :  *
                               1279                 :                :  * Returns the RecPtr of the last inserted record.
                               1280                 :                :  */
                               1281                 :                : XLogRecPtr
 4661 tgl@sss.pgh.pa.us        1282                 :CBC        1339 : LogStandbySnapshot(void)
                               1283                 :                : {
                               1284                 :                :     XLogRecPtr  recptr;
                               1285                 :                :     RunningTransactions running;
                               1286                 :                :     xl_standby_lock *locks;
                               1287                 :                :     int         nlocks;
                               1288                 :                : 
 5740 simon@2ndQuadrant.co     1289         [ -  + ]:           1339 :     Assert(XLogStandbyInfoActive());
                               1290                 :                : 
                               1291                 :                : #ifdef USE_INJECTION_POINTS
                               1292                 :                :     if (IS_INJECTION_POINT_ATTACHED("skip-log-running-xacts"))
                               1293                 :                :     {
                               1294                 :                :         /*
                               1295                 :                :          * This record could move slot's xmin forward during decoding, leading
                               1296                 :                :          * to unpredictable results, so skip it when requested by the test.
                               1297                 :                :          */
                               1298                 :                :         return GetInsertRecPtr();
                               1299                 :                :     }
                               1300                 :                : #endif
                               1301                 :                : 
                               1302                 :                :     /*
                               1303                 :                :      * Get details of any AccessExclusiveLocks being held at the moment.
                               1304                 :                :      */
                               1305                 :           1339 :     locks = GetRunningTransactionLocks(&nlocks);
                               1306         [ +  + ]:           1339 :     if (nlocks > 0)
                               1307                 :            155 :         LogAccessExclusiveLocks(nlocks, locks);
 4477 tgl@sss.pgh.pa.us        1308                 :           1339 :     pfree(locks);
                               1309                 :                : 
                               1310                 :                :     /*
                               1311                 :                :      * Log details of all in-progress transactions. This should be the last
                               1312                 :                :      * record we write, because standby will open up when it sees this.
                               1313                 :                :      */
 5740 simon@2ndQuadrant.co     1314                 :           1339 :     running = GetRunningTransactionData();
                               1315                 :                : 
                               1316                 :                :     /*
                               1317                 :                :      * GetRunningTransactionData() acquired ProcArrayLock, we must release it.
                               1318                 :                :      * For Hot Standby this can be done before inserting the WAL record
                               1319                 :                :      * because ProcArrayApplyRecoveryInfo() rechecks the commit status using
                               1320                 :                :      * the clog. For logical decoding, though, the lock can't be released
                               1321                 :                :      * early because the clog might be "in the future" from the POV of the
                               1322                 :                :      * historic snapshot. This would allow for situations where we're waiting
                               1323                 :                :      * for the end of a transaction listed in the xl_running_xacts record
                               1324                 :                :      * which, according to the WAL, has committed before the xl_running_xacts
                               1325                 :                :      * record. Fortunately this routine isn't executed frequently, and it's
                               1326                 :                :      * only a shared lock.
                               1327                 :                :      */
 4205 rhaas@postgresql.org     1328         [ +  + ]:           1339 :     if (wal_level < WAL_LEVEL_LOGICAL)
                               1329                 :            862 :         LWLockRelease(ProcArrayLock);
                               1330                 :                : 
 4252                          1331                 :           1339 :     recptr = LogCurrentRunningXacts(running);
                               1332                 :                : 
                               1333                 :                :     /* Release lock if we kept it longer ... */
 4205                          1334         [ +  + ]:           1339 :     if (wal_level >= WAL_LEVEL_LOGICAL)
                               1335                 :            477 :         LWLockRelease(ProcArrayLock);
                               1336                 :                : 
                               1337                 :                :     /* GetRunningTransactionData() acquired XidGenLock, we must release it */
 5387 heikki.linnakangas@i     1338                 :           1339 :     LWLockRelease(XidGenLock);
                               1339                 :                : 
 4252 rhaas@postgresql.org     1340                 :           1339 :     return recptr;
                               1341                 :                : }
                               1342                 :                : 
                               1343                 :                : /*
                               1344                 :                :  * Record an enhanced snapshot of running transactions into WAL.
                               1345                 :                :  *
                               1346                 :                :  * The definitions of RunningTransactionsData and xl_running_xacts are
                               1347                 :                :  * similar. We keep them separate because xl_running_xacts is a contiguous
                               1348                 :                :  * chunk of memory and never exists fully until it is assembled in WAL.
                               1349                 :                :  * The inserted records are marked as not being important for durability,
                               1350                 :                :  * to avoid triggering superfluous checkpoint / archiving activity.
                               1351                 :                :  */
                               1352                 :                : static XLogRecPtr
 5740 simon@2ndQuadrant.co     1353                 :           1339 : LogCurrentRunningXacts(RunningTransactions CurrRunningXacts)
                               1354                 :                : {
                               1355                 :                :     xl_running_xacts xlrec;
                               1356                 :                :     XLogRecPtr  recptr;
                               1357                 :                : 
                               1358                 :           1339 :     xlrec.xcnt = CurrRunningXacts->xcnt;
 4661                          1359                 :           1339 :     xlrec.subxcnt = CurrRunningXacts->subxcnt;
  436 heikki.linnakangas@i     1360                 :           1339 :     xlrec.subxid_overflow = (CurrRunningXacts->subxid_status != SUBXIDS_IN_ARRAY);
 5740 simon@2ndQuadrant.co     1361                 :           1339 :     xlrec.nextXid = CurrRunningXacts->nextXid;
                               1362                 :           1339 :     xlrec.oldestRunningXid = CurrRunningXacts->oldestRunningXid;
 5594                          1363                 :           1339 :     xlrec.latestCompletedXid = CurrRunningXacts->latestCompletedXid;
                               1364                 :                : 
                               1365                 :                :     /* Header */
 3943 heikki.linnakangas@i     1366                 :           1339 :     XLogBeginInsert();
 3180 andres@anarazel.de       1367                 :           1339 :     XLogSetRecordFlags(XLOG_MARK_UNIMPORTANT);
  207 peter@eisentraut.org     1368                 :           1339 :     XLogRegisterData(&xlrec, MinSizeOfXactRunningXacts);
                               1369                 :                : 
                               1370                 :                :     /* array of TransactionIds */
 5740 simon@2ndQuadrant.co     1371         [ +  + ]:           1339 :     if (xlrec.xcnt > 0)
  207 peter@eisentraut.org     1372                 :            443 :         XLogRegisterData(CurrRunningXacts->xids,
 2999 tgl@sss.pgh.pa.us        1373                 :            443 :                          (xlrec.xcnt + xlrec.subxcnt) * sizeof(TransactionId));
                               1374                 :                : 
 3943 heikki.linnakangas@i     1375                 :           1339 :     recptr = XLogInsert(RM_STANDBY_ID, XLOG_RUNNING_XACTS);
                               1376                 :                : 
  436                          1377         [ +  + ]:           1339 :     if (xlrec.subxid_overflow)
  635 michael@paquier.xyz      1378         [ -  + ]:              2 :         elog(DEBUG2,
                               1379                 :                :              "snapshot of %d running transactions overflowed (lsn %X/%08X oldest xid %u latest complete %u next xid %u)",
                               1380                 :                :              CurrRunningXacts->xcnt,
                               1381                 :                :              LSN_FORMAT_ARGS(recptr),
                               1382                 :                :              CurrRunningXacts->oldestRunningXid,
                               1383                 :                :              CurrRunningXacts->latestCompletedXid,
                               1384                 :                :              CurrRunningXacts->nextXid);
                               1385                 :                :     else
                               1386         [ +  + ]:           1337 :         elog(DEBUG2,
                               1387                 :                :              "snapshot of %d+%d running transaction ids (lsn %X/%08X oldest xid %u latest complete %u next xid %u)",
                               1388                 :                :              CurrRunningXacts->xcnt, CurrRunningXacts->subxcnt,
                               1389                 :                :              LSN_FORMAT_ARGS(recptr),
                               1390                 :                :              CurrRunningXacts->oldestRunningXid,
                               1391                 :                :              CurrRunningXacts->latestCompletedXid,
                               1392                 :                :              CurrRunningXacts->nextXid);
                               1393                 :                : 
                               1394                 :                :     /*
                               1395                 :                :      * Ensure running_xacts information is synced to disk not too far in the
                               1396                 :                :      * future. We don't want to stall anything though (i.e. use XLogFlush()),
                               1397                 :                :      * so we let the wal writer do it during normal operation.
                               1398                 :                :      * XLogSetAsyncXactLSN() conveniently will mark the LSN as to-be-synced
                               1399                 :                :      * and nudge the WALWriter into action if sleeping. Check
                               1400                 :                :      * XLogBackgroundFlush() for details why a record might not be flushed
                               1401                 :                :      * without it.
                               1402                 :                :      */
 4252 rhaas@postgresql.org     1403                 :           1339 :     XLogSetAsyncXactLSN(recptr);
                               1404                 :                : 
                               1405                 :           1339 :     return recptr;
                               1406                 :                : }
                               1407                 :                : 
                               1408                 :                : /*
                               1409                 :                :  * Wholesale logging of AccessExclusiveLocks. Other lock types need not be
                               1410                 :                :  * logged, as described in backend/storage/lmgr/README.
                               1411                 :                :  */
                               1412                 :                : static void
 5740 simon@2ndQuadrant.co     1413                 :          97389 : LogAccessExclusiveLocks(int nlocks, xl_standby_lock *locks)
                               1414                 :                : {
                               1415                 :                :     xl_standby_locks xlrec;
                               1416                 :                : 
                               1417                 :          97389 :     xlrec.nlocks = nlocks;
                               1418                 :                : 
 3943 heikki.linnakangas@i     1419                 :          97389 :     XLogBeginInsert();
  207 peter@eisentraut.org     1420                 :          97389 :     XLogRegisterData(&xlrec, offsetof(xl_standby_locks, locks));
                               1421                 :          97389 :     XLogRegisterData(locks, nlocks * sizeof(xl_standby_lock));
 3180 andres@anarazel.de       1422                 :          97389 :     XLogSetRecordFlags(XLOG_MARK_UNIMPORTANT);
                               1423                 :                : 
 3943 heikki.linnakangas@i     1424                 :          97389 :     (void) XLogInsert(RM_STANDBY_ID, XLOG_STANDBY_LOCK);
 5740 simon@2ndQuadrant.co     1425                 :          97389 : }
                               1426                 :                : 
                               1427                 :                : /*
                               1428                 :                :  * Individual logging of AccessExclusiveLocks for use during LockAcquire()
                               1429                 :                :  */
                               1430                 :                : void
                               1431                 :          97234 : LogAccessExclusiveLock(Oid dbOid, Oid relOid)
                               1432                 :                : {
                               1433                 :                :     xl_standby_lock xlrec;
                               1434                 :                : 
 3090                          1435                 :          97234 :     xlrec.xid = GetCurrentTransactionId();
                               1436                 :                : 
 5740                          1437                 :          97234 :     xlrec.dbOid = dbOid;
                               1438                 :          97234 :     xlrec.relOid = relOid;
                               1439                 :                : 
                               1440                 :          97234 :     LogAccessExclusiveLocks(1, &xlrec);
 3090                          1441                 :          97234 :     MyXactFlags |= XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK;
 5740                          1442                 :          97234 : }
                               1443                 :                : 
                               1444                 :                : /*
                               1445                 :                :  * Prepare to log an AccessExclusiveLock, for use during LockAcquire()
                               1446                 :                :  */
                               1447                 :                : void
 5395                          1448                 :          97449 : LogAccessExclusiveLockPrepare(void)
                               1449                 :                : {
                               1450                 :                :     /*
                               1451                 :                :      * Ensure that a TransactionId has been assigned to this transaction, for
                               1452                 :                :      * two reasons, both related to lock release on the standby. First, we
                               1453                 :                :      * must assign an xid so that RecordTransactionCommit() and
                               1454                 :                :      * RecordTransactionAbort() do not optimise away the transaction
                               1455                 :                :      * completion record which recovery relies upon to release locks. It's a
                               1456                 :                :      * hack, but for a corner case not worth adding code for into the main
                               1457                 :                :      * commit path. Second, we must assign an xid before the lock is recorded
                               1458                 :                :      * in shared memory, otherwise a concurrently executing
                               1459                 :                :      * GetRunningTransactionLocks() might see a lock associated with an
                               1460                 :                :      * InvalidTransactionId which we later assert cannot happen.
                               1461                 :                :      */
 3090                          1462                 :          97449 :     (void) GetCurrentTransactionId();
 5395                          1463                 :          97449 : }
                               1464                 :                : 
                               1465                 :                : /*
                               1466                 :                :  * Emit WAL for invalidations. This currently is only used for commits without
                               1467                 :                :  * an xid but which contain invalidations.
                               1468                 :                :  */
                               1469                 :                : void
 3423 andres@anarazel.de       1470                 :           8985 : LogStandbyInvalidations(int nmsgs, SharedInvalidationMessage *msgs,
                               1471                 :                :                         bool relcacheInitFileInval)
                               1472                 :                : {
                               1473                 :                :     xl_invalidations xlrec;
                               1474                 :                : 
                               1475                 :                :     /* prepare record */
                               1476                 :           8985 :     memset(&xlrec, 0, sizeof(xlrec));
                               1477                 :           8985 :     xlrec.dbId = MyDatabaseId;
                               1478                 :           8985 :     xlrec.tsId = MyDatabaseTableSpace;
                               1479                 :           8985 :     xlrec.relcacheInitFileInval = relcacheInitFileInval;
                               1480                 :           8985 :     xlrec.nmsgs = nmsgs;
                               1481                 :                : 
                               1482                 :                :     /* perform insertion */
                               1483                 :           8985 :     XLogBeginInsert();
  207 peter@eisentraut.org     1484                 :           8985 :     XLogRegisterData(&xlrec, MinSizeOfInvalidations);
                               1485                 :           8985 :     XLogRegisterData(msgs,
                               1486                 :                :                      nmsgs * sizeof(SharedInvalidationMessage));
 3423 andres@anarazel.de       1487                 :           8985 :     XLogInsert(RM_STANDBY_ID, XLOG_INVALIDATIONS);
                               1488                 :           8985 : }
                               1489                 :                : 
                               1490                 :                : /* Return the description of recovery conflict */
                               1491                 :                : static const char *
 1702 fujii@postgresql.org     1492                 :             10 : get_recovery_conflict_desc(ProcSignalReason reason)
                               1493                 :                : {
 1531 peter@eisentraut.org     1494                 :             10 :     const char *reasonDesc = _("unknown reason");
                               1495                 :                : 
 1702 fujii@postgresql.org     1496   [ +  +  +  +  :             10 :     switch (reason)
                                        -  -  -  - ]
                               1497                 :                :     {
                               1498                 :              4 :         case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
 1531 peter@eisentraut.org     1499                 :              4 :             reasonDesc = _("recovery conflict on buffer pin");
 1702 fujii@postgresql.org     1500                 :              4 :             break;
                               1501                 :              2 :         case PROCSIG_RECOVERY_CONFLICT_LOCK:
 1531 peter@eisentraut.org     1502                 :              2 :             reasonDesc = _("recovery conflict on lock");
 1702 fujii@postgresql.org     1503                 :              2 :             break;
                               1504                 :              2 :         case PROCSIG_RECOVERY_CONFLICT_TABLESPACE:
 1531 peter@eisentraut.org     1505                 :              2 :             reasonDesc = _("recovery conflict on tablespace");
 1702 fujii@postgresql.org     1506                 :              2 :             break;
                               1507                 :              2 :         case PROCSIG_RECOVERY_CONFLICT_SNAPSHOT:
 1531 peter@eisentraut.org     1508                 :              2 :             reasonDesc = _("recovery conflict on snapshot");
 1702 fujii@postgresql.org     1509                 :              2 :             break;
  883 andres@anarazel.de       1510                 :UBC           0 :         case PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT:
                               1511                 :              0 :             reasonDesc = _("recovery conflict on replication slot");
                               1512                 :              0 :             break;
 1702 fujii@postgresql.org     1513                 :              0 :         case PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK:
 1531 peter@eisentraut.org     1514                 :              0 :             reasonDesc = _("recovery conflict on buffer deadlock");
 1702 fujii@postgresql.org     1515                 :              0 :             break;
                               1516                 :              0 :         case PROCSIG_RECOVERY_CONFLICT_DATABASE:
 1531 peter@eisentraut.org     1517                 :              0 :             reasonDesc = _("recovery conflict on database");
 1702 fujii@postgresql.org     1518                 :              0 :             break;
                               1519                 :              0 :         default:
                               1520                 :              0 :             break;
                               1521                 :                :     }
                               1522                 :                : 
 1702 fujii@postgresql.org     1523                 :CBC          10 :     return reasonDesc;
                               1524                 :                : }
        

Generated by: LCOV version 2.4-beta