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