Skip to content

Commit 4274808

Browse files
committed
First non-stub implementation of shared free space map. It's not super
useful as yet, since its primary source of information is (full) VACUUM, which makes a concerted effort to get rid of free space before telling the map about it ... next stop is concurrent VACUUM ...
1 parent 755e367 commit 4274808

File tree

6 files changed

+994
-39
lines changed

6 files changed

+994
-39
lines changed

src/backend/commands/dbcommands.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.75 2001/06/12 05:55:49 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.76 2001/07/02 20:50:46 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -30,7 +30,8 @@
3030
#include "commands/comment.h"
3131
#include "commands/dbcommands.h"
3232
#include "miscadmin.h"
33-
#include "storage/sinval.h" /* for DatabaseHasActiveBackends */
33+
#include "storage/freespace.h"
34+
#include "storage/sinval.h"
3435
#include "utils/builtins.h"
3536
#include "utils/fmgroids.h"
3637
#include "utils/syscache.h"
@@ -372,6 +373,11 @@ dropdb(const char *dbname)
372373
*/
373374
DropBuffers(db_id);
374375

376+
/*
377+
* Also, clean out any entries in the shared free space map.
378+
*/
379+
FreeSpaceMapForgetDatabase(db_id);
380+
375381
/*
376382
* Remove the database's subdirectory and everything in it.
377383
*/

src/backend/commands/vacuum.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.200 2001/06/29 20:14:27 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.201 2001/07/02 20:50:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -37,6 +37,7 @@
3737
#include "commands/vacuum.h"
3838
#include "miscadmin.h"
3939
#include "nodes/execnodes.h"
40+
#include "storage/freespace.h"
4041
#include "storage/sinval.h"
4142
#include "storage/smgr.h"
4243
#include "tcop/tcopprot.h"
@@ -146,6 +147,8 @@ static void vacuum_index(VacPageList vacpagelist, Relation indrel,
146147
double num_tuples, int keep_tuples);
147148
static void scan_index(Relation indrel, double num_tuples);
148149
static VacPage tid_reaped(ItemPointer itemptr, VacPageList vacpagelist);
150+
static void vac_update_fsm(Relation onerel, VacPageList fraged_pages,
151+
BlockNumber rel_pages);
149152
static VacPage copy_vac_page(VacPage vacpage);
150153
static void vpage_insert(VacPageList vacpagelist, VacPage vpnew);
151154
static void get_indices(Relation relation, int *nindices, Relation **Irel);
@@ -579,6 +582,9 @@ vacuum_rel(Oid relid)
579582
activate_indexes_of_a_table(relid, true);
580583
#endif /* NOT_USED */
581584

585+
/* update shared free space map with final free space info */
586+
vac_update_fsm(onerel, &fraged_pages, vacrelstats->rel_pages);
587+
582588
/* all done with this class, but hold lock until commit */
583589
heap_close(onerel, NoLock);
584590

@@ -1157,6 +1163,10 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
11571163
* useful as move targets, since we only want to move down. Note
11581164
* that since we stop the outer loop at last_move_dest_block, pages
11591165
* removed here cannot have had anything moved onto them already.
1166+
*
1167+
* Also note that we don't change the stored fraged_pages list,
1168+
* only our local variable num_fraged_pages; so the forgotten pages
1169+
* are still available to be loaded into the free space map later.
11601170
*/
11611171
while (num_fraged_pages > 0 &&
11621172
fraged_pages->pagedesc[num_fraged_pages-1]->blkno >= blkno)
@@ -2080,6 +2090,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
20802090
if (blkno < nblocks)
20812091
{
20822092
blkno = smgrtruncate(DEFAULT_SMGR, onerel, blkno);
2093+
onerel->rd_nblocks = blkno; /* update relcache immediately */
2094+
onerel->rd_targblock = InvalidBlockNumber;
20832095
vacrelstats->rel_pages = blkno; /* set new number of blocks */
20842096
}
20852097

@@ -2145,6 +2157,8 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages)
21452157
RelationGetRelationName(onerel),
21462158
vacrelstats->rel_pages, relblocks);
21472159
relblocks = smgrtruncate(DEFAULT_SMGR, onerel, relblocks);
2160+
onerel->rd_nblocks = relblocks; /* update relcache immediately */
2161+
onerel->rd_targblock = InvalidBlockNumber;
21482162
vacrelstats->rel_pages = relblocks; /* set new number of
21492163
* blocks */
21502164
}
@@ -2414,6 +2428,45 @@ vac_update_relstats(Oid relid, BlockNumber num_pages, double num_tuples,
24142428
heap_close(rd, RowExclusiveLock);
24152429
}
24162430

2431+
/*
2432+
* Update the shared Free Space Map with the info we now have about
2433+
* free space in the relation, discarding any old info the map may have.
2434+
*/
2435+
static void
2436+
vac_update_fsm(Relation onerel, VacPageList fraged_pages,
2437+
BlockNumber rel_pages)
2438+
{
2439+
int nPages = fraged_pages->num_pages;
2440+
int i;
2441+
BlockNumber *pages;
2442+
Size *spaceAvail;
2443+
2444+
/* +1 to avoid palloc(0) */
2445+
pages = (BlockNumber *) palloc((nPages + 1) * sizeof(BlockNumber));
2446+
spaceAvail = (Size *) palloc((nPages + 1) * sizeof(Size));
2447+
2448+
for (i = 0; i < nPages; i++)
2449+
{
2450+
pages[i] = fraged_pages->pagedesc[i]->blkno;
2451+
spaceAvail[i] = fraged_pages->pagedesc[i]->free;
2452+
/*
2453+
* fraged_pages may contain entries for pages that we later decided
2454+
* to truncate from the relation; don't enter them into the map!
2455+
*/
2456+
if (pages[i] >= rel_pages)
2457+
{
2458+
nPages = i;
2459+
break;
2460+
}
2461+
}
2462+
2463+
MultiRecordFreeSpace(&onerel->rd_node,
2464+
0, MaxBlockNumber,
2465+
nPages, pages, spaceAvail);
2466+
pfree(pages);
2467+
pfree(spaceAvail);
2468+
}
2469+
24172470
/* Copy a VacPage structure */
24182471
static VacPage
24192472
copy_vac_page(VacPage vacpage)

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