OSDN Git Service

9dabcf75e06625b1454e8aaf98cfb19e1f8caad0
[pg-rex/syncrep.git] / src / backend / utils / adt / xid.c
1 /*-------------------------------------------------------------------------
2  *
3  * xid.c
4  *        POSTGRES transaction identifier and command identifier datatypes.
5  *
6  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/xid.c,v 1.2 2003/08/04 00:43:26 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <limits.h>
18
19 #include "access/xact.h"
20 #include "libpq/pqformat.h"
21 #include "utils/builtins.h"
22
23
24 #define PG_GETARG_TRANSACTIONID(n)      DatumGetTransactionId(PG_GETARG_DATUM(n))
25 #define PG_RETURN_TRANSACTIONID(x)      return TransactionIdGetDatum(x)
26
27 #define PG_GETARG_COMMANDID(n)          DatumGetCommandId(PG_GETARG_DATUM(n))
28 #define PG_RETURN_COMMANDID(x)          return CommandIdGetDatum(x)
29
30
31 Datum
32 xidin(PG_FUNCTION_ARGS)
33 {
34         char       *str = PG_GETARG_CSTRING(0);
35
36         PG_RETURN_TRANSACTIONID((TransactionId) strtoul(str, NULL, 0));
37 }
38
39 Datum
40 xidout(PG_FUNCTION_ARGS)
41 {
42         TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
43
44         /* maximum 32 bit unsigned integer representation takes 10 chars */
45         char       *str = palloc(11);
46
47         snprintf(str, 11, "%lu", (unsigned long) transactionId);
48
49         PG_RETURN_CSTRING(str);
50 }
51
52 /*
53  *              xidrecv                 - converts external binary format to xid
54  */
55 Datum
56 xidrecv(PG_FUNCTION_ARGS)
57 {
58         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
59
60         PG_RETURN_TRANSACTIONID((TransactionId) pq_getmsgint(buf, sizeof(TransactionId)));
61 }
62
63 /*
64  *              xidsend                 - converts xid to binary format
65  */
66 Datum
67 xidsend(PG_FUNCTION_ARGS)
68 {
69         TransactionId arg1 = PG_GETARG_TRANSACTIONID(0);
70         StringInfoData buf;
71
72         pq_begintypsend(&buf);
73         pq_sendint(&buf, arg1, sizeof(arg1));
74         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
75 }
76
77 /*
78  *              xideq                   - are two xids equal?
79  */
80 Datum
81 xideq(PG_FUNCTION_ARGS)
82 {
83         TransactionId xid1 = PG_GETARG_TRANSACTIONID(0);
84         TransactionId xid2 = PG_GETARG_TRANSACTIONID(1);
85
86         PG_RETURN_BOOL(TransactionIdEquals(xid1, xid2));
87 }
88
89 /*
90  *              xid_age                 - compute age of an XID (relative to current xact)
91  */
92 Datum
93 xid_age(PG_FUNCTION_ARGS)
94 {
95         TransactionId xid = PG_GETARG_TRANSACTIONID(0);
96         TransactionId now = GetCurrentTransactionId();
97
98         /* Permanent XIDs are always infinitely old */
99         if (!TransactionIdIsNormal(xid))
100                 PG_RETURN_INT32(INT_MAX);
101
102         PG_RETURN_INT32((int32) (now - xid));
103 }
104
105
106 /*****************************************************************************
107  *       COMMAND IDENTIFIER ROUTINES                                                                                     *
108  *****************************************************************************/
109
110 /*
111  *              cidin   - converts CommandId to internal representation.
112  */
113 Datum
114 cidin(PG_FUNCTION_ARGS)
115 {
116         char       *s = PG_GETARG_CSTRING(0);
117         CommandId       c;
118
119         c = atoi(s);
120
121         PG_RETURN_COMMANDID(c);
122 }
123
124 /*
125  *              cidout  - converts a cid to external representation.
126  */
127 Datum
128 cidout(PG_FUNCTION_ARGS)
129 {
130         CommandId       c = PG_GETARG_COMMANDID(0);
131         char       *result = (char *) palloc(16);
132
133         snprintf(result, 16, "%u", (unsigned int) c);
134         PG_RETURN_CSTRING(result);
135 }
136
137 /*
138  *              cidrecv                 - converts external binary format to cid
139  */
140 Datum
141 cidrecv(PG_FUNCTION_ARGS)
142 {
143         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
144
145         PG_RETURN_COMMANDID((CommandId) pq_getmsgint(buf, sizeof(CommandId)));
146 }
147
148 /*
149  *              cidsend                 - converts cid to binary format
150  */
151 Datum
152 cidsend(PG_FUNCTION_ARGS)
153 {
154         CommandId       arg1 = PG_GETARG_COMMANDID(0);
155         StringInfoData buf;
156
157         pq_begintypsend(&buf);
158         pq_sendint(&buf, arg1, sizeof(arg1));
159         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
160 }
161
162 Datum
163 cideq(PG_FUNCTION_ARGS)
164 {
165         CommandId       arg1 = PG_GETARG_COMMANDID(0);
166         CommandId       arg2 = PG_GETARG_COMMANDID(1);
167
168         PG_RETURN_BOOL(arg1 == arg2);
169 }