2004-07-01 02:52:04 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* subtrans.c
|
2004-08-24 01:22:45 +02:00
|
|
|
* PostgreSQL subtransaction-log manager
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
2004-08-24 01:22:45 +02:00
|
|
|
* The pg_subtrans manager is a pg_clog-like manager that stores the parent
|
2004-07-01 02:52:04 +02:00
|
|
|
* transaction Id for each transaction. It is a fundamental part of the
|
2004-08-29 07:07:03 +02:00
|
|
|
* nested transactions implementation. A main transaction has a parent
|
2004-07-01 02:52:04 +02:00
|
|
|
* of InvalidTransactionId, and each subtransaction has its immediate parent.
|
|
|
|
* The tree can easily be walked from child to parent, but not in the
|
|
|
|
* opposite direction.
|
|
|
|
*
|
2004-08-24 01:22:45 +02:00
|
|
|
* This code is based on clog.c, but the robustness requirements
|
|
|
|
* are completely different from pg_clog, because we only need to remember
|
|
|
|
* pg_subtrans information for currently-open transactions. Thus, there is
|
|
|
|
* no need to preserve data over a crash and restart.
|
|
|
|
*
|
|
|
|
* There are no XLOG interactions since we do not care about preserving
|
|
|
|
* data across crashes. During database startup, we simply force the
|
|
|
|
* currently-active page of SUBTRANS to zeroes.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2004-07-01 02:52:04 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2006-07-13 18:49:20 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.17 2006/07/13 16:49:13 momjian Exp $
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/slru.h"
|
|
|
|
#include "access/subtrans.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
2004-08-22 04:41:58 +02:00
|
|
|
#include "utils/tqual.h"
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Defines for SubTrans page sizes. A page is the same BLCKSZ as is used
|
|
|
|
* everywhere else in Postgres.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
|
|
|
* Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
|
|
|
|
* SubTrans page numbering also wraps around at
|
|
|
|
* 0xFFFFFFFF/SUBTRANS_XACTS_PER_PAGE, and segment numbering at
|
|
|
|
* 0xFFFFFFFF/SUBTRANS_XACTS_PER_PAGE/SLRU_SEGMENTS_PER_PAGE. We need take no
|
|
|
|
* explicit notice of that fact in this module, except when comparing segment
|
2004-08-24 01:22:45 +02:00
|
|
|
* and page numbers in TruncateSUBTRANS (see SubTransPagePrecedes).
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* We need four bytes per xact */
|
|
|
|
#define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
|
|
|
|
|
|
|
|
#define TransactionIdToPage(xid) ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)
|
|
|
|
#define TransactionIdToEntry(xid) ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
|
|
|
|
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/*
|
|
|
|
* Link to shared-memory data structures for SUBTRANS control
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
static SlruCtlData SubTransCtlData;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
#define SubTransCtl (&SubTransCtlData)
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
static int ZeroSUBTRANSPage(int pageno);
|
2004-07-01 02:52:04 +02:00
|
|
|
static bool SubTransPagePrecedes(int page1, int page2);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record the parent of a subtransaction in the subtrans log.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SubTransSetParent(TransactionId xid, TransactionId parent)
|
|
|
|
{
|
|
|
|
int pageno = TransactionIdToPage(xid);
|
|
|
|
int entryno = TransactionIdToEntry(xid);
|
2004-08-24 01:22:45 +02:00
|
|
|
int slotno;
|
2004-07-01 02:52:04 +02:00
|
|
|
TransactionId *ptr;
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
slotno = SimpleLruReadPage(SubTransCtl, pageno, xid);
|
|
|
|
ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
|
2004-07-01 02:52:04 +02:00
|
|
|
ptr += entryno;
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/* Current state should be 0 */
|
|
|
|
Assert(*ptr == InvalidTransactionId);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
*ptr = parent;
|
|
|
|
|
2005-11-05 22:19:47 +01:00
|
|
|
SubTransCtl->shared->page_dirty[slotno] = true;
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(SubtransControlLock);
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interrogate the parent of a transaction in the subtrans log.
|
|
|
|
*/
|
|
|
|
TransactionId
|
|
|
|
SubTransGetParent(TransactionId xid)
|
|
|
|
{
|
|
|
|
int pageno = TransactionIdToPage(xid);
|
|
|
|
int entryno = TransactionIdToEntry(xid);
|
2004-08-24 01:22:45 +02:00
|
|
|
int slotno;
|
2004-07-01 02:52:04 +02:00
|
|
|
TransactionId *ptr;
|
2004-08-29 07:07:03 +02:00
|
|
|
TransactionId parent;
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-22 04:41:58 +02:00
|
|
|
/* Can't ask about stuff that might not be around anymore */
|
2004-09-16 20:35:23 +02:00
|
|
|
Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
|
2004-08-22 04:41:58 +02:00
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
/* Bootstrap and frozen XIDs have no parent */
|
|
|
|
if (!TransactionIdIsNormal(xid))
|
|
|
|
return InvalidTransactionId;
|
|
|
|
|
2005-12-06 19:10:06 +01:00
|
|
|
/* lock is acquired by SimpleLruReadPage_ReadOnly */
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2005-12-06 19:10:06 +01:00
|
|
|
slotno = SimpleLruReadPage_ReadOnly(SubTransCtl, pageno, xid);
|
2004-08-24 01:22:45 +02:00
|
|
|
ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
|
2004-07-01 02:52:04 +02:00
|
|
|
ptr += entryno;
|
|
|
|
|
|
|
|
parent = *ptr;
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(SubtransControlLock);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SubTransGetTopmostTransaction
|
|
|
|
*
|
|
|
|
* Returns the topmost transaction of the given transaction id.
|
2004-08-22 04:41:58 +02:00
|
|
|
*
|
2004-09-16 20:35:23 +02:00
|
|
|
* Because we cannot look back further than TransactionXmin, it is possible
|
2004-08-22 04:41:58 +02:00
|
|
|
* that this function will lie and return an intermediate subtransaction ID
|
|
|
|
* instead of the true topmost parent ID. This is OK, because in practice
|
|
|
|
* we only care about detecting whether the topmost parent is still running
|
|
|
|
* or is part of a current snapshot's list of still-running transactions.
|
2004-09-16 20:35:23 +02:00
|
|
|
* Therefore, any XID before TransactionXmin is as good as any other.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
TransactionId
|
|
|
|
SubTransGetTopmostTransaction(TransactionId xid)
|
|
|
|
{
|
|
|
|
TransactionId parentXid = xid,
|
2004-08-29 07:07:03 +02:00
|
|
|
previousXid = xid;
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-22 04:41:58 +02:00
|
|
|
/* Can't ask about stuff that might not be around anymore */
|
2004-09-16 20:35:23 +02:00
|
|
|
Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
|
2004-08-22 04:41:58 +02:00
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
while (TransactionIdIsValid(parentXid))
|
|
|
|
{
|
|
|
|
previousXid = parentXid;
|
2004-09-16 20:35:23 +02:00
|
|
|
if (TransactionIdPrecedes(parentXid, TransactionXmin))
|
2004-08-22 04:41:58 +02:00
|
|
|
break;
|
2004-07-01 02:52:04 +02:00
|
|
|
parentXid = SubTransGetParent(parentXid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(TransactionIdIsValid(previousXid));
|
|
|
|
|
|
|
|
return previousXid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Initialization of shared memory for SUBTRANS
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2004-07-01 02:52:04 +02:00
|
|
|
SUBTRANSShmemSize(void)
|
|
|
|
{
|
2005-12-07 00:08:34 +01:00
|
|
|
return SimpleLruShmemSize(NUM_SUBTRANS_BUFFERS);
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SUBTRANSShmemInit(void)
|
|
|
|
{
|
|
|
|
SubTransCtl->PagePrecedes = SubTransPagePrecedes;
|
2005-12-07 00:08:34 +01:00
|
|
|
SimpleLruInit(SubTransCtl, "SUBTRANS Ctl", NUM_SUBTRANS_BUFFERS,
|
2004-08-24 01:22:45 +02:00
|
|
|
SubtransControlLock, "pg_subtrans");
|
|
|
|
/* Override default assumption that writes should be fsync'd */
|
|
|
|
SubTransCtl->do_fsync = false;
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This func must be called ONCE on system install. It creates
|
2004-08-24 01:22:45 +02:00
|
|
|
* the initial SUBTRANS segment. (The SUBTRANS directory is assumed to
|
|
|
|
* have been created by the initdb shell script, and SUBTRANSShmemInit
|
|
|
|
* must have been called already.)
|
|
|
|
*
|
|
|
|
* Note: it's not really necessary to create the initial segment now,
|
2004-08-29 07:07:03 +02:00
|
|
|
* since slru.c would create it on first write anyway. But we may as well
|
2004-08-24 01:22:45 +02:00
|
|
|
* do it to be sure the directory is set up correctly.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
BootStrapSUBTRANS(void)
|
|
|
|
{
|
|
|
|
int slotno;
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/* Create and zero the first page of the subtrans log */
|
|
|
|
slotno = ZeroSUBTRANSPage(0);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
/* Make sure it's written out */
|
|
|
|
SimpleLruWritePage(SubTransCtl, slotno, NULL);
|
2005-11-05 22:19:47 +01:00
|
|
|
Assert(!SubTransCtl->shared->page_dirty[slotno]);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(SubtransControlLock);
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Initialize (or reinitialize) a page of SUBTRANS to zeroes.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
|
|
|
* The page is not actually written, just set up in shared memory.
|
|
|
|
* The slot number of the new page is returned.
|
|
|
|
*
|
|
|
|
* Control lock must be held at entry, and will be held at exit.
|
|
|
|
*/
|
|
|
|
static int
|
2004-08-24 01:22:45 +02:00
|
|
|
ZeroSUBTRANSPage(int pageno)
|
2004-07-01 02:52:04 +02:00
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
return SimpleLruZeroPage(SubTransCtl, pageno);
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be called ONCE during postmaster or standalone-backend startup,
|
|
|
|
* after StartupXLOG has initialized ShmemVariableCache->nextXid.
|
2005-06-18 00:32:51 +02:00
|
|
|
*
|
|
|
|
* oldestActiveXID is the oldest XID of any prepared transaction, or nextXid
|
|
|
|
* if there are none.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
void
|
2005-06-18 00:32:51 +02:00
|
|
|
StartupSUBTRANS(TransactionId oldestActiveXID)
|
2004-07-01 02:52:04 +02:00
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
int startPage;
|
2005-06-18 00:32:51 +02:00
|
|
|
int endPage;
|
2004-08-24 01:22:45 +02:00
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Since we don't expect pg_subtrans to be valid across crashes, we
|
2005-06-18 00:32:51 +02:00
|
|
|
* initialize the currently-active page(s) to zeroes during startup.
|
2005-10-15 04:49:52 +02:00
|
|
|
* Whenever we advance into a new page, ExtendSUBTRANS will likewise zero
|
|
|
|
* the new page without regard to whatever was previously on disk.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
|
|
|
|
|
2005-06-18 00:32:51 +02:00
|
|
|
startPage = TransactionIdToPage(oldestActiveXID);
|
|
|
|
endPage = TransactionIdToPage(ShmemVariableCache->nextXid);
|
|
|
|
|
|
|
|
while (startPage != endPage)
|
|
|
|
{
|
|
|
|
(void) ZeroSUBTRANSPage(startPage);
|
|
|
|
startPage++;
|
|
|
|
}
|
2004-08-24 01:22:45 +02:00
|
|
|
(void) ZeroSUBTRANSPage(startPage);
|
|
|
|
|
|
|
|
LWLockRelease(SubtransControlLock);
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be called ONCE during postmaster or standalone-backend shutdown
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ShutdownSUBTRANS(void)
|
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
/*
|
|
|
|
* Flush dirty SUBTRANS pages to disk
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* This is not actually necessary from a correctness point of view. We do
|
|
|
|
* it merely as a debugging aid.
|
2004-08-24 01:22:45 +02:00
|
|
|
*/
|
2004-07-01 02:52:04 +02:00
|
|
|
SimpleLruFlush(SubTransCtl, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a checkpoint --- either during shutdown, or on-the-fly
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CheckPointSUBTRANS(void)
|
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
/*
|
|
|
|
* Flush dirty SUBTRANS pages to disk
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* This is not actually necessary from a correctness point of view. We do
|
|
|
|
* it merely to improve the odds that writing of dirty pages is done by
|
|
|
|
* the checkpoint process and not by backends.
|
2004-08-24 01:22:45 +02:00
|
|
|
*/
|
2004-07-01 02:52:04 +02:00
|
|
|
SimpleLruFlush(SubTransCtl, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Make sure that SUBTRANS has room for a newly-allocated XID.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
|
|
|
* NB: this is called while holding XidGenLock. We want it to be very fast
|
|
|
|
* most of the time; even when it's not so fast, no actual I/O need happen
|
2004-08-24 01:22:45 +02:00
|
|
|
* unless we're forced to write out a dirty subtrans page to make room
|
2004-07-01 02:52:04 +02:00
|
|
|
* in shared memory.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExtendSUBTRANS(TransactionId newestXact)
|
|
|
|
{
|
|
|
|
int pageno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No work except at first XID of a page. But beware: just after
|
|
|
|
* wraparound, the first XID of page zero is FirstNormalTransactionId.
|
|
|
|
*/
|
|
|
|
if (TransactionIdToEntry(newestXact) != 0 &&
|
|
|
|
!TransactionIdEquals(newestXact, FirstNormalTransactionId))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pageno = TransactionIdToPage(newestXact);
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/* Zero the page */
|
|
|
|
ZeroSUBTRANSPage(pageno);
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(SubtransControlLock);
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Remove all SUBTRANS segments before the one holding the passed transaction ID
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
2004-08-24 01:22:45 +02:00
|
|
|
* This is normally called during checkpoint, with oldestXact being the
|
2004-09-16 20:35:23 +02:00
|
|
|
* oldest TransactionXmin of any running transaction.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
TruncateSUBTRANS(TransactionId oldestXact)
|
|
|
|
{
|
|
|
|
int cutoffPage;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The cutoff point is the start of the segment containing oldestXact. We
|
|
|
|
* pass the *page* containing oldestXact to SimpleLruTruncate.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
cutoffPage = TransactionIdToPage(oldestXact);
|
2004-08-24 01:22:45 +02:00
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
SimpleLruTruncate(SubTransCtl, cutoffPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Decide which of two SUBTRANS page numbers is "older" for truncation purposes.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
|
|
|
* We need to use comparison of TransactionIds here in order to do the right
|
|
|
|
* thing with wraparound XID arithmetic. However, if we are asked about
|
|
|
|
* page number zero, we don't want to hand InvalidTransactionId to
|
|
|
|
* TransactionIdPrecedes: it'll get weird about permanent xact IDs. So,
|
|
|
|
* offset both xids by FirstNormalTransactionId to avoid that.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
SubTransPagePrecedes(int page1, int page2)
|
|
|
|
{
|
|
|
|
TransactionId xid1;
|
|
|
|
TransactionId xid2;
|
|
|
|
|
|
|
|
xid1 = ((TransactionId) page1) * SUBTRANS_XACTS_PER_PAGE;
|
|
|
|
xid1 += FirstNormalTransactionId;
|
|
|
|
xid2 = ((TransactionId) page2) * SUBTRANS_XACTS_PER_PAGE;
|
|
|
|
xid2 += FirstNormalTransactionId;
|
|
|
|
|
|
|
|
return TransactionIdPrecedes(xid1, xid2);
|
|
|
|
}
|