1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type tuple id
6 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/tid.c
14 * input routine largely stolen from boxin().
16 *-------------------------------------------------------------------------
23 #include "access/heapam.h"
24 #include "access/sysattr.h"
25 #include "catalog/namespace.h"
26 #include "catalog/pg_type.h"
27 #include "libpq/pqformat.h"
28 #include "miscadmin.h"
29 #include "parser/parsetree.h"
30 #include "utils/acl.h"
31 #include "utils/builtins.h"
32 #include "utils/rel.h"
33 #include "utils/snapmgr.h"
34 #include "utils/tqual.h"
37 #define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
38 #define ItemPointerGetDatum(X) PointerGetDatum(X)
39 #define PG_GETARG_ITEMPOINTER(n) DatumGetItemPointer(PG_GETARG_DATUM(n))
40 #define PG_RETURN_ITEMPOINTER(x) return ItemPointerGetDatum(x)
47 /* ----------------------------------------------------------------
49 * ----------------------------------------------------------------
52 tidin(PG_FUNCTION_ARGS)
54 char *str = PG_GETARG_CSTRING(0);
59 BlockNumber blockNumber;
60 OffsetNumber offsetNumber;
64 for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
65 if (*p == DELIM || (*p == LDELIM && !i))
70 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
71 errmsg("invalid input syntax for type tid: \"%s\"",
75 blockNumber = strtoul(coord[0], &badp, 10);
76 if (errno || *badp != DELIM)
78 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
79 errmsg("invalid input syntax for type tid: \"%s\"",
82 hold_offset = strtol(coord[1], &badp, 10);
83 if (errno || *badp != RDELIM ||
84 hold_offset > USHRT_MAX || hold_offset < 0)
86 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
87 errmsg("invalid input syntax for type tid: \"%s\"",
90 offsetNumber = hold_offset;
92 result = (ItemPointer) palloc(sizeof(ItemPointerData));
94 ItemPointerSet(result, blockNumber, offsetNumber);
96 PG_RETURN_ITEMPOINTER(result);
99 /* ----------------------------------------------------------------
101 * ----------------------------------------------------------------
104 tidout(PG_FUNCTION_ARGS)
106 ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
107 BlockNumber blockNumber;
108 OffsetNumber offsetNumber;
111 blockNumber = BlockIdGetBlockNumber(&(itemPtr->ip_blkid));
112 offsetNumber = itemPtr->ip_posid;
114 /* Perhaps someday we should output this as a record. */
115 snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);
117 PG_RETURN_CSTRING(pstrdup(buf));
121 * tidrecv - converts external binary format to tid
124 tidrecv(PG_FUNCTION_ARGS)
126 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
128 BlockNumber blockNumber;
129 OffsetNumber offsetNumber;
131 blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
132 offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));
134 result = (ItemPointer) palloc(sizeof(ItemPointerData));
136 ItemPointerSet(result, blockNumber, offsetNumber);
138 PG_RETURN_ITEMPOINTER(result);
142 * tidsend - converts tid to binary format
145 tidsend(PG_FUNCTION_ARGS)
147 ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
149 BlockNumber blockNumber;
150 OffsetNumber offsetNumber;
153 blockId = &(itemPtr->ip_blkid);
154 blockNumber = BlockIdGetBlockNumber(blockId);
155 offsetNumber = itemPtr->ip_posid;
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));
163 /*****************************************************************************
165 *****************************************************************************/
168 tideq(PG_FUNCTION_ARGS)
170 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
171 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
173 PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) == 0);
177 tidne(PG_FUNCTION_ARGS)
179 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
180 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
182 PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) != 0);
186 tidlt(PG_FUNCTION_ARGS)
188 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
189 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
191 PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) < 0);
195 tidle(PG_FUNCTION_ARGS)
197 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
198 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
200 PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) <= 0);
204 tidgt(PG_FUNCTION_ARGS)
206 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
207 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
209 PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) > 0);
213 tidge(PG_FUNCTION_ARGS)
215 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
216 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
218 PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) >= 0);
222 bttidcmp(PG_FUNCTION_ARGS)
224 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
225 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
227 PG_RETURN_INT32(ItemPointerCompare(arg1, arg2));
231 tidlarger(PG_FUNCTION_ARGS)
233 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
234 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
236 PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) >= 0 ? arg1 : arg2);
240 tidsmaller(PG_FUNCTION_ARGS)
242 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
243 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
245 PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) <= 0 ? arg1 : arg2);
250 * Functions to get latest tid of a specified tuple.
252 * Maybe these implementations should be moved to another place
255 static ItemPointerData Current_last_tid = {{0, 0}, 0};
258 setLastTid(const ItemPointer tid)
260 Current_last_tid = *tid;
264 * Handle CTIDs of views.
265 * CTID should be defined in the view and it must
266 * correspond to the CTID of a base relation.
269 currtid_for_view(Relation viewrel, ItemPointer tid)
271 TupleDesc att = RelationGetDescr(viewrel);
273 RewriteRule *rewrite;
278 for (i = 0; i < natts; i++)
280 if (strcmp(NameStr(att->attrs[i]->attname), "ctid") == 0)
282 if (att->attrs[i]->atttypid != TIDOID)
283 elog(ERROR, "ctid isn't of type TID");
289 elog(ERROR, "currtid cannot handle views with no CTID");
290 rulelock = viewrel->rd_rules;
292 elog(ERROR, "the view has no rules");
293 for (i = 0; i < rulelock->numLocks; i++)
295 rewrite = rulelock->rules[i];
296 if (rewrite->event == CMD_SELECT)
301 if (list_length(rewrite->actions) != 1)
302 elog(ERROR, "only one select rule is allowed in views");
303 query = (Query *) linitial(rewrite->actions);
304 tle = get_tle_by_resno(query->targetList, tididx + 1);
305 if (tle && tle->expr && IsA(tle->expr, Var))
307 Var *var = (Var *) tle->expr;
310 if (!IS_SPECIAL_VARNO(var->varno) &&
311 var->varattno == SelfItemPointerAttributeNumber)
313 rte = rt_fetch(var->varno, query->rtable);
316 heap_close(viewrel, AccessShareLock);
317 return DirectFunctionCall2(currtid_byreloid, ObjectIdGetDatum(rte->relid), PointerGetDatum(tid));
324 elog(ERROR, "currtid cannot handle this view");
329 currtid_byreloid(PG_FUNCTION_ARGS)
331 Oid reloid = PG_GETARG_OID(0);
332 ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
338 result = (ItemPointer) palloc(sizeof(ItemPointerData));
341 *result = Current_last_tid;
342 PG_RETURN_ITEMPOINTER(result);
345 rel = heap_open(reloid, AccessShareLock);
347 aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
349 if (aclresult != ACLCHECK_OK)
350 aclcheck_error(aclresult, ACL_KIND_CLASS,
351 RelationGetRelationName(rel));
353 if (rel->rd_rel->relkind == RELKIND_VIEW)
354 return currtid_for_view(rel, tid);
356 ItemPointerCopy(tid, result);
358 snapshot = RegisterSnapshot(GetLatestSnapshot());
359 heap_get_latest_tid(rel, snapshot, result);
360 UnregisterSnapshot(snapshot);
362 heap_close(rel, AccessShareLock);
364 PG_RETURN_ITEMPOINTER(result);
368 currtid_byrelname(PG_FUNCTION_ARGS)
370 text *relname = PG_GETARG_TEXT_P(0);
371 ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
378 relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
379 rel = heap_openrv(relrv, AccessShareLock);
381 aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
383 if (aclresult != ACLCHECK_OK)
384 aclcheck_error(aclresult, ACL_KIND_CLASS,
385 RelationGetRelationName(rel));
387 if (rel->rd_rel->relkind == RELKIND_VIEW)
388 return currtid_for_view(rel, tid);
390 result = (ItemPointer) palloc(sizeof(ItemPointerData));
391 ItemPointerCopy(tid, result);
393 snapshot = RegisterSnapshot(GetLatestSnapshot());
394 heap_get_latest_tid(rel, snapshot, result);
395 UnregisterSnapshot(snapshot);
397 heap_close(rel, AccessShareLock);
399 PG_RETURN_ITEMPOINTER(result);