1 /*-------------------------------------------------------------------------
4 * POSTGRES transaction identifier and command identifier datatypes.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
19 #include "access/transam.h"
20 #include "access/xact.h"
21 #include "libpq/pqformat.h"
22 #include "utils/builtins.h"
24 #define PG_GETARG_TRANSACTIONID(n) DatumGetTransactionId(PG_GETARG_DATUM(n))
25 #define PG_RETURN_TRANSACTIONID(x) return TransactionIdGetDatum(x)
27 #define PG_GETARG_COMMANDID(n) DatumGetCommandId(PG_GETARG_DATUM(n))
28 #define PG_RETURN_COMMANDID(x) return CommandIdGetDatum(x)
32 xidin(PG_FUNCTION_ARGS
)
34 char *str
= PG_GETARG_CSTRING(0);
36 PG_RETURN_TRANSACTIONID((TransactionId
) strtoul(str
, NULL
, 0));
40 xidout(PG_FUNCTION_ARGS
)
42 TransactionId transactionId
= PG_GETARG_TRANSACTIONID(0);
44 /* maximum 32 bit unsigned integer representation takes 10 chars */
45 char *str
= palloc(11);
47 snprintf(str
, 11, "%lu", (unsigned long) transactionId
);
49 PG_RETURN_CSTRING(str
);
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_sendint(&buf
, arg1
, sizeof(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 * xid_age - compute age of an XID (relative to current xact)
93 xid_age(PG_FUNCTION_ARGS
)
95 TransactionId xid
= PG_GETARG_TRANSACTIONID(0);
96 TransactionId now
= GetTopTransactionId();
98 /* Permanent XIDs are always infinitely old */
99 if (!TransactionIdIsNormal(xid
))
100 PG_RETURN_INT32(INT_MAX
);
102 PG_RETURN_INT32((int32
) (now
- xid
));
106 /*****************************************************************************
107 * COMMAND IDENTIFIER ROUTINES *
108 *****************************************************************************/
111 * cidin - converts CommandId to internal representation.
114 cidin(PG_FUNCTION_ARGS
)
116 char *s
= PG_GETARG_CSTRING(0);
121 PG_RETURN_COMMANDID(c
);
125 * cidout - converts a cid to external representation.
128 cidout(PG_FUNCTION_ARGS
)
130 CommandId c
= PG_GETARG_COMMANDID(0);
131 char *result
= (char *) palloc(16);
133 snprintf(result
, 16, "%u", (unsigned int) c
);
134 PG_RETURN_CSTRING(result
);
138 * cidrecv - converts external binary format to cid
141 cidrecv(PG_FUNCTION_ARGS
)
143 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
145 PG_RETURN_COMMANDID((CommandId
) pq_getmsgint(buf
, sizeof(CommandId
)));
149 * cidsend - converts cid to binary format
152 cidsend(PG_FUNCTION_ARGS
)
154 CommandId arg1
= PG_GETARG_COMMANDID(0);
157 pq_begintypsend(&buf
);
158 pq_sendint(&buf
, arg1
, sizeof(arg1
));
159 PG_RETURN_BYTEA_P(pq_endtypsend(&buf
));
163 cideq(PG_FUNCTION_ARGS
)
165 CommandId arg1
= PG_GETARG_COMMANDID(0);
166 CommandId arg2
= PG_GETARG_COMMANDID(1);
168 PG_RETURN_BOOL(arg1
== arg2
);