Properly access a buffer's LSN using existing access macros instead of abusing
[PostgreSQL.git] / src / backend / storage / freespace / indexfsm.c
blob7e33879682d026c74777c9944562fadbde51852d
1 /*-------------------------------------------------------------------------
3 * indexfsm.c
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
10 * IDENTIFICATION
11 * $PostgreSQL$
14 * NOTES:
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 *-------------------------------------------------------------------------
23 #include "postgres.h"
25 #include "storage/freespace.h"
26 #include "storage/indexfsm.h"
27 #include "storage/smgr.h"
30 * Exported routines
34 * InitIndexFreeSpaceMap - Create or reset the FSM fork for relation.
36 void
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);
43 else
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.
52 BlockNumber
53 GetFreeIndexPage(Relation rel)
55 BlockNumber blkno = GetPageWithFreeSpace(rel, BLCKSZ/2);
57 if (blkno != InvalidBlockNumber)
58 RecordUsedIndexPage(rel, blkno);
60 return blkno;
64 * RecordFreeIndexPage - mark a page as free in the FSM
66 void
67 RecordFreeIndexPage(Relation rel, BlockNumber freeBlock)
69 RecordPageWithFreeSpace(rel, freeBlock, BLCKSZ - 1);
74 * RecordUsedIndexPage - mark a page as used in the FSM
76 void
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.
88 void
89 IndexFreeSpaceMapTruncate(Relation rel, BlockNumber nblocks)
91 FreeSpaceMapTruncateRel(rel, nblocks);
95 * IndexFreeSpaceMapVacuum - scan and fix any inconsistencies in the FSM
97 void
98 IndexFreeSpaceMapVacuum(Relation rel)
100 FreeSpaceMapVacuum(rel);