1 /*-------------------------------------------------------------------------
4 * This code manages relations that reside on magnetic disk.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
21 #include "catalog/catalog.h"
22 #include "miscadmin.h"
23 #include "postmaster/bgwriter.h"
24 #include "storage/fd.h"
25 #include "storage/bufmgr.h"
26 #include "storage/relfilenode.h"
27 #include "storage/smgr.h"
28 #include "utils/hsearch.h"
29 #include "utils/memutils.h"
32 /* interval for calling AbsorbFsyncRequests in mdsync */
33 #define FSYNCS_PER_ABSORB 10
35 /* special values for the segno arg to RememberFsyncRequest */
36 #define FORGET_RELATION_FSYNC (InvalidBlockNumber)
37 #define FORGET_DATABASE_FSYNC (InvalidBlockNumber-1)
38 #define UNLINK_RELATION_REQUEST (InvalidBlockNumber-2)
41 * On Windows, we have to interpret EACCES as possibly meaning the same as
42 * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
43 * that's what you get. Ugh. This code is designed so that we don't
44 * actually believe these cases are okay without further evidence (namely,
45 * a pending fsync request getting revoked ... see mdsync).
48 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
50 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
54 * The magnetic disk storage manager keeps track of open file
55 * descriptors in its own descriptor pool. This is done to make it
56 * easier to support relations that are larger than the operating
57 * system's file size limit (often 2GBytes). In order to do that,
58 * we break relations up into "segment" files that are each shorter than
59 * the OS file size limit. The segment size is set by the RELSEG_SIZE
60 * configuration constant in pg_config.h.
62 * On disk, a relation must consist of consecutively numbered segment
63 * files in the pattern
64 * -- Zero or more full segments of exactly RELSEG_SIZE blocks each
65 * -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
66 * -- Optionally, any number of inactive segments of size 0 blocks.
67 * The full and partial segments are collectively the "active" segments.
68 * Inactive segments are those that once contained data but are currently
69 * not needed because of an mdtruncate() operation. The reason for leaving
70 * them present at size zero, rather than unlinking them, is that other
71 * backends and/or the bgwriter might be holding open file references to
72 * such segments. If the relation expands again after mdtruncate(), such
73 * that a deactivated segment becomes active again, it is important that
74 * such file references still be valid --- else data might get written
75 * out to an unlinked old copy of a segment file that will eventually
78 * The file descriptor pointer (md_fd field) stored in the SMgrRelation
79 * cache is, therefore, just the head of a list of MdfdVec objects, one
80 * per segment. But note the md_fd pointer can be NULL, indicating
83 * Also note that mdfd_chain == NULL does not necessarily mean the relation
84 * doesn't have another segment after this one; we may just not have
85 * opened the next segment yet. (We could not have "all segments are
86 * in the chain" as an invariant anyway, since another backend could
87 * extend the relation when we weren't looking.) We do not make chain
88 * entries for inactive segments, however; as soon as we find a partial
89 * segment, we assume that any subsequent segments are inactive.
91 * All MdfdVec objects are palloc'd in the MdCxt memory context.
94 typedef struct _MdfdVec
96 File mdfd_vfd
; /* fd number in fd.c's pool */
97 BlockNumber mdfd_segno
; /* segment number, from 0 */
98 struct _MdfdVec
*mdfd_chain
; /* next segment, or NULL */
101 static MemoryContext MdCxt
; /* context for all md.c allocations */
105 * In some contexts (currently, standalone backends and the bgwriter process)
106 * we keep track of pending fsync operations: we need to remember all relation
107 * segments that have been written since the last checkpoint, so that we can
108 * fsync them down to disk before completing the next checkpoint. This hash
109 * table remembers the pending operations. We use a hash table mostly as
110 * a convenient way of eliminating duplicate requests.
112 * We use a similar mechanism to remember no-longer-needed files that can
113 * be deleted after the next checkpoint, but we use a linked list instead of
114 * a hash table, because we don't expect there to be any duplicate requests.
116 * (Regular backends do not track pending operations locally, but forward
117 * them to the bgwriter.)
121 RelFileNode rnode
; /* the targeted relation */
123 BlockNumber segno
; /* which segment */
124 } PendingOperationTag
;
126 typedef uint16 CycleCtr
; /* can be any convenient integer size */
130 PendingOperationTag tag
; /* hash table key (must be first!) */
131 bool canceled
; /* T => request canceled, not yet removed */
132 CycleCtr cycle_ctr
; /* mdsync_cycle_ctr when request was made */
133 } PendingOperationEntry
;
137 RelFileNode rnode
; /* the dead relation to delete */
138 CycleCtr cycle_ctr
; /* mdckpt_cycle_ctr when request was made */
139 } PendingUnlinkEntry
;
141 static HTAB
*pendingOpsTable
= NULL
;
142 static List
*pendingUnlinks
= NIL
;
144 static CycleCtr mdsync_cycle_ctr
= 0;
145 static CycleCtr mdckpt_cycle_ctr
= 0;
148 typedef enum /* behavior for mdopen & _mdfd_getseg */
150 EXTENSION_FAIL
, /* ereport if segment not present */
151 EXTENSION_RETURN_NULL
, /* return NULL if not present */
152 EXTENSION_CREATE
/* create new segments as needed */
156 static MdfdVec
*mdopen(SMgrRelation reln
, ForkNumber forknum
,
157 ExtensionBehavior behavior
);
158 static void register_dirty_segment(SMgrRelation reln
, ForkNumber forknum
,
160 static void register_unlink(RelFileNode rnode
);
161 static MdfdVec
*_fdvec_alloc(void);
162 static MdfdVec
*_mdfd_openseg(SMgrRelation reln
, ForkNumber forkno
,
163 BlockNumber segno
, int oflags
);
164 static MdfdVec
*_mdfd_getseg(SMgrRelation reln
, ForkNumber forkno
,
165 BlockNumber blkno
, bool isTemp
, ExtensionBehavior behavior
);
166 static BlockNumber
_mdnblocks(SMgrRelation reln
, ForkNumber forknum
,
171 * mdinit() -- Initialize private state for magnetic disk storage manager.
176 MdCxt
= AllocSetContextCreate(TopMemoryContext
,
178 ALLOCSET_DEFAULT_MINSIZE
,
179 ALLOCSET_DEFAULT_INITSIZE
,
180 ALLOCSET_DEFAULT_MAXSIZE
);
183 * Create pending-operations hashtable if we need it. Currently, we need
184 * it if we are standalone (not under a postmaster) OR if we are a
185 * bootstrap-mode subprocess of a postmaster (that is, a startup or
188 if (!IsUnderPostmaster
|| IsBootstrapProcessingMode())
192 MemSet(&hash_ctl
, 0, sizeof(hash_ctl
));
193 hash_ctl
.keysize
= sizeof(PendingOperationTag
);
194 hash_ctl
.entrysize
= sizeof(PendingOperationEntry
);
195 hash_ctl
.hash
= tag_hash
;
196 hash_ctl
.hcxt
= MdCxt
;
197 pendingOpsTable
= hash_create("Pending Ops Table",
200 HASH_ELEM
| HASH_FUNCTION
| HASH_CONTEXT
);
201 pendingUnlinks
= NIL
;
206 * mdexists() -- Does the physical file exist?
208 * Note: this will return true for lingering files, with pending deletions
211 mdexists(SMgrRelation reln
, ForkNumber forkNum
)
214 * Close it first, to ensure that we notice if the fork has been
215 * unlinked since we opened it.
217 mdclose(reln
, forkNum
);
219 return (mdopen(reln
, forkNum
, EXTENSION_RETURN_NULL
) != NULL
);
223 * mdcreate() -- Create a new relation on magnetic disk.
225 * If isRedo is true, it's okay for the relation to exist already.
228 mdcreate(SMgrRelation reln
, ForkNumber forkNum
, bool isRedo
)
233 if (isRedo
&& reln
->md_fd
[forkNum
] != NULL
)
234 return; /* created and opened already... */
236 Assert(reln
->md_fd
[forkNum
] == NULL
);
238 path
= relpath(reln
->smgr_rnode
, forkNum
);
240 fd
= PathNameOpenFile(path
, O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
, 0600);
244 int save_errno
= errno
;
247 * During bootstrap, there are cases where a system relation will be
248 * accessed (by internal backend processes) before the bootstrap
249 * script nominally creates it. Therefore, allow the file to exist
250 * already, even if isRedo is not set. (See also mdopen)
252 if (isRedo
|| IsBootstrapProcessingMode())
253 fd
= PathNameOpenFile(path
, O_RDWR
| PG_BINARY
, 0600);
257 /* be sure to report the error reported by create, not open */
260 (errcode_for_file_access(),
261 errmsg("could not create relation %u/%u/%u/%u: %m",
262 reln
->smgr_rnode
.spcNode
,
263 reln
->smgr_rnode
.dbNode
,
264 reln
->smgr_rnode
.relNode
,
271 reln
->md_fd
[forkNum
] = _fdvec_alloc();
273 reln
->md_fd
[forkNum
]->mdfd_vfd
= fd
;
274 reln
->md_fd
[forkNum
]->mdfd_segno
= 0;
275 reln
->md_fd
[forkNum
]->mdfd_chain
= NULL
;
279 * mdunlink() -- Unlink a relation.
281 * Note that we're passed a RelFileNode --- by the time this is called,
282 * there won't be an SMgrRelation hashtable entry anymore.
284 * Actually, we don't unlink the first segment file of the relation, but
285 * just truncate it to zero length, and record a request to unlink it after
286 * the next checkpoint. Additional segments can be unlinked immediately,
287 * however. Leaving the empty file in place prevents that relfilenode
288 * number from being reused. The scenario this protects us from is:
289 * 1. We delete a relation (and commit, and actually remove its file).
290 * 2. We create a new relation, which by chance gets the same relfilenode as
291 * the just-deleted one (OIDs must've wrapped around for that to happen).
292 * 3. We crash before another checkpoint occurs.
293 * During replay, we would delete the file and then recreate it, which is fine
294 * if the contents of the file were repopulated by subsequent WAL entries.
295 * But if we didn't WAL-log insertions, but instead relied on fsyncing the
296 * file after populating it (as for instance CLUSTER and CREATE INDEX do),
297 * the contents of the file would be lost forever. By leaving the empty file
298 * until after the next checkpoint, we prevent reassignment of the relfilenode
299 * number until it's safe, because relfilenode assignment skips over any
302 * If isRedo is true, it's okay for the relation to be already gone.
303 * Also, we should remove the file immediately instead of queuing a request
304 * for later, since during redo there's no possibility of creating a
305 * conflicting relation.
307 * Note: any failure should be reported as WARNING not ERROR, because
308 * we are usually not in a transaction anymore when this is called.
311 mdunlink(RelFileNode rnode
, ForkNumber forkNum
, bool isRedo
)
317 * We have to clean out any pending fsync requests for the doomed
318 * relation, else the next mdsync() will fail.
320 ForgetRelationFsyncRequests(rnode
, forkNum
);
322 path
= relpath(rnode
, forkNum
);
325 * Delete or truncate the first segment.
327 if (isRedo
|| forkNum
!= MAIN_FORKNUM
)
331 /* truncate(2) would be easier here, but Windows hasn't got it */
334 fd
= BasicOpenFile(path
, O_RDWR
| PG_BINARY
, 0);
339 ret
= ftruncate(fd
, 0);
349 if (!isRedo
|| errno
!= ENOENT
)
351 (errcode_for_file_access(),
352 errmsg("could not remove relation %u/%u/%u/%u: %m",
360 * Delete any additional segments.
364 char *segpath
= (char *) palloc(strlen(path
) + 12);
368 * Note that because we loop until getting ENOENT, we will correctly
369 * remove all inactive segments as well as active ones.
371 for (segno
= 1;; segno
++)
373 sprintf(segpath
, "%s.%u", path
, segno
);
374 if (unlink(segpath
) < 0)
376 /* ENOENT is expected after the last segment... */
379 (errcode_for_file_access(),
380 errmsg("could not remove segment %u of relation %u/%u/%u/%u: %m",
394 /* Register request to unlink first segment later */
395 if (!isRedo
&& forkNum
== MAIN_FORKNUM
)
396 register_unlink(rnode
);
400 * mdextend() -- Add a block to the specified relation.
402 * The semantics are nearly the same as mdwrite(): write at the
403 * specified position. However, this is to be used for the case of
404 * extending a relation (i.e., blocknum is at or beyond the current
405 * EOF). Note that we assume writing a block beyond current EOF
406 * causes intervening file space to become filled with zeroes.
409 mdextend(SMgrRelation reln
, ForkNumber forknum
, BlockNumber blocknum
,
410 char *buffer
, bool isTemp
)
416 /* This assert is too expensive to have on normally ... */
417 #ifdef CHECK_WRITE_VS_EXTEND
418 Assert(blocknum
>= mdnblocks(reln
, forknum
));
422 * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
423 * more --- we mustn't create a block whose number actually is
424 * InvalidBlockNumber.
426 if (blocknum
== InvalidBlockNumber
)
428 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
429 errmsg("cannot extend relation %u/%u/%u/%u beyond %u blocks",
430 reln
->smgr_rnode
.spcNode
,
431 reln
->smgr_rnode
.dbNode
,
432 reln
->smgr_rnode
.relNode
,
434 InvalidBlockNumber
)));
436 v
= _mdfd_getseg(reln
, forknum
, blocknum
, isTemp
, EXTENSION_CREATE
);
438 seekpos
= (off_t
) BLCKSZ
* (blocknum
% ((BlockNumber
) RELSEG_SIZE
));
439 Assert(seekpos
< (off_t
) BLCKSZ
* RELSEG_SIZE
);
442 * Note: because caller usually obtained blocknum by calling mdnblocks,
443 * which did a seek(SEEK_END), this seek is often redundant and will be
444 * optimized away by fd.c. It's not redundant, however, if there is a
445 * partial page at the end of the file. In that case we want to try to
446 * overwrite the partial page with a full page. It's also not redundant
447 * if bufmgr.c had to dump another buffer of the same file to make room
448 * for the new page's buffer.
450 if (FileSeek(v
->mdfd_vfd
, seekpos
, SEEK_SET
) != seekpos
)
452 (errcode_for_file_access(),
453 errmsg("could not seek to block %u of relation %u/%u/%u/%u: %m",
455 reln
->smgr_rnode
.spcNode
,
456 reln
->smgr_rnode
.dbNode
,
457 reln
->smgr_rnode
.relNode
,
460 if ((nbytes
= FileWrite(v
->mdfd_vfd
, buffer
, BLCKSZ
)) != BLCKSZ
)
464 (errcode_for_file_access(),
465 errmsg("could not extend relation %u/%u/%u/%u: %m",
466 reln
->smgr_rnode
.spcNode
,
467 reln
->smgr_rnode
.dbNode
,
468 reln
->smgr_rnode
.relNode
,
470 errhint("Check free disk space.")));
471 /* short write: complain appropriately */
473 (errcode(ERRCODE_DISK_FULL
),
474 errmsg("could not extend relation %u/%u/%u/%u: wrote only %d of %d bytes at block %u",
475 reln
->smgr_rnode
.spcNode
,
476 reln
->smgr_rnode
.dbNode
,
477 reln
->smgr_rnode
.relNode
,
479 nbytes
, BLCKSZ
, blocknum
),
480 errhint("Check free disk space.")));
484 register_dirty_segment(reln
, forknum
, v
);
486 Assert(_mdnblocks(reln
, forknum
, v
) <= ((BlockNumber
) RELSEG_SIZE
));
490 * mdopen() -- Open the specified relation.
492 * Note we only open the first segment, when there are multiple segments.
494 * If first segment is not present, either ereport or return NULL according
495 * to "behavior". We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
496 * EXTENSION_CREATE means it's OK to extend an existing relation, not to
497 * invent one out of whole cloth.
500 mdopen(SMgrRelation reln
, ForkNumber forknum
, ExtensionBehavior behavior
)
506 /* No work if already open */
507 if (reln
->md_fd
[forknum
])
508 return reln
->md_fd
[forknum
];
510 path
= relpath(reln
->smgr_rnode
, forknum
);
512 fd
= PathNameOpenFile(path
, O_RDWR
| PG_BINARY
, 0600);
517 * During bootstrap, there are cases where a system relation will be
518 * accessed (by internal backend processes) before the bootstrap
519 * script nominally creates it. Therefore, accept mdopen() as a
520 * substitute for mdcreate() in bootstrap mode only. (See mdcreate)
522 if (IsBootstrapProcessingMode())
523 fd
= PathNameOpenFile(path
, O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
, 0600);
527 if (behavior
== EXTENSION_RETURN_NULL
&&
528 FILE_POSSIBLY_DELETED(errno
))
531 (errcode_for_file_access(),
532 errmsg("could not open relation %u/%u/%u/%u: %m",
533 reln
->smgr_rnode
.spcNode
,
534 reln
->smgr_rnode
.dbNode
,
535 reln
->smgr_rnode
.relNode
,
542 reln
->md_fd
[forknum
] = mdfd
= _fdvec_alloc();
545 mdfd
->mdfd_segno
= 0;
546 mdfd
->mdfd_chain
= NULL
;
547 Assert(_mdnblocks(reln
, forknum
, mdfd
) <= ((BlockNumber
) RELSEG_SIZE
));
553 * mdclose() -- Close the specified relation, if it isn't closed already.
556 mdclose(SMgrRelation reln
, ForkNumber forknum
)
558 MdfdVec
*v
= reln
->md_fd
[forknum
];
560 /* No work if already closed */
564 reln
->md_fd
[forknum
] = NULL
; /* prevent dangling pointer after error */
570 /* if not closed already */
571 if (v
->mdfd_vfd
>= 0)
572 FileClose(v
->mdfd_vfd
);
573 /* Now free vector */
580 * mdread() -- Read the specified block from a relation.
583 mdread(SMgrRelation reln
, ForkNumber forknum
, BlockNumber blocknum
,
590 v
= _mdfd_getseg(reln
, forknum
, blocknum
, false, EXTENSION_FAIL
);
592 seekpos
= (off_t
) BLCKSZ
* (blocknum
% ((BlockNumber
) RELSEG_SIZE
));
593 Assert(seekpos
< (off_t
) BLCKSZ
* RELSEG_SIZE
);
595 if (FileSeek(v
->mdfd_vfd
, seekpos
, SEEK_SET
) != seekpos
)
597 (errcode_for_file_access(),
598 errmsg("could not seek to block %u of relation %u/%u/%u/%u: %m",
600 reln
->smgr_rnode
.spcNode
,
601 reln
->smgr_rnode
.dbNode
,
602 reln
->smgr_rnode
.relNode
,
605 if ((nbytes
= FileRead(v
->mdfd_vfd
, buffer
, BLCKSZ
)) != BLCKSZ
)
609 (errcode_for_file_access(),
610 errmsg("could not read block %u of relation %u/%u/%u/%u: %m",
612 reln
->smgr_rnode
.spcNode
,
613 reln
->smgr_rnode
.dbNode
,
614 reln
->smgr_rnode
.relNode
,
618 * Short read: we are at or past EOF, or we read a partial block at
619 * EOF. Normally this is an error; upper levels should never try to
620 * read a nonexistent block. However, if zero_damaged_pages is ON or
621 * we are InRecovery, we should instead return zeroes without
622 * complaining. This allows, for example, the case of trying to
623 * update a block that was later truncated away.
625 if (zero_damaged_pages
|| InRecovery
)
626 MemSet(buffer
, 0, BLCKSZ
);
629 (errcode(ERRCODE_DATA_CORRUPTED
),
630 errmsg("could not read block %u of relation %u/%u/%u/%u: read only %d of %d bytes",
632 reln
->smgr_rnode
.spcNode
,
633 reln
->smgr_rnode
.dbNode
,
634 reln
->smgr_rnode
.relNode
,
641 * mdwrite() -- Write the supplied block at the appropriate location.
643 * This is to be used only for updating already-existing blocks of a
644 * relation (ie, those before the current EOF). To extend a relation,
648 mdwrite(SMgrRelation reln
, ForkNumber forknum
, BlockNumber blocknum
,
649 char *buffer
, bool isTemp
)
655 /* This assert is too expensive to have on normally ... */
656 #ifdef CHECK_WRITE_VS_EXTEND
657 Assert(blocknum
< mdnblocks(reln
, forknum
));
660 v
= _mdfd_getseg(reln
, forknum
, blocknum
, isTemp
, EXTENSION_FAIL
);
662 seekpos
= (off_t
) BLCKSZ
* (blocknum
% ((BlockNumber
) RELSEG_SIZE
));
663 Assert(seekpos
< (off_t
) BLCKSZ
* RELSEG_SIZE
);
665 if (FileSeek(v
->mdfd_vfd
, seekpos
, SEEK_SET
) != seekpos
)
667 (errcode_for_file_access(),
668 errmsg("could not seek to block %u of relation %u/%u/%u/%u: %m",
670 reln
->smgr_rnode
.spcNode
,
671 reln
->smgr_rnode
.dbNode
,
672 reln
->smgr_rnode
.relNode
,
675 if ((nbytes
= FileWrite(v
->mdfd_vfd
, buffer
, BLCKSZ
)) != BLCKSZ
)
679 (errcode_for_file_access(),
680 errmsg("could not write block %u of relation %u/%u/%u/%u: %m",
682 reln
->smgr_rnode
.spcNode
,
683 reln
->smgr_rnode
.dbNode
,
684 reln
->smgr_rnode
.relNode
,
686 /* short write: complain appropriately */
688 (errcode(ERRCODE_DISK_FULL
),
689 errmsg("could not write block %u of relation %u/%u/%u/%u: wrote only %d of %d bytes",
691 reln
->smgr_rnode
.spcNode
,
692 reln
->smgr_rnode
.dbNode
,
693 reln
->smgr_rnode
.relNode
,
696 errhint("Check free disk space.")));
700 register_dirty_segment(reln
, forknum
, v
);
704 * mdnblocks() -- Get the number of blocks stored in a relation.
706 * Important side effect: all active segments of the relation are opened
707 * and added to the mdfd_chain list. If this routine has not been
708 * called, then only segments up to the last one actually touched
709 * are present in the chain.
712 mdnblocks(SMgrRelation reln
, ForkNumber forknum
)
714 MdfdVec
*v
= mdopen(reln
, forknum
, EXTENSION_FAIL
);
716 BlockNumber segno
= 0;
719 * Skip through any segments that aren't the last one, to avoid redundant
720 * seeks on them. We have previously verified that these segments are
721 * exactly RELSEG_SIZE long, and it's useless to recheck that each time.
723 * NOTE: this assumption could only be wrong if another backend has
724 * truncated the relation. We rely on higher code levels to handle that
725 * scenario by closing and re-opening the md fd, which is handled via
726 * relcache flush. (Since the bgwriter doesn't participate in relcache
727 * flush, it could have segment chain entries for inactive segments;
728 * that's OK because the bgwriter never needs to compute relation size.)
730 while (v
->mdfd_chain
!= NULL
)
738 nblocks
= _mdnblocks(reln
, forknum
, v
);
739 if (nblocks
> ((BlockNumber
) RELSEG_SIZE
))
740 elog(FATAL
, "segment too big");
741 if (nblocks
< ((BlockNumber
) RELSEG_SIZE
))
742 return (segno
* ((BlockNumber
) RELSEG_SIZE
)) + nblocks
;
745 * If segment is exactly RELSEG_SIZE, advance to next one.
749 if (v
->mdfd_chain
== NULL
)
752 * Because we pass O_CREAT, we will create the next segment (with
753 * zero length) immediately, if the last segment is of length
754 * RELSEG_SIZE. While perhaps not strictly necessary, this keeps
757 v
->mdfd_chain
= _mdfd_openseg(reln
, forknum
, segno
, O_CREAT
);
758 if (v
->mdfd_chain
== NULL
)
760 (errcode_for_file_access(),
761 errmsg("could not open segment %u of relation %u/%u/%u/%u: %m",
763 reln
->smgr_rnode
.spcNode
,
764 reln
->smgr_rnode
.dbNode
,
765 reln
->smgr_rnode
.relNode
,
774 * mdtruncate() -- Truncate relation to specified number of blocks.
777 mdtruncate(SMgrRelation reln
, ForkNumber forknum
, BlockNumber nblocks
,
782 BlockNumber priorblocks
;
785 * NOTE: mdnblocks makes sure we have opened all active segments, so that
786 * truncation loop will get them all!
788 curnblk
= mdnblocks(reln
, forknum
);
789 if (nblocks
> curnblk
)
791 /* Bogus request ... but no complaint if InRecovery */
795 (errmsg("could not truncate relation %u/%u/%u/%u to %u blocks: it's only %u blocks now",
796 reln
->smgr_rnode
.spcNode
,
797 reln
->smgr_rnode
.dbNode
,
798 reln
->smgr_rnode
.relNode
,
802 if (nblocks
== curnblk
)
803 return; /* no work */
805 v
= mdopen(reln
, forknum
, EXTENSION_FAIL
);
812 if (priorblocks
> nblocks
)
815 * This segment is no longer active (and has already been unlinked
816 * from the mdfd_chain). We truncate the file, but do not delete
817 * it, for reasons explained in the header comments.
819 if (FileTruncate(v
->mdfd_vfd
, 0) < 0)
821 (errcode_for_file_access(),
822 errmsg("could not truncate relation %u/%u/%u/%u to %u blocks: %m",
823 reln
->smgr_rnode
.spcNode
,
824 reln
->smgr_rnode
.dbNode
,
825 reln
->smgr_rnode
.relNode
,
829 register_dirty_segment(reln
, forknum
, v
);
831 Assert(ov
!= reln
->md_fd
[forknum
]); /* we never drop the 1st segment */
834 else if (priorblocks
+ ((BlockNumber
) RELSEG_SIZE
) > nblocks
)
837 * This is the last segment we want to keep. Truncate the file to
838 * the right length, and clear chain link that points to any
839 * remaining segments (which we shall zap). NOTE: if nblocks is
840 * exactly a multiple K of RELSEG_SIZE, we will truncate the K+1st
841 * segment to 0 length but keep it. This adheres to the invariant
842 * given in the header comments.
844 BlockNumber lastsegblocks
= nblocks
- priorblocks
;
846 if (FileTruncate(v
->mdfd_vfd
, (off_t
) lastsegblocks
* BLCKSZ
) < 0)
848 (errcode_for_file_access(),
849 errmsg("could not truncate relation %u/%u/%u/%u to %u blocks: %m",
850 reln
->smgr_rnode
.spcNode
,
851 reln
->smgr_rnode
.dbNode
,
852 reln
->smgr_rnode
.relNode
,
856 register_dirty_segment(reln
, forknum
, v
);
858 ov
->mdfd_chain
= NULL
;
863 * We still need this segment and 0 or more blocks beyond it, so
864 * nothing to do here.
868 priorblocks
+= RELSEG_SIZE
;
873 * mdimmedsync() -- Immediately sync a relation to stable storage.
875 * Note that only writes already issued are synced; this routine knows
876 * nothing of dirty buffers that may exist inside the buffer manager.
879 mdimmedsync(SMgrRelation reln
, ForkNumber forknum
)
885 * NOTE: mdnblocks makes sure we have opened all active segments, so that
886 * fsync loop will get them all!
888 curnblk
= mdnblocks(reln
, forknum
);
890 v
= mdopen(reln
, forknum
, EXTENSION_FAIL
);
894 if (FileSync(v
->mdfd_vfd
) < 0)
896 (errcode_for_file_access(),
897 errmsg("could not fsync segment %u of relation %u/%u/%u/%u: %m",
899 reln
->smgr_rnode
.spcNode
,
900 reln
->smgr_rnode
.dbNode
,
901 reln
->smgr_rnode
.relNode
,
908 * mdsync() -- Sync previous writes to stable storage.
913 static bool mdsync_in_progress
= false;
915 HASH_SEQ_STATUS hstat
;
916 PendingOperationEntry
*entry
;
920 * This is only called during checkpoints, and checkpoints should only
921 * occur in processes that have created a pendingOpsTable.
923 if (!pendingOpsTable
)
924 elog(ERROR
, "cannot sync without a pendingOpsTable");
927 * If we are in the bgwriter, the sync had better include all fsync
928 * requests that were queued by backends up to this point. The tightest
929 * race condition that could occur is that a buffer that must be written
930 * and fsync'd for the checkpoint could have been dumped by a backend just
931 * before it was visited by BufferSync(). We know the backend will have
932 * queued an fsync request before clearing the buffer's dirtybit, so we
933 * are safe as long as we do an Absorb after completing BufferSync().
935 AbsorbFsyncRequests();
938 * To avoid excess fsync'ing (in the worst case, maybe a never-terminating
939 * checkpoint), we want to ignore fsync requests that are entered into the
940 * hashtable after this point --- they should be processed next time,
941 * instead. We use mdsync_cycle_ctr to tell old entries apart from new
942 * ones: new ones will have cycle_ctr equal to the incremented value of
945 * In normal circumstances, all entries present in the table at this point
946 * will have cycle_ctr exactly equal to the current (about to be old)
947 * value of mdsync_cycle_ctr. However, if we fail partway through the
948 * fsync'ing loop, then older values of cycle_ctr might remain when we
949 * come back here to try again. Repeated checkpoint failures would
950 * eventually wrap the counter around to the point where an old entry
951 * might appear new, causing us to skip it, possibly allowing a checkpoint
952 * to succeed that should not have. To forestall wraparound, any time the
953 * previous mdsync() failed to complete, run through the table and
954 * forcibly set cycle_ctr = mdsync_cycle_ctr.
956 * Think not to merge this loop with the main loop, as the problem is
957 * exactly that that loop may fail before having visited all the entries.
958 * From a performance point of view it doesn't matter anyway, as this path
959 * will never be taken in a system that's functioning normally.
961 if (mdsync_in_progress
)
963 /* prior try failed, so update any stale cycle_ctr values */
964 hash_seq_init(&hstat
, pendingOpsTable
);
965 while ((entry
= (PendingOperationEntry
*) hash_seq_search(&hstat
)) != NULL
)
967 entry
->cycle_ctr
= mdsync_cycle_ctr
;
971 /* Advance counter so that new hashtable entries are distinguishable */
974 /* Set flag to detect failure if we don't reach the end of the loop */
975 mdsync_in_progress
= true;
977 /* Now scan the hashtable for fsync requests to process */
978 absorb_counter
= FSYNCS_PER_ABSORB
;
979 hash_seq_init(&hstat
, pendingOpsTable
);
980 while ((entry
= (PendingOperationEntry
*) hash_seq_search(&hstat
)) != NULL
)
983 * If the entry is new then don't process it this time. Note that
984 * "continue" bypasses the hash-remove call at the bottom of the loop.
986 if (entry
->cycle_ctr
== mdsync_cycle_ctr
)
989 /* Else assert we haven't missed it */
990 Assert((CycleCtr
) (entry
->cycle_ctr
+ 1) == mdsync_cycle_ctr
);
993 * If fsync is off then we don't have to bother opening the file at
994 * all. (We delay checking until this point so that changing fsync on
995 * the fly behaves sensibly.) Also, if the entry is marked canceled,
996 * fall through to delete it.
998 if (enableFsync
&& !entry
->canceled
)
1003 * If in bgwriter, we want to absorb pending requests every so
1004 * often to prevent overflow of the fsync request queue. It is
1005 * unspecified whether newly-added entries will be visited by
1006 * hash_seq_search, but we don't care since we don't need to
1007 * process them anyway.
1009 if (--absorb_counter
<= 0)
1011 AbsorbFsyncRequests();
1012 absorb_counter
= FSYNCS_PER_ABSORB
;
1016 * The fsync table could contain requests to fsync segments that
1017 * have been deleted (unlinked) by the time we get to them. Rather
1018 * than just hoping an ENOENT (or EACCES on Windows) error can be
1019 * ignored, what we do on error is absorb pending requests and
1020 * then retry. Since mdunlink() queues a "revoke" message before
1021 * actually unlinking, the fsync request is guaranteed to be
1022 * marked canceled after the absorb if it really was this case.
1023 * DROP DATABASE likewise has to tell us to forget fsync requests
1024 * before it starts deletions.
1026 for (failures
= 0;; failures
++) /* loop exits at "break" */
1032 * Find or create an smgr hash entry for this relation. This
1033 * may seem a bit unclean -- md calling smgr? But it's really
1034 * the best solution. It ensures that the open file reference
1035 * isn't permanently leaked if we get an error here. (You may
1036 * say "but an unreferenced SMgrRelation is still a leak!" Not
1037 * really, because the only case in which a checkpoint is done
1038 * by a process that isn't about to shut down is in the
1039 * bgwriter, and it will periodically do smgrcloseall(). This
1040 * fact justifies our not closing the reln in the success path
1041 * either, which is a good thing since in non-bgwriter cases
1042 * we couldn't safely do that.) Furthermore, in many cases
1043 * the relation will have been dirtied through this same smgr
1044 * relation, and so we can save a file open/close cycle.
1046 reln
= smgropen(entry
->tag
.rnode
);
1049 * It is possible that the relation has been dropped or
1050 * truncated since the fsync request was entered. Therefore,
1051 * allow ENOENT, but only if we didn't fail already on this
1052 * file. This applies both during _mdfd_getseg() and during
1053 * FileSync, since fd.c might have closed the file behind our
1056 seg
= _mdfd_getseg(reln
, entry
->tag
.forknum
,
1057 entry
->tag
.segno
* ((BlockNumber
) RELSEG_SIZE
),
1058 false, EXTENSION_RETURN_NULL
);
1060 FileSync(seg
->mdfd_vfd
) >= 0)
1061 break; /* success; break out of retry loop */
1064 * XXX is there any point in allowing more than one retry?
1065 * Don't see one at the moment, but easy to change the test
1068 if (!FILE_POSSIBLY_DELETED(errno
) ||
1071 (errcode_for_file_access(),
1072 errmsg("could not fsync segment %u of relation %u/%u/%u/%u: %m",
1074 entry
->tag
.rnode
.spcNode
,
1075 entry
->tag
.rnode
.dbNode
,
1076 entry
->tag
.rnode
.relNode
,
1077 entry
->tag
.forknum
)));
1080 (errcode_for_file_access(),
1081 errmsg("could not fsync segment %u of relation %u/%u/%u/%u but retrying: %m",
1083 entry
->tag
.rnode
.spcNode
,
1084 entry
->tag
.rnode
.dbNode
,
1085 entry
->tag
.rnode
.relNode
,
1086 entry
->tag
.forknum
)));
1089 * Absorb incoming requests and check to see if canceled.
1091 AbsorbFsyncRequests();
1092 absorb_counter
= FSYNCS_PER_ABSORB
; /* might as well... */
1094 if (entry
->canceled
)
1096 } /* end retry loop */
1100 * If we get here, either we fsync'd successfully, or we don't have to
1101 * because enableFsync is off, or the entry is (now) marked canceled.
1102 * Okay to delete it.
1104 if (hash_search(pendingOpsTable
, &entry
->tag
,
1105 HASH_REMOVE
, NULL
) == NULL
)
1106 elog(ERROR
, "pendingOpsTable corrupted");
1107 } /* end loop over hashtable entries */
1109 /* Flag successful completion of mdsync */
1110 mdsync_in_progress
= false;
1114 * mdpreckpt() -- Do pre-checkpoint work
1116 * To distinguish unlink requests that arrived before this checkpoint
1117 * started from those that arrived during the checkpoint, we use a cycle
1118 * counter similar to the one we use for fsync requests. That cycle
1119 * counter is incremented here.
1121 * This must be called *before* the checkpoint REDO point is determined.
1122 * That ensures that we won't delete files too soon.
1124 * Note that we can't do anything here that depends on the assumption
1125 * that the checkpoint will be completed.
1133 * In case the prior checkpoint wasn't completed, stamp all entries in the
1134 * list with the current cycle counter. Anything that's in the list at
1135 * the start of checkpoint can surely be deleted after the checkpoint is
1136 * finished, regardless of when the request was made.
1138 foreach(cell
, pendingUnlinks
)
1140 PendingUnlinkEntry
*entry
= (PendingUnlinkEntry
*) lfirst(cell
);
1142 entry
->cycle_ctr
= mdckpt_cycle_ctr
;
1146 * Any unlink requests arriving after this point will be assigned the next
1147 * cycle counter, and won't be unlinked until next checkpoint.
1153 * mdpostckpt() -- Do post-checkpoint work
1155 * Remove any lingering files that can now be safely removed.
1160 while (pendingUnlinks
!= NIL
)
1162 PendingUnlinkEntry
*entry
= (PendingUnlinkEntry
*) linitial(pendingUnlinks
);
1166 * New entries are appended to the end, so if the entry is new we've
1167 * reached the end of old entries.
1169 if (entry
->cycle_ctr
== mdckpt_cycle_ctr
)
1172 /* Else assert we haven't missed it */
1173 Assert((CycleCtr
) (entry
->cycle_ctr
+ 1) == mdckpt_cycle_ctr
);
1175 /* Unlink the file */
1176 path
= relpath(entry
->rnode
, MAIN_FORKNUM
);
1177 if (unlink(path
) < 0)
1180 * There's a race condition, when the database is dropped at the
1181 * same time that we process the pending unlink requests. If the
1182 * DROP DATABASE deletes the file before we do, we will get ENOENT
1183 * here. rmtree() also has to ignore ENOENT errors, to deal with
1184 * the possibility that we delete the file first.
1186 if (errno
!= ENOENT
)
1188 (errcode_for_file_access(),
1189 errmsg("could not remove relation %u/%u/%u/%u: %m",
1190 entry
->rnode
.spcNode
,
1191 entry
->rnode
.dbNode
,
1192 entry
->rnode
.relNode
,
1197 pendingUnlinks
= list_delete_first(pendingUnlinks
);
1203 * register_dirty_segment() -- Mark a relation segment as needing fsync
1205 * If there is a local pending-ops table, just make an entry in it for
1206 * mdsync to process later. Otherwise, try to pass off the fsync request
1207 * to the background writer process. If that fails, just do the fsync
1208 * locally before returning (we expect this will not happen often enough
1209 * to be a performance problem).
1212 register_dirty_segment(SMgrRelation reln
, ForkNumber forknum
, MdfdVec
*seg
)
1214 if (pendingOpsTable
)
1216 /* push it into local pending-ops table */
1217 RememberFsyncRequest(reln
->smgr_rnode
, forknum
, seg
->mdfd_segno
);
1221 if (ForwardFsyncRequest(reln
->smgr_rnode
, forknum
, seg
->mdfd_segno
))
1222 return; /* passed it off successfully */
1224 if (FileSync(seg
->mdfd_vfd
) < 0)
1226 (errcode_for_file_access(),
1227 errmsg("could not fsync segment %u of relation %u/%u/%u/%u: %m",
1229 reln
->smgr_rnode
.spcNode
,
1230 reln
->smgr_rnode
.dbNode
,
1231 reln
->smgr_rnode
.relNode
,
1237 * register_unlink() -- Schedule a file to be deleted after next checkpoint
1239 * As with register_dirty_segment, this could involve either a local or
1240 * a remote pending-ops table.
1243 register_unlink(RelFileNode rnode
)
1245 if (pendingOpsTable
)
1247 /* push it into local pending-ops table */
1248 RememberFsyncRequest(rnode
, MAIN_FORKNUM
, UNLINK_RELATION_REQUEST
);
1253 * Notify the bgwriter about it. If we fail to queue the request
1254 * message, we have to sleep and try again, because we can't simply
1255 * delete the file now. Ugly, but hopefully won't happen often.
1257 * XXX should we just leave the file orphaned instead?
1259 Assert(IsUnderPostmaster
);
1260 while (!ForwardFsyncRequest(rnode
, MAIN_FORKNUM
,
1261 UNLINK_RELATION_REQUEST
))
1262 pg_usleep(10000L); /* 10 msec seems a good number */
1267 * RememberFsyncRequest() -- callback from bgwriter side of fsync request
1269 * We stuff most fsync requests into the local hash table for execution
1270 * during the bgwriter's next checkpoint. UNLINK requests go into a
1271 * separate linked list, however, because they get processed separately.
1273 * The range of possible segment numbers is way less than the range of
1274 * BlockNumber, so we can reserve high values of segno for special purposes.
1276 * - FORGET_RELATION_FSYNC means to cancel pending fsyncs for a relation
1277 * - FORGET_DATABASE_FSYNC means to cancel pending fsyncs for a whole database
1278 * - UNLINK_RELATION_REQUEST is a request to delete the file after the next
1281 * (Handling the FORGET_* requests is a tad slow because the hash table has
1282 * to be searched linearly, but it doesn't seem worth rethinking the table
1283 * structure for them.)
1286 RememberFsyncRequest(RelFileNode rnode
, ForkNumber forknum
, BlockNumber segno
)
1288 Assert(pendingOpsTable
);
1290 if (segno
== FORGET_RELATION_FSYNC
)
1292 /* Remove any pending requests for the entire relation */
1293 HASH_SEQ_STATUS hstat
;
1294 PendingOperationEntry
*entry
;
1296 hash_seq_init(&hstat
, pendingOpsTable
);
1297 while ((entry
= (PendingOperationEntry
*) hash_seq_search(&hstat
)) != NULL
)
1299 if (RelFileNodeEquals(entry
->tag
.rnode
, rnode
) &&
1300 entry
->tag
.forknum
== forknum
)
1302 /* Okay, cancel this entry */
1303 entry
->canceled
= true;
1307 else if (segno
== FORGET_DATABASE_FSYNC
)
1309 /* Remove any pending requests for the entire database */
1310 HASH_SEQ_STATUS hstat
;
1311 PendingOperationEntry
*entry
;
1316 /* Remove fsync requests */
1317 hash_seq_init(&hstat
, pendingOpsTable
);
1318 while ((entry
= (PendingOperationEntry
*) hash_seq_search(&hstat
)) != NULL
)
1320 if (entry
->tag
.rnode
.dbNode
== rnode
.dbNode
)
1322 /* Okay, cancel this entry */
1323 entry
->canceled
= true;
1327 /* Remove unlink requests */
1329 for (cell
= list_head(pendingUnlinks
); cell
; cell
= next
)
1331 PendingUnlinkEntry
*entry
= (PendingUnlinkEntry
*) lfirst(cell
);
1334 if (entry
->rnode
.dbNode
== rnode
.dbNode
)
1336 pendingUnlinks
= list_delete_cell(pendingUnlinks
, cell
, prev
);
1343 else if (segno
== UNLINK_RELATION_REQUEST
)
1345 /* Unlink request: put it in the linked list */
1346 MemoryContext oldcxt
= MemoryContextSwitchTo(MdCxt
);
1347 PendingUnlinkEntry
*entry
;
1349 entry
= palloc(sizeof(PendingUnlinkEntry
));
1350 entry
->rnode
= rnode
;
1351 entry
->cycle_ctr
= mdckpt_cycle_ctr
;
1353 pendingUnlinks
= lappend(pendingUnlinks
, entry
);
1355 MemoryContextSwitchTo(oldcxt
);
1359 /* Normal case: enter a request to fsync this segment */
1360 PendingOperationTag key
;
1361 PendingOperationEntry
*entry
;
1364 /* ensure any pad bytes in the hash key are zeroed */
1365 MemSet(&key
, 0, sizeof(key
));
1367 key
.forknum
= forknum
;
1370 entry
= (PendingOperationEntry
*) hash_search(pendingOpsTable
,
1374 /* if new or previously canceled entry, initialize it */
1375 if (!found
|| entry
->canceled
)
1377 entry
->canceled
= false;
1378 entry
->cycle_ctr
= mdsync_cycle_ctr
;
1382 * NB: it's intentional that we don't change cycle_ctr if the entry
1383 * already exists. The fsync request must be treated as old, even
1384 * though the new request will be satisfied too by any subsequent
1387 * However, if the entry is present but is marked canceled, we should
1388 * act just as though it wasn't there. The only case where this could
1389 * happen would be if a file had been deleted, we received but did not
1390 * yet act on the cancel request, and the same relfilenode was then
1391 * assigned to a new file. We mustn't lose the new request, but it
1392 * should be considered new not old.
1398 * ForgetRelationFsyncRequests -- forget any fsyncs for a rel
1401 ForgetRelationFsyncRequests(RelFileNode rnode
, ForkNumber forknum
)
1403 if (pendingOpsTable
)
1405 /* standalone backend or startup process: fsync state is local */
1406 RememberFsyncRequest(rnode
, forknum
, FORGET_RELATION_FSYNC
);
1408 else if (IsUnderPostmaster
)
1411 * Notify the bgwriter about it. If we fail to queue the revoke
1412 * message, we have to sleep and try again ... ugly, but hopefully
1413 * won't happen often.
1415 * XXX should we CHECK_FOR_INTERRUPTS in this loop? Escaping with an
1416 * error would leave the no-longer-used file still present on disk,
1417 * which would be bad, so I'm inclined to assume that the bgwriter
1418 * will always empty the queue soon.
1420 while (!ForwardFsyncRequest(rnode
, forknum
, FORGET_RELATION_FSYNC
))
1421 pg_usleep(10000L); /* 10 msec seems a good number */
1424 * Note we don't wait for the bgwriter to actually absorb the revoke
1425 * message; see mdsync() for the implications.
1431 * ForgetDatabaseFsyncRequests -- forget any fsyncs and unlinks for a DB
1434 ForgetDatabaseFsyncRequests(Oid dbid
)
1438 rnode
.dbNode
= dbid
;
1442 if (pendingOpsTable
)
1444 /* standalone backend or startup process: fsync state is local */
1445 RememberFsyncRequest(rnode
, InvalidForkNumber
, FORGET_DATABASE_FSYNC
);
1447 else if (IsUnderPostmaster
)
1449 /* see notes in ForgetRelationFsyncRequests */
1450 while (!ForwardFsyncRequest(rnode
, InvalidForkNumber
,
1451 FORGET_DATABASE_FSYNC
))
1452 pg_usleep(10000L); /* 10 msec seems a good number */
1458 * _fdvec_alloc() -- Make a MdfdVec object.
1463 return (MdfdVec
*) MemoryContextAlloc(MdCxt
, sizeof(MdfdVec
));
1467 * Open the specified segment of the relation,
1468 * and make a MdfdVec object for it. Returns NULL on failure.
1471 _mdfd_openseg(SMgrRelation reln
, ForkNumber forknum
, BlockNumber segno
,
1479 path
= relpath(reln
->smgr_rnode
, forknum
);
1483 /* be sure we have enough space for the '.segno' */
1484 fullpath
= (char *) palloc(strlen(path
) + 12);
1485 sprintf(fullpath
, "%s.%u", path
, segno
);
1492 fd
= PathNameOpenFile(fullpath
, O_RDWR
| PG_BINARY
| oflags
, 0600);
1499 /* allocate an mdfdvec entry for it */
1502 /* fill the entry */
1504 v
->mdfd_segno
= segno
;
1505 v
->mdfd_chain
= NULL
;
1506 Assert(_mdnblocks(reln
, forknum
, v
) <= ((BlockNumber
) RELSEG_SIZE
));
1513 * _mdfd_getseg() -- Find the segment of the relation holding the
1516 * If the segment doesn't exist, we ereport, return NULL, or create the
1517 * segment, according to "behavior". Note: isTemp need only be correct
1518 * in the EXTENSION_CREATE case.
1521 _mdfd_getseg(SMgrRelation reln
, ForkNumber forknum
, BlockNumber blkno
,
1522 bool isTemp
, ExtensionBehavior behavior
)
1524 MdfdVec
*v
= mdopen(reln
, forknum
, behavior
);
1525 BlockNumber targetseg
;
1526 BlockNumber nextsegno
;
1529 return NULL
; /* only possible if EXTENSION_RETURN_NULL */
1531 targetseg
= blkno
/ ((BlockNumber
) RELSEG_SIZE
);
1532 for (nextsegno
= 1; nextsegno
<= targetseg
; nextsegno
++)
1534 Assert(nextsegno
== v
->mdfd_segno
+ 1);
1536 if (v
->mdfd_chain
== NULL
)
1539 * Normally we will create new segments only if authorized by the
1540 * caller (i.e., we are doing mdextend()). But when doing WAL
1541 * recovery, create segments anyway; this allows cases such as
1542 * replaying WAL data that has a write into a high-numbered
1543 * segment of a relation that was later deleted. We want to go
1544 * ahead and create the segments so we can finish out the replay.
1546 * We have to maintain the invariant that segments before the last
1547 * active segment are of size RELSEG_SIZE; therefore, pad them out
1548 * with zeroes if needed. (This only matters if caller is
1549 * extending the relation discontiguously, but that can happen in
1552 if (behavior
== EXTENSION_CREATE
|| InRecovery
)
1554 if (_mdnblocks(reln
, forknum
, v
) < RELSEG_SIZE
)
1556 char *zerobuf
= palloc0(BLCKSZ
);
1558 mdextend(reln
, forknum
,
1559 nextsegno
* ((BlockNumber
) RELSEG_SIZE
) - 1,
1563 v
->mdfd_chain
= _mdfd_openseg(reln
, forknum
, +nextsegno
, O_CREAT
);
1567 /* We won't create segment if not existent */
1568 v
->mdfd_chain
= _mdfd_openseg(reln
, forknum
, nextsegno
, 0);
1570 if (v
->mdfd_chain
== NULL
)
1572 if (behavior
== EXTENSION_RETURN_NULL
&&
1573 FILE_POSSIBLY_DELETED(errno
))
1576 (errcode_for_file_access(),
1577 errmsg("could not open segment %u of relation %u/%u/%u/%u (target block %u): %m",
1579 reln
->smgr_rnode
.spcNode
,
1580 reln
->smgr_rnode
.dbNode
,
1581 reln
->smgr_rnode
.relNode
,
1592 * Get number of blocks present in a single disk file
1595 _mdnblocks(SMgrRelation reln
, ForkNumber forknum
, MdfdVec
*seg
)
1599 len
= FileSeek(seg
->mdfd_vfd
, 0L, SEEK_END
);
1602 (errcode_for_file_access(),
1603 errmsg("could not seek to end of segment %u of relation %u/%u/%u/%u: %m",
1605 reln
->smgr_rnode
.spcNode
,
1606 reln
->smgr_rnode
.dbNode
,
1607 reln
->smgr_rnode
.relNode
,
1609 /* note that this calculation will ignore any partial block at EOF */
1610 return (BlockNumber
) (len
/ BLCKSZ
);