PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
timeline.c File Reference
#include "postgres_fe.h"
#include "pg_rewind.h"
#include "access/timeline.h"
#include "access/xlog_internal.h"
Include dependency graph for timeline.c:

Go to the source code of this file.

Functions

TimeLineHistoryEntryrewind_parseTimeLineHistory (char *buffer, TimeLineID targetTLI, int *nentries)
 

Function Documentation

TimeLineHistoryEntry* rewind_parseTimeLineHistory ( char *  buffer,
TimeLineID  targetTLI,
int *  nentries 
)

Definition at line 30 of file timeline.c.

References _, TimeLineHistoryEntry::begin, buffer, TimeLineHistoryEntry::end, InvalidXLogRecPtr, NULL, pg_malloc(), pg_realloc(), and TimeLineHistoryEntry::tli.

Referenced by getTimelineHistory().

31 {
32  char *fline;
33  TimeLineHistoryEntry *entry;
34  TimeLineHistoryEntry *entries = NULL;
35  int nlines = 0;
36  TimeLineID lasttli = 0;
37  XLogRecPtr prevend;
38  char *bufptr;
39  bool lastline = false;
40 
41  /*
42  * Parse the file...
43  */
44  prevend = InvalidXLogRecPtr;
45  bufptr = buffer;
46  while (!lastline)
47  {
48  char *ptr;
49  TimeLineID tli;
50  uint32 switchpoint_hi;
51  uint32 switchpoint_lo;
52  int nfields;
53 
54  fline = bufptr;
55  while (*bufptr && *bufptr != '\n')
56  bufptr++;
57  if (!(*bufptr))
58  lastline = true;
59  else
60  *bufptr++ = '\0';
61 
62  /* skip leading whitespace and check for # comment */
63  for (ptr = fline; *ptr; ptr++)
64  {
65  if (!isspace((unsigned char) *ptr))
66  break;
67  }
68  if (*ptr == '\0' || *ptr == '#')
69  continue;
70 
71  nfields = sscanf(fline, "%u\t%X/%X", &tli, &switchpoint_hi, &switchpoint_lo);
72 
73  if (nfields < 1)
74  {
75  /* expect a numeric timeline ID as first field of line */
76  fprintf(stderr, _("syntax error in history file: %s\n"), fline);
77  fprintf(stderr, _("Expected a numeric timeline ID.\n"));
78  exit(1);
79  }
80  if (nfields != 3)
81  {
82  fprintf(stderr, _("syntax error in history file: %s\n"), fline);
83  fprintf(stderr, _("Expected a write-ahead log switchpoint location.\n"));
84  exit(1);
85  }
86  if (entries && tli <= lasttli)
87  {
88  fprintf(stderr, _("invalid data in history file: %s\n"), fline);
89  fprintf(stderr, _("Timeline IDs must be in increasing sequence.\n"));
90  exit(1);
91  }
92 
93  lasttli = tli;
94 
95  nlines++;
96  entries = pg_realloc(entries, nlines * sizeof(TimeLineHistoryEntry));
97 
98  entry = &entries[nlines - 1];
99  entry->tli = tli;
100  entry->begin = prevend;
101  entry->end = ((uint64) (switchpoint_hi)) << 32 | (uint64) switchpoint_lo;
102  prevend = entry->end;
103 
104  /* we ignore the remainder of each line */
105  }
106 
107  if (entries && targetTLI <= lasttli)
108  {
109  fprintf(stderr, _("invalid data in history file\n"));
110  fprintf(stderr, _("Timeline IDs must be less than child timeline's ID.\n"));
111  exit(1);
112  }
113 
114  /*
115  * Create one more entry for the "tip" of the timeline, which has no entry
116  * in the history file.
117  */
118  nlines++;
119  if (entries)
120  entries = pg_realloc(entries, nlines * sizeof(TimeLineHistoryEntry));
121  else
122  entries = pg_malloc(1 * sizeof(TimeLineHistoryEntry));
123 
124  entry = &entries[nlines - 1];
125  entry->tli = targetTLI;
126  entry->begin = prevend;
127  entry->end = InvalidXLogRecPtr;
128 
129  *nentries = nlines;
130  return entries;
131 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:45
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
TimeLineID tli
Definition: timeline.h:27
unsigned int uint32
Definition: c.h:268
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define NULL
Definition: c.h:229
XLogRecPtr end
Definition: timeline.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:211
XLogRecPtr begin
Definition: timeline.h:28
#define _(x)
Definition: elog.c:84