PostgreSQL Source Code  git master
pg_lsn.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_lsn.c
4  * Operations for the pg_lsn datatype.
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/backend/utils/adt/pg_lsn.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "funcapi.h"
17 #include "libpq/pqformat.h"
18 #include "utils/builtins.h"
19 #include "utils/numeric.h"
20 #include "utils/pg_lsn.h"
21 
22 #define MAXPG_LSNLEN 17
23 #define MAXPG_LSNCOMPONENT 8
24 
25 /*----------------------------------------------------------
26  * Formatting and conversion routines.
27  *---------------------------------------------------------*/
28 
30 pg_lsn_in_internal(const char *str, bool *have_error)
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 }
62 
63 Datum
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 }
79 
80 Datum
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 }
97 
98 Datum
100 {
102  XLogRecPtr result;
103 
104  result = pq_getmsgint64(buf);
105  PG_RETURN_LSN(result);
106 }
107 
108 Datum
110 {
111  XLogRecPtr lsn = PG_GETARG_LSN(0);
113 
114  pq_begintypsend(&buf);
115  pq_sendint64(&buf, lsn);
117 }
118 
119 
120 /*----------------------------------------------------------
121  * Operators for PostgreSQL LSNs
122  *---------------------------------------------------------*/
123 
124 Datum
126 {
127  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
128  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
129 
130  PG_RETURN_BOOL(lsn1 == lsn2);
131 }
132 
133 Datum
135 {
136  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
137  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
138 
139  PG_RETURN_BOOL(lsn1 != lsn2);
140 }
141 
142 Datum
144 {
145  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
146  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
147 
148  PG_RETURN_BOOL(lsn1 < lsn2);
149 }
150 
151 Datum
153 {
154  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
155  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
156 
157  PG_RETURN_BOOL(lsn1 > lsn2);
158 }
159 
160 Datum
162 {
163  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
164  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
165 
166  PG_RETURN_BOOL(lsn1 <= lsn2);
167 }
168 
169 Datum
171 {
172  XLogRecPtr lsn1 = PG_GETARG_LSN(0);
173  XLogRecPtr lsn2 = PG_GETARG_LSN(1);
174 
175  PG_RETURN_BOOL(lsn1 >= lsn2);
176 }
177 
178 Datum
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 }
186 
187 Datum
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 }
195 
196 /* btree index opclass support */
197 Datum
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 }
210 
211 /* hash index opclass support */
212 Datum
214 {
215  /* We can use hashint8 directly */
216  return hashint8(fcinfo);
217 }
218 
219 Datum
221 {
222  return hashint8extended(fcinfo);
223 }
224 
225 
226 /*----------------------------------------------------------
227  * Arithmetic operators on PostgreSQL LSNs.
228  *---------------------------------------------------------*/
229 
230 Datum
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 }
252 
253 /*
254  * Add the number of bytes to pg_lsn, giving a new pg_lsn.
255  * Must handle both positive and negative numbers of bytes.
256  */
257 Datum
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 }
286 
287 /*
288  * Subtract the number of bytes from pg_lsn, giving a new pg_lsn.
289  * Must handle both positive and negative numbers of bytes.
290  */
291 Datum
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 MAXPG_LSNLEN
Definition: pg_lsn.c:22
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Datum pg_lsn_le(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:161
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:231
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum pg_lsn_hash_extended(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:220
StringInfoData * StringInfo
Definition: stringinfo.h:44
Datum pg_lsn_send(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:109
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
Datum pg_lsn_pli(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:258
int errcode(int sqlerrcode)
Definition: elog.c:691
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Datum pg_lsn_in(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:64
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
Datum pg_lsn_smaller(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:188
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
bool numeric_is_nan(Numeric num)
Definition: numeric.c:774
Datum pg_lsn_mii(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:292
Datum pg_lsn_recv(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:99
Datum numeric_pg_lsn(PG_FUNCTION_ARGS)
Definition: numeric.c:4427
static char * buf
Definition: pg_test_fsync.c:68
#define CStringGetDatum(X)
Definition: postgres.h:578
unsigned int uint32
Definition: c.h:429
Datum pg_lsn_ge(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:170
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:621
Datum pg_lsn_eq(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:125
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
Datum pg_lsn_gt(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:152
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:367
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2788
Datum pg_lsn_out(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:81
#define ereport(elevel,...)
Definition: elog.h:155
XLogRecPtr pg_lsn_in_internal(const char *str, bool *have_error)
Definition: pg_lsn.c:30
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:800
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
Datum pg_lsn_lt(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:143
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:902
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Datum pg_lsn_cmp(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:198
#define MAXPG_LSNCOMPONENT
Definition: pg_lsn.c:23
Datum pg_lsn_hash(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:213
#define snprintf
Definition: port.h:215
Datum numeric_add(PG_FUNCTION_ARGS)
Definition: numeric.c:2711
#define UINT64_FORMAT
Definition: c.h:472
Datum pg_lsn_larger(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:179
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
Datum pg_lsn_ne(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:134