1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* smgr.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* public interface routines to storage manager switch.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* All file system operations in POSTGRES dispatch through these
|
|
|
|
* routines.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, 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
|
2004-07-11 21:52:52 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/smgr/smgr.c,v 1.76 2004/07/11 19:52:51 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-07-11 21:52:52 +02:00
|
|
|
#include "commands/tablespace.h"
|
2000-11-08 23:10:03 +01:00
|
|
|
#include "storage/bufmgr.h"
|
2001-06-28 01:31:40 +02:00
|
|
|
#include "storage/freespace.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/ipc.h"
|
1996-11-03 06:08:01 +01:00
|
|
|
#include "storage/smgr.h"
|
2004-02-10 02:55:27 +01:00
|
|
|
#include "utils/hsearch.h"
|
2000-11-08 23:10:03 +01:00
|
|
|
#include "utils/memutils.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
|
|
|
* This struct of function pointers defines the API between smgr.c and
|
|
|
|
* any individual storage manager module. Note that smgr subfunctions are
|
|
|
|
* generally expected to return TRUE on success, FALSE on error. (For
|
|
|
|
* nblocks and truncate we instead say that returning InvalidBlockNumber
|
|
|
|
* indicates an error.)
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct f_smgr
|
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
bool (*smgr_init) (void); /* may be NULL */
|
|
|
|
bool (*smgr_shutdown) (void); /* may be NULL */
|
|
|
|
bool (*smgr_close) (SMgrRelation reln);
|
|
|
|
bool (*smgr_create) (SMgrRelation reln, bool isRedo);
|
|
|
|
bool (*smgr_unlink) (RelFileNode rnode, bool isRedo);
|
|
|
|
bool (*smgr_extend) (SMgrRelation reln, BlockNumber blocknum,
|
2004-05-31 22:31:33 +02:00
|
|
|
char *buffer, bool isTemp);
|
2004-02-10 02:55:27 +01:00
|
|
|
bool (*smgr_read) (SMgrRelation reln, BlockNumber blocknum,
|
2004-05-31 22:31:33 +02:00
|
|
|
char *buffer);
|
2004-02-10 02:55:27 +01:00
|
|
|
bool (*smgr_write) (SMgrRelation reln, BlockNumber blocknum,
|
2004-05-31 22:31:33 +02:00
|
|
|
char *buffer, bool isTemp);
|
2004-02-10 02:55:27 +01:00
|
|
|
BlockNumber (*smgr_nblocks) (SMgrRelation reln);
|
2004-05-31 22:31:33 +02:00
|
|
|
BlockNumber (*smgr_truncate) (SMgrRelation reln, BlockNumber nblocks,
|
|
|
|
bool isTemp);
|
2004-06-02 19:28:18 +02:00
|
|
|
bool (*smgr_immedsync) (SMgrRelation reln);
|
2004-02-10 02:55:27 +01:00
|
|
|
bool (*smgr_commit) (void); /* may be NULL */
|
|
|
|
bool (*smgr_abort) (void); /* may be NULL */
|
|
|
|
bool (*smgr_sync) (void); /* may be NULL */
|
1997-09-08 23:56:23 +02:00
|
|
|
} f_smgr;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
static const f_smgr smgrsw[] = {
|
1997-09-07 07:04:48 +02:00
|
|
|
/* magnetic disk */
|
2004-02-10 02:55:27 +01:00
|
|
|
{mdinit, NULL, mdclose, mdcreate, mdunlink, mdextend,
|
2004-06-02 19:28:18 +02:00
|
|
|
mdread, mdwrite, mdnblocks, mdtruncate, mdimmedsync,
|
|
|
|
NULL, NULL, mdsync
|
2004-02-10 02:55:27 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
static const int NSmgr = lengthof(smgrsw);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-10-08 20:30:52 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
|
|
|
* Each backend has a hashtable that stores all extant SMgrRelation objects.
|
|
|
|
*/
|
|
|
|
static HTAB *SMgrRelationHash = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/*
|
|
|
|
* We keep a list of all relations (represented as RelFileNode values)
|
|
|
|
* that have been created or deleted in the current transaction. When
|
|
|
|
* a relation is created, we create the physical file immediately, but
|
|
|
|
* remember it so that we can delete the file again if the current
|
2001-03-22 05:01:46 +01:00
|
|
|
* transaction is aborted. Conversely, a deletion request is NOT
|
2000-11-08 23:10:03 +01:00
|
|
|
* executed immediately, but is just entered in the list. When and if
|
|
|
|
* the transaction commits, we can delete the physical file.
|
|
|
|
*
|
2004-07-01 02:52:04 +02:00
|
|
|
* The list is kept in CurTransactionContext. In subtransactions, each
|
|
|
|
* subtransaction has its own list in its own CurTransactionContext, but
|
|
|
|
* successful subtransactions attach their lists to their parent's list.
|
|
|
|
* Failed subtransactions can immediately execute the abort-time actions.
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct PendingRelDelete
|
|
|
|
{
|
|
|
|
RelFileNode relnode; /* relation that may need to be deleted */
|
2004-02-10 02:55:27 +01:00
|
|
|
int which; /* which storage manager? */
|
2002-08-06 04:36:35 +02:00
|
|
|
bool isTemp; /* is it a temporary relation? */
|
2001-03-22 05:01:46 +01:00
|
|
|
bool atCommit; /* T=delete at commit; F=delete at abort */
|
2000-11-08 23:10:03 +01:00
|
|
|
} PendingRelDelete;
|
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
static List *pendingDeletes = NIL; /* head of linked list */
|
|
|
|
|
|
|
|
static List *upperPendingDeletes = NIL; /* list of upper-xact lists */
|
2000-11-08 23:10:03 +01:00
|
|
|
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
|
|
|
* Declarations for smgr-related XLOG records
|
|
|
|
*
|
|
|
|
* Note: we log file creation and truncation here, but logging of deletion
|
|
|
|
* actions is handled by xact.c, because it is part of transaction commit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* XLOG gives us high 4 bits */
|
|
|
|
#define XLOG_SMGR_CREATE 0x10
|
|
|
|
#define XLOG_SMGR_TRUNCATE 0x20
|
|
|
|
|
|
|
|
typedef struct xl_smgr_create
|
|
|
|
{
|
|
|
|
RelFileNode rnode;
|
|
|
|
} xl_smgr_create;
|
|
|
|
|
|
|
|
typedef struct xl_smgr_truncate
|
|
|
|
{
|
|
|
|
BlockNumber blkno;
|
|
|
|
RelFileNode rnode;
|
|
|
|
} xl_smgr_truncate;
|
|
|
|
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/* local function prototypes */
|
|
|
|
static void smgrshutdown(int code, Datum arg);
|
|
|
|
static void smgr_internal_unlink(RelFileNode rnode, int which,
|
|
|
|
bool isTemp, bool isRedo);
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* smgrinit(), smgrshutdown() -- Initialize or shut down all storage
|
|
|
|
* managers.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-10 02:55:27 +01:00
|
|
|
* Note: in the normal multiprocess scenario with a postmaster, these are
|
|
|
|
* called at postmaster start and stop, not per-backend.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2002-08-06 04:36:35 +02:00
|
|
|
smgrinit(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_init)
|
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[i].smgr_init)) ())
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(FATAL, "smgr initialization failed on %s: %m",
|
2000-06-05 09:29:25 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(smgrout,
|
2001-03-22 05:01:46 +01:00
|
|
|
Int16GetDatum(i))));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* register the shutdown proc */
|
2000-10-02 21:42:56 +02:00
|
|
|
on_proc_exit(smgrshutdown, 0);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
2003-12-12 19:45:10 +01:00
|
|
|
smgrshutdown(int code, Datum arg)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_shutdown)
|
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[i].smgr_shutdown)) ())
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(FATAL, "smgr shutdown failed on %s: %m",
|
2000-06-05 09:29:25 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(smgrout,
|
2001-03-22 05:01:46 +01:00
|
|
|
Int16GetDatum(i))));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
|
|
|
* smgropen() -- Return an SMgrRelation object, creating it if need be.
|
|
|
|
*
|
|
|
|
* This does not attempt to actually open the object.
|
|
|
|
*/
|
|
|
|
SMgrRelation
|
|
|
|
smgropen(RelFileNode rnode)
|
|
|
|
{
|
|
|
|
SMgrRelation reln;
|
|
|
|
bool found;
|
|
|
|
|
|
|
|
if (SMgrRelationHash == NULL)
|
|
|
|
{
|
|
|
|
/* First time through: initialize the hash table */
|
|
|
|
HASHCTL ctl;
|
|
|
|
|
|
|
|
MemSet(&ctl, 0, sizeof(ctl));
|
|
|
|
ctl.keysize = sizeof(RelFileNode);
|
|
|
|
ctl.entrysize = sizeof(SMgrRelationData);
|
|
|
|
ctl.hash = tag_hash;
|
|
|
|
SMgrRelationHash = hash_create("smgr relation table", 400,
|
|
|
|
&ctl, HASH_ELEM | HASH_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up or create an entry */
|
|
|
|
reln = (SMgrRelation) hash_search(SMgrRelationHash,
|
|
|
|
(void *) &rnode,
|
|
|
|
HASH_ENTER, &found);
|
|
|
|
if (reln == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
|
|
|
|
|
|
|
/* Initialize it if not present before */
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* hash_search already filled in the lookup key */
|
|
|
|
reln->smgr_which = 0; /* we only have md.c at present */
|
|
|
|
reln->md_fd = NULL; /* mark it not open */
|
|
|
|
}
|
|
|
|
|
|
|
|
return reln;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* smgrclose() -- Close and delete an SMgrRelation object.
|
|
|
|
*
|
|
|
|
* It is the caller's responsibility not to leave any dangling references
|
|
|
|
* to the object. (Pointers should be cleared after successful return;
|
|
|
|
* on the off chance of failure, the SMgrRelation object will still exist.)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrclose(SMgrRelation reln)
|
|
|
|
{
|
|
|
|
if (! (*(smgrsw[reln->smgr_which].smgr_close)) (reln))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not close relation %u/%u/%u: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode)));
|
|
|
|
|
|
|
|
if (hash_search(SMgrRelationHash,
|
|
|
|
(void *) &(reln->smgr_rnode),
|
|
|
|
HASH_REMOVE, NULL) == NULL)
|
|
|
|
elog(ERROR, "SMgrRelation hashtable corrupted");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* smgrcloseall() -- Close all existing SMgrRelation objects.
|
|
|
|
*
|
|
|
|
* It is the caller's responsibility not to leave any dangling references.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrcloseall(void)
|
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
SMgrRelation reln;
|
|
|
|
|
|
|
|
/* Nothing to do if hashtable not set up */
|
|
|
|
if (SMgrRelationHash == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hash_seq_init(&status, SMgrRelationHash);
|
|
|
|
|
|
|
|
while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
smgrclose(reln);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* smgrclosenode() -- Close SMgrRelation object for given RelFileNode,
|
|
|
|
* if one exists.
|
|
|
|
*
|
|
|
|
* This has the same effects as smgrclose(smgropen(rnode)), but it avoids
|
|
|
|
* uselessly creating a hashtable entry only to drop it again when no
|
|
|
|
* such entry exists already.
|
|
|
|
*
|
|
|
|
* It is the caller's responsibility not to leave any dangling references.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrclosenode(RelFileNode rnode)
|
|
|
|
{
|
|
|
|
SMgrRelation reln;
|
|
|
|
|
|
|
|
/* Nothing to do if hashtable not set up */
|
|
|
|
if (SMgrRelationHash == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
reln = (SMgrRelation) hash_search(SMgrRelationHash,
|
|
|
|
(void *) &rnode,
|
|
|
|
HASH_FIND, NULL);
|
|
|
|
if (reln != NULL)
|
|
|
|
smgrclose(reln);
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* smgrcreate() -- Create a new relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-10 02:55:27 +01:00
|
|
|
* Given an already-created (but presumably unused) SMgrRelation,
|
|
|
|
* cause the underlying disk file or other storage to be created.
|
|
|
|
*
|
|
|
|
* If isRedo is true, it is okay for the underlying file to exist
|
|
|
|
* already because we are in a WAL replay sequence. In this case
|
|
|
|
* we should make no PendingRelDelete entry; the WAL sequence will
|
|
|
|
* tell whether to drop the file.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
|
|
|
smgrcreate(SMgrRelation reln, bool isTemp, bool isRedo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-02-11 23:55:26 +01:00
|
|
|
XLogRecPtr lsn;
|
|
|
|
XLogRecData rdata;
|
|
|
|
xl_smgr_create xlrec;
|
2000-11-08 23:10:03 +01:00
|
|
|
PendingRelDelete *pending;
|
2004-07-01 02:52:04 +02:00
|
|
|
MemoryContext old_cxt;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-07-11 21:52:52 +02:00
|
|
|
/*
|
|
|
|
* We may be using the target table space for the first time in this
|
|
|
|
* database, so create a per-database subdirectory if needed.
|
|
|
|
*
|
|
|
|
* XXX this is a fairly ugly violation of module layering, but this seems
|
|
|
|
* to be the best place to put the check. Maybe TablespaceCreateDbspace
|
|
|
|
* should be here and not in commands/tablespace.c? But that would imply
|
|
|
|
* importing a lot of stuff that smgr.c oughtn't know, either.
|
|
|
|
*/
|
|
|
|
TablespaceCreateDbspace(reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
|
|
|
isRedo);
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[reln->smgr_which].smgr_create)) (reln, isRedo))
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not create relation %u/%u/%u: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode)));
|
|
|
|
|
|
|
|
if (isRedo)
|
|
|
|
return;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
|
|
|
* Make a non-transactional XLOG entry showing the file creation. It's
|
|
|
|
* non-transactional because we should replay it whether the transaction
|
|
|
|
* commits or not; if not, the file will be dropped at abort time.
|
|
|
|
*/
|
|
|
|
xlrec.rnode = reln->smgr_rnode;
|
|
|
|
|
|
|
|
rdata.buffer = InvalidBuffer;
|
|
|
|
rdata.data = (char *) &xlrec;
|
|
|
|
rdata.len = sizeof(xlrec);
|
|
|
|
rdata.next = NULL;
|
|
|
|
|
|
|
|
lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_CREATE | XLOG_NO_TRAN, &rdata);
|
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/* Add the relation to the list of stuff to delete at abort */
|
2004-07-01 02:52:04 +02:00
|
|
|
old_cxt = MemoryContextSwitchTo(CurTransactionContext);
|
|
|
|
|
|
|
|
pending = (PendingRelDelete *) palloc(sizeof(PendingRelDelete));
|
2004-02-10 02:55:27 +01:00
|
|
|
pending->relnode = reln->smgr_rnode;
|
|
|
|
pending->which = reln->smgr_which;
|
|
|
|
pending->isTemp = isTemp;
|
2000-11-08 23:10:03 +01:00
|
|
|
pending->atCommit = false; /* delete if abort */
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
pendingDeletes = lcons(pending, pendingDeletes);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-10 02:55:27 +01:00
|
|
|
* smgrscheduleunlink() -- Schedule unlinking a relation at xact commit.
|
|
|
|
*
|
|
|
|
* The relation is marked to be removed from the store if we
|
|
|
|
* successfully commit the current transaction.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-10 02:55:27 +01:00
|
|
|
* This also implies smgrclose() on the SMgrRelation object.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
|
|
|
smgrscheduleunlink(SMgrRelation reln, bool isTemp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-11-08 23:10:03 +01:00
|
|
|
PendingRelDelete *pending;
|
2004-07-01 02:52:04 +02:00
|
|
|
MemoryContext old_cxt;
|
2000-11-08 23:10:03 +01:00
|
|
|
|
|
|
|
/* Add the relation to the list of stuff to delete at commit */
|
2004-07-01 02:52:04 +02:00
|
|
|
old_cxt = MemoryContextSwitchTo(CurTransactionContext);
|
|
|
|
|
|
|
|
pending = (PendingRelDelete *) palloc(sizeof(PendingRelDelete));
|
2004-02-10 02:55:27 +01:00
|
|
|
pending->relnode = reln->smgr_rnode;
|
|
|
|
pending->which = reln->smgr_which;
|
|
|
|
pending->isTemp = isTemp;
|
2000-11-08 23:10:03 +01:00
|
|
|
pending->atCommit = true; /* delete if commit */
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
pendingDeletes = lcons(pending, pendingDeletes);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
2000-11-08 23:10:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: if the relation was created in this transaction, it will now
|
2001-03-22 05:01:46 +01:00
|
|
|
* be present in the pending-delete list twice, once with atCommit
|
|
|
|
* true and once with atCommit false. Hence, it will be physically
|
|
|
|
* deleted at end of xact in either case (and the other entry will be
|
|
|
|
* ignored by smgrDoPendingDeletes, so no error will occur). We could
|
|
|
|
* instead remove the existing list entry and delete the physical file
|
2000-11-08 23:10:03 +01:00
|
|
|
* immediately, but for now I'll keep the logic simple.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/* Now close the file and throw away the hashtable entry */
|
|
|
|
smgrclose(reln);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-10 02:55:27 +01:00
|
|
|
* smgrdounlink() -- Immediately unlink a relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-10 02:55:27 +01:00
|
|
|
* The relation is removed from the store. This should not be used
|
|
|
|
* during transactional operations, since it can't be undone.
|
2001-05-10 22:38:49 +02:00
|
|
|
*
|
2004-02-10 02:55:27 +01:00
|
|
|
* If isRedo is true, it is okay for the underlying file to be gone
|
|
|
|
* already. (In practice isRedo will always be true.)
|
|
|
|
*
|
|
|
|
* This also implies smgrclose() on the SMgrRelation object.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
|
|
|
smgrdounlink(SMgrRelation reln, bool isTemp, bool isRedo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
RelFileNode rnode = reln->smgr_rnode;
|
|
|
|
int which = reln->smgr_which;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/* Close the file and throw away the hashtable entry */
|
|
|
|
smgrclose(reln);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
smgr_internal_unlink(rnode, which, isTemp, isRedo);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-10 02:55:27 +01:00
|
|
|
* Shared subroutine that actually does the unlink ...
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
static void
|
|
|
|
smgr_internal_unlink(RelFileNode rnode, int which, bool isTemp, bool isRedo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
|
|
|
* Get rid of any leftover buffers for the rel (shouldn't be any in the
|
|
|
|
* commit case, but there can be in the abort case).
|
|
|
|
*/
|
2004-05-31 05:48:10 +02:00
|
|
|
DropRelFileNodeBuffers(rnode, isTemp, 0);
|
2004-02-10 02:55:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the free space map to forget this relation. It won't be accessed
|
|
|
|
* any more anyway, but we may as well recycle the map space quickly.
|
|
|
|
*/
|
|
|
|
FreeSpaceMapForgetRel(&rnode);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And delete the physical files.
|
|
|
|
*
|
|
|
|
* Note: we treat deletion failure as a WARNING, not an error,
|
|
|
|
* because we've already decided to commit or abort the current xact.
|
|
|
|
*/
|
|
|
|
if (! (*(smgrsw[which].smgr_unlink)) (rnode, isRedo))
|
|
|
|
ereport(WARNING,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not unlink relation %u/%u/%u: %m",
|
|
|
|
rnode.spcNode,
|
|
|
|
rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
rnode.relNode)));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-10 02:55:27 +01:00
|
|
|
* smgrextend() -- Add a new block to a file.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-10 02:55:27 +01:00
|
|
|
* The semantics are basically the same as smgrwrite(): write at the
|
|
|
|
* specified position. However, we are expecting to extend the
|
|
|
|
* relation (ie, blocknum is the current EOF), and so in case of
|
|
|
|
* failure we clean up by truncating.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2004-05-31 22:31:33 +02:00
|
|
|
smgrextend(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-05-31 22:31:33 +02:00
|
|
|
if (! (*(smgrsw[reln->smgr_which].smgr_extend)) (reln, blocknum, buffer,
|
|
|
|
isTemp))
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not extend relation %u/%u/%u: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode),
|
|
|
|
errhint("Check free disk space.")));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* smgrread() -- read a particular block from a relation into the supplied
|
|
|
|
* buffer.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This routine is called from the buffer manager in order to
|
|
|
|
* instantiate pages in the shared buffer cache. All storage managers
|
2004-02-10 02:55:27 +01:00
|
|
|
* return pages in the format that POSTGRES expects.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
|
|
|
smgrread(SMgrRelation reln, BlockNumber blocknum, char *buffer)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[reln->smgr_which].smgr_read)) (reln, blocknum, buffer))
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not read block %u of relation %u/%u/%u: %m",
|
2004-02-10 02:55:27 +01:00
|
|
|
blocknum,
|
2004-06-18 08:14:31 +02:00
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode)));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* smgrwrite() -- Write the supplied buffer out.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-08-06 04:36:35 +02:00
|
|
|
* This is not a synchronous write -- the block is not necessarily
|
2004-05-31 22:31:33 +02:00
|
|
|
* on disk at return, only dumped out to the kernel. However,
|
|
|
|
* provisions will be made to fsync the write before the next checkpoint.
|
|
|
|
*
|
|
|
|
* isTemp indicates that the relation is a temp table (ie, is managed
|
|
|
|
* by the local-buffer manager). In this case no provisions need be
|
|
|
|
* made to fsync the write before checkpointing.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2004-05-31 22:31:33 +02:00
|
|
|
smgrwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp)
|
2000-10-16 16:52:28 +02:00
|
|
|
{
|
2004-05-31 22:31:33 +02:00
|
|
|
if (! (*(smgrsw[reln->smgr_which].smgr_write)) (reln, blocknum, buffer,
|
|
|
|
isTemp))
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not write block %u of relation %u/%u/%u: %m",
|
2004-02-10 02:55:27 +01:00
|
|
|
blocknum,
|
2004-06-18 08:14:31 +02:00
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode)));
|
2000-10-16 16:52:28 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2004-01-06 19:07:32 +01:00
|
|
|
* smgrnblocks() -- Calculate the number of blocks in the
|
1997-09-07 07:04:48 +02:00
|
|
|
* supplied relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the number of blocks on success, aborts the current
|
|
|
|
* transaction on failure.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-06-28 01:31:40 +02:00
|
|
|
BlockNumber
|
2004-02-10 02:55:27 +01:00
|
|
|
smgrnblocks(SMgrRelation reln)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber nblocks;
|
2001-06-29 23:08:25 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
nblocks = (*(smgrsw[reln->smgr_which].smgr_nblocks)) (reln);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-06-29 23:08:25 +02:00
|
|
|
/*
|
|
|
|
* NOTE: if a relation ever did grow to 2^32-1 blocks, this code would
|
|
|
|
* fail --- but that's a good thing, because it would stop us from
|
|
|
|
* extending the rel another block and having a block whose number
|
|
|
|
* actually is InvalidBlockNumber.
|
|
|
|
*/
|
|
|
|
if (nblocks == InvalidBlockNumber)
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not count blocks of relation %u/%u/%u: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode)));
|
2001-06-29 23:08:25 +02:00
|
|
|
|
|
|
|
return nblocks;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1996-11-27 08:25:52 +01:00
|
|
|
/*
|
2004-01-06 19:07:32 +01:00
|
|
|
* smgrtruncate() -- Truncate supplied relation to the specified number
|
|
|
|
* of blocks
|
1996-11-27 08:25:52 +01:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the number of blocks on success, aborts the current
|
|
|
|
* transaction on failure.
|
1996-11-27 08:25:52 +01:00
|
|
|
*/
|
2001-06-28 01:31:40 +02:00
|
|
|
BlockNumber
|
2004-05-31 22:31:33 +02:00
|
|
|
smgrtruncate(SMgrRelation reln, BlockNumber nblocks, bool isTemp)
|
1996-11-27 08:25:52 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber newblks;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
|
|
|
* Tell the free space map to forget anything it may have stored
|
|
|
|
* for the about-to-be-deleted blocks. We want to be sure it
|
|
|
|
* won't return bogus block numbers later on.
|
|
|
|
*/
|
|
|
|
FreeSpaceMapTruncateRel(&reln->smgr_rnode, nblocks);
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Do the truncation */
|
2004-05-31 22:31:33 +02:00
|
|
|
newblks = (*(smgrsw[reln->smgr_which].smgr_truncate)) (reln, nblocks,
|
|
|
|
isTemp);
|
2004-02-10 02:55:27 +01:00
|
|
|
if (newblks == InvalidBlockNumber)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not truncate relation %u/%u/%u to %u blocks: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_rnode.relNode,
|
|
|
|
nblocks)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-05-31 22:31:33 +02:00
|
|
|
if (!isTemp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Make a non-transactional XLOG entry showing the file truncation.
|
|
|
|
* It's non-transactional because we should replay it whether the
|
|
|
|
* transaction commits or not; the underlying file change is certainly
|
|
|
|
* not reversible.
|
|
|
|
*/
|
|
|
|
XLogRecPtr lsn;
|
|
|
|
XLogRecData rdata;
|
|
|
|
xl_smgr_truncate xlrec;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
2004-05-31 22:31:33 +02:00
|
|
|
xlrec.blkno = newblks;
|
|
|
|
xlrec.rnode = reln->smgr_rnode;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
2004-05-31 22:31:33 +02:00
|
|
|
rdata.buffer = InvalidBuffer;
|
|
|
|
rdata.data = (char *) &xlrec;
|
|
|
|
rdata.len = sizeof(xlrec);
|
|
|
|
rdata.next = NULL;
|
|
|
|
|
|
|
|
lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_TRUNCATE | XLOG_NO_TRAN,
|
|
|
|
&rdata);
|
|
|
|
}
|
2004-02-11 23:55:26 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return newblks;
|
1996-11-27 08:25:52 +01:00
|
|
|
}
|
|
|
|
|
2004-06-02 19:28:18 +02:00
|
|
|
/*
|
|
|
|
* smgrimmedsync() -- Force the specified relation to stable storage.
|
|
|
|
*
|
|
|
|
* Synchronously force all of the specified relation down to disk.
|
|
|
|
*
|
|
|
|
* This is really only useful for non-WAL-logged index building:
|
|
|
|
* instead of incrementally WAL-logging the index build steps,
|
|
|
|
* we can just write completed index pages to disk with smgrwrite
|
|
|
|
* or smgrextend, and then fsync the completed index file before
|
|
|
|
* committing the transaction. (This is sufficient for purposes of
|
|
|
|
* crash recovery, since it effectively duplicates forcing a checkpoint
|
|
|
|
* for the completed index. But it is *not* workable if one wishes
|
|
|
|
* to use the WAL log for PITR or replication purposes.)
|
|
|
|
*
|
|
|
|
* The preceding writes should specify isTemp = true to avoid
|
|
|
|
* duplicative fsyncs.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrimmedsync(SMgrRelation reln)
|
|
|
|
{
|
|
|
|
if (! (*(smgrsw[reln->smgr_which].smgr_immedsync)) (reln))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not sync relation %u/%u/%u: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-06-02 19:28:18 +02:00
|
|
|
reln->smgr_rnode.relNode)));
|
|
|
|
}
|
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/*
|
2004-01-06 19:07:32 +01:00
|
|
|
* smgrDoPendingDeletes() -- Take care of relation deletes at end of xact.
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2000-11-08 23:10:03 +01:00
|
|
|
smgrDoPendingDeletes(bool isCommit)
|
|
|
|
{
|
2004-07-01 02:52:04 +02:00
|
|
|
ListCell *p;
|
|
|
|
|
|
|
|
foreach(p, pendingDeletes)
|
2000-11-08 23:10:03 +01:00
|
|
|
{
|
2004-07-01 02:52:04 +02:00
|
|
|
PendingRelDelete *pending = lfirst(p);
|
2000-11-08 23:10:03 +01:00
|
|
|
|
|
|
|
if (pending->atCommit == isCommit)
|
2004-02-10 02:55:27 +01:00
|
|
|
smgr_internal_unlink(pending->relnode,
|
|
|
|
pending->which,
|
|
|
|
pending->isTemp,
|
|
|
|
false);
|
2000-11-08 23:10:03 +01:00
|
|
|
}
|
2004-07-01 02:52:04 +02:00
|
|
|
|
|
|
|
/* We needn't free the cells since they are in CurTransactionContext */
|
|
|
|
pendingDeletes = NIL;
|
2000-11-08 23:10:03 +01:00
|
|
|
}
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
|
|
|
* smgrGetPendingDeletes() -- Get a list of relations to be deleted.
|
|
|
|
*
|
|
|
|
* The return value is the number of relations scheduled for termination.
|
|
|
|
* *ptr is set to point to a freshly-palloc'd array of RelFileNodes.
|
|
|
|
* If there are no relations to be deleted, *ptr is set to NULL.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
|
|
|
* Note that the list does not include anything scheduled for termination
|
|
|
|
* by upper-level transactions.
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
smgrGetPendingDeletes(bool forCommit, RelFileNode **ptr)
|
|
|
|
{
|
|
|
|
int nrels;
|
|
|
|
RelFileNode *rptr;
|
2004-07-01 02:52:04 +02:00
|
|
|
ListCell *p;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
|
|
|
nrels = 0;
|
2004-07-01 02:52:04 +02:00
|
|
|
foreach(p, pendingDeletes)
|
2004-02-11 23:55:26 +01:00
|
|
|
{
|
2004-07-01 02:52:04 +02:00
|
|
|
PendingRelDelete *pending = lfirst(p);
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
if (pending->atCommit == forCommit)
|
|
|
|
nrels++;
|
|
|
|
}
|
|
|
|
if (nrels == 0)
|
|
|
|
{
|
|
|
|
*ptr = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rptr = (RelFileNode *) palloc(nrels * sizeof(RelFileNode));
|
|
|
|
*ptr = rptr;
|
2004-07-01 02:52:04 +02:00
|
|
|
foreach(p, pendingDeletes)
|
2004-02-11 23:55:26 +01:00
|
|
|
{
|
2004-07-01 02:52:04 +02:00
|
|
|
PendingRelDelete *pending = lfirst(p);
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
if (pending->atCommit == forCommit)
|
|
|
|
*rptr++ = pending->relnode;
|
|
|
|
}
|
|
|
|
return nrels;
|
|
|
|
}
|
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
/*
|
|
|
|
* AtSubStart_smgr() --- Take care of subtransaction start.
|
|
|
|
*
|
|
|
|
* Push empty state for the new subtransaction.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AtSubStart_smgr(void)
|
|
|
|
{
|
|
|
|
MemoryContext old_cxt;
|
|
|
|
|
|
|
|
/* Keep the list-of-lists in TopTransactionContext for simplicity */
|
|
|
|
old_cxt = MemoryContextSwitchTo(TopTransactionContext);
|
|
|
|
|
|
|
|
upperPendingDeletes = lcons(pendingDeletes, upperPendingDeletes);
|
|
|
|
|
|
|
|
pendingDeletes = NIL;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AtSubCommit_smgr() --- Take care of subtransaction commit.
|
|
|
|
*
|
|
|
|
* Reassign all items in the pending deletes list to the parent transaction.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AtSubCommit_smgr(void)
|
|
|
|
{
|
|
|
|
List *parentPendingDeletes;
|
|
|
|
|
|
|
|
parentPendingDeletes = (List *) linitial(upperPendingDeletes);
|
|
|
|
upperPendingDeletes = list_delete_first(upperPendingDeletes);
|
|
|
|
|
|
|
|
pendingDeletes = list_concat(parentPendingDeletes, pendingDeletes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AtSubAbort_smgr() --- Take care of subtransaction abort.
|
|
|
|
*
|
|
|
|
* Delete created relations and forget about deleted relations.
|
|
|
|
* We can execute these operations immediately because we know this
|
|
|
|
* subtransaction will not commit.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AtSubAbort_smgr(void)
|
|
|
|
{
|
|
|
|
smgrDoPendingDeletes(false);
|
|
|
|
|
|
|
|
/* Must pop the stack, too */
|
|
|
|
pendingDeletes = (List *) linitial(upperPendingDeletes);
|
|
|
|
upperPendingDeletes = list_delete_first(upperPendingDeletes);
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2002-08-06 04:36:35 +02:00
|
|
|
* smgrcommit() -- Prepare to commit changes made during the current
|
|
|
|
* transaction.
|
|
|
|
*
|
2004-01-06 19:07:32 +01:00
|
|
|
* This is called before we actually commit.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2002-08-06 04:36:35 +02:00
|
|
|
smgrcommit(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_commit)
|
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[i].smgr_commit)) ())
|
2004-05-31 05:48:10 +02:00
|
|
|
elog(ERROR, "transaction commit failed on %s: %m",
|
2000-06-05 09:29:25 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(smgrout,
|
2001-03-22 05:01:46 +01:00
|
|
|
Int16GetDatum(i))));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*
|
|
|
|
* smgrabort() -- Abort changes made during the current transaction.
|
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2002-08-06 04:36:35 +02:00
|
|
|
smgrabort(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_abort)
|
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[i].smgr_abort)) ())
|
2004-05-31 05:48:10 +02:00
|
|
|
elog(ERROR, "transaction abort failed on %s: %m",
|
2000-06-05 09:29:25 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(smgrout,
|
2001-03-22 05:01:46 +01:00
|
|
|
Int16GetDatum(i))));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*
|
2004-01-06 19:07:32 +01:00
|
|
|
* smgrsync() -- Sync files to disk at checkpoint time.
|
2002-08-06 04:36:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2002-08-06 04:36:35 +02:00
|
|
|
smgrsync(void)
|
2000-10-28 18:21:00 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_sync)
|
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
if (! (*(smgrsw[i].smgr_sync)) ())
|
2004-05-31 05:48:10 +02:00
|
|
|
elog(ERROR, "storage sync failed on %s: %m",
|
2000-10-28 18:21:00 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(smgrout,
|
2001-03-22 05:01:46 +01:00
|
|
|
Int16GetDatum(i))));
|
2000-10-28 18:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
smgr_redo(XLogRecPtr lsn, XLogRecord *record)
|
|
|
|
{
|
2004-02-11 23:55:26 +01:00
|
|
|
uint8 info = record->xl_info & ~XLR_INFO_MASK;
|
|
|
|
|
|
|
|
if (info == XLOG_SMGR_CREATE)
|
|
|
|
{
|
|
|
|
xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
|
|
|
|
SMgrRelation reln;
|
|
|
|
|
|
|
|
reln = smgropen(xlrec->rnode);
|
|
|
|
smgrcreate(reln, false, true);
|
|
|
|
}
|
|
|
|
else if (info == XLOG_SMGR_TRUNCATE)
|
|
|
|
{
|
|
|
|
xl_smgr_truncate *xlrec = (xl_smgr_truncate *) XLogRecGetData(record);
|
|
|
|
SMgrRelation reln;
|
|
|
|
BlockNumber newblks;
|
|
|
|
|
|
|
|
reln = smgropen(xlrec->rnode);
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*
|
|
|
|
* First, force bufmgr to drop any buffers it has for the to-be-
|
|
|
|
* truncated blocks. We must do this, else subsequent XLogReadBuffer
|
|
|
|
* operations will not re-extend the file properly.
|
|
|
|
*/
|
|
|
|
DropRelFileNodeBuffers(xlrec->rnode, false, xlrec->blkno);
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Can't use smgrtruncate because it would try to xlog */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the free space map to forget anything it may have stored
|
|
|
|
* for the about-to-be-deleted blocks. We want to be sure it
|
|
|
|
* won't return bogus block numbers later on.
|
|
|
|
*/
|
|
|
|
FreeSpaceMapTruncateRel(&reln->smgr_rnode, xlrec->blkno);
|
|
|
|
|
|
|
|
/* Do the truncation */
|
|
|
|
newblks = (*(smgrsw[reln->smgr_which].smgr_truncate)) (reln,
|
2004-05-31 22:31:33 +02:00
|
|
|
xlrec->blkno,
|
|
|
|
false);
|
2004-02-11 23:55:26 +01:00
|
|
|
if (newblks == InvalidBlockNumber)
|
|
|
|
ereport(WARNING,
|
|
|
|
(errcode_for_file_access(),
|
2004-06-18 08:14:31 +02:00
|
|
|
errmsg("could not truncate relation %u/%u/%u to %u blocks: %m",
|
|
|
|
reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
2004-02-11 23:55:26 +01:00
|
|
|
reln->smgr_rnode.relNode,
|
|
|
|
xlrec->blkno)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(PANIC, "smgr_redo: unknown op code %u", info);
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
smgr_undo(XLogRecPtr lsn, XLogRecord *record)
|
|
|
|
{
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Since we have no transactional WAL entries, should never undo */
|
|
|
|
elog(PANIC, "smgr_undo: cannot undo");
|
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
|
|
|
smgr_desc(char *buf, uint8 xl_info, char *rec)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2004-02-11 23:55:26 +01:00
|
|
|
uint8 info = xl_info & ~XLR_INFO_MASK;
|
|
|
|
|
|
|
|
if (info == XLOG_SMGR_CREATE)
|
|
|
|
{
|
|
|
|
xl_smgr_create *xlrec = (xl_smgr_create *) rec;
|
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
sprintf(buf + strlen(buf), "file create: %u/%u/%u",
|
|
|
|
xlrec->rnode.spcNode, xlrec->rnode.dbNode,
|
|
|
|
xlrec->rnode.relNode);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
else if (info == XLOG_SMGR_TRUNCATE)
|
|
|
|
{
|
|
|
|
xl_smgr_truncate *xlrec = (xl_smgr_truncate *) rec;
|
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
sprintf(buf + strlen(buf), "file truncate: %u/%u/%u to %u blocks",
|
|
|
|
xlrec->rnode.spcNode, xlrec->rnode.dbNode,
|
|
|
|
xlrec->rnode.relNode, xlrec->blkno);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
strcat(buf, "UNKNOWN");
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|