]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/tid.c
Update copyright for 2006. Update scripts.
[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-2006, 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.52 2006/03/05 15:58:44 momjian 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         /* Perhaps someday we should output this as a record. */
115         snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);
116
117         PG_RETURN_CSTRING(pstrdup(buf));
118 }
119
120 /*
121  *              tidrecv                 - converts external binary format to tid
122  */
123 Datum
124 tidrecv(PG_FUNCTION_ARGS)
125 {
126         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
127         ItemPointer result;
128         BlockNumber blockNumber;
129         OffsetNumber offsetNumber;
130
131         blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
132         offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));
133
134         result = (ItemPointer) palloc(sizeof(ItemPointerData));
135
136         ItemPointerSet(result, blockNumber, offsetNumber);
137
138         PG_RETURN_ITEMPOINTER(result);
139 }
140
141 /*
142  *              tidsend                 - converts tid to binary format
143  */
144 Datum
145 tidsend(PG_FUNCTION_ARGS)
146 {
147         ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
148         BlockId         blockId;
149         BlockNumber blockNumber;
150         OffsetNumber offsetNumber;
151         StringInfoData buf;
152
153         blockId = &(itemPtr->ip_blkid);
154         blockNumber = BlockIdGetBlockNumber(blockId);
155         offsetNumber = itemPtr->ip_posid;
156
157         pq_begintypsend(&buf);
158         pq_sendint(&buf, blockNumber, sizeof(blockNumber));
159         pq_sendint(&buf, offsetNumber, sizeof(offsetNumber));
160         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
161 }
162
163 /*****************************************************************************
164  *       PUBLIC ROUTINES                                                                                                                 *
165  *****************************************************************************/
166
167 Datum
168 tideq(PG_FUNCTION_ARGS)
169 {
170         ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
171         ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
172
173         PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
174                                    BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
175                                    arg1->ip_posid == arg2->ip_posid);
176 }
177
178 Datum
179 tidne(PG_FUNCTION_ARGS)
180 {
181         ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
182         ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
183
184         PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
185                                    BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
186                                    arg1->ip_posid != arg2->ip_posid);
187 }
188
189 /*
190  *      Functions to get latest tid of a specified tuple.
191  *
192  *      Maybe these implementations should be moved to another place
193  */
194
195 static ItemPointerData Current_last_tid = {{0, 0}, 0};
196
197 void
198 setLastTid(const ItemPointer tid)
199 {
200         Current_last_tid = *tid;
201 }
202
203 /*
204  *      Handle CTIDs of views.
205  *              CTID should be defined in the view and it must
206  *              correspond to the CTID of a base relation.
207  */
208 static Datum
209 currtid_for_view(Relation viewrel, ItemPointer tid)
210 {
211         TupleDesc       att = RelationGetDescr(viewrel);
212         RuleLock   *rulelock;
213         RewriteRule *rewrite;
214         int                     i,
215                                 natts = att->natts,
216                                 tididx = -1;
217
218         for (i = 0; i < natts; i++)
219         {
220                 if (strcmp(NameStr(att->attrs[i]->attname), "ctid") == 0)
221                 {
222                         if (att->attrs[i]->atttypid != TIDOID)
223                                 elog(ERROR, "ctid isn't of type TID");
224                         tididx = i;
225                         break;
226                 }
227         }
228         if (tididx < 0)
229                 elog(ERROR, "currtid cannot handle views with no CTID");
230         rulelock = viewrel->rd_rules;
231         if (!rulelock)
232                 elog(ERROR, "the view has no rules");
233         for (i = 0; i < rulelock->numLocks; i++)
234         {
235                 rewrite = rulelock->rules[i];
236                 if (rewrite->event == CMD_SELECT)
237                 {
238                         Query      *query;
239                         TargetEntry *tle;
240
241                         if (list_length(rewrite->actions) != 1)
242                                 elog(ERROR, "only one select rule is allowed in views");
243                         query = (Query *) linitial(rewrite->actions);
244                         tle = get_tle_by_resno(query->targetList, tididx + 1);
245                         if (tle && tle->expr && IsA(tle->expr, Var))
246                         {
247                                 Var                *var = (Var *) tle->expr;
248                                 RangeTblEntry *rte;
249
250                                 if (var->varno > 0 && var->varno < INNER &&
251                                         var->varattno == SelfItemPointerAttributeNumber)
252                                 {
253                                         rte = rt_fetch(var->varno, query->rtable);
254                                         if (rte)
255                                         {
256                                                 heap_close(viewrel, AccessShareLock);
257                                                 return DirectFunctionCall2(currtid_byreloid, ObjectIdGetDatum(rte->relid), PointerGetDatum(tid));
258                                         }
259                                 }
260                         }
261                         break;
262                 }
263         }
264         elog(ERROR, "currtid cannot handle this view");
265         return (Datum) 0;
266 }
267
268 Datum
269 currtid_byreloid(PG_FUNCTION_ARGS)
270 {
271         Oid                     reloid = PG_GETARG_OID(0);
272         ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
273         ItemPointer result;
274         Relation        rel;
275
276         result = (ItemPointer) palloc(sizeof(ItemPointerData));
277         if (!reloid)
278         {
279                 *result = Current_last_tid;
280                 PG_RETURN_ITEMPOINTER(result);
281         }
282
283         rel = heap_open(reloid, AccessShareLock);
284         if (rel->rd_rel->relkind == RELKIND_VIEW)
285                 return currtid_for_view(rel, tid);
286
287         ItemPointerCopy(tid, result);
288         heap_get_latest_tid(rel, SnapshotNow, result);
289
290         heap_close(rel, AccessShareLock);
291
292         PG_RETURN_ITEMPOINTER(result);
293 }
294
295 Datum
296 currtid_byrelname(PG_FUNCTION_ARGS)
297 {
298         text       *relname = PG_GETARG_TEXT_P(0);
299         ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
300         ItemPointer result;
301         RangeVar   *relrv;
302         Relation        rel;
303
304         relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
305         rel = heap_openrv(relrv, AccessShareLock);
306         if (rel->rd_rel->relkind == RELKIND_VIEW)
307                 return currtid_for_view(rel, tid);
308
309         result = (ItemPointer) palloc(sizeof(ItemPointerData));
310         ItemPointerCopy(tid, result);
311
312         heap_get_latest_tid(rel, SnapshotNow, result);
313
314         heap_close(rel, AccessShareLock);
315
316         PG_RETURN_ITEMPOINTER(result);
317 }