PostgreSQL Source Code  git master
smgr.c File Reference
#include "postgres.h"
#include "commands/tablespace.h"
#include "storage/bufmgr.h"
#include "storage/ipc.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 add_to_unowned_list (SMgrRelation reln)
 
static void remove_from_unowned_list (SMgrRelation reln)
 
void smgrinit (void)
 
SMgrRelation smgropen (RelFileNode rnode, BackendId backend)
 
void smgrsetowner (SMgrRelation *owner, SMgrRelation reln)
 
void smgrclearowner (SMgrRelation *owner, SMgrRelation reln)
 
bool smgrexists (SMgrRelation reln, ForkNumber forknum)
 
void smgrclose (SMgrRelation reln)
 
void smgrcloseall (void)
 
void smgrclosenode (RelFileNodeBackend rnode)
 
void smgrcreate (SMgrRelation reln, ForkNumber forknum, bool isRedo)
 
void smgrdounlink (SMgrRelation reln, bool isRedo)
 
void smgrdounlinkall (SMgrRelation *rels, int nrels, bool isRedo)
 
void smgrdounlinkfork (SMgrRelation reln, ForkNumber forknum, bool isRedo)
 
void smgrextend (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
 
void smgrprefetch (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
 
void smgrread (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
 
void smgrwrite (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
 
void smgrwriteback (SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
 
BlockNumber smgrnblocks (SMgrRelation reln, ForkNumber forknum)
 
void smgrtruncate (SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
 
void smgrimmedsync (SMgrRelation reln, ForkNumber forknum)
 
void smgrpreckpt (void)
 
void smgrsync (void)
 
void smgrpostckpt (void)
 
void AtEOXact_SMgr (void)
 

Variables

static const f_smgr smgrsw []
 
static const int NSmgr = lengthof(smgrsw)
 
static HTABSMgrRelationHash = NULL
 
static SMgrRelation first_unowned_reln = NULL
 

Typedef Documentation

◆ f_smgr

Function Documentation

◆ add_to_unowned_list()

static void add_to_unowned_list ( SMgrRelation  reln)
static

Definition at line 244 of file smgr.c.

References first_unowned_reln, and SMgrRelationData::next_unowned_reln.

Referenced by smgrclearowner(), and smgropen().

245 {
246  /* place it at head of the list (to make smgrsetowner cheap) */
248  first_unowned_reln = reln;
249 }
struct SMgrRelationData * next_unowned_reln
Definition: smgr.h:75
static SMgrRelation first_unowned_reln
Definition: smgr.c:85

◆ AtEOXact_SMgr()

void AtEOXact_SMgr ( void  )

Definition at line 798 of file smgr.c.

References Assert, SMgrRelationData::smgr_owner, and smgrclose().

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

799 {
800  /*
801  * Zap all unowned SMgrRelations. We rely on smgrclose() to remove each
802  * one from the list.
803  */
804  while (first_unowned_reln != NULL)
805  {
808  }
809 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:296
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:46
#define Assert(condition)
Definition: c.h:670
static SMgrRelation first_unowned_reln
Definition: smgr.c:85

◆ remove_from_unowned_list()

static void remove_from_unowned_list ( SMgrRelation  reln)
static

Definition at line 265 of file smgr.c.

References cur, link(), and SMgrRelationData::next_unowned_reln.

Referenced by smgrclose(), and smgrsetowner().

266 {
269 
270  for (link = &first_unowned_reln, cur = *link;
271  cur != NULL;
272  link = &cur->next_unowned_reln, cur = *link)
273  {
274  if (cur == reln)
275  {
276  *link = cur->next_unowned_reln;
277  cur->next_unowned_reln = NULL;
278  break;
279  }
280  }
281 }
struct cursor * cur
Definition: ecpg.c:28
int link(const char *fromname, const char *toname)
struct SMgrRelationData * next_unowned_reln
Definition: smgr.h:75
static SMgrRelation first_unowned_reln
Definition: smgr.c:85

◆ smgrclearowner()

void smgrclearowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 222 of file smgr.c.

References add_to_unowned_list(), and SMgrRelationData::smgr_owner.

Referenced by FreeFakeRelcacheEntry().

223 {
224  /* Do nothing if the SMgrRelation object is not owned by the owner */
225  if (reln->smgr_owner != owner)
226  return;
227 
228  /* unset the owner's reference */
229  *owner = NULL;
230 
231  /* unset our reference to the owner */
232  reln->smgr_owner = NULL;
233 
234  add_to_unowned_list(reln);
235 }
static void add_to_unowned_list(SMgrRelation reln)
Definition: smgr.c:244
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:46

◆ smgrclose()

void smgrclose ( SMgrRelation  reln)

Definition at line 296 of file smgr.c.

References elog, ERROR, HASH_REMOVE, hash_search(), MAX_FORKNUM, remove_from_unowned_list(), f_smgr::smgr_close, SMgrRelationData::smgr_owner, SMgrRelationData::smgr_rnode, and SMgrRelationData::smgr_which.

Referenced by AtEOXact_SMgr(), ATExecSetTableSpace(), FinishPreparedTransaction(), smgrcloseall(), smgrclosenode(), smgrDoPendingDeletes(), xact_redo_abort(), and xact_redo_commit().

297 {
298  SMgrRelation *owner;
299  ForkNumber forknum;
300 
301  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
302  smgrsw[reln->smgr_which].smgr_close(reln, forknum);
303 
304  owner = reln->smgr_owner;
305 
306  if (!owner)
308 
310  (void *) &(reln->smgr_rnode),
311  HASH_REMOVE, NULL) == NULL)
312  elog(ERROR, "SMgrRelation hashtable corrupted");
313 
314  /*
315  * Unhook the owner pointer, if any. We do this last since in the remote
316  * possibility of failure above, the SMgrRelation object will still exist.
317  */
318  if (owner)
319  *owner = NULL;
320 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:42
static void remove_from_unowned_list(SMgrRelation reln)
Definition: smgr.c:265
static HTAB * SMgrRelationHash
Definition: smgr.c:83
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
#define ERROR
Definition: elog.h:43
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
ForkNumber
Definition: relpath.h:24
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:46
int smgr_which
Definition: smgr.h:65
#define MAX_FORKNUM
Definition: relpath.h:39
#define elog
Definition: elog.h:219

◆ smgrcloseall()

void smgrcloseall ( void  )

Definition at line 326 of file smgr.c.

References hash_seq_init(), hash_seq_search(), smgrclose(), and status().

Referenced by BackgroundWriterMain(), CheckpointerMain(), RelationCacheInvalidate(), RequestCheckpoint(), WalWriterMain(), and XLogDropDatabase().

327 {
329  SMgrRelation reln;
330 
331  /* Nothing to do if hashtable not set up */
332  if (SMgrRelationHash == NULL)
333  return;
334 
336 
337  while ((reln = (SMgrRelation) hash_seq_search(&status)) != NULL)
338  smgrclose(reln);
339 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:296
static HTAB * SMgrRelationHash
Definition: smgr.c:83
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1385
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1375
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ smgrclosenode()

void smgrclosenode ( RelFileNodeBackend  rnode)

Definition at line 350 of file smgr.c.

References HASH_FIND, hash_search(), and smgrclose().

Referenced by LocalExecuteInvalidationMessage(), and RelationSetNewRelfilenode().

351 {
352  SMgrRelation reln;
353 
354  /* Nothing to do if hashtable not set up */
355  if (SMgrRelationHash == NULL)
356  return;
357 
359  (void *) &rnode,
360  HASH_FIND, NULL);
361  if (reln != NULL)
362  smgrclose(reln);
363 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:296
SMgrRelationData * SMgrRelation
Definition: smgr.h:78
static HTAB * SMgrRelationHash
Definition: smgr.c:83
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902

◆ smgrcreate()

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

Definition at line 376 of file smgr.c.

References RelFileNode::dbNode, SMgrRelationData::md_num_open_segs, RelFileNodeBackend::node, f_smgr::smgr_create, SMgrRelationData::smgr_rnode, SMgrRelationData::smgr_which, RelFileNode::spcNode, and TablespaceCreateDbspace().

Referenced by ATExecSetTableSpace(), fsm_extend(), heap_create_init_fork(), index_build(), RelationCreateStorage(), smgr_redo(), vm_extend(), and XLogReadBufferExtended().

377 {
378  /*
379  * Exit quickly in WAL replay mode if we've already opened the file. If
380  * it's open, it surely must exist.
381  */
382  if (isRedo && reln->md_num_open_segs[forknum] > 0)
383  return;
384 
385  /*
386  * We may be using the target table space for the first time in this
387  * database, so create a per-database subdirectory if needed.
388  *
389  * XXX this is a fairly ugly violation of module layering, but this seems
390  * to be the best place to put the check. Maybe TablespaceCreateDbspace
391  * should be here and not in commands/tablespace.c? But that would imply
392  * importing a lot of stuff that smgr.c oughtn't know, either.
393  */
395  reln->smgr_rnode.node.dbNode,
396  isRedo);
397 
398  smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
399 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_create)(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:43
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
int smgr_which
Definition: smgr.h:65
RelFileNode node
Definition: relfilenode.h:74
void TablespaceCreateDbspace(Oid spcNode, Oid dbNode, bool isRedo)
Definition: tablespace.c:115
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:71

◆ smgrdounlink()

void smgrdounlink ( SMgrRelation  reln,
bool  isRedo 
)

Definition at line 414 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodesAllBuffers(), InvalidForkNumber, MAX_FORKNUM, f_smgr::smgr_close, SMgrRelationData::smgr_rnode, f_smgr::smgr_unlink, and SMgrRelationData::smgr_which.

Referenced by FinishPreparedTransaction(), xact_redo_abort(), and xact_redo_commit().

415 {
416  RelFileNodeBackend rnode = reln->smgr_rnode;
417  int which = reln->smgr_which;
418  ForkNumber forknum;
419 
420  /* Close the forks at smgr level */
421  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
422  smgrsw[which].smgr_close(reln, forknum);
423 
424  /*
425  * Get rid of any remaining buffers for the relation. bufmgr will just
426  * drop them without bothering to write the contents.
427  */
428  DropRelFileNodesAllBuffers(&rnode, 1);
429 
430  /*
431  * It'd be nice to tell the stats collector to forget it immediately, too.
432  * But we can't because we don't know the OID (and in cases involving
433  * relfilenode swaps, it's not always clear which table OID to forget,
434  * anyway).
435  */
436 
437  /*
438  * Send a shared-inval message to force other backends to close any
439  * dangling smgr references they may have for this rel. We should do this
440  * before starting the actual unlinking, in case we fail partway through
441  * that step. Note that the sinval message will eventually come back to
442  * this backend, too, and thereby provide a backstop that we closed our
443  * own smgr rel.
444  */
445  CacheInvalidateSmgr(rnode);
446 
447  /*
448  * Delete the physical file(s).
449  *
450  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
451  * ERROR, because we've already decided to commit or abort the current
452  * xact.
453  */
454  smgrsw[which].smgr_unlink(rnode, InvalidForkNumber, isRedo);
455 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1332
static const f_smgr smgrsw[]
Definition: smgr.c:68
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
void(* smgr_unlink)(RelFileNodeBackend rnode, ForkNumber forknum, bool isRedo)
Definition: smgr.c:46
ForkNumber
Definition: relpath.h:24
void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes)
Definition: bufmgr.c:2940
int smgr_which
Definition: smgr.h:65
#define MAX_FORKNUM
Definition: relpath.h:39

◆ smgrdounlinkall()

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

Definition at line 471 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodesAllBuffers(), i, MAX_FORKNUM, palloc(), pfree(), f_smgr::smgr_close, SMgrRelationData::smgr_rnode, f_smgr::smgr_unlink, and SMgrRelationData::smgr_which.

Referenced by smgrDoPendingDeletes().

472 {
473  int i = 0;
474  RelFileNodeBackend *rnodes;
475  ForkNumber forknum;
476 
477  if (nrels == 0)
478  return;
479 
480  /*
481  * create an array which contains all relations to be dropped, and close
482  * each relation's forks at the smgr level while at it
483  */
484  rnodes = palloc(sizeof(RelFileNodeBackend) * nrels);
485  for (i = 0; i < nrels; i++)
486  {
487  RelFileNodeBackend rnode = rels[i]->smgr_rnode;
488  int which = rels[i]->smgr_which;
489 
490  rnodes[i] = rnode;
491 
492  /* Close the forks at smgr level */
493  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
494  smgrsw[which].smgr_close(rels[i], forknum);
495  }
496 
497  /*
498  * Get rid of any remaining buffers for the relations. bufmgr will just
499  * drop them without bothering to write the contents.
500  */
501  DropRelFileNodesAllBuffers(rnodes, nrels);
502 
503  /*
504  * It'd be nice to tell the stats collector to forget them immediately,
505  * too. But we can't because we don't know the OIDs.
506  */
507 
508  /*
509  * Send a shared-inval message to force other backends to close any
510  * dangling smgr references they may have for these rels. We should do
511  * this before starting the actual unlinking, in case we fail partway
512  * through that step. Note that the sinval messages will eventually come
513  * back to this backend, too, and thereby provide a backstop that we
514  * closed our own smgr rel.
515  */
516  for (i = 0; i < nrels; i++)
517  CacheInvalidateSmgr(rnodes[i]);
518 
519  /*
520  * Delete the physical file(s).
521  *
522  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
523  * ERROR, because we've already decided to commit or abort the current
524  * xact.
525  */
526 
527  for (i = 0; i < nrels; i++)
528  {
529  int which = rels[i]->smgr_which;
530 
531  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
532  smgrsw[which].smgr_unlink(rnodes[i], forknum, isRedo);
533  }
534 
535  pfree(rnodes);
536 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1332
static const f_smgr smgrsw[]
Definition: smgr.c:68
void pfree(void *pointer)
Definition: mcxt.c:949
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
ForkNumber
Definition: relpath.h:24
void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes)
Definition: bufmgr.c:2940
int smgr_which
Definition: smgr.h:65
#define MAX_FORKNUM
Definition: relpath.h:39
void * palloc(Size size)
Definition: mcxt.c:848
int i

◆ smgrdounlinkfork()

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

Definition at line 549 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodeBuffers(), f_smgr::smgr_close, SMgrRelationData::smgr_rnode, f_smgr::smgr_unlink, and SMgrRelationData::smgr_which.

550 {
551  RelFileNodeBackend rnode = reln->smgr_rnode;
552  int which = reln->smgr_which;
553 
554  /* Close the fork at smgr level */
555  smgrsw[which].smgr_close(reln, forknum);
556 
557  /*
558  * Get rid of any remaining buffers for the fork. bufmgr will just drop
559  * them without bothering to write the contents.
560  */
561  DropRelFileNodeBuffers(rnode, forknum, 0);
562 
563  /*
564  * It'd be nice to tell the stats collector to forget it immediately, too.
565  * But we can't because we don't know the OID (and in cases involving
566  * relfilenode swaps, it's not always clear which table OID to forget,
567  * anyway).
568  */
569 
570  /*
571  * Send a shared-inval message to force other backends to close any
572  * dangling smgr references they may have for this rel. We should do this
573  * before starting the actual unlinking, in case we fail partway through
574  * that step. Note that the sinval message will eventually come back to
575  * this backend, too, and thereby provide a backstop that we closed our
576  * own smgr rel.
577  */
578  CacheInvalidateSmgr(rnode);
579 
580  /*
581  * Delete the physical file(s).
582  *
583  * Note: smgr_unlink must treat deletion failure as a WARNING, not an
584  * ERROR, because we've already decided to commit or abort the current
585  * xact.
586  */
587  smgrsw[which].smgr_unlink(rnode, forknum, isRedo);
588 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1332
static const f_smgr smgrsw[]
Definition: smgr.c:68
void DropRelFileNodeBuffers(RelFileNodeBackend rnode, ForkNumber forkNum, BlockNumber firstDelBlock)
Definition: bufmgr.c:2883
void(* smgr_close)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:42
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
void(* smgr_unlink)(RelFileNodeBackend rnode, ForkNumber forknum, bool isRedo)
Definition: smgr.c:46
int smgr_which
Definition: smgr.h:65

◆ smgrexists()

bool smgrexists ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 287 of file smgr.c.

References f_smgr::smgr_exists, and SMgrRelationData::smgr_which.

Referenced by ATExecSetTableSpace(), autoprewarm_database_main(), FreeSpaceMapTruncateRel(), fsm_extend(), fsm_readbuf(), index_build(), pg_prewarm(), RelationTruncate(), smgr_redo(), visibilitymap_truncate(), vm_extend(), and vm_readbuf().

288 {
289  return smgrsw[reln->smgr_which].smgr_exists(reln, forknum);
290 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
bool(* smgr_exists)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:45
int smgr_which
Definition: smgr.h:65

◆ smgrextend()

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

Definition at line 600 of file smgr.c.

References f_smgr::smgr_extend, and SMgrRelationData::smgr_which.

Referenced by _bt_blwritepage(), _hash_alloc_buckets(), copy_relation_data(), end_heap_rewrite(), fsm_extend(), raw_heap_insert(), ReadBuffer_common(), and vm_extend().

602 {
603  smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum,
604  buffer, skipFsync);
605 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
int smgr_which
Definition: smgr.h:65
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
void(* smgr_extend)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:48

◆ smgrimmedsync()

void smgrimmedsync ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 734 of file smgr.c.

References f_smgr::smgr_immedsync, and SMgrRelationData::smgr_which.

Referenced by _bt_load(), blbuildempty(), btbuildempty(), copy_relation_data(), heap_create_init_fork(), heap_sync(), and spgbuildempty().

735 {
736  smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
737 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_immedsync)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:61
int smgr_which
Definition: smgr.h:65

◆ smgrinit()

void smgrinit ( void  )

Definition at line 102 of file smgr.c.

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

Referenced by BaseInit().

103 {
104  int i;
105 
106  for (i = 0; i < NSmgr; i++)
107  {
108  if (smgrsw[i].smgr_init)
109  smgrsw[i].smgr_init();
110  }
111 
112  /* register the shutdown proc */
114 }
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:292
static const f_smgr smgrsw[]
Definition: smgr.c:68
static void smgrshutdown(int code, Datum arg)
Definition: smgr.c:120
void(* smgr_init)(void)
Definition: smgr.c:40
static const int NSmgr
Definition: smgr.c:76
int i

◆ smgrnblocks()

BlockNumber smgrnblocks ( SMgrRelation  reln,
ForkNumber  forknum 
)

Definition at line 672 of file smgr.c.

References f_smgr::smgr_nblocks, and SMgrRelationData::smgr_which.

Referenced by copy_relation_data(), FreeSpaceMapTruncateRel(), fsm_extend(), fsm_readbuf(), gistBuildCallback(), ReadBuffer_common(), RelationGetNumberOfBlocksInFork(), visibilitymap_truncate(), vm_extend(), vm_readbuf(), and XLogReadBufferExtended().

673 {
674  return smgrsw[reln->smgr_which].smgr_nblocks(reln, forknum);
675 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
BlockNumber(* smgr_nblocks)(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:58
int smgr_which
Definition: smgr.h:65

◆ smgropen()

SMgrRelation smgropen ( RelFileNode  rnode,
BackendId  backend 
)

Definition at line 137 of file smgr.c.

References add_to_unowned_list(), RelFileNodeBackend::backend, HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), InvalidBlockNumber, HASHCTL::keysize, MAX_FORKNUM, SMgrRelationData::md_num_open_segs, MemSet, RelFileNodeBackend::node, SMgrRelationData::smgr_fsm_nblocks, SMgrRelationData::smgr_owner, SMgrRelationData::smgr_targblock, SMgrRelationData::smgr_vm_nblocks, and SMgrRelationData::smgr_which.

Referenced by ATExecSetTableSpace(), FinishPreparedTransaction(), FlushBuffer(), IssuePendingWritebacks(), LocalBufferAlloc(), mdsync(), ReadBufferWithoutRelcache(), RelationCreateStorage(), smgr_redo(), smgrDoPendingDeletes(), xact_redo_abort(), xact_redo_commit(), and XLogReadBufferExtended().

138 {
139  RelFileNodeBackend brnode;
140  SMgrRelation reln;
141  bool found;
142 
143  if (SMgrRelationHash == NULL)
144  {
145  /* First time through: initialize the hash table */
146  HASHCTL ctl;
147 
148  MemSet(&ctl, 0, sizeof(ctl));
149  ctl.keysize = sizeof(RelFileNodeBackend);
150  ctl.entrysize = sizeof(SMgrRelationData);
151  SMgrRelationHash = hash_create("smgr relation table", 400,
152  &ctl, HASH_ELEM | HASH_BLOBS);
153  first_unowned_reln = NULL;
154  }
155 
156  /* Look up or create an entry */
157  brnode.node = rnode;
158  brnode.backend = backend;
160  (void *) &brnode,
161  HASH_ENTER, &found);
162 
163  /* Initialize it if not present before */
164  if (!found)
165  {
166  int forknum;
167 
168  /* hash_search already filled in the lookup key */
169  reln->smgr_owner = NULL;
173  reln->smgr_which = 0; /* we only have md.c at present */
174 
175  /* mark it not open */
176  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
177  reln->md_num_open_segs[forknum] = 0;
178 
179  /* it has no owner yet */
180  add_to_unowned_list(reln);
181  }
182 
183  return reln;
184 }
struct SMgrRelationData SMgrRelationData
BlockNumber smgr_vm_nblocks
Definition: smgr.h:57
#define HASH_ELEM
Definition: hsearch.h:87
SMgrRelationData * SMgrRelation
Definition: smgr.h:78
Size entrysize
Definition: hsearch.h:73
static HTAB * SMgrRelationHash
Definition: smgr.c:83
#define MemSet(start, val, len)
Definition: c.h:853
BlockNumber smgr_fsm_nblocks
Definition: smgr.h:56
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
static void add_to_unowned_list(SMgrRelation reln)
Definition: smgr.c:244
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:46
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
int smgr_which
Definition: smgr.h:65
struct RelFileNodeBackend RelFileNodeBackend
Size keysize
Definition: hsearch.h:72
RelFileNode node
Definition: relfilenode.h:74
BlockNumber smgr_targblock
Definition: smgr.h:55
BackendId backend
Definition: relfilenode.h:75
#define InvalidBlockNumber
Definition: block.h:33
#define MAX_FORKNUM
Definition: relpath.h:39
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:71
static SMgrRelation first_unowned_reln
Definition: smgr.c:85

◆ smgrpostckpt()

void smgrpostckpt ( void  )

Definition at line 774 of file smgr.c.

References i, NSmgr, and f_smgr::smgr_post_ckpt.

Referenced by CreateCheckPoint().

775 {
776  int i;
777 
778  for (i = 0; i < NSmgr; i++)
779  {
780  if (smgrsw[i].smgr_post_ckpt)
782  }
783 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_post_ckpt)(void)
Definition: smgr.c:64
static const int NSmgr
Definition: smgr.c:76
int i

◆ smgrpreckpt()

void smgrpreckpt ( void  )

Definition at line 744 of file smgr.c.

References i, NSmgr, and f_smgr::smgr_pre_ckpt.

Referenced by CreateCheckPoint().

745 {
746  int i;
747 
748  for (i = 0; i < NSmgr; i++)
749  {
750  if (smgrsw[i].smgr_pre_ckpt)
752  }
753 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
static const int NSmgr
Definition: smgr.c:76
int i
void(* smgr_pre_ckpt)(void)
Definition: smgr.c:62

◆ smgrprefetch()

void smgrprefetch ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum 
)

Definition at line 611 of file smgr.c.

References f_smgr::smgr_prefetch, and SMgrRelationData::smgr_which.

Referenced by LocalPrefetchBuffer(), and PrefetchBuffer().

612 {
613  smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum);
614 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_prefetch)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:50
int smgr_which
Definition: smgr.h:65

◆ smgrread()

void smgrread ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
char *  buffer 
)

Definition at line 625 of file smgr.c.

References f_smgr::smgr_read, and SMgrRelationData::smgr_which.

Referenced by copy_relation_data(), pg_prewarm(), and ReadBuffer_common().

627 {
628  smgrsw[reln->smgr_which].smgr_read(reln, forknum, blocknum, buffer);
629 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_read)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: smgr.c:52
int smgr_which
Definition: smgr.h:65
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214

◆ smgrsetowner()

void smgrsetowner ( SMgrRelation owner,
SMgrRelation  reln 
)

Definition at line 193 of file smgr.c.

References Assert, remove_from_unowned_list(), and SMgrRelationData::smgr_owner.

194 {
195  /* We don't support "disowning" an SMgrRelation here, use smgrclearowner */
196  Assert(owner != NULL);
197 
198  /*
199  * First, unhook any old owner. (Normally there shouldn't be any, but it
200  * seems possible that this can happen during swap_relation_files()
201  * depending on the order of processing. It's ok to close the old
202  * relcache entry early in that case.)
203  *
204  * If there isn't an old owner, then the reln should be in the unowned
205  * list, and we need to remove it.
206  */
207  if (reln->smgr_owner)
208  *(reln->smgr_owner) = NULL;
209  else
211 
212  /* Now establish the ownership relationship. */
213  reln->smgr_owner = owner;
214  *owner = reln;
215 }
static void remove_from_unowned_list(SMgrRelation reln)
Definition: smgr.c:265
struct SMgrRelationData ** smgr_owner
Definition: smgr.h:46
#define Assert(condition)
Definition: c.h:670

◆ smgrshutdown()

static void smgrshutdown ( int  code,
Datum  arg 
)
static

Definition at line 120 of file smgr.c.

References i, NSmgr, and f_smgr::smgr_shutdown.

Referenced by smgrinit().

121 {
122  int i;
123 
124  for (i = 0; i < NSmgr; i++)
125  {
126  if (smgrsw[i].smgr_shutdown)
128  }
129 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
static const int NSmgr
Definition: smgr.c:76
int i
void(* smgr_shutdown)(void)
Definition: smgr.c:41

◆ smgrsync()

void smgrsync ( void  )

Definition at line 759 of file smgr.c.

References i, NSmgr, and f_smgr::smgr_sync.

Referenced by CheckPointBuffers().

760 {
761  int i;
762 
763  for (i = 0; i < NSmgr; i++)
764  {
765  if (smgrsw[i].smgr_sync)
766  smgrsw[i].smgr_sync();
767  }
768 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
void(* smgr_sync)(void)
Definition: smgr.c:63
static const int NSmgr
Definition: smgr.c:76
int i

◆ smgrtruncate()

void smgrtruncate ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  nblocks 
)

Definition at line 684 of file smgr.c.

References CacheInvalidateSmgr(), DropRelFileNodeBuffers(), SMgrRelationData::smgr_rnode, f_smgr::smgr_truncate, and SMgrRelationData::smgr_which.

Referenced by FreeSpaceMapTruncateRel(), RelationTruncate(), smgr_redo(), and visibilitymap_truncate().

685 {
686  /*
687  * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will
688  * just drop them without bothering to write the contents.
689  */
690  DropRelFileNodeBuffers(reln->smgr_rnode, forknum, nblocks);
691 
692  /*
693  * Send a shared-inval message to force other backends to close any smgr
694  * references they may have for this rel. This is useful because they
695  * might have open file pointers to segments that got removed, and/or
696  * smgr_targblock variables pointing past the new rel end. (The inval
697  * message will come back to our backend, too, causing a
698  * probably-unnecessary local smgr flush. But we don't expect that this
699  * is a performance-critical path.) As in the unlink code, we want to be
700  * sure the message is sent before we start changing things on-disk.
701  */
703 
704  /*
705  * Do the truncation.
706  */
707  smgrsw[reln->smgr_which].smgr_truncate(reln, forknum, nblocks);
708 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1332
void(* smgr_truncate)(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:59
static const f_smgr smgrsw[]
Definition: smgr.c:68
void DropRelFileNodeBuffers(RelFileNodeBackend rnode, ForkNumber forkNum, BlockNumber firstDelBlock)
Definition: bufmgr.c:2883
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
int smgr_which
Definition: smgr.h:65

◆ smgrwrite()

void smgrwrite ( SMgrRelation  reln,
ForkNumber  forknum,
BlockNumber  blocknum,
char *  buffer,
bool  skipFsync 
)

Definition at line 647 of file smgr.c.

References SMgrRelationData::smgr_which, and f_smgr::smgr_write.

Referenced by _bt_blwritepage(), blbuildempty(), btbuildempty(), FlushBuffer(), FlushRelationBuffers(), LocalBufferAlloc(), and spgbuildempty().

649 {
650  smgrsw[reln->smgr_which].smgr_write(reln, forknum, blocknum,
651  buffer, skipFsync);
652 }
void(* smgr_write)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:54
static const f_smgr smgrsw[]
Definition: smgr.c:68
int smgr_which
Definition: smgr.h:65
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214

◆ smgrwriteback()

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

Definition at line 660 of file smgr.c.

References SMgrRelationData::smgr_which, and f_smgr::smgr_writeback.

Referenced by IssuePendingWritebacks().

662 {
663  smgrsw[reln->smgr_which].smgr_writeback(reln, forknum, blocknum,
664  nblocks);
665 }
static const f_smgr smgrsw[]
Definition: smgr.c:68
int smgr_which
Definition: smgr.h:65
void(* smgr_writeback)(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:56

Variable Documentation

◆ first_unowned_reln

SMgrRelation first_unowned_reln = NULL
static

Definition at line 85 of file smgr.c.

Referenced by add_to_unowned_list().

◆ NSmgr

const int NSmgr = lengthof(smgrsw)
static

Definition at line 76 of file smgr.c.

Referenced by smgrinit(), smgrpostckpt(), smgrpreckpt(), smgrshutdown(), and smgrsync().

◆ SMgrRelationHash

HTAB* SMgrRelationHash = NULL
static

Definition at line 83 of file smgr.c.

◆ smgrsw

const f_smgr smgrsw[]
static
Initial value:
= {
}
}
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1025
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:873
void mdsync(void)
Definition: md.c:1054
void mdinit(void)
Definition: md.c:206
void mdpreckpt(void)
Definition: md.c:1346
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:495
void mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
Definition: md.c:387
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:682
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:928
bool mdexists(SMgrRelation reln, ForkNumber forkNum)
Definition: md.c:277
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:802
void mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:659
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:628
void mdpostckpt(void)
Definition: md.c:1361
void mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
Definition: md.c:294
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: md.c:731

Definition at line 68 of file smgr.c.