1 /*-------------------------------------------------------------------------
4 * POSTGRES transaction identifier and command identifier datatypes.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/xid.c
13 *-------------------------------------------------------------------------
19 #include "access/multixact.h"
20 #include "access/transam.h"
21 #include "access/xact.h"
22 #include "common/hashfn.h"
23 #include "common/int.h"
24 #include "libpq/pqformat.h"
25 #include "utils/builtins.h"
26 #include "utils/xid8.h"
28 #define PG_GETARG_COMMANDID(n) DatumGetCommandId(PG_GETARG_DATUM(n))
29 #define PG_RETURN_COMMANDID(x) return CommandIdGetDatum(x)
33 xidin(PG_FUNCTION_ARGS
)
35 char *str
= PG_GETARG_CSTRING(0);
38 result
= uint32in_subr(str
, NULL
, "xid", fcinfo
->context
);
39 PG_RETURN_TRANSACTIONID(result
);
43 xidout(PG_FUNCTION_ARGS
)
45 TransactionId transactionId
= PG_GETARG_TRANSACTIONID(0);
46 char *result
= (char *) palloc(16);
48 snprintf(result
, 16, "%lu", (unsigned long) transactionId
);
49 PG_RETURN_CSTRING(result
);
53 * xidrecv - converts external binary format to xid
56 xidrecv(PG_FUNCTION_ARGS
)
58 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
60 PG_RETURN_TRANSACTIONID((TransactionId
) pq_getmsgint(buf
, sizeof(TransactionId
)));
64 * xidsend - converts xid to binary format
67 xidsend(PG_FUNCTION_ARGS
)
69 TransactionId arg1
= PG_GETARG_TRANSACTIONID(0);
72 pq_begintypsend(&buf
);
73 pq_sendint32(&buf
, arg1
);
74 PG_RETURN_BYTEA_P(pq_endtypsend(&buf
));
78 * xideq - are two xids equal?
81 xideq(PG_FUNCTION_ARGS
)
83 TransactionId xid1
= PG_GETARG_TRANSACTIONID(0);
84 TransactionId xid2
= PG_GETARG_TRANSACTIONID(1);
86 PG_RETURN_BOOL(TransactionIdEquals(xid1
, xid2
));
90 * xidneq - are two xids different?
93 xidneq(PG_FUNCTION_ARGS
)
95 TransactionId xid1
= PG_GETARG_TRANSACTIONID(0);
96 TransactionId xid2
= PG_GETARG_TRANSACTIONID(1);
98 PG_RETURN_BOOL(!TransactionIdEquals(xid1
, xid2
));
102 hashxid(PG_FUNCTION_ARGS
)
104 return hash_uint32(PG_GETARG_TRANSACTIONID(0));
108 hashxidextended(PG_FUNCTION_ARGS
)
110 return hash_uint32_extended(PG_GETARG_TRANSACTIONID(0), PG_GETARG_INT64(1));
114 * xid_age - compute age of an XID (relative to latest stable xid)
117 xid_age(PG_FUNCTION_ARGS
)
119 TransactionId xid
= PG_GETARG_TRANSACTIONID(0);
120 TransactionId now
= GetStableLatestTransactionId();
122 /* Permanent XIDs are always infinitely old */
123 if (!TransactionIdIsNormal(xid
))
124 PG_RETURN_INT32(INT_MAX
);
126 PG_RETURN_INT32((int32
) (now
- xid
));
130 * mxid_age - compute age of a multi XID (relative to latest stable mxid)
133 mxid_age(PG_FUNCTION_ARGS
)
135 TransactionId xid
= PG_GETARG_TRANSACTIONID(0);
136 MultiXactId now
= ReadNextMultiXactId();
138 if (!MultiXactIdIsValid(xid
))
139 PG_RETURN_INT32(INT_MAX
);
141 PG_RETURN_INT32((int32
) (now
- xid
));
146 * qsort comparison function for XIDs
148 * We can't use wraparound comparison for XIDs because that does not respect
149 * the triangle inequality! Any old sort order will do.
152 xidComparator(const void *arg1
, const void *arg2
)
154 TransactionId xid1
= *(const TransactionId
*) arg1
;
155 TransactionId xid2
= *(const TransactionId
*) arg2
;
157 return pg_cmp_u32(xid1
, xid2
);
161 * xidLogicalComparator
162 * qsort comparison function for XIDs
164 * This is used to compare only XIDs from the same epoch (e.g. for backends
165 * running at the same time). So there must be only normal XIDs, so there's
166 * no issue with triangle inequality.
169 xidLogicalComparator(const void *arg1
, const void *arg2
)
171 TransactionId xid1
= *(const TransactionId
*) arg1
;
172 TransactionId xid2
= *(const TransactionId
*) arg2
;
174 Assert(TransactionIdIsNormal(xid1
));
175 Assert(TransactionIdIsNormal(xid2
));
177 if (TransactionIdPrecedes(xid1
, xid2
))
180 if (TransactionIdPrecedes(xid2
, xid1
))
187 xid8toxid(PG_FUNCTION_ARGS
)
189 FullTransactionId fxid
= PG_GETARG_FULLTRANSACTIONID(0);
191 PG_RETURN_TRANSACTIONID(XidFromFullTransactionId(fxid
));
195 xid8in(PG_FUNCTION_ARGS
)
197 char *str
= PG_GETARG_CSTRING(0);
200 result
= uint64in_subr(str
, NULL
, "xid8", fcinfo
->context
);
201 PG_RETURN_FULLTRANSACTIONID(FullTransactionIdFromU64(result
));
205 xid8out(PG_FUNCTION_ARGS
)
207 FullTransactionId fxid
= PG_GETARG_FULLTRANSACTIONID(0);
208 char *result
= (char *) palloc(21);
210 snprintf(result
, 21, UINT64_FORMAT
, U64FromFullTransactionId(fxid
));
211 PG_RETURN_CSTRING(result
);
215 xid8recv(PG_FUNCTION_ARGS
)
217 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
220 value
= (uint64
) pq_getmsgint64(buf
);
221 PG_RETURN_FULLTRANSACTIONID(FullTransactionIdFromU64(value
));
225 xid8send(PG_FUNCTION_ARGS
)
227 FullTransactionId arg1
= PG_GETARG_FULLTRANSACTIONID(0);
230 pq_begintypsend(&buf
);
231 pq_sendint64(&buf
, (uint64
) U64FromFullTransactionId(arg1
));
232 PG_RETURN_BYTEA_P(pq_endtypsend(&buf
));
236 xid8eq(PG_FUNCTION_ARGS
)
238 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
239 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
241 PG_RETURN_BOOL(FullTransactionIdEquals(fxid1
, fxid2
));
245 xid8ne(PG_FUNCTION_ARGS
)
247 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
248 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
250 PG_RETURN_BOOL(!FullTransactionIdEquals(fxid1
, fxid2
));
254 xid8lt(PG_FUNCTION_ARGS
)
256 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
257 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
259 PG_RETURN_BOOL(FullTransactionIdPrecedes(fxid1
, fxid2
));
263 xid8gt(PG_FUNCTION_ARGS
)
265 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
266 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
268 PG_RETURN_BOOL(FullTransactionIdFollows(fxid1
, fxid2
));
272 xid8le(PG_FUNCTION_ARGS
)
274 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
275 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
277 PG_RETURN_BOOL(FullTransactionIdPrecedesOrEquals(fxid1
, fxid2
));
281 xid8ge(PG_FUNCTION_ARGS
)
283 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
284 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
286 PG_RETURN_BOOL(FullTransactionIdFollowsOrEquals(fxid1
, fxid2
));
290 xid8cmp(PG_FUNCTION_ARGS
)
292 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
293 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
295 if (FullTransactionIdFollows(fxid1
, fxid2
))
297 else if (FullTransactionIdEquals(fxid1
, fxid2
))
304 hashxid8(PG_FUNCTION_ARGS
)
306 return hashint8(fcinfo
);
310 hashxid8extended(PG_FUNCTION_ARGS
)
312 return hashint8extended(fcinfo
);
316 xid8_larger(PG_FUNCTION_ARGS
)
318 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
319 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
321 if (FullTransactionIdFollows(fxid1
, fxid2
))
322 PG_RETURN_FULLTRANSACTIONID(fxid1
);
324 PG_RETURN_FULLTRANSACTIONID(fxid2
);
328 xid8_smaller(PG_FUNCTION_ARGS
)
330 FullTransactionId fxid1
= PG_GETARG_FULLTRANSACTIONID(0);
331 FullTransactionId fxid2
= PG_GETARG_FULLTRANSACTIONID(1);
333 if (FullTransactionIdPrecedes(fxid1
, fxid2
))
334 PG_RETURN_FULLTRANSACTIONID(fxid1
);
336 PG_RETURN_FULLTRANSACTIONID(fxid2
);
339 /*****************************************************************************
340 * COMMAND IDENTIFIER ROUTINES *
341 *****************************************************************************/
344 * cidin - converts CommandId to internal representation.
347 cidin(PG_FUNCTION_ARGS
)
349 char *str
= PG_GETARG_CSTRING(0);
352 result
= uint32in_subr(str
, NULL
, "cid", fcinfo
->context
);
353 PG_RETURN_COMMANDID(result
);
357 * cidout - converts a cid to external representation.
360 cidout(PG_FUNCTION_ARGS
)
362 CommandId c
= PG_GETARG_COMMANDID(0);
363 char *result
= (char *) palloc(16);
365 snprintf(result
, 16, "%lu", (unsigned long) c
);
366 PG_RETURN_CSTRING(result
);
370 * cidrecv - converts external binary format to cid
373 cidrecv(PG_FUNCTION_ARGS
)
375 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
377 PG_RETURN_COMMANDID((CommandId
) pq_getmsgint(buf
, sizeof(CommandId
)));
381 * cidsend - converts cid to binary format
384 cidsend(PG_FUNCTION_ARGS
)
386 CommandId arg1
= PG_GETARG_COMMANDID(0);
389 pq_begintypsend(&buf
);
390 pq_sendint32(&buf
, arg1
);
391 PG_RETURN_BYTEA_P(pq_endtypsend(&buf
));
395 cideq(PG_FUNCTION_ARGS
)
397 CommandId arg1
= PG_GETARG_COMMANDID(0);
398 CommandId arg2
= PG_GETARG_COMMANDID(1);
400 PG_RETURN_BOOL(arg1
== arg2
);
404 hashcid(PG_FUNCTION_ARGS
)
406 return hash_uint32(PG_GETARG_COMMANDID(0));
410 hashcidextended(PG_FUNCTION_ARGS
)
412 return hash_uint32_extended(PG_GETARG_COMMANDID(0), PG_GETARG_INT64(1));