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.38 2005/03/12 05:21:52 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 FSMRelation *lookup_fsm_rel(RelFileNode *rel);
225 static FSMRelation *create_fsm_rel(RelFileNode *rel);
226 static void delete_fsm_rel(FSMRelation *fsmrel);
227 static int realloc_fsm_rel(FSMRelation *fsmrel, int nPages, bool isIndex);
228 static void link_fsm_rel_usage(FSMRelation *fsmrel);
229 static void unlink_fsm_rel_usage(FSMRelation *fsmrel);
230 static void link_fsm_rel_storage(FSMRelation *fsmrel);
231 static void unlink_fsm_rel_storage(FSMRelation *fsmrel);
232 static BlockNumber find_free_space(FSMRelation *fsmrel, Size spaceNeeded);
233 static BlockNumber find_index_free_space(FSMRelation *fsmrel);
234 static void fsm_record_free_space(FSMRelation *fsmrel, BlockNumber page,
236 static bool lookup_fsm_page_entry(FSMRelation *fsmrel, BlockNumber page,
238 static void compact_fsm_storage(void);
239 static void push_fsm_rels_after(FSMRelation *afterRel);
240 static void pack_incoming_pages(FSMPageData *newLocation, int newPages,
241 PageFreeSpaceInfo *pageSpaces, int nPages);
242 static void pack_existing_pages(FSMPageData *newLocation, int newPages,
243 FSMPageData *oldLocation, int oldPages);
244 static int fsm_calc_request(FSMRelation *fsmrel);
245 static int fsm_calc_target_allocation(int myRequest);
246 static int fsm_current_chunks(FSMRelation *fsmrel);
247 static int fsm_current_allocation(FSMRelation *fsmrel);
256 * InitFreeSpaceMap -- Initialize the freespace module.
258 * This must be called once during shared memory initialization.
259 * It builds the empty free space map table. FreeSpaceLock must also be
260 * initialized at some point, but is not touched here --- we assume there is
261 * no need for locking, since only the calling process can be accessing shared
265 InitFreeSpaceMap(void)
271 /* Create table header */
272 FreeSpaceMap = (FSMHeader *) ShmemInitStruct("Free Space Map Header", sizeof(FSMHeader), &found);
273 if (FreeSpaceMap == NULL)
275 (errcode(ERRCODE_OUT_OF_MEMORY),
276 errmsg("insufficient shared memory for free space map")));
278 MemSet(FreeSpaceMap, 0, sizeof(FSMHeader));
280 /* Create hashtable for FSMRelations */
281 info.keysize = sizeof(RelFileNode);
282 info.entrysize = sizeof(FSMRelation);
283 info.hash = tag_hash;
285 FreeSpaceMapRelHash = ShmemInitHash("Free Space Map Hash",
289 (HASH_ELEM | HASH_FUNCTION));
291 if (!FreeSpaceMapRelHash)
293 (errcode(ERRCODE_OUT_OF_MEMORY),
294 errmsg("insufficient shared memory for free space map")));
300 /* Allocate page-storage arena */
301 nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
302 /* This check ensures spareChunks will be greater than zero */
303 if (nchunks <= MaxFSMRelations)
305 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
306 errmsg("max_fsm_pages must exceed max_fsm_relations * %d",
309 FreeSpaceMap->arena = (char *) ShmemAlloc(nchunks * CHUNKBYTES);
310 if (FreeSpaceMap->arena == NULL)
312 (errcode(ERRCODE_OUT_OF_MEMORY),
313 errmsg("insufficient shared memory for free space map")));
315 FreeSpaceMap->totalChunks = nchunks;
316 FreeSpaceMap->usedChunks = 0;
317 FreeSpaceMap->sumRequests = 0;
321 * Estimate amount of shmem space needed for FSM.
324 FreeSpaceShmemSize(void)
330 size = MAXALIGN(sizeof(FSMHeader));
332 /* hash table, including the FSMRelation objects */
333 size += hash_estimate_size(MaxFSMRelations + 1, sizeof(FSMRelation));
335 /* page-storage arena */
336 nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
338 if (nchunks >= (INT_MAX / CHUNKBYTES))
340 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
341 errmsg("max_fsm_pages is too large")));
343 size += MAXALIGN(nchunks * CHUNKBYTES);
349 * GetPageWithFreeSpace - try to find a page in the given relation with
350 * at least the specified amount of free space.
352 * If successful, return the block number; if not, return InvalidBlockNumber.
354 * The caller must be prepared for the possibility that the returned page
355 * will turn out to have too little space available by the time the caller
356 * gets a lock on it. In that case, the caller should report the actual
357 * amount of free space available on that page and then try again (see
358 * RecordAndGetPageWithFreeSpace). If InvalidBlockNumber is returned,
359 * extend the relation.
362 GetPageWithFreeSpace(RelFileNode *rel, Size spaceNeeded)
365 BlockNumber freepage;
367 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
370 * We always add a rel to the hashtable when it is inquired about.
372 fsmrel = create_fsm_rel(rel);
375 * Update the moving average of space requests. This code implements
376 * an exponential moving average with an equivalent period of about 63
377 * requests. Ignore silly requests, however, to ensure that the
378 * average stays sane.
380 if (spaceNeeded > 0 && spaceNeeded < BLCKSZ)
382 int cur_avg = (int) fsmrel->avgRequest;
384 cur_avg += ((int) spaceNeeded - cur_avg) / 32;
385 fsmrel->avgRequest = (Size) cur_avg;
387 freepage = find_free_space(fsmrel, spaceNeeded);
388 LWLockRelease(FreeSpaceLock);
393 * RecordAndGetPageWithFreeSpace - update info about a page and try again.
395 * We provide this combo form, instead of a separate Record operation,
396 * to save one lock and hash table lookup cycle.
399 RecordAndGetPageWithFreeSpace(RelFileNode *rel,
405 BlockNumber freepage;
407 /* Sanity check: ensure spaceAvail will fit into OffsetNumber */
408 AssertArg(oldSpaceAvail < BLCKSZ);
410 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
413 * We always add a rel to the hashtable when it is inquired about.
415 fsmrel = create_fsm_rel(rel);
418 fsm_record_free_space(fsmrel, oldPage, oldSpaceAvail);
421 * Update the moving average of space requests, same as in
422 * GetPageWithFreeSpace.
424 if (spaceNeeded > 0 && spaceNeeded < BLCKSZ)
426 int cur_avg = (int) fsmrel->avgRequest;
428 cur_avg += ((int) spaceNeeded - cur_avg) / 32;
429 fsmrel->avgRequest = (Size) cur_avg;
432 freepage = find_free_space(fsmrel, spaceNeeded);
433 LWLockRelease(FreeSpaceLock);
438 * GetAvgFSMRequestSize - get average FSM request size for a relation.
440 * If the relation is not known to FSM, return a default value.
443 GetAvgFSMRequestSize(RelFileNode *rel)
448 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
449 fsmrel = lookup_fsm_rel(rel);
451 result = fsmrel->avgRequest;
453 result = INITIAL_AVERAGE;
454 LWLockRelease(FreeSpaceLock);
459 * RecordRelationFreeSpace - record available-space info about a relation.
461 * Any pre-existing info about the relation is assumed obsolete and discarded.
463 * The given pageSpaces[] array must be sorted in order by blkno. Note that
464 * the FSM is at liberty to discard some or all of the data.
467 RecordRelationFreeSpace(RelFileNode *rel,
469 PageFreeSpaceInfo *pageSpaces)
473 /* Limit nPages to something sane */
476 else if (nPages > MaxFSMPages)
477 nPages = MaxFSMPages;
479 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
482 * Note we don't record info about a relation unless there's already
483 * an FSM entry for it, implying someone has done GetPageWithFreeSpace
484 * for it. Inactive rels thus will not clutter the map simply by
487 fsmrel = lookup_fsm_rel(rel);
492 FSMPageData *newLocation;
494 curAlloc = realloc_fsm_rel(fsmrel, nPages, false);
495 curAllocPages = curAlloc * CHUNKPAGES;
498 * If the data fits in our current allocation, just copy it;
499 * otherwise must compress.
501 newLocation = (FSMPageData *)
502 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
503 if (nPages <= curAllocPages)
507 for (i = 0; i < nPages; i++)
509 BlockNumber page = pageSpaces[i].blkno;
510 Size avail = pageSpaces[i].avail;
512 /* Check caller provides sorted data */
513 if (i > 0 && page <= pageSpaces[i - 1].blkno)
514 elog(ERROR, "free-space data is not in page order");
515 FSMPageSetPageNum(newLocation, page);
516 FSMPageSetSpace(newLocation, avail);
519 fsmrel->storedPages = nPages;
523 pack_incoming_pages(newLocation, curAllocPages,
525 fsmrel->storedPages = curAllocPages;
528 LWLockRelease(FreeSpaceLock);
532 * GetFreeIndexPage - like GetPageWithFreeSpace, but for indexes
535 GetFreeIndexPage(RelFileNode *rel)
538 BlockNumber freepage;
540 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
543 * We always add a rel to the hashtable when it is inquired about.
545 fsmrel = create_fsm_rel(rel);
547 freepage = find_index_free_space(fsmrel);
548 LWLockRelease(FreeSpaceLock);
553 * RecordIndexFreeSpace - like RecordRelationFreeSpace, but for indexes
556 RecordIndexFreeSpace(RelFileNode *rel,
562 /* Limit nPages to something sane */
565 else if (nPages > MaxFSMPages)
566 nPages = MaxFSMPages;
568 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
571 * Note we don't record info about a relation unless there's already
572 * an FSM entry for it, implying someone has done GetFreeIndexPage for
573 * it. Inactive rels thus will not clutter the map simply by being
576 fsmrel = lookup_fsm_rel(rel);
582 IndexFSMPageData *newLocation;
584 curAlloc = realloc_fsm_rel(fsmrel, nPages, true);
585 curAllocPages = curAlloc * INDEXCHUNKPAGES;
588 * If the data fits in our current allocation, just copy it;
589 * otherwise must compress. But compression is easy: we merely
590 * forget extra pages.
592 newLocation = (IndexFSMPageData *)
593 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
594 if (nPages > curAllocPages)
595 nPages = curAllocPages;
597 for (i = 0; i < nPages; i++)
599 BlockNumber page = pages[i];
601 /* Check caller provides sorted data */
602 if (i > 0 && page <= pages[i - 1])
603 elog(ERROR, "free-space data is not in page order");
604 IndexFSMPageSetPageNum(newLocation, page);
607 fsmrel->storedPages = nPages;
609 LWLockRelease(FreeSpaceLock);
613 * FreeSpaceMapTruncateRel - adjust for truncation of a relation.
615 * We need to delete any stored data past the new relation length, so that
616 * we don't bogusly return removed block numbers.
619 FreeSpaceMapTruncateRel(RelFileNode *rel, BlockNumber nblocks)
623 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
624 fsmrel = lookup_fsm_rel(rel);
629 /* Use lookup to locate first entry >= nblocks */
630 (void) lookup_fsm_page_entry(fsmrel, nblocks, &pageIndex);
631 /* Delete all such entries */
632 fsmrel->storedPages = pageIndex;
633 /* XXX should we adjust rel's lastPageCount and sumRequests? */
635 LWLockRelease(FreeSpaceLock);
639 * FreeSpaceMapForgetRel - forget all about a relation.
641 * This is called when a relation is deleted. Although we could just let
642 * the rel age out of the map, it's better to reclaim and reuse the space
646 FreeSpaceMapForgetRel(RelFileNode *rel)
650 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
651 fsmrel = lookup_fsm_rel(rel);
653 delete_fsm_rel(fsmrel);
654 LWLockRelease(FreeSpaceLock);
658 * FreeSpaceMapForgetDatabase - forget all relations of a database.
660 * This is called during DROP DATABASE. As above, might as well reclaim
661 * map space sooner instead of later.
664 FreeSpaceMapForgetDatabase(Oid dbid)
669 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
670 for (fsmrel = FreeSpaceMap->usageList; fsmrel; fsmrel = nextrel)
672 nextrel = fsmrel->nextUsage; /* in case we delete it */
673 if (fsmrel->key.dbNode == dbid)
674 delete_fsm_rel(fsmrel);
676 LWLockRelease(FreeSpaceLock);
680 * PrintFreeSpaceMapStatistics - print statistics about FSM contents
682 * The info is sent to ereport() with the specified message level. This is
683 * intended for use during VACUUM.
686 PrintFreeSpaceMapStatistics(int elevel)
694 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
695 /* Count total space used --- tedious, but seems useful */
696 for (fsmrel = FreeSpaceMap->firstRel;
698 fsmrel = fsmrel->nextPhysical)
699 storedPages += fsmrel->storedPages;
700 /* Copy other stats before dropping lock */
701 numRels = FreeSpaceMap->numRels;
702 sumRequests = FreeSpaceMap->sumRequests;
703 LWLockRelease(FreeSpaceLock);
705 /* Convert stats to actual number of page slots needed */
706 needed = (sumRequests + numRels) * CHUNKPAGES;
709 (errmsg("free space map: %d relations, %d pages stored; %.0f total pages used",
710 numRels, storedPages, needed),
711 errdetail("FSM size: %d relations + %d pages = %.0f kB shared memory.",
712 MaxFSMRelations, MaxFSMPages,
713 (double) FreeSpaceShmemSize() / 1024.0)));
715 if (numRels == MaxFSMRelations)
717 (errmsg("max_fsm_relations(%d) equals the number of relations checked",
719 errhint("You have >= %d relations.\n"
720 "Consider increasing the configuration parameter \"max_fsm_relations\".",
722 else if (needed > MaxFSMPages)
724 (errmsg("the number of page slots needed (%.0f) exceeds max_fsm_pages (%d)",
726 errhint("Consider increasing the configuration parameter \"max_fsm_relations\"\n"
727 "to a value over %.0f.", needed)));
731 * DumpFreeSpaceMap - dump contents of FSM into a disk file for later reload
733 * This is expected to be called during database shutdown, after updates to
734 * the FSM have stopped. We lock the FreeSpaceLock but that's purely pro
735 * forma --- if anyone else is still accessing FSM, there's a problem.
738 DumpFreeSpaceMap(int code, Datum arg)
741 char cachefilename[MAXPGPATH];
742 FsmCacheFileHeader header;
745 /* Try to create file */
746 snprintf(cachefilename, sizeof(cachefilename), "%s/%s",
747 DataDir, FSM_CACHE_FILENAME);
749 unlink(cachefilename); /* in case it exists w/wrong permissions */
751 fp = AllocateFile(cachefilename, PG_BINARY_W);
754 elog(LOG, "could not write \"%s\": %m", cachefilename);
758 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
760 /* Write file header */
761 MemSet(&header, 0, sizeof(header));
762 strcpy(header.label, FSM_CACHE_LABEL);
763 header.endian = FSM_CACHE_ENDIAN;
764 header.version = FSM_CACHE_VERSION;
765 header.numRels = FreeSpaceMap->numRels;
766 if (fwrite(&header, 1, sizeof(header), fp) != sizeof(header))
769 /* For each relation, in order from least to most recently used... */
770 for (fsmrel = FreeSpaceMap->usageListTail;
772 fsmrel = fsmrel->priorUsage)
774 FsmCacheRelHeader relheader;
777 /* Write relation header */
778 MemSet(&relheader, 0, sizeof(relheader));
779 relheader.key = fsmrel->key;
780 relheader.isIndex = fsmrel->isIndex;
781 relheader.avgRequest = fsmrel->avgRequest;
782 relheader.lastPageCount = fsmrel->lastPageCount;
783 relheader.storedPages = fsmrel->storedPages;
784 if (fwrite(&relheader, 1, sizeof(relheader), fp) != sizeof(relheader))
787 /* Write the per-page data directly from the arena */
788 nPages = fsmrel->storedPages;
795 len = nPages * sizeof(IndexFSMPageData);
797 len = nPages * sizeof(FSMPageData);
799 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
800 if (fwrite(data, 1, len, fp) != len)
806 LWLockRelease(FreeSpaceLock);
810 elog(LOG, "could not write \"%s\": %m", cachefilename);
811 /* Remove busted cache file */
812 unlink(cachefilename);
818 elog(LOG, "could not write \"%s\": %m", cachefilename);
821 LWLockRelease(FreeSpaceLock);
825 /* Remove busted cache file */
826 unlink(cachefilename);
830 * LoadFreeSpaceMap - load contents of FSM from a disk file
832 * This is expected to be called during database startup, before any FSM
833 * updates begin. We lock the FreeSpaceLock but that's purely pro
834 * forma --- if anyone else is accessing FSM yet, there's a problem.
836 * Notes: no complaint is issued if no cache file is found. If the file is
837 * found, it is deleted after reading. Thus, if we crash without a clean
838 * shutdown, the next cycle of life starts with no FSM data. To do otherwise,
839 * we'd need to do significantly more validation in this routine, because of
840 * the likelihood that what is in the dump file would be out-of-date, eg
841 * there might be entries for deleted or truncated rels.
844 LoadFreeSpaceMap(void)
847 char cachefilename[MAXPGPATH];
848 FsmCacheFileHeader header;
851 /* Try to open file */
852 snprintf(cachefilename, sizeof(cachefilename), "%s/%s",
853 DataDir, FSM_CACHE_FILENAME);
855 fp = AllocateFile(cachefilename, PG_BINARY_R);
859 elog(LOG, "could not read \"%s\": %m", cachefilename);
863 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
865 /* Read and verify file header */
866 if (fread(&header, 1, sizeof(header), fp) != sizeof(header) ||
867 strcmp(header.label, FSM_CACHE_LABEL) != 0 ||
868 header.endian != FSM_CACHE_ENDIAN ||
869 header.version != FSM_CACHE_VERSION ||
872 elog(LOG, "bogus file header in \"%s\"", cachefilename);
876 /* For each relation, in order from least to most recently used... */
877 for (relno = 0; relno < header.numRels; relno++)
879 FsmCacheRelHeader relheader;
887 /* Read and verify relation header, as best we can */
888 if (fread(&relheader, 1, sizeof(relheader), fp) != sizeof(relheader) ||
889 (relheader.isIndex != false && relheader.isIndex != true) ||
890 relheader.avgRequest >= BLCKSZ ||
891 relheader.lastPageCount < 0 ||
892 relheader.storedPages < 0)
894 elog(LOG, "bogus rel header in \"%s\"", cachefilename);
898 /* Make sure lastPageCount doesn't exceed current MaxFSMPages */
899 if (relheader.lastPageCount > MaxFSMPages)
900 relheader.lastPageCount = MaxFSMPages;
902 /* Read the per-page data */
903 nPages = relheader.storedPages;
904 if (relheader.isIndex)
905 len = nPages * sizeof(IndexFSMPageData);
907 len = nPages * sizeof(FSMPageData);
908 data = (char *) palloc(len);
909 if (fread(data, 1, len, fp) != len)
911 elog(LOG, "premature EOF in \"%s\"", cachefilename);
917 * Okay, create the FSM entry and insert data into it. Since the
918 * rels were stored in reverse usage order, at the end of the loop
919 * they will be correctly usage-ordered in memory; and if
920 * MaxFSMRelations is less than it used to be, we will correctly
921 * drop the least recently used ones.
923 fsmrel = create_fsm_rel(&relheader.key);
924 fsmrel->avgRequest = relheader.avgRequest;
926 curAlloc = realloc_fsm_rel(fsmrel, relheader.lastPageCount,
928 if (relheader.isIndex)
930 IndexFSMPageData *newLocation;
932 curAllocPages = curAlloc * INDEXCHUNKPAGES;
935 * If the data fits in our current allocation, just copy it;
936 * otherwise must compress. But compression is easy: we
937 * merely forget extra pages.
939 newLocation = (IndexFSMPageData *)
940 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
941 if (nPages > curAllocPages)
942 nPages = curAllocPages;
943 memcpy(newLocation, data, nPages * sizeof(IndexFSMPageData));
944 fsmrel->storedPages = nPages;
948 FSMPageData *newLocation;
950 curAllocPages = curAlloc * CHUNKPAGES;
953 * If the data fits in our current allocation, just copy it;
954 * otherwise must compress.
956 newLocation = (FSMPageData *)
957 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
958 if (nPages <= curAllocPages)
960 memcpy(newLocation, data, nPages * sizeof(FSMPageData));
961 fsmrel->storedPages = nPages;
965 pack_existing_pages(newLocation, curAllocPages,
966 (FSMPageData *) data, nPages);
967 fsmrel->storedPages = curAllocPages;
977 LWLockRelease(FreeSpaceLock);
981 /* Remove cache file before it can become stale; see notes above */
982 unlink(cachefilename);
987 * Internal routines. These all assume the caller holds the FreeSpaceLock.
991 * Lookup a relation in the hash table. If not present, return NULL.
993 * The relation's position in the LRU list is not changed.
996 lookup_fsm_rel(RelFileNode *rel)
1000 fsmrel = (FSMRelation *) hash_search(FreeSpaceMapRelHash,
1011 * Lookup a relation in the hash table, creating an entry if not present.
1013 * On successful lookup, the relation is moved to the front of the LRU list.
1015 static FSMRelation *
1016 create_fsm_rel(RelFileNode *rel)
1018 FSMRelation *fsmrel;
1021 fsmrel = (FSMRelation *) hash_search(FreeSpaceMapRelHash,
1027 (errcode(ERRCODE_OUT_OF_MEMORY),
1028 errmsg("out of shared memory")));
1032 /* New hashtable entry, initialize it (hash_search set the key) */
1033 fsmrel->isIndex = false; /* until we learn different */
1034 fsmrel->avgRequest = INITIAL_AVERAGE;
1035 fsmrel->lastPageCount = 0;
1036 fsmrel->firstChunk = -1; /* no space allocated */
1037 fsmrel->storedPages = 0;
1038 fsmrel->nextPage = 0;
1040 /* Discard lowest-priority existing rel, if we are over limit */
1041 if (FreeSpaceMap->numRels >= MaxFSMRelations)
1042 delete_fsm_rel(FreeSpaceMap->usageListTail);
1044 /* Add new entry at front of LRU list */
1045 link_fsm_rel_usage(fsmrel);
1046 fsmrel->nextPhysical = NULL; /* not in physical-storage list */
1047 fsmrel->priorPhysical = NULL;
1048 FreeSpaceMap->numRels++;
1049 /* sumRequests is unchanged because request must be zero */
1053 /* Existing entry, move to front of LRU list */
1054 if (fsmrel->priorUsage != NULL)
1056 unlink_fsm_rel_usage(fsmrel);
1057 link_fsm_rel_usage(fsmrel);
1065 * Remove an existing FSMRelation entry.
1068 delete_fsm_rel(FSMRelation *fsmrel)
1070 FSMRelation *result;
1072 FreeSpaceMap->sumRequests -= fsm_calc_request(fsmrel);
1073 unlink_fsm_rel_usage(fsmrel);
1074 unlink_fsm_rel_storage(fsmrel);
1075 FreeSpaceMap->numRels--;
1076 result = (FSMRelation *) hash_search(FreeSpaceMapRelHash,
1077 (void *) &(fsmrel->key),
1081 elog(ERROR, "FreeSpaceMap hashtable corrupted");
1085 * Reallocate space for a FSMRelation.
1087 * This is shared code for RecordRelationFreeSpace and RecordIndexFreeSpace.
1088 * The return value is the actual new allocation, in chunks.
1091 realloc_fsm_rel(FSMRelation *fsmrel, int nPages, bool isIndex)
1098 * Delete any existing entries, and update request status.
1100 fsmrel->storedPages = 0;
1101 FreeSpaceMap->sumRequests -= fsm_calc_request(fsmrel);
1102 fsmrel->lastPageCount = nPages;
1103 fsmrel->isIndex = isIndex;
1104 myRequest = fsm_calc_request(fsmrel);
1105 FreeSpaceMap->sumRequests += myRequest;
1106 myAlloc = fsm_calc_target_allocation(myRequest);
1109 * Need to reallocate space if (a) my target allocation is more than
1110 * my current allocation, AND (b) my actual immediate need
1111 * (myRequest+1 chunks) is more than my current allocation. Otherwise
1112 * just store the new data in-place.
1114 curAlloc = fsm_current_allocation(fsmrel);
1115 if (myAlloc > curAlloc && (myRequest + 1) > curAlloc && nPages > 0)
1117 /* Remove entry from storage list, and compact */
1118 unlink_fsm_rel_storage(fsmrel);
1119 compact_fsm_storage();
1120 /* Reattach to end of storage list */
1121 link_fsm_rel_storage(fsmrel);
1122 /* And allocate storage */
1123 fsmrel->firstChunk = FreeSpaceMap->usedChunks;
1124 FreeSpaceMap->usedChunks += myAlloc;
1126 /* Watch out for roundoff error */
1127 if (FreeSpaceMap->usedChunks > FreeSpaceMap->totalChunks)
1129 FreeSpaceMap->usedChunks = FreeSpaceMap->totalChunks;
1130 curAlloc = FreeSpaceMap->totalChunks - fsmrel->firstChunk;
1137 * Link a FSMRelation into the LRU list (always at the head).
1140 link_fsm_rel_usage(FSMRelation *fsmrel)
1142 fsmrel->priorUsage = NULL;
1143 fsmrel->nextUsage = FreeSpaceMap->usageList;
1144 FreeSpaceMap->usageList = fsmrel;
1145 if (fsmrel->nextUsage != NULL)
1146 fsmrel->nextUsage->priorUsage = fsmrel;
1148 FreeSpaceMap->usageListTail = fsmrel;
1152 * Delink a FSMRelation from the LRU list.
1155 unlink_fsm_rel_usage(FSMRelation *fsmrel)
1157 if (fsmrel->priorUsage != NULL)
1158 fsmrel->priorUsage->nextUsage = fsmrel->nextUsage;
1160 FreeSpaceMap->usageList = fsmrel->nextUsage;
1161 if (fsmrel->nextUsage != NULL)
1162 fsmrel->nextUsage->priorUsage = fsmrel->priorUsage;
1164 FreeSpaceMap->usageListTail = fsmrel->priorUsage;
1167 * We don't bother resetting fsmrel's links, since it's about to be
1168 * deleted or relinked at the head.
1173 * Link a FSMRelation into the storage-order list (always at the tail).
1176 link_fsm_rel_storage(FSMRelation *fsmrel)
1178 fsmrel->nextPhysical = NULL;
1179 fsmrel->priorPhysical = FreeSpaceMap->lastRel;
1180 if (FreeSpaceMap->lastRel != NULL)
1181 FreeSpaceMap->lastRel->nextPhysical = fsmrel;
1183 FreeSpaceMap->firstRel = fsmrel;
1184 FreeSpaceMap->lastRel = fsmrel;
1188 * Delink a FSMRelation from the storage-order list, if it's in it.
1191 unlink_fsm_rel_storage(FSMRelation *fsmrel)
1193 if (fsmrel->priorPhysical != NULL || FreeSpaceMap->firstRel == fsmrel)
1195 if (fsmrel->priorPhysical != NULL)
1196 fsmrel->priorPhysical->nextPhysical = fsmrel->nextPhysical;
1198 FreeSpaceMap->firstRel = fsmrel->nextPhysical;
1199 if (fsmrel->nextPhysical != NULL)
1200 fsmrel->nextPhysical->priorPhysical = fsmrel->priorPhysical;
1202 FreeSpaceMap->lastRel = fsmrel->priorPhysical;
1204 /* mark as not in list, since we may not put it back immediately */
1205 fsmrel->nextPhysical = NULL;
1206 fsmrel->priorPhysical = NULL;
1207 /* Also mark it as having no storage */
1208 fsmrel->firstChunk = -1;
1209 fsmrel->storedPages = 0;
1213 * Look to see if a page with at least the specified amount of space is
1214 * available in the given FSMRelation. If so, return its page number,
1215 * and advance the nextPage counter so that the next inquiry will return
1216 * a different page if possible; also update the entry to show that the
1217 * requested space is not available anymore. Return InvalidBlockNumber
1221 find_free_space(FSMRelation *fsmrel, Size spaceNeeded)
1224 int pagesToCheck, /* outer loop counter */
1225 pageIndex; /* current page index */
1227 if (fsmrel->isIndex)
1228 elog(ERROR, "find_free_space called for an index relation");
1229 info = (FSMPageData *)
1230 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1231 pageIndex = fsmrel->nextPage;
1232 /* Last operation may have left nextPage pointing past end */
1233 if (pageIndex >= fsmrel->storedPages)
1236 for (pagesToCheck = fsmrel->storedPages; pagesToCheck > 0; pagesToCheck--)
1238 FSMPageData *page = info + pageIndex;
1239 Size spaceAvail = FSMPageGetSpace(page);
1241 /* Check this page */
1242 if (spaceAvail >= spaceNeeded)
1245 * Found what we want --- adjust the entry, and update
1248 FSMPageSetSpace(page, spaceAvail - spaceNeeded);
1249 fsmrel->nextPage = pageIndex + 1;
1250 return FSMPageGetPageNum(page);
1252 /* Advance pageIndex, wrapping around if needed */
1253 if (++pageIndex >= fsmrel->storedPages)
1257 return InvalidBlockNumber; /* nothing found */
1261 * As above, but for index case --- we only deal in whole pages.
1264 find_index_free_space(FSMRelation *fsmrel)
1266 IndexFSMPageData *info;
1270 * If isIndex isn't set, it could be that RecordIndexFreeSpace() has
1271 * never yet been called on this relation, and we're still looking at
1272 * the default setting from create_fsm_rel(). If so, just act as
1273 * though there's no space.
1275 if (!fsmrel->isIndex)
1277 if (fsmrel->storedPages == 0)
1278 return InvalidBlockNumber;
1279 elog(ERROR, "find_index_free_space called for a non-index relation");
1283 * For indexes, there's no need for the nextPage state variable; we
1284 * just remove and return the first available page. (We could save
1285 * cycles here by returning the last page, but it seems better to
1286 * encourage re-use of lower-numbered pages.)
1288 if (fsmrel->storedPages <= 0)
1289 return InvalidBlockNumber; /* no pages available */
1290 info = (IndexFSMPageData *)
1291 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1292 result = IndexFSMPageGetPageNum(info);
1293 fsmrel->storedPages--;
1294 memmove(info, info + 1, fsmrel->storedPages * sizeof(IndexFSMPageData));
1299 * fsm_record_free_space - guts of RecordFreeSpace operation (now only
1300 * provided as part of RecordAndGetPageWithFreeSpace).
1303 fsm_record_free_space(FSMRelation *fsmrel, BlockNumber page, Size spaceAvail)
1307 if (fsmrel->isIndex)
1308 elog(ERROR, "fsm_record_free_space called for an index relation");
1309 if (lookup_fsm_page_entry(fsmrel, page, &pageIndex))
1311 /* Found an existing entry for page; update it */
1314 info = (FSMPageData *)
1315 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1317 FSMPageSetSpace(info, spaceAvail);
1322 * No existing entry; ignore the call. We used to add the page to
1323 * the FSM --- but in practice, if the page hasn't got enough
1324 * space to satisfy the caller who's kicking it back to us, then
1325 * it's probably uninteresting to everyone else as well.
1331 * Look for an entry for a specific page (block number) in a FSMRelation.
1332 * Returns TRUE if a matching entry exists, else FALSE.
1334 * The output argument *outPageIndex is set to indicate where the entry exists
1335 * (if TRUE result) or could be inserted (if FALSE result).
1338 lookup_fsm_page_entry(FSMRelation *fsmrel, BlockNumber page,
1341 /* Check for empty relation */
1342 if (fsmrel->storedPages <= 0)
1348 /* Do binary search */
1349 if (fsmrel->isIndex)
1351 IndexFSMPageData *info;
1355 info = (IndexFSMPageData *)
1356 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1358 high = fsmrel->storedPages - 1;
1364 middle = low + (high - low) / 2;
1365 probe = IndexFSMPageGetPageNum(info + middle);
1368 *outPageIndex = middle;
1371 else if (probe < page)
1376 *outPageIndex = low;
1385 info = (FSMPageData *)
1386 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1388 high = fsmrel->storedPages - 1;
1394 middle = low + (high - low) / 2;
1395 probe = FSMPageGetPageNum(info + middle);
1398 *outPageIndex = middle;
1401 else if (probe < page)
1406 *outPageIndex = low;
1412 * Re-pack the FSM storage arena, dropping data if necessary to meet the
1413 * current allocation target for each relation. At conclusion, all available
1414 * space in the arena will be coalesced at the end.
1417 compact_fsm_storage(void)
1419 int nextChunkIndex = 0;
1420 bool did_push = false;
1421 FSMRelation *fsmrel;
1423 for (fsmrel = FreeSpaceMap->firstRel;
1425 fsmrel = fsmrel->nextPhysical)
1436 * Calculate target allocation, make sure we don't overrun due to
1439 newAlloc = fsm_calc_target_allocation(fsm_calc_request(fsmrel));
1440 if (newAlloc > FreeSpaceMap->totalChunks - nextChunkIndex)
1441 newAlloc = FreeSpaceMap->totalChunks - nextChunkIndex;
1442 if (fsmrel->isIndex)
1443 newAllocPages = newAlloc * INDEXCHUNKPAGES;
1445 newAllocPages = newAlloc * CHUNKPAGES;
1448 * Determine current size, current and new locations
1450 curChunks = fsm_current_chunks(fsmrel);
1451 oldChunkIndex = fsmrel->firstChunk;
1452 oldLocation = FreeSpaceMap->arena + oldChunkIndex * CHUNKBYTES;
1453 newChunkIndex = nextChunkIndex;
1454 newLocation = FreeSpaceMap->arena + newChunkIndex * CHUNKBYTES;
1457 * It's possible that we have to move data down, not up, if the
1458 * allocations of previous rels expanded. This normally means
1459 * that our allocation expanded too (or at least got no worse),
1460 * and ditto for later rels. So there should be room to move all
1461 * our data down without dropping any --- but we might have to
1462 * push down following rels to acquire the room. We don't want to
1463 * do the push more than once, so pack everything against the end
1464 * of the arena if so.
1466 * In corner cases where we are on the short end of a roundoff choice
1467 * that we were formerly on the long end of, it's possible that we
1468 * have to move down and compress our data too. In fact, even
1469 * after pushing down the following rels, there might not be as
1470 * much space as we computed for this rel above --- that would
1471 * imply that some following rel(s) are also on the losing end of
1472 * roundoff choices. We could handle this fairly by doing the
1473 * per-rel compactions out-of-order, but that seems like way too
1474 * much complexity to deal with a very infrequent corner case.
1475 * Instead, we simply drop pages from the end of the current rel's
1476 * data until it fits.
1478 if (newChunkIndex > oldChunkIndex)
1480 int limitChunkIndex;
1482 if (newAllocPages < fsmrel->storedPages)
1484 /* move and compress --- just drop excess pages */
1485 fsmrel->storedPages = newAllocPages;
1486 curChunks = fsm_current_chunks(fsmrel);
1488 /* is there enough space? */
1489 if (fsmrel->nextPhysical != NULL)
1490 limitChunkIndex = fsmrel->nextPhysical->firstChunk;
1492 limitChunkIndex = FreeSpaceMap->totalChunks;
1493 if (newChunkIndex + curChunks > limitChunkIndex)
1495 /* not enough space, push down following rels */
1498 push_fsm_rels_after(fsmrel);
1501 /* now is there enough space? */
1502 if (fsmrel->nextPhysical != NULL)
1503 limitChunkIndex = fsmrel->nextPhysical->firstChunk;
1505 limitChunkIndex = FreeSpaceMap->totalChunks;
1506 if (newChunkIndex + curChunks > limitChunkIndex)
1508 /* uh-oh, forcibly cut the allocation to fit */
1509 newAlloc = limitChunkIndex - newChunkIndex;
1512 * If newAlloc < 0 at this point, we are moving the
1513 * rel's firstChunk into territory currently assigned
1514 * to a later rel. This is okay so long as we do not
1515 * copy any data. The rels will be back in
1516 * nondecreasing firstChunk order at completion of the
1521 if (fsmrel->isIndex)
1522 newAllocPages = newAlloc * INDEXCHUNKPAGES;
1524 newAllocPages = newAlloc * CHUNKPAGES;
1525 fsmrel->storedPages = newAllocPages;
1526 curChunks = fsm_current_chunks(fsmrel);
1529 memmove(newLocation, oldLocation, curChunks * CHUNKBYTES);
1531 else if (newAllocPages < fsmrel->storedPages)
1534 * Need to compress the page data. For an index,
1535 * "compression" just means dropping excess pages; otherwise
1536 * we try to keep the ones with the most space.
1538 if (fsmrel->isIndex)
1540 fsmrel->storedPages = newAllocPages;
1541 /* may need to move data */
1542 if (newChunkIndex != oldChunkIndex)
1543 memmove(newLocation, oldLocation, newAlloc * CHUNKBYTES);
1547 pack_existing_pages((FSMPageData *) newLocation,
1549 (FSMPageData *) oldLocation,
1550 fsmrel->storedPages);
1551 fsmrel->storedPages = newAllocPages;
1554 else if (newChunkIndex != oldChunkIndex)
1557 * No compression needed, but must copy the data up
1559 memmove(newLocation, oldLocation, curChunks * CHUNKBYTES);
1561 fsmrel->firstChunk = newChunkIndex;
1562 nextChunkIndex += newAlloc;
1564 Assert(nextChunkIndex <= FreeSpaceMap->totalChunks);
1565 FreeSpaceMap->usedChunks = nextChunkIndex;
1569 * Push all FSMRels physically after afterRel to the end of the storage arena.
1571 * We sometimes have to do this when deletion or truncation of a relation
1572 * causes the allocations of remaining rels to expand markedly. We must
1573 * temporarily push existing data down to the end so that we can move it
1574 * back up in an orderly fashion.
1577 push_fsm_rels_after(FSMRelation *afterRel)
1579 int nextChunkIndex = FreeSpaceMap->totalChunks;
1580 FSMRelation *fsmrel;
1582 FreeSpaceMap->usedChunks = FreeSpaceMap->totalChunks;
1584 for (fsmrel = FreeSpaceMap->lastRel;
1586 fsmrel = fsmrel->priorPhysical)
1594 if (fsmrel == afterRel)
1597 chunkCount = fsm_current_chunks(fsmrel);
1598 nextChunkIndex -= chunkCount;
1599 newChunkIndex = nextChunkIndex;
1600 oldChunkIndex = fsmrel->firstChunk;
1601 if (newChunkIndex < oldChunkIndex)
1603 /* we're pushing down, how can it move up? */
1604 elog(PANIC, "inconsistent entry sizes in FSM");
1606 else if (newChunkIndex > oldChunkIndex)
1608 /* need to move it */
1609 newLocation = FreeSpaceMap->arena + newChunkIndex * CHUNKBYTES;
1610 oldLocation = FreeSpaceMap->arena + oldChunkIndex * CHUNKBYTES;
1611 memmove(newLocation, oldLocation, chunkCount * CHUNKBYTES);
1612 fsmrel->firstChunk = newChunkIndex;
1615 Assert(nextChunkIndex >= 0);
1619 * Pack a set of per-page freespace data into a smaller amount of space.
1621 * The method is to compute a low-resolution histogram of the free space
1622 * amounts, then determine which histogram bin contains the break point.
1623 * We then keep all pages above that bin, none below it, and just enough
1624 * of the pages in that bin to fill the output area exactly.
1626 #define HISTOGRAM_BINS 64
1629 pack_incoming_pages(FSMPageData *newLocation, int newPages,
1630 PageFreeSpaceInfo *pageSpaces, int nPages)
1632 int histogram[HISTOGRAM_BINS];
1639 Assert(newPages < nPages); /* else I shouldn't have been called */
1640 /* Build histogram */
1641 MemSet(histogram, 0, sizeof(histogram));
1642 for (i = 0; i < nPages; i++)
1644 Size avail = pageSpaces[i].avail;
1646 if (avail >= BLCKSZ)
1647 elog(ERROR, "bogus freespace amount");
1648 avail /= (BLCKSZ / HISTOGRAM_BINS);
1651 /* Find the breakpoint bin */
1653 for (i = HISTOGRAM_BINS - 1; i >= 0; i--)
1655 int sum = above + histogram[i];
1662 thresholdL = i * BLCKSZ / HISTOGRAM_BINS; /* low bound of bp bin */
1663 thresholdU = (i + 1) * BLCKSZ / HISTOGRAM_BINS; /* hi bound */
1664 binct = newPages - above; /* number to take from bp bin */
1665 /* And copy the appropriate data */
1666 for (i = 0; i < nPages; i++)
1668 BlockNumber page = pageSpaces[i].blkno;
1669 Size avail = pageSpaces[i].avail;
1671 /* Check caller provides sorted data */
1672 if (i > 0 && page <= pageSpaces[i - 1].blkno)
1673 elog(ERROR, "free-space data is not in page order");
1674 /* Save this page? */
1675 if (avail >= thresholdU ||
1676 (avail >= thresholdL && (--binct >= 0)))
1678 FSMPageSetPageNum(newLocation, page);
1679 FSMPageSetSpace(newLocation, avail);
1684 Assert(newPages == 0);
1688 * Pack a set of per-page freespace data into a smaller amount of space.
1690 * This is algorithmically identical to pack_incoming_pages(), but accepts
1691 * a different input representation. Also, we assume the input data has
1692 * previously been checked for validity (size in bounds, pages in order).
1694 * Note: it is possible for the source and destination arrays to overlap.
1695 * The caller is responsible for making sure newLocation is at lower addresses
1696 * so that we can copy data moving forward in the arrays without problem.
1699 pack_existing_pages(FSMPageData *newLocation, int newPages,
1700 FSMPageData *oldLocation, int oldPages)
1702 int histogram[HISTOGRAM_BINS];
1709 Assert(newPages < oldPages); /* else I shouldn't have been called */
1710 /* Build histogram */
1711 MemSet(histogram, 0, sizeof(histogram));
1712 for (i = 0; i < oldPages; i++)
1714 Size avail = FSMPageGetSpace(oldLocation + i);
1716 /* Shouldn't happen, but test to protect against stack clobber */
1717 if (avail >= BLCKSZ)
1718 elog(ERROR, "bogus freespace amount");
1719 avail /= (BLCKSZ / HISTOGRAM_BINS);
1722 /* Find the breakpoint bin */
1724 for (i = HISTOGRAM_BINS - 1; i >= 0; i--)
1726 int sum = above + histogram[i];
1733 thresholdL = i * BLCKSZ / HISTOGRAM_BINS; /* low bound of bp bin */
1734 thresholdU = (i + 1) * BLCKSZ / HISTOGRAM_BINS; /* hi bound */
1735 binct = newPages - above; /* number to take from bp bin */
1736 /* And copy the appropriate data */
1737 for (i = 0; i < oldPages; i++)
1739 BlockNumber page = FSMPageGetPageNum(oldLocation + i);
1740 Size avail = FSMPageGetSpace(oldLocation + i);
1742 /* Save this page? */
1743 if (avail >= thresholdU ||
1744 (avail >= thresholdL && (--binct >= 0)))
1746 FSMPageSetPageNum(newLocation, page);
1747 FSMPageSetSpace(newLocation, avail);
1752 Assert(newPages == 0);
1756 * Calculate number of chunks "requested" by a rel.
1758 * Rel's lastPageCount and isIndex settings must be up-to-date when called.
1760 * See notes at top of file for details.
1763 fsm_calc_request(FSMRelation *fsmrel)
1767 /* Convert page count to chunk count */
1768 if (fsmrel->isIndex)
1769 chunkCount = (fsmrel->lastPageCount - 1) / INDEXCHUNKPAGES + 1;
1771 chunkCount = (fsmrel->lastPageCount - 1) / CHUNKPAGES + 1;
1772 /* "Request" is anything beyond our one guaranteed chunk */
1773 if (chunkCount <= 0)
1776 return chunkCount - 1;
1780 * Calculate target allocation (number of chunks) for a rel
1782 * Parameter is the result from fsm_calc_request(). The global sumRequests
1783 * and numRels totals must be up-to-date already.
1785 * See notes at top of file for details.
1788 fsm_calc_target_allocation(int myRequest)
1793 spareChunks = FreeSpaceMap->totalChunks - FreeSpaceMap->numRels;
1794 Assert(spareChunks > 0);
1795 if (spareChunks >= FreeSpaceMap->sumRequests)
1797 /* We aren't oversubscribed, so allocate exactly the request */
1802 extra = (int) rint(spareChunks * myRequest / FreeSpaceMap->sumRequests);
1803 if (extra < 0) /* shouldn't happen, but make sure */
1810 * Calculate number of chunks actually used to store current data
1813 fsm_current_chunks(FSMRelation *fsmrel)
1817 /* Make sure storedPages==0 produces right answer */
1818 if (fsmrel->storedPages <= 0)
1820 /* Convert page count to chunk count */
1821 if (fsmrel->isIndex)
1822 chunkCount = (fsmrel->storedPages - 1) / INDEXCHUNKPAGES + 1;
1824 chunkCount = (fsmrel->storedPages - 1) / CHUNKPAGES + 1;
1829 * Calculate current actual allocation (number of chunks) for a rel
1832 fsm_current_allocation(FSMRelation *fsmrel)
1834 if (fsmrel->nextPhysical != NULL)
1835 return fsmrel->nextPhysical->firstChunk - fsmrel->firstChunk;
1836 else if (fsmrel == FreeSpaceMap->lastRel)
1837 return FreeSpaceMap->usedChunks - fsmrel->firstChunk;
1840 /* it's not in the storage-order list */
1841 Assert(fsmrel->firstChunk < 0 && fsmrel->storedPages == 0);
1847 #ifdef FREESPACE_DEBUG
1849 * Dump contents of freespace map for debugging.
1851 * We assume caller holds the FreeSpaceLock, or is otherwise unconcerned
1852 * about other processes.
1857 FSMRelation *fsmrel;
1858 FSMRelation *prevrel = NULL;
1862 for (fsmrel = FreeSpaceMap->usageList; fsmrel; fsmrel = fsmrel->nextUsage)
1865 fprintf(stderr, "Map %d: rel %u/%u/%u isIndex %d avgRequest %u lastPageCount %d nextPage %d\nMap= ",
1867 fsmrel->key.spcNode, fsmrel->key.dbNode, fsmrel->key.relNode,
1868 (int) fsmrel->isIndex, fsmrel->avgRequest,
1869 fsmrel->lastPageCount, fsmrel->nextPage);
1870 if (fsmrel->isIndex)
1872 IndexFSMPageData *page;
1874 page = (IndexFSMPageData *)
1875 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1876 for (nPages = 0; nPages < fsmrel->storedPages; nPages++)
1878 fprintf(stderr, " %u",
1879 IndexFSMPageGetPageNum(page));
1887 page = (FSMPageData *)
1888 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1889 for (nPages = 0; nPages < fsmrel->storedPages; nPages++)
1891 fprintf(stderr, " %u:%u",
1892 FSMPageGetPageNum(page),
1893 FSMPageGetSpace(page));
1897 fprintf(stderr, "\n");
1898 /* Cross-check list links */
1899 if (prevrel != fsmrel->priorUsage)
1900 fprintf(stderr, "DumpFreeSpace: broken list links\n");
1903 if (prevrel != FreeSpaceMap->usageListTail)
1904 fprintf(stderr, "DumpFreeSpace: broken list links\n");
1905 /* Cross-check global counters */
1906 if (relNum != FreeSpaceMap->numRels)
1907 fprintf(stderr, "DumpFreeSpace: %d rels in list, but numRels = %d\n",
1908 relNum, FreeSpaceMap->numRels);
1911 #endif /* FREESPACE_DEBUG */