\echo 'GIN WAL diff test'
\o /dev/null
SELECT setseed(.43);
CREATE TABLE gin_test_tbl(i int4[]) with (autovacuum_enabled = off);

INSERT INTO gin_test_tbl SELECT array[random()*100000, random()*1000, g] FROM generate_series(1, 2000000) g;

SELECT  pg_current_wal_insert_lsn() AS prev_lsn; \gset
CREATE INDEX gin_test_idx ON gin_test_tbl USING gin (i)
  WITH (fastupdate = on, gin_pending_list_limit = 4096);
SELECT  pg_current_wal_insert_lsn()::pg_lsn AS lsn; \gset
SELECT pg_wal_lsn_diff(:'lsn'::pg_lsn, :'prev_lsn'::pg_lsn) AS wal_diff; \gset
\o
SELECT pg_size_pretty(:wal_diff::numeric);

-- 
\echo 'GIST WAL diff test'
\o /dev/null
SELECT setseed(.43);
CREATE TABLE gist_point_tbl(id int4, p point);

INSERT INTO gist_point_tbl (id, p)
SELECT g, point(random()*1000, random()*1000) FROM generate_series(1, 1000000) g;

SELECT  pg_current_wal_insert_lsn() AS prev_lsn; \gset
CREATE INDEX gist_pointidx ON gist_point_tbl USING gist(p);
SELECT  pg_current_wal_insert_lsn()::pg_lsn AS lsn; \gset
SELECT pg_wal_lsn_diff(:'lsn'::pg_lsn, :'prev_lsn'::pg_lsn) AS wal_diff; \gset
\o
SELECT pg_size_pretty(:wal_diff::numeric);

-- 
\echo 'SP-GIST WAL diff test'
\o /dev/null

drop table spgist_point_tbl;
create table spgist_point_tbl(id int4, p point);

SELECT setseed(.43);

INSERT INTO spgist_point_tbl (id, p)
SELECT g, point(random()*1000, random()*1000) FROM generate_series(1, 1000000) g;

SELECT  pg_current_wal_insert_lsn() AS prev_lsn; \gset
CREATE INDEX spgist_point_idx ON spgist_point_tbl USING spgist(p);
SELECT  pg_current_wal_insert_lsn()::pg_lsn AS lsn; \gset
SELECT pg_wal_lsn_diff(:'lsn'::pg_lsn, :'prev_lsn'::pg_lsn) AS wal_diff; \gset
\o
SELECT pg_size_pretty(:wal_diff::numeric);

/*
master:
=======

GIN WAL diff test
 pg_size_pretty 
----------------
 347 MB
(1 row)

GIST WAL diff test
 pg_size_pretty 
----------------
 157 MB
(1 row)

SP-GIST WAL diff test
 pg_size_pretty 
----------------
 119 MB
(1 row)

patched:
========

GIN WAL diff test
 pg_size_pretty 
----------------
 66 MB
(1 row)

GIST WAL diff test
 pg_size_pretty 
----------------
 43 MB
(1 row)

SP-GIST WAL diff test
 pg_size_pretty 
----------------
 38 MB
(1 row)

*/
