LCOV - differential code coverage report
Current view: top level - src/backend/access/transam - timeline.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: c38de3ae6098ccf1904109f2e550eb4777f66024 vs e352ac8c5c90ed6ce792f044c22145578542a330 Lines: 81.0 % 200 162 38 18 144 9
Current Date: 2026-11-05 14:14:01 +0100 Functions: 100.0 % 9 9 3 6 1
Baseline: lcov-20261105-baseline Branches: 50.6 % 180 91 6 83 6 85
Baseline Date: 2026-11-05 14:14:01 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 13 13 13
(30,360] days: 88.9 % 9 8 1 5 3
(360..) days: 79.2 % 178 141 37 141
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 8 8 2 6
Branch coverage date bins:
(7,30] days: 50.0 % 6 3 3 3
(30,360] days: 50.0 % 6 3 3 3
(360..) days: 50.6 % 168 85 83 85

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * timeline.c
                                  4                 :                :  *      Functions for reading and writing timeline history files.
                                  5                 :                :  *
                                  6                 :                :  * A timeline history file lists the timeline changes of the timeline, in
                                  7                 :                :  * a simple text format. They are archived along with the WAL segments.
                                  8                 :                :  *
                                  9                 :                :  * The files are named like "<tli>.history". For example, if the database
                                 10                 :                :  * starts up and switches to timeline 5, the timeline history file would be
                                 11                 :                :  * called "00000005.history".
                                 12                 :                :  *
                                 13                 :                :  * Each line in the file represents a timeline switch:
                                 14                 :                :  *
                                 15                 :                :  * <parentTLI> <switchpoint> <reason>
                                 16                 :                :  *
                                 17                 :                :  *  parentTLI   ID of the parent timeline
                                 18                 :                :  *  switchpoint XLogRecPtr of the WAL location where the switch happened
                                 19                 :                :  *  reason      human-readable explanation of why the timeline was changed
                                 20                 :                :  *
                                 21                 :                :  * The fields are separated by tabs. Lines beginning with # are comments, and
                                 22                 :                :  * are ignored. Empty lines are also ignored.
                                 23                 :                :  *
                                 24                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 25                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 26                 :                :  *
                                 27                 :                :  * src/backend/access/transam/timeline.c
                                 28                 :                :  *
                                 29                 :                :  *-------------------------------------------------------------------------
                                 30                 :                :  */
                                 31                 :                : 
                                 32                 :                : #include "postgres.h"
                                 33                 :                : 
                                 34                 :                : #include <sys/stat.h>
                                 35                 :                : #include <unistd.h>
                                 36                 :                : 
                                 37                 :                : #include "access/timeline.h"
                                 38                 :                : #include "access/xlog.h"
                                 39                 :                : #include "access/xlog_internal.h"
                                 40                 :                : #include "access/xlogarchive.h"
                                 41                 :                : #include "access/xlogdefs.h"
                                 42                 :                : #include "common/pg_parse_lsn.h"
                                 43                 :                : #include "pgstat.h"
                                 44                 :                : #include "storage/fd.h"
                                 45                 :                : #include "utils/wait_event.h"
                                 46                 :                : 
                                 47                 :                : /*
                                 48                 :                :  * Copies all timeline history files with id's between 'begin' and 'end'
                                 49                 :                :  * from archive to pg_wal.
                                 50                 :                :  */
                                 51                 :                : void
 5034 heikki.linnakangas@i       52                 :CBC        1134 : restoreTimeLineHistoryFiles(TimeLineID begin, TimeLineID end)
                                 53                 :                : {
                                 54                 :                :     char        path[MAXPGPATH];
                                 55                 :                :     char        histfname[MAXFNAMELEN];
                                 56                 :                :     TimeLineID  tli;
                                 57                 :                : 
                                 58         [ +  + ]:           1143 :     for (tli = begin; tli < end; tli++)
                                 59                 :                :     {
                                 60         [ +  + ]:              9 :         if (tli == 1)
                                 61                 :              6 :             continue;
                                 62                 :                : 
                                 63                 :              3 :         TLHistoryFileName(histfname, tli);
                                 64         [ +  + ]:              3 :         if (RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0, false))
                                 65                 :              1 :             KeepFileRestoredFromArchive(path, histfname);
                                 66                 :                :     }
                                 67                 :           1134 : }
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * Try to read a timeline's history file.
                                 71                 :                :  *
                                 72                 :                :  * If successful, return the list of component TLIs (the given TLI followed by
                                 73                 :                :  * its ancestor TLIs).  If we can't find the history file, assume that the
                                 74                 :                :  * timeline has no parents, and return a list of just the specified timeline
                                 75                 :                :  * ID.
                                 76                 :                :  */
                                 77                 :                : List *
 5147                            78                 :           2999 : readTimeLineHistory(TimeLineID targetTLI)
                                 79                 :                : {
                                 80                 :                :     List       *result;
                                 81                 :                :     char        path[MAXPGPATH];
                                 82                 :                :     char        histfname[MAXFNAMELEN];
                                 83                 :                :     FILE       *fd;
                                 84                 :                :     TimeLineHistoryEntry *entry;
 5084                            85                 :           2999 :     TimeLineID  lasttli = 0;
                                 86                 :                :     XLogRecPtr  prevend;
 5058                            87                 :           2999 :     bool        fromArchive = false;
                                 88                 :                : 
                                 89                 :                :     /* Timeline 1 does not have a history file, so no need to check */
 5147                            90         [ +  + ]:           2999 :     if (targetTLI == 1)
                                 91                 :                :     {
  330 michael@paquier.xyz        92                 :           2878 :         entry = palloc_object(TimeLineHistoryEntry);
 5084 heikki.linnakangas@i       93                 :           2878 :         entry->tli = targetTLI;
                                 94                 :           2878 :         entry->begin = entry->end = InvalidXLogRecPtr;
                                 95                 :           2878 :         return list_make1(entry);
                                 96                 :                :     }
                                 97                 :                : 
 4991                            98         [ +  + ]:            121 :     if (ArchiveRecoveryRequested)
                                 99                 :                :     {
 5147                           100                 :             47 :         TLHistoryFileName(histfname, targetTLI);
                                101                 :                :         fromArchive =
 5058                           102                 :             47 :             RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0, false);
                                103                 :                :     }
                                104                 :                :     else
 5147                           105                 :             74 :         TLHistoryFilePath(path, targetTLI);
                                106                 :                : 
                                107                 :            121 :     fd = AllocateFile(path, "r");
                                108         [ -  + ]:            121 :     if (fd == NULL)
                                109                 :                :     {
 5147 heikki.linnakangas@i      110         [ #  # ]:UBC           0 :         if (errno != ENOENT)
                                111         [ #  # ]:              0 :             ereport(FATAL,
                                112                 :                :                     (errcode_for_file_access(),
                                113                 :                :                      errmsg("could not open file \"%s\": %m", path)));
                                114                 :                :         /* Not there, so assume no parents */
  330 michael@paquier.xyz       115                 :              0 :         entry = palloc_object(TimeLineHistoryEntry);
 5084 heikki.linnakangas@i      116                 :              0 :         entry->tli = targetTLI;
                                117                 :              0 :         entry->begin = entry->end = InvalidXLogRecPtr;
                                118                 :              0 :         return list_make1(entry);
                                119                 :                :     }
                                120                 :                : 
 5147 heikki.linnakangas@i      121                 :CBC         121 :     result = NIL;
                                122                 :                : 
                                123                 :                :     /*
                                124                 :                :      * Parse the file...
                                125                 :                :      */
 5084                           126                 :            121 :     prevend = InvalidXLogRecPtr;
                                127                 :                :     for (;;)
 5147                           128                 :            294 :     {
                                129                 :                :         char        fline[MAXPGPATH];
                                130                 :                :         char       *res;
                                131                 :                :         char       *ptr;
                                132                 :                :         char       *token_end;
                                133                 :                :         char        save;
                                134                 :                :         TimeLineID  tli;
                                135                 :                :         XLogRecPtr  switchpoint;
   14 fujii@postgresql.org      136                 :GNC         415 :         bool        valid_switchpoint = false;
                                137                 :                :         int         nchars;
                                138                 :                :         size_t      nspaces;
                                139                 :                : 
 2372 fujii@postgresql.org      140                 :CBC         415 :         pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_READ);
                                141                 :            415 :         res = fgets(fline, sizeof(fline), fd);
                                142                 :            415 :         pgstat_report_wait_end();
                                143         [ +  + ]:            415 :         if (res == NULL)
                                144                 :                :         {
                                145         [ -  + ]:            121 :             if (ferror(fd))
 2372 fujii@postgresql.org      146         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                147                 :                :                         (errcode_for_file_access(),
                                148                 :                :                          errmsg("could not read file \"%s\": %m", path)));
                                149                 :                : 
 2372 fujii@postgresql.org      150                 :CBC         121 :             break;
                                151                 :                :         }
                                152                 :                : 
                                153                 :                :         /* skip leading whitespace and check for # comment */
 5147 heikki.linnakangas@i      154         [ +  + ]:            384 :         for (ptr = fline; *ptr; ptr++)
                                155                 :                :         {
                                156         [ +  + ]:            294 :             if (!isspace((unsigned char) *ptr))
                                157                 :            204 :                 break;
                                158                 :                :         }
                                159   [ +  +  -  + ]:            294 :         if (*ptr == '\0' || *ptr == '#')
                                160                 :             90 :             continue;
                                161                 :                : 
   14 fujii@postgresql.org      162         [ -  + ]:GNC         204 :         if (sscanf(fline, "%u%n", &tli, &nchars) != 1)
                                163                 :                :         {
                                164                 :                :             /* expect a numeric timeline ID as first field of line */
 5147 heikki.linnakangas@i      165         [ #  # ]:UBC           0 :             ereport(FATAL,
                                166                 :                :                     (errmsg("syntax error in history file: %s", fline),
                                167                 :                :                      errhint("Expected a numeric timeline ID.")));
                                168                 :                :         }
                                169                 :                : 
                                170                 :                :         /* the switchpoint location follows, separated by whitespace */
   14 fujii@postgresql.org      171                 :GNC         204 :         ptr = fline + nchars;
                                172                 :            204 :         nspaces = strspn(ptr, " \t\n\r\f\v");
                                173         [ +  - ]:            204 :         if (nspaces > 0)
                                174                 :                :         {
                                175                 :            204 :             ptr += nspaces;
                                176                 :                : 
                                177                 :                :             /*
                                178                 :                :              * isolate the location from the rest of the line before parsing
                                179                 :                :              * it
                                180                 :                :              */
                                181                 :            204 :             token_end = ptr + strcspn(ptr, " \t\n\r\f\v");
                                182                 :            204 :             save = *token_end;
                                183                 :            204 :             *token_end = '\0';
                                184                 :            204 :             valid_switchpoint = pg_parse_lsn(ptr, &switchpoint);
                                185                 :            204 :             *token_end = save;
                                186                 :                :         }
                                187                 :                : 
                                188         [ -  + ]:            204 :         if (!valid_switchpoint)
 5084 heikki.linnakangas@i      189         [ #  # ]:UBC           0 :             ereport(FATAL,
                                190                 :                :                     (errmsg("syntax error in history file: %s", fline),
                                191                 :                :                      errhint("Expected a write-ahead log switchpoint location.")));
                                192                 :                : 
 5084 heikki.linnakangas@i      193   [ +  +  -  + ]:CBC         204 :         if (result && tli <= lasttli)
 5147 heikki.linnakangas@i      194         [ #  # ]:UBC           0 :             ereport(FATAL,
                                195                 :                :                     (errmsg("invalid data in history file: %s", fline),
                                196                 :                :                      errhint("Timeline IDs must be in increasing sequence.")));
                                197                 :                : 
 5084 heikki.linnakangas@i      198                 :CBC         204 :         lasttli = tli;
                                199                 :                : 
  330 michael@paquier.xyz       200                 :            204 :         entry = palloc_object(TimeLineHistoryEntry);
 5084 heikki.linnakangas@i      201                 :            204 :         entry->tli = tli;
                                202                 :            204 :         entry->begin = prevend;
   14 fujii@postgresql.org      203                 :GNC         204 :         entry->end = switchpoint;
 5084 heikki.linnakangas@i      204                 :CBC         204 :         prevend = entry->end;
                                205                 :                : 
                                206                 :                :         /* Build list with newest item first */
                                207                 :            204 :         result = lcons(entry, result);
                                208                 :                : 
                                209                 :                :         /* we ignore the remainder of each line */
                                210                 :                :     }
                                211                 :                : 
 5147                           212                 :            121 :     FreeFile(fd);
                                213                 :                : 
 5084                           214   [ +  -  -  + ]:            121 :     if (result && targetTLI <= lasttli)
 5147 heikki.linnakangas@i      215         [ #  # ]:UBC           0 :         ereport(FATAL,
                                216                 :                :                 (errmsg("invalid data in history file \"%s\"", path),
                                217                 :                :                  errhint("Timeline IDs must be less than child timeline's ID.")));
                                218                 :                : 
                                219                 :                :     /*
                                220                 :                :      * Create one more entry for the "tip" of the timeline, which has no entry
                                221                 :                :      * in the history file.
                                222                 :                :      */
  330 michael@paquier.xyz       223                 :CBC         121 :     entry = palloc_object(TimeLineHistoryEntry);
 5084 heikki.linnakangas@i      224                 :            121 :     entry->tli = targetTLI;
                                225                 :            121 :     entry->begin = prevend;
                                226                 :            121 :     entry->end = InvalidXLogRecPtr;
                                227                 :                : 
                                228                 :            121 :     result = lcons(entry, result);
                                229                 :                : 
                                230                 :                :     /*
                                231                 :                :      * If the history file was fetched from archive, save it in pg_wal for
                                232                 :                :      * future reference.
                                233                 :                :      */
 5058                           234         [ +  + ]:            121 :     if (fromArchive)
                                235                 :              3 :         KeepFileRestoredFromArchive(path, histfname);
                                236                 :                : 
 5147                           237                 :            121 :     return result;
                                238                 :                : }
                                239                 :                : 
                                240                 :                : /*
                                241                 :                :  * Probe whether a timeline history file exists for the given timeline ID
                                242                 :                :  */
                                243                 :                : bool
                                244                 :            501 : existsTimeLineHistory(TimeLineID probeTLI)
                                245                 :                : {
                                246                 :                :     char        path[MAXPGPATH];
                                247                 :                :     char        histfname[MAXFNAMELEN];
                                248                 :                :     FILE       *fd;
                                249                 :                : 
                                250                 :                :     /* Timeline 1 does not have a history file, so no need to check */
                                251         [ -  + ]:            501 :     if (probeTLI == 1)
 5147 heikki.linnakangas@i      252                 :UBC           0 :         return false;
                                253                 :                : 
 4991 heikki.linnakangas@i      254         [ +  + ]:CBC         501 :     if (ArchiveRecoveryRequested)
                                255                 :                :     {
 5147                           256                 :            449 :         TLHistoryFileName(histfname, probeTLI);
 5099                           257                 :            449 :         RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0, false);
                                258                 :                :     }
                                259                 :                :     else
 5147                           260                 :             52 :         TLHistoryFilePath(path, probeTLI);
                                261                 :                : 
                                262                 :            500 :     fd = AllocateFile(path, "r");
                                263         [ +  + ]:            500 :     if (fd != NULL)
                                264                 :                :     {
                                265                 :             53 :         FreeFile(fd);
                                266                 :             53 :         return true;
                                267                 :                :     }
                                268                 :                :     else
                                269                 :                :     {
                                270         [ -  + ]:            447 :         if (errno != ENOENT)
 5147 heikki.linnakangas@i      271         [ #  # ]:UBC           0 :             ereport(FATAL,
                                272                 :                :                     (errcode_for_file_access(),
                                273                 :                :                      errmsg("could not open file \"%s\": %m", path)));
 5147 heikki.linnakangas@i      274                 :CBC         447 :         return false;
                                275                 :                :     }
                                276                 :                : }
                                277                 :                : 
                                278                 :                : /*
                                279                 :                :  * Find the newest existing timeline, assuming that startTLI exists.
                                280                 :                :  *
                                281                 :                :  * Note: while this is somewhat heuristic, it does positively guarantee
                                282                 :                :  * that (result + 1) is not a known timeline, and therefore it should
                                283                 :                :  * be safe to assign that ID to a new timeline.
                                284                 :                :  */
                                285                 :                : TimeLineID
                                286                 :            435 : findNewestTimeLine(TimeLineID startTLI)
                                287                 :                : {
                                288                 :                :     TimeLineID  newestTLI;
                                289                 :                :     TimeLineID  probeTLI;
                                290                 :                : 
                                291                 :                :     /*
                                292                 :                :      * The algorithm is just to probe for the existence of timeline history
                                293                 :                :      * files.  XXX is it useful to allow gaps in the sequence?
                                294                 :                :      */
                                295                 :            435 :     newestTLI = startTLI;
                                296                 :                : 
                                297                 :            435 :     for (probeTLI = startTLI + 1;; probeTLI++)
                                298                 :                :     {
                                299         [ +  + ]:            449 :         if (existsTimeLineHistory(probeTLI))
                                300                 :                :         {
 3424 tgl@sss.pgh.pa.us         301                 :             14 :             newestTLI = probeTLI;   /* probeTLI exists */
                                302                 :                :         }
                                303                 :                :         else
                                304                 :                :         {
                                305                 :                :             /* doesn't exist, assume we're done */
 5147 heikki.linnakangas@i      306                 :            434 :             break;
                                307                 :                :         }
                                308                 :                :     }
                                309                 :                : 
                                310                 :            434 :     return newestTLI;
                                311                 :                : }
                                312                 :                : 
                                313                 :                : /*
                                314                 :                :  * Create a new timeline history file.
                                315                 :                :  *
                                316                 :                :  *  newTLI: ID of the new timeline
                                317                 :                :  *  parentTLI: ID of its immediate parent
                                318                 :                :  *  switchpoint: WAL location where the system switched to the new timeline
                                319                 :                :  *  reason: human-readable explanation of why the timeline was switched
                                320                 :                :  *
                                321                 :                :  * Currently this is only used at the end recovery, and so there are no locking
                                322                 :                :  * considerations.  But we should be just as tense as XLogFileInit to avoid
                                323                 :                :  * emplacing a bogus file.
                                324                 :                :  */
                                325                 :                : void
                                326                 :             66 : writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
                                327                 :                :                      XLogRecPtr switchpoint, const char *reason)
                                328                 :                : {
                                329                 :                :     char        path[MAXPGPATH];
                                330                 :                :     char        tmppath[MAXPGPATH];
                                331                 :                :     char        histfname[MAXFNAMELEN];
                                332                 :                :     char        buffer[BLCKSZ];
                                333                 :                :     int         srcfd;
                                334                 :                :     int         fd;
                                335                 :                :     ssize_t     nbytes;
                                336                 :                : 
                                337         [ -  + ]:             66 :     Assert(newTLI > parentTLI); /* else bad selection of newTLI */
                                338                 :                : 
                                339                 :                :     /*
                                340                 :                :      * Write into a temp file name.
                                341                 :                :      */
                                342                 :             66 :     snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
                                343                 :                : 
                                344                 :             66 :     unlink(tmppath);
                                345                 :                : 
                                346                 :                :     /* do not use get_sync_bit() here --- want to fsync only at end of fill */
 3330 peter_e@gmx.net           347                 :             66 :     fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL);
 5147 heikki.linnakangas@i      348         [ -  + ]:             66 :     if (fd < 0)
 5147 heikki.linnakangas@i      349         [ #  # ]:UBC           0 :         ereport(ERROR,
                                350                 :                :                 (errcode_for_file_access(),
                                351                 :                :                  errmsg("could not create file \"%s\": %m", tmppath)));
                                352                 :                : 
                                353                 :                :     /*
                                354                 :                :      * If a history file exists for the parent, copy it verbatim
                                355                 :                :      */
 4991 heikki.linnakangas@i      356         [ +  - ]:CBC          66 :     if (ArchiveRecoveryRequested)
                                357                 :                :     {
 5147                           358                 :             66 :         TLHistoryFileName(histfname, parentTLI);
 5099                           359                 :             66 :         RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0, false);
                                360                 :                :     }
                                361                 :                :     else
 5147 heikki.linnakangas@i      362                 :UBC           0 :         TLHistoryFilePath(path, parentTLI);
                                363                 :                : 
 3330 peter_e@gmx.net           364                 :CBC          66 :     srcfd = OpenTransientFile(path, O_RDONLY);
 5147 heikki.linnakangas@i      365         [ +  + ]:             66 :     if (srcfd < 0)
                                366                 :                :     {
                                367         [ -  + ]:             57 :         if (errno != ENOENT)
 5147 heikki.linnakangas@i      368         [ #  # ]:UBC           0 :             ereport(ERROR,
                                369                 :                :                     (errcode_for_file_access(),
                                370                 :                :                      errmsg("could not open file \"%s\": %m", path)));
                                371                 :                :         /* Not there, so assume parent has no parents */
                                372                 :                :     }
                                373                 :                :     else
                                374                 :                :     {
                                375                 :                :         for (;;)
                                376                 :                :         {
 5147 heikki.linnakangas@i      377                 :CBC           9 :             errno = 0;
 3519 rhaas@postgresql.org      378                 :             18 :             pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_READ);
  113 peter@eisentraut.org      379                 :GNC          18 :             nbytes = read(srcfd, buffer, sizeof(buffer));
 3519 rhaas@postgresql.org      380                 :CBC          18 :             pgstat_report_wait_end();
 5147 heikki.linnakangas@i      381   [ +  -  -  + ]:             18 :             if (nbytes < 0 || errno != 0)
 5147 heikki.linnakangas@i      382         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                383                 :                :                         (errcode_for_file_access(),
                                384                 :                :                          errmsg("could not read file \"%s\": %m", path)));
 5147 heikki.linnakangas@i      385         [ +  + ]:CBC          18 :             if (nbytes == 0)
                                386                 :              9 :                 break;
                                387                 :              9 :             errno = 0;
 3519 rhaas@postgresql.org      388                 :              9 :             pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_WRITE);
  113 peter@eisentraut.org      389         [ -  + ]:GNC           9 :             if (write(fd, buffer, nbytes) != nbytes)
                                390                 :                :             {
 5147 heikki.linnakangas@i      391                 :UBC           0 :                 int         save_errno = errno;
                                392                 :                : 
                                393                 :                :                 /*
                                394                 :                :                  * If we fail to make the file, delete it to release disk
                                395                 :                :                  * space
                                396                 :                :                  */
                                397                 :              0 :                 unlink(tmppath);
                                398                 :                : 
                                399                 :                :                 /*
                                400                 :                :                  * if write didn't set errno, assume problem is no disk space
                                401                 :                :                  */
                                402         [ #  # ]:              0 :                 errno = save_errno ? save_errno : ENOSPC;
                                403                 :                : 
                                404         [ #  # ]:              0 :                 ereport(ERROR,
                                405                 :                :                         (errcode_for_file_access(),
                                406                 :                :                          errmsg("could not write to file \"%s\": %m", tmppath)));
                                407                 :                :             }
 3519 rhaas@postgresql.org      408                 :CBC           9 :             pgstat_report_wait_end();
                                409                 :                :         }
                                410                 :                : 
 2679 peter@eisentraut.org      411         [ -  + ]:              9 :         if (CloseTransientFile(srcfd) != 0)
 2798 michael@paquier.xyz       412         [ #  # ]:UBC           0 :             ereport(ERROR,
                                413                 :                :                     (errcode_for_file_access(),
                                414                 :                :                      errmsg("could not close file \"%s\": %m", path)));
                                415                 :                :     }
                                416                 :                : 
                                417                 :                :     /*
                                418                 :                :      * Append one line with the details of this timeline split.
                                419                 :                :      *
                                420                 :                :      * If we did have a parent file, insert an extra newline just in case the
                                421                 :                :      * parent file failed to end with one.
                                422                 :                :      */
 5147 heikki.linnakangas@i      423                 :CBC          66 :     snprintf(buffer, sizeof(buffer),
                                424                 :                :              "%s%u\t%X/%08X\t%s\n",
                                425                 :                :              (srcfd < 0) ? "" : "\n",
                                426                 :                :              parentTLI,
 2081 peter@eisentraut.org      427         [ +  + ]:             66 :              LSN_FORMAT_ARGS(switchpoint),
                                428                 :                :              reason);
                                429                 :                : 
 5147 heikki.linnakangas@i      430                 :             66 :     nbytes = strlen(buffer);
                                431                 :             66 :     errno = 0;
 2372 fujii@postgresql.org      432                 :             66 :     pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_WRITE);
  113 peter@eisentraut.org      433         [ -  + ]:GNC          66 :     if (write(fd, buffer, nbytes) != nbytes)
                                434                 :                :     {
 5147 heikki.linnakangas@i      435                 :UBC           0 :         int         save_errno = errno;
                                436                 :                : 
                                437                 :                :         /*
                                438                 :                :          * If we fail to make the file, delete it to release disk space
                                439                 :                :          */
                                440                 :              0 :         unlink(tmppath);
                                441                 :                :         /* if write didn't set errno, assume problem is no disk space */
                                442         [ #  # ]:              0 :         errno = save_errno ? save_errno : ENOSPC;
                                443                 :                : 
                                444         [ #  # ]:              0 :         ereport(ERROR,
                                445                 :                :                 (errcode_for_file_access(),
                                446                 :                :                  errmsg("could not write to file \"%s\": %m", tmppath)));
                                447                 :                :     }
 2372 fujii@postgresql.org      448                 :CBC          66 :     pgstat_report_wait_end();
                                449                 :                : 
 3519 rhaas@postgresql.org      450                 :             66 :     pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_SYNC);
 5147 heikki.linnakangas@i      451         [ -  + ]:             66 :     if (pg_fsync(fd) != 0)
 2908 tmunro@postgresql.or      452         [ #  # ]:UBC           0 :         ereport(data_sync_elevel(ERROR),
                                453                 :                :                 (errcode_for_file_access(),
                                454                 :                :                  errmsg("could not fsync file \"%s\": %m", tmppath)));
 3519 rhaas@postgresql.org      455                 :CBC          66 :     pgstat_report_wait_end();
                                456                 :                : 
 2679 peter@eisentraut.org      457         [ -  + ]:             66 :     if (CloseTransientFile(fd) != 0)
 5147 heikki.linnakangas@i      458         [ #  # ]:UBC           0 :         ereport(ERROR,
                                459                 :                :                 (errcode_for_file_access(),
                                460                 :                :                  errmsg("could not close file \"%s\": %m", tmppath)));
                                461                 :                : 
                                462                 :                :     /*
                                463                 :                :      * Now move the completed history file into place with its final name.
                                464                 :                :      */
 5147 heikki.linnakangas@i      465                 :CBC          66 :     TLHistoryFilePath(path, newTLI);
 1584 michael@paquier.xyz       466   [ +  -  -  + ]:             66 :     Assert(access(path, F_OK) != 0 && errno == ENOENT);
                                467                 :             66 :     durable_rename(tmppath, path, ERROR);
                                468                 :                : 
                                469                 :                :     /* The history file can be archived immediately. */
 4382 fujii@postgresql.org      470   [ +  +  -  +  :             66 :     if (XLogArchivingActive())
                                              +  + ]
                                471                 :                :     {
                                472                 :             16 :         TLHistoryFileName(histfname, newTLI);
 1888 alvherre@alvh.no-ip.      473                 :             16 :         XLogArchiveNotify(histfname);
                                474                 :                :     }
 5147 heikki.linnakangas@i      475                 :             66 : }
                                476                 :                : 
                                477                 :                : /*
                                478                 :                :  * Writes a history file for given timeline and contents.
                                479                 :                :  *
                                480                 :                :  * Currently this is only used in the walreceiver process, and so there are
                                481                 :                :  * no locking considerations.  But we should be just as tense as XLogFileInit
                                482                 :                :  * to avoid emplacing a bogus file.
                                483                 :                :  */
                                484                 :                : void
  113 peter@eisentraut.org      485                 :GNC          13 : writeTimeLineHistoryFile(TimeLineID tli, const char *content, size_t size)
                                486                 :                : {
                                487                 :                :     char        path[MAXPGPATH];
                                488                 :                :     char        tmppath[MAXPGPATH];
                                489                 :                :     int         fd;
                                490                 :                : 
                                491                 :                :     /*
                                492                 :                :      * Write into a temp file name.
                                493                 :                :      */
 5075 heikki.linnakangas@i      494                 :CBC          13 :     snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
                                495                 :                : 
                                496                 :             13 :     unlink(tmppath);
                                497                 :                : 
                                498                 :                :     /* do not use get_sync_bit() here --- want to fsync only at end of fill */
 3330 peter_e@gmx.net           499                 :             13 :     fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL);
 5075 heikki.linnakangas@i      500         [ -  + ]:             13 :     if (fd < 0)
 5075 heikki.linnakangas@i      501         [ #  # ]:UBC           0 :         ereport(ERROR,
                                502                 :                :                 (errcode_for_file_access(),
                                503                 :                :                  errmsg("could not create file \"%s\": %m", tmppath)));
                                504                 :                : 
 5075 heikki.linnakangas@i      505                 :CBC          13 :     errno = 0;
 3519 rhaas@postgresql.org      506                 :             13 :     pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE);
  113 peter@eisentraut.org      507         [ -  + ]:GNC          13 :     if (write(fd, content, size) != size)
                                508                 :                :     {
 5075 heikki.linnakangas@i      509                 :UBC           0 :         int         save_errno = errno;
                                510                 :                : 
                                511                 :                :         /*
                                512                 :                :          * If we fail to make the file, delete it to release disk space
                                513                 :                :          */
                                514                 :              0 :         unlink(tmppath);
                                515                 :                :         /* if write didn't set errno, assume problem is no disk space */
                                516         [ #  # ]:              0 :         errno = save_errno ? save_errno : ENOSPC;
                                517                 :                : 
                                518         [ #  # ]:              0 :         ereport(ERROR,
                                519                 :                :                 (errcode_for_file_access(),
                                520                 :                :                  errmsg("could not write to file \"%s\": %m", tmppath)));
                                521                 :                :     }
 3519 rhaas@postgresql.org      522                 :CBC          13 :     pgstat_report_wait_end();
                                523                 :                : 
                                524                 :             13 :     pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_FILE_SYNC);
 5075 heikki.linnakangas@i      525         [ -  + ]:             13 :     if (pg_fsync(fd) != 0)
 2908 tmunro@postgresql.or      526         [ #  # ]:UBC           0 :         ereport(data_sync_elevel(ERROR),
                                527                 :                :                 (errcode_for_file_access(),
                                528                 :                :                  errmsg("could not fsync file \"%s\": %m", tmppath)));
 3519 rhaas@postgresql.org      529                 :CBC          13 :     pgstat_report_wait_end();
                                530                 :                : 
 2679 peter@eisentraut.org      531         [ -  + ]:             13 :     if (CloseTransientFile(fd) != 0)
 5075 heikki.linnakangas@i      532         [ #  # ]:UBC           0 :         ereport(ERROR,
                                533                 :                :                 (errcode_for_file_access(),
                                534                 :                :                  errmsg("could not close file \"%s\": %m", tmppath)));
                                535                 :                : 
                                536                 :                :     /*
                                537                 :                :      * Now move the completed history file into place with its final name,
                                538                 :                :      * replacing any existing file with the same name.
                                539                 :                :      */
 5075 heikki.linnakangas@i      540                 :CBC          13 :     TLHistoryFilePath(path, tli);
 1584 michael@paquier.xyz       541                 :             13 :     durable_rename(tmppath, path, ERROR);
 5075 heikki.linnakangas@i      542                 :             13 : }
                                543                 :                : 
                                544                 :                : /*
                                545                 :                :  * Returns true if 'expectedTLEs' contains a timeline with id 'tli'
                                546                 :                :  */
                                547                 :                : bool
 5084                           548                 :        2943644 : tliInHistory(TimeLineID tli, List *expectedTLEs)
                                549                 :                : {
                                550                 :                :     ListCell   *cell;
                                551                 :                : 
                                552   [ +  -  +  -  :        2955216 :     foreach(cell, expectedTLEs)
                                              +  - ]
                                553                 :                :     {
                                554         [ +  + ]:        2955216 :         if (((TimeLineHistoryEntry *) lfirst(cell))->tli == tli)
                                555                 :        2943644 :             return true;
                                556                 :                :     }
                                557                 :                : 
 5084 heikki.linnakangas@i      558                 :UBC           0 :     return false;
                                559                 :                : }
                                560                 :                : 
                                561                 :                : /*
                                562                 :                :  * Returns the ID of the timeline in use at a particular point in time, in
                                563                 :                :  * the given timeline history.
                                564                 :                :  */
                                565                 :                : TimeLineID
 5084 heikki.linnakangas@i      566                 :CBC        3030 : tliOfPointInHistory(XLogRecPtr ptr, List *history)
                                567                 :                : {
                                568                 :                :     ListCell   *cell;
                                569                 :                : 
                                570   [ +  -  +  -  :           3044 :     foreach(cell, history)
                                              +  - ]
                                571                 :                :     {
                                572                 :           3044 :         TimeLineHistoryEntry *tle = (TimeLineHistoryEntry *) lfirst(cell);
                                573                 :                : 
  364 alvherre@kurilemu.de      574   [ +  +  +  + ]:           3044 :         if ((!XLogRecPtrIsValid(tle->begin) || tle->begin <= ptr) &&
                                575   [ +  +  +  - ]:           3030 :             (!XLogRecPtrIsValid(tle->end) || ptr < tle->end))
                                576                 :                :         {
                                577                 :                :             /* found it */
 5084 heikki.linnakangas@i      578                 :           3030 :             return tle->tli;
                                579                 :                :         }
                                580                 :                :     }
                                581                 :                : 
                                582                 :                :     /* shouldn't happen. */
 5084 heikki.linnakangas@i      583         [ #  # ]:UBC           0 :     elog(ERROR, "timeline history was not contiguous");
                                584                 :                :     return 0;                   /* keep compiler quiet */
                                585                 :                : }
                                586                 :                : 
                                587                 :                : /*
                                588                 :                :  * Returns the point in history where we branched off the given timeline,
                                589                 :                :  * and the timeline we branched to (*nextTLI). Returns InvalidXLogRecPtr if
                                590                 :                :  * the timeline is current, ie. we have not branched off from it, and throws
                                591                 :                :  * an error if the timeline is not part of this server's history.
                                592                 :                :  */
                                593                 :                : XLogRecPtr
 5040 heikki.linnakangas@i      594                 :CBC        1636 : tliSwitchPoint(TimeLineID tli, List *history, TimeLineID *nextTLI)
                                595                 :                : {
                                596                 :                :     ListCell   *cell;
                                597                 :                : 
                                598         [ +  + ]:           1636 :     if (nextTLI)
                                599                 :           1635 :         *nextTLI = 0;
 4908 bruce@momjian.us          600   [ +  -  +  -  :           1652 :     foreach(cell, history)
                                              +  - ]
                                601                 :                :     {
 5084 heikki.linnakangas@i      602                 :           1652 :         TimeLineHistoryEntry *tle = (TimeLineHistoryEntry *) lfirst(cell);
                                603                 :                : 
                                604         [ +  + ]:           1652 :         if (tle->tli == tli)
                                605                 :           1636 :             return tle->end;
 5040                           606         [ +  + ]:             16 :         if (nextTLI)
                                607                 :             15 :             *nextTLI = tle->tli;
                                608                 :                :     }
                                609                 :                : 
 5084 heikki.linnakangas@i      610         [ #  # ]:UBC           0 :     ereport(ERROR,
                                611                 :                :             (errmsg("requested timeline %u is not in this server's history",
                                612                 :                :                     tli)));
                                613                 :                :     return InvalidXLogRecPtr;   /* keep compiler quiet */
                                614                 :                : }
        

Generated by: LCOV version 2.0-1