PostgreSQL Source Code git master
pg_lsn.c File Reference
#include "postgres.h"
#include "libpq/pqformat.h"
#include "utils/fmgrprotos.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 22 of file pg_lsn.c.

◆ MAXPG_LSNLEN

#define MAXPG_LSNLEN   17

Definition at line 21 of file pg_lsn.c.

Function Documentation

◆ pg_lsn_cmp()

Datum pg_lsn_cmp ( PG_FUNCTION_ARGS  )

Definition at line 191 of file pg_lsn.c.

192{
195
196 if (a > b)
198 else if (a == b)
200 else
201 PG_RETURN_INT32(-1);
202}
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int b
Definition: isn.c:69
int a
Definition: isn.c:68
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References a, b, PG_GETARG_LSN, and PG_RETURN_INT32.

◆ pg_lsn_eq()

Datum pg_lsn_eq ( PG_FUNCTION_ARGS  )

Definition at line 118 of file pg_lsn.c.

119{
120 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
121 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
122
123 PG_RETURN_BOOL(lsn1 == lsn2);
124}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_LSN, and PG_RETURN_BOOL.

◆ pg_lsn_ge()

Datum pg_lsn_ge ( PG_FUNCTION_ARGS  )

Definition at line 163 of file pg_lsn.c.

164{
165 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
166 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
167
168 PG_RETURN_BOOL(lsn1 >= lsn2);
169}

References PG_GETARG_LSN, and PG_RETURN_BOOL.

◆ pg_lsn_gt()

Datum pg_lsn_gt ( PG_FUNCTION_ARGS  )

Definition at line 145 of file pg_lsn.c.

146{
147 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
148 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
149
150 PG_RETURN_BOOL(lsn1 > lsn2);
151}

References PG_GETARG_LSN, and PG_RETURN_BOOL.

◆ pg_lsn_hash()

Datum pg_lsn_hash ( PG_FUNCTION_ARGS  )

Definition at line 206 of file pg_lsn.c.

207{
208 /* We can use hashint8 directly */
209 return hashint8(fcinfo);
210}
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83

References hashint8().

◆ pg_lsn_hash_extended()

Datum pg_lsn_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 213 of file pg_lsn.c.

214{
215 return hashint8extended(fcinfo);
216}
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103

References hashint8extended().

◆ pg_lsn_in()

Datum pg_lsn_in ( PG_FUNCTION_ARGS  )

Definition at line 63 of file pg_lsn.c.

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 ereturn(fcinfo->context, (Datum) 0,
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:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereturn(context, dummy_value,...)
Definition: elog.h:277
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
const char * str
XLogRecPtr pg_lsn_in_internal(const char *str, bool *have_error)
Definition: pg_lsn.c:29
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:34
uintptr_t Datum
Definition: postgres.h:69

References ereturn, errcode(), errmsg(), PG_GETARG_CSTRING, pg_lsn_in_internal(), PG_RETURN_LSN, and str.

Referenced by libpqrcv_create_slot(), and parse_subscription_options().

◆ pg_lsn_in_internal()

XLogRecPtr pg_lsn_in_internal ( const char *  str,
bool *  have_error 
)

Definition at line 29 of file pg_lsn.c.

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 Assert(condition)
Definition: c.h:815
uint64_t uint64
Definition: c.h:489
uint32_t uint32
Definition: c.h:488
#define MAXPG_LSNCOMPONENT
Definition: pg_lsn.c:22
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert, InvalidXLogRecPtr, MAXPG_LSNCOMPONENT, and str.

Referenced by check_recovery_target_lsn(), and pg_lsn_in().

◆ pg_lsn_larger()

Datum pg_lsn_larger ( PG_FUNCTION_ARGS  )

Definition at line 172 of file pg_lsn.c.

173{
174 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
175 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
176
177 PG_RETURN_LSN((lsn1 > lsn2) ? lsn1 : lsn2);
178}

References PG_GETARG_LSN, and PG_RETURN_LSN.

◆ pg_lsn_le()

Datum pg_lsn_le ( PG_FUNCTION_ARGS  )

Definition at line 154 of file pg_lsn.c.

155{
156 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
157 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
158
159 PG_RETURN_BOOL(lsn1 <= lsn2);
160}

References PG_GETARG_LSN, and PG_RETURN_BOOL.

◆ pg_lsn_lt()

Datum pg_lsn_lt ( PG_FUNCTION_ARGS  )

Definition at line 136 of file pg_lsn.c.

137{
138 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
139 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
140
141 PG_RETURN_BOOL(lsn1 < lsn2);
142}

References PG_GETARG_LSN, and PG_RETURN_BOOL.

◆ pg_lsn_mi()

Datum pg_lsn_mi ( PG_FUNCTION_ARGS  )

Definition at line 224 of file pg_lsn.c.

225{
226 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
227 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
228 char buf[256];
229 Datum result;
230
231 /* Output could be as large as plus or minus 2^63 - 1. */
232 if (lsn1 < lsn2)
233 snprintf(buf, sizeof buf, "-" UINT64_FORMAT, lsn2 - lsn1);
234 else
235 snprintf(buf, sizeof buf, UINT64_FORMAT, lsn1 - lsn2);
236
237 /* Convert to numeric. */
241 Int32GetDatum(-1));
242
243 return result;
244}
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:637
#define UINT64_FORMAT
Definition: c.h:507
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:645
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:238
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217

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

Referenced by pg_wal_lsn_diff().

◆ pg_lsn_mii()

Datum pg_lsn_mii ( PG_FUNCTION_ARGS  )

Definition at line 285 of file pg_lsn.c.

286{
287 XLogRecPtr lsn = PG_GETARG_LSN(0);
288 Numeric nbytes = PG_GETARG_NUMERIC(1);
289 Datum num;
290 Datum res;
291 char buf[32];
292
293 if (numeric_is_nan(nbytes))
295 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
296 errmsg("cannot subtract NaN from pg_lsn")));
297
298 /* Convert to numeric */
299 snprintf(buf, sizeof(buf), UINT64_FORMAT, lsn);
303 Int32GetDatum(-1));
304
305 /* Subtract two numerics */
307 num,
308 NumericGetDatum(nbytes));
309
310 /* Convert to pg_lsn */
312}
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:3043
bool numeric_is_nan(Numeric num)
Definition: numeric.c:851
Datum numeric_pg_lsn(PG_FUNCTION_ARGS)
Definition: numeric.c:4868
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:78
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73

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, res, snprintf, and UINT64_FORMAT.

◆ pg_lsn_ne()

Datum pg_lsn_ne ( PG_FUNCTION_ARGS  )

Definition at line 127 of file pg_lsn.c.

128{
129 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
130 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
131
132 PG_RETURN_BOOL(lsn1 != lsn2);
133}

References PG_GETARG_LSN, and PG_RETURN_BOOL.

◆ pg_lsn_out()

Datum pg_lsn_out ( PG_FUNCTION_ARGS  )

Definition at line 80 of file pg_lsn.c.

81{
83 char buf[MAXPG_LSNLEN + 1];
84 char *result;
85
86 snprintf(buf, sizeof buf, "%X/%X", LSN_FORMAT_ARGS(lsn));
87 result = pstrdup(buf);
88 PG_RETURN_CSTRING(result);
89}
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
char * pstrdup(const char *in)
Definition: mcxt.c:1696
#define MAXPG_LSNLEN
Definition: pg_lsn.c:21
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

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

◆ pg_lsn_pli()

Datum pg_lsn_pli ( PG_FUNCTION_ARGS  )

Definition at line 251 of file pg_lsn.c.

252{
253 XLogRecPtr lsn = PG_GETARG_LSN(0);
254 Numeric nbytes = PG_GETARG_NUMERIC(1);
255 Datum num;
256 Datum res;
257 char buf[32];
258
259 if (numeric_is_nan(nbytes))
261 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
262 errmsg("cannot add NaN to pg_lsn")));
263
264 /* Convert to numeric */
265 snprintf(buf, sizeof(buf), UINT64_FORMAT, lsn);
269 Int32GetDatum(-1));
270
271 /* Add two numerics */
273 num,
274 NumericGetDatum(nbytes));
275
276 /* Convert to pg_lsn */
278}
Datum numeric_add(PG_FUNCTION_ARGS)
Definition: numeric.c:2966

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, res, snprintf, and UINT64_FORMAT.

◆ pg_lsn_recv()

Datum pg_lsn_recv ( PG_FUNCTION_ARGS  )

Definition at line 92 of file pg_lsn.c.

93{
95 XLogRecPtr result;
96
97 result = pq_getmsgint64(buf);
98 PG_RETURN_LSN(result);
99}
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453
StringInfoData * StringInfo
Definition: stringinfo.h:54

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

◆ pg_lsn_send()

Datum pg_lsn_send ( PG_FUNCTION_ARGS  )

Definition at line 102 of file pg_lsn.c.

103{
104 XLogRecPtr lsn = PG_GETARG_LSN(0);
106
108 pq_sendint64(&buf, lsn);
110}
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:152

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

◆ pg_lsn_smaller()

Datum pg_lsn_smaller ( PG_FUNCTION_ARGS  )

Definition at line 181 of file pg_lsn.c.

182{
183 XLogRecPtr lsn1 = PG_GETARG_LSN(0);
184 XLogRecPtr lsn2 = PG_GETARG_LSN(1);
185
186 PG_RETURN_LSN((lsn1 < lsn2) ? lsn1 : lsn2);
187}

References PG_GETARG_LSN, and PG_RETURN_LSN.