~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/xid.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * xid.c
 
4
 *        POSTGRES transaction identifier and command identifier datatypes.
 
5
 *
 
6
 * Portions Copyright (c) 1996-2011, 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/transam.h"
 
20
#include "access/xact.h"
 
21
#include "libpq/pqformat.h"
 
22
#include "utils/builtins.h"
 
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 = GetTopTransactionId();
 
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
 * xidComparator
 
107
 *              qsort comparison function for XIDs
 
108
 *
 
109
 * We can't use wraparound comparison for XIDs because that does not respect
 
110
 * the triangle inequality!  Any old sort order will do.
 
111
 */
 
112
int
 
113
xidComparator(const void *arg1, const void *arg2)
 
114
{
 
115
        TransactionId xid1 = *(const TransactionId *) arg1;
 
116
        TransactionId xid2 = *(const TransactionId *) arg2;
 
117
 
 
118
        if (xid1 > xid2)
 
119
                return 1;
 
120
        if (xid1 < xid2)
 
121
                return -1;
 
122
        return 0;
 
123
}
 
124
 
 
125
/*****************************************************************************
 
126
 *       COMMAND IDENTIFIER ROUTINES                                                                                     *
 
127
 *****************************************************************************/
 
128
 
 
129
/*
 
130
 *              cidin   - converts CommandId to internal representation.
 
131
 */
 
132
Datum
 
133
cidin(PG_FUNCTION_ARGS)
 
134
{
 
135
        char       *s = PG_GETARG_CSTRING(0);
 
136
        CommandId       c;
 
137
 
 
138
        c = atoi(s);
 
139
 
 
140
        PG_RETURN_COMMANDID(c);
 
141
}
 
142
 
 
143
/*
 
144
 *              cidout  - converts a cid to external representation.
 
145
 */
 
146
Datum
 
147
cidout(PG_FUNCTION_ARGS)
 
148
{
 
149
        CommandId       c = PG_GETARG_COMMANDID(0);
 
150
        char       *result = (char *) palloc(16);
 
151
 
 
152
        snprintf(result, 16, "%u", (unsigned int) c);
 
153
        PG_RETURN_CSTRING(result);
 
154
}
 
155
 
 
156
/*
 
157
 *              cidrecv                 - converts external binary format to cid
 
158
 */
 
159
Datum
 
160
cidrecv(PG_FUNCTION_ARGS)
 
161
{
 
162
        StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
 
163
 
 
164
        PG_RETURN_COMMANDID((CommandId) pq_getmsgint(buf, sizeof(CommandId)));
 
165
}
 
166
 
 
167
/*
 
168
 *              cidsend                 - converts cid to binary format
 
169
 */
 
170
Datum
 
171
cidsend(PG_FUNCTION_ARGS)
 
172
{
 
173
        CommandId       arg1 = PG_GETARG_COMMANDID(0);
 
174
        StringInfoData buf;
 
175
 
 
176
        pq_begintypsend(&buf);
 
177
        pq_sendint(&buf, arg1, sizeof(arg1));
 
178
        PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 
179
}
 
180
 
 
181
Datum
 
182
cideq(PG_FUNCTION_ARGS)
 
183
{
 
184
        CommandId       arg1 = PG_GETARG_COMMANDID(0);
 
185
        CommandId       arg2 = PG_GETARG_COMMANDID(1);
 
186
 
 
187
        PG_RETURN_BOOL(arg1 == arg2);
 
188
}