1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* heapam.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap access method code
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-08-12 03:36:05 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.197 2005/08/12 01:35:54 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* INTERFACE ROUTINES
|
2001-11-02 17:30:29 +01:00
|
|
|
* relation_open - open any relation by relation OID
|
2002-09-04 22:31:48 +02:00
|
|
|
* relation_openrv - open any relation specified by a RangeVar
|
2001-11-02 17:30:29 +01:00
|
|
|
* relation_close - close any relation
|
|
|
|
* heap_open - open a heap relation by relation OID
|
2002-03-26 20:17:02 +01:00
|
|
|
* heap_openrv - open a heap relation specified by a RangeVar
|
2001-11-02 17:30:29 +01:00
|
|
|
* heap_close - (now just a macro for relation_close)
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_beginscan - begin relation scan
|
|
|
|
* heap_rescan - restart a relation scan
|
|
|
|
* heap_endscan - end relation scan
|
|
|
|
* heap_getnext - retrieve next tuple in scan
|
2002-05-21 01:51:44 +02:00
|
|
|
* heap_fetch - retrieve tuple with tid
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_insert - insert tuple into a relation
|
|
|
|
* heap_delete - delete a tuple from a relation
|
2000-09-07 11:58:38 +02:00
|
|
|
* heap_update - replace a tuple in a relation with another tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_markpos - mark scan position
|
|
|
|
* heap_restrpos - restore position to marked location
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* This file contains the heap_ routines which implement
|
|
|
|
* the POSTGRES heap access method used for all POSTGRES
|
|
|
|
* relations.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
1996-10-20 10:32:11 +02:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "access/hio.h"
|
2005-04-28 23:47:18 +02:00
|
|
|
#include "access/multixact.h"
|
2000-07-04 01:58:32 +02:00
|
|
|
#include "access/tuptoaster.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "access/valid.h"
|
2001-06-09 20:16:59 +02:00
|
|
|
#include "access/xlogutils.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "catalog/catalog.h"
|
2002-03-26 20:17:02 +01:00
|
|
|
#include "catalog/namespace.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "miscadmin.h"
|
2005-05-19 23:35:48 +02:00
|
|
|
#include "storage/procarray.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/inval.h"
|
|
|
|
#include "utils/relcache.h"
|
2001-06-22 21:16:24 +02:00
|
|
|
#include "pgstat.h"
|
1996-10-20 10:32:11 +02:00
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
|
|
|
|
ItemPointerData from, Buffer newbuf, HeapTuple newtup, bool move);
|
2000-10-20 13:01:21 +02:00
|
|
|
|
1996-10-21 07:59:49 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap support routines
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
1998-08-19 04:04:17 +02:00
|
|
|
* initscan - scan code common to heap_beginscan and heap_rescan
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
2002-05-21 01:51:44 +02:00
|
|
|
initscan(HeapScanDesc scan, ScanKey key)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-05-08 21:09:25 +02:00
|
|
|
* Determine the number of blocks we have to scan.
|
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* It is sufficient to do this once at scan start, since any tuples added
|
|
|
|
* while the scan is in progress will be invisible to my transaction
|
|
|
|
* anyway...
|
1999-10-31 01:10:21 +02:00
|
|
|
*/
|
2004-05-08 21:09:25 +02:00
|
|
|
scan->rs_nblocks = RelationGetNumberOfBlocks(scan->rs_rd);
|
1999-10-31 01:10:21 +02:00
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
scan->rs_ctup.t_datamcxt = NULL;
|
|
|
|
scan->rs_ctup.t_data = NULL;
|
|
|
|
scan->rs_cbuf = InvalidBuffer;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* we don't have a marked position... */
|
1998-08-19 04:04:17 +02:00
|
|
|
ItemPointerSetInvalid(&(scan->rs_mctid));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* copy the scan key, if appropriate
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (key != NULL)
|
2002-05-21 01:51:44 +02:00
|
|
|
memcpy(scan->rs_key, key, scan->rs_nkeys * sizeof(ScanKeyData));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heapgettup - fetch next heap tuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* routine used by heap_getnext() which does most of the
|
|
|
|
* real work in scanning tuples.
|
1998-08-19 04:04:17 +02:00
|
|
|
*
|
2001-06-09 20:16:59 +02:00
|
|
|
* The passed-in *buffer must be either InvalidBuffer or the pinned
|
|
|
|
* current page of the scan. If we have to move to another page,
|
|
|
|
* we will unpin this buffer (if valid). On return, *buffer is either
|
|
|
|
* InvalidBuffer or the ID of a pinned buffer.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1998-11-27 20:52:36 +01:00
|
|
|
static void
|
1996-07-09 08:22:35 +02:00
|
|
|
heapgettup(Relation relation,
|
1997-09-07 07:04:48 +02:00
|
|
|
int dir,
|
2001-06-09 20:16:59 +02:00
|
|
|
HeapTuple tuple,
|
1998-12-15 13:47:01 +01:00
|
|
|
Buffer *buffer,
|
1998-07-27 21:38:40 +02:00
|
|
|
Snapshot snapshot,
|
1997-09-07 07:04:48 +02:00
|
|
|
int nkeys,
|
2004-05-08 21:09:25 +02:00
|
|
|
ScanKey key,
|
|
|
|
BlockNumber pages)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
ItemId lpp;
|
|
|
|
Page dp;
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber page;
|
1999-05-25 18:15:34 +02:00
|
|
|
int lines;
|
|
|
|
OffsetNumber lineoff;
|
|
|
|
int linesleft;
|
2001-06-09 20:16:59 +02:00
|
|
|
ItemPointer tid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-01-07 19:56:30 +01:00
|
|
|
tid = (tuple->t_data == NULL) ? NULL : &(tuple->t_self);
|
2001-06-09 20:16:59 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* debugging stuff
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2001-03-22 07:16:21 +01:00
|
|
|
* check validity of arguments, here and for other functions too Note: no
|
|
|
|
* locking manipulations needed--this is a local function
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifdef HEAPDEBUGALL
|
|
|
|
if (ItemPointerIsValid(tid))
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(DEBUG2, "heapgettup(%s, tid=0x%x[%d,%d], dir=%d, ...)",
|
1997-09-07 07:04:48 +02:00
|
|
|
RelationGetRelationName(relation), tid, tid->ip_blkid,
|
|
|
|
tid->ip_posid, dir);
|
|
|
|
else
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(DEBUG2, "heapgettup(%s, tid=0x%x, dir=%d, ...)",
|
1997-09-07 07:04:48 +02:00
|
|
|
RelationGetRelationName(relation), tid, dir);
|
1997-11-21 00:24:03 +01:00
|
|
|
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(DEBUG2, "heapgettup(..., b=0x%x, nkeys=%d, key=0x%x", buffer, nkeys, key);
|
|
|
|
|
|
|
|
elog(DEBUG2, "heapgettup: relation(%c)=`%s', %p",
|
1999-11-08 00:08:36 +01:00
|
|
|
relation->rd_rel->relkind, RelationGetRelationName(relation),
|
1998-07-27 21:38:40 +02:00
|
|
|
snapshot);
|
2004-01-05 21:36:04 +01:00
|
|
|
#endif /* HEAPDEBUGALL */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!ItemPointerIsValid(tid))
|
2001-06-09 20:16:59 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(!PointerIsValid(tid));
|
2001-06-09 20:16:59 +02:00
|
|
|
tid = NULL;
|
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-11-14 22:04:32 +01:00
|
|
|
tuple->t_tableOid = relation->rd_id;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* return null immediately if relation is empty
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-05-08 21:09:25 +02:00
|
|
|
if (pages == 0)
|
1998-11-27 20:52:36 +01:00
|
|
|
{
|
2001-06-09 20:16:59 +02:00
|
|
|
if (BufferIsValid(*buffer))
|
|
|
|
ReleaseBuffer(*buffer);
|
|
|
|
*buffer = InvalidBuffer;
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tuple->t_data = NULL;
|
|
|
|
return;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* calculate next starting lineoff, given scan direction
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
if (dir == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-06-09 20:16:59 +02:00
|
|
|
* ``no movement'' scan direction: refetch same tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-09 20:16:59 +02:00
|
|
|
if (tid == NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-09 20:16:59 +02:00
|
|
|
if (BufferIsValid(*buffer))
|
|
|
|
ReleaseBuffer(*buffer);
|
1998-12-15 13:47:01 +01:00
|
|
|
*buffer = InvalidBuffer;
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tuple->t_data = NULL;
|
|
|
|
return;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
*buffer = ReleaseAndReadBuffer(*buffer,
|
|
|
|
relation,
|
2001-06-29 23:08:25 +02:00
|
|
|
ItemPointerGetBlockNumber(tid));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
dp = (Page) BufferGetPage(*buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
lineoff = ItemPointerGetOffsetNumber(tid);
|
|
|
|
lpp = PageGetItemId(dp, lineoff);
|
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
|
|
|
|
tuple->t_len = ItemIdGetLength(lpp);
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
return;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else if (dir < 0)
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* reverse scan direction
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (tid == NULL)
|
|
|
|
{
|
|
|
|
page = pages - 1; /* final page */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
page = ItemPointerGetBlockNumber(tid); /* current page */
|
|
|
|
}
|
2001-06-28 01:31:40 +02:00
|
|
|
|
|
|
|
Assert(page < pages);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
*buffer = ReleaseAndReadBuffer(*buffer,
|
|
|
|
relation,
|
2001-06-29 23:08:25 +02:00
|
|
|
page);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
dp = (Page) BufferGetPage(*buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
lines = PageGetMaxOffsetNumber(dp);
|
|
|
|
if (tid == NULL)
|
|
|
|
{
|
|
|
|
lineoff = lines; /* final offnum */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
lineoff = /* previous offnum */
|
|
|
|
OffsetNumberPrev(ItemPointerGetOffsetNumber(tid));
|
|
|
|
}
|
|
|
|
/* page and lineoff now reference the physically previous tid */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* forward scan direction
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-09 20:16:59 +02:00
|
|
|
if (tid == NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
page = 0; /* first page */
|
|
|
|
lineoff = FirstOffsetNumber; /* first offnum */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
page = ItemPointerGetBlockNumber(tid); /* current page */
|
|
|
|
lineoff = /* next offnum */
|
|
|
|
OffsetNumberNext(ItemPointerGetOffsetNumber(tid));
|
|
|
|
}
|
|
|
|
|
2001-06-28 01:31:40 +02:00
|
|
|
Assert(page < pages);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
*buffer = ReleaseAndReadBuffer(*buffer,
|
|
|
|
relation,
|
2001-06-29 23:08:25 +02:00
|
|
|
page);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
dp = (Page) BufferGetPage(*buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
lines = PageGetMaxOffsetNumber(dp);
|
2001-06-09 20:16:59 +02:00
|
|
|
/* page and lineoff now reference the physically next tid */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* 'dir' is now non-zero */
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* calculate line pointer and number of remaining items to check on
|
|
|
|
* this page.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
lpp = PageGetItemId(dp, lineoff);
|
|
|
|
if (dir < 0)
|
|
|
|
linesleft = lineoff - 1;
|
|
|
|
else
|
|
|
|
linesleft = lines - lineoff;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* advance the scan until we find a qualifying tuple or run out of
|
|
|
|
* stuff to scan
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
while (linesleft >= 0)
|
|
|
|
{
|
1998-11-27 20:52:36 +01:00
|
|
|
if (ItemIdIsUsed(lpp))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
bool valid;
|
2002-05-24 20:57:57 +02:00
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
|
|
|
|
tuple->t_len = ItemIdGetLength(lpp);
|
|
|
|
ItemPointerSet(&(tuple->t_self), page, lineoff);
|
2001-03-22 07:16:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if current tuple qualifies, return it.
|
1998-11-27 20:52:36 +01:00
|
|
|
*/
|
1998-12-15 13:47:01 +01:00
|
|
|
HeapTupleSatisfies(tuple, relation, *buffer, (PageHeader) dp,
|
2002-05-24 20:57:57 +02:00
|
|
|
snapshot, nkeys, key, valid);
|
|
|
|
if (valid)
|
1998-12-15 13:47:01 +01:00
|
|
|
{
|
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
1998-11-27 20:52:36 +01:00
|
|
|
return;
|
1998-12-15 13:47:01 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* otherwise move to the next item on the page
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
--linesleft;
|
|
|
|
if (dir < 0)
|
|
|
|
{
|
|
|
|
--lpp; /* move back in this page's ItemId array */
|
1998-11-27 20:52:36 +01:00
|
|
|
--lineoff;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
++lpp; /* move forward in this page's ItemId
|
|
|
|
* array */
|
1998-11-27 20:52:36 +01:00
|
|
|
++lineoff;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* if we get here, it means we've exhausted the items on this page
|
2001-06-09 20:16:59 +02:00
|
|
|
* and it's time to move to the next.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-06-09 20:16:59 +02:00
|
|
|
* return NULL if we've exhausted all the pages
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
if ((dir < 0) ? (page == 0) : (page + 1 >= pages))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-12-15 13:47:01 +01:00
|
|
|
if (BufferIsValid(*buffer))
|
|
|
|
ReleaseBuffer(*buffer);
|
|
|
|
*buffer = InvalidBuffer;
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tuple->t_data = NULL;
|
|
|
|
return;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-06-28 01:31:40 +02:00
|
|
|
page = (dir < 0) ? (page - 1) : (page + 1);
|
|
|
|
|
|
|
|
Assert(page < pages);
|
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
*buffer = ReleaseAndReadBuffer(*buffer,
|
|
|
|
relation,
|
2001-06-29 23:08:25 +02:00
|
|
|
page);
|
2001-06-09 20:16:59 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_SHARE);
|
|
|
|
dp = (Page) BufferGetPage(*buffer);
|
1998-11-27 20:52:36 +01:00
|
|
|
lines = PageGetMaxOffsetNumber((Page) dp);
|
1997-09-07 07:04:48 +02:00
|
|
|
linesleft = lines - 1;
|
|
|
|
if (dir < 0)
|
1998-11-27 20:52:36 +01:00
|
|
|
{
|
|
|
|
lineoff = lines;
|
|
|
|
lpp = PageGetItemId(dp, lines);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
1998-11-27 20:52:36 +01:00
|
|
|
{
|
|
|
|
lineoff = FirstOffsetNumber;
|
1997-09-07 07:04:48 +02:00
|
|
|
lpp = PageGetItemId(dp, FirstOffsetNumber);
|
1998-11-27 20:52:36 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-30 18:11:02 +02:00
|
|
|
#if defined(DISABLE_COMPLEX_MACRO)
|
|
|
|
/*
|
|
|
|
* This is formatted so oddly so that the correspondence to the macro
|
|
|
|
* definition in access/heapam.h is maintained.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
|
|
|
|
bool *isnull)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
(attnum) > 0 ?
|
|
|
|
(
|
|
|
|
((isnull) ? (*(isnull) = false) : (dummyret) NULL),
|
|
|
|
HeapTupleNoNulls(tup) ?
|
|
|
|
(
|
2000-11-30 19:38:47 +01:00
|
|
|
(tupleDesc)->attrs[(attnum) - 1]->attcacheoff >= 0 ?
|
2000-06-30 18:11:02 +02:00
|
|
|
(
|
2001-03-22 05:01:46 +01:00
|
|
|
fetchatt((tupleDesc)->attrs[(attnum) - 1],
|
|
|
|
(char *) (tup)->t_data + (tup)->t_data->t_hoff +
|
|
|
|
(tupleDesc)->attrs[(attnum) - 1]->attcacheoff)
|
2000-06-30 18:11:02 +02:00
|
|
|
)
|
|
|
|
:
|
|
|
|
nocachegetattr((tup), (attnum), (tupleDesc), (isnull))
|
|
|
|
)
|
|
|
|
:
|
|
|
|
(
|
|
|
|
att_isnull((attnum) - 1, (tup)->t_data->t_bits) ?
|
|
|
|
(
|
|
|
|
((isnull) ? (*(isnull) = true) : (dummyret) NULL),
|
|
|
|
(Datum) NULL
|
|
|
|
)
|
|
|
|
:
|
|
|
|
(
|
|
|
|
nocachegetattr((tup), (attnum), (tupleDesc), (isnull))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
:
|
|
|
|
(
|
|
|
|
(Datum) NULL
|
|
|
|
)
|
2001-10-25 07:50:21 +02:00
|
|
|
);
|
2000-06-30 18:11:02 +02:00
|
|
|
}
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* defined(DISABLE_COMPLEX_MACRO) */
|
2000-06-30 18:11:02 +02:00
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap access method interface
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2000-08-03 21:19:38 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
2001-11-02 17:30:29 +01:00
|
|
|
* relation_open - open any relation by relation OID
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-08-03 21:19:38 +02:00
|
|
|
* If lockmode is not "NoLock", the specified kind of lock is
|
2001-11-02 17:30:29 +01:00
|
|
|
* obtained on the relation. (Generally, NoLock should only be
|
|
|
|
* used if the caller knows it has some appropriate lock on the
|
|
|
|
* relation already.)
|
|
|
|
*
|
2000-08-03 21:19:38 +02:00
|
|
|
* An error is raised if the relation does not exist.
|
2001-11-02 17:30:29 +01:00
|
|
|
*
|
|
|
|
* NB: a "relation" is anything with a pg_class entry. The caller is
|
|
|
|
* expected to check whether the relkind is something it can handle.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Relation
|
2001-11-02 17:30:29 +01:00
|
|
|
relation_open(Oid relationId, LOCKMODE lockmode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation r;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
|
|
|
|
|
|
|
|
/* The relcache does all the real work... */
|
|
|
|
r = RelationIdGetRelation(relationId);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!RelationIsValid(r))
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "could not open relation with OID %u", relationId);
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2000-08-03 21:19:38 +02:00
|
|
|
if (lockmode != NoLock)
|
|
|
|
LockRelation(r, lockmode);
|
1999-09-18 21:08:25 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return r;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 18:40:05 +02:00
|
|
|
/* ----------------
|
|
|
|
* conditional_relation_open - open with option not to wait
|
|
|
|
*
|
|
|
|
* As above, but if nowait is true, then throw an error rather than
|
|
|
|
* waiting when the lock is not immediately obtainable.
|
|
|
|
* ----------------
|
|
|
|
*/
|
2004-03-11 02:47:41 +01:00
|
|
|
Relation
|
|
|
|
conditional_relation_open(Oid relationId, LOCKMODE lockmode, bool nowait)
|
|
|
|
{
|
|
|
|
Relation r;
|
|
|
|
|
|
|
|
Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
|
|
|
|
|
|
|
|
/* The relcache does all the real work... */
|
|
|
|
r = RelationIdGetRelation(relationId);
|
|
|
|
|
|
|
|
if (!RelationIsValid(r))
|
|
|
|
elog(ERROR, "could not open relation with OID %u", relationId);
|
|
|
|
|
|
|
|
if (lockmode != NoLock)
|
|
|
|
{
|
|
|
|
if (nowait)
|
|
|
|
{
|
|
|
|
if (!ConditionalLockRelation(r, lockmode))
|
2004-10-01 18:40:05 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("could not obtain lock on relation \"%s\"",
|
2004-10-01 18:40:05 +02:00
|
|
|
RelationGetRelationName(r))));
|
2004-03-11 02:47:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LockRelation(r, lockmode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
2002-03-26 20:17:02 +01:00
|
|
|
* relation_openrv - open any relation specified by a RangeVar
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-03-26 20:17:02 +01:00
|
|
|
* As above, but the relation is specified by a RangeVar.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Relation
|
2002-03-26 20:17:02 +01:00
|
|
|
relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-03-26 20:17:02 +01:00
|
|
|
Oid relOid;
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
/*
|
|
|
|
* Check for shared-cache-inval messages before trying to open the
|
2001-11-05 18:46:40 +01:00
|
|
|
* relation. This is needed to cover the case where the name
|
|
|
|
* identifies a rel that has been dropped and recreated since the
|
2002-03-26 20:17:02 +01:00
|
|
|
* start of our transaction: if we don't flush the old syscache entry
|
2001-11-05 18:46:40 +01:00
|
|
|
* then we'll latch onto that entry and suffer an error when we do
|
|
|
|
* LockRelation. Note that relation_open does not need to do this,
|
|
|
|
* since a relation's OID never changes.
|
2001-11-02 17:30:29 +01:00
|
|
|
*
|
|
|
|
* We skip this if asked for NoLock, on the assumption that the caller
|
|
|
|
* has already ensured some appropriate lock is held.
|
|
|
|
*/
|
|
|
|
if (lockmode != NoLock)
|
|
|
|
AcceptInvalidationMessages();
|
|
|
|
|
2002-03-26 20:17:02 +01:00
|
|
|
/* Look up the appropriate relation using namespace search */
|
|
|
|
relOid = RangeVarGetRelid(relation, false);
|
|
|
|
|
|
|
|
/* Let relation_open do the rest */
|
|
|
|
return relation_open(relOid, lockmode);
|
|
|
|
}
|
|
|
|
|
2000-08-03 21:19:38 +02:00
|
|
|
/* ----------------
|
2001-11-02 17:30:29 +01:00
|
|
|
* relation_close - close any relation
|
|
|
|
*
|
|
|
|
* If lockmode is not "NoLock", we first release the specified lock.
|
2000-08-03 21:19:38 +02:00
|
|
|
*
|
2001-11-02 17:30:29 +01:00
|
|
|
* Note that it is often sensible to hold a lock beyond relation_close;
|
|
|
|
* in that case, the lock is released automatically at xact end.
|
2000-08-03 21:19:38 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2001-11-02 17:30:29 +01:00
|
|
|
void
|
|
|
|
relation_close(Relation relation, LOCKMODE lockmode)
|
2000-08-03 21:19:38 +02:00
|
|
|
{
|
2001-11-02 17:30:29 +01:00
|
|
|
Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
|
2000-08-03 21:19:38 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
if (lockmode != NoLock)
|
|
|
|
UnlockRelation(relation, lockmode);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
/* The relcache does the real work... */
|
|
|
|
RelationClose(relation);
|
2000-08-03 21:19:38 +02:00
|
|
|
}
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
|
2000-08-03 21:19:38 +02:00
|
|
|
/* ----------------
|
2001-11-02 17:30:29 +01:00
|
|
|
* heap_open - open a heap relation by relation OID
|
2000-08-03 21:19:38 +02:00
|
|
|
*
|
2001-11-02 17:30:29 +01:00
|
|
|
* This is essentially relation_open plus check that the relation
|
|
|
|
* is not an index or special relation. (The caller should also check
|
|
|
|
* that it's not a view before assuming it has storage.)
|
2000-08-03 21:19:38 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Relation
|
2001-11-02 17:30:29 +01:00
|
|
|
heap_open(Oid relationId, LOCKMODE lockmode)
|
2000-08-03 21:19:38 +02:00
|
|
|
{
|
|
|
|
Relation r;
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
r = relation_open(relationId, lockmode);
|
2000-08-03 21:19:38 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
if (r->rd_rel->relkind == RELKIND_INDEX)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("\"%s\" is an index",
|
2003-07-21 22:29:40 +02:00
|
|
|
RelationGetRelationName(r))));
|
2001-11-02 17:30:29 +01:00
|
|
|
else if (r->rd_rel->relkind == RELKIND_SPECIAL)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is a special relation",
|
|
|
|
RelationGetRelationName(r))));
|
2002-08-29 02:17:06 +02:00
|
|
|
else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is a composite type",
|
|
|
|
RelationGetRelationName(r))));
|
2001-06-22 21:16:24 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
pgstat_initstats(&r->pgstat_info, r);
|
2001-06-22 21:16:24 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return r;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
2002-03-26 20:17:02 +01:00
|
|
|
* heap_openrv - open a heap relation specified
|
|
|
|
* by a RangeVar node
|
|
|
|
*
|
|
|
|
* As above, but relation is specified by a RangeVar.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Relation
|
|
|
|
heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
|
|
|
|
{
|
|
|
|
Relation r;
|
|
|
|
|
|
|
|
r = relation_openrv(relation, lockmode);
|
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
if (r->rd_rel->relkind == RELKIND_INDEX)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("\"%s\" is an index",
|
2003-07-21 22:29:40 +02:00
|
|
|
RelationGetRelationName(r))));
|
2001-11-02 17:30:29 +01:00
|
|
|
else if (r->rd_rel->relkind == RELKIND_SPECIAL)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is a special relation",
|
|
|
|
RelationGetRelationName(r))));
|
2002-08-29 02:17:06 +02:00
|
|
|
else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is a composite type",
|
|
|
|
RelationGetRelationName(r))));
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2001-11-02 17:30:29 +01:00
|
|
|
pgstat_initstats(&r->pgstat_info, r);
|
|
|
|
|
|
|
|
return r;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_beginscan - begin relation scan
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
HeapScanDesc
|
2002-05-21 01:51:44 +02:00
|
|
|
heap_beginscan(Relation relation, Snapshot snapshot,
|
|
|
|
int nkeys, ScanKey key)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapScanDesc scan;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* increment relation ref count while scanning relation
|
1999-09-18 21:08:25 +02:00
|
|
|
*
|
2001-03-22 07:16:21 +01:00
|
|
|
* This is just to make really sure the relcache entry won't go away
|
|
|
|
* while the scan has a pointer to it. Caller should be holding the
|
|
|
|
* rel open anyway, so this is redundant in all normal scenarios...
|
1999-09-18 21:08:25 +02:00
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
RelationIncrementReferenceCount(relation);
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* allocate and initialize scan descriptor
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
scan = (HeapScanDesc) palloc(sizeof(HeapScanDescData));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
scan->rs_rd = relation;
|
1999-10-31 01:10:21 +02:00
|
|
|
scan->rs_snapshot = snapshot;
|
2002-05-21 01:51:44 +02:00
|
|
|
scan->rs_nkeys = nkeys;
|
2001-06-22 21:16:24 +02:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* we do this here instead of in initscan() because heap_rescan also
|
|
|
|
* calls initscan() and we don't want to allocate memory again
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
if (nkeys > 0)
|
1998-08-19 04:04:17 +02:00
|
|
|
scan->rs_key = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys);
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
1998-08-19 04:04:17 +02:00
|
|
|
scan->rs_key = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
pgstat_initstats(&scan->rs_pgstat_info, relation);
|
|
|
|
|
|
|
|
initscan(scan, key);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return scan;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_rescan - restart a relation scan
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_rescan(HeapScanDesc scan,
|
1997-09-07 07:04:48 +02:00
|
|
|
ScanKey key)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* unpin scan buffers
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-09 20:16:59 +02:00
|
|
|
if (BufferIsValid(scan->rs_cbuf))
|
|
|
|
ReleaseBuffer(scan->rs_cbuf);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* reinitialize scan descriptor
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
initscan(scan, key);
|
2001-06-22 21:16:24 +02:00
|
|
|
|
|
|
|
pgstat_reset_heap_scan(&scan->rs_pgstat_info);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_endscan - end relation scan
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* See how to integrate with index scans.
|
|
|
|
* Check handling if reldesc caching.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_endscan(HeapScanDesc scan)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/* Note: no locking manipulations needed */
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* unpin scan buffers
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-09 20:16:59 +02:00
|
|
|
if (BufferIsValid(scan->rs_cbuf))
|
|
|
|
ReleaseBuffer(scan->rs_cbuf);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* decrement relation reference count and free scan descriptor storage
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
RelationDecrementReferenceCount(scan->rs_rd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-05-21 04:28:55 +02:00
|
|
|
if (scan->rs_key)
|
|
|
|
pfree(scan->rs_key);
|
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
pfree(scan);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_getnext - retrieve next tuple in scan
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Fix to work with index relations.
|
1998-08-19 04:04:17 +02:00
|
|
|
* We don't return the buffer anymore, but you can get it from the
|
|
|
|
* returned HeapTuple.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HEAPDEBUGALL
|
|
|
|
#define HEAPDEBUG_1 \
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(DEBUG2, "heap_getnext([%s,nkeys=%d],dir=%d) called", \
|
2002-05-21 01:51:44 +02:00
|
|
|
RelationGetRelationName(scan->rs_rd), scan->rs_nkeys, (int) direction)
|
1996-07-09 08:22:35 +02:00
|
|
|
#define HEAPDEBUG_2 \
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(DEBUG2, "heap_getnext returning EOS")
|
2001-06-09 20:16:59 +02:00
|
|
|
#define HEAPDEBUG_3 \
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(DEBUG2, "heap_getnext returning tuple")
|
1996-07-09 08:22:35 +02:00
|
|
|
#else
|
|
|
|
#define HEAPDEBUG_1
|
|
|
|
#define HEAPDEBUG_2
|
|
|
|
#define HEAPDEBUG_3
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* !defined(HEAPDEBUGALL) */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
HeapTuple
|
2002-05-21 01:51:44 +02:00
|
|
|
heap_getnext(HeapScanDesc scan, ScanDirection direction)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/* Note: no locking manipulations needed */
|
|
|
|
|
|
|
|
HEAPDEBUG_1; /* heap_getnext( info ) */
|
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
/*
|
|
|
|
* Note: we depend here on the -1/0/1 encoding of ScanDirection.
|
|
|
|
*/
|
|
|
|
heapgettup(scan->rs_rd,
|
|
|
|
(int) direction,
|
|
|
|
&(scan->rs_ctup),
|
|
|
|
&(scan->rs_cbuf),
|
|
|
|
scan->rs_snapshot,
|
|
|
|
scan->rs_nkeys,
|
2004-05-08 21:09:25 +02:00
|
|
|
scan->rs_key,
|
|
|
|
scan->rs_nblocks);
|
2002-05-21 01:51:44 +02:00
|
|
|
|
|
|
|
if (scan->rs_ctup.t_data == NULL && !BufferIsValid(scan->rs_cbuf))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
HEAPDEBUG_2; /* heap_getnext returning EOS */
|
|
|
|
return NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
pgstat_count_heap_scan(&scan->rs_pgstat_info);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* if we get here it means we have a new current scan tuple, so point
|
|
|
|
* to the proper return buffer and return the tuple.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
HEAPDEBUG_3; /* heap_getnext returning tuple */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
if (scan->rs_ctup.t_data != NULL)
|
|
|
|
pgstat_count_heap_getnext(&scan->rs_pgstat_info);
|
|
|
|
|
1998-11-27 20:52:36 +01:00
|
|
|
return ((scan->rs_ctup.t_data == NULL) ? NULL : &(scan->rs_ctup));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
/*
|
|
|
|
* heap_fetch - retrieve tuple with given tid
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-05-24 20:57:57 +02:00
|
|
|
* On entry, tuple->t_self is the TID to fetch. We pin the buffer holding
|
|
|
|
* the tuple, fill in the remaining fields of *tuple, and check the tuple
|
|
|
|
* against the specified snapshot.
|
1998-08-19 04:04:17 +02:00
|
|
|
*
|
2002-05-24 21:52:43 +02:00
|
|
|
* If successful (tuple found and passes snapshot time qual), then *userbuf
|
|
|
|
* is set to the buffer holding the tuple and TRUE is returned. The caller
|
|
|
|
* must unpin the buffer when done with the tuple.
|
2002-05-21 01:51:44 +02:00
|
|
|
*
|
2004-10-26 18:05:03 +02:00
|
|
|
* If the tuple is not found (ie, item number references a deleted slot),
|
|
|
|
* then tuple->t_data is set to NULL and FALSE is returned.
|
2002-05-24 21:52:43 +02:00
|
|
|
*
|
2004-10-26 18:05:03 +02:00
|
|
|
* If the tuple is found but fails the time qual check, then FALSE is returned
|
|
|
|
* but tuple->t_data is left pointing to the tuple.
|
|
|
|
*
|
|
|
|
* keep_buf determines what is done with the buffer in the FALSE-result cases.
|
|
|
|
* When the caller specifies keep_buf = true, we retain the pin on the buffer
|
|
|
|
* and return it in *userbuf (so the caller must eventually unpin it); when
|
|
|
|
* keep_buf = false, the pin is released and *userbuf is set to InvalidBuffer.
|
2002-05-24 21:52:43 +02:00
|
|
|
*
|
2003-07-21 22:29:40 +02:00
|
|
|
* It is somewhat inconsistent that we ereport() on invalid block number but
|
2002-05-24 21:52:43 +02:00
|
|
|
* return false on invalid item number. This is historical. The only
|
|
|
|
* justification I can see is that the caller can relatively easily check the
|
|
|
|
* block number for validity, but cannot check the item number without reading
|
|
|
|
* the page himself.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-05-24 20:57:57 +02:00
|
|
|
bool
|
1996-07-09 08:22:35 +02:00
|
|
|
heap_fetch(Relation relation,
|
1998-07-27 21:38:40 +02:00
|
|
|
Snapshot snapshot,
|
1998-11-27 20:52:36 +01:00
|
|
|
HeapTuple tuple,
|
2001-06-22 21:16:24 +02:00
|
|
|
Buffer *userbuf,
|
2002-05-24 20:57:57 +02:00
|
|
|
bool keep_buf,
|
2002-05-21 01:51:44 +02:00
|
|
|
PgStat_Info *pgstat_info)
|
2004-04-21 20:24:26 +02:00
|
|
|
{
|
|
|
|
/* Assume *userbuf is undefined on entry */
|
|
|
|
*userbuf = InvalidBuffer;
|
|
|
|
return heap_release_fetch(relation, snapshot, tuple,
|
|
|
|
userbuf, keep_buf, pgstat_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* heap_release_fetch - retrieve tuple with given tid
|
|
|
|
*
|
|
|
|
* This has the same API as heap_fetch except that if *userbuf is not
|
|
|
|
* InvalidBuffer on entry, that buffer will be released before reading
|
|
|
|
* the new page. This saves a separate ReleaseBuffer step and hence
|
|
|
|
* one entry into the bufmgr when looping through multiple fetches.
|
2004-10-26 18:05:03 +02:00
|
|
|
* Also, if *userbuf is the same buffer that holds the target tuple,
|
|
|
|
* we avoid bufmgr manipulation altogether.
|
2004-04-21 20:24:26 +02:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
heap_release_fetch(Relation relation,
|
|
|
|
Snapshot snapshot,
|
|
|
|
HeapTuple tuple,
|
|
|
|
Buffer *userbuf,
|
|
|
|
bool keep_buf,
|
|
|
|
PgStat_Info *pgstat_info)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-05-24 20:57:57 +02:00
|
|
|
ItemPointer tid = &(tuple->t_self);
|
1999-05-25 18:15:34 +02:00
|
|
|
ItemId lp;
|
|
|
|
Buffer buffer;
|
|
|
|
PageHeader dp;
|
|
|
|
OffsetNumber offnum;
|
2002-05-24 20:57:57 +02:00
|
|
|
bool valid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-05-17 17:55:24 +02:00
|
|
|
* get the buffer from the relation descriptor. Note that this does a
|
2004-04-21 20:24:26 +02:00
|
|
|
* buffer pin, and releases the old *userbuf if not InvalidBuffer.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-04-21 20:24:26 +02:00
|
|
|
buffer = ReleaseAndReadBuffer(*userbuf, relation,
|
|
|
|
ItemPointerGetBlockNumber(tid));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
/*
|
|
|
|
* Need share lock on buffer to examine tuple commit status.
|
|
|
|
*/
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2005-03-28 01:53:05 +02:00
|
|
|
dp = (PageHeader) BufferGetPage(buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-03-28 01:53:05 +02:00
|
|
|
* We'd better check for out-of-range offnum in case of VACUUM since
|
|
|
|
* the TID was obtained.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
offnum = ItemPointerGetOffsetNumber(tid);
|
2005-03-28 01:53:05 +02:00
|
|
|
if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(dp))
|
|
|
|
{
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
if (keep_buf)
|
|
|
|
*userbuf = buffer;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
*userbuf = InvalidBuffer;
|
|
|
|
}
|
|
|
|
tuple->t_datamcxt = NULL;
|
|
|
|
tuple->t_data = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the item line pointer corresponding to the requested tid
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
lp = PageGetItemId(dp, offnum);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-03-28 01:53:05 +02:00
|
|
|
* Must check for deleted tuple.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-11-23 21:07:06 +01:00
|
|
|
if (!ItemIdIsUsed(lp))
|
|
|
|
{
|
2000-07-04 03:39:24 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2004-10-26 18:05:03 +02:00
|
|
|
if (keep_buf)
|
|
|
|
*userbuf = buffer;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
*userbuf = InvalidBuffer;
|
|
|
|
}
|
2002-05-24 21:52:43 +02:00
|
|
|
tuple->t_datamcxt = NULL;
|
|
|
|
tuple->t_data = NULL;
|
|
|
|
return false;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
/*
|
|
|
|
* fill in *tuple fields
|
|
|
|
*/
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
|
|
|
|
tuple->t_len = ItemIdGetLength(lp);
|
2000-11-14 22:04:32 +01:00
|
|
|
tuple->t_tableOid = relation->rd_id;
|
1998-11-27 20:52:36 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-05-24 20:57:57 +02:00
|
|
|
* check time qualification of tuple, then release lock
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-11-27 20:52:36 +01:00
|
|
|
HeapTupleSatisfies(tuple, relation, buffer, dp,
|
2004-01-07 19:56:30 +01:00
|
|
|
snapshot, 0, NULL, valid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
if (valid)
|
1999-09-24 02:25:33 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* All checks passed, so return the tuple as valid. Caller is now
|
|
|
|
* responsible for releasing the buffer.
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
|
|
|
*userbuf = buffer;
|
2001-06-22 21:16:24 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Count the successful fetch in *pgstat_info if given, otherwise
|
|
|
|
* in the relation's default statistics area.
|
2002-05-21 01:51:44 +02:00
|
|
|
*/
|
|
|
|
if (pgstat_info != NULL)
|
|
|
|
pgstat_count_heap_fetch(pgstat_info);
|
2001-06-22 21:16:24 +02:00
|
|
|
else
|
|
|
|
pgstat_count_heap_fetch(&relation->pgstat_info);
|
2002-05-24 20:57:57 +02:00
|
|
|
|
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2002-05-24 20:57:57 +02:00
|
|
|
|
|
|
|
/* Tuple failed time qual, but maybe caller wants to see it anyway. */
|
|
|
|
if (keep_buf)
|
|
|
|
*userbuf = buffer;
|
2004-10-26 18:05:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
*userbuf = InvalidBuffer;
|
2002-05-24 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
/*
|
1999-10-11 08:28:29 +02:00
|
|
|
* heap_get_latest_tid - get the latest tid of a specified tuple
|
|
|
|
*/
|
|
|
|
ItemPointer
|
|
|
|
heap_get_latest_tid(Relation relation,
|
2000-04-12 19:17:23 +02:00
|
|
|
Snapshot snapshot,
|
|
|
|
ItemPointer tid)
|
1999-10-11 08:28:29 +02:00
|
|
|
{
|
1999-10-31 01:10:21 +02:00
|
|
|
ItemId lp = NULL;
|
1999-10-11 08:28:29 +02:00
|
|
|
Buffer buffer;
|
|
|
|
PageHeader dp;
|
2000-04-12 19:17:23 +02:00
|
|
|
OffsetNumber offnum;
|
|
|
|
HeapTupleData tp;
|
|
|
|
HeapTupleHeader t_data;
|
|
|
|
ItemPointerData ctid;
|
|
|
|
bool invalidBlock,
|
2002-05-24 20:57:57 +02:00
|
|
|
linkend,
|
|
|
|
valid;
|
1999-10-11 08:28:29 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get the buffer from the relation descriptor Note that this does a
|
|
|
|
* buffer pin.
|
1999-10-11 08:28:29 +02:00
|
|
|
*/
|
|
|
|
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get the item line pointer corresponding to the requested tid
|
1999-10-11 08:28:29 +02:00
|
|
|
*/
|
|
|
|
dp = (PageHeader) BufferGetPage(buffer);
|
|
|
|
offnum = ItemPointerGetOffsetNumber(tid);
|
|
|
|
invalidBlock = true;
|
|
|
|
if (!PageIsNew(dp))
|
|
|
|
{
|
|
|
|
lp = PageGetItemId(dp, offnum);
|
|
|
|
if (ItemIdIsUsed(lp))
|
|
|
|
invalidBlock = false;
|
|
|
|
}
|
|
|
|
if (invalidBlock)
|
2000-04-12 19:17:23 +02:00
|
|
|
{
|
1999-10-11 08:28:29 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
return NULL;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
1999-10-11 08:28:29 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* more sanity checks
|
1999-10-11 08:28:29 +02:00
|
|
|
*/
|
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
tp.t_datamcxt = NULL;
|
1999-10-11 08:28:29 +02:00
|
|
|
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;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* check time qualification of tid
|
1999-10-11 08:28:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
HeapTupleSatisfies(&tp, relation, buffer, dp,
|
2004-01-07 19:56:30 +01:00
|
|
|
snapshot, 0, NULL, valid);
|
1999-10-11 08:28:29 +02:00
|
|
|
|
|
|
|
linkend = true;
|
2002-05-01 03:23:37 +02:00
|
|
|
if ((t_data->t_infomask & HEAP_XMIN_COMMITTED) != 0 &&
|
1999-10-11 08:28:29 +02:00
|
|
|
!ItemPointerEquals(tid, &ctid))
|
|
|
|
linkend = false;
|
|
|
|
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
if (!valid)
|
1999-10-11 08:28:29 +02:00
|
|
|
{
|
|
|
|
if (linkend)
|
|
|
|
return NULL;
|
2001-07-06 11:41:36 +02:00
|
|
|
heap_get_latest_tid(relation, snapshot, &ctid);
|
|
|
|
*tid = ctid;
|
1999-10-11 08:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tid;
|
|
|
|
}
|
|
|
|
|
2002-05-22 00:05:55 +02:00
|
|
|
/*
|
|
|
|
* heap_insert - insert tuple into a heap
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-05-22 00:05:55 +02:00
|
|
|
* The new tuple is stamped with current transaction ID and the specified
|
|
|
|
* command ID.
|
2005-06-20 20:37:02 +02:00
|
|
|
*
|
|
|
|
* If use_wal is false, the new tuple is not logged in WAL, even for a
|
|
|
|
* non-temp relation. Safe usage of this behavior requires that we arrange
|
|
|
|
* that all new tuples go into new pages not containing any tuples from other
|
|
|
|
* transactions, that the relation gets fsync'd before commit, and that the
|
|
|
|
* transaction emits at least one WAL record to ensure RecordTransactionCommit
|
|
|
|
* will decide to WAL-log the commit.
|
|
|
|
*
|
|
|
|
* use_fsm is passed directly to RelationGetBufferForTuple, which see for
|
|
|
|
* more info.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
Oid
|
2005-06-20 20:37:02 +02:00
|
|
|
heap_insert(Relation relation, HeapTuple tup, CommandId cid,
|
|
|
|
bool use_wal, bool use_fsm)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-09-16 18:58:44 +02:00
|
|
|
TransactionId xid = GetCurrentTransactionId();
|
2001-03-22 05:01:46 +01:00
|
|
|
Buffer buffer;
|
2000-07-03 04:54:21 +02:00
|
|
|
|
2001-08-10 20:57:42 +02:00
|
|
|
if (relation->rd_rel->relhasoids)
|
|
|
|
{
|
2002-09-02 03:05:06 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
/* this is redundant with an Assert in HeapTupleSetOid */
|
|
|
|
Assert(tup->t_data->t_infomask & HEAP_HASOID);
|
|
|
|
#endif
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2001-08-10 20:57:42 +02:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* If the object id of this tuple has already been assigned, trust
|
|
|
|
* the caller. There are a couple of ways this can happen. At
|
|
|
|
* initial db creation, the backend program sets oids for tuples.
|
|
|
|
* When we define an index, we set the oid. Finally, in the
|
|
|
|
* future, we may allow users to set their own object ids in order
|
|
|
|
* to support a persistent object store (objects need to contain
|
|
|
|
* pointers to one another).
|
2001-08-10 20:57:42 +02:00
|
|
|
*/
|
2002-07-20 07:16:59 +02:00
|
|
|
if (!OidIsValid(HeapTupleGetOid(tup)))
|
2005-08-12 03:36:05 +02:00
|
|
|
HeapTupleSetOid(tup, GetNewOid(relation));
|
2001-08-10 20:57:42 +02:00
|
|
|
}
|
2002-09-02 03:05:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check there is not space for an OID */
|
|
|
|
Assert(!(tup->t_data->t_infomask & HEAP_HASOID));
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-07-30 18:08:33 +02:00
|
|
|
tup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
|
2002-09-02 03:05:06 +02:00
|
|
|
tup->t_data->t_infomask |= HEAP_XMAX_INVALID;
|
2004-09-16 18:58:44 +02:00
|
|
|
HeapTupleHeaderSetXmin(tup->t_data, xid);
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetCmin(tup->t_data, cid);
|
2004-09-17 20:09:55 +02:00
|
|
|
HeapTupleHeaderSetXmax(tup->t_data, 0); /* zero out Datum fields */
|
|
|
|
HeapTupleHeaderSetCmax(tup->t_data, 0); /* for cleanliness */
|
2000-11-14 22:04:32 +01:00
|
|
|
tup->t_tableOid = relation->rd_id;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* If the new tuple is too big for storage or contains already toasted
|
2004-08-29 07:07:03 +02:00
|
|
|
* out-of-line attributes from some other relation, invoke the
|
|
|
|
* toaster.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2004-01-16 21:51:30 +01:00
|
|
|
if (HeapTupleHasExternal(tup) ||
|
2000-08-04 06:16:17 +02:00
|
|
|
(MAXALIGN(tup->t_len) > TOAST_TUPLE_THRESHOLD))
|
2000-07-04 01:10:14 +02:00
|
|
|
heap_tuple_toast_attrs(relation, tup, NULL);
|
|
|
|
|
2001-06-29 23:08:25 +02:00
|
|
|
/* Find buffer to insert this tuple into */
|
2005-06-20 20:37:02 +02:00
|
|
|
buffer = RelationGetBufferForTuple(relation, tup->t_len,
|
|
|
|
InvalidBuffer, use_fsm);
|
2000-07-03 04:54:21 +02:00
|
|
|
|
2003-07-21 22:29:40 +02:00
|
|
|
/* NO EREPORT(ERROR) from here till changes are logged */
|
2001-01-12 22:54:01 +01:00
|
|
|
START_CRIT_SECTION();
|
2002-08-13 22:11:03 +02:00
|
|
|
|
2000-07-03 04:54:21 +02:00
|
|
|
RelationPutHeapTuple(relation, buffer, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
pgstat_count_heap_insert(&relation->pgstat_info);
|
|
|
|
|
2000-06-02 12:20:27 +02:00
|
|
|
/* XLOG stuff */
|
2005-06-20 20:37:02 +02:00
|
|
|
if (relation->rd_istemp)
|
|
|
|
{
|
|
|
|
/* No XLOG record, but still need to flag that XID exists on disk */
|
|
|
|
MyXactMadeTempRelUpdate = true;
|
|
|
|
}
|
|
|
|
else if (use_wal)
|
2000-06-02 12:20:27 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_insert xlrec;
|
|
|
|
xl_heap_header xlhdr;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogRecData rdata[3];
|
|
|
|
Page page = BufferGetPage(buffer);
|
|
|
|
uint8 info = XLOG_HEAP_INSERT;
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
xlrec.target.node = relation->rd_node;
|
|
|
|
xlrec.target.tid = tup->t_self;
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].len = SizeOfHeapInsert;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
|
|
|
xlhdr.t_natts = tup->t_data->t_natts;
|
2002-09-27 00:46:29 +02:00
|
|
|
xlhdr.t_infomask = tup->t_data->t_infomask;
|
2000-12-28 14:00:29 +01:00
|
|
|
xlhdr.t_hoff = tup->t_data->t_hoff;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-09-27 00:46:29 +02:00
|
|
|
/*
|
|
|
|
* note we mark rdata[1] as belonging to buffer; if XLogInsert
|
|
|
|
* decides to write the whole page to the xlog, we don't need to
|
|
|
|
* store xl_heap_header in the xlog.
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[1].data = (char *) &xlhdr;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[1].len = SizeOfHeapHeader;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = buffer;
|
|
|
|
rdata[1].buffer_std = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[1].next = &(rdata[2]);
|
|
|
|
|
2002-07-20 07:16:59 +02:00
|
|
|
/* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[2].data = (char *) tup->t_data + offsetof(HeapTupleHeaderData, t_bits);
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[2].len = tup->t_len - offsetof(HeapTupleHeaderData, t_bits);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[2].buffer = buffer;
|
|
|
|
rdata[2].buffer_std = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[2].next = NULL;
|
|
|
|
|
2002-09-27 00:46:29 +02:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* If this is the single and first tuple on page, we can reinit
|
|
|
|
* the page instead of restoring the whole thing. Set flag, and
|
|
|
|
* hide buffer references from XLogInsert.
|
2002-09-27 00:46:29 +02:00
|
|
|
*/
|
2000-12-28 14:00:29 +01:00
|
|
|
if (ItemPointerGetOffsetNumber(&(tup->t_self)) == FirstOffsetNumber &&
|
|
|
|
PageGetMaxOffsetNumber(page) == FirstOffsetNumber)
|
|
|
|
{
|
|
|
|
info |= XLOG_HEAP_INIT_PAGE;
|
|
|
|
rdata[1].buffer = rdata[2].buffer = InvalidBuffer;
|
|
|
|
}
|
2000-06-02 12:20:27 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
recptr = XLogInsert(RM_HEAP_ID, info, rdata);
|
|
|
|
|
|
|
|
PageSetLSN(page, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2000-06-02 12:20:27 +02:00
|
|
|
}
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
END_CRIT_SECTION();
|
2000-06-02 12:20:27 +02:00
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2000-07-04 03:39:24 +02:00
|
|
|
WriteBuffer(buffer);
|
2000-07-03 04:54:21 +02:00
|
|
|
|
2001-01-07 23:14:31 +01:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If tuple is cachable, mark it for invalidation from the caches in
|
|
|
|
* case we abort. Note it is OK to do this after WriteBuffer releases
|
|
|
|
* the buffer, because the "tup" data structure is all in local
|
|
|
|
* memory, not in the shared buffer.
|
2001-01-07 23:14:31 +01:00
|
|
|
*/
|
2002-03-03 18:47:56 +01:00
|
|
|
CacheInvalidateHeapTuple(relation, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-07-20 07:16:59 +02:00
|
|
|
return HeapTupleGetOid(tup);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-05-22 00:05:55 +02:00
|
|
|
/*
|
|
|
|
* simple_heap_insert - insert a tuple
|
|
|
|
*
|
|
|
|
* Currently, this routine differs from heap_insert only in supplying
|
|
|
|
* a default command ID. But it should be used rather than using
|
|
|
|
* heap_insert directly in most places where we are modifying system catalogs.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
simple_heap_insert(Relation relation, HeapTuple tup)
|
|
|
|
{
|
2005-06-20 20:37:02 +02:00
|
|
|
return heap_insert(relation, tup, GetCurrentCommandId(), true, true);
|
2002-05-22 00:05:55 +02:00
|
|
|
}
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
/*
|
|
|
|
* heap_delete - delete a tuple
|
2001-01-23 05:32:23 +01:00
|
|
|
*
|
|
|
|
* NB: do not call this directly unless you are prepared to deal with
|
|
|
|
* concurrent-update conditions. Use simple_heap_delete instead.
|
2003-09-16 01:33:43 +02:00
|
|
|
*
|
2003-10-01 23:30:53 +02:00
|
|
|
* relation - table to be modified
|
|
|
|
* tid - TID of tuple to be deleted
|
|
|
|
* ctid - output parameter, used only for failure case (see below)
|
|
|
|
* cid - delete command ID to use in verifying tuple visibility
|
2004-09-11 20:28:34 +02:00
|
|
|
* crosscheck - if not InvalidSnapshot, also check tuple against this
|
2003-10-01 23:30:53 +02:00
|
|
|
* wait - true if should wait for any conflicting update to commit/abort
|
|
|
|
*
|
2003-09-16 01:33:43 +02:00
|
|
|
* Normal, successful return value is HeapTupleMayBeUpdated, which
|
|
|
|
* actually means we did delete it. Failure return codes are
|
|
|
|
* HeapTupleSelfUpdated, HeapTupleUpdated, or HeapTupleBeingUpdated
|
2003-10-01 23:30:53 +02:00
|
|
|
* (the last only possible if wait == false). On a failure return,
|
|
|
|
* *ctid is set to the ctid link of the target tuple (possibly a later
|
|
|
|
* version of the row).
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2005-03-21 00:40:34 +01:00
|
|
|
HTSU_Result
|
2002-05-22 00:05:55 +02:00
|
|
|
heap_delete(Relation relation, ItemPointer tid,
|
2004-09-11 20:28:34 +02:00
|
|
|
ItemPointer ctid, CommandId cid,
|
|
|
|
Snapshot crosscheck, bool wait)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-04-30 21:03:33 +02:00
|
|
|
HTSU_Result result;
|
2004-09-16 18:58:44 +02:00
|
|
|
TransactionId xid = GetCurrentTransactionId();
|
1999-05-25 18:15:34 +02:00
|
|
|
ItemId lp;
|
|
|
|
HeapTupleData tp;
|
|
|
|
PageHeader dp;
|
|
|
|
Buffer buffer;
|
2005-04-30 21:03:33 +02:00
|
|
|
bool have_tuple_lock = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(ItemPointerIsValid(tid));
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
dp = (PageHeader) BufferGetPage(buffer);
|
|
|
|
lp = PageGetItemId(dp, ItemPointerGetOffsetNumber(tid));
|
1999-12-16 23:20:03 +01:00
|
|
|
tp.t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
tp.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
|
|
|
|
tp.t_len = ItemIdGetLength(lp);
|
|
|
|
tp.t_self = *tid;
|
2000-11-14 22:04:32 +01:00
|
|
|
tp.t_tableOid = relation->rd_id;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
l1:
|
2004-10-16 00:40:29 +02:00
|
|
|
result = HeapTupleSatisfiesUpdate(tp.t_data, cid, buffer);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
if (result == HeapTupleInvisible)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "attempted to delete invisible tuple");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-09-16 01:33:43 +02:00
|
|
|
else if (result == HeapTupleBeingUpdated && wait)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-04-28 23:47:18 +02:00
|
|
|
TransactionId xwait;
|
|
|
|
uint16 infomask;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/* must copy state data before unlocking buffer */
|
|
|
|
xwait = HeapTupleHeaderGetXmax(tp.t_data);
|
|
|
|
infomask = tp.t_data->t_infomask;
|
|
|
|
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire tuple lock to establish our priority for the tuple
|
|
|
|
* (see heap_lock_tuple). LockTuple will release us when we are
|
|
|
|
* next-in-line for the tuple.
|
|
|
|
*
|
|
|
|
* If we are forced to "start over" below, we keep the tuple lock;
|
|
|
|
* this arranges that we stay at the head of the line while
|
|
|
|
* rechecking tuple state.
|
|
|
|
*/
|
|
|
|
if (!have_tuple_lock)
|
|
|
|
{
|
|
|
|
LockTuple(relation, &(tp.t_self), ExclusiveLock);
|
|
|
|
have_tuple_lock = true;
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* Sleep until concurrent transaction ends. Note that we don't care
|
|
|
|
* if the locker has an exclusive or shared lock, because we need
|
|
|
|
* exclusive.
|
1999-06-10 16:17:12 +02:00
|
|
|
*/
|
2005-04-28 23:47:18 +02:00
|
|
|
|
|
|
|
if (infomask & HEAP_XMAX_IS_MULTI)
|
1998-12-15 13:47:01 +01:00
|
|
|
{
|
2005-04-28 23:47:18 +02:00
|
|
|
/* wait for multixact */
|
|
|
|
MultiXactIdWait((MultiXactId) xwait);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If xwait had just locked the tuple then some other xact could
|
|
|
|
* update this tuple before we get to this point. Check for xmax
|
|
|
|
* change, and start over if so.
|
|
|
|
*/
|
|
|
|
if (!(tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
|
|
|
|
!TransactionIdEquals(HeapTupleHeaderGetXmax(tp.t_data),
|
|
|
|
xwait))
|
|
|
|
goto l1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You might think the multixact is necessarily done here, but
|
|
|
|
* not so: it could have surviving members, namely our own xact
|
|
|
|
* or other subxacts of this backend. It is legal for us to
|
|
|
|
* delete the tuple in either case, however (the latter case is
|
|
|
|
* essentially a situation of upgrading our former shared lock
|
|
|
|
* to exclusive). We don't bother changing the on-disk hint bits
|
|
|
|
* since we are about to overwrite the xmax altogether.
|
|
|
|
*/
|
1998-12-15 13:47:01 +01:00
|
|
|
}
|
2005-04-28 23:47:18 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* wait for regular transaction to end */
|
|
|
|
XactLockTableWait(xwait);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xwait is done, but if xwait had just locked the tuple then some
|
|
|
|
* other xact could update this tuple before we get to this point.
|
|
|
|
* Check for xmax change, and start over if so.
|
|
|
|
*/
|
|
|
|
if ((tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
|
|
|
|
!TransactionIdEquals(HeapTupleHeaderGetXmax(tp.t_data),
|
|
|
|
xwait))
|
|
|
|
goto l1;
|
|
|
|
|
|
|
|
/* Otherwise we can mark it committed or aborted */
|
|
|
|
if (!(tp.t_data->t_infomask & (HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID)))
|
|
|
|
{
|
|
|
|
if (TransactionIdDidCommit(xwait))
|
|
|
|
tp.t_data->t_infomask |= HEAP_XMAX_COMMITTED;
|
|
|
|
else
|
|
|
|
tp.t_data->t_infomask |= HEAP_XMAX_INVALID;
|
|
|
|
SetBufferCommitInfoNeedsSave(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We may overwrite if previous xmax aborted, or if it committed
|
|
|
|
* but only locked the tuple without updating it.
|
|
|
|
*/
|
|
|
|
if (tp.t_data->t_infomask & (HEAP_XMAX_INVALID |
|
|
|
|
HEAP_IS_LOCKED))
|
1998-12-15 13:47:01 +01:00
|
|
|
result = HeapTupleMayBeUpdated;
|
|
|
|
else
|
|
|
|
result = HeapTupleUpdated;
|
|
|
|
}
|
2003-10-01 23:30:53 +02:00
|
|
|
|
2004-09-11 20:28:34 +02:00
|
|
|
if (crosscheck != InvalidSnapshot && result == HeapTupleMayBeUpdated)
|
2003-10-01 23:30:53 +02:00
|
|
|
{
|
|
|
|
/* Perform additional check for serializable RI updates */
|
2004-10-16 00:40:29 +02:00
|
|
|
if (!HeapTupleSatisfiesSnapshot(tp.t_data, crosscheck, buffer))
|
2003-10-01 23:30:53 +02:00
|
|
|
result = HeapTupleUpdated;
|
|
|
|
}
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
if (result != HeapTupleMayBeUpdated)
|
|
|
|
{
|
2003-09-16 01:33:43 +02:00
|
|
|
Assert(result == HeapTupleSelfUpdated ||
|
|
|
|
result == HeapTupleUpdated ||
|
|
|
|
result == HeapTupleBeingUpdated);
|
2001-01-23 05:32:23 +01:00
|
|
|
*ctid = tp.t_data->t_ctid;
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
2005-04-30 21:03:33 +02:00
|
|
|
if (have_tuple_lock)
|
|
|
|
UnlockTuple(relation, &(tp.t_self), ExclusiveLock);
|
1998-12-15 13:47:01 +01:00
|
|
|
return result;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
START_CRIT_SECTION();
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
/* store transaction information of xact deleting the tuple */
|
|
|
|
tp.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
|
2002-09-02 03:05:06 +02:00
|
|
|
HEAP_XMAX_INVALID |
|
2005-04-28 23:47:18 +02:00
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
2002-09-02 03:05:06 +02:00
|
|
|
HEAP_MOVED);
|
2004-09-16 18:58:44 +02:00
|
|
|
HeapTupleHeaderSetXmax(tp.t_data, xid);
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetCmax(tp.t_data, cid);
|
2002-08-13 22:11:03 +02:00
|
|
|
/* Make sure there is no forward chain link in t_ctid */
|
|
|
|
tp.t_data->t_ctid = tp.t_self;
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
/* XLOG stuff */
|
2002-08-06 04:36:35 +02:00
|
|
|
if (!relation->rd_istemp)
|
2000-06-02 12:20:27 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_delete xlrec;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogRecData rdata[2];
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
xlrec.target.node = relation->rd_node;
|
|
|
|
xlrec.target.tid = tp.t_self;
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].len = SizeOfHeapDelete;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
|
|
|
rdata[1].data = NULL;
|
|
|
|
rdata[1].len = 0;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = buffer;
|
|
|
|
rdata[1].buffer_std = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[1].next = NULL;
|
|
|
|
|
|
|
|
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE, rdata);
|
2000-06-02 12:20:27 +02:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
PageSetLSN(dp, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(dp, ThisTimeLineID);
|
2000-06-02 12:20:27 +02:00
|
|
|
}
|
2002-08-06 04:36:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No XLOG record, but still need to flag that XID exists on disk */
|
|
|
|
MyXactMadeTempRelUpdate = true;
|
|
|
|
}
|
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
END_CRIT_SECTION();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-01-15 06:29:19 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-01-16 21:51:30 +01:00
|
|
|
* If the tuple has toasted out-of-line attributes, we need to delete
|
2004-08-29 07:07:03 +02:00
|
|
|
* those items too. We have to do this before WriteBuffer because we
|
|
|
|
* need to look at the contents of the tuple, but it's OK to release
|
|
|
|
* the context lock on the buffer first.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2004-01-16 21:51:30 +01:00
|
|
|
if (HeapTupleHasExternal(&tp))
|
|
|
|
heap_tuple_toast_attrs(relation, NULL, &tp);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
pgstat_count_heap_delete(&relation->pgstat_info);
|
|
|
|
|
2001-01-07 23:14:31 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Mark tuple for invalidation from system caches at next command
|
|
|
|
* boundary. We have to do this before WriteBuffer because we need to
|
|
|
|
* look at the contents of the tuple, so we need to hold our refcount
|
|
|
|
* on the buffer.
|
2001-01-07 23:14:31 +01:00
|
|
|
*/
|
2002-03-03 18:47:56 +01:00
|
|
|
CacheInvalidateHeapTuple(relation, &tp);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
WriteBuffer(buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/*
|
|
|
|
* Release the lmgr tuple lock, if we had it.
|
|
|
|
*/
|
|
|
|
if (have_tuple_lock)
|
|
|
|
UnlockTuple(relation, &(tp.t_self), ExclusiveLock);
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
return HeapTupleMayBeUpdated;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-01-23 05:32:23 +01:00
|
|
|
/*
|
|
|
|
* simple_heap_delete - delete a tuple
|
|
|
|
*
|
|
|
|
* This routine may be used to delete a tuple when concurrent updates of
|
|
|
|
* the target tuple are not expected (for example, because we have a lock
|
2001-03-22 05:01:46 +01:00
|
|
|
* on the relation associated with the tuple). Any failure is reported
|
2003-07-21 22:29:40 +02:00
|
|
|
* via ereport().
|
2001-01-23 05:32:23 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
simple_heap_delete(Relation relation, ItemPointer tid)
|
|
|
|
{
|
|
|
|
ItemPointerData ctid;
|
2005-03-21 00:40:34 +01:00
|
|
|
HTSU_Result result;
|
2001-01-23 05:32:23 +01:00
|
|
|
|
2003-09-16 01:33:43 +02:00
|
|
|
result = heap_delete(relation, tid,
|
|
|
|
&ctid,
|
2004-09-11 20:28:34 +02:00
|
|
|
GetCurrentCommandId(), InvalidSnapshot,
|
2004-08-29 07:07:03 +02:00
|
|
|
true /* wait for commit */ );
|
2001-01-23 05:32:23 +01:00
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case HeapTupleSelfUpdated:
|
|
|
|
/* Tuple was already updated in current command? */
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "tuple already updated by self");
|
2001-01-23 05:32:23 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HeapTupleMayBeUpdated:
|
|
|
|
/* done successfully */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HeapTupleUpdated:
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "tuple concurrently updated");
|
2001-01-23 05:32:23 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "unrecognized heap_delete status: %u", result);
|
2001-01-23 05:32:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* heap_update - replace a tuple
|
2001-01-23 05:32:23 +01:00
|
|
|
*
|
|
|
|
* NB: do not call this directly unless you are prepared to deal with
|
|
|
|
* concurrent-update conditions. Use simple_heap_update instead.
|
2003-09-16 01:33:43 +02:00
|
|
|
*
|
2003-10-01 23:30:53 +02:00
|
|
|
* relation - table to be modified
|
|
|
|
* otid - TID of old tuple to be replaced
|
|
|
|
* newtup - newly constructed tuple data to store
|
|
|
|
* ctid - output parameter, used only for failure case (see below)
|
|
|
|
* cid - update command ID to use in verifying old tuple visibility
|
2004-09-11 20:28:34 +02:00
|
|
|
* crosscheck - if not InvalidSnapshot, also check old tuple against this
|
2003-10-01 23:30:53 +02:00
|
|
|
* wait - true if should wait for any conflicting update to commit/abort
|
|
|
|
*
|
2003-09-16 01:33:43 +02:00
|
|
|
* Normal, successful return value is HeapTupleMayBeUpdated, which
|
|
|
|
* actually means we *did* update it. Failure return codes are
|
|
|
|
* HeapTupleSelfUpdated, HeapTupleUpdated, or HeapTupleBeingUpdated
|
2003-10-01 23:30:53 +02:00
|
|
|
* (the last only possible if wait == false). On a failure return,
|
|
|
|
* *ctid is set to the ctid link of the old tuple (possibly a later
|
|
|
|
* version of the row).
|
|
|
|
* On success, newtup->t_self is set to the TID where the new tuple
|
|
|
|
* was inserted.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2005-03-21 00:40:34 +01:00
|
|
|
HTSU_Result
|
1999-11-24 01:44:37 +01:00
|
|
|
heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
|
2004-09-11 20:28:34 +02:00
|
|
|
ItemPointer ctid, CommandId cid,
|
|
|
|
Snapshot crosscheck, bool wait)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-04-30 21:03:33 +02:00
|
|
|
HTSU_Result result;
|
2004-09-16 18:58:44 +02:00
|
|
|
TransactionId xid = GetCurrentTransactionId();
|
1999-05-25 18:15:34 +02:00
|
|
|
ItemId lp;
|
|
|
|
HeapTupleData oldtup;
|
|
|
|
PageHeader dp;
|
2001-01-15 06:29:19 +01:00
|
|
|
Buffer buffer,
|
|
|
|
newbuf;
|
|
|
|
bool need_toast,
|
|
|
|
already_marked;
|
2001-05-17 00:35:12 +02:00
|
|
|
Size newtupsize,
|
|
|
|
pagefree;
|
2005-04-30 21:03:33 +02:00
|
|
|
bool have_tuple_lock = false;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(ItemPointerIsValid(otid));
|
|
|
|
|
|
|
|
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(otid));
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
dp = (PageHeader) BufferGetPage(buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
lp = PageGetItemId(dp, ItemPointerGetOffsetNumber(otid));
|
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
oldtup.t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
oldtup.t_data = (HeapTupleHeader) PageGetItem(dp, lp);
|
|
|
|
oldtup.t_len = ItemIdGetLength(lp);
|
|
|
|
oldtup.t_self = *otid;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2001-01-07 23:14:31 +01:00
|
|
|
/*
|
|
|
|
* Note: beyond this point, use oldtup not otid to refer to old tuple.
|
|
|
|
* otid may very well point at newtup->t_self, which we will overwrite
|
|
|
|
* with the new tuple's location, so there's great risk of confusion
|
|
|
|
* if we use otid anymore.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
l2:
|
2004-10-16 00:40:29 +02:00
|
|
|
result = HeapTupleSatisfiesUpdate(oldtup.t_data, cid, buffer);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
if (result == HeapTupleInvisible)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
1997-09-07 07:04:48 +02:00
|
|
|
ReleaseBuffer(buffer);
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "attempted to update invisible tuple");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-09-16 01:33:43 +02:00
|
|
|
else if (result == HeapTupleBeingUpdated && wait)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-04-28 23:47:18 +02:00
|
|
|
TransactionId xwait;
|
|
|
|
uint16 infomask;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/* must copy state data before unlocking buffer */
|
|
|
|
xwait = HeapTupleHeaderGetXmax(oldtup.t_data);
|
|
|
|
infomask = oldtup.t_data->t_infomask;
|
|
|
|
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire tuple lock to establish our priority for the tuple
|
|
|
|
* (see heap_lock_tuple). LockTuple will release us when we are
|
|
|
|
* next-in-line for the tuple.
|
|
|
|
*
|
|
|
|
* If we are forced to "start over" below, we keep the tuple lock;
|
|
|
|
* this arranges that we stay at the head of the line while
|
|
|
|
* rechecking tuple state.
|
|
|
|
*/
|
|
|
|
if (!have_tuple_lock)
|
|
|
|
{
|
|
|
|
LockTuple(relation, &(oldtup.t_self), ExclusiveLock);
|
|
|
|
have_tuple_lock = true;
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* Sleep until concurrent transaction ends. Note that we don't care
|
|
|
|
* if the locker has an exclusive or shared lock, because we need
|
|
|
|
* exclusive.
|
1999-06-10 16:17:12 +02:00
|
|
|
*/
|
2005-04-28 23:47:18 +02:00
|
|
|
|
|
|
|
if (infomask & HEAP_XMAX_IS_MULTI)
|
1998-12-15 13:47:01 +01:00
|
|
|
{
|
2005-04-28 23:47:18 +02:00
|
|
|
/* wait for multixact */
|
|
|
|
MultiXactIdWait((MultiXactId) xwait);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If xwait had just locked the tuple then some other xact could
|
|
|
|
* update this tuple before we get to this point. Check for xmax
|
|
|
|
* change, and start over if so.
|
|
|
|
*/
|
|
|
|
if (!(oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
|
|
|
|
!TransactionIdEquals(HeapTupleHeaderGetXmax(oldtup.t_data),
|
|
|
|
xwait))
|
|
|
|
goto l2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You might think the multixact is necessarily done here, but
|
|
|
|
* not so: it could have surviving members, namely our own xact
|
|
|
|
* or other subxacts of this backend. It is legal for us to
|
|
|
|
* update the tuple in either case, however (the latter case is
|
|
|
|
* essentially a situation of upgrading our former shared lock
|
|
|
|
* to exclusive). We don't bother changing the on-disk hint bits
|
|
|
|
* since we are about to overwrite the xmax altogether.
|
|
|
|
*/
|
1998-12-15 13:47:01 +01:00
|
|
|
}
|
2005-04-28 23:47:18 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* wait for regular transaction to end */
|
|
|
|
XactLockTableWait(xwait);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xwait is done, but if xwait had just locked the tuple then some
|
|
|
|
* other xact could update this tuple before we get to this point.
|
|
|
|
* Check for xmax change, and start over if so.
|
|
|
|
*/
|
|
|
|
if ((oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
|
|
|
|
!TransactionIdEquals(HeapTupleHeaderGetXmax(oldtup.t_data),
|
|
|
|
xwait))
|
|
|
|
goto l2;
|
|
|
|
|
|
|
|
/* Otherwise we can mark it committed or aborted */
|
|
|
|
if (!(oldtup.t_data->t_infomask & (HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID)))
|
|
|
|
{
|
|
|
|
if (TransactionIdDidCommit(xwait))
|
|
|
|
oldtup.t_data->t_infomask |= HEAP_XMAX_COMMITTED;
|
|
|
|
else
|
|
|
|
oldtup.t_data->t_infomask |= HEAP_XMAX_INVALID;
|
|
|
|
SetBufferCommitInfoNeedsSave(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We may overwrite if previous xmax aborted, or if it committed
|
|
|
|
* but only locked the tuple without updating it.
|
|
|
|
*/
|
|
|
|
if (oldtup.t_data->t_infomask & (HEAP_XMAX_INVALID |
|
|
|
|
HEAP_IS_LOCKED))
|
1998-12-15 13:47:01 +01:00
|
|
|
result = HeapTupleMayBeUpdated;
|
|
|
|
else
|
|
|
|
result = HeapTupleUpdated;
|
|
|
|
}
|
2003-10-01 23:30:53 +02:00
|
|
|
|
2004-09-11 20:28:34 +02:00
|
|
|
if (crosscheck != InvalidSnapshot && result == HeapTupleMayBeUpdated)
|
2003-10-01 23:30:53 +02:00
|
|
|
{
|
|
|
|
/* Perform additional check for serializable RI updates */
|
2004-10-16 00:40:29 +02:00
|
|
|
if (!HeapTupleSatisfiesSnapshot(oldtup.t_data, crosscheck, buffer))
|
2003-10-01 23:30:53 +02:00
|
|
|
result = HeapTupleUpdated;
|
|
|
|
}
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
if (result != HeapTupleMayBeUpdated)
|
|
|
|
{
|
2003-09-16 01:33:43 +02:00
|
|
|
Assert(result == HeapTupleSelfUpdated ||
|
|
|
|
result == HeapTupleUpdated ||
|
|
|
|
result == HeapTupleBeingUpdated);
|
2001-01-23 05:32:23 +01:00
|
|
|
*ctid = oldtup.t_data->t_ctid;
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
1997-09-07 07:04:48 +02:00
|
|
|
ReleaseBuffer(buffer);
|
2005-04-30 21:03:33 +02:00
|
|
|
if (have_tuple_lock)
|
|
|
|
UnlockTuple(relation, &(oldtup.t_self), ExclusiveLock);
|
1998-12-15 13:47:01 +01:00
|
|
|
return result;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-01-15 06:29:19 +01:00
|
|
|
/* Fill in OID and transaction status data for newtup */
|
2002-07-20 07:16:59 +02:00
|
|
|
if (relation->rd_rel->relhasoids)
|
|
|
|
{
|
2002-09-02 03:05:06 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
/* this is redundant with an Assert in HeapTupleSetOid */
|
|
|
|
Assert(newtup->t_data->t_infomask & HEAP_HASOID);
|
|
|
|
#endif
|
2002-07-20 07:16:59 +02:00
|
|
|
HeapTupleSetOid(newtup, HeapTupleGetOid(&oldtup));
|
|
|
|
}
|
2002-09-02 03:05:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check there is not space for an OID */
|
|
|
|
Assert(!(newtup->t_data->t_infomask & HEAP_HASOID));
|
|
|
|
}
|
|
|
|
|
1998-11-27 20:52:36 +01:00
|
|
|
newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
|
1999-03-28 22:32:42 +02:00
|
|
|
newtup->t_data->t_infomask |= (HEAP_XMAX_INVALID | HEAP_UPDATED);
|
2004-09-16 18:58:44 +02:00
|
|
|
HeapTupleHeaderSetXmin(newtup->t_data, xid);
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetCmin(newtup->t_data, cid);
|
2004-09-17 20:09:55 +02:00
|
|
|
HeapTupleHeaderSetXmax(newtup->t_data, 0); /* zero out Datum fields */
|
|
|
|
HeapTupleHeaderSetCmax(newtup->t_data, 0); /* for cleanliness */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-01-15 06:29:19 +01:00
|
|
|
/*
|
|
|
|
* If the toaster needs to be activated, OR if the new tuple will not
|
2001-03-22 05:01:46 +01:00
|
|
|
* fit on the same page as the old, then we need to release the
|
|
|
|
* context lock (but not the pin!) on the old tuple's buffer while we
|
|
|
|
* are off doing TOAST and/or table-file-extension work. We must mark
|
|
|
|
* the old tuple to show that it's already being updated, else other
|
2005-03-28 03:50:34 +02:00
|
|
|
* processes may try to update it themselves.
|
2001-01-15 06:29:19 +01:00
|
|
|
*
|
2004-01-16 21:51:30 +01:00
|
|
|
* We need to invoke the toaster if there are already any out-of-line
|
|
|
|
* toasted values present, or if the new tuple is over-threshold.
|
2000-07-04 19:11:40 +02:00
|
|
|
*/
|
2004-01-16 21:51:30 +01:00
|
|
|
need_toast = (HeapTupleHasExternal(&oldtup) ||
|
|
|
|
HeapTupleHasExternal(newtup) ||
|
2001-01-15 06:29:19 +01:00
|
|
|
(MAXALIGN(newtup->t_len) > TOAST_TUPLE_THRESHOLD));
|
2000-07-04 19:11:40 +02:00
|
|
|
|
2001-05-17 00:35:12 +02:00
|
|
|
newtupsize = MAXALIGN(newtup->t_len);
|
|
|
|
pagefree = PageGetFreeSpace((Page) dp);
|
|
|
|
|
|
|
|
if (need_toast || newtupsize > pagefree)
|
2000-09-07 11:58:38 +02:00
|
|
|
{
|
|
|
|
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
|
2001-01-15 06:29:19 +01:00
|
|
|
HEAP_XMAX_INVALID |
|
2005-04-28 23:47:18 +02:00
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
2002-09-02 03:05:06 +02:00
|
|
|
HEAP_MOVED);
|
2004-09-16 18:58:44 +02:00
|
|
|
HeapTupleHeaderSetXmax(oldtup.t_data, xid);
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetCmax(oldtup.t_data, cid);
|
2001-01-15 06:29:19 +01:00
|
|
|
already_marked = true;
|
2000-09-07 11:58:38 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2001-01-15 06:29:19 +01:00
|
|
|
|
|
|
|
/* Let the toaster do its thing */
|
|
|
|
if (need_toast)
|
2001-05-17 00:35:12 +02:00
|
|
|
{
|
2001-01-15 06:29:19 +01:00
|
|
|
heap_tuple_toast_attrs(relation, newtup, &oldtup);
|
2001-05-17 00:35:12 +02:00
|
|
|
newtupsize = MAXALIGN(newtup->t_len);
|
|
|
|
}
|
2001-01-15 06:29:19 +01:00
|
|
|
|
2001-05-17 00:35:12 +02:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Now, do we need a new page for the tuple, or not? This is a
|
|
|
|
* bit tricky since someone else could have added tuples to the
|
|
|
|
* page while we weren't looking. We have to recheck the
|
|
|
|
* available space after reacquiring the buffer lock. But don't
|
|
|
|
* bother to do that if the former amount of free space is still
|
|
|
|
* not enough; it's unlikely there's more free now than before.
|
2001-05-17 00:35:12 +02:00
|
|
|
*
|
|
|
|
* What's more, if we need to get a new page, we will need to acquire
|
2001-10-25 07:50:21 +02:00
|
|
|
* buffer locks on both old and new pages. To avoid deadlock
|
|
|
|
* against some other backend trying to get the same two locks in
|
|
|
|
* the other order, we must be consistent about the order we get
|
|
|
|
* the locks in. We use the rule "lock the lower-numbered page of
|
|
|
|
* the relation first". To implement this, we must do
|
|
|
|
* RelationGetBufferForTuple while not holding the lock on the old
|
|
|
|
* page, and we must rely on it to get the locks on both pages in
|
|
|
|
* the correct order.
|
2001-05-17 00:35:12 +02:00
|
|
|
*/
|
|
|
|
if (newtupsize > pagefree)
|
|
|
|
{
|
|
|
|
/* Assume there's no chance to put newtup on same page. */
|
|
|
|
newbuf = RelationGetBufferForTuple(relation, newtup->t_len,
|
2005-06-20 20:37:02 +02:00
|
|
|
buffer, true);
|
2001-05-17 00:35:12 +02:00
|
|
|
}
|
2001-01-15 06:29:19 +01:00
|
|
|
else
|
2001-05-17 00:35:12 +02:00
|
|
|
{
|
|
|
|
/* Re-acquire the lock on the old tuple's page. */
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
/* Re-check using the up-to-date free space */
|
|
|
|
pagefree = PageGetFreeSpace((Page) dp);
|
|
|
|
if (newtupsize > pagefree)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Rats, it doesn't fit anymore. We must now unlock and
|
2001-10-25 07:50:21 +02:00
|
|
|
* relock to avoid deadlock. Fortunately, this path
|
|
|
|
* should seldom be taken.
|
2001-05-17 00:35:12 +02:00
|
|
|
*/
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
newbuf = RelationGetBufferForTuple(relation, newtup->t_len,
|
2005-06-20 20:37:02 +02:00
|
|
|
buffer, true);
|
2001-05-17 00:35:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* OK, it fits here, so we're done. */
|
|
|
|
newbuf = buffer;
|
|
|
|
}
|
|
|
|
}
|
2000-09-07 11:58:38 +02:00
|
|
|
}
|
2001-01-15 06:29:19 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No TOAST work needed, and it'll fit on same page */
|
|
|
|
already_marked = false;
|
|
|
|
newbuf = buffer;
|
|
|
|
}
|
2000-07-03 04:54:21 +02:00
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
pgstat_count_heap_update(&relation->pgstat_info);
|
|
|
|
|
2001-05-17 00:35:12 +02:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* At this point newbuf and buffer are both pinned and locked, and
|
|
|
|
* newbuf has enough space for the new tuple. If they are the same
|
|
|
|
* buffer, only one pin is held.
|
2001-05-17 00:35:12 +02:00
|
|
|
*/
|
|
|
|
|
2003-07-21 22:29:40 +02:00
|
|
|
/* NO EREPORT(ERROR) from here till changes are logged */
|
2001-01-12 22:54:01 +01:00
|
|
|
START_CRIT_SECTION();
|
2000-07-03 04:54:21 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
RelationPutHeapTuple(relation, newbuf, newtup); /* insert new tuple */
|
2001-01-15 06:29:19 +01:00
|
|
|
|
2005-03-28 03:50:34 +02:00
|
|
|
if (!already_marked)
|
2000-09-07 11:58:38 +02:00
|
|
|
{
|
2001-01-15 06:29:19 +01:00
|
|
|
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID |
|
2005-04-28 23:47:18 +02:00
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
2002-09-02 03:05:06 +02:00
|
|
|
HEAP_MOVED);
|
2004-09-16 18:58:44 +02:00
|
|
|
HeapTupleHeaderSetXmax(oldtup.t_data, xid);
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetCmax(oldtup.t_data, cid);
|
2000-09-07 11:58:38 +02:00
|
|
|
}
|
1998-12-15 13:47:01 +01:00
|
|
|
|
2000-07-03 04:54:21 +02:00
|
|
|
/* record address of new tuple in t_ctid of old one */
|
|
|
|
oldtup.t_data->t_ctid = newtup->t_self;
|
|
|
|
|
|
|
|
/* XLOG stuff */
|
2002-08-06 04:36:35 +02:00
|
|
|
if (!relation->rd_istemp)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecPtr recptr = log_heap_update(relation, buffer, oldtup.t_self,
|
2001-01-15 06:29:19 +01:00
|
|
|
newbuf, newtup, false);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-07-03 04:54:21 +02:00
|
|
|
if (newbuf != buffer)
|
|
|
|
{
|
2000-09-07 11:58:38 +02:00
|
|
|
PageSetLSN(BufferGetPage(newbuf), recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(BufferGetPage(newbuf), ThisTimeLineID);
|
2000-07-03 04:54:21 +02:00
|
|
|
}
|
2000-09-07 11:58:38 +02:00
|
|
|
PageSetLSN(BufferGetPage(buffer), recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(BufferGetPage(buffer), ThisTimeLineID);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2002-08-06 04:36:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No XLOG record, but still need to flag that XID exists on disk */
|
|
|
|
MyXactMadeTempRelUpdate = true;
|
|
|
|
}
|
2001-01-15 06:29:19 +01:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
END_CRIT_SECTION();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-03 04:54:21 +02:00
|
|
|
if (newbuf != buffer)
|
|
|
|
LockBuffer(newbuf, BUFFER_LOCK_UNLOCK);
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
2001-01-07 23:14:31 +01:00
|
|
|
/*
|
|
|
|
* Mark old tuple for invalidation from system caches at next command
|
|
|
|
* boundary. We have to do this before WriteBuffer because we need to
|
|
|
|
* look at the contents of the tuple, so we need to hold our refcount.
|
|
|
|
*/
|
2002-03-03 18:47:56 +01:00
|
|
|
CacheInvalidateHeapTuple(relation, &oldtup);
|
2001-01-07 23:14:31 +01:00
|
|
|
|
|
|
|
if (newbuf != buffer)
|
|
|
|
WriteBuffer(newbuf);
|
|
|
|
WriteBuffer(buffer);
|
|
|
|
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If new tuple is cachable, mark it for invalidation from the caches
|
|
|
|
* in case we abort. Note it is OK to do this after WriteBuffer
|
|
|
|
* releases the buffer, because the "newtup" data structure is all in
|
|
|
|
* local memory, not in the shared buffer.
|
2001-01-07 23:14:31 +01:00
|
|
|
*/
|
2002-03-03 18:47:56 +01:00
|
|
|
CacheInvalidateHeapTuple(relation, newtup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/*
|
|
|
|
* Release the lmgr tuple lock, if we had it.
|
|
|
|
*/
|
|
|
|
if (have_tuple_lock)
|
|
|
|
UnlockTuple(relation, &(oldtup.t_self), ExclusiveLock);
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
return HeapTupleMayBeUpdated;
|
|
|
|
}
|
|
|
|
|
2001-01-23 05:32:23 +01:00
|
|
|
/*
|
|
|
|
* simple_heap_update - replace a tuple
|
|
|
|
*
|
|
|
|
* This routine may be used to update a tuple when concurrent updates of
|
|
|
|
* the target tuple are not expected (for example, because we have a lock
|
2001-03-22 05:01:46 +01:00
|
|
|
* on the relation associated with the tuple). Any failure is reported
|
2003-07-21 22:29:40 +02:00
|
|
|
* via ereport().
|
2001-01-23 05:32:23 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
|
|
|
|
{
|
|
|
|
ItemPointerData ctid;
|
2005-03-21 00:40:34 +01:00
|
|
|
HTSU_Result result;
|
2001-01-23 05:32:23 +01:00
|
|
|
|
2003-09-16 01:33:43 +02:00
|
|
|
result = heap_update(relation, otid, tup,
|
|
|
|
&ctid,
|
2004-09-11 20:28:34 +02:00
|
|
|
GetCurrentCommandId(), InvalidSnapshot,
|
2004-08-29 07:07:03 +02:00
|
|
|
true /* wait for commit */ );
|
2001-01-23 05:32:23 +01:00
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case HeapTupleSelfUpdated:
|
|
|
|
/* Tuple was already updated in current command? */
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "tuple already updated by self");
|
2001-01-23 05:32:23 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HeapTupleMayBeUpdated:
|
|
|
|
/* done successfully */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HeapTupleUpdated:
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "tuple concurrently updated");
|
2001-01-23 05:32:23 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "unrecognized heap_update status: %u", result);
|
2001-01-23 05:32:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* heap_lock_tuple - lock a tuple in shared or exclusive mode
|
2005-04-30 21:03:33 +02:00
|
|
|
*
|
|
|
|
* NOTES: because the shared-memory lock table is of finite size, but users
|
|
|
|
* could reasonably want to lock large numbers of tuples, we do not rely on
|
|
|
|
* the standard lock manager to store tuple-level locks over the long term.
|
|
|
|
* Instead, a tuple is marked as locked by setting the current transaction's
|
|
|
|
* XID as its XMAX, and setting additional infomask bits to distinguish this
|
|
|
|
* usage from the more normal case of having deleted the tuple. When
|
|
|
|
* multiple transactions concurrently share-lock a tuple, the first locker's
|
|
|
|
* XID is replaced in XMAX with a MultiTransactionId representing the set of
|
|
|
|
* XIDs currently holding share-locks.
|
|
|
|
*
|
|
|
|
* When it is necessary to wait for a tuple-level lock to be released, the
|
|
|
|
* basic delay is provided by XactLockTableWait or MultiXactIdWait on the
|
|
|
|
* contents of the tuple's XMAX. However, that mechanism will release all
|
|
|
|
* waiters concurrently, so there would be a race condition as to which
|
|
|
|
* waiter gets the tuple, potentially leading to indefinite starvation of
|
|
|
|
* some waiters. The possibility of share-locking makes the problem much
|
|
|
|
* worse --- a steady stream of share-lockers can easily block an exclusive
|
|
|
|
* locker forever. To provide more reliable semantics about who gets a
|
|
|
|
* tuple-level lock first, we use the standard lock manager. The protocol
|
|
|
|
* for waiting for a tuple-level lock is really
|
|
|
|
* LockTuple()
|
|
|
|
* XactLockTableWait()
|
|
|
|
* mark tuple as locked by me
|
|
|
|
* UnlockTuple()
|
|
|
|
* When there are multiple waiters, arbitration of who is to get the lock next
|
|
|
|
* is provided by LockTuple(). However, at most one tuple-level lock will
|
|
|
|
* be held or awaited per backend at any time, so we don't risk overflow
|
|
|
|
* of the lock table. Note that incoming share-lockers are required to
|
|
|
|
* do LockTuple as well, if there is any conflict, to ensure that they don't
|
|
|
|
* starve out waiting exclusive-lockers. However, if there is not any active
|
|
|
|
* conflict for a tuple, we don't incur any extra overhead.
|
1998-12-15 13:47:01 +01:00
|
|
|
*/
|
2005-03-21 00:40:34 +01:00
|
|
|
HTSU_Result
|
2005-04-28 23:47:18 +02:00
|
|
|
heap_lock_tuple(Relation relation, HeapTuple tuple, Buffer *buffer,
|
2005-08-01 22:31:16 +02:00
|
|
|
CommandId cid, LockTupleMode mode, bool nowait)
|
1998-12-15 13:47:01 +01:00
|
|
|
{
|
2005-04-30 21:03:33 +02:00
|
|
|
HTSU_Result result;
|
1999-05-25 18:15:34 +02:00
|
|
|
ItemPointer tid = &(tuple->t_self);
|
|
|
|
ItemId lp;
|
|
|
|
PageHeader dp;
|
2005-04-30 21:03:33 +02:00
|
|
|
TransactionId xid;
|
2005-04-28 23:47:18 +02:00
|
|
|
uint16 new_infomask;
|
2005-04-30 21:03:33 +02:00
|
|
|
LOCKMODE tuple_lock_type;
|
|
|
|
bool have_tuple_lock = false;
|
|
|
|
|
|
|
|
tuple_lock_type = (mode == LockTupleShared) ? ShareLock : ExclusiveLock;
|
1998-12-15 13:47:01 +01:00
|
|
|
|
|
|
|
*buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
|
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
dp = (PageHeader) BufferGetPage(*buffer);
|
|
|
|
lp = PageGetItemId(dp, ItemPointerGetOffsetNumber(tid));
|
1999-12-16 23:20:03 +01:00
|
|
|
tuple->t_datamcxt = NULL;
|
1998-12-15 13:47:01 +01:00
|
|
|
tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
|
|
|
|
tuple->t_len = ItemIdGetLength(lp);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
l3:
|
2004-10-16 00:40:29 +02:00
|
|
|
result = HeapTupleSatisfiesUpdate(tuple->t_data, cid, *buffer);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
if (result == HeapTupleInvisible)
|
|
|
|
{
|
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(*buffer);
|
2005-04-28 23:47:18 +02:00
|
|
|
elog(ERROR, "attempted to lock invisible tuple");
|
1998-12-15 13:47:01 +01:00
|
|
|
}
|
|
|
|
else if (result == HeapTupleBeingUpdated)
|
|
|
|
{
|
2005-04-30 21:03:33 +02:00
|
|
|
TransactionId xwait;
|
|
|
|
uint16 infomask;
|
|
|
|
|
|
|
|
/* must copy state data before unlocking buffer */
|
|
|
|
xwait = HeapTupleHeaderGetXmax(tuple->t_data);
|
|
|
|
infomask = tuple->t_data->t_infomask;
|
|
|
|
|
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire tuple lock to establish our priority for the tuple.
|
|
|
|
* LockTuple will release us when we are next-in-line for the
|
|
|
|
* tuple. We must do this even if we are share-locking.
|
|
|
|
*
|
|
|
|
* If we are forced to "start over" below, we keep the tuple lock;
|
|
|
|
* this arranges that we stay at the head of the line while
|
|
|
|
* rechecking tuple state.
|
|
|
|
*/
|
|
|
|
if (!have_tuple_lock)
|
2005-04-28 23:47:18 +02:00
|
|
|
{
|
2005-08-01 22:31:16 +02:00
|
|
|
if (nowait)
|
|
|
|
{
|
|
|
|
if (!ConditionalLockTuple(relation, tid, tuple_lock_type))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
|
|
|
|
errmsg("could not obtain lock on row in relation \"%s\"",
|
|
|
|
RelationGetRelationName(relation))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LockTuple(relation, tid, tuple_lock_type);
|
2005-04-30 21:03:33 +02:00
|
|
|
have_tuple_lock = true;
|
|
|
|
}
|
1998-12-15 13:47:01 +01:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
if (mode == LockTupleShared && (infomask & HEAP_XMAX_SHARED_LOCK))
|
|
|
|
{
|
2005-04-28 23:47:18 +02:00
|
|
|
/*
|
2005-04-30 21:03:33 +02:00
|
|
|
* Acquiring sharelock when there's at least one sharelocker
|
|
|
|
* already. We need not wait for him/them to complete.
|
2005-04-28 23:47:18 +02:00
|
|
|
*/
|
2005-04-30 21:03:33 +02:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
|
1998-12-15 13:47:01 +01:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/*
|
|
|
|
* Make sure it's still a shared lock, else start over. (It's
|
|
|
|
* OK if the ownership of the shared lock has changed, though.)
|
|
|
|
*/
|
|
|
|
if (!(tuple->t_data->t_infomask & HEAP_XMAX_SHARED_LOCK))
|
|
|
|
goto l3;
|
|
|
|
}
|
|
|
|
else if (infomask & HEAP_XMAX_IS_MULTI)
|
|
|
|
{
|
|
|
|
/* wait for multixact to end */
|
2005-08-01 22:31:16 +02:00
|
|
|
if (nowait)
|
|
|
|
{
|
|
|
|
if (!ConditionalMultiXactIdWait((MultiXactId) xwait))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
|
|
|
|
errmsg("could not obtain lock on row in relation \"%s\"",
|
|
|
|
RelationGetRelationName(relation))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
MultiXactIdWait((MultiXactId) xwait);
|
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
|
2005-04-28 23:47:18 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/*
|
|
|
|
* If xwait had just locked the tuple then some other xact
|
|
|
|
* could update this tuple before we get to this point.
|
|
|
|
* Check for xmax change, and start over if so.
|
|
|
|
*/
|
|
|
|
if (!(tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
|
|
|
|
!TransactionIdEquals(HeapTupleHeaderGetXmax(tuple->t_data),
|
|
|
|
xwait))
|
|
|
|
goto l3;
|
2005-04-28 23:47:18 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/*
|
|
|
|
* You might think the multixact is necessarily done here, but
|
|
|
|
* not so: it could have surviving members, namely our own xact
|
|
|
|
* or other subxacts of this backend. It is legal for us to
|
|
|
|
* lock the tuple in either case, however. We don't bother
|
|
|
|
* changing the on-disk hint bits since we are about to
|
|
|
|
* overwrite the xmax altogether.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* wait for regular transaction to end */
|
2005-08-01 22:31:16 +02:00
|
|
|
if (nowait)
|
|
|
|
{
|
|
|
|
if (!ConditionalXactLockTableWait(xwait))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
|
|
|
|
errmsg("could not obtain lock on row in relation \"%s\"",
|
|
|
|
RelationGetRelationName(relation))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
XactLockTableWait(xwait);
|
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
|
2005-04-28 23:47:18 +02:00
|
|
|
|
|
|
|
/*
|
2005-04-30 21:03:33 +02:00
|
|
|
* xwait is done, but if xwait had just locked the tuple then
|
|
|
|
* some other xact could update this tuple before we get to
|
|
|
|
* this point. Check for xmax change, and start over if so.
|
2005-04-28 23:47:18 +02:00
|
|
|
*/
|
2005-04-30 21:03:33 +02:00
|
|
|
if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
|
|
|
|
!TransactionIdEquals(HeapTupleHeaderGetXmax(tuple->t_data),
|
|
|
|
xwait))
|
|
|
|
goto l3;
|
|
|
|
|
|
|
|
/* Otherwise we can mark it committed or aborted */
|
|
|
|
if (!(tuple->t_data->t_infomask & (HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID)))
|
|
|
|
{
|
|
|
|
if (TransactionIdDidCommit(xwait))
|
|
|
|
tuple->t_data->t_infomask |= HEAP_XMAX_COMMITTED;
|
|
|
|
else
|
|
|
|
tuple->t_data->t_infomask |= HEAP_XMAX_INVALID;
|
|
|
|
SetBufferCommitInfoNeedsSave(*buffer);
|
|
|
|
}
|
1998-12-15 13:47:01 +01:00
|
|
|
}
|
2005-04-30 21:03:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We may lock if previous xmax aborted, or if it committed
|
|
|
|
* but only locked the tuple without updating it. The case where
|
|
|
|
* we didn't wait because we are joining an existing shared lock
|
|
|
|
* is correctly handled, too.
|
|
|
|
*/
|
|
|
|
if (tuple->t_data->t_infomask & (HEAP_XMAX_INVALID |
|
|
|
|
HEAP_IS_LOCKED))
|
|
|
|
result = HeapTupleMayBeUpdated;
|
|
|
|
else
|
|
|
|
result = HeapTupleUpdated;
|
1998-12-15 13:47:01 +01:00
|
|
|
}
|
2005-04-28 23:47:18 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
if (result != HeapTupleMayBeUpdated)
|
|
|
|
{
|
2005-04-30 21:03:33 +02:00
|
|
|
ItemPointerData newctid = tuple->t_data->t_ctid;
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
Assert(result == HeapTupleSelfUpdated || result == HeapTupleUpdated);
|
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
2005-04-30 21:03:33 +02:00
|
|
|
if (have_tuple_lock)
|
|
|
|
UnlockTuple(relation, tid, tuple_lock_type);
|
|
|
|
/* can't overwrite t_self (== *tid) until after above Unlock */
|
|
|
|
tuple->t_self = newctid;
|
1998-12-15 13:47:01 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-07-03 04:54:21 +02:00
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* Compute the new xmax and infomask to store into the tuple. Note we
|
|
|
|
* do not modify the tuple just yet, because that would leave it in the
|
|
|
|
* wrong state if multixact.c elogs.
|
2000-07-03 04:54:21 +02:00
|
|
|
*/
|
2005-04-28 23:47:18 +02:00
|
|
|
xid = GetCurrentTransactionId();
|
|
|
|
|
|
|
|
new_infomask = tuple->t_data->t_infomask;
|
|
|
|
|
|
|
|
new_infomask &= ~(HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID |
|
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
|
|
|
HEAP_MOVED);
|
|
|
|
|
|
|
|
if (mode == LockTupleShared)
|
|
|
|
{
|
|
|
|
TransactionId xmax = HeapTupleHeaderGetXmax(tuple->t_data);
|
|
|
|
uint16 old_infomask = tuple->t_data->t_infomask;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is the first acquisition of a shared lock in the current
|
|
|
|
* transaction, set my per-backend OldestMemberMXactId setting.
|
|
|
|
* We can be certain that the transaction will never become a
|
|
|
|
* member of any older MultiXactIds than that. (We have to do this
|
|
|
|
* even if we end up just using our own TransactionId below, since
|
|
|
|
* some other backend could incorporate our XID into a MultiXact
|
|
|
|
* immediately afterwards.)
|
|
|
|
*/
|
|
|
|
MultiXactIdSetOldestMember();
|
|
|
|
|
|
|
|
new_infomask |= HEAP_XMAX_SHARED_LOCK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if we need a MultiXactId because there are multiple
|
|
|
|
* lockers.
|
|
|
|
*
|
|
|
|
* HeapTupleSatisfiesUpdate will have set the HEAP_XMAX_INVALID
|
|
|
|
* bit if the xmax was a MultiXactId but it was not running anymore.
|
|
|
|
* There is a race condition, which is that the MultiXactId may have
|
|
|
|
* finished since then, but that uncommon case is handled within
|
|
|
|
* MultiXactIdExpand.
|
|
|
|
*
|
|
|
|
* There is a similar race condition possible when the old xmax was
|
|
|
|
* a regular TransactionId. We test TransactionIdIsInProgress again
|
|
|
|
* just to narrow the window, but it's still possible to end up
|
|
|
|
* creating an unnecessary MultiXactId. Fortunately this is harmless.
|
|
|
|
*/
|
|
|
|
if (!(old_infomask & (HEAP_XMAX_INVALID | HEAP_XMAX_COMMITTED)))
|
|
|
|
{
|
|
|
|
if (old_infomask & HEAP_XMAX_IS_MULTI)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the XMAX is already a MultiXactId, then we need to
|
|
|
|
* expand it to include our own TransactionId.
|
|
|
|
*/
|
2005-05-03 21:42:41 +02:00
|
|
|
xid = MultiXactIdExpand((MultiXactId) xmax, xid);
|
2005-04-28 23:47:18 +02:00
|
|
|
new_infomask |= HEAP_XMAX_IS_MULTI;
|
|
|
|
}
|
|
|
|
else if (TransactionIdIsInProgress(xmax))
|
|
|
|
{
|
|
|
|
if (TransactionIdEquals(xmax, xid))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the old locker is ourselves, we'll just mark the
|
|
|
|
* tuple again with our own TransactionId. However we
|
|
|
|
* have to consider the possibility that we had
|
|
|
|
* exclusive rather than shared lock before --- if so,
|
|
|
|
* be careful to preserve the exclusivity of the lock.
|
|
|
|
*/
|
|
|
|
if (!(old_infomask & HEAP_XMAX_SHARED_LOCK))
|
|
|
|
{
|
|
|
|
new_infomask &= ~HEAP_XMAX_SHARED_LOCK;
|
|
|
|
new_infomask |= HEAP_XMAX_EXCL_LOCK;
|
|
|
|
mode = LockTupleExclusive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the Xmax is a valid TransactionId, then we need to
|
|
|
|
* create a new MultiXactId that includes both the old
|
|
|
|
* locker and our own TransactionId.
|
|
|
|
*/
|
2005-05-03 21:42:41 +02:00
|
|
|
xid = MultiXactIdCreate(xmax, xid);
|
2005-04-28 23:47:18 +02:00
|
|
|
new_infomask |= HEAP_XMAX_IS_MULTI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Can get here iff HeapTupleSatisfiesUpdate saw the old
|
|
|
|
* xmax as running, but it finished before
|
|
|
|
* TransactionIdIsInProgress() got to run. Treat it like
|
|
|
|
* there's no locker in the tuple.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There was no previous locker, so just insert our own
|
|
|
|
* TransactionId.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We want an exclusive lock on the tuple */
|
|
|
|
new_infomask |= HEAP_XMAX_EXCL_LOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
START_CRIT_SECTION();
|
2000-07-03 04:54:21 +02:00
|
|
|
|
2005-04-28 23:47:18 +02:00
|
|
|
/*
|
|
|
|
* Store transaction information of xact locking the tuple.
|
|
|
|
*
|
|
|
|
* Note: our CID is meaningless if storing a MultiXactId, but no harm
|
|
|
|
* in storing it anyway.
|
|
|
|
*/
|
|
|
|
tuple->t_data->t_infomask = new_infomask;
|
2004-09-16 18:58:44 +02:00
|
|
|
HeapTupleHeaderSetXmax(tuple->t_data, xid);
|
2002-06-15 21:54:24 +02:00
|
|
|
HeapTupleHeaderSetCmax(tuple->t_data, cid);
|
2002-08-13 22:11:03 +02:00
|
|
|
/* Make sure there is no forward chain link in t_ctid */
|
|
|
|
tuple->t_data->t_ctid = *tid;
|
1998-12-15 13:47:01 +01:00
|
|
|
|
2005-04-28 23:47:18 +02:00
|
|
|
/*
|
|
|
|
* XLOG stuff. You might think that we don't need an XLOG record because
|
|
|
|
* there is no state change worth restoring after a crash. You would be
|
|
|
|
* wrong however: we have just written either a TransactionId or a
|
|
|
|
* MultiXactId that may never have been seen on disk before, and we need
|
|
|
|
* to make sure that there are XLOG entries covering those ID numbers.
|
|
|
|
* Else the same IDs might be re-used after a crash, which would be
|
|
|
|
* disastrous if this page made it to disk before the crash. Essentially
|
|
|
|
* we have to enforce the WAL log-before-data rule even in this case.
|
2005-06-08 17:50:28 +02:00
|
|
|
* (Also, in a PITR log-shipping or 2PC environment, we have to have XLOG
|
|
|
|
* entries for everything anyway.)
|
2005-04-28 23:47:18 +02:00
|
|
|
*/
|
|
|
|
if (!relation->rd_istemp)
|
|
|
|
{
|
|
|
|
xl_heap_lock xlrec;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogRecData rdata[2];
|
|
|
|
|
|
|
|
xlrec.target.node = relation->rd_node;
|
|
|
|
xlrec.target.tid = tuple->t_self;
|
2005-06-08 17:50:28 +02:00
|
|
|
xlrec.locking_xid = xid;
|
|
|
|
xlrec.xid_is_mxact = ((new_infomask & HEAP_XMAX_IS_MULTI) != 0);
|
2005-04-28 23:47:18 +02:00
|
|
|
xlrec.shared_lock = (mode == LockTupleShared);
|
|
|
|
rdata[0].data = (char *) &xlrec;
|
|
|
|
rdata[0].len = SizeOfHeapLock;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2005-04-28 23:47:18 +02:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
|
|
|
rdata[1].data = NULL;
|
|
|
|
rdata[1].len = 0;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = *buffer;
|
|
|
|
rdata[1].buffer_std = true;
|
2005-04-28 23:47:18 +02:00
|
|
|
rdata[1].next = NULL;
|
|
|
|
|
|
|
|
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK, rdata);
|
|
|
|
|
|
|
|
PageSetLSN(dp, recptr);
|
|
|
|
PageSetTLI(dp, ThisTimeLineID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No XLOG record, but still need to flag that XID exists on disk */
|
|
|
|
MyXactMadeTempRelUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
WriteNoReleaseBuffer(*buffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-04-30 21:03:33 +02:00
|
|
|
/*
|
|
|
|
* Now that we have successfully marked the tuple as locked, we can
|
|
|
|
* release the lmgr tuple lock, if we had it.
|
|
|
|
*/
|
|
|
|
if (have_tuple_lock)
|
|
|
|
UnlockTuple(relation, tid, tuple_lock_type);
|
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
return HeapTupleMayBeUpdated;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_markpos - mark scan position
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_markpos(HeapScanDesc scan)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/* Note: no locking manipulations needed */
|
|
|
|
|
1998-11-27 20:52:36 +01:00
|
|
|
if (scan->rs_ctup.t_data != NULL)
|
|
|
|
scan->rs_mctid = scan->rs_ctup.t_self;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
1998-08-19 04:04:17 +02:00
|
|
|
ItemPointerSetInvalid(&scan->rs_mctid);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_restrpos - restore position to marked location
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_restrpos(HeapScanDesc scan)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/* XXX no amrestrpos checking that ammarkpos called */
|
|
|
|
|
|
|
|
/* Note: no locking manipulations needed */
|
|
|
|
|
2001-06-09 20:16:59 +02:00
|
|
|
/*
|
|
|
|
* unpin scan buffers
|
|
|
|
*/
|
|
|
|
if (BufferIsValid(scan->rs_cbuf))
|
|
|
|
ReleaseBuffer(scan->rs_cbuf);
|
1998-08-19 04:04:17 +02:00
|
|
|
scan->rs_cbuf = InvalidBuffer;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!ItemPointerIsValid(&scan->rs_mctid))
|
1999-12-16 23:20:03 +01:00
|
|
|
{
|
|
|
|
scan->rs_ctup.t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
scan->rs_ctup.t_data = NULL;
|
1999-12-16 23:20:03 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
1998-11-27 20:52:36 +01:00
|
|
|
scan->rs_ctup.t_self = scan->rs_mctid;
|
1999-12-16 23:20:03 +01:00
|
|
|
scan->rs_ctup.t_datamcxt = NULL;
|
1998-11-27 20:52:36 +01:00
|
|
|
scan->rs_ctup.t_data = (HeapTupleHeader) 0x1; /* for heapgettup */
|
|
|
|
heapgettup(scan->rs_rd,
|
|
|
|
0,
|
2001-06-09 20:16:59 +02:00
|
|
|
&(scan->rs_ctup),
|
1998-11-27 20:52:36 +01:00
|
|
|
&(scan->rs_cbuf),
|
2001-06-09 20:16:59 +02:00
|
|
|
scan->rs_snapshot,
|
1998-11-27 20:52:36 +01:00
|
|
|
0,
|
2004-05-08 21:09:25 +02:00
|
|
|
NULL,
|
|
|
|
scan->rs_nblocks);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-07-04 03:39:24 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
XLogRecPtr
|
2003-02-23 21:32:12 +01:00
|
|
|
log_heap_clean(Relation reln, Buffer buffer, OffsetNumber *unused, int uncnt)
|
2000-10-24 11:56:23 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_clean xlrec;
|
|
|
|
XLogRecPtr recptr;
|
2003-02-23 21:32:12 +01:00
|
|
|
XLogRecData rdata[2];
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* Caller should not call me on a temp relation */
|
|
|
|
Assert(!reln->rd_istemp);
|
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
xlrec.node = reln->rd_node;
|
|
|
|
xlrec.block = BufferGetBlockNumber(buffer);
|
2003-02-23 21:32:12 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].len = SizeOfHeapClean;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
2000-10-24 11:56:23 +02:00
|
|
|
|
2003-02-23 21:32:12 +01:00
|
|
|
/*
|
|
|
|
* The unused-offsets array is not actually in the buffer, but pretend
|
2003-08-04 02:43:34 +02:00
|
|
|
* that it is. When XLogInsert stores the whole buffer, the offsets
|
2003-02-23 21:32:12 +01:00
|
|
|
* array need not be stored too.
|
|
|
|
*/
|
|
|
|
if (uncnt > 0)
|
2000-12-30 16:19:57 +01:00
|
|
|
{
|
2003-02-23 21:32:12 +01:00
|
|
|
rdata[1].data = (char *) unused;
|
|
|
|
rdata[1].len = uncnt * sizeof(OffsetNumber);
|
2000-12-30 16:19:57 +01:00
|
|
|
}
|
|
|
|
else
|
2003-02-23 21:32:12 +01:00
|
|
|
{
|
|
|
|
rdata[1].data = NULL;
|
|
|
|
rdata[1].len = 0;
|
|
|
|
}
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = buffer;
|
|
|
|
rdata[1].buffer_std = true;
|
2003-02-23 21:32:12 +01:00
|
|
|
rdata[1].next = NULL;
|
2000-10-24 11:56:23 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_CLEAN, rdata);
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
return (recptr);
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static XLogRecPtr
|
2001-03-22 05:01:46 +01:00
|
|
|
log_heap_update(Relation reln, Buffer oldbuf, ItemPointerData from,
|
2000-12-28 14:00:29 +01:00
|
|
|
Buffer newbuf, HeapTuple newtup, bool move)
|
|
|
|
{
|
2001-03-26 01:23:59 +02:00
|
|
|
/*
|
|
|
|
* Note: xlhdr is declared to have adequate size and correct alignment
|
|
|
|
* for an xl_heap_header. However the two tids, if present at all,
|
|
|
|
* will be packed in with no wasted space after the xl_heap_header;
|
2001-10-25 07:50:21 +02:00
|
|
|
* they aren't necessarily aligned as implied by this struct
|
|
|
|
* declaration.
|
2001-03-26 01:23:59 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
xl_heap_header hdr;
|
|
|
|
TransactionId tid1;
|
|
|
|
TransactionId tid2;
|
2001-03-26 01:23:59 +02:00
|
|
|
} xlhdr;
|
2001-03-22 05:01:46 +01:00
|
|
|
int hsize = SizeOfHeapHeader;
|
2001-03-26 01:23:59 +02:00
|
|
|
xl_heap_update xlrec;
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogRecData rdata[4];
|
|
|
|
Page page = BufferGetPage(newbuf);
|
|
|
|
uint8 info = (move) ? XLOG_HEAP_MOVE : XLOG_HEAP_UPDATE;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* Caller should not call me on a temp relation */
|
|
|
|
Assert(!reln->rd_istemp);
|
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
xlrec.target.node = reln->rd_node;
|
|
|
|
xlrec.target.tid = from;
|
|
|
|
xlrec.newtid = newtup->t_self;
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].len = SizeOfHeapUpdate;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[0].next = &(rdata[1]);
|
|
|
|
|
|
|
|
rdata[1].data = NULL;
|
|
|
|
rdata[1].len = 0;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[1].buffer = oldbuf;
|
|
|
|
rdata[1].buffer_std = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[1].next = &(rdata[2]);
|
|
|
|
|
2001-03-26 01:23:59 +02:00
|
|
|
xlhdr.hdr.t_natts = newtup->t_data->t_natts;
|
2002-09-27 00:46:29 +02:00
|
|
|
xlhdr.hdr.t_infomask = newtup->t_data->t_infomask;
|
2001-03-26 01:23:59 +02:00
|
|
|
xlhdr.hdr.t_hoff = newtup->t_data->t_hoff;
|
2002-09-27 00:46:29 +02:00
|
|
|
if (move) /* remember xmax & xmin */
|
2000-10-24 11:56:23 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
TransactionId xid[2]; /* xmax, xmin */
|
2000-10-24 11:56:23 +02:00
|
|
|
|
2005-04-28 23:47:18 +02:00
|
|
|
if (newtup->t_data->t_infomask & (HEAP_XMAX_INVALID | HEAP_IS_LOCKED))
|
2002-07-20 07:16:59 +02:00
|
|
|
xid[0] = InvalidTransactionId;
|
2000-10-24 11:56:23 +02:00
|
|
|
else
|
2002-07-20 07:16:59 +02:00
|
|
|
xid[0] = HeapTupleHeaderGetXmax(newtup->t_data);
|
|
|
|
xid[1] = HeapTupleHeaderGetXmin(newtup->t_data);
|
|
|
|
memcpy((char *) &xlhdr + hsize,
|
2002-09-04 22:31:48 +02:00
|
|
|
(char *) xid,
|
2002-08-06 04:36:35 +02:00
|
|
|
2 * sizeof(TransactionId));
|
2001-03-26 01:23:59 +02:00
|
|
|
hsize += 2 * sizeof(TransactionId);
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-09-27 00:46:29 +02:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* As with insert records, we need not store the rdata[2] segment if
|
|
|
|
* we decide to store the whole buffer instead.
|
2002-09-27 00:46:29 +02:00
|
|
|
*/
|
2001-03-26 01:23:59 +02:00
|
|
|
rdata[2].data = (char *) &xlhdr;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[2].len = hsize;
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[2].buffer = newbuf;
|
|
|
|
rdata[2].buffer_std = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[2].next = &(rdata[3]);
|
|
|
|
|
2002-07-20 07:16:59 +02:00
|
|
|
/* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata[3].data = (char *) newtup->t_data + offsetof(HeapTupleHeaderData, t_bits);
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[3].len = newtup->t_len - offsetof(HeapTupleHeaderData, t_bits);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[3].buffer = newbuf;
|
|
|
|
rdata[3].buffer_std = true;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata[3].next = NULL;
|
|
|
|
|
|
|
|
/* If new tuple is the single and first tuple on page... */
|
|
|
|
if (ItemPointerGetOffsetNumber(&(newtup->t_self)) == FirstOffsetNumber &&
|
|
|
|
PageGetMaxOffsetNumber(page) == FirstOffsetNumber)
|
|
|
|
{
|
|
|
|
info |= XLOG_HEAP_INIT_PAGE;
|
|
|
|
rdata[2].buffer = rdata[3].buffer = InvalidBuffer;
|
2000-10-24 11:56:23 +02:00
|
|
|
}
|
2000-12-28 14:00:29 +01:00
|
|
|
|
|
|
|
recptr = XLogInsert(RM_HEAP_ID, info, rdata);
|
2000-10-24 11:56:23 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
return (recptr);
|
2000-10-24 11:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XLogRecPtr
|
2001-03-22 05:01:46 +01:00
|
|
|
log_heap_move(Relation reln, Buffer oldbuf, ItemPointerData from,
|
|
|
|
Buffer newbuf, HeapTuple newtup)
|
2000-10-24 11:56:23 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
return (log_heap_update(reln, oldbuf, from, newbuf, newtup, true));
|
2000-10-24 11:56:23 +02:00
|
|
|
}
|
|
|
|
|
2000-10-29 19:33:41 +01:00
|
|
|
static void
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_clean(XLogRecPtr lsn, XLogRecord *record)
|
2000-10-29 19:33:41 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_clean *xlrec = (xl_heap_clean *) XLogRecGetData(record);
|
|
|
|
Relation reln;
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLR_BKP_BLOCK_1)
|
2000-12-28 14:00:29 +01:00
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
reln = XLogOpenRelation(xlrec->node);
|
2000-12-28 14:00:29 +01:00
|
|
|
if (!RelationIsValid(reln))
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = XLogReadBuffer(false, reln, xlrec->block);
|
|
|
|
if (!BufferIsValid(buffer))
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(PANIC, "heap_clean_redo: no block");
|
2000-12-28 14:00:29 +01:00
|
|
|
|
|
|
|
page = (Page) BufferGetPage(buffer);
|
|
|
|
if (PageIsNew((PageHeader) page))
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(PANIC, "heap_clean_redo: uninitialized page");
|
2000-10-29 19:33:41 +01:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
if (XLByteLE(lsn, PageGetLSN(page)))
|
|
|
|
{
|
2003-12-14 01:34:47 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
2000-12-28 14:00:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-12-30 16:19:57 +01:00
|
|
|
if (record->xl_len > SizeOfHeapClean)
|
2000-10-29 19:33:41 +01:00
|
|
|
{
|
2003-02-23 21:32:12 +01:00
|
|
|
OffsetNumber *unused;
|
|
|
|
OffsetNumber *unend;
|
2001-03-22 05:01:46 +01:00
|
|
|
ItemId lp;
|
2000-10-29 19:33:41 +01:00
|
|
|
|
2003-02-23 21:32:12 +01:00
|
|
|
unused = (OffsetNumber *) ((char *) xlrec + SizeOfHeapClean);
|
|
|
|
unend = (OffsetNumber *) ((char *) xlrec + record->xl_len);
|
2000-10-29 19:33:41 +01:00
|
|
|
|
2003-02-23 21:32:12 +01:00
|
|
|
while (unused < unend)
|
2000-12-30 16:19:57 +01:00
|
|
|
{
|
2002-07-02 07:48:44 +02:00
|
|
|
lp = PageGetItemId(page, *unused + 1);
|
2000-10-29 19:33:41 +01:00
|
|
|
lp->lp_flags &= ~LP_USED;
|
2000-12-30 16:19:57 +01:00
|
|
|
unused++;
|
|
|
|
}
|
2000-10-29 19:33:41 +01:00
|
|
|
}
|
|
|
|
|
2000-12-30 16:19:57 +01:00
|
|
|
PageRepairFragmentation(page, NULL);
|
2002-08-13 22:11:03 +02:00
|
|
|
|
|
|
|
PageSetLSN(page, lsn);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2003-12-14 01:34:47 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
2000-10-29 19:33:41 +01:00
|
|
|
}
|
|
|
|
|
2004-07-11 20:01:45 +02:00
|
|
|
static void
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_newpage(XLogRecPtr lsn, XLogRecord *record)
|
2004-07-11 20:01:45 +02:00
|
|
|
{
|
|
|
|
xl_heap_newpage *xlrec = (xl_heap_newpage *) XLogRecGetData(record);
|
|
|
|
Relation reln;
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Note: the NEWPAGE log record is used for both heaps and indexes, so
|
|
|
|
* do not do anything that assumes we are touching a heap.
|
2004-07-11 20:01:45 +02:00
|
|
|
*/
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLR_BKP_BLOCK_1)
|
2004-07-11 20:01:45 +02:00
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
reln = XLogOpenRelation(xlrec->node);
|
2004-07-11 20:01:45 +02:00
|
|
|
if (!RelationIsValid(reln))
|
|
|
|
return;
|
|
|
|
buffer = XLogReadBuffer(true, reln, xlrec->blkno);
|
|
|
|
if (!BufferIsValid(buffer))
|
|
|
|
elog(PANIC, "heap_newpage_redo: no block");
|
|
|
|
page = (Page) BufferGetPage(buffer);
|
|
|
|
|
|
|
|
Assert(record->xl_len == SizeOfHeapNewpage + BLCKSZ);
|
|
|
|
memcpy(page, (char *) xlrec + SizeOfHeapNewpage, BLCKSZ);
|
|
|
|
|
|
|
|
PageSetLSN(page, lsn);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2004-07-11 20:01:45 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
static void
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_delete(XLogRecPtr lsn, XLogRecord *record)
|
2000-07-04 03:39:24 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_delete *xlrec = (xl_heap_delete *) XLogRecGetData(record);
|
2002-08-13 22:11:03 +02:00
|
|
|
Relation reln;
|
2001-03-22 05:01:46 +01:00
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offnum;
|
|
|
|
ItemId lp = NULL;
|
|
|
|
HeapTupleHeader htup;
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLR_BKP_BLOCK_1)
|
2000-12-28 14:00:29 +01:00
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
reln = XLogOpenRelation(xlrec->target.node);
|
2002-08-13 22:11:03 +02:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
if (!RelationIsValid(reln))
|
|
|
|
return;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
buffer = XLogReadBuffer(false, reln,
|
2000-09-07 11:58:38 +02:00
|
|
|
ItemPointerGetBlockNumber(&(xlrec->target.tid)));
|
|
|
|
if (!BufferIsValid(buffer))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_delete_redo: no block");
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
page = (Page) BufferGetPage(buffer);
|
2000-09-07 11:58:38 +02:00
|
|
|
if (PageIsNew((PageHeader) page))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_delete_redo: uninitialized page");
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (XLByteLE(lsn, PageGetLSN(page))) /* changes are applied */
|
2000-09-07 11:58:38 +02:00
|
|
|
{
|
2005-06-06 19:01:25 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
return;
|
2000-09-07 11:58:38 +02:00
|
|
|
}
|
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
|
2000-10-29 19:33:41 +01:00
|
|
|
if (PageGetMaxOffsetNumber(page) >= offnum)
|
|
|
|
lp = PageGetItemId(page, offnum);
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2000-10-29 19:33:41 +01:00
|
|
|
if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsUsed(lp))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_delete_redo: invalid lp");
|
2000-10-29 19:33:41 +01:00
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
htup = (HeapTupleHeader) PageGetItem(page, lp);
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
htup->t_infomask &= ~(HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID |
|
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
|
|
|
HEAP_MOVED);
|
|
|
|
HeapTupleHeaderSetXmax(htup, record->xl_xid);
|
|
|
|
HeapTupleHeaderSetCmax(htup, FirstCommandId);
|
|
|
|
/* Make sure there is no forward chain link in t_ctid */
|
|
|
|
htup->t_ctid = xlrec->target.tid;
|
|
|
|
PageSetLSN(page, lsn);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
2000-07-04 03:39:24 +02:00
|
|
|
}
|
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
static void
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_insert(XLogRecPtr lsn, XLogRecord *record)
|
2000-07-04 03:39:24 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_insert *xlrec = (xl_heap_insert *) XLogRecGetData(record);
|
2002-08-13 22:11:03 +02:00
|
|
|
Relation reln;
|
2001-03-22 05:01:46 +01:00
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offnum;
|
2005-06-06 19:01:25 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
HeapTupleHeaderData hdr;
|
|
|
|
char data[MaxTupleSize];
|
|
|
|
} tbuf;
|
|
|
|
HeapTupleHeader htup;
|
|
|
|
xl_heap_header xlhdr;
|
|
|
|
uint32 newlen;
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLR_BKP_BLOCK_1)
|
2000-12-28 14:00:29 +01:00
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
reln = XLogOpenRelation(xlrec->target.node);
|
2002-08-13 22:11:03 +02:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
if (!RelationIsValid(reln))
|
|
|
|
return;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
buffer = XLogReadBuffer(true, reln,
|
2000-09-07 11:58:38 +02:00
|
|
|
ItemPointerGetBlockNumber(&(xlrec->target.tid)));
|
|
|
|
if (!BufferIsValid(buffer))
|
|
|
|
return;
|
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
page = (Page) BufferGetPage(buffer);
|
2000-12-28 14:00:29 +01:00
|
|
|
if (PageIsNew((PageHeader) page) &&
|
2005-06-06 19:01:25 +02:00
|
|
|
!(record->xl_info & XLOG_HEAP_INIT_PAGE))
|
|
|
|
elog(PANIC, "heap_insert_redo: uninitialized page");
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLOG_HEAP_INIT_PAGE)
|
|
|
|
PageInit(page, BufferGetPageSize(buffer), 0);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (XLByteLE(lsn, PageGetLSN(page))) /* changes are applied */
|
|
|
|
{
|
2003-12-14 01:34:47 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2005-06-06 19:01:25 +02:00
|
|
|
ReleaseBuffer(buffer);
|
2000-09-07 11:58:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
|
|
|
|
if (PageGetMaxOffsetNumber(page) + 1 < offnum)
|
|
|
|
elog(PANIC, "heap_insert_redo: invalid max offset number");
|
|
|
|
|
|
|
|
newlen = record->xl_len - SizeOfHeapInsert - SizeOfHeapHeader;
|
|
|
|
Assert(newlen <= MaxTupleSize);
|
|
|
|
memcpy((char *) &xlhdr,
|
|
|
|
(char *) xlrec + SizeOfHeapInsert,
|
|
|
|
SizeOfHeapHeader);
|
|
|
|
htup = &tbuf.hdr;
|
|
|
|
MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
|
|
|
|
/* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
|
|
|
|
memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
|
|
|
|
(char *) xlrec + SizeOfHeapInsert + SizeOfHeapHeader,
|
|
|
|
newlen);
|
|
|
|
newlen += offsetof(HeapTupleHeaderData, t_bits);
|
|
|
|
htup->t_natts = xlhdr.t_natts;
|
|
|
|
htup->t_infomask = xlhdr.t_infomask;
|
|
|
|
htup->t_hoff = xlhdr.t_hoff;
|
|
|
|
HeapTupleHeaderSetXmin(htup, record->xl_xid);
|
|
|
|
HeapTupleHeaderSetCmin(htup, FirstCommandId);
|
|
|
|
htup->t_ctid = xlrec->target.tid;
|
|
|
|
|
|
|
|
offnum = PageAddItem(page, (Item) htup, newlen, offnum,
|
|
|
|
LP_USED | OverwritePageMode);
|
|
|
|
if (offnum == InvalidOffsetNumber)
|
|
|
|
elog(PANIC, "heap_insert_redo: failed to add tuple");
|
|
|
|
PageSetLSN(page, lsn);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
2000-09-07 11:58:38 +02:00
|
|
|
}
|
|
|
|
|
2000-10-24 11:56:23 +02:00
|
|
|
/*
|
|
|
|
* Handles UPDATE & MOVE
|
|
|
|
*/
|
2000-10-20 13:01:21 +02:00
|
|
|
static void
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_update(XLogRecPtr lsn, XLogRecord *record, bool move)
|
2000-09-07 11:58:38 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_update *xlrec = (xl_heap_update *) XLogRecGetData(record);
|
2005-06-06 19:01:25 +02:00
|
|
|
Relation reln = XLogOpenRelation(xlrec->target.node);
|
2001-03-22 05:01:46 +01:00
|
|
|
Buffer buffer;
|
|
|
|
bool samepage =
|
|
|
|
(ItemPointerGetBlockNumber(&(xlrec->newtid)) ==
|
|
|
|
ItemPointerGetBlockNumber(&(xlrec->target.tid)));
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offnum;
|
|
|
|
ItemId lp = NULL;
|
|
|
|
HeapTupleHeader htup;
|
2005-06-06 19:01:25 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
HeapTupleHeaderData hdr;
|
|
|
|
char data[MaxTupleSize];
|
|
|
|
} tbuf;
|
|
|
|
xl_heap_header xlhdr;
|
|
|
|
int hsize;
|
|
|
|
uint32 newlen;
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
if (!RelationIsValid(reln))
|
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLR_BKP_BLOCK_1)
|
2000-12-28 14:00:29 +01:00
|
|
|
goto newt;
|
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
/* Deal with old tuple version */
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
buffer = XLogReadBuffer(false, reln,
|
2000-12-28 14:00:29 +01:00
|
|
|
ItemPointerGetBlockNumber(&(xlrec->target.tid)));
|
2000-09-07 11:58:38 +02:00
|
|
|
if (!BufferIsValid(buffer))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_update_redo: no block");
|
2000-09-07 11:58:38 +02:00
|
|
|
|
|
|
|
page = (Page) BufferGetPage(buffer);
|
|
|
|
if (PageIsNew((PageHeader) page))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_update_redo: uninitialized old page");
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (XLByteLE(lsn, PageGetLSN(page))) /* changes are applied */
|
2000-09-07 11:58:38 +02:00
|
|
|
{
|
2005-06-06 19:01:25 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
if (samepage)
|
|
|
|
return;
|
|
|
|
goto newt;
|
2000-09-07 11:58:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
|
2000-10-29 19:33:41 +01:00
|
|
|
if (PageGetMaxOffsetNumber(page) >= offnum)
|
|
|
|
lp = PageGetItemId(page, offnum);
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2000-10-29 19:33:41 +01:00
|
|
|
if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsUsed(lp))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_update_redo: invalid lp");
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
htup = (HeapTupleHeader) PageGetItem(page, lp);
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (move)
|
2000-09-07 11:58:38 +02:00
|
|
|
{
|
2005-06-06 19:01:25 +02:00
|
|
|
htup->t_infomask &= ~(HEAP_XMIN_COMMITTED |
|
|
|
|
HEAP_XMIN_INVALID |
|
|
|
|
HEAP_MOVED_IN);
|
|
|
|
htup->t_infomask |= HEAP_MOVED_OFF;
|
|
|
|
HeapTupleHeaderSetXvac(htup, record->xl_xid);
|
|
|
|
/* Make sure there is no forward chain link in t_ctid */
|
|
|
|
htup->t_ctid = xlrec->target.tid;
|
2000-09-07 11:58:38 +02:00
|
|
|
}
|
2005-06-06 19:01:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
htup->t_infomask &= ~(HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID |
|
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
|
|
|
HEAP_MOVED);
|
|
|
|
HeapTupleHeaderSetXmax(htup, record->xl_xid);
|
|
|
|
HeapTupleHeaderSetCmax(htup, FirstCommandId);
|
|
|
|
/* Set forward chain link in t_ctid */
|
|
|
|
htup->t_ctid = xlrec->newtid;
|
|
|
|
}
|
|
|
|
if (samepage)
|
|
|
|
goto newsame;
|
|
|
|
PageSetLSN(page, lsn);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
2000-09-07 11:58:38 +02:00
|
|
|
|
|
|
|
/* Deal with new tuple */
|
|
|
|
|
|
|
|
newt:;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if ((record->xl_info & XLR_BKP_BLOCK_2) ||
|
|
|
|
((record->xl_info & XLR_BKP_BLOCK_1) && samepage))
|
2000-12-28 14:00:29 +01:00
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
buffer = XLogReadBuffer(true, reln,
|
2001-03-22 05:01:46 +01:00
|
|
|
ItemPointerGetBlockNumber(&(xlrec->newtid)));
|
2000-09-07 11:58:38 +02:00
|
|
|
if (!BufferIsValid(buffer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
page = (Page) BufferGetPage(buffer);
|
2000-10-29 19:33:41 +01:00
|
|
|
|
|
|
|
newsame:;
|
2000-12-28 14:00:29 +01:00
|
|
|
if (PageIsNew((PageHeader) page) &&
|
2005-06-06 19:01:25 +02:00
|
|
|
!(record->xl_info & XLOG_HEAP_INIT_PAGE))
|
|
|
|
elog(PANIC, "heap_update_redo: uninitialized page");
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLOG_HEAP_INIT_PAGE)
|
|
|
|
PageInit(page, BufferGetPageSize(buffer), 0);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (XLByteLE(lsn, PageGetLSN(page))) /* changes are applied */
|
|
|
|
{
|
2003-12-14 01:34:47 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2005-06-06 19:01:25 +02:00
|
|
|
ReleaseBuffer(buffer);
|
2000-09-07 11:58:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
offnum = ItemPointerGetOffsetNumber(&(xlrec->newtid));
|
|
|
|
if (PageGetMaxOffsetNumber(page) + 1 < offnum)
|
|
|
|
elog(PANIC, "heap_update_redo: invalid max offset number");
|
|
|
|
|
|
|
|
hsize = SizeOfHeapUpdate + SizeOfHeapHeader;
|
|
|
|
if (move)
|
|
|
|
hsize += (2 * sizeof(TransactionId));
|
|
|
|
|
|
|
|
newlen = record->xl_len - hsize;
|
|
|
|
Assert(newlen <= MaxTupleSize);
|
|
|
|
memcpy((char *) &xlhdr,
|
|
|
|
(char *) xlrec + SizeOfHeapUpdate,
|
|
|
|
SizeOfHeapHeader);
|
|
|
|
htup = &tbuf.hdr;
|
|
|
|
MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
|
|
|
|
/* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
|
|
|
|
memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
|
|
|
|
(char *) xlrec + hsize,
|
|
|
|
newlen);
|
|
|
|
newlen += offsetof(HeapTupleHeaderData, t_bits);
|
|
|
|
htup->t_natts = xlhdr.t_natts;
|
|
|
|
htup->t_infomask = xlhdr.t_infomask;
|
|
|
|
htup->t_hoff = xlhdr.t_hoff;
|
|
|
|
|
|
|
|
if (move)
|
|
|
|
{
|
|
|
|
TransactionId xid[2]; /* xmax, xmin */
|
|
|
|
|
|
|
|
memcpy((char *) xid,
|
|
|
|
(char *) xlrec + SizeOfHeapUpdate + SizeOfHeapHeader,
|
|
|
|
2 * sizeof(TransactionId));
|
|
|
|
HeapTupleHeaderSetXmin(htup, xid[1]);
|
|
|
|
HeapTupleHeaderSetXmax(htup, xid[0]);
|
|
|
|
HeapTupleHeaderSetXvac(htup, record->xl_xid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HeapTupleHeaderSetXmin(htup, record->xl_xid);
|
|
|
|
HeapTupleHeaderSetCmin(htup, FirstCommandId);
|
|
|
|
}
|
|
|
|
/* Make sure there is no forward chain link in t_ctid */
|
|
|
|
htup->t_ctid = xlrec->newtid;
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
offnum = PageAddItem(page, (Item) htup, newlen, offnum,
|
|
|
|
LP_USED | OverwritePageMode);
|
|
|
|
if (offnum == InvalidOffsetNumber)
|
|
|
|
elog(PANIC, "heap_update_redo: failed to add tuple");
|
|
|
|
PageSetLSN(page, lsn);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
|
|
|
|
2005-04-28 23:47:18 +02:00
|
|
|
static void
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_lock(XLogRecPtr lsn, XLogRecord *record)
|
2005-04-28 23:47:18 +02:00
|
|
|
{
|
|
|
|
xl_heap_lock *xlrec = (xl_heap_lock *) XLogRecGetData(record);
|
|
|
|
Relation reln;
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offnum;
|
|
|
|
ItemId lp = NULL;
|
|
|
|
HeapTupleHeader htup;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (record->xl_info & XLR_BKP_BLOCK_1)
|
2005-04-28 23:47:18 +02:00
|
|
|
return;
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
reln = XLogOpenRelation(xlrec->target.node);
|
2005-04-28 23:47:18 +02:00
|
|
|
|
|
|
|
if (!RelationIsValid(reln))
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = XLogReadBuffer(false, reln,
|
|
|
|
ItemPointerGetBlockNumber(&(xlrec->target.tid)));
|
|
|
|
if (!BufferIsValid(buffer))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_lock_redo: no block");
|
2005-04-28 23:47:18 +02:00
|
|
|
|
|
|
|
page = (Page) BufferGetPage(buffer);
|
|
|
|
if (PageIsNew((PageHeader) page))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_lock_redo: uninitialized page");
|
2005-04-28 23:47:18 +02:00
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
if (XLByteLE(lsn, PageGetLSN(page))) /* changes are applied */
|
2005-04-28 23:47:18 +02:00
|
|
|
{
|
2005-06-06 19:01:25 +02:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
return;
|
2005-04-28 23:47:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
|
|
|
|
if (PageGetMaxOffsetNumber(page) >= offnum)
|
|
|
|
lp = PageGetItemId(page, offnum);
|
|
|
|
|
|
|
|
if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsUsed(lp))
|
2005-06-06 19:01:25 +02:00
|
|
|
elog(PANIC, "heap_lock_redo: invalid lp");
|
2005-04-28 23:47:18 +02:00
|
|
|
|
|
|
|
htup = (HeapTupleHeader) PageGetItem(page, lp);
|
|
|
|
|
2005-06-06 19:01:25 +02:00
|
|
|
htup->t_infomask &= ~(HEAP_XMAX_COMMITTED |
|
|
|
|
HEAP_XMAX_INVALID |
|
|
|
|
HEAP_XMAX_IS_MULTI |
|
|
|
|
HEAP_IS_LOCKED |
|
|
|
|
HEAP_MOVED);
|
2005-06-08 17:50:28 +02:00
|
|
|
if (xlrec->xid_is_mxact)
|
|
|
|
htup->t_infomask |= HEAP_XMAX_IS_MULTI;
|
|
|
|
if (xlrec->shared_lock)
|
|
|
|
htup->t_infomask |= HEAP_XMAX_SHARED_LOCK;
|
|
|
|
else
|
|
|
|
htup->t_infomask |= HEAP_XMAX_EXCL_LOCK;
|
|
|
|
HeapTupleHeaderSetXmax(htup, xlrec->locking_xid);
|
2005-06-06 19:01:25 +02:00
|
|
|
HeapTupleHeaderSetCmax(htup, FirstCommandId);
|
|
|
|
/* Make sure there is no forward chain link in t_ctid */
|
|
|
|
htup->t_ctid = xlrec->target.tid;
|
|
|
|
PageSetLSN(page, lsn);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
|
|
|
WriteBuffer(buffer);
|
2005-04-28 23:47:18 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
void
|
|
|
|
heap_redo(XLogRecPtr lsn, XLogRecord *record)
|
2000-10-20 13:01:21 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
uint8 info = record->xl_info & ~XLR_INFO_MASK;
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
info &= XLOG_HEAP_OPMASK;
|
2000-10-20 13:01:21 +02:00
|
|
|
if (info == XLOG_HEAP_INSERT)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_insert(lsn, record);
|
2000-10-20 13:01:21 +02:00
|
|
|
else if (info == XLOG_HEAP_DELETE)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_delete(lsn, record);
|
2000-10-20 13:01:21 +02:00
|
|
|
else if (info == XLOG_HEAP_UPDATE)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_update(lsn, record, false);
|
2000-10-20 13:01:21 +02:00
|
|
|
else if (info == XLOG_HEAP_MOVE)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_update(lsn, record, true);
|
2000-12-28 14:00:29 +01:00
|
|
|
else if (info == XLOG_HEAP_CLEAN)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_clean(lsn, record);
|
2004-07-11 20:01:45 +02:00
|
|
|
else if (info == XLOG_HEAP_NEWPAGE)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_newpage(lsn, record);
|
2005-04-28 23:47:18 +02:00
|
|
|
else if (info == XLOG_HEAP_LOCK)
|
2005-06-06 19:01:25 +02:00
|
|
|
heap_xlog_lock(lsn, record);
|
2000-10-20 13:01:21 +02:00
|
|
|
else
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(PANIC, "heap_redo: unknown op code %u", info);
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
static void
|
|
|
|
out_target(char *buf, xl_heaptid *target)
|
|
|
|
{
|
2004-06-18 08:14:31 +02:00
|
|
|
sprintf(buf + strlen(buf), "rel %u/%u/%u; tid %u/%u",
|
2004-08-29 07:07:03 +02:00
|
|
|
target->node.spcNode, target->node.dbNode, target->node.relNode,
|
2001-03-22 05:01:46 +01:00
|
|
|
ItemPointerGetBlockNumber(&(target->tid)),
|
|
|
|
ItemPointerGetOffsetNumber(&(target->tid)));
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
void
|
2001-03-22 05:01:46 +01:00
|
|
|
heap_desc(char *buf, uint8 xl_info, char *rec)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
uint8 info = xl_info & ~XLR_INFO_MASK;
|
2000-10-21 17:43:36 +02:00
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
info &= XLOG_HEAP_OPMASK;
|
2000-10-21 17:43:36 +02:00
|
|
|
if (info == XLOG_HEAP_INSERT)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_insert *xlrec = (xl_heap_insert *) rec;
|
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
strcat(buf, "insert: ");
|
|
|
|
out_target(buf, &(xlrec->target));
|
|
|
|
}
|
|
|
|
else if (info == XLOG_HEAP_DELETE)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_delete *xlrec = (xl_heap_delete *) rec;
|
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
strcat(buf, "delete: ");
|
|
|
|
out_target(buf, &(xlrec->target));
|
|
|
|
}
|
2000-10-24 11:56:23 +02:00
|
|
|
else if (info == XLOG_HEAP_UPDATE || info == XLOG_HEAP_MOVE)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_update *xlrec = (xl_heap_update *) rec;
|
|
|
|
|
2000-10-24 11:56:23 +02:00
|
|
|
if (info == XLOG_HEAP_UPDATE)
|
|
|
|
strcat(buf, "update: ");
|
|
|
|
else
|
|
|
|
strcat(buf, "move: ");
|
2000-10-21 17:43:36 +02:00
|
|
|
out_target(buf, &(xlrec->target));
|
|
|
|
sprintf(buf + strlen(buf), "; new %u/%u",
|
2001-03-22 05:01:46 +01:00
|
|
|
ItemPointerGetBlockNumber(&(xlrec->newtid)),
|
|
|
|
ItemPointerGetOffsetNumber(&(xlrec->newtid)));
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2000-12-30 16:19:57 +01:00
|
|
|
else if (info == XLOG_HEAP_CLEAN)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
xl_heap_clean *xlrec = (xl_heap_clean *) rec;
|
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
sprintf(buf + strlen(buf), "clean: rel %u/%u/%u; blk %u",
|
|
|
|
xlrec->node.spcNode, xlrec->node.dbNode,
|
|
|
|
xlrec->node.relNode, xlrec->block);
|
2000-12-30 16:19:57 +01:00
|
|
|
}
|
2004-07-11 20:01:45 +02:00
|
|
|
else if (info == XLOG_HEAP_NEWPAGE)
|
|
|
|
{
|
|
|
|
xl_heap_newpage *xlrec = (xl_heap_newpage *) rec;
|
|
|
|
|
|
|
|
sprintf(buf + strlen(buf), "newpage: rel %u/%u/%u; blk %u",
|
|
|
|
xlrec->node.spcNode, xlrec->node.dbNode,
|
|
|
|
xlrec->node.relNode, xlrec->blkno);
|
|
|
|
}
|
2005-04-28 23:47:18 +02:00
|
|
|
else if (info == XLOG_HEAP_LOCK)
|
|
|
|
{
|
|
|
|
xl_heap_lock *xlrec = (xl_heap_lock *) rec;
|
|
|
|
|
|
|
|
if (xlrec->shared_lock)
|
|
|
|
strcat(buf, "shared_lock: ");
|
|
|
|
else
|
|
|
|
strcat(buf, "exclusive_lock: ");
|
2005-06-08 17:50:28 +02:00
|
|
|
if (xlrec->xid_is_mxact)
|
|
|
|
strcat(buf, "mxid ");
|
|
|
|
else
|
|
|
|
strcat(buf, "xid ");
|
|
|
|
sprintf(buf + strlen(buf), "%u ", xlrec->locking_xid);
|
2005-04-28 23:47:18 +02:00
|
|
|
out_target(buf, &(xlrec->target));
|
|
|
|
}
|
2000-10-21 17:43:36 +02:00
|
|
|
else
|
|
|
|
strcat(buf, "UNKNOWN");
|
|
|
|
}
|