
create language plpgsql;

drop schema if exists test cascade;

drop user if exists test;

create user test;

create schema test authorization test;

\c postgres test

load '$libdir/plugins/index_adviser';

drop view if exists select_index_advisory;
drop function if exists show_index_advisory(index_advisory.backend_pid%type);
drop table if exists index_advisory;

\i contrib/pgadviser/resources/index_advisory.create.sql

\i contrib/pgadviser/resources/show_index_advisory.create.sql

\i contrib/pgadviser/resources/select_index_advisory.create.sql

drop table if exists t, t1;

create table t( a int, b int );

insert into t select s, 99999-s from generate_series(0,99999) as s;

analyze t;

create table t1 as select * from t;

/* notice no ANALYZE for T1 */;

select count(*) from t;

select count(*) from t1;

/* stop here if you wish to run only pg_advise tool */;

/* set the client to see the log messages generated by the Adviser */;
set client_min_messages to log;

explain select * from t where a = 100;

explain select * from t where b = 100;

explain select * from t where a = 100 and b = 100;

explain select * from t where a = 100 or b = 100;

/* now we shall test with T1 */;

explain select * from t1 where a = 100;

explain select * from t1 where b = 100;

explain select * from t1 where a = 100 and b = 100;

explain select * from t1 where a = 100 or b = 100;

/* let's do some sensible join over these two tables */;
explain 
select	*
from	t,
		t1
where	t.a = 100
and		t1.a = 100
	or t.b = 100
		and t1.b = 100;

/* following are the contents of the advise_index table */;
select * from index_advisory;

/* And following is the summary of the advices generated */;

select	* from select_index_advisory;
