1 /*-------------------------------------------------------------------------
2  *
3  * smgr.h
4  *	  storage manager switch public interface declarations.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/smgr.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef SMGR_H
15 #define SMGR_H
16 
17 #include "lib/ilist.h"
18 #include "storage/block.h"
19 #include "storage/relfilenode.h"
20 
21 /*
22  * smgr.c maintains a table of SMgrRelation objects, which are essentially
23  * cached file handles.  An SMgrRelation is created (if not already present)
24  * by smgropen(), and destroyed by smgrclose().  Note that neither of these
25  * operations imply I/O, they just create or destroy a hashtable entry.
26  * (But smgrclose() may release associated resources, such as OS-level file
27  * descriptors.)
28  *
29  * An SMgrRelation may have an "owner", which is just a pointer to it from
30  * somewhere else; smgr.c will clear this pointer if the SMgrRelation is
31  * closed.  We use this to avoid dangling pointers from relcache to smgr
32  * without having to make the smgr explicitly aware of relcache.  There
33  * can't be more than one "owner" pointer per SMgrRelation, but that's
34  * all we need.
35  *
36  * SMgrRelations that do not have an "owner" are considered to be transient,
37  * and are deleted at end of transaction.
38  */
39 typedef struct SMgrRelationData
40 {
41 	/* rnode is the hashtable lookup key, so it must be first! */
42 	RelFileNodeBackend smgr_rnode;	/* relation physical identifier */
43 
44 	/* pointer to owning pointer, or NULL if none */
45 	struct SMgrRelationData **smgr_owner;
46 
47 	/*
48 	 * These next three fields are not actually used or manipulated by smgr,
49 	 * except that they are reset to InvalidBlockNumber upon a cache flush
50 	 * event (in particular, upon truncation of the relation).  Higher levels
51 	 * store cached state here so that it will be reset when truncation
52 	 * happens.  In all three cases, InvalidBlockNumber means "unknown".
53 	 */
54 	BlockNumber smgr_targblock; /* current insertion target block */
55 	BlockNumber smgr_fsm_nblocks;	/* last known size of fsm fork */
56 	BlockNumber smgr_vm_nblocks;	/* last known size of vm fork */
57 
58 	/* additional public fields may someday exist here */
59 
60 	/*
61 	 * Fields below here are intended to be private to smgr.c and its
62 	 * submodules.  Do not touch them from elsewhere.
63 	 */
64 	int			smgr_which;		/* storage manager selector */
65 
66 	/*
67 	 * for md.c; per-fork arrays of the number of open segments
68 	 * (md_num_open_segs) and the segments themselves (md_seg_fds).
69 	 */
70 	int			md_num_open_segs[MAX_FORKNUM + 1];
71 	struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
72 
73 	/* if unowned, list link in list of all unowned SMgrRelations */
74 	dlist_node	node;
75 } SMgrRelationData;
76 
77 typedef SMgrRelationData *SMgrRelation;
78 
79 #define SmgrIsTemp(smgr) \
80 	RelFileNodeBackendIsTemp((smgr)->smgr_rnode)
81 
82 extern void smgrinit(void);
83 extern SMgrRelation smgropen(RelFileNode rnode, BackendId backend);
84 extern bool smgrexists(SMgrRelation reln, ForkNumber forknum);
85 extern void smgrsetowner(SMgrRelation *owner, SMgrRelation reln);
86 extern void smgrclearowner(SMgrRelation *owner, SMgrRelation reln);
87 extern void smgrclose(SMgrRelation reln);
88 extern void smgrcloseall(void);
89 extern void smgrclosenode(RelFileNodeBackend rnode);
90 extern void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo);
91 extern void smgrdosyncall(SMgrRelation *rels, int nrels);
92 extern void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo);
93 extern void smgrextend(SMgrRelation reln, ForkNumber forknum,
94 					   BlockNumber blocknum, char *buffer, bool skipFsync);
95 extern bool smgrprefetch(SMgrRelation reln, ForkNumber forknum,
96 						 BlockNumber blocknum);
97 extern void smgrread(SMgrRelation reln, ForkNumber forknum,
98 					 BlockNumber blocknum, char *buffer);
99 extern void smgrwrite(SMgrRelation reln, ForkNumber forknum,
100 					  BlockNumber blocknum, char *buffer, bool skipFsync);
101 extern void smgrwriteback(SMgrRelation reln, ForkNumber forknum,
102 						  BlockNumber blocknum, BlockNumber nblocks);
103 extern BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum);
104 extern void smgrtruncate(SMgrRelation reln, ForkNumber *forknum,
105 						 int nforks, BlockNumber *nblocks);
106 extern void smgrimmedsync(SMgrRelation reln, ForkNumber forknum);
107 extern void AtEOXact_SMgr(void);
108 
109 #endif							/* SMGR_H */
110