PostgreSQL Source Code  git master
pg_lsn.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
Include dependency graph for pg_lsn.c:

Go to the source code of this file.

Macros

#define MAXPG_LSNLEN   17
 
#define MAXPG_LSNCOMPONENT   8
 

Functions

XLogRecPtr pg_lsn_in_internal (const char *str, bool *have_error)
 
Datum pg_lsn_in (PG_FUNCTION_ARGS)
 
Datum pg_lsn_out (PG_FUNCTION_ARGS)
 
Datum pg_lsn_recv (PG_FUNCTION_ARGS)
 
Datum pg_lsn_send (PG_FUNCTION_ARGS)
 
Datum pg_lsn_eq (PG_FUNCTION_ARGS)
 
Datum pg_lsn_ne (PG_FUNCTION_ARGS)
 
Datum pg_lsn_lt (PG_FUNCTION_ARGS)
 
Datum pg_lsn_gt (PG_FUNCTION_ARGS)
 
Datum pg_lsn_le (PG_FUNCTION_ARGS)
 
Datum pg_lsn_ge (PG_FUNCTION_ARGS)
 
Datum pg_lsn_larger (PG_FUNCTION_ARGS)
 
Datum pg_lsn_smaller (PG_FUNCTION_ARGS)
 
Datum pg_lsn_cmp (PG_FUNCTION_ARGS)
 
Datum pg_lsn_hash (PG_FUNCTION_ARGS)
 
Datum pg_lsn_hash_extended (PG_FUNCTION_ARGS)
 
Datum pg_lsn_mi (PG_FUNCTION_ARGS)
 
Datum pg_lsn_pli (PG_FUNCTION_ARGS)
 
Datum pg_lsn_mii (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MAXPG_LSNCOMPONENT

#define MAXPG_LSNCOMPONENT   8

Definition at line 23 of file pg_lsn.c.

Referenced by pg_lsn_in_internal().

◆ MAXPG_LSNLEN

#define MAXPG_LSNLEN   17

Definition at line 22 of file pg_lsn.c.

Referenced by pg_lsn_out().

Function Documentation

◆ pg_lsn_cmp()

Datum pg_lsn_cmp ( PG_FUNCTION_ARGS  )

Definition at line 198 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_INT32.

199 {
200  XLogRecPtr a = PG_GETARG_LSN(0);
201  XLogRecPtr b = PG_GETARG_LSN(1);
202 
203  if (a > b)
204  PG_RETURN_INT32(1);
205  else if (a == b)
206  PG_RETURN_INT32(0);
207  else
208  PG_RETURN_INT32(-1);
209 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_eq()

Datum pg_lsn_eq ( PG_FUNCTION_ARGS  )

Definition at line 125 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

126 {
127  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
128  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
129 
130  PG_RETURN_BOOL(lsn1 == lsn2);
131 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_ge()

Datum pg_lsn_ge ( PG_FUNCTION_ARGS  )

Definition at line 170 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

171 {
172  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
173  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
174 
175  PG_RETURN_BOOL(lsn1 >= lsn2);
176 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_gt()

Datum pg_lsn_gt ( PG_FUNCTION_ARGS  )

Definition at line 152 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

153 {
154  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
155  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
156 
157  PG_RETURN_BOOL(lsn1 > lsn2);
158 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_hash()

Datum pg_lsn_hash ( PG_FUNCTION_ARGS  )

Definition at line 213 of file pg_lsn.c.

References hashint8().

214 {
215  /* We can use hashint8 directly */
216  return hashint8(fcinfo);
217 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83

◆ pg_lsn_hash_extended()

Datum pg_lsn_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 220 of file pg_lsn.c.

References hashint8extended().

221 {
222  return hashint8extended(fcinfo);
223 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103

◆ pg_lsn_in()

Datum pg_lsn_in ( PG_FUNCTION_ARGS  )

Definition at line 64 of file pg_lsn.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_CSTRING, pg_lsn_in_internal(), PG_RETURN_LSN, and generate_unaccent_rules::str.

Referenced by libpqrcv_create_slot().

65 {
66  char *str = PG_GETARG_CSTRING(0);
67  XLogRecPtr result;
68  bool have_error = false;
69 
70  result = pg_lsn_in_internal(str, &have_error);
71  if (have_error)
72  ereport(ERROR,
73  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
74  errmsg("invalid input syntax for type %s: \"%s\"",
75  "pg_lsn", str)));
76 
77  PG_RETURN_LSN(result);
78 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
XLogRecPtr pg_lsn_in_internal(const char *str, bool *have_error)
Definition: pg_lsn.c:30
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277

◆ pg_lsn_in_internal()

XLogRecPtr pg_lsn_in_internal ( const char *  str,
bool have_error 
)

Definition at line 30 of file pg_lsn.c.

References Assert, InvalidXLogRecPtr, and MAXPG_LSNCOMPONENT.

Referenced by check_recovery_target_lsn(), and pg_lsn_in().

31 {
32  int len1,
33  len2;
34  uint32 id,
35  off;
36  XLogRecPtr result;
37 
38  Assert(have_error != NULL);
39  *have_error = false;
40 
41  /* Sanity check input format. */
42  len1 = strspn(str, "0123456789abcdefABCDEF");
43  if (len1 < 1 || len1 > MAXPG_LSNCOMPONENT || str[len1] != '/')
44  {
45  *have_error = true;
46  return InvalidXLogRecPtr;
47  }
48  len2 = strspn(str + len1 + 1, "0123456789abcdefABCDEF");
49  if (len2 < 1 || len2 > MAXPG_LSNCOMPONENT || str[len1 + 1 + len2] != '\0')
50  {
51  *have_error = true;
52  return InvalidXLogRecPtr;
53  }
54 
55  /* Decode result. */
56  id = (uint32) strtoul(str, NULL, 16);
57  off = (uint32) strtoul(str + len1 + 1, NULL, 16);
58  result = ((uint64) id << 32) | off;
59 
60  return result;
61 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
unsigned int uint32
Definition: c.h:374
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define MAXPG_LSNCOMPONENT
Definition: pg_lsn.c:23

◆ pg_lsn_larger()

Datum pg_lsn_larger ( PG_FUNCTION_ARGS  )

Definition at line 179 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_LSN.

180 {
181  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
182  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
183 
184  PG_RETURN_LSN((lsn1 > lsn2) ? lsn1 : lsn2);
185 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_le()

Datum pg_lsn_le ( PG_FUNCTION_ARGS  )

Definition at line 161 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

162 {
163  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
164  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
165 
166  PG_RETURN_BOOL(lsn1 <= lsn2);
167 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_lt()

Datum pg_lsn_lt ( PG_FUNCTION_ARGS  )

Definition at line 143 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

144 {
145  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
146  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
147 
148  PG_RETURN_BOOL(lsn1 < lsn2);
149 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_mi()

Datum pg_lsn_mi ( PG_FUNCTION_ARGS  )

Definition at line 231 of file pg_lsn.c.

References buf, CStringGetDatum, DirectFunctionCall3, Int32GetDatum, numeric_in(), ObjectIdGetDatum, PG_GETARG_LSN, snprintf, and UINT64_FORMAT.

Referenced by pg_wal_lsn_diff().

232 {
233  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
234  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
235  char buf[256];
236  Datum result;
237 
238  /* Output could be as large as plus or minus 2^63 - 1. */
239  if (lsn1 < lsn2)
240  snprintf(buf, sizeof buf, "-" UINT64_FORMAT, lsn2 - lsn1);
241  else
242  snprintf(buf, sizeof buf, UINT64_FORMAT, lsn1 - lsn2);
243 
244  /* Convert to numeric. */
246  CStringGetDatum(buf),
247  ObjectIdGetDatum(0),
248  Int32GetDatum(-1));
249 
250  return result;
251 }
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
static char * buf
Definition: pg_test_fsync.c:67
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:620
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uintptr_t Datum
Definition: postgres.h:367
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Int32GetDatum(X)
Definition: postgres.h:479
#define snprintf
Definition: port.h:193
#define UINT64_FORMAT
Definition: c.h:417

◆ pg_lsn_mii()

Datum pg_lsn_mii ( PG_FUNCTION_ARGS  )

Definition at line 292 of file pg_lsn.c.

References buf, CStringGetDatum, DirectFunctionCall1, DirectFunctionCall2, DirectFunctionCall3, ereport, errcode(), errmsg(), ERROR, Int32GetDatum, numeric_in(), numeric_is_nan(), numeric_pg_lsn(), numeric_sub(), NumericGetDatum, ObjectIdGetDatum, PG_GETARG_LSN, PG_GETARG_NUMERIC, snprintf, and UINT64_FORMAT.

293 {
294  XLogRecPtr lsn = PG_GETARG_LSN(0);
295  Numeric nbytes = PG_GETARG_NUMERIC(1);
296  Datum num;
297  Datum res;
298  char buf[32];
299 
300  if (numeric_is_nan(nbytes))
301  ereport(ERROR,
302  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
303  errmsg("cannot subtract NaN from pg_lsn")));
304 
305  /* Convert to numeric */
306  snprintf(buf, sizeof(buf), UINT64_FORMAT, lsn);
308  CStringGetDatum(buf),
309  ObjectIdGetDatum(0),
310  Int32GetDatum(-1));
311 
312  /* Subtract two numerics */
314  NumericGetDatum(num),
315  NumericGetDatum(nbytes));
316 
317  /* Convert to pg_lsn */
318  return DirectFunctionCall1(numeric_pg_lsn, res);
319 }
#define NumericGetDatum(X)
Definition: numeric.h:51
int errcode(int sqlerrcode)
Definition: elog.c:610
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
bool numeric_is_nan(Numeric num)
Definition: numeric.c:773
Datum numeric_pg_lsn(PG_FUNCTION_ARGS)
Definition: numeric.c:4439
static char * buf
Definition: pg_test_fsync.c:67
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:620
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uintptr_t Datum
Definition: postgres.h:367
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2786
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define snprintf
Definition: port.h:193
#define UINT64_FORMAT
Definition: c.h:417
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ pg_lsn_ne()

Datum pg_lsn_ne ( PG_FUNCTION_ARGS  )

Definition at line 134 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

135 {
136  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
137  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
138 
139  PG_RETURN_BOOL(lsn1 != lsn2);
140 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_out()

Datum pg_lsn_out ( PG_FUNCTION_ARGS  )

Definition at line 81 of file pg_lsn.c.

References buf, MAXPG_LSNLEN, PG_GETARG_LSN, PG_RETURN_CSTRING, pstrdup(), and snprintf.

82 {
83  XLogRecPtr lsn = PG_GETARG_LSN(0);
84  char buf[MAXPG_LSNLEN + 1];
85  char *result;
86  uint32 id,
87  off;
88 
89  /* Decode ID and offset */
90  id = (uint32) (lsn >> 32);
91  off = (uint32) lsn;
92 
93  snprintf(buf, sizeof buf, "%X/%X", id, off);
94  result = pstrdup(buf);
95  PG_RETURN_CSTRING(result);
96 }
#define MAXPG_LSNLEN
Definition: pg_lsn.c:22
char * pstrdup(const char *in)
Definition: mcxt.c:1186
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:374
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
#define snprintf
Definition: port.h:193

◆ pg_lsn_pli()

Datum pg_lsn_pli ( PG_FUNCTION_ARGS  )

Definition at line 258 of file pg_lsn.c.

References buf, CStringGetDatum, DirectFunctionCall1, DirectFunctionCall2, DirectFunctionCall3, ereport, errcode(), errmsg(), ERROR, Int32GetDatum, numeric_add(), numeric_in(), numeric_is_nan(), numeric_pg_lsn(), NumericGetDatum, ObjectIdGetDatum, PG_GETARG_LSN, PG_GETARG_NUMERIC, snprintf, and UINT64_FORMAT.

259 {
260  XLogRecPtr lsn = PG_GETARG_LSN(0);
261  Numeric nbytes = PG_GETARG_NUMERIC(1);
262  Datum num;
263  Datum res;
264  char buf[32];
265 
266  if (numeric_is_nan(nbytes))
267  ereport(ERROR,
268  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
269  errmsg("cannot add NaN to pg_lsn")));
270 
271  /* Convert to numeric */
272  snprintf(buf, sizeof(buf), UINT64_FORMAT, lsn);
274  CStringGetDatum(buf),
275  ObjectIdGetDatum(0),
276  Int32GetDatum(-1));
277 
278  /* Add two numerics */
280  NumericGetDatum(num),
281  NumericGetDatum(nbytes));
282 
283  /* Convert to pg_lsn */
284  return DirectFunctionCall1(numeric_pg_lsn, res);
285 }
#define NumericGetDatum(X)
Definition: numeric.h:51
int errcode(int sqlerrcode)
Definition: elog.c:610
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
bool numeric_is_nan(Numeric num)
Definition: numeric.c:773
Datum numeric_pg_lsn(PG_FUNCTION_ARGS)
Definition: numeric.c:4439
static char * buf
Definition: pg_test_fsync.c:67
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:620
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uintptr_t Datum
Definition: postgres.h:367
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define snprintf
Definition: port.h:193
Datum numeric_add(PG_FUNCTION_ARGS)
Definition: numeric.c:2709
#define UINT64_FORMAT
Definition: c.h:417
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ pg_lsn_recv()

Datum pg_lsn_recv ( PG_FUNCTION_ARGS  )

Definition at line 99 of file pg_lsn.c.

References buf, PG_GETARG_POINTER, PG_RETURN_LSN, and pq_getmsgint64().

100 {
102  XLogRecPtr result;
103 
104  result = pq_getmsgint64(buf);
105  PG_RETURN_LSN(result);
106 }
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
static char * buf
Definition: pg_test_fsync.c:67
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455

◆ pg_lsn_send()

Datum pg_lsn_send ( PG_FUNCTION_ARGS  )

Definition at line 109 of file pg_lsn.c.

References buf, PG_GETARG_LSN, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint64().

110 {
111  XLogRecPtr lsn = PG_GETARG_LSN(0);
113 
114  pq_begintypsend(&buf);
115  pq_sendint64(&buf, lsn);
117 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_smaller()

Datum pg_lsn_smaller ( PG_FUNCTION_ARGS  )

Definition at line 188 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_LSN.

189 {
190  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
191  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
192 
193  PG_RETURN_LSN((lsn1 < lsn2) ? lsn1 : lsn2);
194 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21