PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 char * smgr_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

typedef struct f_smgr f_smgr

Function Documentation

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 1008 of file smgr.c.

1009{
1011}
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 1029 of file smgr.c.

1035{
1037
1039
1040 /* backend is implied via IO owner */
1041 sd->smgr.rlocator = smgr->smgr_rlocator.locator;
1042 sd->smgr.forkNum = forknum;
1043 sd->smgr.blockNum = blocknum;
1044 sd->smgr.nblocks = nblocks;
1045 sd->smgr.is_temp = SmgrIsTemp(smgr);
1046 /* Temp relations should never be fsync'd */
1047 sd->smgr.skip_fsync = skip_fsync && !SmgrIsTemp(smgr);
1048}
@ PGAIO_TID_SMGR
Definition: aio.h:120
PgAioTargetData * pgaio_io_get_target_data(PgAioHandle *ioh)
Definition: aio_target.c:72
void pgaio_io_set_target(PgAioHandle *ioh, PgAioTargetID targetid)
Definition: aio_target.c:63
#define SmgrIsTemp(smgr)
Definition: smgr.h:74
RelFileLocator locator
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:38
BlockNumber blockNum
Definition: aio_types.h:66
RelFileLocator rlocator
Definition: aio_types.h:65
BlockNumber nblocks
Definition: aio_types.h:67
struct PgAioTargetData::@124 smgr
ForkNumber forkNum
Definition: aio_types.h:68

References PgAioTargetData::blockNum, PgAioTargetData::forkNum, PgAioTargetData::is_temp, RelFileLocatorBackend::locator, PgAioTargetData::nblocks, pgaio_io_get_target_data(), pgaio_io_set_target(), PGAIO_TID_SMGR, PgAioTargetData::rlocator, PgAioTargetData::skip_fsync, PgAioTargetData::smgr, SMgrRelationData::smgr_rlocator, and SmgrIsTemp.

Referenced by mdstartreadv().

◆ ProcessBarrierSmgrRelease()

bool ProcessBarrierSmgrRelease ( void  )

Definition at line 1018 of file smgr.c.

1019{
1021 return true;
1022}
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 1095 of file smgr.c.

1096{
1097 RelPathStr path;
1098 char *desc;
1099
1100 path = relpathbackend(sd->smgr.rlocator,
1101 sd->smgr.is_temp ?
1103 sd->smgr.forkNum);
1104
1105 if (sd->smgr.nblocks == 0)
1106 desc = psprintf(_("file \"%s\""), path.str);
1107 else if (sd->smgr.nblocks == 1)
1108 desc = psprintf(_("block %u in file \"%s\""),
1109 sd->smgr.blockNum,
1110 path.str);
1111 else
1112 desc = psprintf(_("blocks %u..%u in file \"%s\""),
1113 sd->smgr.blockNum,
1114 sd->smgr.blockNum + sd->smgr.nblocks - 1,
1115 path.str);
1116
1117 return desc;
1118}
#define _(x)
Definition: elog.c:91
ProcNumber MyProcNumber
Definition: globals.c:91
#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 _, PgAioTargetData::blockNum, PgAioTargetData::forkNum, INVALID_PROC_NUMBER, PgAioTargetData::is_temp, MyProcNumber, PgAioTargetData::nblocks, psprintf(), relpathbackend, PgAioTargetData::rlocator, PgAioTargetData::smgr, and RelPathStr::str.

◆ smgr_aio_reopen()

static void smgr_aio_reopen ( PgAioHandle ioh)
static

Definition at line 1055 of file smgr.c.

1056{
1059 SMgrRelation reln;
1060 ProcNumber procno;
1061 uint32 off;
1062
1063 /*
1064 * The caller needs to prevent interrupts from being processed, otherwise
1065 * the FD could be closed again before we get to executing the IO.
1066 */
1068
1069 if (sd->smgr.is_temp)
1070 procno = pgaio_io_get_owner(ioh);
1071 else
1072 procno = INVALID_PROC_NUMBER;
1073
1074 reln = smgropen(sd->smgr.rlocator, procno);
1075 switch (pgaio_io_get_op(ioh))
1076 {
1077 case PGAIO_OP_INVALID:
1079 break;
1080 case PGAIO_OP_READV:
1081 od->read.fd = smgrfd(reln, sd->smgr.forkNum, sd->smgr.blockNum, &off);
1082 Assert(off == od->read.offset);
1083 break;
1084 case PGAIO_OP_WRITEV:
1085 od->write.fd = smgrfd(reln, sd->smgr.forkNum, sd->smgr.blockNum, &off);
1086 Assert(off == od->write.offset);
1087 break;
1088 }
1089}
ProcNumber pgaio_io_get_owner(PgAioHandle *ioh)
Definition: aio.c:343
@ 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 pg_unreachable()
Definition: c.h:332
uint32_t uint32
Definition: c.h:502
Assert(PointerIsAligned(start, uint64))
#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:980
uint64 offset
Definition: aio.h:140
int fd
Definition: aio.h:138
struct PgAioOpData::@123 write
struct PgAioOpData::@122 read

References Assert(), PgAioTargetData::blockNum, PgAioOpData::fd, PgAioTargetData::forkNum, INTERRUPTS_CAN_BE_PROCESSED, INVALID_PROC_NUMBER, PgAioTargetData::is_temp, PgAioOpData::offset, 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, PgAioOpData::read, PgAioTargetData::rlocator, PgAioTargetData::smgr, smgrfd(), smgropen(), and PgAioOpData::write.

◆ 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
int smgr_which
Definition: smgr.h:55
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:94

References HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_create, SMgrRelationData::smgr_which, 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:955
#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
dlist_node node
Definition: smgr.h:69
int pincount
Definition: smgr.h:68
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:93

References Assert(), dlist_delete(), elog, ERROR, HASH_REMOVE, hash_search(), HOLD_INTERRUPTS, MAX_FORKNUM, SMgrRelationData::node, SMgrRelationData::pincount, RESUME_INTERRUPTS, f_smgr::smgr_close, SMgrRelationData::smgr_rlocator, SMgrRelationData::smgr_which, 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(), 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:4963
int i
Definition: isn.c:77
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:123

References 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;
541 RelFileLocatorBackend *rlocators;
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 */
564 rlocators = palloc(sizeof(RelFileLocatorBackend) * nrels);
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++)
586 CacheInvalidateSmgr(rlocators[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
604 pfree(rlocators);
605
607}
void DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators)
Definition: bufmgr.c:4593
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition: inval.c:1741
void pfree(void *pointer)
Definition: mcxt.c:2147
void * palloc(Size size)
Definition: mcxt.c:1940

References CacheInvalidateSmgr(), DropRelationsAllBuffers(), HOLD_INTERRUPTS, i, MAX_FORKNUM, palloc(), 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
637
639}
#define InvalidBlockNumber
Definition: block.h:33
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:47
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.c:99

References HOLD_INTERRUPTS, InvalidBlockNumber, RESUME_INTERRUPTS, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_extend, SMgrRelationData::smgr_which, 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 980 of file smgr.c.

981{
982 int fd;
983
984 /*
985 * The caller needs to prevent interrupts from being processed, otherwise
986 * the FD could be closed prematurely.
987 */
989
990 fd = smgrsw[reln->smgr_which].smgr_fd(reln, forknum, blocknum, off);
991
992 return fd;
993}
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int(* smgr_fd)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
Definition: smgr.c:125

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

Referenced by smgr_aio_reopen().

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 965 of file smgr.c.

966{
968 smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
970}

References HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_immedsync, SMgrRelationData::smgr_which, 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:309
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 HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_maxcombine, SMgrRelationData::smgr_which, 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 HOLD_INTERRUPTS, InvalidBlockNumber, RESUME_INTERRUPTS, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_nblocks, SMgrRelationData::smgr_which, 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 InRecovery, InvalidBlockNumber, and SMgrRelationData::smgr_cached_nblocks.

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

◆ smgropen()

SMgrRelation smgropen ( RelFileLocator  rlocator,
ProcNumber  backend 
)

Definition at line 240 of file smgr.c.

241{
242 RelFileLocatorBackend brlocator;
243 SMgrRelation reln;
244 bool found;
245
247
249
250 if (SMgrRelationHash == NULL)
251 {
252 /* First time through: initialize the hash table */
253 HASHCTL ctl;
254
255 ctl.keysize = sizeof(RelFileLocatorBackend);
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 */
274 for (int i = 0; i <= MAX_FORKNUM; ++i)
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}
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
@ 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
struct RelFileLocatorBackend RelFileLocatorBackend
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
struct SMgrRelationData SMgrRelationData
SMgrRelationData * SMgrRelation
Definition: smgr.h:72
RelFileNumber relNumber
BlockNumber smgr_targblock
Definition: smgr.h:46
void(* smgr_open)(SMgrRelation reln)
Definition: smgr.c:92

References Assert(), RelFileLocatorBackend::backend, ctl, dlist_init(), dlist_push_tail(), HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HOLD_INTERRUPTS, i, InvalidBlockNumber, RelFileLocatorBackend::locator, MAX_FORKNUM, SMgrRelationData::node, SMgrRelationData::pincount, RelFileNumberIsValid, RelFileLocator::relNumber, RESUME_INTERRUPTS, SMgrRelationData::smgr_cached_nblocks, f_smgr::smgr_open, SMgrRelationData::smgr_targblock, SMgrRelationData::smgr_which, 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(), SMgrRelationData::node, and SMgrRelationData::pincount.

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 HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_prefetch, SMgrRelationData::smgr_which, 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 HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_readv, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgrread().

◆ smgrregistersync()

void smgrregistersync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 931 of file smgr.c.

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

References HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_registersync, SMgrRelationData::smgr_which, and smgrsw.

Referenced by smgr_bulk_finish().

◆ smgrrelease()

void smgrrelease ( SMgrRelation  reln)

◆ smgrreleaseall()

void smgrreleaseall ( void  )

Definition at line 412 of file smgr.c.

413{
414 HASH_SEQ_STATUS status;
415 SMgrRelation reln;
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 {
428 smgrrelease(reln);
429 }
430
432}
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1420
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385

References 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{
445 SMgrRelation reln;
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)
455 smgrrelease(reln);
456}
@ HASH_FIND
Definition: hsearch.h:113

References 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{
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 HOLD_INTERRUPTS, RESUME_INTERRUPTS, f_smgr::smgr_startreadv, SMgrRelationData::smgr_which, 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 */
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 reln->smgr_cached_nblocks[forknum[i]] = nblocks[i];
915 }
916}
void DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
Definition: bufmgr.c:4470
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber old_blocks, BlockNumber nblocks)
Definition: smgr.c:121

References CacheInvalidateSmgr(), DropRelationBuffers(), i, InvalidBlockNumber, SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_rlocator, f_smgr::smgr_truncate, SMgrRelationData::smgr_which, 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(), SMgrRelationData::node, SMgrRelationData::pincount, 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 HOLD_INTERRUPTS, RESUME_INTERRUPTS, SMgrRelationData::smgr_which, 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 HOLD_INTERRUPTS, RESUME_INTERRUPTS, SMgrRelationData::smgr_which, 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
666
668}
void(* smgr_zeroextend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: smgr.c:101

References HOLD_INTERRUPTS, InvalidBlockNumber, RESUME_INTERRUPTS, SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_which, 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:1095
static void smgr_aio_reopen(PgAioHandle *ioh)
Definition: smgr.c:1055

Definition at line 172 of file smgr.c.

◆ 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:327
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber curnblk, BlockNumber nblocks)
Definition: md.c:1277
void mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition: md.c:1049
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:193
void mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: md.c:837
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1213
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: md.c:212
int mdfd(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
Definition: md.c:1470
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:477
void mdinit(void)
Definition: md.c:180
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:703
void mdzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: md.c:542
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:1154
uint32 mdmaxcombine(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:823
void mdstartreadv(PgAioHandle *ioh, SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: md.c:975
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition: md.c:726
void mdregistersync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1366
void mdopen(SMgrRelation reln)
Definition: md.c:692
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1417

Definition at line 128 of file smgr.c.

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().