-- GIN pending-list cleanup ignores the vacuum BufferAccessStrategy.

create extension if not exists pg_buffercache;
create extension if not exists pageinspect;

create table gt (a int[]);
-- fastupdate plus an absurd pending_list_limit (kB) so the inserts never
-- clean up after themselves and VACUUM is left holding the whole list
create index gt_idx on gt using gin (a)
  with (fastupdate = on, gin_pending_list_limit = 1048576);
insert into gt select array[i, i+1, i+2] from generate_series(1, 200000) i;

select n_pending_pages from gin_metapage_info(get_raw_page('gt_idx', 0));  -- 1471 here

select buffers_evicted from pg_buffercache_evict_relation('gt_idx');  -- cold start
vacuum gt;

-- Unpatched, resident comes back as the entire index.  Patched, resident
-- is short by about n_pending_pages. I get 3235/3235 unpatched vs 2020/3235 patched.
-- relforknumber = 0 because pg_relation_size() is the main fork only,
-- while relfilenode alone would also match the 3 cached free space map pages.
select (select count(*) from pg_buffercache
         where relfilenode = pg_relation_filenode('gt_idx')
           and relforknumber = 0) as resident,
       pg_relation_size('gt_idx') / current_setting('block_size')::int as total;
