1996-07-09 06:22:35 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* hio.c
|
1997-09-07 05:04:48 +00:00
|
|
|
* POSTGRES heap access method input/output code.
|
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-05-12 19:58:28 +00:00
|
|
|
* $Id: hio.c,v 1.38 2001/05/12 19:58:27 tgl Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1999-07-15 23:04:24 +00:00
|
|
|
#include "postgres.h"
|
1996-10-20 08:32:11 +00:00
|
|
|
|
1999-07-15 23:04:24 +00:00
|
|
|
#include "access/heapam.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "access/hio.h"
|
1996-10-21 05:59:49 +00:00
|
|
|
|
1996-07-09 06:22:35 +00:00
|
|
|
/*
|
2001-03-22 04:01:46 +00:00
|
|
|
* RelationPutHeapTuple - place tuple at specified page
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
2000-07-03 02:54:21 +00:00
|
|
|
* !!! ELOG(ERROR) IS DISALLOWED HERE !!!
|
1998-12-15 12:47:01 +00:00
|
|
|
*
|
2000-07-03 02:54:21 +00:00
|
|
|
* Note - we assume that caller hold BUFFER_LOCK_EXCLUSIVE on the buffer.
|
1998-12-15 12:47:01 +00:00
|
|
|
*
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
RelationPutHeapTuple(Relation relation,
|
1998-12-15 12:47:01 +00:00
|
|
|
Buffer buffer,
|
1997-09-07 05:04:48 +00:00
|
|
|
HeapTuple tuple)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1999-05-25 16:15:34 +00:00
|
|
|
Page pageHeader;
|
|
|
|
OffsetNumber offnum;
|
2000-03-17 02:36:41 +00:00
|
|
|
Size len;
|
1999-05-25 16:15:34 +00:00
|
|
|
ItemId itemId;
|
|
|
|
Item item;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2001-03-22 06:16:21 +00:00
|
|
|
/*
|
|
|
|
* increment access statistics
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
|
|
|
IncrHeapAccessStat(local_RelationPutHeapTuple);
|
|
|
|
IncrHeapAccessStat(global_RelationPutHeapTuple);
|
|
|
|
|
|
|
|
pageHeader = (Page) BufferGetPage(buffer);
|
2000-04-12 17:17:23 +00:00
|
|
|
len = MAXALIGN(tuple->t_len); /* be conservative */
|
2000-03-17 02:36:41 +00:00
|
|
|
Assert(len <= PageGetFreeSpace(pageHeader));
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1998-11-27 19:52:36 +00:00
|
|
|
offnum = PageAddItem((Page) pageHeader, (Item) tuple->t_data,
|
1997-09-07 05:04:48 +00:00
|
|
|
tuple->t_len, InvalidOffsetNumber, LP_USED);
|
|
|
|
|
2000-07-03 02:54:21 +00:00
|
|
|
if (offnum == InvalidOffsetNumber)
|
|
|
|
elog(STOP, "RelationPutHeapTuple: failed to add tuple");
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
itemId = PageGetItemId((Page) pageHeader, offnum);
|
|
|
|
item = PageGetItem((Page) pageHeader, itemId);
|
|
|
|
|
1999-05-25 16:15:34 +00:00
|
|
|
ItemPointerSet(&((HeapTupleHeader) item)->t_ctid,
|
|
|
|
BufferGetBlockNumber(buffer), offnum);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
/* return an accurate tuple */
|
1998-12-15 12:47:01 +00:00
|
|
|
ItemPointerSet(&tuple->t_self, BufferGetBlockNumber(buffer), offnum);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-07-03 02:54:21 +00:00
|
|
|
* RelationGetBufferForTuple
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
2001-05-12 19:58:28 +00:00
|
|
|
* Returns exclusive-locked buffer with free space >= given len.
|
2000-09-07 09:58:38 +00:00
|
|
|
*
|
2001-03-22 04:01:46 +00:00
|
|
|
* Note that we use LockPage to lock relation for extension. We can
|
2000-09-07 09:58:38 +00:00
|
|
|
* do this as long as in all other places we use page-level locking
|
|
|
|
* for indices only. Alternatively, we could define pseudo-table as
|
|
|
|
* we do for transactions with XactLockTable.
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
2000-07-03 02:54:21 +00:00
|
|
|
* ELOG(ERROR) is allowed here, so this routine *must* be called
|
|
|
|
* before any (unlogged) changes are made in buffer pool.
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
2000-07-03 02:54:21 +00:00
|
|
|
Buffer
|
2000-09-07 09:58:38 +00:00
|
|
|
RelationGetBufferForTuple(Relation relation, Size len)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2001-05-12 19:58:28 +00:00
|
|
|
Buffer buffer = InvalidBuffer;
|
1997-09-08 02:41:22 +00:00
|
|
|
Page pageHeader;
|
2001-05-12 19:58:28 +00:00
|
|
|
BlockNumber lastblock,
|
|
|
|
oldnblocks;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-07-03 02:54:21 +00:00
|
|
|
len = MAXALIGN(len); /* be conservative */
|
1999-11-29 04:34:55 +00:00
|
|
|
|
|
|
|
/*
|
2000-07-03 02:54:21 +00:00
|
|
|
* If we're gonna fail for oversize tuple, do it right away
|
1999-11-29 04:34:55 +00:00
|
|
|
*/
|
|
|
|
if (len > MaxTupleSize)
|
2000-11-16 05:51:07 +00:00
|
|
|
elog(ERROR, "Tuple is too big: size %lu, max size %ld",
|
2001-03-22 04:01:46 +00:00
|
|
|
(unsigned long) len, MaxTupleSize);
|
1999-11-29 04:34:55 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*
|
2001-05-12 19:58:28 +00:00
|
|
|
* First, use relcache's record of table length to guess where the
|
|
|
|
* last page is, and try to put the tuple there. This cached value
|
|
|
|
* may be out of date, in which case we'll be inserting into a non-last
|
|
|
|
* page, but that should be OK. Note that in a newly created relcache
|
|
|
|
* entry, rd_nblocks may be zero; if so, we'll set it correctly below.
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
2001-05-12 19:58:28 +00:00
|
|
|
if (relation->rd_nblocks > 0)
|
2000-09-07 09:58:38 +00:00
|
|
|
{
|
2001-05-12 19:58:28 +00:00
|
|
|
lastblock = relation->rd_nblocks - 1;
|
|
|
|
buffer = ReadBuffer(relation, lastblock);
|
2000-07-03 02:54:21 +00:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
2000-09-07 09:58:38 +00:00
|
|
|
pageHeader = (Page) BufferGetPage(buffer);
|
2001-05-12 19:58:28 +00:00
|
|
|
if (len <= PageGetFreeSpace(pageHeader))
|
|
|
|
return buffer;
|
|
|
|
/*
|
|
|
|
* Doesn't fit, so we'll have to try someplace else.
|
|
|
|
*/
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
/* buffer release will happen below... */
|
2000-09-07 09:58:38 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
/*
|
2001-05-12 19:58:28 +00:00
|
|
|
* Before extending relation, make sure no one else has done
|
|
|
|
* so more recently than our last rd_nblocks update. (If we
|
|
|
|
* blindly extend the relation here, then probably most of the
|
|
|
|
* page the other guy added will end up going to waste.)
|
|
|
|
*
|
|
|
|
* We have to use a lock to ensure no one else is extending the
|
|
|
|
* rel at the same time, else we will both try to initialize the
|
|
|
|
* same new page.
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
2001-05-12 19:58:28 +00:00
|
|
|
if (!relation->rd_myxactonly)
|
|
|
|
LockPage(relation, 0, ExclusiveLock);
|
|
|
|
|
|
|
|
oldnblocks = relation->rd_nblocks;
|
|
|
|
/*
|
|
|
|
* XXX This does an lseek - rather expensive - but at the moment it is
|
|
|
|
* the only way to accurately determine how many blocks are in a
|
|
|
|
* relation. Is it worth keeping an accurate file length in shared
|
|
|
|
* memory someplace, rather than relying on the kernel to do it for us?
|
|
|
|
*/
|
|
|
|
relation->rd_nblocks = RelationGetNumberOfBlocks(relation);
|
|
|
|
|
|
|
|
if (relation->rd_nblocks > oldnblocks)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2001-05-12 19:58:28 +00:00
|
|
|
/*
|
|
|
|
* Someone else has indeed extended the relation recently.
|
|
|
|
* Try to fit our tuple into the new last page.
|
|
|
|
*/
|
|
|
|
lastblock = relation->rd_nblocks - 1;
|
|
|
|
buffer = ReleaseAndReadBuffer(buffer, relation, lastblock, false);
|
1998-12-15 12:47:01 +00:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
1997-09-07 05:04:48 +00:00
|
|
|
pageHeader = (Page) BufferGetPage(buffer);
|
2001-05-12 19:58:28 +00:00
|
|
|
if (len <= PageGetFreeSpace(pageHeader))
|
1999-11-29 04:34:55 +00:00
|
|
|
{
|
2001-05-12 19:58:28 +00:00
|
|
|
/* OK, we don't need to extend again. */
|
|
|
|
if (!relation->rd_myxactonly)
|
|
|
|
UnlockPage(relation, 0, ExclusiveLock);
|
|
|
|
return buffer;
|
1999-11-29 04:34:55 +00:00
|
|
|
}
|
2001-05-12 19:58:28 +00:00
|
|
|
/*
|
|
|
|
* Doesn't fit, so we'll have to extend the relation (again).
|
|
|
|
*/
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
/* buffer release will happen below... */
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2001-05-12 19:58:28 +00:00
|
|
|
/*
|
|
|
|
* Extend the relation by one page and update rd_nblocks for next time.
|
|
|
|
*/
|
|
|
|
lastblock = relation->rd_nblocks;
|
|
|
|
buffer = ReleaseAndReadBuffer(buffer, relation, lastblock, true);
|
|
|
|
relation->rd_nblocks = lastblock + 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to initialize the empty new page.
|
|
|
|
*/
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
pageHeader = (Page) BufferGetPage(buffer);
|
|
|
|
Assert(PageIsNew((PageHeader) pageHeader));
|
|
|
|
PageInit(pageHeader, BufferGetPageSize(buffer), 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release the file-extension lock; it's now OK for someone else
|
|
|
|
* to extend the relation some more.
|
|
|
|
*/
|
1999-02-02 03:45:56 +00:00
|
|
|
if (!relation->rd_myxactonly)
|
1999-05-07 01:23:11 +00:00
|
|
|
UnlockPage(relation, 0, ExclusiveLock);
|
1998-12-15 12:47:01 +00:00
|
|
|
|
2001-05-12 19:58:28 +00:00
|
|
|
if (len > PageGetFreeSpace(pageHeader))
|
|
|
|
{
|
|
|
|
/* We should not get here given the test at the top */
|
|
|
|
elog(STOP, "Tuple is too big: size %lu",
|
|
|
|
(unsigned long) len);
|
|
|
|
}
|
1998-12-15 12:47:01 +00:00
|
|
|
|
2001-05-12 19:58:28 +00:00
|
|
|
return buffer;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|