SELECT version();

CREATE TYPE type_table_test_enum AS ENUM ( 'Ole', 'Bole', 'Skole' );
CREATE TABLE table_test_enum ( val type_table_test_enum NOT NULL, dat date );
CREATE UNIQUE INDEX ON table_test_enum ( val ) WHERE val = 'Ole' AND dat IS NULL;

INSERT INTO table_test_enum SELECT 'Ole', current_date FROM generate_series( 1, 1000 );
INSERT INTO table_test_enum SELECT 'Bole', current_date FROM generate_series( 1, 1000 );
INSERT INTO table_test_enum SELECT 'Skole', current_date FROM generate_series( 1, 1000 );
INSERT INTO table_test_enum VALUES ( 'Ole', NULL );
VACUUM (analyze) table_test_enum;

EXPLAIN (analyze, costs, buffers, verbose) SELECT val FROM table_test_enum WHERE val = 'Ole' and dat IS NULL;

PREPARE qry1(varchar) AS SELECT val FROM table_test_enum WHERE val = $1::type_table_test_enum AND dat IS NULL;
PREPARE qry2(type_table_test_enum) AS select val FROM table_test_enum WHERE val = $1 AND dat IS NULL;
PREPARE qry3(unknown) AS SELECT val FROM table_test_enum WHERE val = $1 AND dat IS NULL;

EXPLAIN (analyze, costs, buffers, verbose) EXECUTE qry1('Ole');
EXPLAIN (analyze, costs, buffers, verbose) EXECUTE qry2('Ole');
EXPLAIN (analyze, costs, buffers, verbose) EXECUTE qry3('Ole');

DEALLOCATE qry1;
DEALLOCATE qry2;
DEALLOCATE qry3;

DROP TABLE table_test_enum;
DROP TYPE type_table_test_enum;