mirror of
https://github.com/zebrajr/postgres.git
synced 2025-12-06 00:20:01 +01:00
The new facility makes it easier to optimize bulk loading, as the logic for buffering, WAL-logging, and syncing the relation only needs to be implemented once. It's also less error-prone: We have had a number of bugs in how a relation is fsync'd - or not - at the end of a bulk loading operation. By centralizing that logic to one place, we only need to write it correctly once. The new facility is faster for small relations: Instead of of calling smgrimmedsync(), we register the fsync to happen at next checkpoint, which avoids the fsync latency. That can make a big difference if you are e.g. restoring a schema-only dump with lots of relations. It is also slightly more efficient with large relations, as the WAL logging is performed multiple pages at a time. That avoids some WAL header overhead. The sorted GiST index build did that already, this moves the buffering to the new facility. The changes to pageinspect GiST test needs an explanation: Before this patch, the sorted GiST index build set the LSN on every page to the special GistBuildLSN value, not the LSN of the WAL record, even though they were WAL-logged. There was no particular need for it, it just happened naturally when we wrote out the pages before WAL-logging them. Now we WAL-log the pages first, like in B-tree build, so the pages are stamped with the record's real LSN. When the build is not WAL-logged, we still use GistBuildLSN. To make the test output predictable, use an unlogged index. Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/30e8f366-58b3-b239-c521-422122dd5150%40iki.fi
128 lines
4.7 KiB
C
128 lines
4.7 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* smgr.h
|
|
* storage manager switch public interface declarations.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/storage/smgr.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef SMGR_H
|
|
#define SMGR_H
|
|
|
|
#include "lib/ilist.h"
|
|
#include "storage/block.h"
|
|
#include "storage/relfilelocator.h"
|
|
|
|
/*
|
|
* smgr.c maintains a table of SMgrRelation objects, which are essentially
|
|
* cached file handles. An SMgrRelation is created (if not already present)
|
|
* by smgropen(), and destroyed by smgrdestroy(). Note that neither of these
|
|
* operations imply I/O, they just create or destroy a hashtable entry. (But
|
|
* smgrdestroy() may release associated resources, such as OS-level file
|
|
* descriptors.)
|
|
*
|
|
* An SMgrRelation may be "pinned", to prevent it from being destroyed while
|
|
* it's in use. We use this to prevent pointers relcache to smgr from being
|
|
* invalidated. SMgrRelations that are not pinned are deleted at end of
|
|
* transaction.
|
|
*/
|
|
typedef struct SMgrRelationData
|
|
{
|
|
/* rlocator is the hashtable lookup key, so it must be first! */
|
|
RelFileLocatorBackend smgr_rlocator; /* relation physical identifier */
|
|
|
|
/*
|
|
* The following fields are reset to InvalidBlockNumber upon a cache flush
|
|
* event, and hold the last known size for each fork. This information is
|
|
* currently only reliable during recovery, since there is no cache
|
|
* invalidation for fork extension.
|
|
*/
|
|
BlockNumber smgr_targblock; /* current insertion target block */
|
|
BlockNumber smgr_cached_nblocks[MAX_FORKNUM + 1]; /* last known size */
|
|
|
|
/* additional public fields may someday exist here */
|
|
|
|
/*
|
|
* Fields below here are intended to be private to smgr.c and its
|
|
* submodules. Do not touch them from elsewhere.
|
|
*/
|
|
int smgr_which; /* storage manager selector */
|
|
|
|
/*
|
|
* for md.c; per-fork arrays of the number of open segments
|
|
* (md_num_open_segs) and the segments themselves (md_seg_fds).
|
|
*/
|
|
int md_num_open_segs[MAX_FORKNUM + 1];
|
|
struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
|
|
|
|
/*
|
|
* Pinning support. If unpinned (ie. pincount == 0), 'node' is a list
|
|
* link in list of all unpinned SMgrRelations.
|
|
*/
|
|
int pincount;
|
|
dlist_node node;
|
|
} SMgrRelationData;
|
|
|
|
typedef SMgrRelationData *SMgrRelation;
|
|
|
|
#define SmgrIsTemp(smgr) \
|
|
RelFileLocatorBackendIsTemp((smgr)->smgr_rlocator)
|
|
|
|
extern void smgrinit(void);
|
|
extern SMgrRelation smgropen(RelFileLocator rlocator, BackendId backend);
|
|
extern bool smgrexists(SMgrRelation reln, ForkNumber forknum);
|
|
extern void smgrpin(SMgrRelation reln);
|
|
extern void smgrunpin(SMgrRelation reln);
|
|
extern void smgrclose(SMgrRelation reln);
|
|
extern void smgrdestroyall(void);
|
|
extern void smgrrelease(SMgrRelation reln);
|
|
extern void smgrreleaseall(void);
|
|
extern void smgrreleaserellocator(RelFileLocatorBackend rlocator);
|
|
extern void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo);
|
|
extern void smgrdosyncall(SMgrRelation *rels, int nrels);
|
|
extern void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo);
|
|
extern void smgrextend(SMgrRelation reln, ForkNumber forknum,
|
|
BlockNumber blocknum, const void *buffer, bool skipFsync);
|
|
extern void smgrzeroextend(SMgrRelation reln, ForkNumber forknum,
|
|
BlockNumber blocknum, int nblocks, bool skipFsync);
|
|
extern bool smgrprefetch(SMgrRelation reln, ForkNumber forknum,
|
|
BlockNumber blocknum, int nblocks);
|
|
extern void smgrreadv(SMgrRelation reln, ForkNumber forknum,
|
|
BlockNumber blocknum,
|
|
void **buffer, BlockNumber nblocks);
|
|
extern void smgrwritev(SMgrRelation reln, ForkNumber forknum,
|
|
BlockNumber blocknum,
|
|
const void **buffer, BlockNumber nblocks,
|
|
bool skipFsync);
|
|
extern void smgrwriteback(SMgrRelation reln, ForkNumber forknum,
|
|
BlockNumber blocknum, BlockNumber nblocks);
|
|
extern BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum);
|
|
extern BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum);
|
|
extern void smgrtruncate(SMgrRelation reln, ForkNumber *forknum,
|
|
int nforks, BlockNumber *nblocks);
|
|
extern void smgrimmedsync(SMgrRelation reln, ForkNumber forknum);
|
|
extern void smgrregistersync(SMgrRelation reln, ForkNumber forknum);
|
|
extern void AtEOXact_SMgr(void);
|
|
extern bool ProcessBarrierSmgrRelease(void);
|
|
|
|
static inline void
|
|
smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
|
|
void *buffer)
|
|
{
|
|
smgrreadv(reln, forknum, blocknum, &buffer, 1);
|
|
}
|
|
|
|
static inline void
|
|
smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
|
|
const void *buffer, bool skipFsync)
|
|
{
|
|
smgrwritev(reln, forknum, blocknum, &buffer, 1, skipFsync);
|
|
}
|
|
|
|
#endif /* SMGR_H */
|