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.23 2001/01/24 19:43:14 momjian Exp $
14 * input routine largely stolen from boxin().
16 *-------------------------------------------------------------------------
21 #include "access/heapam.h"
22 #include "utils/builtins.h"
24 #define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
25 #define ItemPointerGetDatum(X) PointerGetDatum(X)
26 #define PG_GETARG_ITEMPOINTER(n) DatumGetItemPointer(PG_GETARG_DATUM(n))
27 #define PG_RETURN_ITEMPOINTER(x) return ItemPointerGetDatum(x)
34 /* ----------------------------------------------------------------
36 * ----------------------------------------------------------------
39 tidin(PG_FUNCTION_ARGS)
41 char *str = PG_GETARG_CSTRING(0);
46 BlockNumber blockNumber;
47 OffsetNumber offsetNumber;
49 for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
50 if (*p == DELIM || (*p == LDELIM && !i))
54 elog(ERROR, "invalid tid format: '%s'", str);
56 blockNumber = (BlockNumber) atoi(coord[0]);
57 offsetNumber = (OffsetNumber) atoi(coord[1]);
59 result = (ItemPointer) palloc(sizeof(ItemPointerData));
61 ItemPointerSet(result, blockNumber, offsetNumber);
63 PG_RETURN_ITEMPOINTER(result);
66 /* ----------------------------------------------------------------
68 * ----------------------------------------------------------------
71 tidout(PG_FUNCTION_ARGS)
73 ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
75 BlockNumber blockNumber;
76 OffsetNumber offsetNumber;
78 static char *invalidTid = "()";
80 if (!ItemPointerIsValid(itemPtr))
81 PG_RETURN_CSTRING(pstrdup(invalidTid));
83 blockId = &(itemPtr->ip_blkid);
85 blockNumber = BlockIdGetBlockNumber(blockId);
86 offsetNumber = itemPtr->ip_posid;
88 sprintf(buf, "(%d,%d)", (int) blockNumber, (int) offsetNumber);
90 PG_RETURN_CSTRING(pstrdup(buf));
93 /*****************************************************************************
95 *****************************************************************************/
98 tideq(PG_FUNCTION_ARGS)
100 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
101 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
103 PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
104 BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
105 arg1->ip_posid == arg2->ip_posid);
110 tidne(PG_FUNCTION_ARGS)
112 ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
113 ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
115 PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
116 BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
117 arg1->ip_posid != arg2->ip_posid);
122 * Functions to get latest tid of a specified tuple.
124 * Maybe these implementations should be moved to another place
127 currtid_byreloid(PG_FUNCTION_ARGS)
129 Oid reloid = PG_GETARG_OID(0);
130 ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
135 result = (ItemPointer) palloc(sizeof(ItemPointerData));
136 ItemPointerSetInvalid(result);
137 if ((rel = heap_open(reloid, AccessShareLock)) != NULL)
139 ret = heap_get_latest_tid(rel, SnapshotNow, tid);
141 ItemPointerCopy(ret, result);
142 heap_close(rel, AccessShareLock);
145 elog(ERROR, "Relation %u not found", reloid);
147 PG_RETURN_ITEMPOINTER(result);
151 currtid_byrelname(PG_FUNCTION_ARGS)
153 text *relname = PG_GETARG_TEXT_P(0);
154 ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
160 str = DatumGetCString(DirectFunctionCall1(textout,
161 PointerGetDatum(relname)));
163 result = (ItemPointer) palloc(sizeof(ItemPointerData));
164 ItemPointerSetInvalid(result);
165 if ((rel = heap_openr(str, AccessShareLock)) != NULL)
167 ret = heap_get_latest_tid(rel, SnapshotNow, tid);
169 ItemPointerCopy(ret, result);
170 heap_close(rel, AccessShareLock);
173 elog(ERROR, "Relation %s not found", str);
177 PG_RETURN_ITEMPOINTER(result);