1SET enable_seqscan to false;
2-- test search for "not equals"
3CREATE TABLE test_ne (
4   a  TIMESTAMP,
5   b  NUMERIC
6);
7CREATE INDEX test_ne_idx ON test_ne USING gist (a, b);
8INSERT INTO test_ne SELECT '2009-01-01', 10.7 FROM generate_series(1,1000);
9INSERT INTO test_ne VALUES('2007-02-03', -91.3);
10INSERT INTO test_ne VALUES('2011-09-01', 43.7);
11INSERT INTO test_ne SELECT '2009-01-01', 10.7 FROM generate_series(1,1000);
12SET enable_indexscan to false;
13EXPLAIN (COSTS OFF) SELECT * FROM test_ne WHERE a <> '2009-01-01' AND b <> 10.7;
14                                              QUERY PLAN
15------------------------------------------------------------------------------------------------------
16 Bitmap Heap Scan on test_ne
17   Recheck Cond: ((a <> 'Thu Jan 01 00:00:00 2009'::timestamp without time zone) AND (b <> 10.7))
18   ->  Bitmap Index Scan on test_ne_idx
19         Index Cond: ((a <> 'Thu Jan 01 00:00:00 2009'::timestamp without time zone) AND (b <> 10.7))
20(4 rows)
21
22SELECT * FROM test_ne WHERE a <> '2009-01-01' AND b <> 10.7;
23            a             |   b
24--------------------------+-------
25 Sat Feb 03 00:00:00 2007 | -91.3
26 Thu Sep 01 00:00:00 2011 |  43.7
27(2 rows)
28
29RESET enable_indexscan;
30-- test search for "not equals" using an exclusion constraint
31CREATE TABLE zoo (
32   cage   INTEGER,
33   animal TEXT,
34   EXCLUDE USING gist (cage WITH =, animal WITH <>)
35);
36INSERT INTO zoo VALUES(123, 'zebra');
37INSERT INTO zoo VALUES(123, 'zebra');
38INSERT INTO zoo VALUES(123, 'lion');
39ERROR:  conflicting key value violates exclusion constraint "zoo_cage_animal_excl"
40DETAIL:  Key (cage, animal)=(123, lion) conflicts with existing key (cage, animal)=(123, zebra).
41INSERT INTO zoo VALUES(124, 'lion');
42