Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9df8ced

Browse files
author
Hiroshi Inoue
committed
Improve the treatment of partial(incomplete) blocks of relation files.
This may solve a TODO item * Recover or force failure when disk space is exhausted
1 parent 66fbea5 commit 9df8ced

File tree

1 file changed

+22
-5
lines changed
  • src/backend/storage/smgr

1 file changed

+22
-5
lines changed

src/backend/storage/smgr/md.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.55 1999/09/28 11:41:07 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.56 1999/10/06 06:38:04 inoue Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -233,7 +233,7 @@ mdunlink(Relation reln)
233233
int
234234
mdextend(Relation reln, char *buffer)
235235
{
236-
long pos;
236+
long pos, nbytes;
237237
int nblocks;
238238
MdfdVec *v;
239239

@@ -243,8 +243,22 @@ mdextend(Relation reln, char *buffer)
243243
if ((pos = FileSeek(v->mdfd_vfd, 0L, SEEK_END)) < 0)
244244
return SM_FAIL;
245245

246-
if (FileWrite(v->mdfd_vfd, buffer, BLCKSZ) != BLCKSZ)
246+
if (pos % BLCKSZ != 0) /* the last block is incomplete */
247+
{
248+
pos -= pos % BLCKSZ;
249+
if (FileSeek(v->mdfd_vfd, pos, SEEK_SET) < 0)
250+
return SM_FAIL;
251+
}
252+
253+
if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ)) != BLCKSZ)
254+
{
255+
if (nbytes > 0)
256+
{
257+
FileTruncate(v->mdfd_vfd, pos);
258+
FileSeek(v->mdfd_vfd, pos, SEEK_SET);
259+
}
247260
return SM_FAIL;
261+
}
248262

249263
/* remember that we did a write, so we can sync at xact commit */
250264
v->mdfd_flags |= MDFD_DIRTY;
@@ -432,6 +446,8 @@ mdread(Relation reln, BlockNumber blocknum, char *buffer)
432446
{
433447
if (nbytes == 0)
434448
MemSet(buffer, 0, BLCKSZ);
449+
else if (blocknum == 0 && nbytes > 0 && mdnblocks(reln) == 0)
450+
MemSet(buffer, 0, BLCKSZ);
435451
else
436452
status = SM_FAIL;
437453
}
@@ -1067,6 +1083,7 @@ _mdnblocks(File file, Size blcksz)
10671083
{
10681084
long len;
10691085

1070-
len = FileSeek(file, 0L, SEEK_END) - 1;
1071-
return (BlockNumber) ((len < 0) ? 0 : 1 + len / blcksz);
1086+
len = FileSeek(file, 0L, SEEK_END);
1087+
if (len < 0) return 0; /* on failure, assume file is empty */
1088+
return (BlockNumber) (len / blcksz);
10721089
}

0 commit comments

Comments
 (0)