1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* tid.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Functions for the built-in type tuple id
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-07-06 01:12:09 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.21 2000/07/05 23:11:35 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* input routine largely stolen from boxin().
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1999-07-17 22:18:55 +02:00
|
|
|
#include "postgres.h"
|
2000-05-29 03:59:17 +02:00
|
|
|
|
|
|
|
#include "access/heapam.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/builtins.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#define LDELIM '('
|
|
|
|
#define RDELIM ')'
|
|
|
|
#define DELIM ','
|
|
|
|
#define NTIDARGS 2
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* tidin
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
ItemPointer
|
1999-10-11 08:28:29 +02:00
|
|
|
tidin(const char *str)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
const char *p,
|
1997-09-08 04:41:22 +02:00
|
|
|
*coord[NTIDARGS];
|
|
|
|
int i;
|
|
|
|
ItemPointer result;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
BlockNumber blockNumber;
|
|
|
|
OffsetNumber offsetNumber;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
|
|
|
|
if (*p == DELIM || (*p == LDELIM && !i))
|
|
|
|
coord[i++] = p + 1;
|
|
|
|
|
1999-10-11 08:28:29 +02:00
|
|
|
/* if (i < NTIDARGS - 1) */
|
|
|
|
if (i < NTIDARGS)
|
|
|
|
{
|
|
|
|
elog(ERROR, "%s invalid tid format", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
return NULL;
|
1999-10-11 08:28:29 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
blockNumber = (BlockNumber) atoi(coord[0]);
|
|
|
|
offsetNumber = (OffsetNumber) atoi(coord[1]);
|
|
|
|
|
|
|
|
result = (ItemPointer) palloc(sizeof(ItemPointerData));
|
|
|
|
|
|
|
|
ItemPointerSet(result, blockNumber, offsetNumber);
|
|
|
|
|
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* tidout
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1996-07-09 08:22:35 +02:00
|
|
|
tidout(ItemPointer itemPtr)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
BlockNumber blockNumber;
|
|
|
|
OffsetNumber offsetNumber;
|
|
|
|
BlockId blockId;
|
|
|
|
char buf[32];
|
|
|
|
char *str;
|
1999-10-11 08:28:29 +02:00
|
|
|
static char *invalidTid = "()";
|
|
|
|
|
|
|
|
if (!itemPtr || !ItemPointerIsValid(itemPtr))
|
|
|
|
{
|
|
|
|
str = palloc(strlen(invalidTid));
|
|
|
|
strcpy(str, invalidTid);
|
|
|
|
return str;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
blockId = &(itemPtr->ip_blkid);
|
|
|
|
|
|
|
|
blockNumber = BlockIdGetBlockNumber(blockId);
|
|
|
|
offsetNumber = itemPtr->ip_posid;
|
|
|
|
|
|
|
|
sprintf(buf, "(%d,%d)", blockNumber, offsetNumber);
|
|
|
|
|
|
|
|
str = (char *) palloc(strlen(buf) + 1);
|
|
|
|
strcpy(str, buf);
|
|
|
|
|
|
|
|
return str;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-10-11 08:28:29 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* PUBLIC ROUTINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
bool
|
|
|
|
tideq(ItemPointer arg1, ItemPointer arg2)
|
|
|
|
{
|
|
|
|
if ((!arg1) || (!arg2))
|
|
|
|
return false;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
return (BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
|
|
|
|
BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
|
|
|
|
arg1->ip_posid == arg2->ip_posid);
|
1999-10-11 08:28:29 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 00:38:00 +02:00
|
|
|
#ifdef NOT_USED
|
1999-10-11 08:28:29 +02:00
|
|
|
bool
|
|
|
|
tidne(ItemPointer arg1, ItemPointer arg2)
|
|
|
|
{
|
|
|
|
if ((!arg1) || (!arg2))
|
|
|
|
return false;
|
2000-04-12 19:17:23 +02:00
|
|
|
return (BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
|
|
|
|
BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
|
|
|
|
arg1->ip_posid != arg2->ip_posid);
|
1999-10-11 08:28:29 +02:00
|
|
|
}
|
2000-06-09 00:38:00 +02:00
|
|
|
#endif
|
1999-10-11 08:28:29 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions to get latest tid of a specified tuple.
|
2000-06-09 03:11:16 +02:00
|
|
|
*
|
|
|
|
* Maybe these implementations should be moved to another place
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
currtid_byreloid(PG_FUNCTION_ARGS)
|
1999-10-11 08:28:29 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Oid reloid = PG_GETARG_OID(0);
|
|
|
|
ItemPointer tid = (ItemPointer) PG_GETARG_POINTER(1);
|
|
|
|
ItemPointer result,
|
|
|
|
ret;
|
|
|
|
Relation rel;
|
1999-10-11 08:28:29 +02:00
|
|
|
|
|
|
|
result = (ItemPointer) palloc(sizeof(ItemPointerData));
|
|
|
|
ItemPointerSetInvalid(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
if ((rel = heap_open(reloid, AccessShareLock)) != NULL)
|
1999-10-11 08:28:29 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
1999-10-11 08:28:29 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
currtid_byrelname(PG_FUNCTION_ARGS)
|
1999-10-11 08:28:29 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
|
|
|
ItemPointer tid = (ItemPointer) PG_GETARG_POINTER(1);
|
|
|
|
ItemPointer result,
|
|
|
|
ret;
|
|
|
|
char *str;
|
|
|
|
Relation rel;
|
1999-10-11 08:28:29 +02:00
|
|
|
|
2000-07-06 01:12:09 +02:00
|
|
|
str = DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(relname)));
|
1999-10-11 08:28:29 +02:00
|
|
|
|
|
|
|
result = (ItemPointer) palloc(sizeof(ItemPointerData));
|
|
|
|
ItemPointerSetInvalid(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
if ((rel = heap_openr(str, AccessShareLock)) != NULL)
|
1999-10-11 08:28:29 +02:00
|
|
|
{
|
|
|
|
ret = heap_get_latest_tid(rel, SnapshotNow, tid);
|
|
|
|
if (ret)
|
|
|
|
ItemPointerCopy(ret, result);
|
|
|
|
heap_close(rel, AccessShareLock);
|
|
|
|
}
|
|
|
|
else
|
2000-06-09 03:11:16 +02:00
|
|
|
elog(ERROR, "Relation %s not found", str);
|
|
|
|
|
1999-10-11 08:28:29 +02:00
|
|
|
pfree(str);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|