Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dfd0121

Browse files
committed
Move some md.c-specific logic from smgr.c to md.c.
Potential future SMGR implementations may not want to create tablespace directories when creating an SMGR relation. Move that logic to mdcreate(). Move the initialization of md-specific data structures from smgropen() to a new callback mdopen(). Author: Thomas Munro Reviewed-by: Shawn Debnath (as part of an earlier patch set) Discussion: https://postgr.es/m/CA%2BhUKG%2BOZqOiOuDm5tC5DyQZtJ3FH4%2BFSVMqtdC4P1atpJ%2Bqhg%40mail.gmail.com
1 parent 3093eb2 commit dfd0121

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

src/backend/storage/smgr/md.c

+31-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "miscadmin.h"
2929
#include "access/xlogutils.h"
3030
#include "access/xlog.h"
31+
#include "commands/tablespace.h"
3132
#include "pgstat.h"
3233
#include "postmaster/bgwriter.h"
3334
#include "storage/fd.h"
@@ -120,7 +121,7 @@ static MemoryContext MdCxt; /* context for all MdfdVec objects */
120121
/* local routines */
121122
static void mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum,
122123
bool isRedo);
123-
static MdfdVec *mdopen(SMgrRelation reln, ForkNumber forknum, int behavior);
124+
static MdfdVec *mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior);
124125
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
125126
MdfdVec *seg);
126127
static void register_unlink_segment(RelFileNodeBackend rnode, ForkNumber forknum,
@@ -165,7 +166,7 @@ mdexists(SMgrRelation reln, ForkNumber forkNum)
165166
*/
166167
mdclose(reln, forkNum);
167168

168-
return (mdopen(reln, forkNum, EXTENSION_RETURN_NULL) != NULL);
169+
return (mdopenfork(reln, forkNum, EXTENSION_RETURN_NULL) != NULL);
169170
}
170171

171172
/*
@@ -185,6 +186,19 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
185186

186187
Assert(reln->md_num_open_segs[forkNum] == 0);
187188

189+
/*
190+
* We may be using the target table space for the first time in this
191+
* database, so create a per-database subdirectory if needed.
192+
*
193+
* XXX this is a fairly ugly violation of module layering, but this seems
194+
* to be the best place to put the check. Maybe TablespaceCreateDbspace
195+
* should be here and not in commands/tablespace.c? But that would imply
196+
* importing a lot of stuff that smgr.c oughtn't know, either.
197+
*/
198+
TablespaceCreateDbspace(reln->smgr_rnode.node.spcNode,
199+
reln->smgr_rnode.node.dbNode,
200+
isRedo);
201+
188202
path = relpath(reln->smgr_rnode, forkNum);
189203

190204
fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
@@ -425,7 +439,7 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
425439
}
426440

427441
/*
428-
* mdopen() -- Open the specified relation.
442+
* mdopenfork() -- Open one fork of the specified relation.
429443
*
430444
* Note we only open the first segment, when there are multiple segments.
431445
*
@@ -435,7 +449,7 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
435449
* invent one out of whole cloth.
436450
*/
437451
static MdfdVec *
438-
mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
452+
mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
439453
{
440454
MdfdVec *mdfd;
441455
char *path;
@@ -474,6 +488,17 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
474488
return mdfd;
475489
}
476490

491+
/*
492+
* mdopen() -- Initialize newly-opened relation.
493+
*/
494+
void
495+
mdopen(SMgrRelation reln)
496+
{
497+
/* mark it not open */
498+
for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
499+
reln->md_num_open_segs[forknum] = 0;
500+
}
501+
477502
/*
478503
* mdclose() -- Close the specified relation, if it isn't closed already.
479504
*/
@@ -713,7 +738,7 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
713738
BlockNumber
714739
mdnblocks(SMgrRelation reln, ForkNumber forknum)
715740
{
716-
MdfdVec *v = mdopen(reln, forknum, EXTENSION_FAIL);
741+
MdfdVec *v = mdopenfork(reln, forknum, EXTENSION_FAIL);
717742
BlockNumber nblocks;
718743
BlockNumber segno = 0;
719744

@@ -1137,7 +1162,7 @@ _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
11371162
v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
11381163
else
11391164
{
1140-
v = mdopen(reln, forknum, behavior);
1165+
v = mdopenfork(reln, forknum, behavior);
11411166
if (!v)
11421167
return NULL; /* if behavior & EXTENSION_RETURN_NULL */
11431168
}

src/backend/storage/smgr/smgr.c

+4-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
#include "postgres.h"
1919

20-
#include "commands/tablespace.h"
2120
#include "lib/ilist.h"
2221
#include "storage/bufmgr.h"
2322
#include "storage/ipc.h"
@@ -41,6 +40,7 @@ typedef struct f_smgr
4140
{
4241
void (*smgr_init) (void); /* may be NULL */
4342
void (*smgr_shutdown) (void); /* may be NULL */
43+
void (*smgr_open) (SMgrRelation reln);
4444
void (*smgr_close) (SMgrRelation reln, ForkNumber forknum);
4545
void (*smgr_create) (SMgrRelation reln, ForkNumber forknum,
4646
bool isRedo);
@@ -68,6 +68,7 @@ static const f_smgr smgrsw[] = {
6868
{
6969
.smgr_init = mdinit,
7070
.smgr_shutdown = NULL,
71+
.smgr_open = mdopen,
7172
.smgr_close = mdclose,
7273
.smgr_create = mdcreate,
7374
.smgr_exists = mdexists,
@@ -170,18 +171,15 @@ smgropen(RelFileNode rnode, BackendId backend)
170171
/* Initialize it if not present before */
171172
if (!found)
172173
{
173-
int forknum;
174-
175174
/* hash_search already filled in the lookup key */
176175
reln->smgr_owner = NULL;
177176
reln->smgr_targblock = InvalidBlockNumber;
178177
reln->smgr_fsm_nblocks = InvalidBlockNumber;
179178
reln->smgr_vm_nblocks = InvalidBlockNumber;
180179
reln->smgr_which = 0; /* we only have md.c at present */
181180

182-
/* mark it not open */
183-
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
184-
reln->md_num_open_segs[forknum] = 0;
181+
/* implementation-specific initialization */
182+
smgrsw[reln->smgr_which].smgr_open(reln);
185183

186184
/* it has no owner yet */
187185
dlist_push_tail(&unowned_relns, &reln->node);
@@ -330,33 +328,10 @@ smgrclosenode(RelFileNodeBackend rnode)
330328
* Given an already-created (but presumably unused) SMgrRelation,
331329
* cause the underlying disk file or other storage for the fork
332330
* to be created.
333-
*
334-
* If isRedo is true, it is okay for the underlying file to exist
335-
* already because we are in a WAL replay sequence.
336331
*/
337332
void
338333
smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
339334
{
340-
/*
341-
* Exit quickly in WAL replay mode if we've already opened the file. If
342-
* it's open, it surely must exist.
343-
*/
344-
if (isRedo && reln->md_num_open_segs[forknum] > 0)
345-
return;
346-
347-
/*
348-
* We may be using the target table space for the first time in this
349-
* database, so create a per-database subdirectory if needed.
350-
*
351-
* XXX this is a fairly ugly violation of module layering, but this seems
352-
* to be the best place to put the check. Maybe TablespaceCreateDbspace
353-
* should be here and not in commands/tablespace.c? But that would imply
354-
* importing a lot of stuff that smgr.c oughtn't know, either.
355-
*/
356-
TablespaceCreateDbspace(reln->smgr_rnode.node.spcNode,
357-
reln->smgr_rnode.node.dbNode,
358-
isRedo);
359-
360335
smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo);
361336
}
362337

src/include/storage/md.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
/* md storage manager functionality */
2323
extern void mdinit(void);
24+
extern void mdopen(SMgrRelation reln);
2425
extern void mdclose(SMgrRelation reln, ForkNumber forknum);
2526
extern void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo);
2627
extern bool mdexists(SMgrRelation reln, ForkNumber forknum);

0 commit comments

Comments
 (0)