diff --git a/pgxn/neon/file_cache.c b/pgxn/neon/file_cache.c index 97a4c39e49..2505fcb847 100644 --- a/pgxn/neon/file_cache.c +++ b/pgxn/neon/file_cache.c @@ -1563,8 +1563,12 @@ local_cache_pages(PG_FUNCTION_ARGS) hash_seq_init(&status, lfc_hash); while ((entry = hash_seq_search(&status)) != NULL) { - for (int i = 0; i < BLOCKS_PER_CHUNK; i++) - n_pages += GET_STATE(entry, i) == AVAILABLE; + /* Skip hole tags */ + if (NInfoGetRelNumber(BufTagGetNRelFileInfo(entry->key)) != 0) + { + for (int i = 0; i < BLOCKS_PER_CHUNK; i++) + n_pages += GET_STATE(entry, i) == AVAILABLE; + } } } } @@ -1592,16 +1596,19 @@ local_cache_pages(PG_FUNCTION_ARGS) { for (int i = 0; i < BLOCKS_PER_CHUNK; i++) { - if (GET_STATE(entry, i) == AVAILABLE) + if (NInfoGetRelNumber(BufTagGetNRelFileInfo(entry->key)) != 0) { - fctx->record[n].pageoffs = entry->offset * BLOCKS_PER_CHUNK + i; - fctx->record[n].relfilenode = NInfoGetRelNumber(BufTagGetNRelFileInfo(entry->key)); - fctx->record[n].reltablespace = NInfoGetSpcOid(BufTagGetNRelFileInfo(entry->key)); - fctx->record[n].reldatabase = NInfoGetDbOid(BufTagGetNRelFileInfo(entry->key)); - fctx->record[n].forknum = entry->key.forkNum; - fctx->record[n].blocknum = entry->key.blockNum + i; - fctx->record[n].accesscount = entry->access_count; - n += 1; + if (GET_STATE(entry, i) == AVAILABLE) + { + fctx->record[n].pageoffs = entry->offset * BLOCKS_PER_CHUNK + i; + fctx->record[n].relfilenode = NInfoGetRelNumber(BufTagGetNRelFileInfo(entry->key)); + fctx->record[n].reltablespace = NInfoGetSpcOid(BufTagGetNRelFileInfo(entry->key)); + fctx->record[n].reldatabase = NInfoGetDbOid(BufTagGetNRelFileInfo(entry->key)); + fctx->record[n].forknum = entry->key.forkNum; + fctx->record[n].blocknum = entry->key.blockNum + i; + fctx->record[n].accesscount = entry->access_count; + n += 1; + } } } } diff --git a/test_runner/regress/test_lfc_resize.py b/test_runner/regress/test_lfc_resize.py index 51074751e0..83fd3aa719 100644 --- a/test_runner/regress/test_lfc_resize.py +++ b/test_runner/regress/test_lfc_resize.py @@ -49,6 +49,8 @@ def test_lfc_resize(neon_simple_env: NeonEnv, pg_bin: PgBin): conn = endpoint.connect() cur = conn.cursor() + cur.execute("create extension neon") + def get_lfc_size() -> tuple[int, int]: lfc_file_path = endpoint.lfc_path() lfc_file_size = lfc_file_path.stat().st_size @@ -103,3 +105,23 @@ def test_lfc_resize(neon_simple_env: NeonEnv, pg_bin: PgBin): time.sleep(1) assert int(lfc_file_blocks) <= 128 * 1024 + + # Now test that number of rows returned by local_cache is the same as file_cache_used_pages. + # Perform several iterations to make cache cache content stabilized. + nretries = 10 + while True: + cur.execute("select count(*) from local_cache") + local_cache_size = cur.fetchall()[0][0] + + cur.execute( + "select lfc_value::bigint FROM neon_lfc_stats where lfc_key='file_cache_used_pages'" + ) + used_pages = cur.fetchall()[0][0] + + if local_cache_size == used_pages or nretries == 0: + break + + nretries = nretries - 1 + time.sleep(1) + + assert local_cache_size == used_pages