mirror of
https://github.com/postgres/postgres.git
synced 2025-12-10 14:22:35 +03:00
Create the planner mechanism for optimizing simple MIN and MAX queries
into indexscans on matching indexes. For the moment, it only handles int4 and text datatypes; next step is to add a column to pg_aggregate so that all MIN/MAX aggregates can be handled. Per my recent proposal.
This commit is contained in:
@@ -293,3 +293,58 @@ FROM bool_test;
|
||||
t | t | f | | f | t
|
||||
(1 row)
|
||||
|
||||
--
|
||||
-- Test several cases that should be optimized into indexscans instead of
|
||||
-- the generic aggregate implementation. We can't actually verify that they
|
||||
-- are done as indexscans, but we can check that the results are correct.
|
||||
--
|
||||
-- Basic cases
|
||||
select max(unique1) from tenk1;
|
||||
max
|
||||
------
|
||||
9999
|
||||
(1 row)
|
||||
|
||||
select max(unique1) from tenk1 where unique1 < 42;
|
||||
max
|
||||
-----
|
||||
41
|
||||
(1 row)
|
||||
|
||||
select max(unique1) from tenk1 where unique1 > 42;
|
||||
max
|
||||
------
|
||||
9999
|
||||
(1 row)
|
||||
|
||||
select max(unique1) from tenk1 where unique1 > 42000;
|
||||
max
|
||||
-----
|
||||
|
||||
(1 row)
|
||||
|
||||
-- multi-column index (uses tenk1_thous_tenthous)
|
||||
select max(tenthous) from tenk1 where thousand = 33;
|
||||
max
|
||||
------
|
||||
9033
|
||||
(1 row)
|
||||
|
||||
select min(tenthous) from tenk1 where thousand = 33;
|
||||
min
|
||||
-----
|
||||
33
|
||||
(1 row)
|
||||
|
||||
-- check parameter propagation into an indexscan subquery
|
||||
select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
|
||||
from int4_tbl;
|
||||
f1 | gt
|
||||
-------------+----
|
||||
0 | 1
|
||||
123456 |
|
||||
-123456 | 0
|
||||
2147483647 |
|
||||
-2147483647 | 0
|
||||
(5 rows)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ CREATE INDEX onek_stringu1 ON onek USING btree(stringu1 name_ops);
|
||||
CREATE INDEX tenk1_unique1 ON tenk1 USING btree(unique1 int4_ops);
|
||||
CREATE INDEX tenk1_unique2 ON tenk1 USING btree(unique2 int4_ops);
|
||||
CREATE INDEX tenk1_hundred ON tenk1 USING btree(hundred int4_ops);
|
||||
CREATE INDEX tenk1_thous_tenthous ON tenk1 (thousand, tenthous);
|
||||
CREATE INDEX tenk2_unique1 ON tenk2 USING btree(unique1 int4_ops);
|
||||
CREATE INDEX tenk2_unique2 ON tenk2 USING btree(unique2 int4_ops);
|
||||
CREATE INDEX tenk2_hundred ON tenk2 USING btree(hundred int4_ops);
|
||||
|
||||
@@ -180,3 +180,23 @@ SELECT
|
||||
BOOL_OR(NOT b2) AS "f",
|
||||
BOOL_OR(NOT b3) AS "t"
|
||||
FROM bool_test;
|
||||
|
||||
--
|
||||
-- Test several cases that should be optimized into indexscans instead of
|
||||
-- the generic aggregate implementation. We can't actually verify that they
|
||||
-- are done as indexscans, but we can check that the results are correct.
|
||||
--
|
||||
|
||||
-- Basic cases
|
||||
select max(unique1) from tenk1;
|
||||
select max(unique1) from tenk1 where unique1 < 42;
|
||||
select max(unique1) from tenk1 where unique1 > 42;
|
||||
select max(unique1) from tenk1 where unique1 > 42000;
|
||||
|
||||
-- multi-column index (uses tenk1_thous_tenthous)
|
||||
select max(tenthous) from tenk1 where thousand = 33;
|
||||
select min(tenthous) from tenk1 where thousand = 33;
|
||||
|
||||
-- check parameter propagation into an indexscan subquery
|
||||
select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
|
||||
from int4_tbl;
|
||||
|
||||
@@ -20,6 +20,8 @@ CREATE INDEX tenk1_unique2 ON tenk1 USING btree(unique2 int4_ops);
|
||||
|
||||
CREATE INDEX tenk1_hundred ON tenk1 USING btree(hundred int4_ops);
|
||||
|
||||
CREATE INDEX tenk1_thous_tenthous ON tenk1 (thousand, tenthous);
|
||||
|
||||
CREATE INDEX tenk2_unique1 ON tenk2 USING btree(unique1 int4_ops);
|
||||
|
||||
CREATE INDEX tenk2_unique2 ON tenk2 USING btree(unique2 int4_ops);
|
||||
|
||||
Reference in New Issue
Block a user