PostgreSQL Source Code  git master
pg_lsn.c File Reference
#include "postgres.h"
#include "access/hash.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

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_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 23 of file pg_lsn.c.

Referenced by pg_lsn_in().

◆ 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 161 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_INT32.

162 {
163  XLogRecPtr a = PG_GETARG_LSN(0);
164  XLogRecPtr b = PG_GETARG_LSN(1);
165 
166  if (a > b)
167  PG_RETURN_INT32(1);
168  else if (a == b)
169  PG_RETURN_INT32(0);
170  else
171  PG_RETURN_INT32(-1);
172 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#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 106 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

107 {
108  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
109  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
110 
111  PG_RETURN_BOOL(lsn1 == lsn2);
112 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_ge()

Datum pg_lsn_ge ( 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:319
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_gt()

Datum pg_lsn_gt ( 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:319
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_hash()

Datum pg_lsn_hash ( PG_FUNCTION_ARGS  )

Definition at line 176 of file pg_lsn.c.

References hashint8().

177 {
178  /* We can use hashint8 directly */
179  return hashint8(fcinfo);
180 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:80

◆ pg_lsn_hash_extended()

Datum pg_lsn_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 183 of file pg_lsn.c.

References hashint8extended().

184 {
185  return hashint8extended(fcinfo);
186 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:100

◆ pg_lsn_in()

Datum pg_lsn_in ( PG_FUNCTION_ARGS  )

Definition at line 30 of file pg_lsn.c.

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

Referenced by libpqrcv_create_slot(), and readRecoveryCommandFile().

31 {
32  char *str = PG_GETARG_CSTRING(0);
33  int len1,
34  len2;
35  uint32 id,
36  off;
37  XLogRecPtr result;
38 
39  /* Sanity check input format. */
40  len1 = strspn(str, "0123456789abcdefABCDEF");
41  if (len1 < 1 || len1 > MAXPG_LSNCOMPONENT || str[len1] != '/')
42  ereport(ERROR,
43  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
44  errmsg("invalid input syntax for type %s: \"%s\"",
45  "pg_lsn", str)));
46  len2 = strspn(str + len1 + 1, "0123456789abcdefABCDEF");
47  if (len2 < 1 || len2 > MAXPG_LSNCOMPONENT || str[len1 + 1 + len2] != '\0')
48  ereport(ERROR,
49  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
50  errmsg("invalid input syntax for type %s: \"%s\"",
51  "pg_lsn", str)));
52 
53  /* Decode result. */
54  id = (uint32) strtoul(str, NULL, 16);
55  off = (uint32) strtoul(str + len1 + 1, NULL, 16);
56  result = ((uint64) id << 32) | off;
57 
58  PG_RETURN_LSN(result);
59 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:306
#define ereport(elevel, rest)
Definition: elog.h:122
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define MAXPG_LSNCOMPONENT
Definition: pg_lsn.c:23

◆ pg_lsn_le()

Datum pg_lsn_le ( 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:319
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_lt()

Datum pg_lsn_lt ( 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:319
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_mi()

Datum pg_lsn_mi ( PG_FUNCTION_ARGS  )

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

195 {
196  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
197  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
198  char buf[256];
199  Datum result;
200 
201  /* Output could be as large as plus or minus 2^63 - 1. */
202  if (lsn1 < lsn2)
203  snprintf(buf, sizeof buf, "-" UINT64_FORMAT, lsn2 - lsn1);
204  else
205  snprintf(buf, sizeof buf, UINT64_FORMAT, lsn1 - lsn2);
206 
207  /* Convert to numeric. */
209  CStringGetDatum(buf),
210  ObjectIdGetDatum(0),
211  Int32GetDatum(-1));
212 
213  return result;
214 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
static char * buf
Definition: pg_test_fsync.c:67
#define CStringGetDatum(X)
Definition: postgres.h:584
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:570
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:589
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uintptr_t Datum
Definition: postgres.h:372
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Int32GetDatum(X)
Definition: postgres.h:485
#define UINT64_FORMAT
Definition: c.h:349

◆ pg_lsn_ne()

Datum pg_lsn_ne ( PG_FUNCTION_ARGS  )

Definition at line 115 of file pg_lsn.c.

References PG_GETARG_LSN, and PG_RETURN_BOOL.

116 {
117  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
118  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
119 
120  PG_RETURN_BOOL(lsn1 != lsn2);
121 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ pg_lsn_out()

Datum pg_lsn_out ( PG_FUNCTION_ARGS  )

Definition at line 62 of file pg_lsn.c.

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

63 {
64  XLogRecPtr lsn = PG_GETARG_LSN(0);
65  char buf[MAXPG_LSNLEN + 1];
66  char *result;
67  uint32 id,
68  off;
69 
70  /* Decode ID and offset */
71  id = (uint32) (lsn >> 32);
72  off = (uint32) lsn;
73 
74  snprintf(buf, sizeof buf, "%X/%X", id, off);
75  result = pstrdup(buf);
76  PG_RETURN_CSTRING(result);
77 }
#define MAXPG_LSNLEN
Definition: pg_lsn.c:22
char * pstrdup(const char *in)
Definition: mcxt.c:1063
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:306
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322

◆ pg_lsn_recv()

Datum pg_lsn_recv ( PG_FUNCTION_ARGS  )

Definition at line 80 of file pg_lsn.c.

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

81 {
83  XLogRecPtr result;
84 
85  result = pq_getmsgint64(buf);
86  PG_RETURN_LSN(result);
87 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#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 90 of file pg_lsn.c.

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

91 {
92  XLogRecPtr lsn = PG_GETARG_LSN(0);
94 
95  pq_begintypsend(&buf);
96  pq_sendint64(&buf, lsn);
98 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
static void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.h:156
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
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