PostgreSQL Source Code git master
Loading...
Searching...
No Matches
smgr.c File Reference
#include "postgres.h"
#include "access/xlogutils.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "storage/aio.h"
#include "storage/bufmgr.h"
#include "storage/ipc.h"
#include "storage/md.h"
#include "storage/smgr.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
Include dependency graph for smgr.c:

Go to the source code of this file.

Data Structures

struct  f_smgr
 

Typedefs

typedef struct f_smgr f_smgr
 

Functions

static void smgrshutdown (int code, Datum arg)
 
static void smgrdestroy (SMgrRelation reln)
 
static void smgr_aio_reopen (PgAioHandle *ioh)
 
static charsmgr_aio_describe_identity (const PgAioTargetData *sd)
 
void smgrinit (void)
 
SMgrRelation smgropen (RelFileLocator rlocator, ProcNumber backend)
 
void smgrpin (SMgrRelation reln)
 
void smgrunpin (SMgrRelation reln)
 
void smgrrelease (SMgrRelation reln)
 
void smgrclose (SMgrRelation reln)
 
void smgrdestroyall (void)
 
void smgrreleaseall (void)
 
void smgrreleaserellocator (RelFileLocatorBackend rlocator)
 
bool smgrexists (SMgrRelation reln, ForkNumber forknum)
 
void smgrcreate (SMgrRelation reln, ForkNumber forknum, bool isRedo)
 
void smgrdosyncall (SMgrRelation *rels, int nrels)
 
void smgrdounlinkall (SMgrRelation *rels, int nrels, bool isRedo)
 
void smgrextend (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
 
void smgrzeroextend (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
 
bool smgrprefetch (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
 
uint32 smgrmaxcombine (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
 
void smgrreadv (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
 
void smgrstartreadv (PgAioHandle *ioh, SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
 
void smgrwritev (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
 
void smgrwriteback (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
 
BlockNumber smgrnblocks (SMgrRelation reln, ForkNumber forknum)
 
BlockNumber smgrnblocks_cached (SMgrRelation reln, ForkNumber forknum)
 
void smgrtruncate (SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *old_nblocks, BlockNumber *nblocks)
 
void smgrregistersync (SMgrRelation reln, ForkNumber forknum)
 
void smgrimmedsync (SMgrRelation reln, ForkNumber forknum)
 
static int smgrfd (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
 
void AtEOXact_SMgr (void)
 
bool ProcessBarrierSmgrRelease (void)
 
void pgaio_io_set_target_smgr (PgAioHandle *ioh, SMgrRelationData *smgr, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skip_fsync)
 

Variables

static const f_smgr smgrsw []
 
static const int NSmgr = lengthof(smgrsw)
 
static HTABSMgrRelationHash = NULL
 
static dlist_head unpinned_relns
 
const PgAioTargetInfo aio_smgr_target_info
 

Typedef Documentation

◆ f_smgr

Function Documentation

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 1017 of file smgr.c.

1018{
1020}
void smgrdestroyall(void)
Definition smgr.c:386

References smgrdestroyall().

Referenced by AbortTransaction(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), CommitTransaction(), PrepareTransaction(), and WalWriterMain().

◆ pgaio_io_set_target_smgr()

void pgaio_io_set_target_smgr ( PgAioHandle ioh,
SMgrRelationData smgr,
ForkNumber  forknum,
BlockNumber  blocknum,
int  nblocks,
bool  skip_fsync 
)

Definition at line 1038 of file smgr.c.

1044{
1046
1048
1049 /* backend is implied via IO owner */
1050 sd->smgr.rlocator = smgr->smgr_rlocator.locator;
1051 sd->smgr.forkNum = forknum;
1052 sd->smgr.blockNum = blocknum;
1053 sd->smgr.nblocks = nblocks;
1054 sd->smgr.is_temp = SmgrIsTemp(smgr);
1055 /* Temp relations should never be fsync'd */
1056 sd->smgr.skip_fsync = skip_fsync && !SmgrIsTemp(smgr);
1057}
@ PGAIO_TID_SMGR
Definition aio.h:120
PgAioTargetData * pgaio_io_get_target_data(PgAioHandle *ioh)
Definition aio_target.c:73
void pgaio_io_set_target(PgAioHandle *ioh, PgAioTargetID targetid)
Definition aio_target.c:64
static int fb(int x)
#define SmgrIsTemp(smgr)
Definition smgr.h:74
RelFileLocator locator
RelFileLocatorBackend smgr_rlocator
Definition smgr.h:38

References fb(), RelFileLocatorBackend::locator, pgaio_io_get_target_data(), pgaio_io_set_target(), PGAIO_TID_SMGR, SMgrRelationData::smgr_rlocator, and SmgrIsTemp.

Referenced by mdstartreadv().

◆ ProcessBarrierSmgrRelease()

bool ProcessBarrierSmgrRelease ( void  )

Definition at line 1027 of file smgr.c.

1028{
1030 return true;
1031}
void smgrreleaseall(void)
Definition smgr.c:412

References smgrreleaseall().

Referenced by ProcessProcSignalBarrier().

◆ smgr_aio_describe_identity()

static char * smgr_aio_describe_identity ( const PgAioTargetData sd)
static

Definition at line 1104 of file smgr.c.

1105{
1106 RelPathStr path;
1107 char *desc;
1108
1109 path = relpathbackend(sd->smgr.rlocator,
1110 sd->smgr.is_temp ?
1112 sd->smgr.forkNum);
1113
1114 if (sd->smgr.nblocks == 0)
1115 desc = psprintf(_("file \"%s\""), path.str);
1116 else if (sd->smgr.nblocks == 1)
1117 desc = psprintf(_("block %u in file \"%s\""),
1118 sd->smgr.blockNum,
1119 path.str);
1120 else
1121 desc = psprintf(_("blocks %u..%u in file \"%s\""),
1122 sd->smgr.blockNum,
1123 sd->smgr.blockNum + sd->smgr.nblocks - 1,
1124 path.str);
1125
1126 return desc;
1127}
#define _(x)
Definition elog.c:91
ProcNumber MyProcNumber
Definition globals.c:90
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
#define relpathbackend(rlocator, backend, forknum)
Definition relpath.h:141
char str[REL_PATH_STR_MAXLEN+1]
Definition relpath.h:123

References _, fb(), INVALID_PROC_NUMBER, MyProcNumber, psprintf(), relpathbackend, and RelPathStr::str.

◆ smgr_aio_reopen()

static void smgr_aio_reopen ( PgAioHandle ioh)
static

Definition at line 1064 of file smgr.c.

1065{
1069 ProcNumber procno;
1070 uint32 off;
1071
1072 /*
1073 * The caller needs to prevent interrupts from being processed, otherwise
1074 * the FD could be closed again before we get to executing the IO.
1075 */
1077
1078 if (sd->smgr.is_temp)
1079 procno = pgaio_io_get_owner(ioh);
1080 else
1081 procno = INVALID_PROC_NUMBER;
1082
1083 reln = smgropen(sd->smgr.rlocator, procno);
1084 switch (pgaio_io_get_op(ioh))
1085 {
1086 case PGAIO_OP_INVALID:
1088 break;
1089 case PGAIO_OP_READV:
1090 od->read.fd = smgrfd(reln, sd->smgr.forkNum, sd->smgr.blockNum, &off);
1091 Assert(off == od->read.offset);
1092 break;
1093 case PGAIO_OP_WRITEV:
1094 od->write.fd = smgrfd(reln, sd->smgr.forkNum, sd->smgr.blockNum, &off);
1095 Assert(off == od->write.offset);
1096 break;
1097 }
1098}
ProcNumber pgaio_io_get_owner(PgAioHandle *ioh)
Definition aio.c:355
@ PGAIO_OP_WRITEV
Definition aio.h:93
@ PGAIO_OP_INVALID
Definition aio.h:90
@ PGAIO_OP_READV
Definition aio.h:92
PgAioOpData * pgaio_io_get_op_data(PgAioHandle *ioh)
Definition aio_io.c:58
PgAioOp pgaio_io_get_op(PgAioHandle *ioh)
Definition aio_io.c:52
#define Assert(condition)
Definition c.h:873
#define pg_unreachable()
Definition c.h:341
uint32_t uint32
Definition c.h:546
#define INTERRUPTS_CAN_BE_PROCESSED()
Definition miscadmin.h:130
int ProcNumber
Definition procnumber.h:24
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition smgr.c:240
static int smgrfd(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
Definition smgr.c:989

References Assert, fb(), INTERRUPTS_CAN_BE_PROCESSED, INVALID_PROC_NUMBER, pg_unreachable, pgaio_io_get_op(), pgaio_io_get_op_data(), pgaio_io_get_owner(), pgaio_io_get_target_data(), PGAIO_OP_INVALID, PGAIO_OP_READV, PGAIO_OP_WRITEV, smgrfd(), and smgropen().

◆ smgrclose()

◆ smgrcreate()

void smgrcreate ( SMgrRelation  reln,
ForkNumber  forknum,
bool  isRedo 
)

Definition at line 481 of file smgr.c.

482{
484 smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
486}
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
static const f_smgr smgrsw[]
Definition smgr.c:128
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition smgr.c:94

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_create, and smgrsw.

Referenced by CreateAndCopyRelationData(), ExtendBufferedRelTo(), fill_seq_with_data(), heapam_relation_copy_data(), heapam_relation_set_new_filelocator(), index_build(), index_copy_data(), RelationCreateStorage(), smgr_redo(), and XLogReadBufferExtended().

◆ smgrdestroy()

static void smgrdestroy ( SMgrRelation  reln)
static

Definition at line 323 of file smgr.c.

324{
325 ForkNumber forknum;
326
327 Assert(reln->pincount == 0);
328
330
331 for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
332 smgrsw[reln->smgr_which].smgr_close(reln, forknum);
333
334 dlist_delete(&reln->node);
335
337 &(reln->smgr_rlocator),
338 HASH_REMOVE, NULL) == NULL)
339 elog(ERROR, "SMgrRelation hashtable corrupted");
340
342}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
@ HASH_REMOVE
Definition hsearch.h:115
static void dlist_delete(dlist_node *node)
Definition ilist.h:405
ForkNumber
Definition relpath.h:56
#define MAX_FORKNUM
Definition relpath.h:70
static HTAB * SMgrRelationHash
Definition smgr.c:160
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:93

References Assert, dlist_delete(), elog, ERROR, fb(), HASH_REMOVE, hash_search(), HOLD_INTERRUPTS, MAX_FORKNUM, RESUME_INTERRUPTS, f_smgr::smgr_close, SMgrRelationHash, and smgrsw.

Referenced by smgrdestroyall().

◆ smgrdestroyall()

void smgrdestroyall ( void  )

Definition at line 386 of file smgr.c.

387{
389
390 /* seems unsafe to accept interrupts while in a dlist_foreach_modify() */
392
393 /*
394 * Zap all unpinned SMgrRelations. We rely on smgrdestroy() to remove
395 * each one from the list.
396 */
398 {
400 iter.cur);
401
402 smgrdestroy(rel);
403 }
404
406}
#define dlist_foreach_modify(iter, lhead)
Definition ilist.h:640
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
static void smgrdestroy(SMgrRelation reln)
Definition smgr.c:323
static dlist_head unpinned_relns
Definition smgr.c:162
dlist_node * cur
Definition ilist.h:200

References dlist_mutable_iter::cur, dlist_container, dlist_foreach_modify, HOLD_INTERRUPTS, RESUME_INTERRUPTS, smgrdestroy(), and unpinned_relns.

Referenced by AtEOXact_SMgr(), BackgroundWriterMain(), CheckpointerMain(), RequestCheckpoint(), xlog_redo(), and XLogDropDatabase().

◆ smgrdosyncall()

void smgrdosyncall ( SMgrRelation rels,
int  nrels 
)

Definition at line 498 of file smgr.c.

499{
500 int i = 0;
501 ForkNumber forknum;
502
503 if (nrels == 0)
504 return;
505
506 FlushRelationsAllBuffers(rels, nrels);
507
509
510 /*
511 * Sync the physical file(s).
512 */
513 for (i = 0; i < nrels; i++)
514 {
515 int which = rels[i]->smgr_which;
516
517 for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
518 {
519 if (smgrsw[which].smgr_exists(rels[i], forknum))
520 smgrsw[which].smgr_immedsync(rels[i], forknum);
521 }
522 }
523
525}
void FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
Definition bufmgr.c:5165
int i
Definition isn.c:77
int smgr_which
Definition smgr.h:55
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:123

References fb(), FlushRelationsAllBuffers(), HOLD_INTERRUPTS, i, MAX_FORKNUM, RESUME_INTERRUPTS, f_smgr::smgr_immedsync, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgrDoPendingSyncs().

◆ smgrdounlinkall()

void smgrdounlinkall ( SMgrRelation rels,
int  nrels,
bool  isRedo 
)

Definition at line 538 of file smgr.c.

539{
540 int i = 0;
542 ForkNumber forknum;
543
544 if (nrels == 0)
545 return;
546
547 /*
548 * It would be unsafe to process interrupts between DropRelationBuffers()
549 * and unlinking the underlying files. This probably should be a critical
550 * section, but we're not there yet.
551 */
553
554 /*
555 * Get rid of any remaining buffers for the relations. bufmgr will just
556 * drop them without bothering to write the contents.
557 */
558 DropRelationsAllBuffers(rels, nrels);
559
560 /*
561 * create an array which contains all relations to be dropped, and close
562 * each relation's forks at the smgr level while at it
563 */
565 for (i = 0; i < nrels; i++)
566 {
567 RelFileLocatorBackend rlocator = rels[i]->smgr_rlocator;
568 int which = rels[i]->smgr_which;
569
570 rlocators[i] = rlocator;
571
572 /* Close the forks at smgr level */
573 for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
574 smgrsw[which].smgr_close(rels[i], forknum);
575 }
576
577 /*
578 * Send a shared-inval message to force other backends to close any
579 * dangling smgr references they may have for these rels. We should do
580 * this before starting the actual unlinking, in case we fail partway
581 * through that step. Note that the sinval messages will eventually come
582 * back to this backend, too, and thereby provide a backstop that we
583 * closed our own smgr rel.
584 */
585 for (i = 0; i < nrels; i++)
587
588 /*
589 * Delete the physical file(s).
590 *
591 * Note: smgr_unlink must treat deletion failure as a WARNING, not an
592 * ERROR, because we've already decided to commit or abort the current
593 * xact.
594 */
595
596 for (i = 0; i < nrels; i++)
597 {
598 int which = rels[i]->smgr_which;
599
600 for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
601 smgrsw[which].smgr_unlink(rlocators[i], forknum, isRedo);
602 }
603
605
607}
void DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators)
Definition bufmgr.c:4800
#define palloc_array(type, count)
Definition fe_memutils.h:76
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition inval.c:1755
void pfree(void *pointer)
Definition mcxt.c:1616

References CacheInvalidateSmgr(), DropRelationsAllBuffers(), fb(), HOLD_INTERRUPTS, i, MAX_FORKNUM, palloc_array, pfree(), RESUME_INTERRUPTS, SMgrRelationData::smgr_rlocator, SMgrRelationData::smgr_which, and smgrsw.

Referenced by DropRelationFiles(), RelationSetNewRelfilenumber(), and smgrDoPendingDeletes().

◆ smgrexists()

◆ smgrextend()

void smgrextend ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
const void buffer,
bool  skipFsync 
)

Definition at line 620 of file smgr.c.

622{
624
625 smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
626 buffer, skipFsync);
627
628 /*
629 * Normally we expect this to increase nblocks by one, but if the cached
630 * value isn't as expected, just invalidate it so the next call asks the
631 * kernel.
632 */
633 if (reln->smgr_cached_nblocks[forknum] == blocknum)
634 reln->smgr_cached_nblocks[forknum] = blocknum + 1;
635 else
636 reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
637
639}
#define InvalidBlockNumber
Definition block.h:33
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition smgr.c:99

References fb(), HOLD_INTERRUPTS, InvalidBlockNumber, RESUME_INTERRUPTS, f_smgr::smgr_extend, and smgrsw.

Referenced by _hash_alloc_buckets(), RelationCopyStorageUsingBuffer(), and smgr_bulk_flush().

◆ smgrfd()

static int smgrfd ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
uint32 off 
)
static

Definition at line 989 of file smgr.c.

990{
991 int fd;
992
993 /*
994 * The caller needs to prevent interrupts from being processed, otherwise
995 * the FD could be closed prematurely.
996 */
998
999 fd = smgrsw[reln->smgr_which].smgr_fd(reln, forknum, blocknum, off);
1000
1001 return fd;
1002}
static int fd(const char *x, int i)
int(* smgr_fd)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
Definition smgr.c:125

References Assert, fb(), fd(), INTERRUPTS_CAN_BE_PROCESSED, f_smgr::smgr_fd, and smgrsw.

Referenced by smgr_aio_reopen().

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 974 of file smgr.c.

975{
977 smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
979}

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_immedsync, and smgrsw.

Referenced by smgr_bulk_finish().

◆ smgrinit()

void smgrinit ( void  )

Definition at line 188 of file smgr.c.

189{
190 int i;
191
193
194 for (i = 0; i < NSmgr; i++)
195 {
196 if (smgrsw[i].smgr_init)
197 smgrsw[i].smgr_init();
198 }
199
201
202 /* register the shutdown proc */
204}
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:316
static const int NSmgr
Definition smgr.c:154
static void smgrshutdown(int code, Datum arg)
Definition smgr.c:210
void(* smgr_init)(void)
Definition smgr.c:90

References HOLD_INTERRUPTS, i, NSmgr, on_proc_exit(), RESUME_INTERRUPTS, f_smgr::smgr_init, smgrshutdown(), and smgrsw.

Referenced by BaseInit().

◆ smgrmaxcombine()

uint32 smgrmaxcombine ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum 
)

Definition at line 697 of file smgr.c.

699{
700 uint32 ret;
701
703 ret = smgrsw[reln->smgr_which].smgr_maxcombine(reln, forknum, blocknum);
705
706 return ret;
707}
uint32(* smgr_maxcombine)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition smgr.c:105

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_maxcombine, and smgrsw.

Referenced by StartReadBuffersImpl().

◆ smgrnblocks()

BlockNumber smgrnblocks ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 819 of file smgr.c.

820{
821 BlockNumber result;
822
823 /* Check and return if we get the cached value for the number of blocks. */
824 result = smgrnblocks_cached(reln, forknum);
825 if (result != InvalidBlockNumber)
826 return result;
827
829
830 result = smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
831
832 reln->smgr_cached_nblocks[forknum] = result;
833
835
836 return result;
837}
uint32 BlockNumber
Definition block.h:31
BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:847
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:120

References fb(), HOLD_INTERRUPTS, InvalidBlockNumber, RESUME_INTERRUPTS, f_smgr::smgr_nblocks, smgrnblocks_cached(), and smgrsw.

Referenced by ExtendBufferedRelLocal(), ExtendBufferedRelShared(), ExtendBufferedRelTo(), FreeSpaceMapPrepareTruncateRel(), fsm_readbuf(), gistBuildCallback(), pg_truncate_visibility_map(), RelationCopyStorage(), RelationCopyStorageUsingBuffer(), RelationGetNumberOfBlocksInFork(), RelationTruncate(), ScanSourceDatabasePgClass(), smgr_bulk_start_smgr(), smgr_redo(), smgrDoPendingSyncs(), table_block_relation_size(), visibilitymap_prepare_truncate(), vm_readbuf(), XLogPrefetcherNextBlock(), and XLogReadBufferExtended().

◆ smgrnblocks_cached()

BlockNumber smgrnblocks_cached ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 847 of file smgr.c.

848{
849 /*
850 * For now, this function uses cached values only in recovery due to lack
851 * of a shared invalidation mechanism for changes in file size. Code
852 * elsewhere reads smgr_cached_nblocks and copes with stale data.
853 */
854 if (InRecovery && reln->smgr_cached_nblocks[forknum] != InvalidBlockNumber)
855 return reln->smgr_cached_nblocks[forknum];
856
857 return InvalidBlockNumber;
858}
bool InRecovery
Definition xlogutils.c:50

References fb(), InRecovery, and InvalidBlockNumber.

Referenced by DropRelationBuffers(), DropRelationsAllBuffers(), and smgrnblocks().

◆ smgropen()

SMgrRelation smgropen ( RelFileLocator  rlocator,
ProcNumber  backend 
)

Definition at line 240 of file smgr.c.

241{
244 bool found;
245
247
249
250 if (SMgrRelationHash == NULL)
251 {
252 /* First time through: initialize the hash table */
253 HASHCTL ctl;
254
256 ctl.entrysize = sizeof(SMgrRelationData);
257 SMgrRelationHash = hash_create("smgr relation table", 400,
260 }
261
262 /* Look up or create an entry */
263 brlocator.locator = rlocator;
264 brlocator.backend = backend;
266 &brlocator,
267 HASH_ENTER, &found);
268
269 /* Initialize it if not present before */
270 if (!found)
271 {
272 /* hash_search already filled in the lookup key */
273 reln->smgr_targblock = InvalidBlockNumber;
274 for (int i = 0; i <= MAX_FORKNUM; ++i)
275 reln->smgr_cached_nblocks[i] = InvalidBlockNumber;
276 reln->smgr_which = 0; /* we only have md.c at present */
277
278 /* it is not pinned yet */
279 reln->pincount = 0;
281
282 /* implementation-specific initialization */
283 smgrsw[reln->smgr_which].smgr_open(reln);
284 }
285
287
288 return reln;
289}
struct SMgrRelationData * SMgrRelation
Definition bufmgr.h:97
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
@ HASH_ENTER
Definition hsearch.h:114
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
static void dlist_init(dlist_head *head)
Definition ilist.h:314
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition ilist.h:364
tree ctl
Definition radixtree.h:1838
#define RelFileNumberIsValid(relnumber)
Definition relpath.h:27
Size keysize
Definition hsearch.h:75
RelFileNumber relNumber
void(* smgr_open)(SMgrRelation reln)
Definition smgr.c:92

References Assert, ctl, dlist_init(), dlist_push_tail(), fb(), HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HOLD_INTERRUPTS, i, InvalidBlockNumber, HASHCTL::keysize, MAX_FORKNUM, RelFileNumberIsValid, RelFileLocator::relNumber, RESUME_INTERRUPTS, f_smgr::smgr_open, SMgrRelationHash, smgrsw, and unpinned_relns.

Referenced by CreateAndCopyRelationData(), CreateFakeRelcacheEntry(), DropRelationFiles(), fill_seq_with_data(), FlushBuffer(), FlushLocalBuffer(), IssuePendingWritebacks(), mdsyncfiletag(), ReadBufferWithoutRelcache(), RelationCopyStorageUsingBuffer(), RelationCreateStorage(), RelationGetSmgr(), RelationSetNewRelfilenumber(), ScanSourceDatabasePgClass(), smgr_aio_reopen(), smgr_redo(), smgrDoPendingDeletes(), smgrDoPendingSyncs(), XLogPrefetcherNextBlock(), and XLogReadBufferExtended().

◆ smgrpin()

void smgrpin ( SMgrRelation  reln)

Definition at line 296 of file smgr.c.

297{
298 if (reln->pincount == 0)
299 dlist_delete(&reln->node);
300 reln->pincount++;
301}

References dlist_delete(), and fb().

Referenced by RelationGetSmgr().

◆ smgrprefetch()

bool smgrprefetch ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
int  nblocks 
)

Definition at line 678 of file smgr.c.

680{
681 bool ret;
682
684 ret = smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum, nblocks);
686
687 return ret;
688}
bool(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition smgr.c:103

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_prefetch, and smgrsw.

Referenced by PrefetchLocalBuffer(), PrefetchSharedBuffer(), and StartReadBuffersImpl().

◆ smgrreadv()

void smgrreadv ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
void **  buffers,
BlockNumber  nblocks 
)

Definition at line 721 of file smgr.c.

723{
725 smgrsw[reln->smgr_which].smgr_readv(reln, forknum, blocknum, buffers,
726 nblocks);
728}
void(* smgr_readv)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition smgr.c:107

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_readv, and smgrsw.

Referenced by smgrread().

◆ smgrregistersync()

void smgrregistersync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 940 of file smgr.c.

941{
943 smgrsw[reln->smgr_which].smgr_registersync(reln, forknum);
945}
void(* smgr_registersync)(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:124

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_registersync, and smgrsw.

Referenced by smgr_bulk_finish().

◆ smgrrelease()

void smgrrelease ( SMgrRelation  reln)

Definition at line 350 of file smgr.c.

351{
353
354 for (ForkNumber forknum = 0; forknum <= MAX_FORKNUM; forknum++)
355 {
356 smgrsw[reln->smgr_which].smgr_close(reln, forknum);
357 reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
358 }
359 reln->smgr_targblock = InvalidBlockNumber;
360
362}

References fb(), HOLD_INTERRUPTS, InvalidBlockNumber, MAX_FORKNUM, RESUME_INTERRUPTS, f_smgr::smgr_close, and smgrsw.

Referenced by smgrclose(), smgrreleaseall(), and smgrreleaserellocator().

◆ smgrreleaseall()

void smgrreleaseall ( void  )

Definition at line 412 of file smgr.c.

413{
414 HASH_SEQ_STATUS status;
416
417 /* Nothing to do if hashtable not set up */
418 if (SMgrRelationHash == NULL)
419 return;
420
421 /* seems unsafe to accept interrupts while iterating */
423
425
426 while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
427 {
429 }
430
432}
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380

References fb(), hash_seq_init(), hash_seq_search(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, SMgrRelationHash, and smgrrelease().

Referenced by ProcessBarrierSmgrRelease(), read_rel_block_ll(), and RelationCacheInvalidate().

◆ smgrreleaserellocator()

void smgrreleaserellocator ( RelFileLocatorBackend  rlocator)

Definition at line 443 of file smgr.c.

444{
446
447 /* Nothing to do if hashtable not set up */
448 if (SMgrRelationHash == NULL)
449 return;
450
452 &rlocator,
453 HASH_FIND, NULL);
454 if (reln != NULL)
456}
@ HASH_FIND
Definition hsearch.h:113

References fb(), HASH_FIND, hash_search(), SMgrRelationHash, and smgrrelease().

Referenced by LocalExecuteInvalidationMessage().

◆ smgrshutdown()

static void smgrshutdown ( int  code,
Datum  arg 
)
static

Definition at line 210 of file smgr.c.

211{
212 int i;
213
215
216 for (i = 0; i < NSmgr; i++)
217 {
218 if (smgrsw[i].smgr_shutdown)
220 }
221
223}
void(* smgr_shutdown)(void)
Definition smgr.c:91

References HOLD_INTERRUPTS, i, NSmgr, RESUME_INTERRUPTS, f_smgr::smgr_shutdown, and smgrsw.

Referenced by smgrinit().

◆ smgrstartreadv()

void smgrstartreadv ( PgAioHandle ioh,
SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
void **  buffers,
BlockNumber  nblocks 
)

Definition at line 753 of file smgr.c.

756{
758 smgrsw[reln->smgr_which].smgr_startreadv(ioh,
759 reln, forknum, blocknum, buffers,
760 nblocks);
762}
void(* smgr_startreadv)(PgAioHandle *ioh, SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition smgr.c:110

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_startreadv, and smgrsw.

Referenced by AsyncReadBuffers(), and read_rel_block_ll().

◆ smgrtruncate()

void smgrtruncate ( SMgrRelation  reln,
ForkNumber forknum,
int  nforks,
BlockNumber old_nblocks,
BlockNumber nblocks 
)

Definition at line 875 of file smgr.c.

877{
878 int i;
879
880 /*
881 * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
882 * just drop them without bothering to write the contents.
883 */
884 DropRelationBuffers(reln, forknum, nforks, nblocks);
885
886 /*
887 * Send a shared-inval message to force other backends to close any smgr
888 * references they may have for this rel. This is useful because they
889 * might have open file pointers to segments that got removed, and/or
890 * smgr_targblock variables pointing past the new rel end. (The inval
891 * message will come back to our backend, too, causing a
892 * probably-unnecessary local smgr flush. But we don't expect that this
893 * is a performance-critical path.) As in the unlink code, we want to be
894 * sure the message is sent before we start changing things on-disk.
895 */
896 CacheInvalidateSmgr(reln->smgr_rlocator);
897
898 /* Do the truncation */
899 for (i = 0; i < nforks; i++)
900 {
901 /* Make the cached size is invalid if we encounter an error. */
902 reln->smgr_cached_nblocks[forknum[i]] = InvalidBlockNumber;
903
904 smgrsw[reln->smgr_which].smgr_truncate(reln, forknum[i],
905 old_nblocks[i], nblocks[i]);
906
907 /*
908 * We might as well update the local smgr_cached_nblocks values. The
909 * smgr cache inval message that this function sent will cause other
910 * backends to invalidate their copies of smgr_cached_nblocks, and
911 * these ones too at the next command boundary. But ensure they aren't
912 * outright wrong until then.
913 *
914 * We can have nblocks > old_nblocks when a relation was truncated
915 * multiple times, a replica applied all the truncations, and later
916 * restarts from a restartpoint located before the truncations. The
917 * relation on disk will be the size of the last truncate. When
918 * replaying the first truncate, we will have nblocks > current size.
919 * In such cases, smgr_truncate does nothing, so set the cached size
920 * to the old size rather than the requested size.
921 */
922 reln->smgr_cached_nblocks[forknum[i]] =
923 nblocks[i] > old_nblocks[i] ? old_nblocks[i] : nblocks[i];
924 }
925}
void DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Definition bufmgr.c:4680
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber old_blocks, BlockNumber nblocks)
Definition smgr.c:121

References CacheInvalidateSmgr(), DropRelationBuffers(), fb(), i, InvalidBlockNumber, f_smgr::smgr_truncate, and smgrsw.

Referenced by pg_truncate_visibility_map(), RelationTruncate(), and smgr_redo().

◆ smgrunpin()

void smgrunpin ( SMgrRelation  reln)

Definition at line 311 of file smgr.c.

312{
313 Assert(reln->pincount > 0);
314 reln->pincount--;
315 if (reln->pincount == 0)
317}

References Assert, dlist_push_tail(), fb(), and unpinned_relns.

Referenced by RelationCloseSmgr().

◆ smgrwriteback()

void smgrwriteback ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
BlockNumber  nblocks 
)

Definition at line 805 of file smgr.c.

807{
809 smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
810 nblocks);
812}
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition smgr.c:118

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_writeback, and smgrsw.

Referenced by IssuePendingWritebacks().

◆ smgrwritev()

void smgrwritev ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
const void **  buffers,
BlockNumber  nblocks,
bool  skipFsync 
)

Definition at line 791 of file smgr.c.

793{
795 smgrsw[reln->smgr_which].smgr_writev(reln, forknum, blocknum,
796 buffers, nblocks, skipFsync);
798}
void(* smgr_writev)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition smgr.c:114

References fb(), HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_writev, and smgrsw.

Referenced by smgrwrite().

◆ smgrzeroextend()

void smgrzeroextend ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
int  nblocks,
bool  skipFsync 
)

Definition at line 649 of file smgr.c.

651{
653
654 smgrsw[reln->smgr_which].smgr_zeroextend(reln, forknum, blocknum,
655 nblocks, skipFsync);
656
657 /*
658 * Normally we expect this to increase the fork size by nblocks, but if
659 * the cached value isn't as expected, just invalidate it so the next call
660 * asks the kernel.
661 */
662 if (reln->smgr_cached_nblocks[forknum] == blocknum)
663 reln->smgr_cached_nblocks[forknum] = blocknum + nblocks;
664 else
665 reln->smgr_cached_nblocks[forknum] = InvalidBlockNumber;
666
668}
void(* smgr_zeroextend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition smgr.c:101

References fb(), HOLD_INTERRUPTS, InvalidBlockNumber, RESUME_INTERRUPTS, f_smgr::smgr_zeroextend, and smgrsw.

Referenced by ExtendBufferedRelLocal(), and ExtendBufferedRelShared().

Variable Documentation

◆ aio_smgr_target_info

const PgAioTargetInfo aio_smgr_target_info
Initial value:
= {
.name = "smgr",
.reopen = smgr_aio_reopen,
.describe_identity = smgr_aio_describe_identity,
}
static char * smgr_aio_describe_identity(const PgAioTargetData *sd)
Definition smgr.c:1104
static void smgr_aio_reopen(PgAioHandle *ioh)
Definition smgr.c:1064

Definition at line 172 of file smgr.c.

172 {
173 .name = "smgr",
174 .reopen = smgr_aio_reopen,
175 .describe_identity = smgr_aio_describe_identity,
176};

◆ NSmgr

const int NSmgr = lengthof(smgrsw)
static

Definition at line 154 of file smgr.c.

Referenced by smgrinit(), and smgrshutdown().

◆ SMgrRelationHash

HTAB* SMgrRelationHash = NULL
static

Definition at line 160 of file smgr.c.

Referenced by smgrdestroy(), smgropen(), smgrreleaseall(), and smgrreleaserellocator().

◆ smgrsw

const f_smgr smgrsw[]
static
Initial value:
= {
{
.smgr_init = mdinit,
.smgr_shutdown = NULL,
.smgr_open = mdopen,
.smgr_close = mdclose,
.smgr_create = mdcreate,
.smgr_exists = mdexists,
.smgr_unlink = mdunlink,
.smgr_extend = mdextend,
.smgr_zeroextend = mdzeroextend,
.smgr_prefetch = mdprefetch,
.smgr_maxcombine = mdmaxcombine,
.smgr_readv = mdreadv,
.smgr_startreadv = mdstartreadv,
.smgr_writev = mdwritev,
.smgr_writeback = mdwriteback,
.smgr_nblocks = mdnblocks,
.smgr_truncate = mdtruncate,
.smgr_immedsync = mdimmedsync,
.smgr_registersync = mdregistersync,
.smgr_fd = mdfd,
}
}
void mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition md.c:337
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber curnblk, BlockNumber nblocks)
Definition md.c:1290
void mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition md.c:1059
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition md.c:203
void mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition md.c:847
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition md.c:1223
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition md.c:222
int mdfd(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
Definition md.c:1483
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition md.c:487
void mdinit(void)
Definition md.c:190
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition md.c:713
void mdzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition md.c:552
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition md.c:1164
uint32 mdmaxcombine(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition md.c:833
void mdstartreadv(PgAioHandle *ioh, SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition md.c:985
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition md.c:736
void mdregistersync(SMgrRelation reln, ForkNumber forknum)
Definition md.c:1379
void mdopen(SMgrRelation reln)
Definition md.c:702
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition md.c:1430

Definition at line 128 of file smgr.c.

128 {
129 /* magnetic disk */
130 {
131 .smgr_init = mdinit,
132 .smgr_shutdown = NULL,
133 .smgr_open = mdopen,
134 .smgr_close = mdclose,
135 .smgr_create = mdcreate,
136 .smgr_exists = mdexists,
137 .smgr_unlink = mdunlink,
138 .smgr_extend = mdextend,
139 .smgr_zeroextend = mdzeroextend,
140 .smgr_prefetch = mdprefetch,
141 .smgr_maxcombine = mdmaxcombine,
142 .smgr_readv = mdreadv,
143 .smgr_startreadv = mdstartreadv,
144 .smgr_writev = mdwritev,
145 .smgr_writeback = mdwriteback,
146 .smgr_nblocks = mdnblocks,
147 .smgr_truncate = mdtruncate,
148 .smgr_immedsync = mdimmedsync,
149 .smgr_registersync = mdregistersync,
150 .smgr_fd = mdfd,
151 }
152};

Referenced by smgrcreate(), smgrdestroy(), smgrdosyncall(), smgrdounlinkall(), smgrexists(), smgrextend(), smgrfd(), smgrimmedsync(), smgrinit(), smgrmaxcombine(), smgrnblocks(), smgropen(), smgrprefetch(), smgrreadv(), smgrregistersync(), smgrrelease(), smgrshutdown(), smgrstartreadv(), smgrtruncate(), smgrwriteback(), smgrwritev(), and smgrzeroextend().

◆ unpinned_relns

dlist_head unpinned_relns
static

Definition at line 162 of file smgr.c.

Referenced by smgrdestroyall(), smgropen(), and smgrunpin().