#!/bin/bash -x

# MERGE concurrent pgbench test
# Greg Smith, 2ndQuadrant

# Setup pgbench database
dropdb pgbench
createdb pgbench
pgbench -i -s 1 pgbench

# The function call version of the test code isn't working, so
# this doesn't do anything yet
#
# psql -f setup-function.sql -d pgbench

# This should return 0 rows
psql -c "select * FROM pgbench_accounts WHERE aid > 100000 ORDER BY aid" -d pgbench

pgbench -f update-merge.sql -t 10000 -c 8 -j 4 -s 2 pgbench

psql -c "select count(*) as updated FROM pgbench_accounts WHERE NOT abalance=0" -d pgbench

# Do this to actually list the nodes that have been touched.
# Since this should start empty, it's not releveant right now
#
# psql -c "select * FROM pgbench_accounts WHERE NOT abalance=0 ORDER BY aid" -d pgbench

# Count the number of new entries added by the INSERT path of MERGE
psql -c "select count(*) as inserted FROM pgbench_accounts WHERE aid > 100000" -d pgbench

# This actually shows each of the newly inserted rows.  If you adjust
# statement logging, it's possible to see each of the new rows as they
# get inserted, which then allows confirming by hand whether the
# new balances have been correctly updated.
#
# psql -c "select * FROM pgbench_accounts WHERE aid > 100000 ORDER BY aid" -d pgbench
