]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/tid.c
Suppress compiler warning.
[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-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.27 2001/09/17 00:29:10 tgl Exp $
12  *
13  * NOTES
14  *        input routine largely stolen from boxin().
15  *
16  *-------------------------------------------------------------------------
17  */
18
19 #include "postgres.h"
20
21 #include "access/heapam.h"
22 #include "utils/builtins.h"
23
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)
28
29 #define LDELIM                  '('
30 #define RDELIM                  ')'
31 #define DELIM                   ','
32 #define NTIDARGS                2
33
34 /* ----------------------------------------------------------------
35  *              tidin
36  * ----------------------------------------------------------------
37  */
38 Datum
39 tidin(PG_FUNCTION_ARGS)
40 {
41         char       *str = PG_GETARG_CSTRING(0);
42         char       *p,
43                            *coord[NTIDARGS];
44         int                     i;
45         ItemPointer result;
46         BlockNumber blockNumber;
47         OffsetNumber offsetNumber;
48
49         for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
50                 if (*p == DELIM || (*p == LDELIM && !i))
51                         coord[i++] = p + 1;
52
53         if (i < NTIDARGS)
54                 elog(ERROR, "invalid tid format: '%s'", str);
55
56         blockNumber = (BlockNumber) atoi(coord[0]);
57         offsetNumber = (OffsetNumber) atoi(coord[1]);
58
59         result = (ItemPointer) palloc(sizeof(ItemPointerData));
60
61         ItemPointerSet(result, blockNumber, offsetNumber);
62
63         PG_RETURN_ITEMPOINTER(result);
64 }
65
66 /* ----------------------------------------------------------------
67  *              tidout
68  * ----------------------------------------------------------------
69  */
70 Datum
71 tidout(PG_FUNCTION_ARGS)
72 {
73         ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
74         BlockId         blockId;
75         BlockNumber blockNumber;
76         OffsetNumber offsetNumber;
77         char            buf[32];
78         static char *invalidTid = "()";
79
80         if (!ItemPointerIsValid(itemPtr))
81                 PG_RETURN_CSTRING(pstrdup(invalidTid));
82
83         blockId = &(itemPtr->ip_blkid);
84
85         blockNumber = BlockIdGetBlockNumber(blockId);
86         offsetNumber = itemPtr->ip_posid;
87
88         sprintf(buf, "(%d,%d)", (int) blockNumber, (int) offsetNumber);
89
90         PG_RETURN_CSTRING(pstrdup(buf));
91 }
92
93 /*****************************************************************************
94  *       PUBLIC ROUTINES                                                                                                                 *
95  *****************************************************************************/
96
97 Datum
98 tideq(PG_FUNCTION_ARGS)
99 {
100         ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
101         ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
102
103         PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
104                                    BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
105                                    arg1->ip_posid == arg2->ip_posid);
106 }
107
108 #ifdef NOT_USED
109 Datum
110 tidne(PG_FUNCTION_ARGS)
111 {
112         ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
113         ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
114
115         PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
116                                    BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
117                                    arg1->ip_posid != arg2->ip_posid);
118 }
119
120 #endif
121
122 /*
123  *      Functions to get latest tid of a specified tuple.
124  *
125  *      Maybe these implementations should be moved to another place
126  */
127
128 static  ItemPointerData Current_last_tid = { {0, 0}, 0};
129
130 void
131 setLastTid(const ItemPointer tid)
132 {
133         Current_last_tid = *tid;
134 }
135
136 Datum
137 currtid_byreloid(PG_FUNCTION_ARGS)
138 {
139         Oid                     reloid = PG_GETARG_OID(0);
140         ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
141         ItemPointer result;
142         Relation        rel;
143
144         result = (ItemPointer) palloc(sizeof(ItemPointerData));
145         if (!reloid) 
146         { 
147                 *result = Current_last_tid; 
148                 PG_RETURN_ITEMPOINTER(result); 
149         } 
150         ItemPointerCopy(tid, result);
151         if ((rel = heap_open(reloid, AccessShareLock)) != NULL)
152         {
153                 heap_get_latest_tid(rel, SnapshotNow, result);
154                 heap_close(rel, AccessShareLock);
155         }
156         else
157                 elog(ERROR, "Relation %u not found", reloid);
158
159         PG_RETURN_ITEMPOINTER(result);
160 }
161
162 Datum
163 currtid_byrelname(PG_FUNCTION_ARGS)
164 {
165         text       *relname = PG_GETARG_TEXT_P(0);
166         ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
167         ItemPointer result;
168         char       *str;
169         Relation        rel;
170
171         str = DatumGetCString(DirectFunctionCall1(textout,
172                                                                                           PointerGetDatum(relname)));
173
174         result = (ItemPointer) palloc(sizeof(ItemPointerData));
175         ItemPointerCopy(tid, result);
176         if ((rel = heap_openr(str, AccessShareLock)) != NULL)
177         {
178                 heap_get_latest_tid(rel, SnapshotNow, result);
179                 heap_close(rel, AccessShareLock);
180         }
181         else
182                 elog(ERROR, "Relation %s not found", str);
183
184         pfree(str);
185
186         PG_RETURN_ITEMPOINTER(result);
187 }