PostgreSQL Source Code  git master
xid.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * xid.c
4  * POSTGRES transaction identifier and command identifier datatypes.
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/xid.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <limits.h>
18 
19 #include "access/multixact.h"
20 #include "access/transam.h"
21 #include "access/xact.h"
22 #include "libpq/pqformat.h"
23 #include "utils/builtins.h"
24 #include "utils/xid8.h"
25 
26 #define PG_GETARG_COMMANDID(n) DatumGetCommandId(PG_GETARG_DATUM(n))
27 #define PG_RETURN_COMMANDID(x) return CommandIdGetDatum(x)
28 
29 
30 Datum
32 {
33  char *str = PG_GETARG_CSTRING(0);
34 
35  PG_RETURN_TRANSACTIONID((TransactionId) strtoul(str, NULL, 0));
36 }
37 
38 Datum
40 {
41  TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
42  char *result = (char *) palloc(16);
43 
44  snprintf(result, 16, "%lu", (unsigned long) transactionId);
45  PG_RETURN_CSTRING(result);
46 }
47 
48 /*
49  * xidrecv - converts external binary format to xid
50  */
51 Datum
53 {
55 
57 }
58 
59 /*
60  * xidsend - converts xid to binary format
61  */
62 Datum
64 {
67 
68  pq_begintypsend(&buf);
69  pq_sendint32(&buf, arg1);
71 }
72 
73 /*
74  * xideq - are two xids equal?
75  */
76 Datum
78 {
81 
83 }
84 
85 /*
86  * xidneq - are two xids different?
87  */
88 Datum
90 {
93 
94  PG_RETURN_BOOL(!TransactionIdEquals(xid1, xid2));
95 }
96 
97 /*
98  * xid_age - compute age of an XID (relative to latest stable xid)
99  */
100 Datum
102 {
105 
106  /* Permanent XIDs are always infinitely old */
107  if (!TransactionIdIsNormal(xid))
108  PG_RETURN_INT32(INT_MAX);
109 
110  PG_RETURN_INT32((int32) (now - xid));
111 }
112 
113 /*
114  * mxid_age - compute age of a multi XID (relative to latest stable mxid)
115  */
116 Datum
118 {
121 
122  if (!MultiXactIdIsValid(xid))
123  PG_RETURN_INT32(INT_MAX);
124 
125  PG_RETURN_INT32((int32) (now - xid));
126 }
127 
128 /*
129  * xidComparator
130  * qsort comparison function for XIDs
131  *
132  * We can't use wraparound comparison for XIDs because that does not respect
133  * the triangle inequality! Any old sort order will do.
134  */
135 int
136 xidComparator(const void *arg1, const void *arg2)
137 {
138  TransactionId xid1 = *(const TransactionId *) arg1;
139  TransactionId xid2 = *(const TransactionId *) arg2;
140 
141  if (xid1 > xid2)
142  return 1;
143  if (xid1 < xid2)
144  return -1;
145  return 0;
146 }
147 
148 Datum
150 {
152 
154 }
155 
156 Datum
158 {
159  char *str = PG_GETARG_CSTRING(0);
160 
162 }
163 
164 Datum
166 {
168  char *result = (char *) palloc(21);
169 
170  snprintf(result, 21, UINT64_FORMAT, U64FromFullTransactionId(fxid));
171  PG_RETURN_CSTRING(result);
172 }
173 
174 Datum
176 {
178  uint64 value;
179 
180  value = (uint64) pq_getmsgint64(buf);
182 }
183 
184 Datum
186 {
189 
190  pq_begintypsend(&buf);
191  pq_sendint64(&buf, (uint64) U64FromFullTransactionId(arg1));
193 }
194 
195 Datum
197 {
200 
202 }
203 
204 Datum
206 {
209 
210  PG_RETURN_BOOL(!FullTransactionIdEquals(fxid1, fxid2));
211 }
212 
213 Datum
215 {
218 
220 }
221 
222 Datum
224 {
227 
229 }
230 
231 Datum
233 {
236 
238 }
239 
240 Datum
242 {
245 
247 }
248 
249 Datum
251 {
254 
255  if (FullTransactionIdFollows(fxid1, fxid2))
256  PG_RETURN_INT32(1);
257  else if (FullTransactionIdEquals(fxid1, fxid2))
258  PG_RETURN_INT32(0);
259  else
260  PG_RETURN_INT32(-1);
261 }
262 
263 /*****************************************************************************
264  * COMMAND IDENTIFIER ROUTINES *
265  *****************************************************************************/
266 
267 /*
268  * cidin - converts CommandId to internal representation.
269  */
270 Datum
272 {
273  char *str = PG_GETARG_CSTRING(0);
274 
275  PG_RETURN_COMMANDID((CommandId) strtoul(str, NULL, 0));
276 }
277 
278 /*
279  * cidout - converts a cid to external representation.
280  */
281 Datum
283 {
285  char *result = (char *) palloc(16);
286 
287  snprintf(result, 16, "%lu", (unsigned long) c);
288  PG_RETURN_CSTRING(result);
289 }
290 
291 /*
292  * cidrecv - converts external binary format to cid
293  */
294 Datum
296 {
298 
300 }
301 
302 /*
303  * cidsend - converts cid to binary format
304  */
305 Datum
307 {
308  CommandId arg1 = PG_GETARG_COMMANDID(0);
310 
311  pq_begintypsend(&buf);
312  pq_sendint32(&buf, arg1);
314 }
315 
316 Datum
318 {
319  CommandId arg1 = PG_GETARG_COMMANDID(0);
320  CommandId arg2 = PG_GETARG_COMMANDID(1);
321 
322  PG_RETURN_BOOL(arg1 == arg2);
323 }
Datum xid_age(PG_FUNCTION_ARGS)
Definition: xid.c:101
Datum cidin(PG_FUNCTION_ARGS)
Definition: xid.c:271
uint32 CommandId
Definition: c.h:589
#define PG_GETARG_FULLTRANSACTIONID(X)
Definition: xid8.h:19
Datum xidsend(PG_FUNCTION_ARGS)
Definition: xid.c:63
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:575
#define PG_RETURN_FULLTRANSACTIONID(X)
Definition: xid8.h:20
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
StringInfoData * StringInfo
Definition: stringinfo.h:44
Datum xidout(PG_FUNCTION_ARGS)
Definition: xid.c:39
#define PG_GETARG_COMMANDID(n)
Definition: xid.c:26
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_RETURN_TRANSACTIONID(x)
Definition: fmgr.h:364
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Datum xid8send(PG_FUNCTION_ARGS)
Definition: xid.c:185
Datum mxid_age(PG_FUNCTION_ARGS)
Definition: xid.c:117
Datum xideq(PG_FUNCTION_ARGS)
Definition: xid.c:77
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
static FullTransactionId FullTransactionIdFromU64(uint64 value)
Definition: transam.h:81
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
#define PG_RETURN_COMMANDID(x)
Definition: xid.c:27
signed int int32
Definition: c.h:417
Datum cideq(PG_FUNCTION_ARGS)
Definition: xid.c:317
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define FullTransactionIdPrecedesOrEquals(a, b)
Definition: transam.h:52
Datum cidrecv(PG_FUNCTION_ARGS)
Definition: xid.c:295
Datum xid8cmp(PG_FUNCTION_ARGS)
Definition: xid.c:250
#define FullTransactionIdFollows(a, b)
Definition: transam.h:53
Datum xidin(PG_FUNCTION_ARGS)
Definition: xid.c:31
char * c
#define PG_GETARG_TRANSACTIONID(n)
Definition: fmgr.h:279
static char * buf
Definition: pg_test_fsync.c:68
Datum cidsend(PG_FUNCTION_ARGS)
Definition: xid.c:306
Datum xid8recv(PG_FUNCTION_ARGS)
Definition: xid.c:175
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
Datum xidrecv(PG_FUNCTION_ARGS)
Definition: xid.c:52
Datum xid8in(PG_FUNCTION_ARGS)
Definition: xid.c:157
Datum xid8lt(PG_FUNCTION_ARGS)
Definition: xid.c:214
Datum xid8ge(PG_FUNCTION_ARGS)
Definition: xid.c:241
TransactionId GetStableLatestTransactionId(void)
Definition: xact.c:541
uint64 pg_strtouint64(const char *str, char **endptr, int base)
Definition: numutils.c:621
Datum xid8ne(PG_FUNCTION_ARGS)
Definition: xid.c:205
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:367
Datum cidout(PG_FUNCTION_ARGS)
Definition: xid.c:282
static struct @143 value
#define FullTransactionIdEquals(a, b)
Definition: transam.h:50
TransactionId MultiXactId
Definition: c.h:585
#define U64FromFullTransactionId(x)
Definition: transam.h:49
#define FullTransactionIdFollowsOrEquals(a, b)
Definition: transam.h:54
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
Datum xid8le(PG_FUNCTION_ARGS)
Definition: xid.c:232
Datum xidneq(PG_FUNCTION_ARGS)
Definition: xid.c:89
void * palloc(Size size)
Definition: mcxt.c:950
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
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define FullTransactionIdPrecedes(a, b)
Definition: transam.h:51
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
#define snprintf
Definition: port.h:215
#define UINT64_FORMAT
Definition: c.h:472
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1542
Datum xid8eq(PG_FUNCTION_ARGS)
Definition: xid.c:196
Datum xid8out(PG_FUNCTION_ARGS)
Definition: xid.c:165
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:136
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:723
Datum xid8toxid(PG_FUNCTION_ARGS)
Definition: xid.c:149
Datum xid8gt(PG_FUNCTION_ARGS)
Definition: xid.c:223