-- non-version-specific tests
--\set ON_ERROR_STOP on

-- TOAST handling
CREATE TABLE toast_test(x TEXT);
INSERT INTO toast_test VALUES (repeat('x', 40000));
CREATE INDEX iix ON toast_test (x);
CLUSTER toast_test USING iix; 
VACUUM FULL toast_test;

-- tablespace
CREATE tablespace tt location '/pg_upgrade/tablespace';
CREATE TABLE tablespace_test(x TEXT) tablespace tt;
INSERT INTO tablespace_test VALUES (repeat('x', 40000));
CREATE INDEX aaa ON tablespace_test(x);

-- test database in different tablespace with table in cluster 
-- default tablespace
CREATE DATABASE tbltest TABLESPACE tt;
\connect tbltest
CREATE TABLE t1 (x int);
CREATE TABLE t2 (x int) TABLESPACE pg_default;
\connect test

-- test postgres or template1 in non-default tablespace
ALTER DATABASE postgres SET TABLESPACE tt;

-- multi-segment
--CREATE TABLE multi_segment_test (x TEXT);
--INSERT INTO multi_segment_test
--SELECT repeat('x', 500) FROM generate_series(1, 2000000);

-- tests for 8.3 -> 8.4

-- generate REINDEX command
CREATE TABLE hash_test(x INT);
CREATE INDEX ii ON hash_test USING hash (x);

-- generate REINDEX command
CREATE TABLE bpchar_test(x CHAR(5));
CREATE INDEX iia ON bpchar_test (x bpchar_pattern_ops);

-- this is OK because NAME is the first column
CREATE TABLE name_test1(x NAME);

-- this will prevent the migration because NAME
-- is not the first column
--        create table name_test2(y int, x name);

CREATE TABLE tsvector_test (x tsvector);
INSERT INTO tsvector_test VALUES ('c bb aaa');
CREATE INDEX wowidx ON tsvector_test USING gin (x);

CREATE TABLE tsvector_test2 (x tsvector, y tsvector);
INSERT INTO tsvector_test2 VALUES ('c bb aaa', NULL);
CREATE INDEX wowidx2 ON tsvector_test2 USING gin (x);

CREATE TABLE tsvector_test3 (x tsvector, y tsvector, z tsvector);
INSERT INTO tsvector_test3 VALUES ('c bb aaa', NULL, NULL);
CREATE INDEX wowidx3 ON tsvector_test3 USING gin (x);


-- generates error
--CREATE TABLE tsquery_test (x tsquery);
--INSERT INTO tsquery_test VALUES ('c & bb & aaa');

-- fails on vacuum
--CREATE TYPE footype AS
--(
--        x double precision,
--        y double precision,
--        z double precision
--);
--
--CREATE TABLE breakmigrator
--(
--        id int,
--        foo_a footype
--);
--
--INSERT INTO breakmigrator (id, foo_a)
--        VALUES (1, (1,2,3));
--INSERT INTO breakmigrator (id, foo_a)
--        VALUES (2, (1,2,3));
--INSERT INTO breakmigrator (id, foo_a)
--        VALUES (3, (1,2,3));

CREATE DATABASE tablespacedb TABLESPACE tt;
\c tablespacedb
CREATE TABLE tbtest (x SERIAL PRIMARY KEY);
INSERT INTO tbtest VALUES (105);

--CREATE TABLE regtest(x regclass);

CREATE DATABASE lo;
\c lo
SELECT lo_import('/etc/motd');
\set loid `psql -qt -c 'select loid from pg_largeobject' lo`
CREATE ROLE user1;
CREATE ROLE user2;
-- force user2 to have a different user id on restore
DROP ROLE user1;
GRANT ALL ON LARGE OBJECT :loid TO user2;
REINDEX TABLE pg_largeobject;

-- prepared transactions
-- max_prepared_transactions must be updated in postgresql.conf
-- BEGIN WORK;
-- PREPARE TRANSACTION '100';

CREATE LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION pymax (a integer, b integer) RETURNS integer AS $$
	if a > b:
	  return a
	  return b
$$ LANGUAGE plpythonu;
