LCOV - differential code coverage report
Current view: top level - src/backend/storage/ipc - standby.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 91.8 % 391 359 32 166 9 184 2 6
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 31 31 10 7 14
Baseline: 16@8cea358b128 Branches: 70.7 % 222 157 3 62 91 7 59
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 3 3 3
(120,180] days: 100.0 % 5 5 5
(240..) days: 91.6 % 383 351 32 166 1 184
Function coverage date bins:
(240..) days: 100.0 % 31 31 10 7 14
Branch coverage date bins:
(120,180] days: 70.0 % 10 7 3 7
(240..) days: 70.8 % 212 150 62 91 59

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

Generated by: LCOV version 2.1-beta2-3-g6141622