Skip to content

Commit 9858a8c

Browse files
committed
Rely on relcache invalidation to update the cached size of the FSM.
1 parent df559de commit 9858a8c

File tree

4 files changed

+39
-36
lines changed

4 files changed

+39
-36
lines changed

src/backend/access/transam/xlogutils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/backend/access/transam/xlogutils.c,v 1.63 2008/11/19 10:34:50 heikki Exp $
14+
* $PostgreSQL: pgsql/src/backend/access/transam/xlogutils.c,v 1.64 2008/11/26 17:08:57 heikki Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -376,7 +376,7 @@ CreateFakeRelcacheEntry(RelFileNode rnode)
376376
rel->rd_lockInfo.lockRelId.relId = rnode.relNode;
377377

378378
rel->rd_targblock = InvalidBlockNumber;
379-
rel->rd_fsm_nblocks_cache = InvalidBlockNumber;
379+
rel->rd_fsm_nblocks = InvalidBlockNumber;
380380
rel->rd_smgr = NULL;
381381

382382
return rel;

src/backend/storage/freespace/freespace.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* 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 $
1212
*
1313
*
1414
* NOTES:
@@ -101,7 +101,7 @@ static BlockNumber fsm_get_heap_blk(FSMAddress addr, uint16 slot);
101101
static BlockNumber fsm_logical_to_physical(FSMAddress addr);
102102

103103
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);
105105

106106
/* functions to convert amount of free space to a FSM category */
107107
static uint8 fsm_space_avail_to_cat(Size avail);
@@ -303,13 +303,13 @@ FreeSpaceMapTruncateRel(Relation rel, BlockNumber nblocks)
303303
smgrtruncate(rel->rd_smgr, FSM_FORKNUM, new_nfsmblocks, rel->rd_istemp);
304304

305305
/*
306-
* Need to invalidate the relcache entry, because rd_fsm_nblocks_cache
306+
* Need to invalidate the relcache entry, because rd_fsm_nblocks
307307
* seen by other backends is no longer valid.
308308
*/
309309
if (!InRecovery)
310310
CacheInvalidateRelcache(rel);
311311

312-
rel->rd_fsm_nblocks_cache = new_nfsmblocks;
312+
rel->rd_fsm_nblocks = new_nfsmblocks;
313313
}
314314

315315
/*
@@ -503,19 +503,20 @@ fsm_readbuf(Relation rel, FSMAddress addr, bool extend)
503503

504504
RelationOpenSmgr(rel);
505505

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)
508508
{
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);
511511
else
512-
rel->rd_fsm_nblocks_cache = smgrnblocks(rel->rd_smgr, FSM_FORKNUM);
512+
rel->rd_fsm_nblocks = 0;
513513
}
514514

515-
if (blkno >= rel->rd_fsm_nblocks_cache)
515+
/* Handle requests beyond EOF */
516+
if (blkno >= rel->rd_fsm_nblocks)
516517
{
517518
if (extend)
518-
fsm_extend(rel, blkno + 1, false);
519+
fsm_extend(rel, blkno + 1);
519520
else
520521
return InvalidBuffer;
521522
}
@@ -536,13 +537,12 @@ fsm_readbuf(Relation rel, FSMAddress addr, bool extend)
536537
/*
537538
* Ensure that the FSM fork is at least n_fsmblocks long, extending
538539
* 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.
541541
*/
542542
static void
543-
fsm_extend(Relation rel, BlockNumber n_fsmblocks, bool createstorage)
543+
fsm_extend(Relation rel, BlockNumber fsm_nblocks)
544544
{
545-
BlockNumber n_fsmblocks_now;
545+
BlockNumber fsm_nblocks_now;
546546
Page pg;
547547

548548
pg = (Page) palloc(BLCKSZ);
@@ -561,27 +561,30 @@ fsm_extend(Relation rel, BlockNumber n_fsmblocks, bool createstorage)
561561
LockRelationForExtension(rel, ExclusiveLock);
562562

563563
/* 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))
565566
{
566567
smgrcreate(rel->rd_smgr, FSM_FORKNUM, false);
567-
n_fsmblocks_now = 0;
568+
fsm_nblocks_now = 0;
568569
}
569570
else
570-
n_fsmblocks_now = smgrnblocks(rel->rd_smgr, FSM_FORKNUM);
571+
fsm_nblocks_now = smgrnblocks(rel->rd_smgr, FSM_FORKNUM);
571572

572-
while (n_fsmblocks_now < n_fsmblocks)
573+
while (fsm_nblocks_now < fsm_nblocks)
573574
{
574-
smgrextend(rel->rd_smgr, FSM_FORKNUM, n_fsmblocks_now,
575+
smgrextend(rel->rd_smgr, FSM_FORKNUM, fsm_nblocks_now,
575576
(char *) pg, rel->rd_istemp);
576-
n_fsmblocks_now++;
577+
fsm_nblocks_now++;
577578
}
578579

579580
UnlockRelationForExtension(rel, ExclusiveLock);
580581

581582
pfree(pg);
582583

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;
585588
}
586589

587590
/*

src/backend/utils/cache/relcache.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.276 2008/11/10 00:49:37 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.277 2008/11/26 17:08:57 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -304,7 +304,7 @@ AllocateRelationDesc(Relation relation, Form_pg_class relp)
304304
*/
305305
MemSet(relation, 0, sizeof(RelationData));
306306
relation->rd_targblock = InvalidBlockNumber;
307-
relation->rd_fsm_nblocks_cache = InvalidBlockNumber;
307+
relation->rd_fsm_nblocks = InvalidBlockNumber;
308308

309309
/* make sure relation is marked as having no open file yet */
310310
relation->rd_smgr = NULL;
@@ -1376,7 +1376,7 @@ formrdesc(const char *relationName, Oid relationReltype,
13761376
*/
13771377
relation = (Relation) palloc0(sizeof(RelationData));
13781378
relation->rd_targblock = InvalidBlockNumber;
1379-
relation->rd_fsm_nblocks_cache = InvalidBlockNumber;
1379+
relation->rd_fsm_nblocks = InvalidBlockNumber;
13801380

13811381
/* make sure relation is marked as having no open file yet */
13821382
relation->rd_smgr = NULL;
@@ -1665,9 +1665,9 @@ RelationReloadIndexInfo(Relation relation)
16651665
heap_freetuple(pg_class_tuple);
16661666
/* We must recalculate physical address in case it changed */
16671667
RelationInitPhysicalAddr(relation);
1668-
/* Must reset targblock and fsm_nblocks_cache in case rel was truncated */
1668+
/* Must reset targblock and fsm_nblocks in case rel was truncated */
16691669
relation->rd_targblock = InvalidBlockNumber;
1670-
relation->rd_fsm_nblocks_cache = InvalidBlockNumber;
1670+
relation->rd_fsm_nblocks = InvalidBlockNumber;
16711671
/* Must free any AM cached data, too */
16721672
if (relation->rd_amcache)
16731673
pfree(relation->rd_amcache);
@@ -1750,7 +1750,7 @@ RelationClearRelation(Relation relation, bool rebuild)
17501750
if (relation->rd_isnailed)
17511751
{
17521752
relation->rd_targblock = InvalidBlockNumber;
1753-
relation->rd_fsm_nblocks_cache = InvalidBlockNumber;
1753+
relation->rd_fsm_nblocks = InvalidBlockNumber;
17541754
if (relation->rd_rel->relkind == RELKIND_INDEX)
17551755
{
17561756
relation->rd_isvalid = false; /* needs to be revalidated */
@@ -2345,7 +2345,7 @@ RelationBuildLocalRelation(const char *relname,
23452345
rel = (Relation) palloc0(sizeof(RelationData));
23462346

23472347
rel->rd_targblock = InvalidBlockNumber;
2348-
rel->rd_fsm_nblocks_cache = InvalidBlockNumber;
2348+
rel->rd_fsm_nblocks = InvalidBlockNumber;
23492349

23502350
/* make sure relation is marked as having no open file yet */
23512351
rel->rd_smgr = NULL;
@@ -3602,7 +3602,7 @@ load_relcache_init_file(void)
36023602
*/
36033603
rel->rd_smgr = NULL;
36043604
rel->rd_targblock = InvalidBlockNumber;
3605-
rel->rd_fsm_nblocks_cache = InvalidBlockNumber;
3605+
rel->rd_fsm_nblocks = InvalidBlockNumber;
36063606
if (rel->rd_isnailed)
36073607
rel->rd_refcnt = 1;
36083608
else

src/include/utils/rel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.108 2008/09/30 10:52:14 heikki Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.109 2008/11/26 17:08:58 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -195,8 +195,8 @@ typedef struct RelationData
195195
List *rd_indpred; /* index predicate tree, if any */
196196
void *rd_amcache; /* available for use by index AM */
197197

198-
/* Cached last-seen size of the FSM */
199-
BlockNumber rd_fsm_nblocks_cache;
198+
/* size of the FSM, or InvalidBlockNumber if not known yet */
199+
BlockNumber rd_fsm_nblocks;
200200

201201
/* use "struct" here to avoid needing to include pgstat.h: */
202202
struct PgStat_TableStatus *pgstat_info; /* statistics collection area */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy