1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type tuple id
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.29 2002/03/30 01:02:41 tgl Exp $
14 * input routine largely stolen from boxin().
16 *-------------------------------------------------------------------------
21 #include "access/heapam.h"
22 #include "catalog/namespace.h"
23 #include "utils/builtins.h"
25 #define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
26 #define ItemPointerGetDatum(X) PointerGetDatum(X)
27 #define PG_GETARG_ITEMPOINTER(n) DatumGetItemPointer(PG_GETARG_DATUM(n))
28 #define PG_RETURN_ITEMPOINTER(x) return ItemPointerGetDatum(x)
35 /* ----------------------------------------------------------------
37 * ----------------------------------------------------------------
40 tidin(PG_FUNCTION_ARGS)
42 char *str = PG_GETARG_CSTRING(0);
47 BlockNumber blockNumber;
48 OffsetNumber offsetNumber;
50 for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
51 if (*p == DELIM || (*p == LDELIM && !i))
55 elog(ERROR, "invalid tid format: '%s'", str);
57 blockNumber = (BlockNumber) atoi(coord[0]);
58 offsetNumber = (OffsetNumber) atoi(coord[1]);
60 result = (ItemPointer) palloc(sizeof(ItemPointerData));
62 ItemPointerSet(result, blockNumber, offsetNumber);
64 PG_RETURN_ITEMPOINTER(result);
67 /* ----------------------------------------------------------------
69 * ----------------------------------------------------------------
72 tidout(PG_FUNCTION_ARGS)
74 ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
76 BlockNumber blockNumber;
77 OffsetNumber offsetNumber;
79 static char *invalidTid = "()";
81 if (!ItemPointerIsValid(itemPtr))
82 PG_RETURN_CSTRING(pstrdup(invalidTid));
84 blockId = &(itemPtr->ip_blkid);
86 blockNumber = BlockIdGetBlockNumber(blockId);
87 offsetNumber = itemPtr->ip_posid;
89 sprintf(buf, "(%d,%d)", (int) blockNumber, (int) offsetNumber);
91 PG_RETURN_CSTRING(pstrdup(buf));
94 /*****************************************************************************
96 *****************************************************************************/
99 tideq(PG_FUNCTION_ARGS)
101 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
102 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
104 PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
105 BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
106 arg1->ip_posid == arg2->ip_posid);
111 tidne(PG_FUNCTION_ARGS)
113 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
114 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
116 PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
117 BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
118 arg1->ip_posid != arg2->ip_posid);
123 * Functions to get latest tid of a specified tuple.
125 * Maybe these implementations should be moved to another place
128 static ItemPointerData Current_last_tid = {{0, 0}, 0};
131 setLastTid(const ItemPointer tid)
133 Current_last_tid = *tid;
137 currtid_byreloid(PG_FUNCTION_ARGS)
139 Oid reloid = PG_GETARG_OID(0);
140 ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
144 result = (ItemPointer) palloc(sizeof(ItemPointerData));
147 *result = Current_last_tid;
148 PG_RETURN_ITEMPOINTER(result);
151 rel = heap_open(reloid, AccessShareLock);
153 ItemPointerCopy(tid, result);
154 heap_get_latest_tid(rel, SnapshotNow, result);
156 heap_close(rel, AccessShareLock);
158 PG_RETURN_ITEMPOINTER(result);
162 currtid_byrelname(PG_FUNCTION_ARGS)
164 text *relname = PG_GETARG_TEXT_P(0);
165 ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
170 relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,
171 "currtid_byrelname"));
172 rel = heap_openrv(relrv, AccessShareLock);
174 result = (ItemPointer) palloc(sizeof(ItemPointerData));
175 ItemPointerCopy(tid, result);
177 heap_get_latest_tid(rel, SnapshotNow, result);
179 heap_close(rel, AccessShareLock);
181 PG_RETURN_ITEMPOINTER(result);