Lines Matching defs:page
1509 ** of the in-memory page cache. If there is an open transaction, or if
1688 ** file will be doing page-aligned sector reads and writes in a random
2189 ** that SQLite can use for the database page cache with the default page
2191 ** This configuration option is a no-op if an application-defined page
2194 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2196 ** The sz argument should be the size of the largest database page
2198 ** page header. ^The number of extra bytes needed by the page header
2206 ** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2207 ** a page cache line is larger than sz bytes or if all of the pMem buffer
2210 ** does an initial bulk allocation for page cache memory
2213 ** page cache memory is needed beyond what is provided by the initial
2275 ** the interface to a custom page cache implementation.)^
2281 ** the current page cache implementation into that object.)^ </dd>
2378 ** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2390 ** to be sorted exceeds the page size times the minimum of the
6876 ** and the number of bytes per page, respectively. The callback should
7073 ** limit by reducing the number of pages held in the page cache
7118 ** <li> An alternative page cache implementation is specified using
7120 ** <li> The page cache allocates from its own memory pool supplied
8303 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
8666 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8690 ** <dd>This parameter returns the number of bytes of page cache
8696 ** no space was left in the page cache.</dd>)^
8847 ** If an IO or other error occurs while writing a page to disk, the effect
8854 ** been written to disk in the middle of a transaction due to the page
9001 ** The sqlite3_pcache_page object represents a single page in the
9002 ** page cache. The page cache will allocate instances of this
9003 ** object. Various methods of the page cache use pointers to instances
9010 void *pBuf; /* The content of the page */
9011 void *pExtra; /* Extra information associated with the page */
9016 ** KEYWORDS: {page cache}
9019 ** register an alternative page cache implementation by passing in an
9022 ** SQLite is used for the page cache.
9024 ** custom page cache using this API, an application can better control
9030 ** The alternative page cache mechanism is an
9032 ** The built-in page cache is recommended for most uses.
9039 ** [[the xInit() page cache method]]
9045 ** required by the custom page cache implementation.
9047 ** built-in default page cache is used instead of the application defined
9048 ** page cache.)^
9050 ** [[the xShutdown() page cache method]]
9065 ** [[the xCreate() page cache methods]]
9072 ** associated with each page cache entry. ^The szExtra parameter will
9074 ** extra szExtra bytes on each page to store metadata about the underlying
9075 ** database page on disk. The value passed into szExtra depends
9082 ** never invoke xUnpin() except to deliberately delete a page.
9088 ** [[the xCachesize() page cache method]]
9096 ** [[the xPagecount() page cache methods]]
9100 ** [[the xFetch() page cache methods]]
9101 ** The xFetch() method locates a page in the cache and returns a pointer to
9102 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9105 ** single database page. The pExtra element of sqlite3_pcache_page will be
9107 ** for each entry in the page cache.
9109 ** The page to be fetched is determined by the key. ^The minimum key value
9110 ** is 1. After it has been retrieved using xFetch, the page is considered
9113 ** If the requested page is already in the page cache, then the page cache
9114 ** implementation must return a pointer to the page buffer with its content
9115 ** intact. If the requested page is not already in the cache, then the
9120 ** <tr><th> createFlag <th> Behavior when page is not already in cache
9121 ** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9122 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9124 ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9125 ** NULL if allocating a new page is effectively impossible.
9134 ** [[the xUnpin() page cache method]]
9135 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9137 ** then the page must be evicted from the cache.
9139 ** zero, then the page may be discarded or retained at the discretion of
9140 ** page cache implementation. ^The page cache implementation
9144 ** call to xUnpin() unpins the page regardless of the number of prior calls
9147 ** [[the xRekey() page cache methods]]
9149 ** page passed as the second argument. If the cache
9155 ** existing cache entries with page numbers (keys) greater than or equal
9160 ** [[the xDestroy() page cache method]]
9167 ** [[the xShrink() page cache method]]
9168 ** ^SQLite invokes the xShrink() method when it wants the page cache to
9169 ** free up as much of heap memory as possible. The page cache implementation
9303 ** and the destination and source page sizes differ, or
9305 ** destination and source page sizes differ.
10370 ** to disk. A dirty page may be in use if a database cursor created by an
10371 ** active SQL statement is reading from it, or if it is page 1 of a database
10372 ** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
13427 /* Maximum page size. The upper bound on this value is 65536. This a limit
13428 ** imposed by the use of 16-bit offsets within each page.
13434 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13435 ** compiled with the default page-size limit will not be able to rollback
13445 ** The default size of a database page.
13457 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
14669 ** The root-page of the schema table.
15083 ** that all locks will fit on a single page even at the minimum page size.
15085 ** is set high so that we don't have to allocate an unused page except
15086 ** for very large databases. But one should test the page skipping logic
15179 ** This header file defines the interface that the sqlite page cache
15180 ** subsystem. The page cache subsystem reads and writes a file a page
15197 ** The type used to represent a page number. The first page in a file
15198 ** is called page 1. 0 is used to represent "not a page".
15257 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15316 /* Functions used to obtain and release page references. */
15324 /* Operations on page references. */
15565 ** For example, the free-page-count field is located at byte offset 36 of
15658 Pgno iTable, /* Index of root page */
16491 ** This header file defines the interface that the sqlite page cache
16501 ** Every page in the cache is controlled by an instance of the following
16505 sqlite3_pcache_page *pPage; /* Pcache object page handle */
16508 PCache *pCache; /* PRIVATE: Cache that owns this page */
16510 Pager *pPager; /* The pager this page is part of */
16511 Pgno pgno; /* Page number for this page */
16513 u32 pageHash; /* Hash of page content */
16522 i16 nRef; /* Number of users of this page */
16534 ** writing this page to the database */
16536 #define PGHDR_MMAP 0x020 /* This is an mmap page object */
16540 /* Initialize and shutdown the page cache subsystem */
16554 int szPage, /* Size of every page */
16555 int szExtra, /* Extra space associated with each page */
16562 /* Modify the page-size after the cache has been created. */
16578 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
16579 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
16580 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
16584 /* Change a page number. Used by incr-vacuum. */
16590 /* Get a list of all dirty pages in the cache, sorted by page number */
16596 /* Clear flags from pages of the page cache */
16602 /* Return the total number of outstanding page references */
16605 /* Increment the reference count of an existing page */
16644 /* Free up as much memory as possible from the page cache */
17758 Pgno tnum; /* Root BTree page for this table */
18081 ** used to store the address of a VDBE instruction, not a database page
18082 ** number (it cannot - the database page is not allocated until the VDBE
19142 int regRoot; /* Register holding root page number for new objects */
19438 Pgno mxPage; /* Maximum page number. 0 for no limit. */
19485 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19492 int szPage; /* Size of each page in pPage[] */
22179 ** page size in bytes.
22320 ** the database page that contains the pending byte. It never attempts
22321 ** to read or write that page. The pending byte page is set aside
22527 Pgno pgnoRoot; /* Root page of the open btree cursor */
22532 const u8 *aRow; /* Data for the current row, if all on one page */
23752 ** Reference: Meeus page 61
35848 int szPage; /* Last known page size */
38216 ** the first page of the database, no damage is done.
41121 ** Return the system page size.
41336 ** current system page-size.
41344 int pgsz = osGetpagesize(); /* System page size */
41683 ** writing a single byte to the end of each (OS) page being
41685 ** last page in order to extend the file. But writing to all new
41693 /* Write to the last byte of each newly allocated or extended page */
45432 * possible initial heap size and the default page size, setting aside the
46798 ** code page.
46827 ** using the ANSI or OEM code page.
51780 /* Fetch a page of a memory-mapped file */
51799 /* Release a memory-mapped page */
52621 ** This file implements that page cache.
52626 ** A complete page cache is an instance of this structure. Every
52627 ** entry in the cache holds a single page of the database file. The
52630 ** A page cache entry is "clean" if it exactly matches what is currently
52631 ** on disk. A page is "dirty" if it has been modified and needs to be
52642 ** page to eject from the cache mid-transaction. It is better to eject
52643 ** a page that does not require a journal sync than one that does.
52645 ** to either the oldest page in the pDirty/pDirtyTail list that has a
52646 ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
52647 ** (so that the right page to eject can be found by following pDirtyPrev
52652 PgHdr *pSynced; /* Last synced page in dirty page list */
52656 int szPage; /* Size of every page in this cache */
52657 int szExtra; /* Size of extra space for each page */
52660 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
52741 assert( pCache!=0 ); /* Every page has an associated PCache */
52762 ** in step 3, and page might be written into the database without first
52766 ** Another example is when the database page size is smaller than the
52767 ** disk sector size. When any page of a sector is journalled, all pages
52817 ** searching for a dirty page to eject from the cache when it might
52843 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
52845 ** optimization, as if pSynced points to a page with the NEED_SYNC
52847 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
52883 ** page size. */
52892 ** Initialize and shutdown the page cache subsystem. Neither of these
52898 ** built-in default page cache is used instead of the application defined
52899 ** page cache. */
52923 ** szExtra is some extra space allocated for each page. The first
52924 ** 8 bytes of the extra space will be zeroed as the page is allocated,
52930 int szPage, /* Size of every page */
52931 int szExtra, /* Extra space associated with each page */
52952 ** Change the page size for PCache object. The caller must ensure that there
52953 ** are no outstanding page references when this function is called.
52976 ** Try to obtain a page from the cache.
52984 ** be 3 (not 1, but 3) to try to create a new page.
52986 ** If the createFlag is 0, then NULL is always returned if the page
52987 ** is not already in the cache. If createFlag is 1, then a new page
53000 PCache *pCache, /* Obtain the page from this cache */
53002 int createFlag /* If true, create page if it does not exist already */
53012 /* eCreate defines what to do if the page does not exist.
53013 ** 0 Do not allocate a new page. (createFlag==0)
53014 ** 1 Allocate a new page if doing so is inexpensive.
53016 ** 2 Allocate a new page even it doing so is difficult.
53032 ** page because no clean pages are available for reuse and the cache
53034 ** try harder to allocate a page. This routine might invoke the stress
53036 ** allocate the new page and will only fail to allocate a new page on
53042 PCache *pCache, /* Obtain the page from this cache */
53050 /* Find a dirty page to write-out and recycle. First try to find a
53051 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
53053 ** unreferenced dirty page.
53055 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
53057 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
53071 "spill page %d making room for %d - cache used: %d/%d",
53091 ** In the uncommon case where the page being fetched has not been
53098 PCache *pCache, /* Obtain the page from this cache */
53124 PCache *pCache, /* Obtain the page from this cache */
53143 ** Decrement the reference count on a page. If the page is clean and the
53160 ** Increase the reference count of a supplied page by 1.
53170 ** Drop a page from the cache. There must be exactly one reference to the
53171 ** page. This function deletes that reference, so after it returns the
53172 ** page pointed to by p is invalid.
53185 ** Make sure the page is marked as dirty. If it isn't dirty already,
53205 ** Make sure the page is marked as clean. If it isn't clean already,
53223 ** Make every page in the cache clean.
53257 ** Change the page number of page p to newPgno.
53283 ** Drop every cache entry whose page number is greater than "pgno". The
53285 ** other than page 1 with a page number greater than pgno.
53287 ** If there is a reference to page 1 and the pgno parameter passed to this
53288 ** function is 0, then the data area associated with page 1 is zeroed, but
53289 ** the page object is not dropped.
53411 ** Return a list of all dirty pages in the cache, sorted by page number.
53432 ** Return the number of references to the page supplied as an argument.
53485 ** Free up as much memory as possible from the page cache.
53494 ** in the page-cache hierarchy.
53547 ** This file implements the default page cache implementation (the
53550 ** If the default page cache implementation is overridden, then neither of
53556 ** | database page content | PgHdr1 | MemPage | PgHdr |
53559 ** The database page content is up front (so that buffer overreads tend to
53562 ** as the database page number and how that database page is used. PgHdr
53566 ** PgHdr1 contains information needed to look up a page by its page number.
53568 ** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
53577 ** was defined, then the page content would be held in a separate memory
53580 ** area after the page content buffer. The header serves as that overrun
53592 ** Memory for a page might come from any of three sources:
53595 ** (2) Global page-cache memory provided using sqlite3_config() with
53600 ** that is allocated when the page cache is created. The size of the local
53628 ** directly before this structure and is used to cache the page content.
53631 ** read a few bytes (no more than 16 bytes) past the end of the page buffer.
53632 ** It will only read past the end of the page buffer, never write. This
53633 ** object is positioned immediately after the page buffer to serve as an
53639 ** in memory directly after the associated page data, if the database is
53640 ** corrupt, code at the b-tree layer may overread the page buffer and
53652 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
53653 unsigned int iKey; /* Key value (page number) */
53654 u16 isBulkLocal; /* This page from bulk local storage */
53657 PCache1 *pCache; /* Cache that currently owns this page */
53664 ** A page is pinned if it is not on the LRU list. To be "pinned" means
53665 ** that the page is in active use and must not be deallocated.
53670 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
53675 ** This page cache implementation works in one of two modes:
53701 /* Each page cache is an instance of the following object. Every
53703 ** temporary or transient database) has a single page cache which
53740 ** Free slots in the allocator used to divide up the global page cache
53764 void *pStart, *pEnd; /* Bounds of global page cache memory */
53767 PgFreeslot *pFree; /* Free page blocks */
53802 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
53856 pX->page.pBuf = zBulk;
53857 pX->page.pExtra = &pX[1];
53964 ** Allocate a new page object initially associated with cache pCache.
53993 p->page.pBuf = pPg;
53994 p->page.pExtra = &p[1];
54004 ** Free a page object allocated by pcache1AllocPage().
54015 pcache1Free(p->page.pBuf);
54039 ** Return true if it desirable to avoid allocating a new page cache
54042 ** If memory was allocated specifically to the page cache using
54044 ** it is desirable to avoid allocating a new page cache entry because
54046 ** for all page cache needs and we should not need to spill the
54049 ** Or, the heap is used for all page cache memory but the heap is
54051 ** allocating a new page cache entry in order to avoid stressing
54106 ** This function is used internally to remove the page pPage from the
54131 ** Remove the page supplied as an argument from the hash table
54133 ** Also free the page if freePage is true.
54174 ** Discard all pages from cache pCache with a page number (key value)
54240 ** PGroup in pcache1.grp is used for all page caches (mode-2).
54245 ** configured a start-time buffer for use as page-cache memory using
54299 PCache1 *pCache; /* The newly created page cache */
54300 PGroup *pGroup; /* The group the new page cache will belong to */
54433 /* Step 4. Try to recycle a page. */
54452 /* Step 5. If a usable page buffer has still not been found,
54468 *(void **)pPage->page.pExtra = 0;
54480 ** Fetch a page by key value.
54482 ** Whether or not a new page may be allocated by this function depends on
54484 ** page. 1 means allocate a new page if space is easily available. 2
54485 ** means to try really hard to allocate a new page.
54492 ** There are three different approaches to obtaining space for a page,
54496 ** copy of the requested page. If one is found, it is returned.
54498 ** 2. If createFlag==0 and the page is not already in the cache, NULL is
54501 ** 3. If createFlag is 1, and the page is not already in the cache, then
54502 ** return NULL (do not allocate a new page) if any of the following
54525 ** then attempt to recycle a page from the LRU list. If it is the right
54529 ** 5. Otherwise, allocate and return a new page buffer.
54548 /* Step 2: If the page was found in the hash table, then return it.
54549 ** If the page was not in the hash table and createFlag is 0, abort.
54550 ** Otherwise (page not in hash and createFlag!=0) continue with
54551 ** subsequent steps to try to create the page. */
54590 assert( offsetof(PgHdr1,page)==0 );
54610 ** Mark a page as unpinned (eligible for asynchronous recycling).
54624 /* It is an error to call this function if the page is already
54633 /* Add the page to the PGroup LRU list. */
54659 assert( iOld!=iNew ); /* The page number really is changing */
54686 ** Discard all unpinned pages in the cache with a page number equal to
54687 ** or greater than parameter iLimit. Any pinned pages with a page number
54748 ** Return the size of the header on each page of this PCACHE implementation.
54781 nFree += pcache1MemSize(p->page.pBuf);
55334 ** This is the implementation of the page cache subsystem or "pager".
55420 /* Read a page from the write-ahead log, if it is present. */
55487 ** stored in each frame (i.e. the db page-size when the WAL was created).
55513 ** Within this comment block, a page is deemed to have been synced
55515 ** Otherwise, the page is not synced until the xSync method of the VFS
55516 ** is called successfully on the file containing the page.
55518 ** Definition: A page of the database file is said to be "overwriteable" if
55519 ** one or more of the following are true about the page:
55521 ** (a) The original content of the page as it was at the beginning of
55525 ** (b) The page was a freelist leaf page at the start of the transaction.
55527 ** (c) The page number is greater than the largest page that existed in
55530 ** (1) A page of the database file is never overwritten unless one of the
55533 ** (a) The page and all other pages on the same sector are overwriteable.
55535 ** (b) The atomic page write optimization is enabled, and the entire
55537 ** number consists of a single page change.
55539 ** (2) The content of a page written into the rollback journal exactly matches
55544 ** (3) Writes to the database file are an integer multiple of the page size
55545 ** in length and are aligned on a page boundary.
55547 ** (4) Reads from the database file are either aligned on a page boundary and
55548 ** an integer multiple of the page size in length or are taken from the
55718 ** A pager moves from WRITER_LOCKED state to this state when a page is
55727 ** * The contents of the page cache have been modified.
55740 ** * The contents of the page cache have been modified (and possibly
55772 ** the contents of the page-cache may be left in an inconsistent state.
55784 ** page-cache and any other in-memory state at the same time. Everything
55947 ** An open page cache is an instance of struct Pager. A description of
56022 ** to allocate a new page to prevent the journal file from being written
56029 ** is larger than the database page-size in order to prevent a journal sync
56050 ** is not an integer multiple of the page-size, the value stored in
56051 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
56053 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
56056 ** During a write-transaction, if pages with page-numbers greater than
56155 Bitvec *pInJournal; /* One bit for each page in the database file */
56169 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
56174 u16 nExtra; /* Add this many bytes to each in-memory page */
56175 i16 nReserve; /* Number of unused bytes at end of each page */
56179 Pgno lckPgno; /* Page number for the locking page */
56180 i64 pageSize; /* Number of bytes in a page */
56194 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
56199 PCache *pPCache; /* Pointer to page cache object */
56245 ** of a 32-bit checksum on each page of data. The checksum covers both
56246 ** the page number and the pPager->pageSize bytes of data for the page.
56260 ** The size of the of each page record in the journal is given by
56309 ** Return true if page pgno can be read directly from the database file
56314 ** * the desired page is not currently in the wal file.
56540 /* Forward references to the various page getters */
56568 ** Return true if it is necessary to write page *pPg into the sub-journal.
56569 ** A page needs to be written into the sub-journal if there exists one
56572 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
56573 ** * The bit corresponding to the page-number is not set in
56595 ** Return true if the page is already in the journal file.
56691 ** a database page may be written atomically, and
56693 ** to the page size.
56696 ** file when it contains rollback data for exactly one page.
56748 ** Return a 32-bit hash of the page data for pPage.
56768 ** that the page is either dirty or still matches the calculated page-hash.
56941 ** - 4 bytes: Random number used for page hash.
56942 ** - 4 bytes: Initial database page count.
56944 ** - 4 bytes: Database page size.
56974 ** Write the nRec Field - the number of page records that follow this
56982 ** rest of the journal file contains valid page records. This assumption
57011 /* The page size */
57035 ** database page size. Since the zHeader buffer is only Pager.pageSize
57057 ** page records following this header and *pDbSize is set to the size of the
57119 /* Read the page-size and sector-size journal header fields. */
57126 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
57128 ** variable is already set to the correct page size.
57134 /* Check that the values read from the page-size and sector-size fields
57143 /* If the either the page-size or sector-size in the journal-header is
57151 /* Update the page-size to match the value read from the journal.
57185 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
57216 ** the super-journal name. This is in case the previous page written to
57257 ** Discard the entire contents of the in-memory page-cache.
57684 ** page of data and the current value of pPager->cksumInit.
57688 ** of the page data, starting with byte offset (pPager->pageSize%200).
57711 ** Report the current page size and number of reserved bytes back
57740 ** Read a single page from either the journal file (if isMainJrnl==1) or
57741 ** from the sub-journal (if isMainJrnl==0) and playback that page.
57742 ** The page begins at offset *pOffset into the file. The *pOffset
57743 ** value is increased to the start of the next page in the journal.
57748 ** If the page number of the page record read from the (sub-)journal file
57753 ** been played back. If the page at *pOffset has already been played back
57755 ** Make sure the pDone bit corresponding to the *pOffset page is set
57758 ** If the page record is successfully read from the (sub-)journal file
57766 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
57784 PgHdr *pPg; /* An existing page in the cache */
57785 Pgno pgno; /* The page number of a page in journal */
57787 char *aData; /* Temporary storage for the page */
57789 int isSynced; /* True if journal page is synced */
57816 /* Read the page number and page data from the journal or sub-journal
57826 /* Sanity checking on the page. This is more important that I originally
57846 /* If this page has already been played back before during the current
57853 /* When playing back page 1, restore the nReserve setting
57860 ** page in the pager cache. In this case just update the pager cache,
57861 ** not the database file. The page is left marked dirty in this case.
57864 ** and a page is moved during an incremental vacuum then the page may
57866 ** during a Movepage() call, then the page may not be in the cache
57871 ** pager cache if it exists and the main file. The page is then marked
57873 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
57876 ** Ticket #1171: The statement journal might contain page content that is
57877 ** different from the page content at the start of the transaction.
57878 ** This occurs when a page is changed prior to the start of a statement
57881 ** for certain that original page contents are synced into the main rollback
57886 ** locked. (2) we know that the original page content is fully synced
57887 ** in the main journal either because the page is not in cache or else
57888 ** the page is marked as needSync==0.
57901 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
57948 ** the database and the page is not in-memory, there is a potential
57949 ** problem. When the page is next fetched by the b-tree layer, it
57955 ** if the page is on the free-list at the start of the transaction, then
57958 ** The solution is to add an in-memory page to the cache containing
57959 ** the data just read from the sub-journal. Mark the page as dirty
57960 ** and if the pager requires a journal-sync, then mark the page as
57973 /* No page should ever be explicitly rolled back that is in use, except
57974 ** for page 1 which is held in use in order to keep the lock on the
57975 ** database active. However such a page may be rolled back as a result
57989 /* If this was page 1, then restore the value of Pager.dbFileVers.
57995 /* Decode the page just read from disk */
58044 ** a couple of kilobytes or so - potentially larger than the page
58259 ** (2) 4 byte big-endian integer which is the number of valid page records
58261 ** number of page records from the journal size.
58268 ** (6) 4 byte big-endian integer which is the page size.
58271 ** + 4 byte page number.
58279 ** valid page entries in the journal. In most cases, you can compute the
58294 ** journal file then all pages up to the first corrupted page are rolled
58318 int needPagerReset; /* True to reset page prior to first page rollback */
58416 ** database file and/or page cache.
58518 ** Read the content for page pPg out of the database file (or out of
58523 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
58530 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
58560 ** zero or the size of the database in page. Bytes 32..35 and 35..39
58561 ** should be page numbers which are never 0xffffffff. So filling
58580 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
58611 ** This function is invoked once for each page that has already been
58613 ** Parameter iPg is the page number of said page. The pCtx argument
58616 ** If page iPg is present in the cache, and has no outstanding references,
58618 ** references, the page content is reloaded from the database. If the
58665 ** + Discard the cached page (if refcount==0), or
58666 ** + Reload page content from the database (if refcount>0).
58686 ** The list of pages passed into this routine is always sorted by page number.
58687 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
58702 /* Verify that the page list is in accending order */
58711 ** any pages with page numbers greater than nTruncate into the WAL file.
58783 ** in pages (assuming the page size currently stored in Pager.pageSize).
58805 ** WAL sub-system, determine the page count based on the size of
58807 ** integer multiple of the page-size, round up the result.
58903 ** Throughout the rollback process, each time a page is rolled back, the
58905 ** implementation below). This is used to ensure that a page is only
58913 ** (or transaction). No page with a page-number greater than this value
58953 ** There might be records in the main journal that have a page number
59237 ** Change the page size used by the Pager object. The new page size
59246 ** * the new page size (value of *pPageSize) is valid (a power
59249 ** * there are no outstanding page references, and
59254 ** then the pager object page size is set to *pPageSize.
59256 ** If the page size is changed, then this function uses sqlite3PagerMalloc()
59258 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
59261 ** If the page size is not changed, either because one of the enumerated
59264 ** then *pPageSize is set to the old, retained page size before returning.
59275 ** there is at least one outstanding page reference, this function
59328 ** Return a pointer to the "temporary page" buffer held internally
59330 ** entire content of a database page. This buffer is used internally
59340 ** Attempt to set the maximum database page count if mxPage is positive.
59342 ** maximum page count below the current size of the database.
59344 ** Regardless of mxPage, return the current maximum page count.
59421 ** However, if the file is between 1 and <page-size> bytes in size, then
59422 ** this is considered a 1 page file.
59466 ** following is true for all dirty pages currently in the page-cache:
59468 ** a) The page number is less than or equal to the size of the
59471 ** b) if the page content were written at this time, it would not
59475 ** dirty page were to be discarded from the cache via the pagerStress()
59476 ** routine, pagerStress() would not write the current page content to
59479 ** content of the page. However, since this content is not present in either
59560 ** Obtain a reference to a memory mapped page object for page number pgno.
59562 ** If successful, set *ppPage to point to the new page reference
59572 void *pData, /* xFetch()'d data for this page */
59573 PgHdr **ppPage /* OUT: Acquired page object */
59575 PgHdr *p; /* Memory mapped page to return */
59610 ** Release a reference to page pPg. pPg must have been returned by an
59661 ** Shutdown the page cache. Free all memory and close all files.
59665 ** and their memory is freed. Any attempt to use a page associated
59666 ** with this page cache after this function returns will likely
59738 ** Return the page number for page pPg.
59746 ** Increment the reference count for page pPg.
59784 ** page currently held in memory before returning SQLITE_OK. If an IO
59916 ** a page is skipped if it meets either of the following criteria:
59918 ** * The page number is greater than Pager.dbSize, or
59919 ** * The PGHDR_DONT_WRITE flag is set on the page.
59921 ** If writing out a page causes the database file to grow, Pager.dbFileSize
59922 ** is updated accordingly. If page 1 is written out, then the value cached
59964 /* If there are dirty pages in the page cache with page numbers greater
59969 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
59982 /* Write out the page data. */
59985 /* If page 1 was just written, update Pager.dbFileVers to match
59987 ** page caused the database file to grow, update dbFileSize.
60000 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
60005 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
60038 ** Append a record of the current state of page pPg to the sub-journal.
60076 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
60102 ** database). The second argument is a reference to a page that is
60103 ** currently dirty but has no outstanding references. The page
60111 ** If successful, sqlite3PcacheMakeClean() is called on the page and
60113 ** page clean, the IO error code is returned. If the page cannot be
60154 /* Write a single frame for this page to the log. */
60175 /* Write the contents of the page out to the database file. */
60182 /* Mark the page as clean. */
60184 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
60224 ** along with each page reference. This space is available to the user
60225 ** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
60247 int nExtra, /* Extra bytes append to each in-memory page */
60268 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
60468 ** choose a default page size in case we have to create the
60469 ** database file. The default page size is the maximum of:
60473 ** + The largest page size that can be written atomically.
60510 ** In this case we accept the default page size and delay actually
60769 ** the contents of the page cache and rolling back any open journal
60899 ** to be in ERROR state when there are zero outstanding page
61001 ** The page getter methods each try to acquire a reference to a
61002 ** page with page number pgno. If the requested reference is
61012 ** If the requested page is already in the cache, it is returned.
61013 ** Otherwise, a new page object is allocated and populated with data
61015 ** choose not to allocate a new page object and may reuse an existing
61018 ** The extra data appended to a page is always initialized to zeros the
61019 ** first time a page is loaded into memory. If the page requested is
61021 ** data is left as it was when the page object was last used.
61023 ** If the database image is smaller than the requested page or if
61025 ** requested page is not already stored in the cache, then no
61027 ** page is initialized to all zeros.
61030 ** the contents of the page. This occurs in two scenarios:
61032 ** a) When reading a free-list leaf page from the database, and
61035 ** a new page into the cache to be filled with the data read
61042 ** savepoints are set. This means if the page is made writable at any
61050 ** to find a page in the in-memory cache first. If the page is not already
61060 DbPage **ppPage, /* Write a pointer to the page here */
61092 ** the page. Return without further ado. */
61098 /* The pager cache has created a new page. Its content needs to
61101 ** (*) obsolete. Was: maximum page number is 2^31
61102 ** (2) Never try to fetch the locking page
61120 ** page that does not need to be journaled. Nevertheless, be sure
61158 /* The page getter for when memory-mapped I/O is enabled */
61162 DbPage **ppPage, /* Write a pointer to the page here */
61169 /* It is acceptable to use a read-only (mmap) page for any page except
61170 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
61230 /* The page getter method for when the pager is an error state */
61234 DbPage **ppPage, /* Write a pointer to the page here */
61245 /* Dispatch all page fetch requests to the appropriate getter method.
61250 DbPage **ppPage, /* Write a pointer to the page here */
61258 ** Acquire a page if it is already in the in-memory cache. Do
61259 ** not read the page from disk. Return a pointer to the page,
61260 ** or 0 if the page is not in cache.
61264 ** in the page if the page is not already in cache. This routine
61265 ** returns NULL if the page is not in cache or if a disk I/O error
61280 ** Release a page reference.
61283 ** used if we know that the page being released is not the last page.
61285 ** to routines can be used to release any page other than BtShared.pPage1.
61476 ** file as well as into the page cache. Which would be incorrect in
61496 ** Write page pPg onto the end of the rollback journal.
61505 /* We should never write to the journal file the page that
61515 ** page in the block above, set the need-sync flag for the page.
61517 ** playback_one_page() will think that the page needs to be restored
61533 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
61549 ** Mark a single data page as writeable. The page is written into the
61550 ** main journal or sub-journal as required. If the page is written into
61576 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
61588 /* Mark the page that is about to be modified as dirty. */
61591 /* If a rollback journal is in use, them make sure the page that is about
61592 ** to change is in the rollback journal, or if the page is a new page off
61609 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
61615 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
61616 ** and before writing the page into the rollback journal. Wait until now,
61617 ** after the page has been successfully journalled, before setting the
61618 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
61622 /* If the statement journal is open and the page is not in it,
61623 ** then write the page into the statement journal.
61638 ** is larger than the page size. SQLite makes the (reasonable) assumption that
61643 ** Usually, the sector size is less than or equal to the page size, in which
61645 ** exceptional case where the page size is smaller than the sector size.
61650 Pgno pg1; /* First page of the sector pPg is located on. */
61653 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
61665 /* This trick assumes that both the page-size and sector-size are
61667 ** of the first page of the sector pPg is located on.
61728 ** Mark a data page as writeable. This routine must be called before
61729 ** making changes to a page. The caller must check the return value
61730 ** of this function and be careful not to change any page data unless
61760 ** Return TRUE if the page given in the argument was previously passed
61762 ** to change the content of the page.
61772 ** write the information on page pPg back to the disk, even though
61773 ** that page might be marked as dirty. This happens, for example, when
61774 ** the page has been added as a leaf of the freelist and so its
61778 ** on the given page is unused. The pager marks the page as clean so
61784 ** This optimization cannot be used with a temp-file, as the page may
61786 ** memory pressure forces page pPg out of the cache, the data does need
61793 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
61809 ** To avoid excess churning of page 1, the update only happens once.
61814 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
61815 ** page data. In this case the file will be updated when the current
61821 ** by writing an updated version of page 1 using a call to the
61851 PgHdr *pPgHdr; /* Reference to page 1 */
61855 /* Open page 1 of the file for writing. */
61859 /* If page one was fetched successfully, and this function is not
61860 ** operating in direct-mode, make page 1 writable. When not in
61861 ** direct mode, page 1 is always held in cache and hence the PagerGet()
61872 /* If running in direct mode, write the contents of page 1 to the file. */
61894 /* Release the page reference. */
62011 /* Must have at least one page for the WAL commit flag.
62047 ** blocks of size page-size, and
62049 ** * Exactly one page has been modified and store in the journal file.
62076 ** following call will modify the in-memory representation of page 1
62077 ** to include the updated change counter and then write page 1
62112 ** Because the change-counter page was just modified, unless the
62161 ** last page in the db image moved to the free-list. In this case the
62162 ** last page is never written out to disk, leaving the database file
62344 ** Return the number of references to the specified page.
62656 ** This function is called by the wal module when writing page content
62660 ** page content. If a malloc fails, this function may return NULL.
62672 ** Move the page pPg to location pgno in the file.
62674 ** There must be no references to the page previously located at
62675 ** pgno (which we call pPgOld) though that page is allowed to be
62676 ** in cache. If the page previously located at pgno is not already
62679 ** References to the page pPg remain valid. Updating any
62681 ** allocated along with the page) is the responsibility of the caller.
62685 ** has been removed (CREATE INDEX needs to move a page when a statement
62688 ** If the fourth argument, isCommit, is non-zero, then this page is being
62690 ** is being committed. In this case, it is guaranteed that the database page
62697 PgHdr *pPgOld; /* The page being overwritten. */
62700 Pgno origPgno; /* The original page number */
62709 ** the page we are moving from.
62717 /* If the page being moved is dirty and has not been saved by the latest
62718 ** savepoint, then save the current contents of the page into the
62722 ** <journal page X, then modify it in memory>
62724 ** <Move page X to location Y>
62727 ** If page X were not written to the sub-journal here, it would not
62741 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
62745 /* If the journal needs to be sync()ed before page pPg->pgno can
62749 ** the journal needs to be sync()ed before database page pPg->pgno
62759 /* If the cache contains a page with page-number pgno, remove it
62761 ** page pgno before the 'move' operation, it needs to be retained
62762 ** for the page moved there.
62775 ** need to rollback later. Just move the page out of the way. */
62786 /* For an in-memory database, make sure the original page continues
62788 ** as the original page since it has already been allocated.
62797 ** sync()ed before any data is written to database file page needSyncPgno.
62798 ** Currently, no such page exists in the page-cache and the
62800 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
62803 ** If the attempt to load the page into the page-cache fails, (due
62805 ** array. Otherwise, if the page is loaded and written again in
62829 ** The page handle passed as the first argument refers to a dirty page
62830 ** with a page number other than iNew. This function changes the page's
62831 ** page number to iNew and sets the value of the PgHdr.flags field to
62841 ** Return a pointer to the data for the specified page.
62850 ** allocated along with the specified page.
63348 ** frames, return the size in bytes of the page images stored within the
63380 ** Each frame records the revised content of a single page from the
63400 ** 8: Database page size. Example: 1024
63408 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
63409 ** of page data. The frame-header is six big-endian 32-bit unsigned
63463 ** To read a page from the database (call it page number P), a reader
63464 ** first checks the WAL to see if it contains page P. If so, then the
63465 ** last valid instance of page P that is a followed by a commit frame
63467 ** contains no copies of page P that are valid and which are a commit
63468 ** frame or are followed by a commit frame, then page P is read from
63481 ** because frames for page P can appear anywhere within the WAL, the
63482 ** reader has to scan the entire WAL looking for page P frames. If the
63486 ** search for frames of a particular page.
63510 ** a page number P and a maximum frame index M, return the index of the
63511 ** last frame in the wal before frame M for page P in the WAL, or return
63512 ** NULL if there are no frames for page P in the WAL prior to M.
63530 ** Each index block contains two sections, a page-mapping that contains the
63531 ** database page number associated with each wal frame, and a hash-table
63532 ** that allows readers to query an index block for a specific page number.
63533 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
63534 ** for the first index block) 32-bit page numbers. The first entry in the
63535 ** first index-block contains the database page number corresponding to the
63541 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
63543 ** allocated size of the page-mapping array - the page-mapping array merely
63546 ** Even without using the hash table, the last frame for page P
63547 ** can be found by scanning the page-mapping sections of each index block
63551 ** holding the content for that page.
63555 ** hash table for each page number in the mapping section, so the hash
63565 ** To look for page P in the hash table, first compute a hash iKey on
63578 ** no hash slot such that aHash[i]==p) then page P is not in the
63581 ** page P.
63584 ** first index block, looking for entries corresponding to page P. On
63586 ** examined in order to either find the last entry for page P, or to
63679 ** Or it can be 1 to represent a 65536-byte page. The latter case was
63688 u16 szPage; /* Database page size in bytes. 1==64K */
63856 ** assuming a database page size of szPage bytes. The offset returned
63876 u32 szPage; /* Database page size */
63918 ** Each page of the wal-index mapping contains a hash-table made up of
63925 ** all frames in the WAL in database page order. Where two or more frames
63926 ** correspond to the same database page, the iterator visits only the
63944 u32 *aPgno; /* Array of page numbers. */
63947 } aSegment[1]; /* One for every 32KB page in the wal-index */
63952 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
63963 ** The block of page numbers associated with the first hash-table in a
63965 ** hash-table on each aligned 32KB page of the wal-index.
63975 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
63994 int iPage, /* The page we seek */
63995 volatile u32 **ppPage /* Write the page pointer here */
64014 /* Request a pointer to the required page from the VFS */
64043 int iPage, /* The page we seek */
64044 volatile u32 **ppPage /* Write the page pointer here */
64185 u32 iPage, /* Database page number for frame */
64187 u8 *aData, /* Pointer to page data */
64216 u32 *piPage, /* OUT: Database page number for frame */
64218 u8 *aData, /* Pointer to page data (for checksum) */
64233 /* A frame is only valid if the page number is creater than zero.
64255 /* If we reach this point, the frame is valid. Return the page number
64328 ** Compute a hash on a page number. The resulting hash value must land
64343 ** of a page hash table in the wal-index. This becomes the return value
64349 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
64354 ** Return pointers to the hash table and page number array stored on
64355 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
64364 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
64392 ** Return the number of the wal-index page that contains the hash-table
64393 ** and page-number array that contain entries corresponding to WAL frame
64410 ** Return the page number associated with frame iFrame in this WAL.
64445 /* Obtain pointers to the hash-table and page-number array containing
64447 ** that the page said hash-table and array reside on is already mapped.(1)
64491 ** Set an entry in the wal-index that will map database page number
64501 ** page number array and hash table entry.
64624 u32 iPg; /* Current 32KB wal-index page */
64633 /* If the database page size is not a power of two, or is greater than
64695 u32 pgno; /* Database page number for frame */
64942 ** Find the smallest page number out of all pages held in the WAL that
64945 ** that page was last written into the WAL. Write into *piPage the page
64948 ** Return 0 on success. If there are no pages in the WAL with a page
64953 u32 *piPage, /* OUT: The page number of the next page */
64954 u32 *piFrame /* OUT: Wal frame index of next page */
64957 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
65326 ** Return the page-size in bytes used by the database.
65372 ** by active readers. This routine will never overwrite a database page
65408 int szPage; /* Database page-size */
65410 u32 iDbpage = 0; /* Next database page to write */
65413 u32 mxPage; /* Max database page to write */
65480 ** maximum size of the pending-byte page (65536 bytes), then
65689 /* The first page of the wal-index must be mapped at this point. */
65759 /* Ensure that page 0 of the wal-index (the page that contains the
65789 /* If the first page of the wal-index has been mapped, try to read the
65871 ** contents of the page cache before proceeding. Or, if it returns
65948 ** reading the database file only. However, the page cache cannot
65991 u32 pgno; /* Database page number for frame */
66039 ** to the caller that the local page cache is obsolete and needs to be
66058 ** access any database page that is modified by a WAL frame up to and
66095 ** paged out or take a page-fault that is time-consuming to resolve,
66139 ** must be zeroed before the requested page is returned.
66267 ** a version of page X that lies before pWal->minFrame (call that version
66269 ** page later in the wal file. But if version B happens to like past
66294 ** comparing their content to the corresponding page with the database
66298 ** This is only really safe if the file-system is such that any page
66303 ** page.
66465 ** is the same database page. */
66497 ** Search the wal file for page pgno. If found, set *piRead to the frame that
66498 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
66506 Pgno pgno, /* Database page number to read data for */
66510 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
66517 /* If the "last page" field of the wal-index header snapshot is 0, then
66528 /* Search the hash table or tables for an entry matching page number
66612 u8 *pOut /* Buffer to write page data to */
66734 /* This call cannot fail. Unless the page for which the page number
66737 ** (if (a) is false) or simply expels the page from the cache (if (b)
66741 ** are no outstanding references to any page other than page 1. And
66742 ** page 1 is never written to the log until the transaction is
66862 int szPage; /* Size of one page */
66900 PgHdr *pPage, /* The page of the frame to be written */
66919 /* Write the page data */
66933 const int szPage = pWal->szPage;/* Database page size */
66983 int szPage, /* Database page-size in bytes */
66993 int nExtra = 0; /* Number of extra copies of last page */
67084 /* Check if this page has already been written into the wal file by
67529 ** read-lock. This function returns the database page-size if it is known,
67585 ** The basic idea is that each page of the file contains N database
67592 ** All of the keys on the page that Ptr(0) points to have values less
67593 ** than Key(0). All of the keys on page Ptr(1) and its subpages have
67602 ** BTrees. Each BTree is identified by the index of its root page. The
67605 ** page. If the payload is larger than the preset amount then surplus
67608 ** page has a small header which contains the Ptr(N) pointer and other
67613 ** The file is divided into pages. The first page is called page 1,
67614 ** the second is page 2, and so forth. A page number of zero indicates
67615 ** "no such page". The page size can be any power of 2 between 512 and 65536.
67616 ** Each page can be either a btree page, a freelist page, an overflow
67617 ** page, or a pointer-map page.
67619 ** The first page is always a btree page. The first 100 bytes of the first
67620 ** page contain a special header (the "file header") that describes the file.
67628 ** 20 1 Bytes of unused space at the end of each page
67634 ** 32 4 First freelist page
67640 ** 48 4 Size of page cache
67641 ** 52 4 Largest root-page (auto/incr_vacuum)
67657 ** space in a page that can be consumed by a single cell for standard
67660 ** on one page. Thus the default max embedded payload fraction is 64.
67663 ** payload is spilled to overflow pages. Once an overflow page is allocated,
67674 ** file header that occurs before the page header.
67679 ** | page header | 8 bytes for leaves. 12 bytes for interior nodes
67692 ** The page headers looks like this:
67697 ** 3 2 number of cells on this page
67702 ** The flags define the format of this btree page. The leaf flag means that
67703 ** this page has no children. The zerodata flag means that this page carries
67708 ** The cell pointer array begins on the first byte after the page header.
67710 ** offsets from the beginning of the page to the cell content in the cell
67713 ** be easily added without having to defragment the page.
67715 ** Cell content is stored at the very end of the page and grows toward the
67716 ** beginning of the page.
67725 ** in the page header at offset 7.
67732 ** the end of the page. Pointers to the cells are in the cell pointer array
67733 ** that immediately follows the page header. Cells is not necessarily
67762 ** 4 First page of the overflow chain. Omitted if no overflow
67764 ** Overflow pages form a linked list. Each page except the last is completely
67765 ** filled with data (pagesize - 4 bytes). The last page can have as little
67769 ** 4 Page number of next overflow page
67773 ** file header points to the first in a linked list of trunk page. Each trunk
67774 ** page points to multiple leaf pages. The content of a leaf page is
67775 ** unspecified. A trunk page looks like this:
67778 ** 4 Page number of next trunk page
67779 ** 4 Number of leaf pointers on this page
67785 /* The following value is the maximum cell size assuming a maximum page
67790 /* The maximum number of cells on a single page of the database. This
67792 ** plus 2 bytes for the index to the cell in the page header). Such
67820 ** first byte of on-disk image of every BTree page.
67829 ** page that has been loaded into memory. The information in this object
67830 ** is derived from the raw on-disk page content.
67832 ** As each database page is loaded into memory, the pager allocats an
67834 ** "extra" information associated with each page of the pager.)
67843 Pgno pgno; /* Page number for this page */
67844 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
67846 u8 leaf; /* True if a leaf page */
67847 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
67854 int nFree; /* Number of free bytes on the page. -1 for unknown */
67855 u16 nCell; /* Number of cells on this page, local and ovfl */
67856 u16 maskPage; /* Mask for page offset */
67860 BtShared *pBt; /* Pointer to BtShared that this page is part of */
67861 u8 *aData; /* Pointer to disk image of the page data */
67862 u8 *aDataEnd; /* One byte past the end of the entire page - not just
67863 ** the usable space, the entire page. Used to prevent
67867 DbPage *pDbPage; /* Pager page handle */
67875 ** is opened on the table with root page BtShared.iTable. Locks are removed
67881 Pgno iTable; /* Root page of table */
67927 BtLock lock; /* Object used to lock page 1 */
67992 Pager *pPager; /* The page cache */
67995 MemPage *pPage1; /* First page of the database */
68004 u8 nReserveWanted; /* Desired number of extra bytes per page */
68010 u32 pageSize; /* Total number of bytes on a page */
68011 u32 usableSize; /* Number of usable bytes on each page */
68044 ** based on information extract from the raw disk page.
68051 u16 nSize; /* Size of the cell content on the main b-tree page */
68099 Pgno *aOverflow; /* Cache of overflow page locations */
68109 Pgno pgnoRoot; /* The root page of this tree */
68110 i8 iPage; /* Index of current page in apPage */
68115 MemPage *pPage; /* Current page */
68116 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
68167 ** The database page the PENDING_BYTE occupies. This page is never used.
68173 ** database page. The first argument to each is the number of usable
68174 ** bytes on each page of the database (often 1024). The second is the
68175 ** page number to look up in the pointer map.
68177 ** PTRMAP_PAGENO returns the database page number of the pointer-map
68178 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
68181 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
68183 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
68191 ** The pointer map is a lookup table that identifies the parent page for
68192 ** each child page in the database file. The parent page is the page that
68193 ** contains a pointer to the child. Every page in the database contains
68194 ** 0 or 1 parent pages. (In this context 'database page' refers
68195 ** to any page that is not part of the pointer map itself.) Each pointer map
68196 ** entry consists of a single byte 'type' and a 4 byte parent page number.
68200 ** position in the file to another as part of autovacuum. When a page
68202 ** new location. The pointer map is used to locate the parent page quickly.
68204 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
68207 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
68210 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
68211 ** overflow pages. The page number identifies the page that
68212 ** contains the cell with a pointer to this overflow page.
68214 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
68215 ** overflow pages. The page-number identifies the previous
68216 ** page in the overflow page list.
68218 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
68219 ** identifies the parent page in the btree.
68253 ** The aRef[] array is allocated so that there is 1 bit for each page in
68254 ** the database. As the integrity-check proceeds, for each page used in
68263 u8 *aPgRef; /* 1 bit per page in the db (see above) */
68634 ** from the header of a btree page. If the page size is 65536 and the page
68643 #define BTALLOC_ANY 0 /* Allocate any page */
68644 #define BTALLOC_EXACT 1 /* Allocate exact page if possible */
68645 #define BTALLOC_LE 2 /* Allocate any page <= the parameter */
68751 ** with the page number and filename associated with the (MemPage*).
68757 zMsg = sqlite3_mprintf("database corruption page %d of %s",
68779 ** table with root page iRoot. Return 1 if it does and 0 if not.
68781 ** For example, when writing to a table with root-page iRoot via
68787 ** caller should have first obtained a lock specifying the root page of
68793 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
68794 ** hold a write-lock on the schema table (root page 1). This is also
68799 Pgno iRoot, /* Root page of b-tree */
68826 /* Figure out the root-page that the lock should be held on. For table
68827 ** b-trees, this is just the root page of the b-tree being read or
68828 ** written. For index b-trees, it is the root page of the associated
68837 /* Two or more indexes share the same root page. There must
68850 /* Search for the required lock. Either a write-lock on root-page iTab, a
68882 ** rooted at page iRoot, one should call:
68902 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
68962 ** Add a lock on the table with root-page iTable to the shared-btree used
69136 ** Invalidate the overflow page-list cache for all cursors opened
69155 ** cursors open on any row within the table with root-page pgnoRoot.
69188 ** when a page that previously contained data becomes a free-list leaf
69189 ** page.
69195 ** 1) When all data is deleted from a page and the page becomes
69196 ** a free-list leaf page, the page is not written to the database
69198 ** such a page is not even journalled (as it will not be modified,
69201 ** 2) When a free-list leaf page is reused, its content is not read
69207 ** a page is moved to the free-list and then reused within the same
69208 ** transaction, a problem comes up. If the page is not journalled when
69214 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
69215 ** moved to become a free-list leaf page, the corresponding bit is
69216 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
69239 ** This function is called when a free-list leaf page is removed from the
69241 ** page from the pager layer with the 'no-content' flag set. True otherwise.
69357 ** the table with root-page iRoot. "Saving the cursor position" means that
69368 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
69581 ** Given a page number of a regular database page, return the page
69582 ** number for the pointer-map page that contains the entry for the
69583 ** input page number.
69585 ** Return 0 (not a valid page) for pgno==1 since there is
69586 ** no pointer map associated with page 1. The integrity_check logic
69606 ** This routine updates the pointer map entry for page number 'key'
69607 ** so that it maps to type 'eType' and parent page number 'pgno'.
69614 DbPage *pDbPage; /* The pointer map page */
69616 Pgno iPtrmap; /* The pointer map page number */
69617 int offset; /* Offset in pointer map page */
69623 /* The super-journal page number must never be used as a pointer map page */
69639 ** If that byte is set, it means this page is also being used
69640 ** as a btree page. */
69668 ** This routine retrieves the pointer map entry for page 'key', writing
69669 ** the type and parent page number to *pEType and *pPgno respectively.
69673 DbPage *pDbPage; /* The pointer map page */
69674 int iPtrmap; /* Pointer map page index */
69675 u8 *pPtrmap; /* Pointer map page data */
69710 ** Given a btree page and a cell index (0 means the first cell on
69711 ** the page, 1 means the second cell, and so forth) return a pointer
69728 ** on a single B-tree page. Make necessary adjustments to the CellInfo
69736 /* If the payload will not fit completely on the local page, we have
69764 ** page pPage, return the number of bytes of payload stored locally.
69884 ** on the local page. No overflow is required.
69921 ** on the local page. No overflow is required.
69943 ** data area of the btree-page. The return number includes the cell
69944 ** data header and the local payload, but not any overflow page or
70077 ** The cell pCell is currently part of page pSrc but will ultimately be part
70079 ** pointer to an overflow page, insert an entry into the pointer-map for
70080 ** the overflow page that will be valid after pCell has been moved to pPage.
70102 ** Defragment the page given. This routine reorganizes cells within the
70103 ** page so that there are no free-blocks on the free-block list.
70106 ** present in the page after this routine returns.
70109 ** b-tree page so that there are no freeblocks or fragment bytes, all
70111 ** cells are packed tightly at the end of the page.
70116 int hdr; /* Offset to the page header */
70118 int usableSize; /* Number of usable bytes on a page */
70121 int nCell; /* Number of cells on the page */
70122 unsigned char *data; /* The page data */
70146 ** reconstruct the entire page. */
70234 ** Search the free-list on page pPg for space to store a cell nByte bytes in
70248 const int hdr = pPg->hdrOffset; /* Offset to page header */
70268 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
70273 ** fragmented bytes within the page. */
70278 /* This slot extends off the end of the usable part of the page */
70300 /* The free slot chain extends off the end of the page */
70307 ** Allocate nByte bytes of space from within the B-Tree page passed
70338 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
70340 ** then the cell content offset of an empty page wants to be 65536.
70394 ** is no way that the allocation can extend off the end of the page.
70414 ** at the end of the page. So do additional corruption checks inside this
70474 ** pointer in the page header) then check to see if iStart should be
70515 ** Decode the flags byte (the first byte of the header) for a page
70536 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
70537 ** interior table b-tree page. */
70539 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
70540 ** leaf table b-tree page. */
70555 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
70556 ** interior index b-tree page. */
70558 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
70559 ** leaf index b-tree page. */
70568 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
70576 "database corruption at page[%u], flagByte[%x], isInit[%u], intKey[%u], intKeyLeaf[%u], leaf[%u], "
70599 ** Compute the amount of freespace on the page. In other words, fill
70604 u8 hdr; /* Offset to beginning of page header */
70606 int usableSize; /* Amount of usable space on each page */
70607 int nFree; /* Number of unused bytes on the page */
70631 /* Compute the total free space on the page
70633 ** start of the first freeblock on the page, or is zero if there are no
70640 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
70647 /* Freeblock off the end of the page */
70661 /* Last freeblock extends past page end */
70669 ** of the page, then the page must be corrupted. This check also
70671 ** area, according to the page header, lies within the page.
70691 int usableSize; /* Maximum usable space on the page */
70719 ** Return SQLITE_OK on success. If we see that the page does
70720 ** not contain a well-formed database page, then return
70722 ** guarantee that the page is well-formed. It only shows that
70740 ** the b-tree page type. */
70752 ** number of cells on the page. */
70755 /* To many cells for a single page. The page must be corrupt */
70759 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
70760 ** possible for a root page of a table that contains no rows) then the
70761 ** offset to the cell content area will equal the page size minus the
70775 ** Set up a raw page so that it looks like a database page holding
70829 ** Get a page from the pager. Initialize the MemPage.pBt and
70833 ** about the content of the page at this time. So do not go to the disk
70835 ** If in the future we call sqlite3PagerWrite() on this page, that
70841 Pgno pgno, /* Number of the page to fetch */
70842 MemPage **ppPage, /* Return the page in this parameter */
70857 ** Retrieve a page from the pager cache. If the requested page is not
70884 ** Get a page from the pager and initialize it.
70886 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
70887 ** call. Do additional sanity checking on the page in this case.
70890 ** The page is fetched as read-write unless pCur is not NULL and is
70898 Pgno pgno, /* Number of the page to get */
70899 MemPage **ppPage, /* Write the page pointer here */
70900 BtCursor *pCur, /* Cursor to receive the page, or NULL */
70901 int bReadOnly /* True for a read-only page */
70929 /* If obtaining a child page for a cursor, we must verify that the page is
70930 ** compatible with the root page. */
70979 ** Get an unused page.
70983 ** * If the page is already in use for some other purpose, immediately
70989 Pgno pgno, /* Number of the page to fetch */
70990 MemPage **ppPage, /* Return the page in this parameter */
71011 ** the transaction, for each page restored this routine is called.
71014 ** page to agree with the restored data.
71024 /* pPage might not be a btree page; it might be an overflow page
71025 ** or ptrmap page or a free page. In those cases, the following
71028 ** btreeInitPage() be called on every btree page so we make
71029 ** the call for every page that comes in for re-initing. */
71082 u8 nReserve; /* Byte of unused space on each page */
71239 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
71422 ** inserting them into a leaf page (function fillInCell()). If
71427 ** it into a database page. This is not actually a problem, but it
71588 ** Change the default pages size and the number of reserved bytes per page.
71589 ** Or, if the page size has already been fixed, return SQLITE_READONLY
71592 ** The page size must be a power of 2 between 512 and 65536. If the page
71593 ** size supplied does not meet this constraint then the page size is not
71599 ** at the beginning of a page.
71602 ** bytes per page is left unchanged.
71604 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
71637 ** Return the currently defined page size
71662 ** Return the number of bytes of space at the end of every page that
71681 ** Set the maximum page count for a database if mxPage is positive.
71683 ** Regardless of the value of mxPage, return the maximum page count.
71704 ** freelist leaf pages are not written back to the database. Thus in-page
71867 ** required as the version of page 1 currently in the page1 buffer
71898 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
71902 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
71914 ** each page to reserve for extensions.
71921 /* After reading the first page of the database assuming a page size
71922 ** of BtShared.pageSize, we have discovered that the page-size is
71925 ** again with the correct page-size.
71944 ** be less than 480. In other words, if the page size is 512, then the
71959 ** cells can will fit on one page. We assume a 10-byte page header.
71965 ** 4-byte overflow page pointer
71968 ** page pointer.
72017 ** this routine unrefs the first page of the database file which
72036 ** into a new empty database by initializing the first page of
72079 ** Initialize the first page of the database file (creating a database
72080 ** consisting of a single page and no schema objects). Return SQLITE_OK
72185 ** page 1. So if some other shared-cache client already has a write-lock
72186 ** on page 1, the transaction cannot be opened. */
72208 ** reading page 1 it discovers that the page-size of the database
72210 ** pBt->pageSize to the page-size of the file on disk.
72269 ** re-read the database size from page 1 if a savepoint or transaction
72303 ** Set the pointer-map entries for all children of page pPage. Also, if
72309 int nCell; /* Number of cells in page pPage */
72339 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
72343 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
72344 ** page of pPage.
72346 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
72347 ** page pointed to by one of the cells on pPage.
72349 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
72350 ** overflow page in the list.
72356 /* The pointer is always the first 4 bytes of the page in this case. */
72408 ** Move the open database page pDbPage to location iFreePage in the
72412 ** the journal needs to be sync()ed before database page pDbPage->pgno
72414 ** page.
72418 MemPage *pDbPage, /* Open page to move */
72420 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
72424 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
72435 /* Move page iDbPage from its current location to page number iFreePage */
72436 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
72444 /* If pDbPage was a btree-page, then it may have child pages and/or cells
72448 ** If pDbPage is an overflow page, then the first 4 bytes may store a
72449 ** pointer to a subsequent overflow page. If this is the case, then
72450 ** the pointer map needs to be updated for the subsequent overflow page.
72467 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
72500 ** that the last page of the file currently in use is no longer in use.
72536 /* Remove the page from the files free-list. This is not required
72551 Pgno iFreePg; /* Index of free page to move pLastPg to */
72561 /* If bCommit is zero, this loop runs exactly once and page pLastPg
72562 ** is swapped with the first free page pulled off the free list.
72565 ** looping until a free-page located within the first nFin pages
72612 int nEntry; /* Number of entries on one ptrmap page */
72693 Pgno iFree; /* The next page to be freed */
72698 /* It is not possible to create a database for which the final page
72699 ** is either a pointer-map page or the pending-byte page. If one
73025 ** call btreeGetPage() on page 1 again to make
73120 ** Create a new cursor for the BTree whose root is on the page
73154 ** No checking is done to make sure that page iTable really is the
73155 ** root page of a b-tree. If it is not, then the cursor acquired
73163 Pgno iTable, /* Root page of table to open */
73230 Pgno iTable, /* Root page of table to open */
73243 Pgno iTable, /* Root page of table to open */
73444 ** Given the page number of an overflow page in the database (parameter
73445 ** ovfl), this function finds the page number of the next page in the
73447 ** pointer-map data instead of reading the content of page ovfl to do so.
73451 ** The page number of the next overflow page in the linked list is
73452 ** written to *pPgnoNext. If page ovfl is the last page in its linked
73456 ** to page number pOvfl was obtained, then *ppPage is set to point to that
73464 Pgno ovfl, /* Current overflow page number */
73466 Pgno *pPgnoNext /* OUT: Next overflow page number */
73476 /* Try to find the next page in the overflow list using the
73477 ** autovacuum pointer-map pages. Guess that the next page in
73478 ** the overflow list is page number (ovfl+1). If that guess turns
73479 ** out to be wrong, fall back to loading the data of page
73480 ** number ovfl to determine the next page number.
73520 ** Copy data from a buffer to a page, or from a page to a buffer.
73522 ** pPayload is a pointer to data stored on database page pDbPage.
73531 void *pPayload, /* Pointer to page data */
73534 int eOp, /* 0 -> copy from page, 1 -> copy to page */
73538 /* Copy data from buffer to page (a write operation) */
73545 /* Copy data from page to buffer (a read operation) */
73562 ** The content being read or written might appear on the main page
73567 ** the overflow page-list cache array (BtCursor.aOverflow).
73571 ** Once an overflow page-list cache has been allocated, it must be
73574 ** mode, the following events may invalidate an overflow page-list cache.
73578 ** * Creating a table (may require moving an overflow page).
73590 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
73618 /* Check if data must be read/written to/from the btree page itself. */
73634 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
73641 ** The aOverflow[] array is sized at one entry for each overflow page
73642 ** in the overflow chain. The page number of the first overflow page is
73663 /* If the overflow page-list cache has been allocated and the
73664 ** entry for the first required overflow page is valid, skip
73676 /* If required, populate the overflow page-list cache. */
73684 /* The only reason to read this page is to obtain the page
73685 ** number for the next page in the overflow chain. The page
73687 ** page-list cache, if any, then fall back to the getOverflowPage()
73699 /* Need to read this page properly. It contains some of the
73711 ** 2) data is required from the start of this overflow page, and
73712 ** 3) there are no dirty pages in the page-cache
73714 ** 5) the page is not in the WAL file
73718 ** output buffer, bypassing the page-cache altogether. This speeds
73830 ** and data to fit on the local page and for there to be no overflow
73837 ** page of the database. The data might change or move the next time
73855 /* There is too little space on the page for the expected amount
73868 ** b-tree page. Write the number of available bytes into *pAmt.
73885 ** Move the cursor down to a new child page. The newPgno argument is the
73886 ** page number of the child page to move to.
73888 ** This function returns SQLITE_CORRUPT if the page-header flags field of
73889 ** the new child page does not match the flags field of the parent (i.e.
73890 ** if an intkey page appears to be the parent of a non-intkey page, or
73913 ** Page pParent is an internal (non-leaf) tree page. This function
73914 ** asserts that page number iChild is the left-child if the iIdx'th
73915 ** cell in page pParent. Or, if iIdx is equal to the total number of
73916 ** cells in pParent, that page number iChild is the right-child of
73917 ** the page.
73934 ** Move the cursor up to the parent page.
73937 ** to the page we are coming from. If we are coming from the
73938 ** right-most child page then pCur->idx is set to one more than
73962 ** Move the cursor to point to the root page of its b-tree structure.
73964 ** If the table has a virtual root page, then the cursor is moved to point
73965 ** to the virtual root page instead of the actual root page. A table has a
73966 ** virtual root page when the actual root page contains no cells and a
73967 ** single child page. This can only happen with the table rooted at page 1.
73972 ** (or virtual root) page and the cursor state is set to CURSOR_VALID.
73975 ** page-header flags indicate that the [virtual] root-page is the expected
73976 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
74032 ** if the root page was already loaded when this function was called (i.e.
74034 ** in such a way that page pRoot is linked into a second b-tree table
74085 ** page to which it is currently pointing. Notice the difference
74088 ** finds the right-most entry beneath the *page*.
74184 ** left pointing at a leaf page which would hold the entry if it
74274 /* pPage->nCell must be greater than zero. If this is the root-page
74276 ** not run. If this is not the root-page, then the moveToChild() routine
74278 ** be the right kind (index or table) of b-tree page. Otherwise
74346 ** Compare the "idx"-th cell on the page the cursor pCur is currently
74376 ** b-tree page. */
74383 ** fits entirely on the main b-tree page. */
74396 ** page of a table.
74412 ** left pointing at a leaf page which would hold the entry if it
74465 ** (2) If the cursor is on the last page of the table and the first
74466 ** cell on that last page is less than or equal to the pIdxKey
74467 ** search key, then we can start the search on the current page
74490 goto bypass_moveto_root; /* Start search on the current page */
74518 /* pPage->nCell must be greater than zero. If this is the root-page
74520 ** not run. If this is not the root-page, then the moveToChild() routine
74522 ** be the right kind (index or table) of b-tree page. Otherwise
74533 /* The maximum supported page-size is 65536 bytes. This means that
74535 ** page is less than 16384 bytes and may be stored as a 2-byte
74538 ** stored entirely within the b-tree page by inspecting the first
74545 ** b-tree page. */
74552 ** fits entirely on the main b-tree page. */
74689 ** to the next cell on the current page. The (slower) btreeNext() helper
74690 ** routine is called when it is necessary to move to a different page or
74782 ** to the previous cell on the current page. The (slower) btreePrevious()
74783 ** helper routine is called when it is necessary to move to a different page
74858 ** Allocate a new page from the database file.
74860 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
74861 ** has already been called on the new page.) The new page has also
74863 ** sqlite3PagerUnref() on the new page when it is done.
74869 ** locate a page close to the page number "nearby". This can be used in an
74873 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
74875 ** eMode is BTALLOC_LT then the page returned will be less than or equal
74876 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
74877 ** are no restrictions on which page is returned.
74881 MemPage **ppPage, /* Store pointer to the allocated page here */
74882 Pgno *pPgno, /* Store the page number here */
74883 Pgno nearby, /* Search for a page near this one */
74912 ** shows that the page 'nearby' is somewhere on the free-list, then
74913 ** the entire-list will be searched for that page.
74933 ** first free-list trunk page. iPrevTrunk is initially 1.
74940 ** is not true. Otherwise, it runs once for each trunk-page on the
74941 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
74942 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
74947 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
74948 ** is the page number of the next freelist trunk page in the list or
74949 ** zero if this is the last freelist trunk page. */
74953 ** stores the page number of the first page of the freelist, or zero if
74969 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
74970 ** is the number of leaf page pointers to follow. */
74974 ** So extract the trunk page itself and use it as the newly
74975 ** allocated page */
74994 /* The list is being searched and this trunk page is the page
75015 /* The trunk page is required by the caller but it contains
75017 ** page in this case.
75121 /* There are no pages on the freelist, so append a new page to the
75128 ** steps, then the page we are about to allocate may contain content
75131 ** the current page content before overwriting it.
75134 ** content for any page that really does lie past the end of the database
75148 /* If *pPgno refers to a pointer-map page, allocate two new pages
75149 ** at the end of the file instead of one. The first allocated page
75150 ** becomes a new pointer-map page, the second is used by the caller.
75153 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
75190 ** This function is used to add page iPage to the database file free-list.
75191 ** It is assumed that the page is not already a part of the free-list.
75195 ** corresponding to page iPage handy, it may pass it as the second value.
75202 MemPage *pTrunk = 0; /* Free-list trunk page */
75203 Pgno iTrunk = 0; /* Page number of free-list trunk page */
75204 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
75223 /* Increment the free page count on pPage1 */
75242 ** to indicate that the page is free.
75251 ** trunk page in the free-list is full, then this page will become a
75252 ** new free-list trunk page. Otherwise, it will become a leaf of the
75253 ** first trunk page in the current free-list. This block tests if it
75254 ** is possible to add the page as a new free-list leaf.
75257 u32 nLeaf; /* Initial number of leaf cells on trunk page */
75276 /* In this case there is room on the trunk page to insert the page
75279 ** Note that the trunk page is not really full until it contains
75291 ** avoid using the last six entries in the freelist trunk page array in
75304 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
75310 ** the page being freed as a leaf page of the first trunk in the free-list.
75312 ** first trunk in the free-list is full. Either way, the page being freed
75313 ** will become the new first trunk page in the free-list.
75325 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
75345 MemPage *pPage, /* The page that contains the Cell */
75360 /* Cell extends past end of page */
75375 /* 0 is not a legal page number and page 1 cannot be an
75376 ** overflow page. Therefore if ovflPgno<2 or past the end of the
75389 ** to an overflow page belonging to a cell that is being deleted/updated.
75390 ** So if there exists more than one reference to this page, then it
75391 ** must not really be an overflow page and the database must be corrupt.
75393 ** freePage2() may zero the page contents if secure-delete mode is
75394 ** enabled. If this 'overflow' page happens to be a page that the
75428 ** Create the byte sequence used to represent a cell on page pPage
75440 MemPage *pPage, /* The page that contains the cell */
75482 /* This is the common case where everything fits on the btree page
75518 ** pPrior Where to write the pgno of the first overflow page
75565 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
75577 ** overflow page is being allocated, add an entry to the pointer-map
75578 ** for that page now.
75580 ** If this is the first overflow page, then write a partial entry
75634 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
75677 ** If the cell content will fit on the page, then put it there. If it
75689 int i, /* New cell becomes the i-th cell of the page */
75698 u8 *data; /* The content of the whole page */
75728 ** only occur when inserting divider cells into the parent page during
75752 ** a btree page has a value of 3 or less, the pCell value might point
75754 ** the source page. Make sure this does not cause problems by not
75770 /* The cell may contain a pointer to an overflow page. If so, write
75771 ** the entry for the overflow page into the pointer map.
75782 ** of the page that participate in the balancing operation. NB is the
75783 ** total number of pages that participate, including the target page and
75804 ** page plus up to three child pages. There are a total of nCell cells.
75839 ** For an index btree, the apEnd[] array holds pointer to the end of page
75857 ** Sometimes when deleting, a child page can have zero cells. In those
75865 MemPage *pRef; /* Reference page */
75907 ** Array apCell[] contains pointers to nCell b-tree page cells. The
75909 ** replaces the current contents of page pPg with the contents of the cell
75914 ** such cells before overwriting the page data.
75920 CellArray *pCArray, /* Content to be added to page pPg */
75922 int nCell, /* Final number of cells on page */
75923 MemPage *pPg /* The page to be reconstructed */
75987 ** This function attempts to add the cells stored in the array to page pPg.
75988 ** If it cannot (because the page needs to be defragmented before the cells
75993 ** (part of page pPg) to populate. After cell apCell[0] is written to the
75994 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
75999 ** content area on page pPg. If the size of the content area is extended,
76004 ** end of the space required by this page for the cell-pointer area (for
76019 u8 *aData = pPg->aData; /* Complete page */
76024 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
76070 ** The cell-pointers and other fields of the page are not updated.
76122 ** pCArray contains pointers to and sizes of all cells in the page being
76123 ** balanced. The current page, pPg, has pPg->nCell cells starting with
76124 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
76134 MemPage *pPg, /* Edit this page */
76135 int iOld, /* Index of first cell currently on page */
76136 int iNew, /* Index of new first cell on page */
76137 int nNew, /* Final number of cells on page */
76155 /* Remove cells from the start and end of the page */
76173 /* Add cells to the start of the page */
76204 /* Append cells to the end of the page */
76232 /* Unable to edit this page. Rebuild it from scratch instead. */
76246 ** a new page to the right-hand side and put the one new entry in
76247 ** that page. This leaves the right side of the tree somewhat
76249 ** at the end soon afterwards so the nearly empty page will quickly
76252 ** pPage is the leaf page which is the right-most page in the tree.
76254 ** which is also the right-most entry on the page.
76258 ** byte page number followed by a variable length integer. In other
76264 MemPage *pNew; /* Newly allocated page */
76276 /* Allocate a new page. This page will become the right-sibling of
76277 ** pPage. Make the parent page writable, so that the new divider cell
76307 ** with entries for the new page, and any pointer from the
76308 ** cell on the page to an overflow page. If either of these
76310 ** of the parent page are still manipulated by thh code below.
76311 ** That is Ok, at this point the parent page is guaranteed to
76313 ** rollback, undoing any changes made to the parent page.
76323 ** consists of a 4-byte page number (the page number of pPage) and
76347 /* Set the right-child pointer of pParent to point to the new page. */
76350 /* Release the reference to the new page. */
76402 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
76403 ** the pointer-map entries for each child page are updated so that the
76404 ** parent page stored in the pointer map is page pTo. If pFrom contained
76405 ** any cells with overflow page pointers, then the corresponding pointer
76406 ** map entries are also updated so that the parent page is page pTo.
76411 ** Before returning, page pTo is reinitialized using btreeInitPage().
76432 /* Copy the b-tree node content from page pFrom to page pTo. */
76437 /* Reinitialize page pTo so that the contents of the MemPage structure
76440 ** page pFrom.
76461 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
76463 ** page are used in the balancing, though both siblings might come from one
76464 ** side if the page is the first or last child of its parent. If the page
76465 ** has fewer than 2 siblings (something which can only happen if the page
76466 ** is a root page or a child of a root page) then all available siblings
76469 ** The number of siblings of the page might be increased or decreased by
76472 ** Note that when this routine is called, some of the cells on the page
76474 ** if the page is overfull. This routine ensures that all cells allocated
76475 ** to the page and its siblings fit into MemPage.aData[] before returning.
76477 ** In the course of balancing the page and its siblings, cells may be
76478 ** inserted into or removed from the parent page (pParent). Doing so
76479 ** may cause the parent page to become overfull or underfull. If this
76488 ** buffer big enough to hold one page. If while inserting cells into the parent
76489 ** page (pParent) the parent page becomes overfull, this buffer is
76491 ** a maximum of four divider cells into the parent page, and the maximum
76493 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
76500 MemPage *pParent, /* Parent page of siblings being balanced */
76501 int iParentIdx, /* Index of "the page" in pParent */
76502 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
76503 int isRoot, /* True if pParent is a root-page */
76524 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
76526 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
76528 Pgno pgno; /* Temp var to store a page number in */
76529 u8 abDone[NB+2]; /* True after i'th new page is populated */
76558 ** This loop also drops the divider cells from the parent page. This
76560 ** overflow cells in the parent page, since if any existed they will
76612 /* Drop the cell from the parent page. apDiv[i] still points to
76710 ** offset section of the btree page will be overwritten and we will no
76758 /* The right pointer of the child page pOld becomes the left
76779 ** size of all cells on the i-th page and cntNew[] which is the index
76780 ** in b.apCell[] of the cell that divides page i from page i+1.
76786 ** szNew[i]: Spaced used on the i-th sibling page.
76788 ** the right of the i-th sibling page.
76901 ** (2) pPage is a virtual root page. A virtual root page is when
76902 ** the real root page is page 1 and we are the only child of
76903 ** that page.
76938 /* Set the pointer-map entry for the new sibling page. */
76949 ** Reassign page numbers so that the new pages are in ascending order.
76971 /* If apNew[i] has a page number that is bigger than any of the
76973 ** entry that has the smallest page number (which we know to be
77009 ** of the right-most new sibling page is set to the value that was
77010 ** originally in the same field of the right-most old sibling page. */
77022 ** entry associated with the first page in the overflow chain, and
77024 ** b) if the sibling pages are not leaves, the child page associated
77053 /* Cell pCell is destined for new sibling page pNew. Originally, it
77054 ** was either part of sibling page iOld (possibly an overflow cell),
77055 ** or else the divider cell to the left of sibling page iOld. So,
77056 ** if sibling page iOld had the same page number as pNew, and if
77057 ** pCell really was a part of sibling page iOld (not a divider or
77094 ** the sibling-page assembled above only.
77135 ** is important, as this code needs to avoid disrupting any page from which
77139 ** then it is not safe to update page apNew[iPg] until after
77143 ** then it is not safe to update page apNew[iPg] until after
77146 ** If neither of the above apply, the page is safe to update.
77200 /* The root page of the b-tree now contains no cells. The only sibling
77201 ** page is the right-child of the parent. Copy the contents of the
77202 ** child page into the parent, decreasing the overall height of the
77210 ** It is critical that the child page be defragmented before being
77211 ** copied into the parent, because if the parent is page 1 then it will
77227 ** sibling page. All other pointer map entries have already been taken
77273 ** This function is called when the root page of a b-tree structure is
77276 ** A new child page is allocated and the contents of the current root
77277 ** page, including overflow cells, are copied into the child. The root
77278 ** page is then overwritten to make it an empty page with the right-child
77279 ** pointer pointing to the new page.
77282 ** that the new child-page now contains pointers to are updated. The
77284 ** page is also updated.
77287 ** page and SQLITE_OK is returned. In this case the caller is required
77293 MemPage *pChild = 0; /* Pointer to a new child page */
77294 Pgno pgnoChild = 0; /* Page number of the new child page */
77300 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
77301 ** page that will become the new right-child of pPage. Copy the contents
77302 ** of the node stored on pRoot into the new child page.
77363 ** The page that pCur currently points to has just been modified in
77388 ** (2) The amount of free space on the page is less than 2/3rds of
77389 ** the total usable space on the page. */
77393 /* The root page of the b-tree is overfull. In this case call the
77394 ** balance_deeper() function to create a new child for the root-page
77395 ** and copy the current contents of the root-page to it. The
77396 ** next iteration of the do-loop will balance the child page.
77413 /* The page being written is not a root page, and there is currently
77414 ** more than one reference to it. This only happens if the page is one
77456 ** will balance the parent page to correct this.
77458 ** If the parent page becomes overfull, the overflow cell or cells
77463 ** different page). Once this subsequent call to balance_nonroot()
77466 ** copied either into the body of a database page or into the new
77489 /* The next iteration of the do-loop balances the parent page. */
77555 Pgno ovflPgno; /* Next overflow page to write */
77556 u32 ovflPageSize; /* Size to write on overflow page */
77663 ** than one table or index with the same root page as used by the cursor.
77797 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
77880 ** back to the root page as balance() used to invalidate the contents
78128 /* If the page containing the entry to delete is not a leaf page, move
78133 ** sub-tree headed by the child page of the cell being deleted. This makes
78154 /* Make the page containing the entry to be deleted writable. Then free any
78156 ** itself from within the page. */
78163 /* If the cell deleted was not located on a leaf page, then the cursor
78165 ** by the child-page of the cell that was just deleted from an internal
78197 /* Balance the tree. If the entry deleted was located on a leaf page,
78198 ** then the cursor still points to that page. In this case the first
78202 ** Otherwise, if the entry deleted was on an internal node page, then
78203 ** pCur is pointing to the leaf page from which a cell was removed to
78215 /* Optimization: If the free space is less than 2/3rds of the page,
78256 ** Create a new BTree table. Write into *piTable the page
78257 ** number for the root page of the new table.
78271 int ptfFlags; /* Page-type flage for the root page of new table */
78284 Pgno pgnoMove; /* Move a page here to make room for the root-page */
78285 MemPage *pPageMove; /* The page to move to. */
78288 ** to make room for the new tables root page. In case this page turns
78289 ** out to be an overflow page, delete all overflow page-map caches
78295 ** root page of the new table should go. meta[3] is the largest root-page
78296 ** created so far, so the new root-page is (meta[3]+1).
78304 /* The new root-page may not be allocated on a pointer-map page, or the
78305 ** PENDING_BYTE page.
78313 /* Allocate a page. The page that currently resides at pgnoRoot will
78314 ** be moved to the allocated page (unless the allocated page happens
78323 /* pgnoRoot is the page that will be used for the root-page of
78326 ** by extending the file), the current page at position pgnoMove
78334 ** corresponding to page pgnoRoot. */
78341 /* Move the page currently at pgnoRoot to pgnoMove. */
78359 /* Obtain the page at pgnoRoot */
78376 /* Update the pointer-map and meta-data with the new root-page number. */
78383 /* When the new root page was allocated, page 1 was made writable in
78420 ** Erase the given database page and all its children. Return
78421 ** the page to the freelist.
78426 int freePageFlag, /* Deallocate page if true */
78480 ** the page number of the root of the table. After this routine returns,
78481 ** the root page is empty, but still exists.
78523 ** page 1) is never added to the freelist.
78528 ** If AUTOVACUUM is enabled and the page at iTable is not the last
78529 ** root page in the database file, then the last root page
78531 ** iTable and that last slot formerly occupied by the last root page
78535 ** page number that used to be the last root page in the file before
78536 ** the move. If no page gets moved, *piMoved is set to 0.
78537 ** The last root page is recorded in meta[3] and the value of
78571 /* If the table being dropped is the table with the largest root-page
78572 ** number in the database, put the root page on the free list.
78580 /* The table being dropped does not have the largest root-page
78581 ** number in the database. So move the page that does into the
78582 ** gap left by the deleted root-page.
78605 /* Set the new 'max-root-page' value in the database header. This
78607 ** be a root-page number, less one again if that is the
78727 ** page in the B-Tree structure (not including overflow pages).
78731 MemPage *pPage; /* Current page of the b-tree */
78733 /* If this is a leaf page or the tree is not an int-key tree, then
78734 ** this page contains countable entries. Increment the entry counter
78744 ** the next page in the tree that has not yet been visited. The
78746 ** of the page, or to the number of cells in the page if the next page
78821 ** corresponds to page iPg is already set.
78829 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
78838 ** Add 1 to the reference count for page iPage. If this is the second
78839 ** reference to the page, add an error message to pCheck->zErrMsg.
78840 ** Return 1 if there are 2 or more references to the page and 0 if
78841 ** if this is the first reference to the page.
78843 ** Also check that the page number is in bounds.
78847 checkAppendMsg(pCheck, "invalid page number %d", iPage);
78851 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
78861 ** Check that the entry in the pointer-map for page iChild maps to
78862 ** page iParent, pointer type ptrType. If not, append an error message
78867 Pgno iChild, /* Child page number */
78869 Pgno iParent /* Expected pointer map parent page number */
78891 ** Check the integrity of the freelist or of an overflow page list.
78896 int isFreeList, /* True for a freelist. False for overflow page list */
78897 Pgno iPage, /* Page number for first page in the list */
78909 checkAppendMsg(pCheck, "failed to get page %d", iPage);
78922 "freelist leaf count too big on page %d", iPage);
78940 ** page in this overflow list, check that the pointer-map entry for
78941 ** the following page matches iPage.
78979 ** entry represents the span of a cell or freeblock on a btree page.
79014 ** Do various sanity checks on a single page of a tree. Return
79021 ** but combine to completely cover the page.
79029 Pgno iPage, /* Page number of the page to check */
79033 MemPage *pPage = 0; /* The page being analyzed */
79038 int nFrag; /* Number of fragmented bytes on the page */
79039 int hdr; /* Offset to the page header */
79050 u32 usableSize; /* Usable size of the page */
79059 /* Check that the page exists
79069 "unable to get the page. error code=%d", rc);
79092 pCheck->zPfx = "On tree page %u cell %d: ";
79097 ** number of cells on the page. */
79101 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
79102 ** immediately follows the b-tree page header. */
79108 /* Analyze the right-child page of internal pages */
79112 pCheck->zPfx = "On page %u at right child: ";
79144 checkAppendMsg(pCheck, "Extends off end of page");
79155 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
79161 Pgno pgnoOvfl; /* First page of the overflow chain */
79174 /* Check sanity of left child page for internal pages */
79184 checkAppendMsg(pCheck, "Child page depth differs");
79194 /* Check for complete coverage of the page
79213 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
79215 ** freeblocks on the page.
79225 ** big-endian integer which is the offset in the b-tree page of the next
79239 ** There is an implied first entry the covers the page header, the cell
79253 "Multiple uses for byte %u of page %u", x>>16, iPage);
79262 ** is stored in the fifth field of the b-tree page header.
79268 "Fragmentation of %d bytes reported as %d on page %u",
79286 ** an array of pages numbers were each page number is the root page of
79301 ** and the checks to make sure every page is referenced are also skipped,
79412 /* Make sure every page in the file is referenced
79430 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
79571 ** Obtain a lock on the table whose root page is iTab. The
79622 ** version of the b-tree page modified by the accessPayload call below.
79711 ** Return the size of the header added to each page by this module.
79773 Pgno iNext; /* Page number of the next source page to copy */
79802 ** the page cache associated with the source database. The mutex
79855 ** Attempt to set the page size of the destination to match the page size
79969 ** page iSrcPg from the source database. Copy this data into the
79974 Pgno iSrcPg, /* Source database page to backup */
79975 const u8 *zSrcData, /* Source database page data */
80000 ** page sizes of the source and destination differ.
80006 /* Backup is not possible if the page size of the destination is changing
80024 /* This loop runs once for each destination page spanned by the source
80025 ** page. For each iteration, variable iOff is set to the byte offset
80026 ** of the destination page.
80039 /* Copy the data from the source page into the destination page.
80042 ** of the page 'extra' space to invalidate the Btree layers
80043 ** cached parse of the page). MemPage.isInit is marked
80094 int pgszSrc = 0; /* Source page size */
80095 int pgszDest = 0; /* Destination page size */
80134 ** try to set its page size to the same as the source database. This
80136 ** not possible to create a database file that uses one page size by
80151 ** and the page sizes are different between source and destination */
80165 const Pgno iSrcPg = p->iNext; /* Source page number */
80167 DbPage *pSrcPg; /* Source page object */
80210 ** database. The complication here is that the destination page
80211 ** size may be different to the source page size.
80213 ** If the source page size is smaller than the destination page size,
80217 ** destination file that lie beyond the nDestTruncate page mark are
80235 /* If the source page-size is smaller than the destination page-size,
80241 ** pending-byte page in the source database may need to be
80424 ** This function is called after the contents of page iPage of the
80425 ** source database have been modified. If page iPage has already been
80444 /* The backup process p has already copied page iPage. But now it
81823 u32 available = 0; /* Number of bytes available on the local btree page */
93240 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
93279 ** than a few bytes. Even if the record occurs at the end of the page
93280 ** content area, the "page header" comes after the page content and so
93389 ** page - where the content is not on an overflow page */
93951 ** every btree page of the table. But if P3 is non-zero, an estimate
94441 ** Open a read-only cursor for the database table whose root page is
94492 ** page is P2 (or whose root page is held in register P2 if the
94514 ** as the root page, not the value of P2 itself.
94613 ** SQLite used to check if the root-page flags were sane at this point
94743 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
96230 ** into the database page. That means that the content of the output
96993 ** Delete an entire database table or index whose root page in the database
97000 ** If AUTOVACUUM is enabled then it is possible that another root page
97001 ** might be moved into the newly deleted root page in order to keep all
97003 ** value of the root page that moved - its value before the move occurred -
97004 ** is stored in register P2. If no page movement was required (because the
97011 ** updating existing cursors when a root page is moved in an AUTOVACUUM
97053 ** Delete all contents of the database table or index whose root page
97120 ** The root page number of the new b-tree is stored in register P2.
97304 ** The root page numbers of all tables in the database are integers
98256 ** P2 contains the root-page of the table to lock.
98719 ** Try to set the maximum page count for database P1 to the value in P3.
98720 ** Do not let the maximum page count fall below the current page count and
98721 ** do not change the maximum page count value if P3==0.
98723 ** Store the maximum page count after the change in register P2.
99863 ** page-cache of the main database. Specifically, the threshold is set to
100097 int pgsz; /* Main database page size */
100186 ** size. But I/O is more efficient if it occurs in page-sized blocks where
100187 ** each block is aligned on a page boundary. This object caches writes to
100188 ** the PMA so that aligned, page-size blocks are written.
101579 ** than (page-size * cache-size), or
101582 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
103144 ** journal data to roll back changes made to the internal page-cache
117491 Pgno iTab; /* The root page of the table to be locked */
117499 ** The table to be locked has root page iTab and is found in database iDb.
117509 Pgno iTab, /* Root page number of the table to be locked */
117545 Pgno iTab, /* Root page number of the table to be locked */
118839 ** The root page number of the new table is left in reg pParse->regRoot.
118840 ** The rowid and root page number values are needed by the code that
119902 /* The root page of the PRIMARY KEY is the table root page */
120112 ** So do not write to the disk again. Extract the root page number
120113 ** for the table from the db->init.newTnum field. (The page number
120116 ** If the root page number is 1, that means this is the sqlite_schema
120274 ** statement to populate the new table. The root-page number for the
120675 ** used by SQLite when the btree layer moves a table root page. The
120676 ** root-page of a table or index in database iDb has changed from iFrom
120716 ** Write code to erase the table with root-page iTable from database iDb.
120718 ** if a root-page of another table is moved by the btree-layer whilst
120729 ** is non-zero, then it is the root page number of a table moved to
120748 ** in case a root-page belonging to another table is moved by the btree layer
120755 ** largest root-page number. This guarantees that none of the root-pages
120763 ** and root page 5 happened to be the largest root-page number in the
120764 ** database, then root page 5 would be moved to page 4 by the
120766 ** a free-list page.
121202 ** root page number of the index. If memRootPage is negative, then
121204 ** the root page number of the index is taken from pIndex->tnum.
121213 Pgno tnum; /* Root page of index */
134751 ** page cache size. The value returned is the maximum number of
134752 ** pages in the page cache. The second form sets both the current
134753 ** page cache size value and the persistent page cache size value
134803 ** database page size in bytes. The second form sets the
134804 ** database page size value. The value can only be set if
134814 /* Malloc may fail when setting the page-size, as there is an internal
135085 ** page cache size. The second form sets the local
135086 ** page cache size value. If N is positive then that is the
135109 ** page cache spill size. The second form turns cache spill on
135119 ** cache_size pages, no spilling occurs until the page count exceeds
135942 int *aRoot; /* Array of root page numbers of all btrees */
137201 ** of pIndex has the same root page number, and if it does, return true.
137234 ** argv[3] = root page number for table or index. 0 for trigger or view.
137313 ** to do here is record the root page number for that index.
137417 ** meta[2] Size of the page cache.
145982 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
149601 ** original database is required. Every page of the database is written
149603 ** Two writes per page are required in step (3) because the original
149670 int nRes; /* Bytes of reserved space at the end of each page */
149770 ** to ensure that we do not try to change the page-size on a WAL database.
149777 /* Do not attempt to change the page size for a WAL database */
149864 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
149876 ** we already have page 1 loaded into cache and marked dirty. */
173924 ** the size of each page cache line (sz), and the number of cache lines
173934 ** that integer the number of extra bytes per page required for each page
173956 ** object. This object specifies the interface to a custom page cache
173964 ** object. SQLite copies of the current page cache implementation into
177212 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
177801 ** or off. "tnum" is the root page of the b-tree to which the imposter
180388 ** Store the current database page-size in bytes in p->nPgsz.
181158 /* Figure out the page-size for the database. This is required in order to
181541 int isFirstTerm = 1; /* True when processing first term on page */
184572 ** of data that will fit on a single leaf page of an intkey table in
192132 i64 nReq; /* Number of bytes required on leaf page */
193605 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
194560 Blob root = {0,0,0}; /* New root page image */
200999 ** child page.
201865 ** an internal node, then the 64-bit integer is a child page number.
203147 RtreeNode **ppLeaf /* OUT: Selected leaf page */
204611 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
204612 ** This ensures that each node is stored on a single database page. If the
204613 ** database page-size is so large that more than RTREE_MAXCELLS entries
205066 ** Argument pCell points to an array of coordinates stored on an rtree page.
205072 ** the array of coordinates on the parent page that bound the page
205142 int nCell; /* Number of cells on page */
208286 ** The problem is that in order to update a b-tree, the leaf page (at least)
208298 ** and all but guarantees each modified leaf page is loaded and stored
208791 ** sqlite3rbu_step() will copy a page of data from the *-wal file into
209089 int iTnum; /* Root page of current object */
209140 ** file should be copied to page iDbPage of the database file.
209972 ** is set to the root page number of the primary key index before
210851 int tnum = pIter->iPkTnum; /* Root page of PK index */
211781 ** The checksum is store in the first page of xShmMap memory as an 8-byte
211802 ** (wal frame -> db page) copy operations required to checkpoint the
211834 ** do not perform any IO. Instead, the frame/page combinations that
211848 ** array populated with a set of (frame -> page) mappings. Because the
211925 ** Called when a page of data is written to offset iOff of the database
211926 ** file while the rbu handle is in capture mode. Record the page number
211927 ** of the page being written in the aFrame[] array.
212309 ** throw an SQLITE_SCHEMA exception. The only database page the
212310 ** statement reads is page 1, which is guaranteed to be in the cache.
212554 ** However, if the sector-size is larger than the page-size, and the
212557 ** then the database page written here may be damaged. Work around
212558 ** this by checkpointing frames until the next page in the aFrame[]
212864 ** page 1 of the database file. */
213238 ** 1. Whenever the first page of a main database file is read or
213284 ** Instead the frame and page numbers that would be read and written
213467 /* If this is being called to read the first page of the target
213469 ** contents of the first page if it does not yet exist. Otherwise,
213481 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
213483 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
213585 ** pretend that it has at least one page. Otherwise, SQLite will not
213746 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
213958 ** in wal mode, even if the first page of the database file has
214204 ** Additional information is available on the "dbstat.html" page of the
214214 ** always approximately 200 bytes of addressable memory following each page
214216 ** do not cause undefined behaviour. This module pads each page buffer
214225 ** root-node of the b-tree structure to each page. The value of the
214228 ** The value of the path for the left-most child page of the root of
214231 ** The next to left-most child of the root page is
214232 ** '/001', and so on, each sibling page identified by a 3-digit hex
214239 ** the left-most cell of the 450th child of the root page are identified
214242 ** '/1c2/000+000000' // First page in overflow chain
214243 ** '/1c2/000+000001' // Second page in overflow chain
214244 ** '/1c2/000+000002' // Third page in overflow chain
214248 ** sort-order than its child page:
214255 " path TEXT," /* 1 Path to page from root (NULL for agg) */
214256 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
214258 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
214259 " payload INTEGER," /* 5 Bytes of payload on this page */
214260 " unused INTEGER," /* 6 Bytes of unused space on this page */
214262 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
214263 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
214275 /* Size information for a single cell within a btree page */
214280 u32 *aOvfl; /* Array of overflow page numbers */
214281 int nLastOvfl; /* Bytes of payload on final overflow page */
214285 /* Size information for a single btree page */
214290 char *zPath; /* Path to this page */
214294 int nCell; /* Number of cells on page */
214295 int nUnused; /* Number of unused bytes on page */
214297 u32 iRightChildPg; /* Right-child page number (or 0) */
214298 int nMxPayload; /* Largest payload of any cell on the page */
214309 StatPage aPage[32]; /* Pages in path to current page */
214512 ** important that statClearPage() is called to free any page refs before
214548 ** For a single cell on a btree page, compute the number of bytes of
214549 ** content (payload) stored on that page. That is to say, compute the
214553 int nUsable, /* Usable bytes per page */
214575 ** cells found on the page currently under analysis.
214618 int nUsable; /* Usable bytes per page */
214699 /* If connected to a ZIPVFS backend, find the page size and
214708 /* Not ZIPVFS: The default page size and offset */
214715 ** Load a copy of the page data for page iPg into the buffer belonging
214716 ** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
214720 Btree *pBt, /* Load page from this b-tree */
214722 StatPage *pPg /* Load page into this object */
214748 ** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
214898 ** next page. The loop will exit via the return at label-statNext-done
215117 ** value must be a BLOB which is the correct page size, otherwise the
215130 int pgno; /* Current page number */
215131 int mxPgno; /* Last page to visit on this scan */
215135 int szPage; /* Size of each page in bytes */
215365 /* The pending byte page. Assume it is zeroed out. Attempting to
215366 ** request this page from the page is an SQLITE_CORRUPT error. */
215431 zErr = "bad page number";
215438 zErr = "bad page value";
221995 ** field that occurs at the start of each leaf page (see fts5_index.c). */
222132 int pgsz; /* Approximate page size used in %_data */
225431 /* Maximum allowed page size */
230034 ** * all segment b-tree leaf data is stored in fixed size page records
230041 ** records that contain a copy of the first rowid on each page spanned by
230095 ** + first leaf page number (often 1, always greater than 0)
230096 ** + final leaf page number
230111 ** on the leaf page, is:
230152 ** Each leaf page begins with a 4-byte header containing 2 16-bit
230155 ** * The byte offset of the first rowid on the page, if it exists
230158 ** * The byte offset of the start of the page footer. If the page
230160 ** size of the leaf page in bytes.
230162 ** The page footer consists of a single varint for each term located
230163 ** on the page. Each varint is the byte offset of the current term
230164 ** within the page, delta-compressed against the previous value. In
230170 ** term/doclist data fits on a single leaf page. If this is not the case,
230173 ** + if the first rowid on a page occurs before the first term, it
230178 ** + the first term on each page is stored in the same way as the
230187 ** a single leaf record only. The format of each doclist index leaf page
230191 ** 0x01: Clear if leaf is also the root page, otherwise set.
230193 ** * Page number of fts index leaf page. As a varint.
230195 ** * First rowid on page indicated by previous field. As a varint.
230197 ** * A list of varints, one for each subsequent termless page. A
230198 ** positive delta if the termless page contains at least one rowid,
230204 ** 0x01: Clear for root page, otherwise set.
230206 ** * Page number of first child page. As a varint.
230208 ** * Copy of first rowid on page indicated by previous field. As a varint.
230211 ** child page.
230229 ** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
230235 #define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
230276 int szLeaf; /* Size of leaf without page-index */
230330 int pgnoFirst; /* First leaf page number in segment */
230331 int pgnoLast; /* Last leaf page number in segment */
230350 int pgno; /* Page number for this page */
230353 Fts5Buffer pgidx; /* Buffer containing page-index */
230354 Fts5Buffer term; /* Buffer containing previous term on page */
230357 int pgno; /* Page number for this page */
230359 i64 iPrev; /* Previous rowid value written to page */
230360 Fts5Buffer buf; /* Buffer containing page data */
230367 u8 bFirstRowidInPage; /* True if next rowid is first in page */
230395 ** Current leaf page number within segment.
230402 ** is the last thing on the leaf page.
230405 ** Buffer containing current leaf page data. Set to NULL at EOF.
230408 ** Leaf page number containing the last term read from the segment. And
230426 ** For each rowid on the page corresponding to the current term, the
230428 ** start of the "position-list-size" field within the page.
230436 int iLeafPgno; /* Current leaf page number */
230438 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
230444 /* The page and offset from which the current term was read. The offset
230468 ** leaf page.
230479 ** page. This macro evaluates to true if the leaf contains no terms, or
230545 Fts5Data *pData; /* Data for current page of this level */
230551 int iLeafPgno; /* Page number of current leaf page */
231336 ** doclist-index page is reached, return non-zero.
231402 ** When this function is called pIter->iLeafPgno is the page number the
231421 /* Advance each level to the last entry on the last page */
231534 int iLeafPg /* Leaf page number to load dlidx for */
231585 ** Load the next leaf page into the segment iterator.
231589 Fts5SegIter *pIter /* Iterator to advance to next page */
231799 ** the position-list size field for the first relevant rowid on the page.
231803 ** relevant rowid on the page and, if necessary, initializes the
231875 ** thing on the page - i.e. the first rowid is on the following page.
231975 /* Next entry is on the next page */
231984 /* Next entry is on the current page */
232048 /* Search for the end of the position list within the current page. */
232056 /* The next entry is on the current page. */
232095 /* Next entry is not on the current page */
232189 ** term may not be stored on the current page. So search forward to
232195 /* The last rowid in the doclist may not be on the current page. Search
232196 ** forward to find the page containing the last rowid. */
232216 ** lies on the page currently indicated by the iterator. In this case
232218 ** field associated with the first relevant rowid on the page.
232220 ** Or, if pLast is non-NULL, then it is the page that contains the last
232222 ** first rowid on this page.
232250 ** page. If the current term is the last term on the page, load the
232261 /* Check if the current doclist ends on this page. If it does, return
232276 ** function searches the leaf page for a term matching (pTerm/nTerm).
232278 ** If the specified term is found on the page, then the iterator is left
232285 ** current page.
232449 /* This block sets stack variable iPg to the leaf page number that may
232729 ** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
233297 /* All data is stored on the current page. Populate the output
233298 ** variables to point into the body of the page object. */
233322 ** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
233390 /* All data is stored on the current page. Populate the output
233391 ** variables to point into the body of the page object. */
233799 ** last term on leaf page (pWriter->iBtPage) is completed.
233806 ** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
233829 ** This is called once for each leaf page except the first that contains
233840 int nTerm, const u8 *pTerm /* First term on new page */
233850 ** This function is called when flushing a leaf page that contains no
233857 /* If there were no rowids on the leaf page either and the doclist-index
233879 ** Rowid iRowid has just been appended to the current leaf page. It is the
233880 ** first on the page. This function appends an appropriate entry to the current
233896 /* The current doclist-index page is full. Write it to disk and push
233898 ** doclist-index leaf page) up into the next level of the b-tree
233955 /* No term was written to this page. */
233959 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
233963 /* Write the page out to disk */
233967 /* Initialize the next page. */
234003 /* If the current leaf page is full, flush it to disk. */
234052 ** to the page. */
234082 /* If this is to be the first rowid written to the page, set the
234083 ** rowid-pointer in the page-header. Also append a value to the dlidx
234198 /* Initialize the 4-byte leaf-page header to 0x00. */
234224 ** Set both the first and last page-numbers to 0 to indicate that the
234229 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
234240 ** a single page has been assigned to more than one segment. In
234256 /* Set up the new page-index array */
234401 /* Flush the last leaf page to disk. Set the output segment b-tree height
234402 ** and last leaf page number at the same time. */
234582 int pgnoLast = 0; /* Last leaf page number in segment */
234594 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
234640 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
236123 ** is also a rowid pointer within the leaf page header, it points to a
236180 /* Check that the leaf page indicated by the iterator really does
236212 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
236599 Fts5Buffer term; /* Current term read from page */
236647 Fts5Buffer term; /* Current term read from page */
236672 /* Decode the position list tail at the start of the page */
236686 /* Decode any more doclist data that appears on the page before the
236696 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
245312 sqlite3_log(SQLITE_NOMEM, "codec: alloc mem failed when set cipher page size(%d).", size);
245619 sqlite3_log(SQLITE_ERROR, "codec: check hmac error at page %d, hmac %d, kdf %d, pageSize %d.",
245805 PgHdr *page = NULL;
245829 rc = sqlite3PagerGet(pPager, pgno, &page, 0);
245831 rc = sqlite3PagerWrite(page);
245833 sqlite3PagerUnref(page);
245835 sqlite3_log(SQLITE_WARNING, "sqlite3_rekey_v2: error when writing page %d: errno = %d.", pgno, rc);
245838 sqlite3_log(SQLITE_WARNING, "sqlite3_rekey_v2: error when reading page %d: errno = %d.", pgno, rc);