diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/access/heap/heapam.c | 91 | ||||
-rw-r--r-- | src/backend/utils/adt/tid.c | 130 |
2 files changed, 216 insertions, 5 deletions
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 4c054921949..89956260cee 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.55 1999/09/24 00:23:54 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.56 1999/10/11 06:28:27 inoue Exp $ * * * INTERFACE ROUTINES @@ -1087,6 +1087,95 @@ heap_fetch(Relation relation, } /* ---------------- + * heap_get_latest_tid - get the latest tid of a specified tuple + * + * ---------------- + */ +ItemPointer +heap_get_latest_tid(Relation relation, + Snapshot snapshot, + ItemPointer tid) +{ + ItemId lp; + Buffer buffer; + PageHeader dp; + OffsetNumber offnum; + HeapTupleData tp; + HeapTupleHeader t_data; + ItemPointerData ctid; + bool invalidBlock,linkend; + + /* ---------------- + * get the buffer from the relation descriptor + * Note that this does a buffer pin. + * ---------------- + */ + + buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid)); + + if (!BufferIsValid(buffer)) + elog(ERROR, "heap_get_latest_tid: %s relation: ReadBuffer(%lx) failed", + &relation->rd_rel->relname, (long) tid); + + LockBuffer(buffer, BUFFER_LOCK_SHARE); + + /* ---------------- + * get the item line pointer corresponding to the requested tid + * ---------------- + */ + dp = (PageHeader) BufferGetPage(buffer); + offnum = ItemPointerGetOffsetNumber(tid); + invalidBlock = true; + if (!PageIsNew(dp)) + { + lp = PageGetItemId(dp, offnum); + if (ItemIdIsUsed(lp)) + invalidBlock = false; + } + if (invalidBlock) + { + LockBuffer(buffer, BUFFER_LOCK_UNLOCK); + ReleaseBuffer(buffer); + return NULL; + } + + /* ---------------- + * more sanity checks + * ---------------- + */ + + t_data = tp.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp); + tp.t_len = ItemIdGetLength(lp); + tp.t_self = *tid; + ctid = tp.t_data->t_ctid; + + /* ---------------- + * check time qualification of tid + * ---------------- + */ + + HeapTupleSatisfies(&tp, relation, buffer, dp, + snapshot, 0, (ScanKey) NULL); + + linkend = true; + if ((t_data->t_infomask & HEAP_XMAX_COMMITTED) && + !ItemPointerEquals(tid, &ctid)) + linkend = false; + + LockBuffer(buffer, BUFFER_LOCK_UNLOCK); + ReleaseBuffer(buffer); + + if (tp.t_data == NULL) + { + if (linkend) + return NULL; + return heap_get_latest_tid(relation, snapshot, &ctid); + } + + return tid; +} + +/* ---------------- * heap_insert - insert tuple * * The assignment of t_min (and thus the others) should be diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c index 9e4b81ec095..93fb33f8a95 100644 --- a/src/backend/utils/adt/tid.c +++ b/src/backend/utils/adt/tid.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.11 1999/07/17 20:18:00 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.12 1999/10/11 06:28:26 inoue Exp $ * * NOTES * input routine largely stolen from boxin(). @@ -28,9 +28,9 @@ * ---------------------------------------------------------------- */ ItemPointer -tidin(char *str) +tidin(const char *str) { - char *p, + const char *p, *coord[NTIDARGS]; int i; ItemPointer result; @@ -45,8 +45,12 @@ tidin(char *str) if (*p == DELIM || (*p == LDELIM && !i)) coord[i++] = p + 1; - if (i < NTIDARGS - 1) + /* if (i < NTIDARGS - 1) */ + if (i < NTIDARGS) + { + elog(ERROR, "%s invalid tid format", str); return NULL; + } blockNumber = (BlockNumber) atoi(coord[0]); offsetNumber = (OffsetNumber) atoi(coord[1]); @@ -70,6 +74,14 @@ tidout(ItemPointer itemPtr) BlockId blockId; char buf[32]; char *str; + static char *invalidTid = "()"; + + if (!itemPtr || !ItemPointerIsValid(itemPtr)) + { + str = palloc(strlen(invalidTid)); + strcpy(str, invalidTid); + return str; + } blockId = &(itemPtr->ip_blkid); @@ -83,3 +95,113 @@ tidout(ItemPointer itemPtr) return str; } + +/***************************************************************************** + * PUBLIC ROUTINES * + *****************************************************************************/ + +bool +tideq(ItemPointer arg1, ItemPointer arg2) +{ + if ((!arg1) || (!arg2)) + { + return false; + } + + return ( BlockIdGetBlockNumber(&(arg1->ip_blkid)) == + BlockIdGetBlockNumber(&(arg2->ip_blkid)) && + arg1->ip_posid == arg2->ip_posid ); +} + +bool +tidne(ItemPointer arg1, ItemPointer arg2) +{ + if ((!arg1) || (!arg2)) + { + return false; + } + return ( BlockIdGetBlockNumber(&(arg1->ip_blkid)) != + BlockIdGetBlockNumber(&(arg2->ip_blkid)) || + arg1->ip_posid != arg2->ip_posid ); +} + +text * +tid_text(ItemPointer tid) +{ + char *str; + + if (!tid) return (text *)NULL; + str = tidout(tid); + + return textin(str); +} /* tid_text() */ + +ItemPointer +text_tid(const text *string) +{ + ItemPointer result; + char *str; + + if (!string) return (ItemPointer)0; + + str = textout(string); + result = tidin(str); + pfree(str); + + return result; +} /* text_tid() */ + + +/* + * Functions to get latest tid of a specified tuple. + * Maybe these implementations is moved + * to another place +*/ +#include <utils/relcache.h> +ItemPointer +currtid_byreloid(Oid reloid, ItemPointer tid) +{ + ItemPointer result = NULL, ret; + Relation rel; + + result = (ItemPointer) palloc(sizeof(ItemPointerData)); + ItemPointerSetInvalid(result); + if (rel = heap_open(reloid, AccessShareLock), rel) + { + ret = heap_get_latest_tid(rel, SnapshotNow, tid); + if (ret) + ItemPointerCopy(ret, result); + heap_close(rel, AccessShareLock); + } + else + elog(ERROR, "Relation %u not found", reloid); + + return result; +} /* currtid_byreloid() */ + +ItemPointer +currtid_byrelname(const text *relname, ItemPointer tid) +{ + ItemPointer result = NULL, ret; + char *str; + Relation rel; + + if (!relname) return result; + + str = textout(relname); + + result = (ItemPointer) palloc(sizeof(ItemPointerData)); + ItemPointerSetInvalid(result); + if (rel = heap_openr(str, AccessShareLock), rel) + { + ret = heap_get_latest_tid(rel, SnapshotNow, tid); + if (ret) + ItemPointerCopy(ret, result); + heap_close(rel, AccessShareLock); + } + else + elog(ERROR, "Relation %s not found", relname); + pfree(str); + + return result; +} /* currtid_byrelname() */ |