1 /*-------------------------------------------------------------------------
4 * POSTGRES free space map for quickly finding free space in relations
7 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/storage/freespace/freespace.c,v 1.24 2003/10/29 17:36:57 tgl 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 HTAB *relHash; /* hashtable of FSMRelation entries */
184 FSMRelation *usageList; /* FSMRelations in usage-recency order */
185 FSMRelation *usageListTail; /* tail of usage-recency list */
186 FSMRelation *firstRel; /* FSMRelations in arena storage order */
187 FSMRelation *lastRel; /* tail of storage-order list */
188 int numRels; /* number of FSMRelations now in use */
189 double sumRequests; /* sum of requested chunks over all rels */
190 char *arena; /* arena for page-info storage */
191 int totalChunks; /* total size of arena, in chunks */
192 int usedChunks; /* # of chunks assigned */
193 /* NB: there are totalChunks - usedChunks free chunks at end of arena */
197 * Per-relation struct --- this is an entry in the shared hash table.
198 * The hash key is the RelFileNode value (hence, we look at the physical
199 * relation ID, not the logical ID, which is appropriate).
203 RelFileNode key; /* hash key (must be first) */
204 FSMRelation *nextUsage; /* next rel in usage-recency order */
205 FSMRelation *priorUsage; /* prior rel in usage-recency order */
206 FSMRelation *nextPhysical; /* next rel in arena-storage order */
207 FSMRelation *priorPhysical; /* prior rel in arena-storage order */
208 bool isIndex; /* if true, we store only page numbers */
209 Size avgRequest; /* moving average of space requests */
210 int lastPageCount; /* pages passed to RecordRelationFreeSpace */
211 int firstChunk; /* chunk # of my first chunk in arena */
212 int storedPages; /* # of pages stored in arena */
213 int nextPage; /* index (from 0) to start next search at */
217 int MaxFSMRelations; /* these are set by guc.c */
220 static FSMHeader *FreeSpaceMap; /* points to FSMHeader in shared memory */
223 static FSMRelation *lookup_fsm_rel(RelFileNode *rel);
224 static FSMRelation *create_fsm_rel(RelFileNode *rel);
225 static void delete_fsm_rel(FSMRelation *fsmrel);
226 static int realloc_fsm_rel(FSMRelation *fsmrel, int nPages, bool isIndex);
227 static void link_fsm_rel_usage(FSMRelation *fsmrel);
228 static void unlink_fsm_rel_usage(FSMRelation *fsmrel);
229 static void link_fsm_rel_storage(FSMRelation *fsmrel);
230 static void unlink_fsm_rel_storage(FSMRelation *fsmrel);
231 static BlockNumber find_free_space(FSMRelation *fsmrel, Size spaceNeeded);
232 static BlockNumber find_index_free_space(FSMRelation *fsmrel);
233 static void fsm_record_free_space(FSMRelation *fsmrel, BlockNumber page,
235 static bool lookup_fsm_page_entry(FSMRelation *fsmrel, BlockNumber page,
237 static void compact_fsm_storage(void);
238 static void push_fsm_rels_after(FSMRelation *afterRel);
239 static void pack_incoming_pages(FSMPageData *newLocation, int newPages,
240 PageFreeSpaceInfo *pageSpaces, int nPages);
241 static void pack_existing_pages(FSMPageData *newLocation, int newPages,
242 FSMPageData *oldLocation, int oldPages);
243 static int fsm_calc_request(FSMRelation *fsmrel);
244 static int fsm_calc_target_allocation(int myRequest);
245 static int fsm_current_chunks(FSMRelation *fsmrel);
246 static int fsm_current_allocation(FSMRelation *fsmrel);
255 * InitFreeSpaceMap -- Initialize the freespace module.
257 * This must be called once during shared memory initialization.
258 * It builds the empty free space map table. FreeSpaceLock must also be
259 * initialized at some point, but is not touched here --- we assume there is
260 * no need for locking, since only the calling process can be accessing shared
264 InitFreeSpaceMap(void)
269 /* Create table header */
270 FreeSpaceMap = (FSMHeader *) ShmemAlloc(sizeof(FSMHeader));
271 if (FreeSpaceMap == NULL)
273 (errcode(ERRCODE_OUT_OF_MEMORY),
274 errmsg("insufficient shared memory for free space map")));
275 MemSet(FreeSpaceMap, 0, sizeof(FSMHeader));
277 /* Create hashtable for FSMRelations */
278 info.keysize = sizeof(RelFileNode);
279 info.entrysize = sizeof(FSMRelation);
280 info.hash = tag_hash;
282 FreeSpaceMap->relHash = ShmemInitHash("Free Space Map Hash",
283 MaxFSMRelations / 10,
286 (HASH_ELEM | HASH_FUNCTION));
288 if (!FreeSpaceMap->relHash)
290 (errcode(ERRCODE_OUT_OF_MEMORY),
291 errmsg("insufficient shared memory for free space map")));
293 /* Allocate page-storage arena */
294 nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
295 /* This check ensures spareChunks will be greater than zero */
296 if (nchunks <= MaxFSMRelations)
298 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
299 errmsg("max_fsm_pages must exceed max_fsm_relations * %d",
302 FreeSpaceMap->arena = (char *) ShmemAlloc(nchunks * CHUNKBYTES);
303 if (FreeSpaceMap->arena == NULL)
305 (errcode(ERRCODE_OUT_OF_MEMORY),
306 errmsg("insufficient shared memory for free space map")));
308 FreeSpaceMap->totalChunks = nchunks;
309 FreeSpaceMap->usedChunks = 0;
310 FreeSpaceMap->sumRequests = 0;
314 * Estimate amount of shmem space needed for FSM.
317 FreeSpaceShmemSize(void)
323 size = MAXALIGN(sizeof(FSMHeader));
325 /* hash table, including the FSMRelation objects */
326 size += hash_estimate_size(MaxFSMRelations, sizeof(FSMRelation));
328 /* page-storage arena */
329 nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
331 if (nchunks >= (INT_MAX / CHUNKBYTES))
333 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
334 errmsg("max_fsm_pages is too large")));
336 size += MAXALIGN(nchunks * CHUNKBYTES);
342 * GetPageWithFreeSpace - try to find a page in the given relation with
343 * at least the specified amount of free space.
345 * If successful, return the block number; if not, return InvalidBlockNumber.
347 * The caller must be prepared for the possibility that the returned page
348 * will turn out to have too little space available by the time the caller
349 * gets a lock on it. In that case, the caller should report the actual
350 * amount of free space available on that page and then try again (see
351 * RecordAndGetPageWithFreeSpace). If InvalidBlockNumber is returned,
352 * extend the relation.
355 GetPageWithFreeSpace(RelFileNode *rel, Size spaceNeeded)
358 BlockNumber freepage;
360 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
363 * We always add a rel to the hashtable when it is inquired about.
365 fsmrel = create_fsm_rel(rel);
368 * Update the moving average of space requests. This code implements
369 * an exponential moving average with an equivalent period of about 63
370 * requests. Ignore silly requests, however, to ensure that the
371 * average stays sane.
373 if (spaceNeeded > 0 && spaceNeeded < BLCKSZ)
375 int cur_avg = (int) fsmrel->avgRequest;
377 cur_avg += ((int) spaceNeeded - cur_avg) / 32;
378 fsmrel->avgRequest = (Size) cur_avg;
380 freepage = find_free_space(fsmrel, spaceNeeded);
381 LWLockRelease(FreeSpaceLock);
386 * RecordAndGetPageWithFreeSpace - update info about a page and try again.
388 * We provide this combo form, instead of a separate Record operation,
389 * to save one lock and hash table lookup cycle.
392 RecordAndGetPageWithFreeSpace(RelFileNode *rel,
398 BlockNumber freepage;
400 /* Sanity check: ensure spaceAvail will fit into OffsetNumber */
401 AssertArg(oldSpaceAvail < BLCKSZ);
403 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
406 * We always add a rel to the hashtable when it is inquired about.
408 fsmrel = create_fsm_rel(rel);
411 fsm_record_free_space(fsmrel, oldPage, oldSpaceAvail);
414 * Update the moving average of space requests, same as in
415 * GetPageWithFreeSpace.
417 if (spaceNeeded > 0 && spaceNeeded < BLCKSZ)
419 int cur_avg = (int) fsmrel->avgRequest;
421 cur_avg += ((int) spaceNeeded - cur_avg) / 32;
422 fsmrel->avgRequest = (Size) cur_avg;
425 freepage = find_free_space(fsmrel, spaceNeeded);
426 LWLockRelease(FreeSpaceLock);
431 * GetAvgFSMRequestSize - get average FSM request size for a relation.
433 * If the relation is not known to FSM, return a default value.
436 GetAvgFSMRequestSize(RelFileNode *rel)
441 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
442 fsmrel = lookup_fsm_rel(rel);
444 result = fsmrel->avgRequest;
446 result = INITIAL_AVERAGE;
447 LWLockRelease(FreeSpaceLock);
452 * RecordRelationFreeSpace - record available-space info about a relation.
454 * Any pre-existing info about the relation is assumed obsolete and discarded.
456 * The given pageSpaces[] array must be sorted in order by blkno. Note that
457 * the FSM is at liberty to discard some or all of the data.
460 RecordRelationFreeSpace(RelFileNode *rel,
462 PageFreeSpaceInfo *pageSpaces)
466 /* Limit nPages to something sane */
469 else if (nPages > MaxFSMPages)
470 nPages = MaxFSMPages;
472 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
475 * Note we don't record info about a relation unless there's already
476 * an FSM entry for it, implying someone has done GetPageWithFreeSpace
477 * for it. Inactive rels thus will not clutter the map simply by
480 fsmrel = lookup_fsm_rel(rel);
485 FSMPageData *newLocation;
487 curAlloc = realloc_fsm_rel(fsmrel, nPages, false);
488 curAllocPages = curAlloc * CHUNKPAGES;
491 * If the data fits in our current allocation, just copy it;
492 * otherwise must compress.
494 newLocation = (FSMPageData *)
495 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
496 if (nPages <= curAllocPages)
500 for (i = 0; i < nPages; i++)
502 BlockNumber page = pageSpaces[i].blkno;
503 Size avail = pageSpaces[i].avail;
505 /* Check caller provides sorted data */
506 if (i > 0 && page <= pageSpaces[i - 1].blkno)
507 elog(ERROR, "free-space data is not in page order");
508 FSMPageSetPageNum(newLocation, page);
509 FSMPageSetSpace(newLocation, avail);
512 fsmrel->storedPages = nPages;
516 pack_incoming_pages(newLocation, curAllocPages,
518 fsmrel->storedPages = curAllocPages;
521 LWLockRelease(FreeSpaceLock);
525 * GetFreeIndexPage - like GetPageWithFreeSpace, but for indexes
528 GetFreeIndexPage(RelFileNode *rel)
531 BlockNumber freepage;
533 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
536 * We always add a rel to the hashtable when it is inquired about.
538 fsmrel = create_fsm_rel(rel);
540 freepage = find_index_free_space(fsmrel);
541 LWLockRelease(FreeSpaceLock);
546 * RecordIndexFreeSpace - like RecordRelationFreeSpace, but for indexes
549 RecordIndexFreeSpace(RelFileNode *rel,
555 /* Limit nPages to something sane */
558 else if (nPages > MaxFSMPages)
559 nPages = MaxFSMPages;
561 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
564 * Note we don't record info about a relation unless there's already
565 * an FSM entry for it, implying someone has done GetFreeIndexPage for
566 * it. Inactive rels thus will not clutter the map simply by being
569 fsmrel = lookup_fsm_rel(rel);
575 IndexFSMPageData *newLocation;
577 curAlloc = realloc_fsm_rel(fsmrel, nPages, true);
578 curAllocPages = curAlloc * INDEXCHUNKPAGES;
581 * If the data fits in our current allocation, just copy it;
582 * otherwise must compress. But compression is easy: we merely
583 * forget extra pages.
585 newLocation = (IndexFSMPageData *)
586 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
587 if (nPages > curAllocPages)
588 nPages = curAllocPages;
590 for (i = 0; i < nPages; i++)
592 BlockNumber page = pages[i];
594 /* Check caller provides sorted data */
595 if (i > 0 && page <= pages[i - 1])
596 elog(ERROR, "free-space data is not in page order");
597 IndexFSMPageSetPageNum(newLocation, page);
600 fsmrel->storedPages = nPages;
602 LWLockRelease(FreeSpaceLock);
606 * FreeSpaceMapTruncateRel - adjust for truncation of a relation.
608 * We need to delete any stored data past the new relation length, so that
609 * we don't bogusly return removed block numbers.
612 FreeSpaceMapTruncateRel(RelFileNode *rel, BlockNumber nblocks)
616 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
617 fsmrel = lookup_fsm_rel(rel);
622 /* Use lookup to locate first entry >= nblocks */
623 (void) lookup_fsm_page_entry(fsmrel, nblocks, &pageIndex);
624 /* Delete all such entries */
625 fsmrel->storedPages = pageIndex;
626 /* XXX should we adjust rel's lastPageCount and sumRequests? */
628 LWLockRelease(FreeSpaceLock);
632 * FreeSpaceMapForgetRel - forget all about a relation.
634 * This is called when a relation is deleted. Although we could just let
635 * the rel age out of the map, it's better to reclaim and reuse the space
639 FreeSpaceMapForgetRel(RelFileNode *rel)
643 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
644 fsmrel = lookup_fsm_rel(rel);
646 delete_fsm_rel(fsmrel);
647 LWLockRelease(FreeSpaceLock);
651 * FreeSpaceMapForgetDatabase - forget all relations of a database.
653 * This is called during DROP DATABASE. As above, might as well reclaim
654 * map space sooner instead of later.
656 * XXX when we implement tablespaces, target Oid will need to be tablespace
657 * ID not database ID.
660 FreeSpaceMapForgetDatabase(Oid dbid)
665 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
666 for (fsmrel = FreeSpaceMap->usageList; fsmrel; fsmrel = nextrel)
668 nextrel = fsmrel->nextUsage; /* in case we delete it */
669 if (fsmrel->key.tblNode == dbid)
670 delete_fsm_rel(fsmrel);
672 LWLockRelease(FreeSpaceLock);
676 * PrintFreeSpaceMapStatistics - print statistics about FSM contents
678 * The info is sent to ereport() with the specified message level. This is
679 * intended for use during VACUUM.
682 PrintFreeSpaceMapStatistics(int elevel)
690 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
691 /* Count total space used --- tedious, but seems useful */
692 for (fsmrel = FreeSpaceMap->firstRel;
694 fsmrel = fsmrel->nextPhysical)
695 storedPages += fsmrel->storedPages;
696 /* Copy other stats before dropping lock */
697 numRels = FreeSpaceMap->numRels;
698 sumRequests = FreeSpaceMap->sumRequests;
699 LWLockRelease(FreeSpaceLock);
701 /* Convert stats to actual number of page slots needed */
702 needed = (sumRequests + numRels) * CHUNKPAGES;
705 (errmsg("free space map: %d relations, %d pages stored; %.0f total pages needed",
706 numRels, storedPages, needed),
707 errdetail("Allocated FSM size: %d relations + %d pages = %.0f kB shared memory.",
708 MaxFSMRelations, MaxFSMPages,
709 (double) FreeSpaceShmemSize() / 1024.0)));
713 * DumpFreeSpaceMap - dump contents of FSM into a disk file for later reload
715 * This is expected to be called during database shutdown, after updates to
716 * the FSM have stopped. We lock the FreeSpaceLock but that's purely pro
717 * forma --- if anyone else is still accessing FSM, there's a problem.
720 DumpFreeSpaceMap(void)
723 char cachefilename[MAXPGPATH];
724 FsmCacheFileHeader header;
727 /* Try to create file */
728 snprintf(cachefilename, sizeof(cachefilename), "%s/%s",
729 DataDir, FSM_CACHE_FILENAME);
731 unlink(cachefilename); /* in case it exists w/wrong permissions */
733 fp = AllocateFile(cachefilename, PG_BINARY_W);
736 elog(LOG, "could not write \"%s\": %m", cachefilename);
740 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
742 /* Write file header */
743 MemSet(&header, 0, sizeof(header));
744 strcpy(header.label, FSM_CACHE_LABEL);
745 header.endian = FSM_CACHE_ENDIAN;
746 header.version = FSM_CACHE_VERSION;
747 header.numRels = FreeSpaceMap->numRels;
748 if (fwrite(&header, 1, sizeof(header), fp) != sizeof(header))
751 /* For each relation, in order from least to most recently used... */
752 for (fsmrel = FreeSpaceMap->usageListTail;
754 fsmrel = fsmrel->priorUsage)
756 FsmCacheRelHeader relheader;
759 /* Write relation header */
760 MemSet(&relheader, 0, sizeof(relheader));
761 relheader.key = fsmrel->key;
762 relheader.isIndex = fsmrel->isIndex;
763 relheader.avgRequest = fsmrel->avgRequest;
764 relheader.lastPageCount = fsmrel->lastPageCount;
765 relheader.storedPages = fsmrel->storedPages;
766 if (fwrite(&relheader, 1, sizeof(relheader), fp) != sizeof(relheader))
769 /* Write the per-page data directly from the arena */
770 nPages = fsmrel->storedPages;
777 len = nPages * sizeof(IndexFSMPageData);
779 len = nPages * sizeof(FSMPageData);
781 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
782 if (fwrite(data, 1, len, fp) != len)
788 LWLockRelease(FreeSpaceLock);
795 elog(LOG, "could not write \"%s\": %m", cachefilename);
798 LWLockRelease(FreeSpaceLock);
802 /* Remove busted cache file */
803 unlink(cachefilename);
807 * LoadFreeSpaceMap - load contents of FSM from a disk file
809 * This is expected to be called during database startup, before any FSM
810 * updates begin. We lock the FreeSpaceLock but that's purely pro
811 * forma --- if anyone else is accessing FSM yet, there's a problem.
813 * Notes: no complaint is issued if no cache file is found. If the file is
814 * found, it is deleted after reading. Thus, if we crash without a clean
815 * shutdown, the next cycle of life starts with no FSM data. To do otherwise,
816 * we'd need to do significantly more validation in this routine, because of
817 * the likelihood that what is in the dump file would be out-of-date, eg
818 * there might be entries for deleted or truncated rels.
821 LoadFreeSpaceMap(void)
824 char cachefilename[MAXPGPATH];
825 FsmCacheFileHeader header;
828 /* Try to open file */
829 snprintf(cachefilename, sizeof(cachefilename), "%s/%s",
830 DataDir, FSM_CACHE_FILENAME);
832 fp = AllocateFile(cachefilename, PG_BINARY_R);
836 elog(LOG, "could not read \"%s\": %m", cachefilename);
840 LWLockAcquire(FreeSpaceLock, LW_EXCLUSIVE);
842 /* Read and verify file header */
843 if (fread(&header, 1, sizeof(header), fp) != sizeof(header) ||
844 strcmp(header.label, FSM_CACHE_LABEL) != 0 ||
845 header.endian != FSM_CACHE_ENDIAN ||
846 header.version != FSM_CACHE_VERSION ||
849 elog(LOG, "bogus file header in \"%s\"", cachefilename);
853 /* For each relation, in order from least to most recently used... */
854 for (relno = 0; relno < header.numRels; relno++)
856 FsmCacheRelHeader relheader;
864 /* Read and verify relation header, as best we can */
865 if (fread(&relheader, 1, sizeof(relheader), fp) != sizeof(relheader) ||
866 (relheader.isIndex != false && relheader.isIndex != true) ||
867 relheader.avgRequest >= BLCKSZ ||
868 relheader.lastPageCount < 0 ||
869 relheader.storedPages < 0)
871 elog(LOG, "bogus rel header in \"%s\"", cachefilename);
875 /* Make sure lastPageCount doesn't exceed current MaxFSMPages */
876 if (relheader.lastPageCount > MaxFSMPages)
877 relheader.lastPageCount = MaxFSMPages;
879 /* Read the per-page data */
880 nPages = relheader.storedPages;
881 if (relheader.isIndex)
882 len = nPages * sizeof(IndexFSMPageData);
884 len = nPages * sizeof(FSMPageData);
885 data = (char *) palloc(len + 1); /* +1 to avoid palloc(0) */
886 if (fread(data, 1, len, fp) != len)
888 elog(LOG, "premature EOF in \"%s\"", cachefilename);
894 * Okay, create the FSM entry and insert data into it. Since the
895 * rels were stored in reverse usage order, at the end of the loop
896 * they will be correctly usage-ordered in memory; and if
897 * MaxFSMRelations is less than it used to be, we will correctly
898 * drop the least recently used ones.
900 fsmrel = create_fsm_rel(&relheader.key);
901 fsmrel->avgRequest = relheader.avgRequest;
903 curAlloc = realloc_fsm_rel(fsmrel, relheader.lastPageCount,
905 if (relheader.isIndex)
907 IndexFSMPageData *newLocation;
909 curAllocPages = curAlloc * INDEXCHUNKPAGES;
912 * If the data fits in our current allocation, just copy it;
913 * otherwise must compress. But compression is easy: we
914 * merely forget extra pages.
916 newLocation = (IndexFSMPageData *)
917 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
918 if (nPages > curAllocPages)
919 nPages = curAllocPages;
920 memcpy(newLocation, data, nPages * sizeof(IndexFSMPageData));
921 fsmrel->storedPages = nPages;
925 FSMPageData *newLocation;
927 curAllocPages = curAlloc * CHUNKPAGES;
930 * If the data fits in our current allocation, just copy it;
931 * otherwise must compress.
933 newLocation = (FSMPageData *)
934 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
935 if (nPages <= curAllocPages)
937 memcpy(newLocation, data, nPages * sizeof(FSMPageData));
938 fsmrel->storedPages = nPages;
942 pack_existing_pages(newLocation, curAllocPages,
943 (FSMPageData *) data, nPages);
944 fsmrel->storedPages = curAllocPages;
954 LWLockRelease(FreeSpaceLock);
958 /* Remove cache file before it can become stale; see notes above */
959 unlink(cachefilename);
964 * Internal routines. These all assume the caller holds the FreeSpaceLock.
968 * Lookup a relation in the hash table. If not present, return NULL.
970 * The relation's position in the LRU list is not changed.
973 lookup_fsm_rel(RelFileNode *rel)
977 fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash,
988 * Lookup a relation in the hash table, creating an entry if not present.
990 * On successful lookup, the relation is moved to the front of the LRU list.
993 create_fsm_rel(RelFileNode *rel)
998 fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash,
1004 (errcode(ERRCODE_OUT_OF_MEMORY),
1005 errmsg("out of shared memory")));
1009 /* New hashtable entry, initialize it (hash_search set the key) */
1010 fsmrel->isIndex = false; /* until we learn different */
1011 fsmrel->avgRequest = INITIAL_AVERAGE;
1012 fsmrel->lastPageCount = 0;
1013 fsmrel->firstChunk = -1; /* no space allocated */
1014 fsmrel->storedPages = 0;
1015 fsmrel->nextPage = 0;
1017 /* Discard lowest-priority existing rel, if we are over limit */
1018 if (FreeSpaceMap->numRels >= MaxFSMRelations)
1019 delete_fsm_rel(FreeSpaceMap->usageListTail);
1021 /* Add new entry at front of LRU list */
1022 link_fsm_rel_usage(fsmrel);
1023 fsmrel->nextPhysical = NULL; /* not in physical-storage list */
1024 fsmrel->priorPhysical = NULL;
1025 FreeSpaceMap->numRels++;
1026 /* sumRequests is unchanged because request must be zero */
1030 /* Existing entry, move to front of LRU list */
1031 if (fsmrel->priorUsage != NULL)
1033 unlink_fsm_rel_usage(fsmrel);
1034 link_fsm_rel_usage(fsmrel);
1042 * Remove an existing FSMRelation entry.
1045 delete_fsm_rel(FSMRelation *fsmrel)
1047 FSMRelation *result;
1049 FreeSpaceMap->sumRequests -= fsm_calc_request(fsmrel);
1050 unlink_fsm_rel_usage(fsmrel);
1051 unlink_fsm_rel_storage(fsmrel);
1052 FreeSpaceMap->numRels--;
1053 result = (FSMRelation *) hash_search(FreeSpaceMap->relHash,
1054 (void *) &(fsmrel->key),
1058 elog(ERROR, "FreeSpaceMap hashtable corrupted");
1062 * Reallocate space for a FSMRelation.
1064 * This is shared code for RecordRelationFreeSpace and RecordIndexFreeSpace.
1065 * The return value is the actual new allocation, in chunks.
1068 realloc_fsm_rel(FSMRelation *fsmrel, int nPages, bool isIndex)
1075 * Delete any existing entries, and update request status.
1077 fsmrel->storedPages = 0;
1078 FreeSpaceMap->sumRequests -= fsm_calc_request(fsmrel);
1079 fsmrel->lastPageCount = nPages;
1080 fsmrel->isIndex = isIndex;
1081 myRequest = fsm_calc_request(fsmrel);
1082 FreeSpaceMap->sumRequests += myRequest;
1083 myAlloc = fsm_calc_target_allocation(myRequest);
1086 * Need to reallocate space if (a) my target allocation is more than
1087 * my current allocation, AND (b) my actual immediate need
1088 * (myRequest+1 chunks) is more than my current allocation. Otherwise
1089 * just store the new data in-place.
1091 curAlloc = fsm_current_allocation(fsmrel);
1092 if (myAlloc > curAlloc && (myRequest + 1) > curAlloc && nPages > 0)
1094 /* Remove entry from storage list, and compact */
1095 unlink_fsm_rel_storage(fsmrel);
1096 compact_fsm_storage();
1097 /* Reattach to end of storage list */
1098 link_fsm_rel_storage(fsmrel);
1099 /* And allocate storage */
1100 fsmrel->firstChunk = FreeSpaceMap->usedChunks;
1101 FreeSpaceMap->usedChunks += myAlloc;
1103 /* Watch out for roundoff error */
1104 if (FreeSpaceMap->usedChunks > FreeSpaceMap->totalChunks)
1106 FreeSpaceMap->usedChunks = FreeSpaceMap->totalChunks;
1107 curAlloc = FreeSpaceMap->totalChunks - fsmrel->firstChunk;
1114 * Link a FSMRelation into the LRU list (always at the head).
1117 link_fsm_rel_usage(FSMRelation *fsmrel)
1119 fsmrel->priorUsage = NULL;
1120 fsmrel->nextUsage = FreeSpaceMap->usageList;
1121 FreeSpaceMap->usageList = fsmrel;
1122 if (fsmrel->nextUsage != NULL)
1123 fsmrel->nextUsage->priorUsage = fsmrel;
1125 FreeSpaceMap->usageListTail = fsmrel;
1129 * Delink a FSMRelation from the LRU list.
1132 unlink_fsm_rel_usage(FSMRelation *fsmrel)
1134 if (fsmrel->priorUsage != NULL)
1135 fsmrel->priorUsage->nextUsage = fsmrel->nextUsage;
1137 FreeSpaceMap->usageList = fsmrel->nextUsage;
1138 if (fsmrel->nextUsage != NULL)
1139 fsmrel->nextUsage->priorUsage = fsmrel->priorUsage;
1141 FreeSpaceMap->usageListTail = fsmrel->priorUsage;
1144 * We don't bother resetting fsmrel's links, since it's about to be
1145 * deleted or relinked at the head.
1150 * Link a FSMRelation into the storage-order list (always at the tail).
1153 link_fsm_rel_storage(FSMRelation *fsmrel)
1155 fsmrel->nextPhysical = NULL;
1156 fsmrel->priorPhysical = FreeSpaceMap->lastRel;
1157 if (FreeSpaceMap->lastRel != NULL)
1158 FreeSpaceMap->lastRel->nextPhysical = fsmrel;
1160 FreeSpaceMap->firstRel = fsmrel;
1161 FreeSpaceMap->lastRel = fsmrel;
1165 * Delink a FSMRelation from the storage-order list, if it's in it.
1168 unlink_fsm_rel_storage(FSMRelation *fsmrel)
1170 if (fsmrel->priorPhysical != NULL || FreeSpaceMap->firstRel == fsmrel)
1172 if (fsmrel->priorPhysical != NULL)
1173 fsmrel->priorPhysical->nextPhysical = fsmrel->nextPhysical;
1175 FreeSpaceMap->firstRel = fsmrel->nextPhysical;
1176 if (fsmrel->nextPhysical != NULL)
1177 fsmrel->nextPhysical->priorPhysical = fsmrel->priorPhysical;
1179 FreeSpaceMap->lastRel = fsmrel->priorPhysical;
1181 /* mark as not in list, since we may not put it back immediately */
1182 fsmrel->nextPhysical = NULL;
1183 fsmrel->priorPhysical = NULL;
1184 /* Also mark it as having no storage */
1185 fsmrel->firstChunk = -1;
1186 fsmrel->storedPages = 0;
1190 * Look to see if a page with at least the specified amount of space is
1191 * available in the given FSMRelation. If so, return its page number,
1192 * and advance the nextPage counter so that the next inquiry will return
1193 * a different page if possible; also update the entry to show that the
1194 * requested space is not available anymore. Return InvalidBlockNumber
1198 find_free_space(FSMRelation *fsmrel, Size spaceNeeded)
1201 int pagesToCheck, /* outer loop counter */
1202 pageIndex; /* current page index */
1204 if (fsmrel->isIndex)
1205 elog(ERROR, "find_free_space called for an index relation");
1206 info = (FSMPageData *)
1207 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1208 pageIndex = fsmrel->nextPage;
1209 /* Last operation may have left nextPage pointing past end */
1210 if (pageIndex >= fsmrel->storedPages)
1213 for (pagesToCheck = fsmrel->storedPages; pagesToCheck > 0; pagesToCheck--)
1215 FSMPageData *page = info + pageIndex;
1216 Size spaceAvail = FSMPageGetSpace(page);
1218 /* Check this page */
1219 if (spaceAvail >= spaceNeeded)
1222 * Found what we want --- adjust the entry, and update
1225 FSMPageSetSpace(page, spaceAvail - spaceNeeded);
1226 fsmrel->nextPage = pageIndex + 1;
1227 return FSMPageGetPageNum(page);
1229 /* Advance pageIndex, wrapping around if needed */
1230 if (++pageIndex >= fsmrel->storedPages)
1234 return InvalidBlockNumber; /* nothing found */
1238 * As above, but for index case --- we only deal in whole pages.
1241 find_index_free_space(FSMRelation *fsmrel)
1243 IndexFSMPageData *info;
1247 * If isIndex isn't set, it could be that RecordIndexFreeSpace() has
1248 * never yet been called on this relation, and we're still looking at
1249 * the default setting from create_fsm_rel(). If so, just act as
1250 * though there's no space.
1252 if (!fsmrel->isIndex)
1254 if (fsmrel->storedPages == 0)
1255 return InvalidBlockNumber;
1256 elog(ERROR, "find_index_free_space called for a non-index relation");
1260 * For indexes, there's no need for the nextPage state variable; we
1261 * just remove and return the first available page. (We could save
1262 * cycles here by returning the last page, but it seems better to
1263 * encourage re-use of lower-numbered pages.)
1265 if (fsmrel->storedPages <= 0)
1266 return InvalidBlockNumber; /* no pages available */
1267 info = (IndexFSMPageData *)
1268 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1269 result = IndexFSMPageGetPageNum(info);
1270 fsmrel->storedPages--;
1271 memmove(info, info + 1, fsmrel->storedPages * sizeof(IndexFSMPageData));
1276 * fsm_record_free_space - guts of RecordFreeSpace operation (now only
1277 * provided as part of RecordAndGetPageWithFreeSpace).
1280 fsm_record_free_space(FSMRelation *fsmrel, BlockNumber page, Size spaceAvail)
1284 if (fsmrel->isIndex)
1285 elog(ERROR, "fsm_record_free_space called for an index relation");
1286 if (lookup_fsm_page_entry(fsmrel, page, &pageIndex))
1288 /* Found an existing entry for page; update it */
1291 info = (FSMPageData *)
1292 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1294 FSMPageSetSpace(info, spaceAvail);
1299 * No existing entry; ignore the call. We used to add the page to
1300 * the FSM --- but in practice, if the page hasn't got enough
1301 * space to satisfy the caller who's kicking it back to us, then
1302 * it's probably uninteresting to everyone else as well.
1308 * Look for an entry for a specific page (block number) in a FSMRelation.
1309 * Returns TRUE if a matching entry exists, else FALSE.
1311 * The output argument *outPageIndex is set to indicate where the entry exists
1312 * (if TRUE result) or could be inserted (if FALSE result).
1315 lookup_fsm_page_entry(FSMRelation *fsmrel, BlockNumber page,
1318 /* Check for empty relation */
1319 if (fsmrel->storedPages <= 0)
1325 /* Do binary search */
1326 if (fsmrel->isIndex)
1328 IndexFSMPageData *info;
1332 info = (IndexFSMPageData *)
1333 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1335 high = fsmrel->storedPages - 1;
1341 middle = low + (high - low) / 2;
1342 probe = IndexFSMPageGetPageNum(info + middle);
1345 *outPageIndex = middle;
1348 else if (probe < page)
1353 *outPageIndex = low;
1362 info = (FSMPageData *)
1363 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1365 high = fsmrel->storedPages - 1;
1371 middle = low + (high - low) / 2;
1372 probe = FSMPageGetPageNum(info + middle);
1375 *outPageIndex = middle;
1378 else if (probe < page)
1383 *outPageIndex = low;
1389 * Re-pack the FSM storage arena, dropping data if necessary to meet the
1390 * current allocation target for each relation. At conclusion, all available
1391 * space in the arena will be coalesced at the end.
1394 compact_fsm_storage(void)
1396 int nextChunkIndex = 0;
1397 FSMRelation *fsmrel;
1399 for (fsmrel = FreeSpaceMap->firstRel;
1401 fsmrel = fsmrel->nextPhysical)
1412 * Calculate target allocation, make sure we don't overrun due to
1415 newAlloc = fsm_calc_target_allocation(fsm_calc_request(fsmrel));
1416 if (newAlloc > FreeSpaceMap->totalChunks - nextChunkIndex)
1417 newAlloc = FreeSpaceMap->totalChunks - nextChunkIndex;
1418 if (fsmrel->isIndex)
1419 newAllocPages = newAlloc * INDEXCHUNKPAGES;
1421 newAllocPages = newAlloc * CHUNKPAGES;
1422 newChunkIndex = nextChunkIndex;
1423 nextChunkIndex += newAlloc;
1426 * Determine current size, current and new locations
1428 curChunks = fsm_current_chunks(fsmrel);
1429 oldChunkIndex = fsmrel->firstChunk;
1430 newLocation = FreeSpaceMap->arena + newChunkIndex * CHUNKBYTES;
1431 oldLocation = FreeSpaceMap->arena + oldChunkIndex * CHUNKBYTES;
1434 * It's possible that we have to move data down, not up, if the
1435 * allocations of previous rels expanded. This normally means that
1436 * our allocation expanded too (or at least got no worse), and
1437 * ditto for later rels. So there should be room to move all our
1438 * data down without dropping any --- but we might have to push down
1439 * following rels to acquire the room. We don't want to do the push
1440 * more than once, so pack everything against the end of the arena
1443 * In corner cases where roundoff has affected our allocation, it's
1444 * possible that we have to move down and compress our data too.
1445 * Since this case is extremely infrequent, we do not try to be smart
1446 * about it --- we just drop pages from the end of the rel's data.
1448 if (newChunkIndex > oldChunkIndex)
1450 int limitChunkIndex;
1452 if (newAllocPages < fsmrel->storedPages)
1454 /* move and compress --- just drop excess pages */
1455 fsmrel->storedPages = newAllocPages;
1456 curChunks = fsm_current_chunks(fsmrel);
1458 if (fsmrel->nextPhysical != NULL)
1459 limitChunkIndex = fsmrel->nextPhysical->firstChunk;
1461 limitChunkIndex = FreeSpaceMap->totalChunks;
1462 if (newChunkIndex + curChunks > limitChunkIndex)
1464 /* need to push down additional rels */
1465 push_fsm_rels_after(fsmrel);
1466 /* recheck for safety */
1467 if (fsmrel->nextPhysical != NULL)
1468 limitChunkIndex = fsmrel->nextPhysical->firstChunk;
1470 limitChunkIndex = FreeSpaceMap->totalChunks;
1471 if (newChunkIndex + curChunks > limitChunkIndex)
1472 elog(PANIC, "insufficient room in FSM");
1474 memmove(newLocation, oldLocation, curChunks * CHUNKBYTES);
1476 else if (newAllocPages < fsmrel->storedPages)
1479 * Need to compress the page data. For an index,
1480 * "compression" just means dropping excess pages; otherwise
1481 * we try to keep the ones with the most space.
1483 if (fsmrel->isIndex)
1485 fsmrel->storedPages = newAllocPages;
1486 /* may need to move data */
1487 if (newChunkIndex != oldChunkIndex)
1488 memmove(newLocation, oldLocation, newAlloc * CHUNKBYTES);
1492 pack_existing_pages((FSMPageData *) newLocation,
1494 (FSMPageData *) oldLocation,
1495 fsmrel->storedPages);
1496 fsmrel->storedPages = newAllocPages;
1499 else if (newChunkIndex != oldChunkIndex)
1502 * No compression needed, but must copy the data up
1504 memmove(newLocation, oldLocation, curChunks * CHUNKBYTES);
1506 fsmrel->firstChunk = newChunkIndex;
1508 Assert(nextChunkIndex <= FreeSpaceMap->totalChunks);
1509 FreeSpaceMap->usedChunks = nextChunkIndex;
1513 * Push all FSMRels physically after afterRel to the end of the storage arena.
1515 * We sometimes have to do this when deletion or truncation of a relation
1516 * causes the allocations of remaining rels to expand markedly. We must
1517 * temporarily push existing data down to the end so that we can move it
1518 * back up in an orderly fashion.
1521 push_fsm_rels_after(FSMRelation *afterRel)
1523 int nextChunkIndex = FreeSpaceMap->totalChunks;
1524 FSMRelation *fsmrel;
1526 FreeSpaceMap->usedChunks = FreeSpaceMap->totalChunks;
1528 for (fsmrel = FreeSpaceMap->lastRel;
1530 fsmrel = fsmrel->priorPhysical)
1538 if (fsmrel == afterRel)
1541 chunkCount = fsm_current_chunks(fsmrel);
1542 nextChunkIndex -= chunkCount;
1543 newChunkIndex = nextChunkIndex;
1544 oldChunkIndex = fsmrel->firstChunk;
1545 if (newChunkIndex < oldChunkIndex)
1548 elog(PANIC, "insufficient room in FSM");
1550 else if (newChunkIndex > oldChunkIndex)
1552 /* need to move it */
1553 newLocation = FreeSpaceMap->arena + newChunkIndex * CHUNKBYTES;
1554 oldLocation = FreeSpaceMap->arena + oldChunkIndex * CHUNKBYTES;
1555 memmove(newLocation, oldLocation, chunkCount * CHUNKBYTES);
1556 fsmrel->firstChunk = newChunkIndex;
1559 Assert(nextChunkIndex >= 0);
1563 * Pack a set of per-page freespace data into a smaller amount of space.
1565 * The method is to compute a low-resolution histogram of the free space
1566 * amounts, then determine which histogram bin contains the break point.
1567 * We then keep all pages above that bin, none below it, and just enough
1568 * of the pages in that bin to fill the output area exactly.
1570 #define HISTOGRAM_BINS 64
1573 pack_incoming_pages(FSMPageData *newLocation, int newPages,
1574 PageFreeSpaceInfo *pageSpaces, int nPages)
1576 int histogram[HISTOGRAM_BINS];
1583 Assert(newPages < nPages); /* else I shouldn't have been called */
1584 /* Build histogram */
1585 MemSet(histogram, 0, sizeof(histogram));
1586 for (i = 0; i < nPages; i++)
1588 Size avail = pageSpaces[i].avail;
1590 if (avail >= BLCKSZ)
1591 elog(ERROR, "bogus freespace amount");
1592 avail /= (BLCKSZ / HISTOGRAM_BINS);
1595 /* Find the breakpoint bin */
1597 for (i = HISTOGRAM_BINS - 1; i >= 0; i--)
1599 int sum = above + histogram[i];
1606 thresholdL = i * BLCKSZ / HISTOGRAM_BINS; /* low bound of bp bin */
1607 thresholdU = (i + 1) * BLCKSZ / HISTOGRAM_BINS; /* hi bound */
1608 binct = newPages - above; /* number to take from bp bin */
1609 /* And copy the appropriate data */
1610 for (i = 0; i < nPages; i++)
1612 BlockNumber page = pageSpaces[i].blkno;
1613 Size avail = pageSpaces[i].avail;
1615 /* Check caller provides sorted data */
1616 if (i > 0 && page <= pageSpaces[i - 1].blkno)
1617 elog(ERROR, "free-space data is not in page order");
1618 /* Save this page? */
1619 if (avail >= thresholdU ||
1620 (avail >= thresholdL && (--binct >= 0)))
1622 FSMPageSetPageNum(newLocation, page);
1623 FSMPageSetSpace(newLocation, avail);
1628 Assert(newPages == 0);
1632 * Pack a set of per-page freespace data into a smaller amount of space.
1634 * This is algorithmically identical to pack_incoming_pages(), but accepts
1635 * a different input representation. Also, we assume the input data has
1636 * previously been checked for validity (size in bounds, pages in order).
1638 * Note: it is possible for the source and destination arrays to overlap.
1639 * The caller is responsible for making sure newLocation is at lower addresses
1640 * so that we can copy data moving forward in the arrays without problem.
1643 pack_existing_pages(FSMPageData *newLocation, int newPages,
1644 FSMPageData *oldLocation, int oldPages)
1646 int histogram[HISTOGRAM_BINS];
1653 Assert(newPages < oldPages); /* else I shouldn't have been called */
1654 /* Build histogram */
1655 MemSet(histogram, 0, sizeof(histogram));
1656 for (i = 0; i < oldPages; i++)
1658 Size avail = FSMPageGetSpace(oldLocation + i);
1660 /* Shouldn't happen, but test to protect against stack clobber */
1661 if (avail >= BLCKSZ)
1662 elog(ERROR, "bogus freespace amount");
1663 avail /= (BLCKSZ / HISTOGRAM_BINS);
1666 /* Find the breakpoint bin */
1668 for (i = HISTOGRAM_BINS - 1; i >= 0; i--)
1670 int sum = above + histogram[i];
1677 thresholdL = i * BLCKSZ / HISTOGRAM_BINS; /* low bound of bp bin */
1678 thresholdU = (i + 1) * BLCKSZ / HISTOGRAM_BINS; /* hi bound */
1679 binct = newPages - above; /* number to take from bp bin */
1680 /* And copy the appropriate data */
1681 for (i = 0; i < oldPages; i++)
1683 BlockNumber page = FSMPageGetPageNum(oldLocation + i);
1684 Size avail = FSMPageGetSpace(oldLocation + i);
1686 /* Save this page? */
1687 if (avail >= thresholdU ||
1688 (avail >= thresholdL && (--binct >= 0)))
1690 FSMPageSetPageNum(newLocation, page);
1691 FSMPageSetSpace(newLocation, avail);
1696 Assert(newPages == 0);
1700 * Calculate number of chunks "requested" by a rel.
1702 * Rel's lastPageCount and isIndex settings must be up-to-date when called.
1704 * See notes at top of file for details.
1707 fsm_calc_request(FSMRelation *fsmrel)
1711 /* Convert page count to chunk count */
1712 if (fsmrel->isIndex)
1713 chunkCount = (fsmrel->lastPageCount - 1) / INDEXCHUNKPAGES + 1;
1715 chunkCount = (fsmrel->lastPageCount - 1) / CHUNKPAGES + 1;
1716 /* "Request" is anything beyond our one guaranteed chunk */
1717 if (chunkCount <= 0)
1720 return chunkCount - 1;
1724 * Calculate target allocation (number of chunks) for a rel
1726 * Parameter is the result from fsm_calc_request(). The global sumRequests
1727 * and numRels totals must be up-to-date already.
1729 * See notes at top of file for details.
1732 fsm_calc_target_allocation(int myRequest)
1737 spareChunks = FreeSpaceMap->totalChunks - FreeSpaceMap->numRels;
1738 Assert(spareChunks > 0);
1739 if (spareChunks >= FreeSpaceMap->sumRequests)
1741 /* We aren't oversubscribed, so allocate exactly the request */
1746 extra = (int) rint(spareChunks * myRequest / FreeSpaceMap->sumRequests);
1747 if (extra < 0) /* shouldn't happen, but make sure */
1754 * Calculate number of chunks actually used to store current data
1757 fsm_current_chunks(FSMRelation *fsmrel)
1761 /* Convert page count to chunk count */
1762 if (fsmrel->isIndex)
1763 chunkCount = (fsmrel->storedPages - 1) / INDEXCHUNKPAGES + 1;
1765 chunkCount = (fsmrel->storedPages - 1) / CHUNKPAGES + 1;
1766 /* Make sure storedPages==0 produces right answer */
1773 * Calculate current actual allocation (number of chunks) for a rel
1776 fsm_current_allocation(FSMRelation *fsmrel)
1778 if (fsmrel->nextPhysical != NULL)
1779 return fsmrel->nextPhysical->firstChunk - fsmrel->firstChunk;
1780 else if (fsmrel == FreeSpaceMap->lastRel)
1781 return FreeSpaceMap->usedChunks - fsmrel->firstChunk;
1784 /* it's not in the storage-order list */
1785 Assert(fsmrel->firstChunk < 0 && fsmrel->storedPages == 0);
1791 #ifdef FREESPACE_DEBUG
1793 * Dump contents of freespace map for debugging.
1795 * We assume caller holds the FreeSpaceLock, or is otherwise unconcerned
1796 * about other processes.
1801 FSMRelation *fsmrel;
1802 FSMRelation *prevrel = NULL;
1806 for (fsmrel = FreeSpaceMap->usageList; fsmrel; fsmrel = fsmrel->nextUsage)
1809 fprintf(stderr, "Map %d: rel %u/%u isIndex %d avgRequest %u lastPageCount %d nextPage %d\nMap= ",
1810 relNum, fsmrel->key.tblNode, fsmrel->key.relNode,
1811 (int) fsmrel->isIndex, fsmrel->avgRequest,
1812 fsmrel->lastPageCount, fsmrel->nextPage);
1813 if (fsmrel->isIndex)
1815 IndexFSMPageData *page;
1817 page = (IndexFSMPageData *)
1818 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1819 for (nPages = 0; nPages < fsmrel->storedPages; nPages++)
1821 fprintf(stderr, " %u",
1822 IndexFSMPageGetPageNum(page));
1830 page = (FSMPageData *)
1831 (FreeSpaceMap->arena + fsmrel->firstChunk * CHUNKBYTES);
1832 for (nPages = 0; nPages < fsmrel->storedPages; nPages++)
1834 fprintf(stderr, " %u:%u",
1835 FSMPageGetPageNum(page),
1836 FSMPageGetSpace(page));
1840 fprintf(stderr, "\n");
1841 /* Cross-check list links */
1842 if (prevrel != fsmrel->priorUsage)
1843 fprintf(stderr, "DumpFreeSpace: broken list links\n");
1846 if (prevrel != FreeSpaceMap->usageListTail)
1847 fprintf(stderr, "DumpFreeSpace: broken list links\n");
1848 /* Cross-check global counters */
1849 if (relNum != FreeSpaceMap->numRels)
1850 fprintf(stderr, "DumpFreeSpace: %d rels in list, but numRels = %d\n",
1851 relNum, FreeSpaceMap->numRels);
1854 #endif /* FREESPACE_DEBUG */