Skip to content

Commit 05d13ca

Browse files
author
Hiroshi Inoue
committed
The 1st step to implement new type of scan,TidScan.
Now WHERE restriction on ctid is allowed though it is sequentially scanned.
1 parent 65a2c8f commit 05d13ca

File tree

6 files changed

+238
-10
lines changed

6 files changed

+238
-10
lines changed

src/backend/access/heap/heapam.c

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.55 1999/09/24 00:23:54 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.56 1999/10/11 06:28:27 inoue Exp $
1111
*
1212
*
1313
* INTERFACE ROUTINES
@@ -1086,6 +1086,95 @@ heap_fetch(Relation relation,
10861086
}
10871087
}
10881088

1089+
/* ----------------
1090+
* heap_get_latest_tid - get the latest tid of a specified tuple
1091+
*
1092+
* ----------------
1093+
*/
1094+
ItemPointer
1095+
heap_get_latest_tid(Relation relation,
1096+
Snapshot snapshot,
1097+
ItemPointer tid)
1098+
{
1099+
ItemId lp;
1100+
Buffer buffer;
1101+
PageHeader dp;
1102+
OffsetNumber offnum;
1103+
HeapTupleData tp;
1104+
HeapTupleHeader t_data;
1105+
ItemPointerData ctid;
1106+
bool invalidBlock,linkend;
1107+
1108+
/* ----------------
1109+
* get the buffer from the relation descriptor
1110+
* Note that this does a buffer pin.
1111+
* ----------------
1112+
*/
1113+
1114+
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
1115+
1116+
if (!BufferIsValid(buffer))
1117+
elog(ERROR, "heap_get_latest_tid: %s relation: ReadBuffer(%lx) failed",
1118+
&relation->rd_rel->relname, (long) tid);
1119+
1120+
LockBuffer(buffer, BUFFER_LOCK_SHARE);
1121+
1122+
/* ----------------
1123+
* get the item line pointer corresponding to the requested tid
1124+
* ----------------
1125+
*/
1126+
dp = (PageHeader) BufferGetPage(buffer);
1127+
offnum = ItemPointerGetOffsetNumber(tid);
1128+
invalidBlock = true;
1129+
if (!PageIsNew(dp))
1130+
{
1131+
lp = PageGetItemId(dp, offnum);
1132+
if (ItemIdIsUsed(lp))
1133+
invalidBlock = false;
1134+
}
1135+
if (invalidBlock)
1136+
{
1137+
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
1138+
ReleaseBuffer(buffer);
1139+
return NULL;
1140+
}
1141+
1142+
/* ----------------
1143+
* more sanity checks
1144+
* ----------------
1145+
*/
1146+
1147+
t_data = tp.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
1148+
tp.t_len = ItemIdGetLength(lp);
1149+
tp.t_self = *tid;
1150+
ctid = tp.t_data->t_ctid;
1151+
1152+
/* ----------------
1153+
* check time qualification of tid
1154+
* ----------------
1155+
*/
1156+
1157+
HeapTupleSatisfies(&tp, relation, buffer, dp,
1158+
snapshot, 0, (ScanKey) NULL);
1159+
1160+
linkend = true;
1161+
if ((t_data->t_infomask & HEAP_XMAX_COMMITTED) &&
1162+
!ItemPointerEquals(tid, &ctid))
1163+
linkend = false;
1164+
1165+
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
1166+
ReleaseBuffer(buffer);
1167+
1168+
if (tp.t_data == NULL)
1169+
{
1170+
if (linkend)
1171+
return NULL;
1172+
return heap_get_latest_tid(relation, snapshot, &ctid);
1173+
}
1174+
1175+
return tid;
1176+
}
1177+
10891178
/* ----------------
10901179
* heap_insert - insert tuple
10911180
*

src/backend/utils/adt/tid.c

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.11 1999/07/17 20:18:00 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.12 1999/10/11 06:28:26 inoue Exp $
1111
*
1212
* NOTES
1313
* input routine largely stolen from boxin().
@@ -28,9 +28,9 @@
2828
* ----------------------------------------------------------------
2929
*/
3030
ItemPointer
31-
tidin(char *str)
31+
tidin(const char *str)
3232
{
33-
char *p,
33+
const char *p,
3434
*coord[NTIDARGS];
3535
int i;
3636
ItemPointer result;
@@ -45,8 +45,12 @@ tidin(char *str)
4545
if (*p == DELIM || (*p == LDELIM && !i))
4646
coord[i++] = p + 1;
4747

48-
if (i < NTIDARGS - 1)
48+
/* if (i < NTIDARGS - 1) */
49+
if (i < NTIDARGS)
50+
{
51+
elog(ERROR, "%s invalid tid format", str);
4952
return NULL;
53+
}
5054

5155
blockNumber = (BlockNumber) atoi(coord[0]);
5256
offsetNumber = (OffsetNumber) atoi(coord[1]);
@@ -70,6 +74,14 @@ tidout(ItemPointer itemPtr)
7074
BlockId blockId;
7175
char buf[32];
7276
char *str;
77+
static char *invalidTid = "()";
78+
79+
if (!itemPtr || !ItemPointerIsValid(itemPtr))
80+
{
81+
str = palloc(strlen(invalidTid));
82+
strcpy(str, invalidTid);
83+
return str;
84+
}
7385

7486
blockId = &(itemPtr->ip_blkid);
7587

@@ -83,3 +95,113 @@ tidout(ItemPointer itemPtr)
8395

8496
return str;
8597
}
98+
99+
/*****************************************************************************
100+
* PUBLIC ROUTINES *
101+
*****************************************************************************/
102+
103+
bool
104+
tideq(ItemPointer arg1, ItemPointer arg2)
105+
{
106+
if ((!arg1) || (!arg2))
107+
{
108+
return false;
109+
}
110+
111+
return ( BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
112+
BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
113+
arg1->ip_posid == arg2->ip_posid );
114+
}
115+
116+
bool
117+
tidne(ItemPointer arg1, ItemPointer arg2)
118+
{
119+
if ((!arg1) || (!arg2))
120+
{
121+
return false;
122+
}
123+
return ( BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
124+
BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
125+
arg1->ip_posid != arg2->ip_posid );
126+
}
127+
128+
text *
129+
tid_text(ItemPointer tid)
130+
{
131+
char *str;
132+
133+
if (!tid) return (text *)NULL;
134+
str = tidout(tid);
135+
136+
return textin(str);
137+
} /* tid_text() */
138+
139+
ItemPointer
140+
text_tid(const text *string)
141+
{
142+
ItemPointer result;
143+
char *str;
144+
145+
if (!string) return (ItemPointer)0;
146+
147+
str = textout(string);
148+
result = tidin(str);
149+
pfree(str);
150+
151+
return result;
152+
} /* text_tid() */
153+
154+
155+
/*
156+
* Functions to get latest tid of a specified tuple.
157+
* Maybe these implementations is moved
158+
* to another place
159+
*/
160+
#include <utils/relcache.h>
161+
ItemPointer
162+
currtid_byreloid(Oid reloid, ItemPointer tid)
163+
{
164+
ItemPointer result = NULL, ret;
165+
Relation rel;
166+
167+
result = (ItemPointer) palloc(sizeof(ItemPointerData));
168+
ItemPointerSetInvalid(result);
169+
if (rel = heap_open(reloid, AccessShareLock), rel)
170+
{
171+
ret = heap_get_latest_tid(rel, SnapshotNow, tid);
172+
if (ret)
173+
ItemPointerCopy(ret, result);
174+
heap_close(rel, AccessShareLock);
175+
}
176+
else
177+
elog(ERROR, "Relation %u not found", reloid);
178+
179+
return result;
180+
} /* currtid_byreloid() */
181+
182+
ItemPointer
183+
currtid_byrelname(const text *relname, ItemPointer tid)
184+
{
185+
ItemPointer result = NULL, ret;
186+
char *str;
187+
Relation rel;
188+
189+
if (!relname) return result;
190+
191+
str = textout(relname);
192+
193+
result = (ItemPointer) palloc(sizeof(ItemPointerData));
194+
ItemPointerSetInvalid(result);
195+
if (rel = heap_openr(str, AccessShareLock), rel)
196+
{
197+
ret = heap_get_latest_tid(rel, SnapshotNow, tid);
198+
if (ret)
199+
ItemPointerCopy(ret, result);
200+
heap_close(rel, AccessShareLock);
201+
}
202+
else
203+
elog(ERROR, "Relation %s not found", relname);
204+
pfree(str);
205+
206+
return result;
207+
} /* currtid_byrelname() */

src/include/access/heapam.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: heapam.h,v 1.46 1999/09/18 19:08:13 tgl Exp $
9+
* $Id: heapam.h,v 1.47 1999/10/11 06:28:29 inoue Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -256,6 +256,7 @@ extern void heap_rescan(HeapScanDesc scan, bool scanFromEnd, ScanKey key);
256256
extern void heap_endscan(HeapScanDesc scan);
257257
extern HeapTuple heap_getnext(HeapScanDesc scandesc, int backw);
258258
extern void heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tup, Buffer *userbuf);
259+
extern ItemPointer heap_get_latest_tid(Relation relation, Snapshot snapshot, ItemPointer tid);
259260
extern Oid heap_insert(Relation relation, HeapTuple tup);
260261
extern int heap_delete(Relation relation, ItemPointer tid, ItemPointer ctid);
261262
extern int heap_replace(Relation relation, ItemPointer otid, HeapTuple tup,

src/include/catalog/pg_operator.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_operator.h,v 1.59 1999/09/06 21:16:18 tgl Exp $
10+
* $Id: pg_operator.h,v 1.60 1999/10/11 06:28:29 inoue Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -136,6 +136,9 @@ DATA(insert OID = 399 ( "=" PGUID 0 b t f 1026 1026 16 399 0 0 0 array_e
136136
DATA(insert OID = 400 ( "=" PGUID 0 b t f 1027 1027 16 400 0 0 0 array_eq eqsel eqjoinsel ));
137137
DATA(insert OID = 401 ( "=" PGUID 0 b t f 1034 1034 16 401 0 0 0 array_eq eqsel eqjoinsel ));
138138

139+
DATA(insert OID = 387 ( "=" PGUID 0 b t t 27 27 16 387 0 0 0 tideq eqsel eqjoinsel ));
140+
#define TIDEqualOperator 387
141+
139142
DATA(insert OID = 410 ( "=" PGUID 0 b t t 20 20 16 410 411 412 412 int8eq eqsel eqjoinsel ));
140143
DATA(insert OID = 411 ( "<>" PGUID 0 b t f 20 20 16 411 410 0 0 int8ne neqsel neqjoinsel ));
141144
DATA(insert OID = 412 ( "<" PGUID 0 b t f 20 20 16 413 415 0 0 int8lt intltsel intltjoinsel ));

src/include/catalog/pg_proc.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_proc.h,v 1.104 1999/09/30 14:54:23 wieck Exp $
9+
* $Id: pg_proc.h,v 1.105 1999/10/11 06:28:28 inoue Exp $
1010
*
1111
* NOTES
1212
* The script catalog/genbki.sh reads this file and generates .bki
@@ -1560,6 +1560,13 @@ DESCR("truncate _char()");
15601560
DATA(insert OID = 1291 ( _varchar PGUID 11 f t t 2 f 1015 "1015 23" 100 0 0 100 _varchar - ));
15611561
DESCR("truncate _varchar()");
15621562

1563+
DATA(insert OID = 1292 ( tideq PGUID 11 f t f 2 f 16 "27 27" 100 0 0 100 tideq - ));
1564+
DESCR("equal");
1565+
DATA(insert OID = 1293 ( currtid PGUID 11 f t f 2 f 27 "26 27" 100 0 0 100 currtid_byreloid - ));
1566+
DESCR("latest tid of a tuple");
1567+
DATA(insert OID = 1294 ( currtid2 PGUID 11 f t f 2 f 27 "25 27" 100 0 0 100 currtid_byrelname - ));
1568+
DESCR("latest tid of a tuple");
1569+
15631570
DATA(insert OID = 1297 ( timestamp_in PGUID 11 f t f 1 f 1296 "0" 100 0 0 100 timestamp_in - ));
15641571
DESCR("(internal)");
15651572
DATA(insert OID = 1298 ( timestamp_out PGUID 11 f t f 1 f 23 "0" 100 0 0 100 timestamp_out - ));

src/include/utils/builtins.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: builtins.h,v 1.88 1999/10/03 23:55:37 tgl Exp $
9+
* $Id: builtins.h,v 1.89 1999/10/11 06:28:28 inoue Exp $
1010
*
1111
* NOTES
1212
* This should normally only be included by fmgr.h.
@@ -399,8 +399,14 @@ extern float64 gistsel(Oid operatorObjectId, Oid indrelid, AttrNumber attributeN
399399
extern float64 gistnpage(Oid operatorObjectId, Oid indrelid, AttrNumber attributeNumber, char *constValue, int32 constFlag, int32 nIndexKeys, Oid indexrelid);
400400

401401
/* tid.c */
402-
extern ItemPointer tidin(char *str);
402+
extern ItemPointer tidin(const char *str);
403403
extern char *tidout(ItemPointer itemPtr);
404+
extern bool tideq(ItemPointer,ItemPointer);
405+
extern bool tidne(ItemPointer,ItemPointer);
406+
extern text *tid_text(ItemPointer);
407+
extern ItemPointer text_tid(const text *);
408+
extern ItemPointer currtid_byreloid(Oid relOid, ItemPointer);
409+
extern ItemPointer currtid_byrelname(const text* relName, ItemPointer);
404410

405411
/* timestamp.c */
406412
extern time_t timestamp_in(const char *timestamp_str);

0 commit comments

Comments
 (0)