1 /*-------------------------------------------------------------------------
4 * POSTGRES free space map for quickly finding free pages in relations
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
16 * This is similar to the FSM used for heap, in freespace.c, but instead
17 * of tracking the amount of free space on pages, we only track whether
18 * pages are completely free or in-use. We use the same FSM implementation
19 * as for heaps, using BLCKSZ - 1 to denote used pages, and 0 for unused.
21 *-------------------------------------------------------------------------
25 #include "storage/freespace.h"
26 #include "storage/indexfsm.h"
27 #include "storage/smgr.h"
34 * InitIndexFreeSpaceMap - Create or reset the FSM fork for relation.
37 InitIndexFreeSpaceMap(Relation rel
)
39 /* Create FSM fork if it doesn't exist yet, or truncate it if it does */
40 RelationOpenSmgr(rel
);
41 if (!smgrexists(rel
->rd_smgr
, FSM_FORKNUM
))
42 smgrcreate(rel
->rd_smgr
, FSM_FORKNUM
, rel
->rd_istemp
, false);
44 smgrtruncate(rel
->rd_smgr
, FSM_FORKNUM
, 0, rel
->rd_istemp
);
48 * GetFreeIndexPage - return a free page from the FSM
50 * As a side effect, the page is marked as used in the FSM.
53 GetFreeIndexPage(Relation rel
)
55 BlockNumber blkno
= GetPageWithFreeSpace(rel
, BLCKSZ
/2);
57 if (blkno
!= InvalidBlockNumber
)
58 RecordUsedIndexPage(rel
, blkno
);
64 * RecordFreeIndexPage - mark a page as free in the FSM
67 RecordFreeIndexPage(Relation rel
, BlockNumber freeBlock
)
69 RecordPageWithFreeSpace(rel
, freeBlock
, BLCKSZ
- 1);
74 * RecordUsedIndexPage - mark a page as used in the FSM
77 RecordUsedIndexPage(Relation rel
, BlockNumber usedBlock
)
79 RecordPageWithFreeSpace(rel
, usedBlock
, 0);
83 * IndexFreeSpaceMapTruncate - adjust for truncation of a relation.
85 * We need to delete any stored data past the new relation length, so that
86 * we don't bogusly return removed block numbers.
89 IndexFreeSpaceMapTruncate(Relation rel
, BlockNumber nblocks
)
91 FreeSpaceMapTruncateRel(rel
, nblocks
);
95 * IndexFreeSpaceMapVacuum - scan and fix any inconsistencies in the FSM
98 IndexFreeSpaceMapVacuum(Relation rel
)
100 FreeSpaceMapVacuum(rel
);