From 83533da71cb359b14171decc402f425f6ce71037 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Thu, 26 Oct 2017 15:04:53 +0500
Subject: [PATCH] Enable Index Only Scan in seg

---
 contrib/seg/Makefile          |  2 +-
 contrib/seg/expected/seg.out  | 10 ++++++++++
 contrib/seg/seg--1.2--1.3.sql | 37 +++++++++++++++++++++++++++++++++++++
 contrib/seg/seg.control       |  2 +-
 contrib/seg/sql/seg.sql       |  3 +++
 5 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 contrib/seg/seg--1.2--1.3.sql

diff --git a/contrib/seg/Makefile b/contrib/seg/Makefile
index 00a5472d3b..41270f84f6 100644
--- a/contrib/seg/Makefile
+++ b/contrib/seg/Makefile
@@ -4,7 +4,7 @@ MODULE_big = seg
 OBJS = seg.o segparse.o $(WIN32RES)
 
 EXTENSION = seg
-DATA = seg--1.1.sql seg--1.1--1.2.sql \
+DATA = seg--1.1.sql seg--1.1--1.2.sql seg--1.2--1.3.sql \
 	seg--1.0--1.1.sql seg--unpackaged--1.0.sql
 PGFILEDESC = "seg - line segment data type"
 
diff --git a/contrib/seg/expected/seg.out b/contrib/seg/expected/seg.out
index 18010c4d5c..f522b7712f 100644
--- a/contrib/seg/expected/seg.out
+++ b/contrib/seg/expected/seg.out
@@ -930,6 +930,16 @@ SELECT '1'::seg <@ '-1 .. 1'::seg AS bool;
 CREATE TABLE test_seg (s seg);
 \copy test_seg from 'data/test_seg.data'
 CREATE INDEX test_seg_ix ON test_seg USING gist (s);
+SET ENABLE_BITMAPSCAN = FALSE;
+EXPLAIN SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
+                                       QUERY PLAN                                       
+----------------------------------------------------------------------------------------
+ Aggregate  (cost=16.21..16.22 rows=1 width=8)
+   ->  Index Only Scan using test_seg_ix on test_seg  (cost=0.14..16.20 rows=3 width=0)
+         Index Cond: (s @> '1.1e1 .. 11.3'::seg)
+(3 rows)
+
+SET ENABLE_BITMAPSCAN = TRUE;
 SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
  count 
 -------
diff --git a/contrib/seg/seg--1.2--1.3.sql b/contrib/seg/seg--1.2--1.3.sql
new file mode 100644
index 0000000000..98bd080f03
--- /dev/null
+++ b/contrib/seg/seg--1.2--1.3.sql
@@ -0,0 +1,37 @@
+/* contrib/seg/seg--1.2--1.3.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION seg UPDATE TO '1.3'" to load this file. \quit
+
+
+UPDATE "pg_catalog"."pg_depend"
+SET deptype = 'a'
+WHERE classid = 'pg_amproc'::regclass
+  AND objid =
+    (SELECT objid
+     FROM "pg_catalog"."pg_depend"
+     WHERE classid = 'pg_amproc'::regclass
+       AND refclassid = 'pg_proc'::regclass
+       AND (refobjid = 'gseg_compress(internal)'::regprocedure))
+  AND refclassid = 'pg_opclass'::regclass
+  AND deptype = 'i';
+  
+ALTER OPERATOR FAMILY gist_seg_ops USING gist drop function 3 (seg);
+ALTER EXTENSION seg DROP function gseg_compress(internal);
+DROP function gseg_compress(internal);
+
+UPDATE "pg_catalog"."pg_depend"
+SET deptype = 'a'
+WHERE classid = 'pg_amproc'::regclass
+  AND objid =
+    (SELECT objid
+     FROM "pg_catalog"."pg_depend"
+     WHERE classid = 'pg_amproc'::regclass
+       AND refclassid = 'pg_proc'::regclass
+       AND (refobjid = 'gseg_decompress(internal)'::regprocedure))
+  AND refclassid = 'pg_opclass'::regclass
+  AND deptype = 'i';
+
+ALTER OPERATOR FAMILY gist_seg_ops USING gist drop function 4 (seg);
+ALTER EXTENSION seg DROP function gseg_decompress(internal);
+DROP function gseg_decompress(internal);
\ No newline at end of file
diff --git a/contrib/seg/seg.control b/contrib/seg/seg.control
index ba3d092c25..d697cd6c2a 100644
--- a/contrib/seg/seg.control
+++ b/contrib/seg/seg.control
@@ -1,5 +1,5 @@
 # seg extension
 comment = 'data type for representing line segments or floating-point intervals'
-default_version = '1.2'
+default_version = '1.3'
 module_pathname = '$libdir/seg'
 relocatable = true
diff --git a/contrib/seg/sql/seg.sql b/contrib/seg/sql/seg.sql
index aa91931474..f2ecc7415f 100644
--- a/contrib/seg/sql/seg.sql
+++ b/contrib/seg/sql/seg.sql
@@ -216,6 +216,9 @@ CREATE TABLE test_seg (s seg);
 \copy test_seg from 'data/test_seg.data'
 
 CREATE INDEX test_seg_ix ON test_seg USING gist (s);
+SET ENABLE_BITMAPSCAN = FALSE;
+EXPLAIN SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
+SET ENABLE_BITMAPSCAN = TRUE;
 SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
 
 -- Test sorting
-- 
2.13.5 (Apple Git-94)

