]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/tid.c
Implement the <> operator for the tid type. Original patch from Mark
[postgresql] / src / backend / utils / adt / tid.c
1 /*-------------------------------------------------------------------------
2  *
3  * tid.c
4  *        Functions for the built-in type tuple id
5  *
6  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/utils/adt/tid.c,v 1.50 2006/02/26 18:36:21 neilc Exp $
12  *
13  * NOTES
14  *        input routine largely stolen from boxin().
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19
20 #include <errno.h>
21 #include <math.h>
22 #include <limits.h>
23
24 #include "access/heapam.h"
25 #include "catalog/namespace.h"
26 #include "catalog/pg_type.h"
27 #include "libpq/pqformat.h"
28 #include "parser/parsetree.h"
29 #include "utils/builtins.h"
30
31
32 #define DatumGetItemPointer(X)   ((ItemPointer) DatumGetPointer(X))
33 #define ItemPointerGetDatum(X)   PointerGetDatum(X)
34 #define PG_GETARG_ITEMPOINTER(n) DatumGetItemPointer(PG_GETARG_DATUM(n))
35 #define PG_RETURN_ITEMPOINTER(x) return ItemPointerGetDatum(x)
36
37 #define LDELIM                  '('
38 #define RDELIM                  ')'
39 #define DELIM                   ','
40 #define NTIDARGS                2
41
42 /* ----------------------------------------------------------------
43  *              tidin
44  * ----------------------------------------------------------------
45  */
46 Datum
47 tidin(PG_FUNCTION_ARGS)
48 {
49         char       *str = PG_GETARG_CSTRING(0);
50         char       *p,
51                            *coord[NTIDARGS];
52         int                     i;
53         ItemPointer result;
54         BlockNumber blockNumber;
55         OffsetNumber offsetNumber;
56         char       *badp;
57         int                     hold_offset;
58
59         for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
60                 if (*p == DELIM || (*p == LDELIM && !i))
61                         coord[i++] = p + 1;
62
63         if (i < NTIDARGS)
64                 ereport(ERROR,
65                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
66                                  errmsg("invalid input syntax for type tid: \"%s\"",
67                                                 str)));
68
69         errno = 0;
70         blockNumber = strtoul(coord[0], &badp, 10);
71         if (errno || *badp != DELIM)
72                 ereport(ERROR,
73                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
74                                  errmsg("invalid input syntax for type tid: \"%s\"",
75                                                 str)));
76
77         hold_offset = strtol(coord[1], &badp, 10);
78         if (errno || *badp != RDELIM ||
79                 hold_offset > USHRT_MAX || hold_offset < 0)
80                 ereport(ERROR,
81                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
82                                  errmsg("invalid input syntax for type tid: \"%s\"",
83                                                 str)));
84
85         offsetNumber = hold_offset;
86
87         result = (ItemPointer) palloc(sizeof(ItemPointerData));
88
89         ItemPointerSet(result, blockNumber, offsetNumber);
90
91         PG_RETURN_ITEMPOINTER(result);
92 }
93
94 /* ----------------------------------------------------------------
95  *              tidout
96  * ----------------------------------------------------------------
97  */
98 Datum
99 tidout(PG_FUNCTION_ARGS)
100 {
101         ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
102         BlockId         blockId;
103         BlockNumber blockNumber;
104         OffsetNumber offsetNumber;
105         char            buf[32];
106
107         if (!ItemPointerIsValid(itemPtr))
108                 PG_RETURN_CSTRING(pstrdup("()"));
109
110         blockId = &(itemPtr->ip_blkid);
111         blockNumber = BlockIdGetBlockNumber(blockId);
112         offsetNumber = itemPtr->ip_posid;
113
114         snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);
115
116         PG_RETURN_CSTRING(pstrdup(buf));
117 }
118
119 /*
120  *              tidrecv                 - converts external binary format to tid
121  */
122 Datum
123 tidrecv(PG_FUNCTION_ARGS)
124 {
125         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
126         ItemPointer result;
127         BlockNumber blockNumber;
128         OffsetNumber offsetNumber;
129
130         blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
131         offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));
132
133         result = (ItemPointer) palloc(sizeof(ItemPointerData));
134
135         ItemPointerSet(result, blockNumber, offsetNumber);
136
137         PG_RETURN_ITEMPOINTER(result);
138 }
139
140 /*
141  *              tidsend                 - converts tid to binary format
142  */
143 Datum
144 tidsend(PG_FUNCTION_ARGS)
145 {
146         ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
147         BlockId         blockId;
148         BlockNumber blockNumber;
149         OffsetNumber offsetNumber;
150         StringInfoData buf;
151
152         blockId = &(itemPtr->ip_blkid);
153         blockNumber = BlockIdGetBlockNumber(blockId);
154         offsetNumber = itemPtr->ip_posid;
155
156         pq_begintypsend(&buf);
157         pq_sendint(&buf, blockNumber, sizeof(blockNumber));
158         pq_sendint(&buf, offsetNumber, sizeof(offsetNumber));
159         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
160 }
161
162 /*****************************************************************************
163  *       PUBLIC ROUTINES                                                                                                                 *
164  *****************************************************************************/
165
166 Datum
167 tideq(PG_FUNCTION_ARGS)
168 {
169         ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
170         ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
171
172         PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
173                                    BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
174                                    arg1->ip_posid == arg2->ip_posid);
175 }
176
177 Datum
178 tidne(PG_FUNCTION_ARGS)
179 {
180         ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
181         ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
182
183         PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
184                                    BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
185                                    arg1->ip_posid != arg2->ip_posid);
186 }
187
188 /*
189  *      Functions to get latest tid of a specified tuple.
190  *
191  *      Maybe these implementations should be moved to another place
192  */
193
194 static ItemPointerData Current_last_tid = {{0, 0}, 0};
195
196 void
197 setLastTid(const ItemPointer tid)
198 {
199         Current_last_tid = *tid;
200 }
201
202 /*
203  *      Handle CTIDs of views.
204  *              CTID should be defined in the view and it must
205  *              correspond to the CTID of a base relation.
206  */
207 static Datum
208 currtid_for_view(Relation viewrel, ItemPointer tid)
209 {
210         TupleDesc       att = RelationGetDescr(viewrel);
211         RuleLock   *rulelock;
212         RewriteRule *rewrite;
213         int                     i,
214                                 natts = att->natts,
215                                 tididx = -1;
216
217         for (i = 0; i < natts; i++)
218         {
219                 if (strcmp(NameStr(att->attrs[i]->attname), "ctid") == 0)
220                 {
221                         if (att->attrs[i]->atttypid != TIDOID)
222                                 elog(ERROR, "ctid isn't of type TID");
223                         tididx = i;
224                         break;
225                 }
226         }
227         if (tididx < 0)
228                 elog(ERROR, "currtid cannot handle views with no CTID");
229         rulelock = viewrel->rd_rules;
230         if (!rulelock)
231                 elog(ERROR, "the view has no rules");
232         for (i = 0; i < rulelock->numLocks; i++)
233         {
234                 rewrite = rulelock->rules[i];
235                 if (rewrite->event == CMD_SELECT)
236                 {
237                         Query      *query;
238                         TargetEntry *tle;
239
240                         if (list_length(rewrite->actions) != 1)
241                                 elog(ERROR, "only one select rule is allowed in views");
242                         query = (Query *) linitial(rewrite->actions);
243                         tle = get_tle_by_resno(query->targetList, tididx + 1);
244                         if (tle && tle->expr && IsA(tle->expr, Var))
245                         {
246                                 Var                *var = (Var *) tle->expr;
247                                 RangeTblEntry *rte;
248
249                                 if (var->varno > 0 && var->varno < INNER &&
250                                         var->varattno == SelfItemPointerAttributeNumber)
251                                 {
252                                         rte = rt_fetch(var->varno, query->rtable);
253                                         if (rte)
254                                         {
255                                                 heap_close(viewrel, AccessShareLock);
256                                                 return DirectFunctionCall2(currtid_byreloid, ObjectIdGetDatum(rte->relid), PointerGetDatum(tid));
257                                         }
258                                 }
259                         }
260                         break;
261                 }
262         }
263         elog(ERROR, "currtid cannot handle this view");
264         return (Datum) 0;
265 }
266
267 Datum
268 currtid_byreloid(PG_FUNCTION_ARGS)
269 {
270         Oid                     reloid = PG_GETARG_OID(0);
271         ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
272         ItemPointer result;
273         Relation        rel;
274
275         result = (ItemPointer) palloc(sizeof(ItemPointerData));
276         if (!reloid)
277         {
278                 *result = Current_last_tid;
279                 PG_RETURN_ITEMPOINTER(result);
280         }
281
282         rel = heap_open(reloid, AccessShareLock);
283         if (rel->rd_rel->relkind == RELKIND_VIEW)
284                 return currtid_for_view(rel, tid);
285
286         ItemPointerCopy(tid, result);
287         heap_get_latest_tid(rel, SnapshotNow, result);
288
289         heap_close(rel, AccessShareLock);
290
291         PG_RETURN_ITEMPOINTER(result);
292 }
293
294 Datum
295 currtid_byrelname(PG_FUNCTION_ARGS)
296 {
297         text       *relname = PG_GETARG_TEXT_P(0);
298         ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
299         ItemPointer result;
300         RangeVar   *relrv;
301         Relation        rel;
302
303         relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
304         rel = heap_openrv(relrv, AccessShareLock);
305         if (rel->rd_rel->relkind == RELKIND_VIEW)
306                 return currtid_for_view(rel, tid);
307
308         result = (ItemPointer) palloc(sizeof(ItemPointerData));
309         ItemPointerCopy(tid, result);
310
311         heap_get_latest_tid(rel, SnapshotNow, result);
312
313         heap_close(rel, AccessShareLock);
314
315         PG_RETURN_ITEMPOINTER(result);
316 }