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/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)
 

Macro Definition Documentation

◆ MAXPG_LSNCOMPONENT

#define MAXPG_LSNCOMPONENT   8

Definition at line 22 of file pg_lsn.c.

Referenced by pg_lsn_in_internal().

◆ MAXPG_LSNLEN

#define MAXPG_LSNLEN   17

Definition at line 21 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 197 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_INT32.

198 {
199  XLogRecPtr a = PG_GETARG_LSN(0);
200  XLogRecPtr b = PG_GETARG_LSN(1);
201 
202  if (a > b)
203  PG_RETURN_INT32(1);
204  else if (a == b)
205  PG_RETURN_INT32(0);
206  else
207  PG_RETURN_INT32(-1);
208 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#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 124 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

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

◆ pg_lsn_ge()

Datum pg_lsn_ge ( PG_FUNCTION_ARGS  )

Definition at line 169 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

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

◆ pg_lsn_gt()

Datum pg_lsn_gt ( PG_FUNCTION_ARGS  )

Definition at line 151 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

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

◆ pg_lsn_hash()

Datum pg_lsn_hash ( PG_FUNCTION_ARGS  )

Definition at line 212 of file pg_lsn.c.

References hashint8().

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

◆ pg_lsn_hash_extended()

Datum pg_lsn_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 219 of file pg_lsn.c.

References hashint8extended().

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

◆ pg_lsn_in()

Datum pg_lsn_in ( PG_FUNCTION_ARGS  )

Definition at line 63 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().

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

◆ pg_lsn_in_internal()

XLogRecPtr pg_lsn_in_internal ( const char *  str,
bool have_error 
)

Definition at line 29 of file pg_lsn.c.

References Assert, InvalidXLogRecPtr, and MAXPG_LSNCOMPONENT.

Referenced by check_recovery_target_lsn(), and pg_lsn_in().

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

◆ pg_lsn_larger()

Datum pg_lsn_larger ( PG_FUNCTION_ARGS  )

Definition at line 178 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_LSN.

179 {
180  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
181  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
182 
183  PG_RETURN_LSN((lsn1 > lsn2) ? lsn1 : lsn2);
184 }
#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 160 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

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

◆ pg_lsn_lt()

Datum pg_lsn_lt ( PG_FUNCTION_ARGS  )

Definition at line 142 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

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

◆ pg_lsn_mi()

Datum pg_lsn_mi ( PG_FUNCTION_ARGS  )

Definition at line 230 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().

231 {
232  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
233  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
234  char buf[256];
235  Datum result;
236 
237  /* Output could be as large as plus or minus 2^63 - 1. */
238  if (lsn1 < lsn2)
239  snprintf(buf, sizeof buf, "-" UINT64_FORMAT, lsn2 - lsn1);
240  else
241  snprintf(buf, sizeof buf, UINT64_FORMAT, lsn1 - lsn2);
242 
243  /* Convert to numeric. */
245  CStringGetDatum(buf),
246  ObjectIdGetDatum(0),
247  Int32GetDatum(-1));
248 
249  return result;
250 }
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
static char * buf
Definition: pg_test_fsync.c:68
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:573
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:621
#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:192
#define UINT64_FORMAT
Definition: c.h:401

◆ pg_lsn_ne()

Datum pg_lsn_ne ( PG_FUNCTION_ARGS  )

Definition at line 133 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

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

◆ pg_lsn_out()

Datum pg_lsn_out ( PG_FUNCTION_ARGS  )

Definition at line 80 of file pg_lsn.c.

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

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

◆ pg_lsn_recv()

Datum pg_lsn_recv ( PG_FUNCTION_ARGS  )

Definition at line 98 of file pg_lsn.c.

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

99 {
101  XLogRecPtr result;
102 
103  result = pq_getmsgint64(buf);
104  PG_RETURN_LSN(result);
105 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
static char * buf
Definition: pg_test_fsync.c:68
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 108 of file pg_lsn.c.

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

109 {
110  XLogRecPtr lsn = PG_GETARG_LSN(0);
112 
113  pq_begintypsend(&buf);
114  pq_sendint64(&buf, lsn);
116 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
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:68
#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 187 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_LSN.

188 {
189  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
190  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
191 
192  PG_RETURN_LSN((lsn1 < lsn2) ? lsn1 : lsn2);
193 }
#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