1 /*-------------------------------------------------------------------------
4 * POSTGRES free space map for quickly finding free space in relations
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.47 2005/08/17 03:50:59 momjian Exp $
16 * The only really interesting aspect of this code is the heuristics for
17 * deciding how much information we can afford to keep about each relation,
18 * given that we have a limited amount of workspace in shared memory.
19 * These currently work as follows:
21 * The number of distinct relations tracked is limited by a configuration
22 * variable (MaxFSMRelations). When this would be exceeded, we discard the
23 * least recently used relation. A doubly-linked list with move-to-front
24 * behavior keeps track of which relation is least recently used.
26 * For each known relation, we track the average request size given to
27 * GetPageWithFreeSpace() as well as the most recent number of pages given
28 * to RecordRelationFreeSpace(). The average request size is not directly
29 * used in this module, but we expect VACUUM to use it to filter out
30 * uninteresting amounts of space before calling RecordRelationFreeSpace().
31 * The sum of the RRFS page counts is thus the total number of "interesting"
32 * pages that we would like to track; this is called DesiredFSMPages.
34 * The number of pages actually tracked is limited by a configuration variable
35 * (MaxFSMPages). When this is less than DesiredFSMPages, each relation
36 * gets to keep a fraction MaxFSMPages/DesiredFSMPages of its free pages.
37 * We discard pages with less free space to reach this target.
39 * Actually, our space allocation is done in "chunks" of CHUNKPAGES pages,
40 * with each relation guaranteed at least one chunk. This reduces thrashing
41 * of the storage allocations when there are small changes in the RRFS page
42 * counts from one VACUUM to the next. (XXX it might also be worthwhile to
43 * impose some kind of moving-average smoothing on the RRFS page counts?)
45 * So the actual arithmetic is: for each relation compute myRequest as the
46 * number of chunks needed to hold its RRFS page count (not counting the
47 * first, guaranteed chunk); compute sumRequests as the sum of these values
48 * over all relations; then for each relation figure its target allocation
50 * 1 + round(spareChunks * myRequest / sumRequests)
51 * where spareChunks = totalChunks - numRels is the number of chunks we have
52 * a choice what to do with. We round off these numbers because truncating
53 * all of them would waste significant space. But because of roundoff, it's
54 * possible for the last few relations to get less space than they should;
55 * the target allocation must be checked against remaining available space.
57 *-------------------------------------------------------------------------
66 #include "miscadmin.h"
67 #include "storage/fd.h"
68 #include "storage/freespace.h"
69 #include "storage/itemptr.h"
70 #include "storage/lwlock.h"
71 #include "storage/shmem.h"
74 /* Initial value for average-request moving average */
75 #define INITIAL_AVERAGE ((Size) (BLCKSZ / 32))
78 * Number of pages and bytes per allocation chunk. Indexes can squeeze 50%
79 * more pages into the same space because they don't need to remember how much
80 * free space on each page. The nominal number of pages, CHUNKPAGES, is for
81 * regular rels, and INDEXCHUNKPAGES is for indexes. CHUNKPAGES should be
82 * even so that no space is wasted in the index case.
85 #define CHUNKBYTES (CHUNKPAGES * sizeof(FSMPageData))
86 #define INDEXCHUNKPAGES ((int) (CHUNKBYTES / sizeof(IndexFSMPageData)))
90 * Typedefs and macros for items in the page-storage arena. We use the
91 * existing ItemPointer and BlockId data structures, which are designed
92 * to pack well (they should be 6 and 4 bytes apiece regardless of machine
93 * alignment issues). Unfortunately we can't use the ItemPointer access
94 * macros, because they include Asserts insisting that ip_posid != 0.
96 typedef ItemPointerData FSMPageData;
97 typedef BlockIdData IndexFSMPageData;
99 #define FSMPageGetPageNum(ptr) \
100 BlockIdGetBlockNumber(&(ptr)->ip_blkid)
101 #define FSMPageGetSpace(ptr) \
102 ((Size) (ptr)->ip_posid)
103 #define FSMPageSetPageNum(ptr, pg) \
104 BlockIdSet(&(ptr)->ip_blkid, pg)
105 #define FSMPageSetSpace(ptr, sz) \
106 ((ptr)->ip_posid = (OffsetNumber) (sz))
107 #define IndexFSMPageGetPageNum(ptr) \
108 BlockIdGetBlockNumber(ptr)
109 #define IndexFSMPageSetPageNum(ptr, pg) \
113 * During database shutdown, we store the contents of FSM into a disk file,
114 * which is re-read during startup. This way we don't have a startup
115 * transient condition where FSM isn't really functioning.
117 * The file format is:
119 * endian constant 0x01020304 for detecting endianness problems
122 * -- for each rel, in *reverse* usage order:
128 * arena data array of storedPages FSMPageData or IndexFSMPageData
132 /* Name of FSM cache file (relative to $PGDATA) */
133 #define FSM_CACHE_FILENAME "global/pg_fsm.cache"
135 /* Fixed values in header */
136 #define FSM_CACHE_LABEL "FSM"
137 #define FSM_CACHE_ENDIAN 0x01020304
138 #define FSM_CACHE_VERSION 20030305
140 /* File header layout */
141 typedef struct FsmCacheFileHeader
147 } FsmCacheFileHeader;
149 /* Per-relation header */
150 typedef struct FsmCacheRelHeader
152 RelFileNode key; /* hash key (must be first) */
153 bool isIndex; /* if true, we store only page numbers */
154 uint32 avgRequest; /* moving average of space requests */
155 int32 lastPageCount; /* pages passed to RecordRelationFreeSpace */
156 int32 storedPages; /* # of pages stored in arena */
161 * Shared free-space-map objects
163 * The per-relation objects are indexed by a hash table, and are also members
164 * of two linked lists: one ordered by recency of usage (most recent first),
165 * and the other ordered by physical location of the associated storage in
166 * the page-info arena.
168 * Each relation owns one or more chunks of per-page storage in the "arena".
169 * The chunks for each relation are always consecutive, so that it can treat
170 * its page storage as a simple array. We further insist that its page data
171 * be ordered by block number, so that binary search is possible.
173 * Note: we handle pointers to these items as pointers, not as SHMEM_OFFSETs.
174 * This assumes that all processes accessing the map will have the shared
175 * memory segment mapped at the same place in their address space.
177 typedef struct FSMHeader FSMHeader;
178 typedef struct FSMRelation FSMRelation;
180 /* Header for whole map */
183 FSMRelation *usageList; /* FSMRelations in usage-recency order */
184 FSMRelation *usageListTail; /* tail of usage-recency list */
185 FSMRelation *firstRel; /* FSMRelations in arena storage order */
186 FSMRelation *lastRel; /* tail of storage-order list */
187 int numRels; /* number of FSMRelations now in use */
188 double sumRequests; /* sum of requested chunks over all rels */
189 char *arena; /* arena for page-info storage */
190 int totalChunks; /* total size of arena, in chunks */
191 int usedChunks; /* # of chunks assigned */
192 /* NB: there are totalChunks - usedChunks free chunks at end of arena */
196 * Per-relation struct --- this is an entry in the shared hash table.
197 * The hash key is the RelFileNode value (hence, we look at the physical
198 * relation ID, not the logical ID, which is appropriate).
202 RelFileNode key; /* hash key (must be first) */
203 FSMRelation *nextUsage; /* next rel in usage-recency order */
204 FSMRelation *priorUsage; /* prior rel in usage-recency order */
205 FSMRelation *nextPhysical; /* next rel in arena-storage order */
206 FSMRelation *priorPhysical; /* prior rel in arena-storage order */
207 bool isIndex; /* if true, we store only page numbers */
208 Size avgRequest; /* moving average of space requests */
209 int lastPageCount; /* pages passed to RecordRelationFreeSpace */
210 int firstChunk; /* chunk # of my first chunk in arena */
211 int storedPages; /* # of pages stored in arena */
212 int nextPage; /* index (from 0) to start next search at */
216 int MaxFSMRelations; /* these are set by guc.c */
219 static FSMHeader *FreeSpaceMap; /* points to FSMHeader in shared memory */
220 static HTAB *FreeSpaceMapRelHash; /* points to (what used to be)
221 * FSMHeader->relHash */
224 static void CheckFreeSpaceMapStatistics(int elevel, int numRels,
226 static FSMRelation *lookup_fsm_rel(RelFileNode *rel);
227 static FSMRelation *create_fsm_rel(RelFileNode *rel);
228 static void delete_fsm_rel(FSMRelation *fsmrel);
229 static int realloc_fsm_rel(FSMRelation *fsmrel, int nPages, bool isIndex);
230 static void link_fsm_rel_usage(FSMRelation *fsmrel);
231 static void unlink_fsm_rel_usage(FSMRelation *fsmrel);
232 static void link_fsm_rel_storage(FSMRelation *fsmrel);
233 static void unlink_fsm_rel_storage(FSMRelation *fsmrel);
234 static BlockNumber find_free_space(FSMRelation *fsmrel, Size spaceNeeded);
235 static BlockNumber find_index_free_space(FSMRelation *fsmrel);
236 static void fsm_record_free_space(FSMRelation *fsmrel, BlockNumber page,
238 static bool lookup_fsm_page_entry(FSMRelation *fsmrel, BlockNumber page,
240 static void compact_fsm_storage(void);
241 static void push_fsm_rels_after(FSMRelation *afterRel);
242 static void pack_incoming_pages(FSMPageData *newLocation, int newPages,
243 PageFreeSpaceInfo *pageSpaces, int nPages);
244 static void pack_existing_pages(FSMPageData *newLocation, int newPages,
245 FSMPageData *oldLocation, int oldPages);
246 static int fsm_calc_request(FSMRelation *fsmrel);
247 static int fsm_calc_target_allocation(int myRequest);
248 static int fsm_current_chunks(FSMRelation *fsmrel);
249 static int fsm_current_allocation(FSMRelation *fsmrel);
258 * InitFreeSpaceMap -- Initialize the freespace module.
260 * This must be called once during shared memory initialization.
261 * It builds the empty free space map table. FreeSpaceLock must also be
262 * initialized at some point, but is not touched here --- we assume there is
263 * no need for locking, since only the calling process can be accessing shared
267 InitFreeSpaceMap(void)
273 /* Create table header */
274 FreeSpaceMap = (FSMHeader *) ShmemInitStruct("Free Space Map Header", sizeof(FSMHeader), &found);
275 if (FreeSpaceMap == NULL)
277 (errcode(ERRCODE_OUT_OF_MEMORY),
278 errmsg("insufficient shared memory for free space map")));
280 MemSet(FreeSpaceMap, 0, sizeof(FSMHeader));
282 /* Create hashtable for FSMRelations */
283 info.keysize = sizeof(RelFileNode);
284 info.entrysize = sizeof(FSMRelation);
285 info.hash = tag_hash;
287 FreeSpaceMapRelHash = ShmemInitHash("Free Space Map Hash",
291 (HASH_ELEM | HASH_FUNCTION));
293 if (!FreeSpaceMapRelHash)
295 (errcode(ERRCODE_OUT_OF_MEMORY),
296 errmsg("insufficient shared memory for free space map")));
302 /* Allocate page-storage arena */
303 nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
304 /* This check ensures spareChunks will be greater than zero */
305 if (nchunks <= MaxFSMRelations)
307 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
308 errmsg("max_fsm_pages must exceed max_fsm_relations * %d",
311 FreeSpaceMap->arena = (char *) ShmemAlloc(nchunks * CHUNKBYTES);
312 if (FreeSpaceMap->arena == NULL)
314 (errcode(ERRCODE_OUT_OF_MEMORY),
315 errmsg("insufficient shared memory for free space map")));
317 FreeSpaceMap->totalChunks = nchunks;
318 FreeSpaceMap->usedChunks = 0;
319 FreeSpaceMap->sumRequests = 0;
323 * Estimate amount of shmem space needed for FSM.
326 FreeSpaceShmemSize(void)
332 size = MAXALIGN(sizeof(FSMHeader));
334 /* hash table, including the FSMRelation objects */
335 size += hash_estimate_size(MaxFSMRelations + 1, sizeof(FSMRelation));
337 /* page-storage arena */
338 nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
340 if (nchunks >= (INT_MAX / CHUNKBYTES))
342 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
343 errmsg("max_fsm_pages is too large")));
345 size += MAXALIGN(nchunks * CHUNKBYTES);
351 * GetPageWithFreeSpace - try to find a page in the given relation with
352 * at least the specified amount of free space.
354 * If successful, return the block number; if not, return InvalidBlockNumber.
356 * The caller must be prepared for the possibility that the returned page
357 * will turn out to have too little space available by the time the caller
358 * gets a lock on it. In that case, the caller should report the actual
359 * amount of free space available on that page and then try again (see
360 * RecordAndGetPageWithFreeSpace). If InvalidBlockNumber is returned,
361 * extend the relation.
364 GetPageWithFreeSpace(RelFileNode *rel, Size spaceNeeded)
367 BlockNumber freepage;
369 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
372 * We always add a rel to the hashtable when it is inquired about.
374 fsmrel = create_fsm_rel(rel);
377 * Update the moving average of space requests. This code implements
378 * an exponential moving average with an equivalent period of about 63
379 * requests. Ignore silly requests, however, to ensure that the
380 * average stays sane.
382 if (spaceNeeded > 0 && spaceNeeded < BLCKSZ)
384 int cur_avg = (int) fsmrel->avgRequest;
386 cur_avg += ((int) spaceNeeded - cur_avg) / 32;
387 fsmrel->avgRequest = (Size) cur_avg;
389 freepage = find_free_space(fsmrel, spaceNeeded);
390 LWLockRelease(FreeSpaceLock);
395 * RecordAndGetPageWithFreeSpace - update info about a page and try again.
397 * We provide this combo form, instead of a separate Record operation,
398 * to save one lock and hash table lookup cycle.
401 RecordAndGetPageWithFreeSpace(RelFileNode *rel,
407 BlockNumber freepage;
409 /* Sanity check: ensure spaceAvail will fit into OffsetNumber */
410 AssertArg(oldSpaceAvail < BLCKSZ);
412 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
415 * We always add a rel to the hashtable when it is inquired about.
417 fsmrel = create_fsm_rel(rel);
420 fsm_record_free_space(fsmrel, oldPage, oldSpaceAvail);
423 * Update the moving average of space requests, same as in
424 * GetPageWithFreeSpace.
426 if (spaceNeeded > 0 && spaceNeeded < BLCKSZ)
428 int cur_avg = (int) fsmrel->avgRequest;
430 cur_avg += ((int) spaceNeeded - cur_avg) / 32;
431 fsmrel->avgRequest = (Size) cur_avg;
434 freepage = find_free_space(fsmrel, spaceNeeded);
435 LWLockRelease(FreeSpaceLock);
440 * GetAvgFSMRequestSize - get average FSM request size for a relation.
442 * If the relation is not known to FSM, return a default value.
445 GetAvgFSMRequestSize(RelFileNode *rel)
450 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
451 fsmrel = lookup_fsm_rel(rel);
453 result = fsmrel->avgRequest;
455 result = INITIAL_AVERAGE;
456 LWLockRelease(FreeSpaceLock);
461 * RecordRelationFreeSpace - record available-space info about a relation.
463 * Any pre-existing info about the relation is assumed obsolete and discarded.
465 * The given pageSpaces[] array must be sorted in order by blkno. Note that
466 * the FSM is at liberty to discard some or all of the data.
469 RecordRelationFreeSpace(RelFileNode *rel,
471 PageFreeSpaceInfo *pageSpaces)
475 /* Limit nPages to something sane */
478 else if (nPages > MaxFSMPages)
479 nPages = MaxFSMPages;
481 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
484 * Note we don't record info about a relation unless there's already
485 * an FSM entry for it, implying someone has done GetPageWithFreeSpace
486 * for it. Inactive rels thus will not clutter the map simply by
489 fsmrel = lookup_fsm_rel(rel);
494 FSMPageData *newLocation;
496 curAlloc = realloc_fsm_rel(fsmrel, nPages, false);
497 curAllocPages = curAlloc * CHUNKPAGES;
500 * If the data fits in our current allocation, just copy it;
501 * otherwise must compress.
503 newLocation = (FSMPageData *)
504 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
505 if (nPages <= curAllocPages)
509 for (i = 0; i < nPages; i++)
511 BlockNumber page = pageSpaces[i].blkno;
512 Size avail = pageSpaces[i].avail;
514 /* Check caller provides sorted data */
515 if (i > 0 && page <= pageSpaces[i - 1].blkno)
516 elog(ERROR, "free-space data is not in page order");
517 FSMPageSetPageNum(newLocation, page);
518 FSMPageSetSpace(newLocation, avail);
521 fsmrel->storedPages = nPages;
525 pack_incoming_pages(newLocation, curAllocPages,
527 fsmrel->storedPages = curAllocPages;
530 LWLockRelease(FreeSpaceLock);
534 * GetFreeIndexPage - like GetPageWithFreeSpace, but for indexes
537 GetFreeIndexPage(RelFileNode *rel)
540 BlockNumber freepage;
542 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
545 * We always add a rel to the hashtable when it is inquired about.
547 fsmrel = create_fsm_rel(rel);
549 freepage = find_index_free_space(fsmrel);
550 LWLockRelease(FreeSpaceLock);
555 * RecordIndexFreeSpace - like RecordRelationFreeSpace, but for indexes
558 RecordIndexFreeSpace(RelFileNode *rel,
564 /* Limit nPages to something sane */
567 else if (nPages > MaxFSMPages)
568 nPages = MaxFSMPages;
570 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
573 * Note we don't record info about a relation unless there's already
574 * an FSM entry for it, implying someone has done GetFreeIndexPage for
575 * it. Inactive rels thus will not clutter the map simply by being
578 fsmrel = lookup_fsm_rel(rel);
584 IndexFSMPageData *newLocation;
586 curAlloc = realloc_fsm_rel(fsmrel, nPages, true);
587 curAllocPages = curAlloc * INDEXCHUNKPAGES;
590 * If the data fits in our current allocation, just copy it;
591 * otherwise must compress. But compression is easy: we merely
592 * forget extra pages.
594 newLocation = (IndexFSMPageData *)
595 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
596 if (nPages > curAllocPages)
597 nPages = curAllocPages;
599 for (i = 0; i < nPages; i++)
601 BlockNumber page = pages[i];
603 /* Check caller provides sorted data */
604 if (i > 0 && page <= pages[i - 1])
605 elog(ERROR, "free-space data is not in page order");
606 IndexFSMPageSetPageNum(newLocation, page);
609 fsmrel->storedPages = nPages;
611 LWLockRelease(FreeSpaceLock);
615 * FreeSpaceMapTruncateRel - adjust for truncation of a relation.
617 * We need to delete any stored data past the new relation length, so that
618 * we don't bogusly return removed block numbers.
621 FreeSpaceMapTruncateRel(RelFileNode *rel, BlockNumber nblocks)
625 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
626 fsmrel = lookup_fsm_rel(rel);
631 /* Use lookup to locate first entry >= nblocks */
632 (void) lookup_fsm_page_entry(fsmrel, nblocks, &pageIndex);
633 /* Delete all such entries */
634 fsmrel->storedPages = pageIndex;
635 /* XXX should we adjust rel's lastPageCount and sumRequests? */
637 LWLockRelease(FreeSpaceLock);
641 * FreeSpaceMapForgetRel - forget all about a relation.
643 * This is called when a relation is deleted. Although we could just let
644 * the rel age out of the map, it's better to reclaim and reuse the space
648 FreeSpaceMapForgetRel(RelFileNode *rel)
652 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
653 fsmrel = lookup_fsm_rel(rel);
655 delete_fsm_rel(fsmrel);
656 LWLockRelease(FreeSpaceLock);
660 * FreeSpaceMapForgetDatabase - forget all relations of a database.
662 * This is called during DROP DATABASE. As above, might as well reclaim
663 * map space sooner instead of later.
666 FreeSpaceMapForgetDatabase(Oid dbid)
671 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
672 for (fsmrel = FreeSpaceMap->usageList; fsmrel; fsmrel = nextrel)
674 nextrel = fsmrel->nextUsage; /* in case we delete it */
675 if (fsmrel->key.dbNode == dbid)
676 delete_fsm_rel(fsmrel);
678 LWLockRelease(FreeSpaceLock);
682 * PrintFreeSpaceMapStatistics - print statistics about FSM contents
684 * The info is sent to ereport() with the specified message level. This is
685 * intended for use during VACUUM.
688 PrintFreeSpaceMapStatistics(int elevel)
696 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
697 /* Count total space used --- tedious, but seems useful */
698 for (fsmrel = FreeSpaceMap->firstRel;
700 fsmrel = fsmrel->nextPhysical)
701 storedPages += fsmrel->storedPages;
703 /* Copy other stats before dropping lock */
704 numRels = FreeSpaceMap->numRels;
705 sumRequests = FreeSpaceMap->sumRequests;
706 LWLockRelease(FreeSpaceLock);
708 /* Convert stats to actual number of page slots needed */
709 needed = (sumRequests + numRels) * CHUNKPAGES;
712 (errmsg("free space map contains %d pages in %d relations",
713 storedPages, numRels),
714 errdetail("A total of %.0f page slots are in use (including overhead).\n"
715 "%.0f page slots are required to track all free space.\n"
716 "Current limits are: %d page slots, %d relations, using %.0f KB.",
717 Min(needed, MaxFSMPages),
718 needed, MaxFSMPages, MaxFSMRelations,
719 (double) FreeSpaceShmemSize() / 1024.0)));
721 CheckFreeSpaceMapStatistics(NOTICE, numRels, needed);
722 /* Print to server logs too because is deals with a config variable. */
723 CheckFreeSpaceMapStatistics(LOG, numRels, needed);
727 CheckFreeSpaceMapStatistics(int elevel, int numRels, double needed)
729 if (numRels == MaxFSMRelations)
731 (errmsg("max_fsm_relations(%d) equals the number of relations checked",
733 errhint("You have >= %d relations.\n"
734 "Consider increasing the configuration parameter \"max_fsm_relations\".",
736 else if (needed > MaxFSMPages)
738 (errmsg("the number of page slots needed (%.0f) exceeds max_fsm_pages (%d)",
739 needed, MaxFSMPages),
740 errhint("Consider increasing the configuration parameter \"max_fsm_pages\"\n"
741 "to a value over %.0f.", needed)));
745 * DumpFreeSpaceMap - dump contents of FSM into a disk file for later reload
747 * This is expected to be called during database shutdown, after updates to
748 * the FSM have stopped. We lock the FreeSpaceLock but that's purely pro
749 * forma --- if anyone else is still accessing FSM, there's a problem.
752 DumpFreeSpaceMap(int code, Datum arg)
755 FsmCacheFileHeader header;
758 /* Try to create file */
759 unlink(FSM_CACHE_FILENAME); /* in case it exists w/wrong permissions */
761 fp = AllocateFile(FSM_CACHE_FILENAME, PG_BINARY_W);
764 elog(LOG, "could not write \"%s\": %m", FSM_CACHE_FILENAME);
768 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
770 /* Write file header */
771 MemSet(&header, 0, sizeof(header));
772 strcpy(header.label, FSM_CACHE_LABEL);
773 header.endian = FSM_CACHE_ENDIAN;
774 header.version = FSM_CACHE_VERSION;
775 header.numRels = FreeSpaceMap->numRels;
776 if (fwrite(&header, 1, sizeof(header), fp) != sizeof(header))
779 /* For each relation, in order from least to most recently used... */
780 for (fsmrel = FreeSpaceMap->usageListTail;
782 fsmrel = fsmrel->priorUsage)
784 FsmCacheRelHeader relheader;
787 /* Write relation header */
788 MemSet(&relheader, 0, sizeof(relheader));
789 relheader.key = fsmrel->key;
790 relheader.isIndex = fsmrel->isIndex;
791 relheader.avgRequest = fsmrel->avgRequest;
792 relheader.lastPageCount = fsmrel->lastPageCount;
793 relheader.storedPages = fsmrel->storedPages;
794 if (fwrite(&relheader, 1, sizeof(relheader), fp) != sizeof(relheader))
797 /* Write the per-page data directly from the arena */
798 nPages = fsmrel->storedPages;
805 len = nPages * sizeof(IndexFSMPageData);
807 len = nPages * sizeof(FSMPageData);
809 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
810 if (fwrite(data, 1, len, fp) != len)
816 LWLockRelease(FreeSpaceLock);
820 elog(LOG, "could not write \"%s\": %m", FSM_CACHE_FILENAME);
821 /* Remove busted cache file */
822 unlink(FSM_CACHE_FILENAME);
828 elog(LOG, "could not write \"%s\": %m", FSM_CACHE_FILENAME);
831 LWLockRelease(FreeSpaceLock);
835 /* Remove busted cache file */
836 unlink(FSM_CACHE_FILENAME);
840 * LoadFreeSpaceMap - load contents of FSM from a disk file
842 * This is expected to be called during database startup, before any FSM
843 * updates begin. We lock the FreeSpaceLock but that's purely pro
844 * forma --- if anyone else is accessing FSM yet, there's a problem.
846 * Notes: no complaint is issued if no cache file is found. If the file is
847 * found, it is deleted after reading. Thus, if we crash without a clean
848 * shutdown, the next cycle of life starts with no FSM data. To do otherwise,
849 * we'd need to do significantly more validation in this routine, because of
850 * the likelihood that what is in the dump file would be out-of-date, eg
851 * there might be entries for deleted or truncated rels.
854 LoadFreeSpaceMap(void)
857 FsmCacheFileHeader header;
860 /* Try to open file */
861 fp = AllocateFile(FSM_CACHE_FILENAME, PG_BINARY_R);
865 elog(LOG, "could not read \"%s\": %m", FSM_CACHE_FILENAME);
869 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
871 /* Read and verify file header */
872 if (fread(&header, 1, sizeof(header), fp) != sizeof(header) ||
873 strcmp(header.label, FSM_CACHE_LABEL) != 0 ||
874 header.endian != FSM_CACHE_ENDIAN ||
875 header.version != FSM_CACHE_VERSION ||
878 elog(LOG, "bogus file header in \"%s\"", FSM_CACHE_FILENAME);
882 /* For each relation, in order from least to most recently used... */
883 for (relno = 0; relno < header.numRels; relno++)
885 FsmCacheRelHeader relheader;
893 /* Read and verify relation header, as best we can */
894 if (fread(&relheader, 1, sizeof(relheader), fp) != sizeof(relheader) ||
895 (relheader.isIndex != false && relheader.isIndex != true) ||
896 relheader.avgRequest >= BLCKSZ ||
897 relheader.lastPageCount < 0 ||
898 relheader.storedPages < 0)
900 elog(LOG, "bogus rel header in \"%s\"", FSM_CACHE_FILENAME);
904 /* Make sure lastPageCount doesn't exceed current MaxFSMPages */
905 if (relheader.lastPageCount > MaxFSMPages)
906 relheader.lastPageCount = MaxFSMPages;
908 /* Read the per-page data */
909 nPages = relheader.storedPages;
910 if (relheader.isIndex)
911 len = nPages * sizeof(IndexFSMPageData);
913 len = nPages * sizeof(FSMPageData);
914 data = (char *) palloc(len);
915 if (fread(data, 1, len, fp) != len)
917 elog(LOG, "premature EOF in \"%s\"", FSM_CACHE_FILENAME);
923 * Okay, create the FSM entry and insert data into it. Since the
924 * rels were stored in reverse usage order, at the end of the loop
925 * they will be correctly usage-ordered in memory; and if
926 * MaxFSMRelations is less than it used to be, we will correctly
927 * drop the least recently used ones.
929 fsmrel = create_fsm_rel(&relheader.key);
930 fsmrel->avgRequest = relheader.avgRequest;
932 curAlloc = realloc_fsm_rel(fsmrel, relheader.lastPageCount,
934 if (relheader.isIndex)
936 IndexFSMPageData *newLocation;
938 curAllocPages = curAlloc * INDEXCHUNKPAGES;
941 * If the data fits in our current allocation, just copy it;
942 * otherwise must compress. But compression is easy: we
943 * merely forget extra pages.
945 newLocation = (IndexFSMPageData *)
946 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
947 if (nPages > curAllocPages)
948 nPages = curAllocPages;
949 memcpy(newLocation, data, nPages * sizeof(IndexFSMPageData));
950 fsmrel->storedPages = nPages;
954 FSMPageData *newLocation;
956 curAllocPages = curAlloc * CHUNKPAGES;
959 * If the data fits in our current allocation, just copy it;
960 * otherwise must compress.
962 newLocation = (FSMPageData *)
963 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
964 if (nPages <= curAllocPages)
966 memcpy(newLocation, data, nPages * sizeof(FSMPageData));
967 fsmrel->storedPages = nPages;
971 pack_existing_pages(newLocation, curAllocPages,
972 (FSMPageData *) data, nPages);
973 fsmrel->storedPages = curAllocPages;
983 LWLockRelease(FreeSpaceLock);
987 /* Remove cache file before it can become stale; see notes above */
988 unlink(FSM_CACHE_FILENAME);
993 * Internal routines. These all assume the caller holds the FreeSpaceLock.
997 * Lookup a relation in the hash table. If not present, return NULL.
999 * The relation's position in the LRU list is not changed.
1001 static FSMRelation *
1002 lookup_fsm_rel(RelFileNode *rel)
1004 FSMRelation *fsmrel;
1006 fsmrel = (FSMRelation *) hash_search(FreeSpaceMapRelHash,
1017 * Lookup a relation in the hash table, creating an entry if not present.
1019 * On successful lookup, the relation is moved to the front of the LRU list.
1021 static FSMRelation *
1022 create_fsm_rel(RelFileNode *rel)
1024 FSMRelation *fsmrel;
1027 fsmrel = (FSMRelation *) hash_search(FreeSpaceMapRelHash,
1034 /* New hashtable entry, initialize it (hash_search set the key) */
1035 fsmrel->isIndex = false; /* until we learn different */
1036 fsmrel->avgRequest = INITIAL_AVERAGE;
1037 fsmrel->lastPageCount = 0;
1038 fsmrel->firstChunk = -1; /* no space allocated */
1039 fsmrel->storedPages = 0;
1040 fsmrel->nextPage = 0;
1042 /* Discard lowest-priority existing rel, if we are over limit */
1043 if (FreeSpaceMap->numRels >= MaxFSMRelations)
1044 delete_fsm_rel(FreeSpaceMap->usageListTail);
1046 /* Add new entry at front of LRU list */
1047 link_fsm_rel_usage(fsmrel);
1048 fsmrel->nextPhysical = NULL; /* not in physical-storage list */
1049 fsmrel->priorPhysical = NULL;
1050 FreeSpaceMap->numRels++;
1051 /* sumRequests is unchanged because request must be zero */
1055 /* Existing entry, move to front of LRU list */
1056 if (fsmrel->priorUsage != NULL)
1058 unlink_fsm_rel_usage(fsmrel);
1059 link_fsm_rel_usage(fsmrel);
1067 * Remove an existing FSMRelation entry.
1070 delete_fsm_rel(FSMRelation *fsmrel)
1072 FSMRelation *result;
1074 FreeSpaceMap->sumRequests -= fsm_calc_request(fsmrel);
1075 unlink_fsm_rel_usage(fsmrel);
1076 unlink_fsm_rel_storage(fsmrel);
1077 FreeSpaceMap->numRels--;
1078 result = (FSMRelation *) hash_search(FreeSpaceMapRelHash,
1079 (void *) &(fsmrel->key),
1083 elog(ERROR, "FreeSpaceMap hashtable corrupted");
1087 * Reallocate space for a FSMRelation.
1089 * This is shared code for RecordRelationFreeSpace and RecordIndexFreeSpace.
1090 * The return value is the actual new allocation, in chunks.
1093 realloc_fsm_rel(FSMRelation *fsmrel, int nPages, bool isIndex)
1100 * Delete any existing entries, and update request status.
1102 fsmrel->storedPages = 0;
1103 FreeSpaceMap->sumRequests -= fsm_calc_request(fsmrel);
1104 fsmrel->lastPageCount = nPages;
1105 fsmrel->isIndex = isIndex;
1106 myRequest = fsm_calc_request(fsmrel);
1107 FreeSpaceMap->sumRequests += myRequest;
1108 myAlloc = fsm_calc_target_allocation(myRequest);
1111 * Need to reallocate space if (a) my target allocation is more than
1112 * my current allocation, AND (b) my actual immediate need
1113 * (myRequest+1 chunks) is more than my current allocation. Otherwise
1114 * just store the new data in-place.
1116 curAlloc = fsm_current_allocation(fsmrel);
1117 if (myAlloc > curAlloc && (myRequest + 1) > curAlloc && nPages > 0)
1119 /* Remove entry from storage list, and compact */
1120 unlink_fsm_rel_storage(fsmrel);
1121 compact_fsm_storage();
1122 /* Reattach to end of storage list */
1123 link_fsm_rel_storage(fsmrel);
1124 /* And allocate storage */
1125 fsmrel->firstChunk = FreeSpaceMap->usedChunks;
1126 FreeSpaceMap->usedChunks += myAlloc;
1128 /* Watch out for roundoff error */
1129 if (FreeSpaceMap->usedChunks > FreeSpaceMap->totalChunks)
1131 FreeSpaceMap->usedChunks = FreeSpaceMap->totalChunks;
1132 curAlloc = FreeSpaceMap->totalChunks - fsmrel->firstChunk;
1139 * Link a FSMRelation into the LRU list (always at the head).
1142 link_fsm_rel_usage(FSMRelation *fsmrel)
1144 fsmrel->priorUsage = NULL;
1145 fsmrel->nextUsage = FreeSpaceMap->usageList;
1146 FreeSpaceMap->usageList = fsmrel;
1147 if (fsmrel->nextUsage != NULL)
1148 fsmrel->nextUsage->priorUsage = fsmrel;
1150 FreeSpaceMap->usageListTail = fsmrel;
1154 * Delink a FSMRelation from the LRU list.
1157 unlink_fsm_rel_usage(FSMRelation *fsmrel)
1159 if (fsmrel->priorUsage != NULL)
1160 fsmrel->priorUsage->nextUsage = fsmrel->nextUsage;
1162 FreeSpaceMap->usageList = fsmrel->nextUsage;
1163 if (fsmrel->nextUsage != NULL)
1164 fsmrel->nextUsage->priorUsage = fsmrel->priorUsage;
1166 FreeSpaceMap->usageListTail = fsmrel->priorUsage;
1169 * We don't bother resetting fsmrel's links, since it's about to be
1170 * deleted or relinked at the head.
1175 * Link a FSMRelation into the storage-order list (always at the tail).
1178 link_fsm_rel_storage(FSMRelation *fsmrel)
1180 fsmrel->nextPhysical = NULL;
1181 fsmrel->priorPhysical = FreeSpaceMap->lastRel;
1182 if (FreeSpaceMap->lastRel != NULL)
1183 FreeSpaceMap->lastRel->nextPhysical = fsmrel;
1185 FreeSpaceMap->firstRel = fsmrel;
1186 FreeSpaceMap->lastRel = fsmrel;
1190 * Delink a FSMRelation from the storage-order list, if it's in it.
1193 unlink_fsm_rel_storage(FSMRelation *fsmrel)
1195 if (fsmrel->priorPhysical != NULL || FreeSpaceMap->firstRel == fsmrel)
1197 if (fsmrel->priorPhysical != NULL)
1198 fsmrel->priorPhysical->nextPhysical = fsmrel->nextPhysical;
1200 FreeSpaceMap->firstRel = fsmrel->nextPhysical;
1201 if (fsmrel->nextPhysical != NULL)
1202 fsmrel->nextPhysical->priorPhysical = fsmrel->priorPhysical;
1204 FreeSpaceMap->lastRel = fsmrel->priorPhysical;
1206 /* mark as not in list, since we may not put it back immediately */
1207 fsmrel->nextPhysical = NULL;
1208 fsmrel->priorPhysical = NULL;
1209 /* Also mark it as having no storage */
1210 fsmrel->firstChunk = -1;
1211 fsmrel->storedPages = 0;
1215 * Look to see if a page with at least the specified amount of space is
1216 * available in the given FSMRelation. If so, return its page number,
1217 * and advance the nextPage counter so that the next inquiry will return
1218 * a different page if possible; also update the entry to show that the
1219 * requested space is not available anymore. Return InvalidBlockNumber
1223 find_free_space(FSMRelation *fsmrel, Size spaceNeeded)
1226 int pagesToCheck, /* outer loop counter */
1227 pageIndex; /* current page index */
1229 if (fsmrel->isIndex)
1230 elog(ERROR, "find_free_space called for an index relation");
1231 info = (FSMPageData *)
1232 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1233 pageIndex = fsmrel->nextPage;
1234 /* Last operation may have left nextPage pointing past end */
1235 if (pageIndex >= fsmrel->storedPages)
1238 for (pagesToCheck = fsmrel->storedPages; pagesToCheck > 0; pagesToCheck--)
1240 FSMPageData *page = info + pageIndex;
1241 Size spaceAvail = FSMPageGetSpace(page);
1243 /* Check this page */
1244 if (spaceAvail >= spaceNeeded)
1247 * Found what we want --- adjust the entry, and update
1250 FSMPageSetSpace(page, spaceAvail - spaceNeeded);
1251 fsmrel->nextPage = pageIndex + 1;
1252 return FSMPageGetPageNum(page);
1254 /* Advance pageIndex, wrapping around if needed */
1255 if (++pageIndex >= fsmrel->storedPages)
1259 return InvalidBlockNumber; /* nothing found */
1263 * As above, but for index case --- we only deal in whole pages.
1266 find_index_free_space(FSMRelation *fsmrel)
1268 IndexFSMPageData *info;
1272 * If isIndex isn't set, it could be that RecordIndexFreeSpace() has
1273 * never yet been called on this relation, and we're still looking at
1274 * the default setting from create_fsm_rel(). If so, just act as
1275 * though there's no space.
1277 if (!fsmrel->isIndex)
1279 if (fsmrel->storedPages == 0)
1280 return InvalidBlockNumber;
1281 elog(ERROR, "find_index_free_space called for a non-index relation");
1285 * For indexes, there's no need for the nextPage state variable; we
1286 * just remove and return the first available page. (We could save
1287 * cycles here by returning the last page, but it seems better to
1288 * encourage re-use of lower-numbered pages.)
1290 if (fsmrel->storedPages <= 0)
1291 return InvalidBlockNumber; /* no pages available */
1292 info = (IndexFSMPageData *)
1293 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1294 result = IndexFSMPageGetPageNum(info);
1295 fsmrel->storedPages--;
1296 memmove(info, info + 1, fsmrel->storedPages * sizeof(IndexFSMPageData));
1301 * fsm_record_free_space - guts of RecordFreeSpace operation (now only
1302 * provided as part of RecordAndGetPageWithFreeSpace).
1305 fsm_record_free_space(FSMRelation *fsmrel, BlockNumber page, Size spaceAvail)
1309 if (fsmrel->isIndex)
1310 elog(ERROR, "fsm_record_free_space called for an index relation");
1311 if (lookup_fsm_page_entry(fsmrel, page, &pageIndex))
1313 /* Found an existing entry for page; update it */
1316 info = (FSMPageData *)
1317 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1319 FSMPageSetSpace(info, spaceAvail);
1324 * No existing entry; ignore the call. We used to add the page to
1325 * the FSM --- but in practice, if the page hasn't got enough
1326 * space to satisfy the caller who's kicking it back to us, then
1327 * it's probably uninteresting to everyone else as well.
1333 * Look for an entry for a specific page (block number) in a FSMRelation.
1334 * Returns TRUE if a matching entry exists, else FALSE.
1336 * The output argument *outPageIndex is set to indicate where the entry exists
1337 * (if TRUE result) or could be inserted (if FALSE result).
1340 lookup_fsm_page_entry(FSMRelation *fsmrel, BlockNumber page,
1343 /* Check for empty relation */
1344 if (fsmrel->storedPages <= 0)
1350 /* Do binary search */
1351 if (fsmrel->isIndex)
1353 IndexFSMPageData *info;
1357 info = (IndexFSMPageData *)
1358 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1360 high = fsmrel->storedPages - 1;
1366 middle = low + (high - low) / 2;
1367 probe = IndexFSMPageGetPageNum(info + middle);
1370 *outPageIndex = middle;
1373 else if (probe < page)
1378 *outPageIndex = low;
1387 info = (FSMPageData *)
1388 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1390 high = fsmrel->storedPages - 1;
1396 middle = low + (high - low) / 2;
1397 probe = FSMPageGetPageNum(info + middle);
1400 *outPageIndex = middle;
1403 else if (probe < page)
1408 *outPageIndex = low;
1414 * Re-pack the FSM storage arena, dropping data if necessary to meet the
1415 * current allocation target for each relation. At conclusion, all available
1416 * space in the arena will be coalesced at the end.
1419 compact_fsm_storage(void)
1421 int nextChunkIndex = 0;
1422 bool did_push = false;
1423 FSMRelation *fsmrel;
1425 for (fsmrel = FreeSpaceMap->firstRel;
1427 fsmrel = fsmrel->nextPhysical)
1438 * Calculate target allocation, make sure we don't overrun due to
1441 newAlloc = fsm_calc_target_allocation(fsm_calc_request(fsmrel));
1442 if (newAlloc > FreeSpaceMap->totalChunks - nextChunkIndex)
1443 newAlloc = FreeSpaceMap->totalChunks - nextChunkIndex;
1444 if (fsmrel->isIndex)
1445 newAllocPages = newAlloc * INDEXCHUNKPAGES;
1447 newAllocPages = newAlloc * CHUNKPAGES;
1450 * Determine current size, current and new locations
1452 curChunks = fsm_current_chunks(fsmrel);
1453 oldChunkIndex = fsmrel->firstChunk;
1454 oldLocation = FreeSpaceMap->arena + oldChunkIndex * CHUNKBYTES;
1455 newChunkIndex = nextChunkIndex;
1456 newLocation = FreeSpaceMap->arena + newChunkIndex * CHUNKBYTES;
1459 * It's possible that we have to move data down, not up, if the
1460 * allocations of previous rels expanded. This normally means
1461 * that our allocation expanded too (or at least got no worse),
1462 * and ditto for later rels. So there should be room to move all
1463 * our data down without dropping any --- but we might have to
1464 * push down following rels to acquire the room. We don't want to
1465 * do the push more than once, so pack everything against the end
1466 * of the arena if so.
1468 * In corner cases where we are on the short end of a roundoff choice
1469 * that we were formerly on the long end of, it's possible that we
1470 * have to move down and compress our data too. In fact, even
1471 * after pushing down the following rels, there might not be as
1472 * much space as we computed for this rel above --- that would
1473 * imply that some following rel(s) are also on the losing end of
1474 * roundoff choices. We could handle this fairly by doing the
1475 * per-rel compactions out-of-order, but that seems like way too
1476 * much complexity to deal with a very infrequent corner case.
1477 * Instead, we simply drop pages from the end of the current rel's
1478 * data until it fits.
1480 if (newChunkIndex > oldChunkIndex)
1482 int limitChunkIndex;
1484 if (newAllocPages < fsmrel->storedPages)
1486 /* move and compress --- just drop excess pages */
1487 fsmrel->storedPages = newAllocPages;
1488 curChunks = fsm_current_chunks(fsmrel);
1490 /* is there enough space? */
1491 if (fsmrel->nextPhysical != NULL)
1492 limitChunkIndex = fsmrel->nextPhysical->firstChunk;
1494 limitChunkIndex = FreeSpaceMap->totalChunks;
1495 if (newChunkIndex + curChunks > limitChunkIndex)
1497 /* not enough space, push down following rels */
1500 push_fsm_rels_after(fsmrel);
1503 /* now is there enough space? */
1504 if (fsmrel->nextPhysical != NULL)
1505 limitChunkIndex = fsmrel->nextPhysical->firstChunk;
1507 limitChunkIndex = FreeSpaceMap->totalChunks;
1508 if (newChunkIndex + curChunks > limitChunkIndex)
1510 /* uh-oh, forcibly cut the allocation to fit */
1511 newAlloc = limitChunkIndex - newChunkIndex;
1514 * If newAlloc < 0 at this point, we are moving the
1515 * rel's firstChunk into territory currently assigned
1516 * to a later rel. This is okay so long as we do not
1517 * copy any data. The rels will be back in
1518 * nondecreasing firstChunk order at completion of the
1523 if (fsmrel->isIndex)
1524 newAllocPages = newAlloc * INDEXCHUNKPAGES;
1526 newAllocPages = newAlloc * CHUNKPAGES;
1527 fsmrel->storedPages = newAllocPages;
1528 curChunks = fsm_current_chunks(fsmrel);
1531 memmove(newLocation, oldLocation, curChunks * CHUNKBYTES);
1533 else if (newAllocPages < fsmrel->storedPages)
1536 * Need to compress the page data. For an index,
1537 * "compression" just means dropping excess pages; otherwise
1538 * we try to keep the ones with the most space.
1540 if (fsmrel->isIndex)
1542 fsmrel->storedPages = newAllocPages;
1543 /* may need to move data */
1544 if (newChunkIndex != oldChunkIndex)
1545 memmove(newLocation, oldLocation, newAlloc * CHUNKBYTES);
1549 pack_existing_pages((FSMPageData *) newLocation,
1551 (FSMPageData *) oldLocation,
1552 fsmrel->storedPages);
1553 fsmrel->storedPages = newAllocPages;
1556 else if (newChunkIndex != oldChunkIndex)
1559 * No compression needed, but must copy the data up
1561 memmove(newLocation, oldLocation, curChunks * CHUNKBYTES);
1563 fsmrel->firstChunk = newChunkIndex;
1564 nextChunkIndex += newAlloc;
1566 Assert(nextChunkIndex <= FreeSpaceMap->totalChunks);
1567 FreeSpaceMap->usedChunks = nextChunkIndex;
1571 * Push all FSMRels physically after afterRel to the end of the storage arena.
1573 * We sometimes have to do this when deletion or truncation of a relation
1574 * causes the allocations of remaining rels to expand markedly. We must
1575 * temporarily push existing data down to the end so that we can move it
1576 * back up in an orderly fashion.
1579 push_fsm_rels_after(FSMRelation *afterRel)
1581 int nextChunkIndex = FreeSpaceMap->totalChunks;
1582 FSMRelation *fsmrel;
1584 FreeSpaceMap->usedChunks = FreeSpaceMap->totalChunks;
1586 for (fsmrel = FreeSpaceMap->lastRel;
1588 fsmrel = fsmrel->priorPhysical)
1596 if (fsmrel == afterRel)
1599 chunkCount = fsm_current_chunks(fsmrel);
1600 nextChunkIndex -= chunkCount;
1601 newChunkIndex = nextChunkIndex;
1602 oldChunkIndex = fsmrel->firstChunk;
1603 if (newChunkIndex < oldChunkIndex)
1605 /* we're pushing down, how can it move up? */
1606 elog(PANIC, "inconsistent entry sizes in FSM");
1608 else if (newChunkIndex > oldChunkIndex)
1610 /* need to move it */
1611 newLocation = FreeSpaceMap->arena + newChunkIndex * CHUNKBYTES;
1612 oldLocation = FreeSpaceMap->arena + oldChunkIndex * CHUNKBYTES;
1613 memmove(newLocation, oldLocation, chunkCount * CHUNKBYTES);
1614 fsmrel->firstChunk = newChunkIndex;
1617 Assert(nextChunkIndex >= 0);
1621 * Pack a set of per-page freespace data into a smaller amount of space.
1623 * The method is to compute a low-resolution histogram of the free space
1624 * amounts, then determine which histogram bin contains the break point.
1625 * We then keep all pages above that bin, none below it, and just enough
1626 * of the pages in that bin to fill the output area exactly.
1628 #define HISTOGRAM_BINS 64
1631 pack_incoming_pages(FSMPageData *newLocation, int newPages,
1632 PageFreeSpaceInfo *pageSpaces, int nPages)
1634 int histogram[HISTOGRAM_BINS];
1641 Assert(newPages < nPages); /* else I shouldn't have been called */
1642 /* Build histogram */
1643 MemSet(histogram, 0, sizeof(histogram));
1644 for (i = 0; i < nPages; i++)
1646 Size avail = pageSpaces[i].avail;
1648 if (avail >= BLCKSZ)
1649 elog(ERROR, "bogus freespace amount");
1650 avail /= (BLCKSZ / HISTOGRAM_BINS);
1653 /* Find the breakpoint bin */
1655 for (i = HISTOGRAM_BINS - 1; i >= 0; i--)
1657 int sum = above + histogram[i];
1664 thresholdL = i * BLCKSZ / HISTOGRAM_BINS; /* low bound of bp bin */
1665 thresholdU = (i + 1) * BLCKSZ / HISTOGRAM_BINS; /* hi bound */
1666 binct = newPages - above; /* number to take from bp bin */
1667 /* And copy the appropriate data */
1668 for (i = 0; i < nPages; i++)
1670 BlockNumber page = pageSpaces[i].blkno;
1671 Size avail = pageSpaces[i].avail;
1673 /* Check caller provides sorted data */
1674 if (i > 0 && page <= pageSpaces[i - 1].blkno)
1675 elog(ERROR, "free-space data is not in page order");
1676 /* Save this page? */
1677 if (avail >= thresholdU ||
1678 (avail >= thresholdL && (--binct >= 0)))
1680 FSMPageSetPageNum(newLocation, page);
1681 FSMPageSetSpace(newLocation, avail);
1686 Assert(newPages == 0);
1690 * Pack a set of per-page freespace data into a smaller amount of space.
1692 * This is algorithmically identical to pack_incoming_pages(), but accepts
1693 * a different input representation. Also, we assume the input data has
1694 * previously been checked for validity (size in bounds, pages in order).
1696 * Note: it is possible for the source and destination arrays to overlap.
1697 * The caller is responsible for making sure newLocation is at lower addresses
1698 * so that we can copy data moving forward in the arrays without problem.
1701 pack_existing_pages(FSMPageData *newLocation, int newPages,
1702 FSMPageData *oldLocation, int oldPages)
1704 int histogram[HISTOGRAM_BINS];
1711 Assert(newPages < oldPages); /* else I shouldn't have been called */
1712 /* Build histogram */
1713 MemSet(histogram, 0, sizeof(histogram));
1714 for (i = 0; i < oldPages; i++)
1716 Size avail = FSMPageGetSpace(oldLocation + i);
1718 /* Shouldn't happen, but test to protect against stack clobber */
1719 if (avail >= BLCKSZ)
1720 elog(ERROR, "bogus freespace amount");
1721 avail /= (BLCKSZ / HISTOGRAM_BINS);
1724 /* Find the breakpoint bin */
1726 for (i = HISTOGRAM_BINS - 1; i >= 0; i--)
1728 int sum = above + histogram[i];
1735 thresholdL = i * BLCKSZ / HISTOGRAM_BINS; /* low bound of bp bin */
1736 thresholdU = (i + 1) * BLCKSZ / HISTOGRAM_BINS; /* hi bound */
1737 binct = newPages - above; /* number to take from bp bin */
1738 /* And copy the appropriate data */
1739 for (i = 0; i < oldPages; i++)
1741 BlockNumber page = FSMPageGetPageNum(oldLocation + i);
1742 Size avail = FSMPageGetSpace(oldLocation + i);
1744 /* Save this page? */
1745 if (avail >= thresholdU ||
1746 (avail >= thresholdL && (--binct >= 0)))
1748 FSMPageSetPageNum(newLocation, page);
1749 FSMPageSetSpace(newLocation, avail);
1754 Assert(newPages == 0);
1758 * Calculate number of chunks "requested" by a rel.
1760 * Rel's lastPageCount and isIndex settings must be up-to-date when called.
1762 * See notes at top of file for details.
1765 fsm_calc_request(FSMRelation *fsmrel)
1769 /* Convert page count to chunk count */
1770 if (fsmrel->isIndex)
1771 chunkCount = (fsmrel->lastPageCount - 1) / INDEXCHUNKPAGES + 1;
1773 chunkCount = (fsmrel->lastPageCount - 1) / CHUNKPAGES + 1;
1774 /* "Request" is anything beyond our one guaranteed chunk */
1775 if (chunkCount <= 0)
1778 return chunkCount - 1;
1782 * Calculate target allocation (number of chunks) for a rel
1784 * Parameter is the result from fsm_calc_request(). The global sumRequests
1785 * and numRels totals must be up-to-date already.
1787 * See notes at top of file for details.
1790 fsm_calc_target_allocation(int myRequest)
1795 spareChunks = FreeSpaceMap->totalChunks - FreeSpaceMap->numRels;
1796 Assert(spareChunks > 0);
1797 if (spareChunks >= FreeSpaceMap->sumRequests)
1799 /* We aren't oversubscribed, so allocate exactly the request */
1804 extra = (int) rint(spareChunks * myRequest / FreeSpaceMap->sumRequests);
1805 if (extra < 0) /* shouldn't happen, but make sure */
1812 * Calculate number of chunks actually used to store current data
1815 fsm_current_chunks(FSMRelation *fsmrel)
1819 /* Make sure storedPages==0 produces right answer */
1820 if (fsmrel->storedPages <= 0)
1822 /* Convert page count to chunk count */
1823 if (fsmrel->isIndex)
1824 chunkCount = (fsmrel->storedPages - 1) / INDEXCHUNKPAGES + 1;
1826 chunkCount = (fsmrel->storedPages - 1) / CHUNKPAGES + 1;
1831 * Calculate current actual allocation (number of chunks) for a rel
1834 fsm_current_allocation(FSMRelation *fsmrel)
1836 if (fsmrel->nextPhysical != NULL)
1837 return fsmrel->nextPhysical->firstChunk - fsmrel->firstChunk;
1838 else if (fsmrel == FreeSpaceMap->lastRel)
1839 return FreeSpaceMap->usedChunks - fsmrel->firstChunk;
1842 /* it's not in the storage-order list */
1843 Assert(fsmrel->firstChunk < 0 && fsmrel->storedPages == 0);
1849 #ifdef FREESPACE_DEBUG
1851 * Dump contents of freespace map for debugging.
1853 * We assume caller holds the FreeSpaceLock, or is otherwise unconcerned
1854 * about other processes.
1859 FSMRelation *fsmrel;
1860 FSMRelation *prevrel = NULL;
1864 for (fsmrel = FreeSpaceMap->usageList; fsmrel; fsmrel = fsmrel->nextUsage)
1867 fprintf(stderr, "Map %d: rel %u/%u/%u isIndex %d avgRequest %u lastPageCount %d nextPage %d\nMap= ",
1869 fsmrel->key.spcNode, fsmrel->key.dbNode, fsmrel->key.relNode,
1870 (int) fsmrel->isIndex, fsmrel->avgRequest,
1871 fsmrel->lastPageCount, fsmrel->nextPage);
1872 if (fsmrel->isIndex)
1874 IndexFSMPageData *page;
1876 page = (IndexFSMPageData *)
1877 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1878 for (nPages = 0; nPages < fsmrel->storedPages; nPages++)
1880 fprintf(stderr, " %u",
1881 IndexFSMPageGetPageNum(page));
1889 page = (FSMPageData *)
1890 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1891 for (nPages = 0; nPages < fsmrel->storedPages; nPages++)
1893 fprintf(stderr, " %u:%u",
1894 FSMPageGetPageNum(page),
1895 FSMPageGetSpace(page));
1899 fprintf(stderr, "\n");
1900 /* Cross-check list links */
1901 if (prevrel != fsmrel->priorUsage)
1902 fprintf(stderr, "DumpFreeSpace: broken list links\n");
1905 if (prevrel != FreeSpaceMap->usageListTail)
1906 fprintf(stderr, "DumpFreeSpace: broken list links\n");
1907 /* Cross-check global counters */
1908 if (relNum != FreeSpaceMap->numRels)
1909 fprintf(stderr, "DumpFreeSpace: %d rels in list, but numRels = %d\n",
1910 relNum, FreeSpaceMap->numRels);
1913 #endif /* FREESPACE_DEBUG */