8
8
* Portions Copyright (c) 1994, Regents of the University of California
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.67 2008/11/19 10:34:52 heikki Exp $
11
+ * $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.68 2008/11/26 17:08:57 heikki Exp $
12
12
*
13
13
*
14
14
* NOTES:
@@ -101,7 +101,7 @@ static BlockNumber fsm_get_heap_blk(FSMAddress addr, uint16 slot);
101
101
static BlockNumber fsm_logical_to_physical (FSMAddress addr );
102
102
103
103
static Buffer fsm_readbuf (Relation rel , FSMAddress addr , bool extend );
104
- static void fsm_extend (Relation rel , BlockNumber nfsmblocks , bool createstorage );
104
+ static void fsm_extend (Relation rel , BlockNumber fsm_nblocks );
105
105
106
106
/* functions to convert amount of free space to a FSM category */
107
107
static uint8 fsm_space_avail_to_cat (Size avail );
@@ -303,13 +303,13 @@ FreeSpaceMapTruncateRel(Relation rel, BlockNumber nblocks)
303
303
smgrtruncate (rel -> rd_smgr , FSM_FORKNUM , new_nfsmblocks , rel -> rd_istemp );
304
304
305
305
/*
306
- * Need to invalidate the relcache entry, because rd_fsm_nblocks_cache
306
+ * Need to invalidate the relcache entry, because rd_fsm_nblocks
307
307
* seen by other backends is no longer valid.
308
308
*/
309
309
if (!InRecovery )
310
310
CacheInvalidateRelcache (rel );
311
311
312
- rel -> rd_fsm_nblocks_cache = new_nfsmblocks ;
312
+ rel -> rd_fsm_nblocks = new_nfsmblocks ;
313
313
}
314
314
315
315
/*
@@ -503,19 +503,20 @@ fsm_readbuf(Relation rel, FSMAddress addr, bool extend)
503
503
504
504
RelationOpenSmgr (rel );
505
505
506
- if ( rel -> rd_fsm_nblocks_cache == InvalidBlockNumber ||
507
- rel -> rd_fsm_nblocks_cache <= blkno )
506
+ /* If we haven't cached the size of the FSM yet, check it first */
507
+ if ( rel -> rd_fsm_nblocks == InvalidBlockNumber )
508
508
{
509
- if (! smgrexists (rel -> rd_smgr , FSM_FORKNUM ))
510
- fsm_extend ( rel , blkno + 1 , true );
509
+ if (smgrexists (rel -> rd_smgr , FSM_FORKNUM ))
510
+ rel -> rd_fsm_nblocks = smgrnblocks ( rel -> rd_smgr , FSM_FORKNUM );
511
511
else
512
- rel -> rd_fsm_nblocks_cache = smgrnblocks ( rel -> rd_smgr , FSM_FORKNUM ) ;
512
+ rel -> rd_fsm_nblocks = 0 ;
513
513
}
514
514
515
- if (blkno >= rel -> rd_fsm_nblocks_cache )
515
+ /* Handle requests beyond EOF */
516
+ if (blkno >= rel -> rd_fsm_nblocks )
516
517
{
517
518
if (extend )
518
- fsm_extend (rel , blkno + 1 , false );
519
+ fsm_extend (rel , blkno + 1 );
519
520
else
520
521
return InvalidBuffer ;
521
522
}
@@ -536,13 +537,12 @@ fsm_readbuf(Relation rel, FSMAddress addr, bool extend)
536
537
/*
537
538
* Ensure that the FSM fork is at least n_fsmblocks long, extending
538
539
* it if necessary with empty pages. And by empty, I mean pages filled
539
- * with zeros, meaning there's no free space. If createstorage is true,
540
- * the FSM file might need to be created first.
540
+ * with zeros, meaning there's no free space.
541
541
*/
542
542
static void
543
- fsm_extend (Relation rel , BlockNumber n_fsmblocks , bool createstorage )
543
+ fsm_extend (Relation rel , BlockNumber fsm_nblocks )
544
544
{
545
- BlockNumber n_fsmblocks_now ;
545
+ BlockNumber fsm_nblocks_now ;
546
546
Page pg ;
547
547
548
548
pg = (Page ) palloc (BLCKSZ );
@@ -561,27 +561,30 @@ fsm_extend(Relation rel, BlockNumber n_fsmblocks, bool createstorage)
561
561
LockRelationForExtension (rel , ExclusiveLock );
562
562
563
563
/* Create the FSM file first if it doesn't exist */
564
- if (createstorage && !smgrexists (rel -> rd_smgr , FSM_FORKNUM ))
564
+ if ((rel -> rd_fsm_nblocks == 0 || rel -> rd_fsm_nblocks == InvalidBlockNumber )
565
+ && !smgrexists (rel -> rd_smgr , FSM_FORKNUM ))
565
566
{
566
567
smgrcreate (rel -> rd_smgr , FSM_FORKNUM , false);
567
- n_fsmblocks_now = 0 ;
568
+ fsm_nblocks_now = 0 ;
568
569
}
569
570
else
570
- n_fsmblocks_now = smgrnblocks (rel -> rd_smgr , FSM_FORKNUM );
571
+ fsm_nblocks_now = smgrnblocks (rel -> rd_smgr , FSM_FORKNUM );
571
572
572
- while (n_fsmblocks_now < n_fsmblocks )
573
+ while (fsm_nblocks_now < fsm_nblocks )
573
574
{
574
- smgrextend (rel -> rd_smgr , FSM_FORKNUM , n_fsmblocks_now ,
575
+ smgrextend (rel -> rd_smgr , FSM_FORKNUM , fsm_nblocks_now ,
575
576
(char * ) pg , rel -> rd_istemp );
576
- n_fsmblocks_now ++ ;
577
+ fsm_nblocks_now ++ ;
577
578
}
578
579
579
580
UnlockRelationForExtension (rel , ExclusiveLock );
580
581
581
582
pfree (pg );
582
583
583
- /* update the cache with the up-to-date size */
584
- rel -> rd_fsm_nblocks_cache = n_fsmblocks_now ;
584
+ /* Update the relcache with the up-to-date size */
585
+ if (!InRecovery )
586
+ CacheInvalidateRelcache (rel );
587
+ rel -> rd_fsm_nblocks = fsm_nblocks_now ;
585
588
}
586
589
587
590
/*
0 commit comments