\timing

-- Non-UNIQUE

CREATE TABLE hashu (col1 integer, col2 integer, col3 integer);

-- UNIQUE

-- one row should succeed
INSERT INTO hashu VALUES (1, 1, 1);
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1);
DROP INDEX hashu_col1_idx;

-- one row should succeed, insert after build
TRUNCATE hashu;
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1);
INSERT INTO hashu VALUES (1, 1, 1);
DROP INDEX hashu_col1_idx;

-- one row should succeed, multi col
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1, col2, col3);
DROP INDEX hashu_col1_idx;

-- multiple unique rows, with hash collisions, should succeed
INSERT INTO hashu SELECT generate_series(10, 100000), 1, 1;
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1);
DROP INDEX hashu_col1_idx;

-- multiple unique rows, with hash collisions, should succeed, after build
TRUNCATE hashu;
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1);
INSERT INTO hashu SELECT generate_series(10, 100000), 1, 1;
DROP INDEX hashu_col1_idx;

-- add duplicate, so index build should fail
INSERT INTO hashu VALUES (1, 1, 1);
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1);
DROP INDEX hashu_col1_idx;

-- add duplicate on 2nd col, so index build should fail
DELETE FROM hashu WHERE col1 = 1;
INSERT INTO hashu VALUES (1, 1, 1);
INSERT INTO hashu VALUES (1, 2, 1);
CREATE UNIQUE INDEX hashu_col1_idx ON hashu USING hash (col1);
DROP INDEX hashu_col1_idx;

-- deferred unique test
-- not written yet

-- concurrent cases covered in separate isolation test

-- speculative insertion case covered in separate isolation test

DROP TABLE hashu;
-- Exit
