#!/bin/sh
# Define parameters

export SCALEFACTOR=10

export PGHOST=localhost
export PGPORT=5415
export PGUSER=postgres
export PGDATABASE=testcase

dropdb $PGDATABASE
createdb $PGDATABASE

# Execute the psql script
psql -a -v p_scaleFactor=$SCALEFACTOR <<**PSQL**

select version();
\timing on
\set ON_ERROR_STOP
SET client_min_messages TO WARNING;

--> create structures
create table perf (
  c1    integer    not null,
  seq   serial     not null,
  c2    text
);

--> populate the table
insert into perf (c1, c2) select i, rpad('2',300) from generate_series (1, 1000*:p_scaleFactor) i;

--> perform changes
insert into perf (c1,c2) select c1, 'updated' from perf where c1 % 5 = 0;

--> vacuum and analyze
select count(*) from perf;
vacuum analyze perf;

--> bad estimate of the number of output rows (always 1)
explain (analyze, buffers, verbose)
 WITH keys AS (
   SELECT c1, min(seq) AS seq
     FROM perf
     GROUP BY c1
   )
   SELECT tbl.*
     FROM perf tbl
          JOIN keys ON (keys.c1 = tbl.c1 AND keys.seq = tbl.seq)
;

**PSQL**

