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
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, 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
|
2008-09-30 12:52:14 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/smgr/smgr.c,v 1.112 2008/09/30 10:52:13 heikki Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-07-17 05:32:14 +02:00
|
|
|
#include "access/xact.h"
|
2006-04-14 22:27:24 +02:00
|
|
|
#include "access/xlogutils.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-09-29 06:02:27 +02:00
|
|
|
#include "storage/ipc.h"
|
1996-11-03 06:08:01 +01:00
|
|
|
#include "storage/smgr.h"
|
2007-07-09 00:23:16 +02: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
|
2007-01-03 19:11:01 +01:00
|
|
|
* generally expected to report problems via elog(ERROR). An exception is
|
|
|
|
* that smgr_unlink should use elog(WARNING), rather than erroring out,
|
|
|
|
* because we normally unlink relations during post-commit/abort cleanup,
|
|
|
|
* and so it's too late to raise an error. Also, various conditions that
|
|
|
|
* would normally be errors should be allowed during bootstrap and/or WAL
|
|
|
|
* recovery --- see comments in md.c for details.
|
2004-02-10 02:55:27 +01:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct f_smgr
|
|
|
|
{
|
2007-01-03 19:11:01 +01:00
|
|
|
void (*smgr_init) (void); /* may be NULL */
|
|
|
|
void (*smgr_shutdown) (void); /* may be NULL */
|
2008-08-11 13:05:11 +02:00
|
|
|
void (*smgr_close) (SMgrRelation reln, ForkNumber forknum);
|
|
|
|
void (*smgr_create) (SMgrRelation reln, ForkNumber forknum,
|
|
|
|
bool isRedo);
|
|
|
|
bool (*smgr_exists) (SMgrRelation reln, ForkNumber forknum);
|
|
|
|
void (*smgr_unlink) (RelFileNode rnode, ForkNumber forknum,
|
|
|
|
bool isRedo);
|
|
|
|
void (*smgr_extend) (SMgrRelation reln, ForkNumber forknum,
|
|
|
|
BlockNumber blocknum, char *buffer, bool isTemp);
|
|
|
|
void (*smgr_read) (SMgrRelation reln, ForkNumber forknum,
|
|
|
|
BlockNumber blocknum, char *buffer);
|
|
|
|
void (*smgr_write) (SMgrRelation reln, ForkNumber forknum,
|
|
|
|
BlockNumber blocknum, char *buffer, bool isTemp);
|
|
|
|
BlockNumber (*smgr_nblocks) (SMgrRelation reln, ForkNumber forknum);
|
|
|
|
void (*smgr_truncate) (SMgrRelation reln, ForkNumber forknum,
|
|
|
|
BlockNumber nblocks, bool isTemp);
|
|
|
|
void (*smgr_immedsync) (SMgrRelation reln, ForkNumber forknum);
|
2007-11-15 22:14:46 +01:00
|
|
|
void (*smgr_commit) (void); /* may be NULL */
|
|
|
|
void (*smgr_abort) (void); /* may be NULL */
|
|
|
|
void (*smgr_pre_ckpt) (void); /* may be NULL */
|
|
|
|
void (*smgr_sync) (void); /* may be NULL */
|
|
|
|
void (*smgr_post_ckpt) (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 */
|
2008-08-11 13:05:11 +02:00
|
|
|
{mdinit, NULL, mdclose, mdcreate, mdexists, mdunlink, mdextend,
|
2004-08-29 07:07:03 +02:00
|
|
|
mdread, mdwrite, mdnblocks, mdtruncate, mdimmedsync,
|
2007-11-15 21:36:40 +01:00
|
|
|
NULL, NULL, mdpreckpt, mdsync, mdpostckpt
|
2004-02-10 02:55:27 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
2004-08-29 07:07:03 +02: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-17 05:32:14 +02:00
|
|
|
* To handle subtransactions, every entry is marked with its transaction
|
|
|
|
* nesting level. At subtransaction commit, we reassign the subtransaction's
|
|
|
|
* entries to the parent nesting level. At subtransaction abort, we can
|
|
|
|
* immediately execute the abort-time actions for all entries of the current
|
|
|
|
* nesting level.
|
|
|
|
*
|
|
|
|
* NOTE: the list is kept in TopMemoryContext to be sure it won't disappear
|
|
|
|
* unbetimes. It'd probably be OK to keep it in TopTransactionContext,
|
|
|
|
* but I'm being paranoid.
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct PendingRelDelete
|
|
|
|
{
|
|
|
|
RelFileNode relnode; /* relation that may need to be deleted */
|
2008-08-11 13:05:11 +02:00
|
|
|
ForkNumber forknum; /* fork number 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 */
|
2004-07-17 05:32:14 +02:00
|
|
|
int nestLevel; /* xact nesting level of request */
|
|
|
|
struct PendingRelDelete *next; /* linked-list link */
|
2000-11-08 23:10:03 +01:00
|
|
|
} PendingRelDelete;
|
|
|
|
|
2004-07-17 05:32:14 +02:00
|
|
|
static PendingRelDelete *pendingDeletes = NULL; /* head of linked list */
|
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
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
RelFileNode rnode;
|
2008-08-11 13:05:11 +02:00
|
|
|
ForkNumber forknum;
|
2004-02-11 23:55:26 +01:00
|
|
|
} xl_smgr_create;
|
|
|
|
|
|
|
|
typedef struct xl_smgr_truncate
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
BlockNumber blkno;
|
|
|
|
RelFileNode rnode;
|
2008-08-11 13:05:11 +02:00
|
|
|
ForkNumber forknum;
|
2004-02-11 23:55:26 +01:00
|
|
|
} xl_smgr_truncate;
|
|
|
|
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/* local function prototypes */
|
|
|
|
static void smgrshutdown(int code, Datum arg);
|
2008-08-11 13:05:11 +02:00
|
|
|
static void smgr_internal_unlink(RelFileNode rnode, ForkNumber forknum,
|
|
|
|
int which, bool isTemp, bool isRedo);
|
2004-02-10 02:55:27 +01:00
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2005-08-08 05:12:16 +02:00
|
|
|
* smgrinit(), smgrshutdown() -- Initialize or shut down storage
|
1997-09-07 07:04:48 +02:00
|
|
|
* managers.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-08-08 05:12:16 +02:00
|
|
|
* Note: smgrinit is called during backend startup (normal or standalone
|
|
|
|
* case), *not* during postmaster start. Therefore, any resources created
|
|
|
|
* here or destroyed in smgrshutdown are backend-local.
|
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)
|
2007-01-03 19:11:01 +01:00
|
|
|
(*(smgrsw[i].smgr_init)) ();
|
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
|
|
|
}
|
|
|
|
|
2005-08-08 05:12:16 +02:00
|
|
|
/*
|
|
|
|
* on_proc_exit hook for smgr cleanup during backend shutdown
|
|
|
|
*/
|
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)
|
2007-01-03 19:11:01 +01:00
|
|
|
(*(smgrsw[i].smgr_shutdown)) ();
|
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)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
SMgrRelation reln;
|
2004-02-10 02:55:27 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* Initialize it if not present before */
|
|
|
|
if (!found)
|
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
int forknum;
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/* hash_search already filled in the lookup key */
|
2005-01-10 21:02:24 +01:00
|
|
|
reln->smgr_owner = NULL;
|
2004-02-10 02:55:27 +01:00
|
|
|
reln->smgr_which = 0; /* we only have md.c at present */
|
2008-08-11 13:05:11 +02:00
|
|
|
|
|
|
|
/* mark it not open */
|
|
|
|
for(forknum = 0; forknum <= MAX_FORKNUM; forknum++)
|
|
|
|
reln->md_fd[forknum] = NULL;
|
2004-02-10 02:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return reln;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-01-10 21:02:24 +01:00
|
|
|
* smgrsetowner() -- Establish a long-lived reference to an SMgrRelation object
|
2004-02-10 02:55:27 +01:00
|
|
|
*
|
2005-01-10 21:02:24 +01:00
|
|
|
* There can be only one owner at a time; this is sufficient since currently
|
|
|
|
* the only such owners exist in the relcache.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* First, unhook any old owner. (Normally there shouldn't be any, but it
|
|
|
|
* seems possible that this can happen during swap_relation_files()
|
2005-01-10 21:02:24 +01:00
|
|
|
* depending on the order of processing. It's ok to close the old
|
|
|
|
* relcache entry early in that case.)
|
|
|
|
*/
|
|
|
|
if (reln->smgr_owner)
|
|
|
|
*(reln->smgr_owner) = NULL;
|
|
|
|
|
|
|
|
/* Now establish the ownership relationship. */
|
|
|
|
reln->smgr_owner = owner;
|
|
|
|
*owner = reln;
|
|
|
|
}
|
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
/*
|
|
|
|
* smgrexists() -- Does the underlying file for a fork exist?
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
smgrexists(SMgrRelation reln, ForkNumber forknum)
|
|
|
|
{
|
|
|
|
return (*(smgrsw[reln->smgr_which].smgr_exists)) (reln, forknum);
|
|
|
|
}
|
|
|
|
|
2005-01-10 21:02:24 +01:00
|
|
|
/*
|
|
|
|
* smgrclose() -- Close and delete an SMgrRelation object.
|
2004-02-10 02:55:27 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrclose(SMgrRelation reln)
|
|
|
|
{
|
2005-01-10 21:02:24 +01:00
|
|
|
SMgrRelation *owner;
|
2008-08-11 13:05:11 +02:00
|
|
|
ForkNumber forknum;
|
2005-01-10 21:02:24 +01:00
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
|
|
|
|
(*(smgrsw[reln->smgr_which].smgr_close)) (reln, forknum);
|
2004-02-10 02:55:27 +01:00
|
|
|
|
2005-01-10 21:02:24 +01:00
|
|
|
owner = reln->smgr_owner;
|
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
if (hash_search(SMgrRelationHash,
|
|
|
|
(void *) &(reln->smgr_rnode),
|
|
|
|
HASH_REMOVE, NULL) == NULL)
|
|
|
|
elog(ERROR, "SMgrRelation hashtable corrupted");
|
2005-01-10 21:02:24 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Unhook the owner pointer, if any. We do this last since in the remote
|
|
|
|
* possibility of failure above, the SMgrRelation object will still exist.
|
2005-01-10 21:02:24 +01:00
|
|
|
*/
|
|
|
|
if (owner)
|
|
|
|
*owner = NULL;
|
2004-02-10 02:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* smgrcloseall() -- Close all existing SMgrRelation objects.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrclosenode(RelFileNode rnode)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
SMgrRelation reln;
|
2004-02-10 02:55:27 +01:00
|
|
|
|
|
|
|
/* 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,
|
2008-08-11 13:05:11 +02:00
|
|
|
* cause the underlying disk file or other storage for the fork
|
|
|
|
* to be created.
|
2004-02-10 02:55:27 +01:00
|
|
|
*
|
|
|
|
* 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
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isTemp, bool isRedo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
XLogRecPtr lsn;
|
|
|
|
XLogRecData rdata;
|
|
|
|
xl_smgr_create xlrec;
|
2000-11-08 23:10:03 +01:00
|
|
|
PendingRelDelete *pending;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/*
|
|
|
|
* Exit quickly in WAL replay mode if we've already opened the file.
|
|
|
|
* If it's open, it surely must exist.
|
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
if (isRedo && reln->md_fd[forknum] != NULL)
|
2008-06-12 11:12:31 +02:00
|
|
|
return;
|
|
|
|
|
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.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* XXX this is a fairly ugly violation of module layering, but this seems
|
|
|
|
* to be the best place to put the check. Maybe TablespaceCreateDbspace
|
2005-10-15 04:49:52 +02:00
|
|
|
* 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.
|
2004-07-11 21:52:52 +02:00
|
|
|
*/
|
|
|
|
TablespaceCreateDbspace(reln->smgr_rnode.spcNode,
|
|
|
|
reln->smgr_rnode.dbNode,
|
|
|
|
isRedo);
|
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_create)) (reln, forknum, isRedo);
|
2004-02-10 02:55:27 +01:00
|
|
|
|
|
|
|
if (isRedo)
|
|
|
|
return;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
2007-09-05 20:10:48 +02:00
|
|
|
* Make an XLOG entry showing the file creation. If we abort, the file
|
|
|
|
* will be dropped at abort time.
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
|
|
|
xlrec.rnode = reln->smgr_rnode;
|
2008-08-11 13:05:11 +02:00
|
|
|
xlrec.forknum = forknum;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
|
|
|
rdata.data = (char *) &xlrec;
|
|
|
|
rdata.len = sizeof(xlrec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata.buffer = InvalidBuffer;
|
2004-02-11 23:55:26 +01:00
|
|
|
rdata.next = NULL;
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_CREATE, &rdata);
|
2004-02-11 23:55:26 +01:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/* Add the relation to the list of stuff to delete at abort */
|
2004-07-17 05:32:14 +02:00
|
|
|
pending = (PendingRelDelete *)
|
|
|
|
MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
|
2004-02-10 02:55:27 +01:00
|
|
|
pending->relnode = reln->smgr_rnode;
|
2008-08-11 13:05:11 +02:00
|
|
|
pending->forknum = forknum;
|
2004-02-10 02:55:27 +01:00
|
|
|
pending->which = reln->smgr_which;
|
|
|
|
pending->isTemp = isTemp;
|
2000-11-08 23:10:03 +01:00
|
|
|
pending->atCommit = false; /* delete if abort */
|
2004-07-17 05:32:14 +02:00
|
|
|
pending->nestLevel = GetCurrentTransactionNestLevel();
|
|
|
|
pending->next = pendingDeletes;
|
|
|
|
pendingDeletes = pending;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-10 02:55:27 +01:00
|
|
|
* smgrscheduleunlink() -- Schedule unlinking a relation at xact commit.
|
|
|
|
*
|
2008-08-11 13:05:11 +02:00
|
|
|
* The fork 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
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrscheduleunlink(SMgrRelation reln, ForkNumber forknum, bool isTemp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-11-08 23:10:03 +01:00
|
|
|
PendingRelDelete *pending;
|
|
|
|
|
|
|
|
/* Add the relation to the list of stuff to delete at commit */
|
2004-07-17 05:32:14 +02:00
|
|
|
pending = (PendingRelDelete *)
|
|
|
|
MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
|
2004-02-10 02:55:27 +01:00
|
|
|
pending->relnode = reln->smgr_rnode;
|
2008-08-11 13:05:11 +02:00
|
|
|
pending->forknum = forknum;
|
2004-02-10 02:55:27 +01:00
|
|
|
pending->which = reln->smgr_which;
|
|
|
|
pending->isTemp = isTemp;
|
2000-11-08 23:10:03 +01:00
|
|
|
pending->atCommit = true; /* delete if commit */
|
2004-07-17 05:32:14 +02:00
|
|
|
pending->nestLevel = GetCurrentTransactionNestLevel();
|
|
|
|
pending->next = pendingDeletes;
|
|
|
|
pendingDeletes = pending;
|
2000-11-08 23:10:03 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* NOTE: if the relation was created in this transaction, it will now 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 immediately, but
|
|
|
|
* for now I'll keep the logic simple.
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
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
|
|
|
*
|
2008-08-11 13:05:11 +02:00
|
|
|
* The specified fork of 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
|
2005-06-18 00:32:51 +02:00
|
|
|
* already.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrdounlink(SMgrRelation reln, ForkNumber forknum, bool isTemp, bool isRedo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
RelFileNode rnode = reln->smgr_rnode;
|
2004-02-10 02:55:27 +01:00
|
|
|
int which = reln->smgr_which;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
/* Close the fork */
|
|
|
|
(*(smgrsw[which].smgr_close)) (reln, forknum);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
smgr_internal_unlink(rnode, forknum, 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
|
2008-08-11 13:05:11 +02:00
|
|
|
smgr_internal_unlink(RelFileNode rnode, ForkNumber forknum,
|
|
|
|
int which, bool isTemp, bool isRedo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
2005-03-20 23:00:54 +01:00
|
|
|
* Get rid of any remaining buffers for the relation. bufmgr will just
|
|
|
|
* drop them without bothering to write the contents.
|
2004-02-10 02:55:27 +01:00
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
DropRelFileNodeBuffers(rnode, forknum, isTemp, 0);
|
2004-02-10 02:55:27 +01:00
|
|
|
|
2006-03-31 00:11:55 +02:00
|
|
|
/*
|
2007-07-09 00:23:16 +02:00
|
|
|
* It'd be nice to tell the stats collector to forget it immediately, too.
|
|
|
|
* But we can't because we don't know the OID (and in cases involving
|
|
|
|
* relfilenode swaps, it's not always clear which table OID to forget,
|
|
|
|
* anyway).
|
2006-03-31 00:11:55 +02:00
|
|
|
*/
|
2006-01-18 21:35:06 +01:00
|
|
|
|
2004-02-10 02:55:27 +01:00
|
|
|
/*
|
|
|
|
* And delete the physical files.
|
|
|
|
*
|
2007-01-03 19:11:01 +01:00
|
|
|
* Note: smgr_unlink must treat deletion failure as a WARNING, not an
|
|
|
|
* ERROR, because we've already decided to commit or abort the current
|
|
|
|
* xact.
|
2004-02-10 02:55:27 +01:00
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[which].smgr_unlink)) (rnode, forknum, isRedo);
|
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
|
|
|
*
|
2007-01-03 19:11:01 +01:00
|
|
|
* The semantics are nearly the same as smgrwrite(): write at the
|
|
|
|
* specified position. However, this is to be used for the case of
|
|
|
|
* extending a relation (i.e., blocknum is at or beyond the current
|
|
|
|
* EOF). Note that we assume writing a block beyond current EOF
|
|
|
|
* causes intervening file space to become filled with zeroes.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-10 02:55:27 +01:00
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
|
|
|
|
char *buffer, bool isTemp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_extend)) (reln, forknum, blocknum,
|
|
|
|
buffer, isTemp);
|
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
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
|
|
|
|
char *buffer)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_read)) (reln, forknum, blocknum, buffer);
|
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
|
|
|
*
|
2007-01-03 19:11:01 +01:00
|
|
|
* This is to be used only for updating already-existing blocks of a
|
|
|
|
* relation (ie, those before the current EOF). To extend a relation,
|
|
|
|
* use smgrextend().
|
|
|
|
*
|
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
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
|
|
|
|
char *buffer, bool isTemp)
|
2000-10-16 16:52:28 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_write)) (reln, forknum, blocknum,
|
|
|
|
buffer, isTemp);
|
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
|
|
|
*/
|
2001-06-28 01:31:40 +02:00
|
|
|
BlockNumber
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrnblocks(SMgrRelation reln, ForkNumber forknum)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
return (*(smgrsw[reln->smgr_which].smgr_nblocks)) (reln, forknum);
|
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
|
|
|
*/
|
2007-01-03 19:11:01 +01:00
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks,
|
|
|
|
bool isTemp)
|
1996-11-27 08:25:52 +01:00
|
|
|
{
|
2005-03-20 23:00:54 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
|
|
|
|
* just drop them without bothering to write the contents.
|
2005-03-20 23:00:54 +01:00
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
DropRelFileNodeBuffers(reln->smgr_rnode, forknum, isTemp, nblocks);
|
2005-03-20 23:00:54 +01:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Do the truncation */
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_truncate)) (reln, forknum, nblocks,
|
|
|
|
isTemp);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-05-31 22:31:33 +02:00
|
|
|
if (!isTemp)
|
|
|
|
{
|
|
|
|
/*
|
2007-09-05 20:10:48 +02:00
|
|
|
* Make an XLOG entry showing the file truncation.
|
2004-05-31 22:31:33 +02:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
XLogRecPtr lsn;
|
|
|
|
XLogRecData rdata;
|
2004-05-31 22:31:33 +02:00
|
|
|
xl_smgr_truncate xlrec;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
2007-01-03 19:11:01 +01:00
|
|
|
xlrec.blkno = nblocks;
|
2004-05-31 22:31:33 +02:00
|
|
|
xlrec.rnode = reln->smgr_rnode;
|
2008-08-11 13:05:11 +02:00
|
|
|
xlrec.forknum = forknum;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
2004-05-31 22:31:33 +02:00
|
|
|
rdata.data = (char *) &xlrec;
|
|
|
|
rdata.len = sizeof(xlrec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata.buffer = InvalidBuffer;
|
2004-05-31 22:31:33 +02:00
|
|
|
rdata.next = NULL;
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_TRUNCATE, &rdata);
|
2004-05-31 22:31:33 +02:00
|
|
|
}
|
1996-11-27 08:25:52 +01:00
|
|
|
}
|
|
|
|
|
2004-06-02 19:28:18 +02:00
|
|
|
/*
|
|
|
|
* smgrimmedsync() -- Force the specified relation to stable storage.
|
|
|
|
*
|
2005-06-20 20:37:02 +02:00
|
|
|
* Synchronously force all previous writes to the specified relation
|
|
|
|
* down to disk.
|
2004-06-02 19:28:18 +02:00
|
|
|
*
|
2004-08-16 01:44:46 +02:00
|
|
|
* This is useful for building completely new relations (eg, new
|
|
|
|
* indexes). Instead of incrementally WAL-logging the index build
|
|
|
|
* steps, we can just write completed index pages to disk with smgrwrite
|
2004-06-02 19:28:18 +02:00
|
|
|
* 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
|
2004-08-16 01:44:46 +02:00
|
|
|
* for the completed index. But it is *not* sufficient if one wishes
|
|
|
|
* to use the WAL log for PITR or replication purposes: in that case
|
|
|
|
* we have to make WAL entries as well.)
|
2004-06-02 19:28:18 +02:00
|
|
|
*
|
|
|
|
* The preceding writes should specify isTemp = true to avoid
|
|
|
|
* duplicative fsyncs.
|
2005-06-20 20:37:02 +02:00
|
|
|
*
|
|
|
|
* Note that you need to do FlushRelationBuffers() first if there is
|
|
|
|
* any possibility that there are dirty buffers for the relation;
|
|
|
|
* otherwise the sync is not very meaningful.
|
2004-06-02 19:28:18 +02:00
|
|
|
*/
|
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
|
2004-06-02 19:28:18 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_immedsync)) (reln, forknum);
|
2004-06-02 19:28:18 +02:00
|
|
|
}
|
|
|
|
|
2005-06-18 00:32:51 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PostPrepare_smgr -- Clean up after a successful PREPARE
|
|
|
|
*
|
|
|
|
* What we have to do here is throw away the in-memory state about pending
|
|
|
|
* relation deletes. It's all been recorded in the 2PC state file and
|
|
|
|
* it's no longer smgr's job to worry about it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PostPrepare_smgr(void)
|
|
|
|
{
|
|
|
|
PendingRelDelete *pending;
|
|
|
|
PendingRelDelete *next;
|
|
|
|
|
|
|
|
for (pending = pendingDeletes; pending != NULL; pending = next)
|
|
|
|
{
|
|
|
|
next = pending->next;
|
|
|
|
pendingDeletes = next;
|
|
|
|
/* must explicitly free the list entry */
|
|
|
|
pfree(pending);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2004-07-17 05:32:14 +02:00
|
|
|
*
|
|
|
|
* This also runs when aborting a subxact; we want to clean up a failed
|
|
|
|
* subxact immediately.
|
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-17 05:32:14 +02:00
|
|
|
int nestLevel = GetCurrentTransactionNestLevel();
|
|
|
|
PendingRelDelete *pending;
|
|
|
|
PendingRelDelete *prev;
|
|
|
|
PendingRelDelete *next;
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-07-17 05:32:14 +02:00
|
|
|
prev = NULL;
|
|
|
|
for (pending = pendingDeletes; pending != NULL; pending = next)
|
2000-11-08 23:10:03 +01:00
|
|
|
{
|
2004-07-17 05:32:14 +02:00
|
|
|
next = pending->next;
|
|
|
|
if (pending->nestLevel < nestLevel)
|
|
|
|
{
|
|
|
|
/* outer-level entries should not be processed yet */
|
|
|
|
prev = pending;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* unlink list entry first, so we don't retry on failure */
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else
|
|
|
|
pendingDeletes = next;
|
|
|
|
/* do deletion if called for */
|
|
|
|
if (pending->atCommit == isCommit)
|
|
|
|
smgr_internal_unlink(pending->relnode,
|
2008-08-11 13:05:11 +02:00
|
|
|
pending->forknum,
|
2004-07-17 05:32:14 +02:00
|
|
|
pending->which,
|
|
|
|
pending->isTemp,
|
|
|
|
false);
|
|
|
|
/* must explicitly free the list entry */
|
|
|
|
pfree(pending);
|
|
|
|
/* prev does not change */
|
|
|
|
}
|
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.
|
2008-08-11 13:05:11 +02:00
|
|
|
* *ptr is set to point to a freshly-palloc'd array of RelFileForks.
|
2004-02-11 23:55:26 +01:00
|
|
|
* If there are no relations to be deleted, *ptr is set to NULL.
|
2004-07-01 02:52:04 +02:00
|
|
|
*
|
2007-09-05 20:10:48 +02:00
|
|
|
* If haveNonTemp isn't NULL, the bool it points to gets set to true if
|
|
|
|
* there is any non-temp table pending to be deleted; false if not.
|
|
|
|
*
|
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
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrGetPendingDeletes(bool forCommit, RelFileFork **ptr, bool *haveNonTemp)
|
2004-02-11 23:55:26 +01:00
|
|
|
{
|
2004-07-17 05:32:14 +02:00
|
|
|
int nestLevel = GetCurrentTransactionNestLevel();
|
2004-02-11 23:55:26 +01:00
|
|
|
int nrels;
|
2008-08-11 13:05:11 +02:00
|
|
|
RelFileFork *rptr;
|
2004-07-17 05:32:14 +02:00
|
|
|
PendingRelDelete *pending;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
|
|
|
nrels = 0;
|
2007-09-05 20:10:48 +02:00
|
|
|
if (haveNonTemp)
|
|
|
|
*haveNonTemp = false;
|
2004-07-17 05:32:14 +02:00
|
|
|
for (pending = pendingDeletes; pending != NULL; pending = pending->next)
|
2004-02-11 23:55:26 +01:00
|
|
|
{
|
2004-07-17 05:32:14 +02:00
|
|
|
if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit)
|
2004-02-11 23:55:26 +01:00
|
|
|
nrels++;
|
|
|
|
}
|
|
|
|
if (nrels == 0)
|
|
|
|
{
|
|
|
|
*ptr = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-11 13:05:11 +02:00
|
|
|
rptr = (RelFileFork *) palloc(nrels * sizeof(RelFileFork));
|
2004-02-11 23:55:26 +01:00
|
|
|
*ptr = rptr;
|
2004-07-17 05:32:14 +02:00
|
|
|
for (pending = pendingDeletes; pending != NULL; pending = pending->next)
|
2004-02-11 23:55:26 +01:00
|
|
|
{
|
2004-07-17 05:32:14 +02:00
|
|
|
if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit)
|
2008-08-11 13:05:11 +02:00
|
|
|
{
|
|
|
|
rptr->rnode = pending->relnode;
|
|
|
|
rptr->forknum = pending->forknum;
|
|
|
|
rptr++;
|
|
|
|
}
|
2007-09-05 20:10:48 +02:00
|
|
|
if (haveNonTemp && !pending->isTemp)
|
|
|
|
*haveNonTemp = true;
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
return nrels;
|
|
|
|
}
|
|
|
|
|
2004-07-01 02:52:04 +02:00
|
|
|
/*
|
|
|
|
* AtSubCommit_smgr() --- Take care of subtransaction commit.
|
|
|
|
*
|
2004-07-17 05:32:14 +02:00
|
|
|
* Reassign all items in the pending-deletes list to the parent transaction.
|
2004-07-01 02:52:04 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AtSubCommit_smgr(void)
|
|
|
|
{
|
2004-07-17 05:32:14 +02:00
|
|
|
int nestLevel = GetCurrentTransactionNestLevel();
|
|
|
|
PendingRelDelete *pending;
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2004-07-17 05:32:14 +02:00
|
|
|
for (pending = pendingDeletes; pending != NULL; pending = pending->next)
|
|
|
|
{
|
|
|
|
if (pending->nestLevel >= nestLevel)
|
|
|
|
pending->nestLevel = nestLevel - 1;
|
|
|
|
}
|
2004-07-01 02:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
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)
|
2007-01-03 19:11:01 +01:00
|
|
|
(*(smgrsw[i].smgr_commit)) ();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*
|
2004-09-06 19:56:33 +02:00
|
|
|
* smgrabort() -- Clean up after transaction abort.
|
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
|
|
|
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)
|
2007-01-03 19:11:01 +01:00
|
|
|
(*(smgrsw[i].smgr_abort)) ();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*
|
2007-11-15 21:36:40 +01:00
|
|
|
* smgrpreckpt() -- Prepare for checkpoint.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrpreckpt(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_pre_ckpt)
|
|
|
|
(*(smgrsw[i].smgr_pre_ckpt)) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* smgrsync() -- Sync files to disk during checkpoint.
|
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)
|
2007-01-03 19:11:01 +01:00
|
|
|
(*(smgrsw[i].smgr_sync)) ();
|
2000-10-28 18:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-15 21:36:40 +01:00
|
|
|
/*
|
|
|
|
* smgrpostckpt() -- Post-checkpoint cleanup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
smgrpostckpt(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NSmgr; i++)
|
|
|
|
{
|
|
|
|
if (smgrsw[i].smgr_post_ckpt)
|
|
|
|
(*(smgrsw[i].smgr_post_ckpt)) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrcreate(reln, xlrec->forknum, false, true);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
else if (info == XLOG_SMGR_TRUNCATE)
|
|
|
|
{
|
|
|
|
xl_smgr_truncate *xlrec = (xl_smgr_truncate *) XLogRecGetData(record);
|
|
|
|
SMgrRelation reln;
|
|
|
|
|
|
|
|
reln = smgropen(xlrec->rnode);
|
|
|
|
|
2007-07-20 18:29:53 +02:00
|
|
|
/*
|
|
|
|
* Forcibly create relation if it doesn't exist (which suggests that
|
|
|
|
* it was dropped somewhere later in the WAL sequence). As in
|
2007-11-15 22:14:46 +01:00
|
|
|
* XLogOpenRelation, we prefer to recreate the rel and replay the log
|
|
|
|
* as best we can until the drop is seen.
|
2007-07-20 18:29:53 +02:00
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
smgrcreate(reln, xlrec->forknum, false, true);
|
2007-07-20 18:29:53 +02:00
|
|
|
|
2005-03-20 23:00:54 +01:00
|
|
|
/* Can't use smgrtruncate because it would try to xlog */
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*
|
|
|
|
* First, force bufmgr to drop any buffers it has for the to-be-
|
2005-10-15 04:49:52 +02:00
|
|
|
* truncated blocks. We must do this, else subsequent XLogReadBuffer
|
|
|
|
* operations will not re-extend the file properly.
|
2004-05-31 05:48:10 +02:00
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
DropRelFileNodeBuffers(xlrec->rnode, xlrec->forknum, false,
|
|
|
|
xlrec->blkno);
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Do the truncation */
|
2007-01-03 19:11:01 +01:00
|
|
|
(*(smgrsw[reln->smgr_which].smgr_truncate)) (reln,
|
2008-08-11 13:05:11 +02:00
|
|
|
xlrec->forknum,
|
2007-01-03 19:11:01 +01:00
|
|
|
xlrec->blkno,
|
|
|
|
false);
|
2006-04-14 22:27:24 +02:00
|
|
|
|
|
|
|
/* Also tell xlogutils.c about it */
|
2008-08-11 13:05:11 +02:00
|
|
|
XLogTruncateRelation(xlrec->rnode, xlrec->forknum, xlrec->blkno);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(PANIC, "smgr_redo: unknown op code %u", info);
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-03-24 05:32:13 +01:00
|
|
|
smgr_desc(StringInfo 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;
|
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
appendStringInfo(buf, "file create: %u/%u/%u/%u",
|
2006-10-04 02:30:14 +02:00
|
|
|
xlrec->rnode.spcNode, xlrec->rnode.dbNode,
|
2008-08-11 13:05:11 +02:00
|
|
|
xlrec->rnode.relNode, xlrec->forknum);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
else if (info == XLOG_SMGR_TRUNCATE)
|
|
|
|
{
|
|
|
|
xl_smgr_truncate *xlrec = (xl_smgr_truncate *) rec;
|
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
appendStringInfo(buf, "file truncate: %u/%u/%u/%u to %u blocks",
|
2006-10-04 02:30:14 +02:00
|
|
|
xlrec->rnode.spcNode, xlrec->rnode.dbNode,
|
2008-08-11 13:05:11 +02:00
|
|
|
xlrec->rnode.relNode, xlrec->forknum,
|
|
|
|
xlrec->blkno);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
else
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "UNKNOWN");
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|