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-2017, 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 
25 #define PG_GETARG_TRANSACTIONID(n) DatumGetTransactionId(PG_GETARG_DATUM(n))
26 #define PG_RETURN_TRANSACTIONID(x) return TransactionIdGetDatum(x)
27 
28 #define PG_GETARG_COMMANDID(n) DatumGetCommandId(PG_GETARG_DATUM(n))
29 #define PG_RETURN_COMMANDID(x) return CommandIdGetDatum(x)
30 
31 
32 Datum
34 {
35  char *str = PG_GETARG_CSTRING(0);
36 
37  PG_RETURN_TRANSACTIONID((TransactionId) strtoul(str, NULL, 0));
38 }
39 
40 Datum
42 {
43  TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
44  char *result = (char *) palloc(16);
45 
46  snprintf(result, 16, "%lu", (unsigned long) transactionId);
47  PG_RETURN_CSTRING(result);
48 }
49 
50 /*
51  * xidrecv - converts external binary format to xid
52  */
53 Datum
55 {
57 
59 }
60 
61 /*
62  * xidsend - converts xid to binary format
63  */
64 Datum
66 {
69 
70  pq_begintypsend(&buf);
71  pq_sendint32(&buf, arg1);
73 }
74 
75 /*
76  * xideq - are two xids equal?
77  */
78 Datum
80 {
83 
85 }
86 
87 /*
88  * xidneq - are two xids different?
89  */
90 Datum
92 {
95 
96  PG_RETURN_BOOL(!TransactionIdEquals(xid1, xid2));
97 }
98 
99 /*
100  * xid_age - compute age of an XID (relative to latest stable xid)
101  */
102 Datum
104 {
107 
108  /* Permanent XIDs are always infinitely old */
109  if (!TransactionIdIsNormal(xid))
110  PG_RETURN_INT32(INT_MAX);
111 
112  PG_RETURN_INT32((int32) (now - xid));
113 }
114 
115 /*
116  * mxid_age - compute age of a multi XID (relative to latest stable mxid)
117  */
118 Datum
120 {
123 
124  if (!MultiXactIdIsValid(xid))
125  PG_RETURN_INT32(INT_MAX);
126 
127  PG_RETURN_INT32((int32) (now - xid));
128 }
129 
130 /*
131  * xidComparator
132  * qsort comparison function for XIDs
133  *
134  * We can't use wraparound comparison for XIDs because that does not respect
135  * the triangle inequality! Any old sort order will do.
136  */
137 int
138 xidComparator(const void *arg1, const void *arg2)
139 {
140  TransactionId xid1 = *(const TransactionId *) arg1;
141  TransactionId xid2 = *(const TransactionId *) arg2;
142 
143  if (xid1 > xid2)
144  return 1;
145  if (xid1 < xid2)
146  return -1;
147  return 0;
148 }
149 
150 /*****************************************************************************
151  * COMMAND IDENTIFIER ROUTINES *
152  *****************************************************************************/
153 
154 /*
155  * cidin - converts CommandId to internal representation.
156  */
157 Datum
159 {
160  char *str = PG_GETARG_CSTRING(0);
161 
162  PG_RETURN_COMMANDID((CommandId) strtoul(str, NULL, 0));
163 }
164 
165 /*
166  * cidout - converts a cid to external representation.
167  */
168 Datum
170 {
172  char *result = (char *) palloc(16);
173 
174  snprintf(result, 16, "%lu", (unsigned long) c);
175  PG_RETURN_CSTRING(result);
176 }
177 
178 /*
179  * cidrecv - converts external binary format to cid
180  */
181 Datum
183 {
185 
187 }
188 
189 /*
190  * cidsend - converts cid to binary format
191  */
192 Datum
194 {
195  CommandId arg1 = PG_GETARG_COMMANDID(0);
197 
198  pq_begintypsend(&buf);
199  pq_sendint32(&buf, arg1);
201 }
202 
203 Datum
205 {
206  CommandId arg1 = PG_GETARG_COMMANDID(0);
207  CommandId arg2 = PG_GETARG_COMMANDID(1);
208 
209  PG_RETURN_BOOL(arg1 == arg2);
210 }
Datum xid_age(PG_FUNCTION_ARGS)
Definition: xid.c:103
Datum cidin(PG_FUNCTION_ARGS)
Definition: xid.c:158
uint32 CommandId
Definition: c.h:459
Datum xidsend(PG_FUNCTION_ARGS)
Definition: xid.c:65
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:445
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
StringInfoData * StringInfo
Definition: stringinfo.h:43
Datum xidout(PG_FUNCTION_ARGS)
Definition: xid.c:41
#define PG_GETARG_COMMANDID(n)
Definition: xid.c:28
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Datum mxid_age(PG_FUNCTION_ARGS)
Definition: xid.c:119
Datum xideq(PG_FUNCTION_ARGS)
Definition: xid.c:79
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
#define PG_RETURN_COMMANDID(x)
Definition: xid.c:29
signed int int32
Definition: c.h:284
Datum cideq(PG_FUNCTION_ARGS)
Definition: xid.c:204
Datum cidrecv(PG_FUNCTION_ARGS)
Definition: xid.c:182
Datum xidin(PG_FUNCTION_ARGS)
Definition: xid.c:33
char * c
static char * buf
Definition: pg_test_fsync.c:67
Datum cidsend(PG_FUNCTION_ARGS)
Definition: xid.c:193
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
Datum xidrecv(PG_FUNCTION_ARGS)
Definition: xid.c:54
TransactionId GetStableLatestTransactionId(void)
Definition: xact.c:462
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
Datum cidout(PG_FUNCTION_ARGS)
Definition: xid.c:169
TransactionId MultiXactId
Definition: c.h:455
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Datum xidneq(PG_FUNCTION_ARGS)
Definition: xid.c:91
void * palloc(Size size)
Definition: mcxt.c:848
#define PG_GETARG_TRANSACTIONID(n)
Definition: xid.c:25
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:138
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:721
#define PG_RETURN_TRANSACTIONID(x)
Definition: xid.c:26