#!/bin/sh

# test_xlogdirtywrite.sh

PGHOME=/tmp/pgsql
PATH=$PGHOME/bin:$PATH
export PATH

pgbench -s 10 -i postgres

psql -e postgres << EOF
SELECT pg_sleep(1);
SELECT * FROM pg_stat_bgwriter;
SELECT * FROM pg_stat_walwriter;
\q
EOF

pgbench -s 10 -c 32 -t 1000 postgres

psql -e postgres << EOF
CHECKPOINT;
SELECT pg_sleep(1);
SELECT * FROM pg_stat_bgwriter;
SELECT * FROM pg_stat_walwriter;

--
-- generates wal records in single transaction
-- to fill the wal buffers.
--
BEGIN;
DELETE FROM pgbench_accounts;
COMMIT;
CHECKPOINT;

--
-- confirm that those shared counters got increased.
--
SELECT pg_sleep(1);
SELECT * FROM pg_stat_bgwriter;
SELECT * FROM pg_stat_walwriter;

--
-- pg_stat_reset() does not affect to the shared counters.
--
SELECT pg_stat_reset();
SELECT pg_sleep(1);
SELECT * FROM pg_stat_bgwriter;
SELECT * FROM pg_stat_walwriter;

--
-- resetting the walwriter counter.
--
SELECT pg_stat_reset_shared('walwriter');
SELECT pg_sleep(1);
SELECT * FROM pg_stat_bgwriter;
SELECT * FROM pg_stat_walwriter;

--
-- resetting the bgwriter counters.
--
SELECT pg_stat_reset_shared('bgwriter');
SELECT pg_sleep(1);
SELECT * FROM pg_stat_bgwriter;
SELECT * FROM pg_stat_walwriter;

\q
EOF
