1998-02-18 08:32:17 +01:00
|
|
|
--
|
2000-01-06 07:41:55 +01:00
|
|
|
-- SUBSELECT
|
1998-02-18 08:32:17 +01:00
|
|
|
--
|
|
|
|
|
|
|
|
SELECT 1 AS one WHERE 1 IN (SELECT 1);
|
|
|
|
|
|
|
|
SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
|
|
|
|
|
|
|
|
SELECT 1 AS zero WHERE 1 IN (SELECT 2);
|
|
|
|
|
2013-01-30 20:16:16 +01:00
|
|
|
-- Check grammar's handling of extra parens in assorted contexts
|
|
|
|
|
|
|
|
SELECT * FROM (SELECT 1 AS x) ss;
|
|
|
|
SELECT * FROM ((SELECT 1 AS x)) ss;
|
|
|
|
|
|
|
|
(SELECT 2) UNION SELECT 2;
|
|
|
|
((SELECT 2)) UNION SELECT 2;
|
|
|
|
|
|
|
|
SELECT ((SELECT 2) UNION SELECT 2);
|
|
|
|
SELECT (((SELECT 2)) UNION SELECT 2);
|
|
|
|
|
|
|
|
SELECT (SELECT ARRAY[1,2,3])[1];
|
|
|
|
SELECT ((SELECT ARRAY[1,2,3]))[2];
|
|
|
|
SELECT (((SELECT ARRAY[1,2,3])))[3];
|
|
|
|
|
1998-02-18 08:32:17 +01:00
|
|
|
-- Set up some simple test tables
|
|
|
|
|
|
|
|
CREATE TABLE SUBSELECT_TBL (
|
|
|
|
f1 integer,
|
|
|
|
f2 integer,
|
|
|
|
f3 float
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
|
|
|
|
|
|
|
|
SELECT '' AS eight, * FROM SUBSELECT_TBL;
|
|
|
|
|
|
|
|
-- Uncorrelated subselects
|
|
|
|
|
|
|
|
SELECT '' AS two, f1 AS "Constant Select" FROM SUBSELECT_TBL
|
|
|
|
WHERE f1 IN (SELECT 1);
|
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
|
|
|
|
WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL);
|
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
|
|
|
|
WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE
|
|
|
|
f2 IN (SELECT f1 FROM SUBSELECT_TBL));
|
|
|
|
|
2000-03-23 08:42:13 +01:00
|
|
|
SELECT '' AS three, f1, f2
|
|
|
|
FROM SUBSELECT_TBL
|
|
|
|
WHERE (f1, f2) NOT IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
|
|
|
|
WHERE f3 IS NOT NULL);
|
|
|
|
|
1998-02-18 08:32:17 +01:00
|
|
|
-- Correlated subselects
|
|
|
|
|
2000-03-23 08:42:13 +01:00
|
|
|
SELECT '' AS six, f1 AS "Correlated Field", f2 AS "Second Field"
|
|
|
|
FROM SUBSELECT_TBL upper
|
|
|
|
WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE f1 = upper.f1);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
|
2000-03-23 08:42:13 +01:00
|
|
|
FROM SUBSELECT_TBL upper
|
|
|
|
WHERE f1 IN
|
|
|
|
(SELECT f2 FROM SUBSELECT_TBL WHERE CAST(upper.f2 AS float) = f3);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
|
2000-03-23 08:42:13 +01:00
|
|
|
FROM SUBSELECT_TBL upper
|
|
|
|
WHERE f3 IN (SELECT upper.f1 + f2 FROM SUBSELECT_TBL
|
|
|
|
WHERE f2 = CAST(f3 AS integer));
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
SELECT '' AS five, f1 AS "Correlated Field"
|
|
|
|
FROM SUBSELECT_TBL
|
2000-03-23 08:42:13 +01:00
|
|
|
WHERE (f1, f2) IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
|
|
|
|
WHERE f3 IS NOT NULL);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Use some existing tables in the regression test
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS eight, ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
|
|
|
|
FROM SUBSELECT_TBL ss
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE f1 NOT IN (SELECT f1+1 FROM INT4_TBL
|
|
|
|
WHERE f1 != ss.f1 AND f1 < 2147483647);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
2000-03-23 08:42:13 +01:00
|
|
|
select q1, float8(count(*)) / (select count(*) from int8_tbl)
|
2002-11-21 01:42:20 +01:00
|
|
|
from int8_tbl group by q1 order by q1;
|
2003-10-18 18:52:15 +02:00
|
|
|
|
Fix placement of initPlans when forcibly materializing a subplan.
If we forcibly place a Material node atop a finished subplan, we need
to move any initPlans attached to the subplan up to the Material node,
in order to keep SS_finalize_plan() happy. I'd figured this out in
commit 7b67a0a49 for the case of materializing a cursor plan, but out of
an abundance of caution, I put the initPlan movement hack at the call
site for that case, rather than inside materialize_finished_plan().
That was the wrong thing, because it turns out to also be necessary for
the only other caller of materialize_finished_plan(), ie subselect.c.
We lacked any test cases that exposed the mistake, but bug#14524 from
Wei Congrui shows that it's possible to get an initPlan reference into
the top tlist in that case too, and then SS_finalize_plan() complains.
Hence, move the hack into materialize_finished_plan().
In HEAD, also relocate some recently-added tests in subselect.sql, which
I'd unthinkingly dropped into the middle of a sequence of related tests.
Report: https://postgr.es/m/20170202060020.1400.89021@wrigleys.postgresql.org
2017-02-03 01:11:27 +01:00
|
|
|
-- Unspecified-type literals in output columns should resolve as text
|
|
|
|
|
|
|
|
SELECT *, pg_typeof(f1) FROM
|
|
|
|
(SELECT 'foo' AS f1 FROM generate_series(1,3)) ss ORDER BY 1;
|
|
|
|
|
|
|
|
-- ... unless there's context to suggest differently
|
|
|
|
|
2018-03-13 07:09:58 +01:00
|
|
|
explain (verbose, costs off) select '42' union all select '43';
|
|
|
|
explain (verbose, costs off) select '42' union all select 43;
|
Fix placement of initPlans when forcibly materializing a subplan.
If we forcibly place a Material node atop a finished subplan, we need
to move any initPlans attached to the subplan up to the Material node,
in order to keep SS_finalize_plan() happy. I'd figured this out in
commit 7b67a0a49 for the case of materializing a cursor plan, but out of
an abundance of caution, I put the initPlan movement hack at the call
site for that case, rather than inside materialize_finished_plan().
That was the wrong thing, because it turns out to also be necessary for
the only other caller of materialize_finished_plan(), ie subselect.c.
We lacked any test cases that exposed the mistake, but bug#14524 from
Wei Congrui shows that it's possible to get an initPlan reference into
the top tlist in that case too, and then SS_finalize_plan() complains.
Hence, move the hack into materialize_finished_plan().
In HEAD, also relocate some recently-added tests in subselect.sql, which
I'd unthinkingly dropped into the middle of a sequence of related tests.
Report: https://postgr.es/m/20170202060020.1400.89021@wrigleys.postgresql.org
2017-02-03 01:11:27 +01:00
|
|
|
|
|
|
|
-- check materialization of an initplan reference (bug #14524)
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select 1 = all (select (select 1));
|
|
|
|
select 1 = all (select (select 1));
|
|
|
|
|
2014-11-23 01:12:38 +01:00
|
|
|
--
|
|
|
|
-- Check EXISTS simplification with LIMIT
|
|
|
|
--
|
|
|
|
explain (costs off)
|
|
|
|
select * from int4_tbl o where exists
|
|
|
|
(select 1 from int4_tbl i where i.f1=o.f1 limit null);
|
|
|
|
explain (costs off)
|
|
|
|
select * from int4_tbl o where not exists
|
|
|
|
(select 1 from int4_tbl i where i.f1=o.f1 limit 1);
|
|
|
|
explain (costs off)
|
|
|
|
select * from int4_tbl o where exists
|
|
|
|
(select 1 from int4_tbl i where i.f1=o.f1 limit 0);
|
|
|
|
|
2003-10-18 18:52:15 +02:00
|
|
|
--
|
|
|
|
-- Test cases to catch unpleasant interactions between IN-join processing
|
|
|
|
-- and subquery pullup.
|
|
|
|
--
|
|
|
|
|
|
|
|
select count(*) from
|
|
|
|
(select 1 from tenk1 a
|
|
|
|
where unique1 IN (select hundred from tenk1 b)) ss;
|
|
|
|
select count(distinct ss.ten) from
|
|
|
|
(select ten from tenk1 a
|
|
|
|
where unique1 IN (select hundred from tenk1 b)) ss;
|
|
|
|
select count(*) from
|
|
|
|
(select 1 from tenk1 a
|
|
|
|
where unique1 IN (select distinct hundred from tenk1 b)) ss;
|
|
|
|
select count(distinct ss.ten) from
|
|
|
|
(select ten from tenk1 a
|
|
|
|
where unique1 IN (select distinct hundred from tenk1 b)) ss;
|
2004-05-11 04:21:39 +02:00
|
|
|
|
2005-07-15 19:09:26 +02:00
|
|
|
--
|
|
|
|
-- Test cases to check for overenthusiastic optimization of
|
|
|
|
-- "IN (SELECT DISTINCT ...)" and related cases. Per example from
|
|
|
|
-- Luca Pireddu and Michael Fuhr.
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TEMP TABLE foo (id integer);
|
|
|
|
CREATE TEMP TABLE bar (id1 integer, id2 integer);
|
|
|
|
|
|
|
|
INSERT INTO foo VALUES (1);
|
|
|
|
|
|
|
|
INSERT INTO bar VALUES (1, 1);
|
|
|
|
INSERT INTO bar VALUES (2, 2);
|
|
|
|
INSERT INTO bar VALUES (3, 1);
|
|
|
|
|
|
|
|
-- These cases require an extra level of distinct-ing above subquery s
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
|
|
|
|
SELECT id1, id2 FROM bar) AS s);
|
|
|
|
|
|
|
|
-- These cases do not
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id2 FROM bar UNION
|
|
|
|
SELECT id2 FROM bar) AS s);
|
|
|
|
|
2004-05-11 04:21:39 +02:00
|
|
|
--
|
|
|
|
-- Test case to catch problems with multiply nested sub-SELECTs not getting
|
|
|
|
-- recalculated properly. Per bug report from Didier Moens.
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE orderstest (
|
|
|
|
approver_ref integer,
|
|
|
|
po_ref integer,
|
2011-06-29 08:26:14 +02:00
|
|
|
ordercanceled boolean
|
2004-05-11 04:21:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO orderstest VALUES (1, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 5, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 6, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 7, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 1, true);
|
|
|
|
INSERT INTO orderstest VALUES (66, 8, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (77, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (1, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (1, 1, false);
|
|
|
|
|
|
|
|
CREATE VIEW orders_view AS
|
|
|
|
SELECT *,
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.approver_ref=1 THEN '---' ELSE 'Approved'
|
|
|
|
END) AS "Approved",
|
|
|
|
(SELECT CASE
|
2011-06-29 08:26:14 +02:00
|
|
|
WHEN ord.ordercanceled
|
|
|
|
THEN 'Canceled'
|
2004-05-11 04:21:39 +02:00
|
|
|
ELSE
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.po_ref=1
|
|
|
|
THEN
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.approver_ref=1
|
|
|
|
THEN '---'
|
|
|
|
ELSE 'Approved'
|
|
|
|
END)
|
|
|
|
ELSE 'PO'
|
2010-11-23 21:27:50 +01:00
|
|
|
END)
|
2004-05-11 04:21:39 +02:00
|
|
|
END) AS "Status",
|
|
|
|
(CASE
|
2011-06-29 08:26:14 +02:00
|
|
|
WHEN ord.ordercanceled
|
|
|
|
THEN 'Canceled'
|
2004-05-11 04:21:39 +02:00
|
|
|
ELSE
|
|
|
|
(CASE
|
|
|
|
WHEN ord.po_ref=1
|
|
|
|
THEN
|
|
|
|
(CASE
|
|
|
|
WHEN ord.approver_ref=1
|
|
|
|
THEN '---'
|
|
|
|
ELSE 'Approved'
|
|
|
|
END)
|
|
|
|
ELSE 'PO'
|
2010-11-23 21:27:50 +01:00
|
|
|
END)
|
2004-05-11 04:21:39 +02:00
|
|
|
END) AS "Status_OK"
|
|
|
|
FROM orderstest ord;
|
|
|
|
|
|
|
|
SELECT * FROM orders_view;
|
|
|
|
|
|
|
|
DROP TABLE orderstest cascade;
|
2005-11-23 18:21:04 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test cases to catch situations where rule rewriter fails to propagate
|
|
|
|
-- hasSubLinks flag correctly. Per example from Kyle Bateman.
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table parts (
|
|
|
|
partnum text,
|
|
|
|
cost float8
|
|
|
|
);
|
|
|
|
|
|
|
|
create temp table shipped (
|
|
|
|
ttype char(2),
|
|
|
|
ordnum int4,
|
|
|
|
partnum text,
|
|
|
|
value float8
|
|
|
|
);
|
|
|
|
|
|
|
|
create temp view shipped_view as
|
|
|
|
select * from shipped where ttype = 'wt';
|
|
|
|
|
|
|
|
create rule shipped_view_insert as on insert to shipped_view do instead
|
|
|
|
insert into shipped values('wt', new.ordnum, new.partnum, new.value);
|
|
|
|
|
|
|
|
insert into parts (partnum, cost) values (1, 1234.56);
|
|
|
|
|
|
|
|
insert into shipped_view (ordnum, partnum, value)
|
2007-06-05 23:31:09 +02:00
|
|
|
values (0, 1, (select cost from parts where partnum = '1'));
|
2005-11-23 18:21:04 +01:00
|
|
|
|
|
|
|
select * from shipped_view;
|
|
|
|
|
|
|
|
create rule shipped_view_update as on update to shipped_view do instead
|
|
|
|
update shipped set partnum = new.partnum, value = new.value
|
|
|
|
where ttype = new.ttype and ordnum = new.ordnum;
|
|
|
|
|
|
|
|
update shipped_view set value = 11
|
|
|
|
from int4_tbl a join int4_tbl b
|
|
|
|
on (a.f1 = (select f1 from int4_tbl c where c.f1=b.f1))
|
|
|
|
where ordnum = a.f1;
|
|
|
|
|
|
|
|
select * from shipped_view;
|
|
|
|
|
|
|
|
select f1, ss1 as relabel from
|
|
|
|
(select *, (select sum(f1) from int4_tbl b where f1 >= a.f1) as ss1
|
|
|
|
from int4_tbl a) ss;
|
2007-07-18 23:40:57 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
|
|
|
|
-- Per bug report from David Sanchez i Gregori.
|
|
|
|
--
|
|
|
|
|
|
|
|
select * from (
|
|
|
|
select max(unique1) from tenk1 as a
|
|
|
|
where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
|
|
|
|
) ss;
|
|
|
|
|
|
|
|
select * from (
|
|
|
|
select min(unique1) from tenk1 as a
|
|
|
|
where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
|
|
|
|
) ss;
|
Fix convert_IN_to_join to properly handle the case where the subselect's
output is not of the same type that's needed for the IN comparison (ie,
where the parser inserted an implicit coercion above the subselect result).
We should record the coerced expression, not just a raw Var referencing
the subselect output, as the quantity that needs to be unique-ified if
we choose to implement the IN as Unique followed by a plain join.
As of 8.3 this error was causing crashes, as seen in bug #4113 from Javier
Hernandez, because the executor was being told to hash or sort the raw
subselect output column using operators appropriate to the coerced type.
In prior versions there was no crash because the executor chose the
hash or sort operators for itself based on the column type it saw.
However, that's still not really right, because what's unique for one data
type might not be unique for another. In corner cases we could get multiple
outputs of a row that should appear only once, as demonstrated by the
regression test case included in this commit.
However, this patch doesn't apply cleanly to 8.2 or before, and the code
involved has shifted enough over time that I'm hesitant to try to back-patch.
Given the lack of complaints from the field about such corner cases, I think
the bug may not be important enough to risk breaking other things with a
back-patch.
2008-04-21 22:54:15 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test that an IN implemented using a UniquePath does unique-ification
|
|
|
|
-- with the right semantics, as per bug #4113. (Unfortunately we have
|
|
|
|
-- no simple way to ensure that this test case actually chooses that type
|
|
|
|
-- of plan, but it does in releases 7.4-8.3. Note that an ordering difference
|
|
|
|
-- here might mean that some other plan type is being used, rendering the test
|
|
|
|
-- pointless.)
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table numeric_table (num_col numeric);
|
|
|
|
insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
|
|
|
|
|
|
|
|
create temp table float_table (float_col float8);
|
|
|
|
insert into float_table values (1), (2), (3);
|
|
|
|
|
|
|
|
select * from float_table
|
|
|
|
where float_col in (select num_col from numeric_table);
|
|
|
|
|
|
|
|
select * from numeric_table
|
|
|
|
where num_col in (select float_col from float_table);
|
Fix mis-calculation of extParam/allParam sets for plan nodes, as seen in
bug #4290. The fundamental bug is that masking extParam by outer_params,
as finalize_plan had been doing, caused us to lose the information that
an initPlan depended on the output of a sibling initPlan. On reflection
the best thing to do seemed to be not to try to adjust outer_params for
this case but get rid of it entirely. The only thing it was really doing
for us was to filter out param IDs associated with SubPlan nodes, and that
can be done (with greater accuracy) while processing individual SubPlan
nodes in finalize_primnode. This approach was vindicated by the discovery
that the masking method was hiding a second bug: SS_finalize_plan failed to
remove extParam bits for initPlan output params that were referenced in the
main plan tree (it only got rid of those referenced by other initPlans).
It's not clear that this caused any real problems, given the limited use
of extParam by the executor, but it's certainly not what was intended.
I originally thought that there was also a problem with needing to include
indirect dependencies on external params in initPlans' param sets, but it
turns out that the executor handles this correctly so long as the depended-on
initPlan is earlier in the initPlans list than the one using its output.
That seems a bit of a fragile assumption, but it is true at the moment,
so I just documented it in some code comments rather than making what would
be rather invasive changes to remove the assumption.
Back-patch to 8.1. Previous versions don't have the case of initPlans
referring to other initPlans' outputs, so while the existing logic is still
questionable for them, there are not any known bugs to be fixed. So I'll
refrain from changing them for now.
2008-07-10 03:17:29 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test case for bug #4290: bogus calculation of subplan param sets
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table ta (id int primary key, val int);
|
|
|
|
|
|
|
|
insert into ta values(1,1);
|
|
|
|
insert into ta values(2,2);
|
|
|
|
|
|
|
|
create temp table tb (id int primary key, aval int);
|
|
|
|
|
|
|
|
insert into tb values(1,1);
|
|
|
|
insert into tb values(2,1);
|
|
|
|
insert into tb values(3,2);
|
|
|
|
insert into tb values(4,2);
|
|
|
|
|
|
|
|
create temp table tc (id int primary key, aid int);
|
|
|
|
|
|
|
|
insert into tc values(1,1);
|
|
|
|
insert into tc values(2,2);
|
|
|
|
|
|
|
|
select
|
|
|
|
( select min(tb.id) from tb
|
|
|
|
where tb.aval = (select ta.val from ta where ta.id = tc.aid) ) as min_tb_id
|
|
|
|
from tc;
|
2009-03-10 21:58:26 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test case for 8.3 "failed to locate grouping columns" bug
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table t1 (f1 numeric(14,0), f2 varchar(30));
|
|
|
|
|
|
|
|
select * from
|
|
|
|
(select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
|
|
|
|
from t1 up) ss
|
|
|
|
group by f1,f2,fs;
|
2010-06-21 02:14:48 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table table_a(id integer);
|
|
|
|
insert into table_a values (42);
|
|
|
|
|
|
|
|
create temp view view_a as select * from table_a;
|
|
|
|
|
|
|
|
select view_a from view_a;
|
|
|
|
select (select view_a) from view_a;
|
|
|
|
select (select (select view_a)) from view_a;
|
|
|
|
select (select (a.*)::text) from view_a a;
|
2010-07-08 02:14:04 +02:00
|
|
|
|
2012-07-20 19:09:00 +02:00
|
|
|
--
|
|
|
|
-- Check that whole-row Vars reading the result of a subselect don't include
|
|
|
|
-- any junk columns therein
|
|
|
|
--
|
|
|
|
|
|
|
|
select q from (select max(f1) from int4_tbl group by f1 order by f1) q;
|
|
|
|
with q as (select max(f1) from int4_tbl group by f1 order by f1)
|
|
|
|
select q from q;
|
|
|
|
|
In the planner, delete joinaliasvars lists after we're done with them.
Although joinaliasvars lists coming out of the parser are quite simple,
those lists can contain arbitrarily complex expressions after subquery
pullup. We do not perform expression preprocessing on them, meaning that
expressions in those lists will not meet the expectations of later phases
of the planner (for example, that they do not contain SubLinks). This had
been thought pretty harmless, since we don't intentionally touch those
lists in later phases --- but Andreas Seltenreich found a case in which
adjust_appendrel_attrs() could recurse into a joinaliasvars list and then
die on its assertion that it never sees a SubLink. We considered a couple
of localized fixes to prevent that specific case from looking at the
joinaliasvars lists, but really this seems like a generic hazard for all
expression processing in the planner. Therefore, probably the best answer
is to delete the joinaliasvars lists from the parsetree at the end of
expression preprocessing, so that there are no reachable expressions that
haven't been through preprocessing.
The case Andreas found seems to be harmless in non-Assert builds, and so
far there are no field reports suggesting that there are user-visible
effects in other cases. I considered back-patching this anyway, but
it turns out that Andreas' test doesn't fail at all in 9.4-9.6, because
in those versions adjust_appendrel_attrs contains code (added in commit
842faa714 and removed again in commit 215b43cdc) to process SubLinks
rather than complain about them. Barring discovery of another path by
which unprocessed joinaliasvars lists can cause trouble, the most
prudent compromise seems to be to patch this into v10 but not further.
Patch by me, with thanks to Amit Langote for initial investigation
and review.
Discussion: https://postgr.es/m/87r2tvt9f1.fsf@ansel.ydns.eu
2017-10-25 00:42:47 +02:00
|
|
|
--
|
|
|
|
-- Test case for sublinks pulled up into joinaliasvars lists in an
|
|
|
|
-- inherited update/delete query
|
|
|
|
--
|
|
|
|
|
|
|
|
begin; -- this shouldn't delete anything, but be safe
|
|
|
|
|
|
|
|
delete from road
|
|
|
|
where exists (
|
|
|
|
select 1
|
|
|
|
from
|
|
|
|
int4_tbl cross join
|
|
|
|
( select f1, array(select q1 from int8_tbl) as arr
|
|
|
|
from text_tbl ) ss
|
|
|
|
where road.name = ss.f1 );
|
|
|
|
|
|
|
|
rollback;
|
|
|
|
|
2010-07-08 02:14:04 +02:00
|
|
|
--
|
|
|
|
-- Test case for sublinks pushed down into subselects via join alias expansion
|
|
|
|
--
|
|
|
|
|
|
|
|
select
|
|
|
|
(select sq1) as qq1
|
|
|
|
from
|
|
|
|
(select exists(select 1 from int4_tbl where f1 = q2) as sq1, 42 as dummy
|
|
|
|
from int8_tbl) sq0
|
|
|
|
join
|
|
|
|
int4_tbl i4 on dummy = i4.f1;
|
2010-07-28 06:50:50 +02:00
|
|
|
|
Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.
The newly added ON CONFLICT clause allows to specify an alternative to
raising a unique or exclusion constraint violation error when inserting.
ON CONFLICT refers to constraints that can either be specified using a
inference clause (by specifying the columns of a unique constraint) or
by naming a unique or exclusion constraint. DO NOTHING avoids the
constraint violation, without touching the pre-existing row. DO UPDATE
SET ... [WHERE ...] updates the pre-existing tuple, and has access to
both the tuple proposed for insertion and the existing tuple; the
optional WHERE clause can be used to prevent an update from being
executed. The UPDATE SET and WHERE clauses have access to the tuple
proposed for insertion using the "magic" EXCLUDED alias, and to the
pre-existing tuple using the table name or its alias.
This feature is often referred to as upsert.
This is implemented using a new infrastructure called "speculative
insertion". It is an optimistic variant of regular insertion that first
does a pre-check for existing tuples and then attempts an insert. If a
violating tuple was inserted concurrently, the speculatively inserted
tuple is deleted and a new attempt is made. If the pre-check finds a
matching tuple the alternative DO NOTHING or DO UPDATE action is taken.
If the insertion succeeds without detecting a conflict, the tuple is
deemed inserted.
To handle the possible ambiguity between the excluded alias and a table
named excluded, and for convenience with long relation names, INSERT
INTO now can alias its target table.
Bumps catversion as stored rules change.
Author: Peter Geoghegan, with significant contributions from Heikki
Linnakangas and Andres Freund. Testing infrastructure by Jeff Janes.
Reviewed-By: Heikki Linnakangas, Andres Freund, Robert Haas, Simon Riggs,
Dean Rasheed, Stephen Frost and many others.
2015-05-08 05:31:36 +02:00
|
|
|
--
|
|
|
|
-- Test case for subselect within UPDATE of INSERT...ON CONFLICT DO UPDATE
|
|
|
|
--
|
|
|
|
create temp table upsert(key int4 primary key, val text);
|
|
|
|
insert into upsert values(1, 'val') on conflict (key) do update set val = 'not seen';
|
|
|
|
insert into upsert values(1, 'val') on conflict (key) do update set val = 'seen with subselect ' || (select f1 from int4_tbl where f1 != 0 limit 1)::text;
|
|
|
|
|
|
|
|
select * from upsert;
|
|
|
|
|
|
|
|
with aa as (select 'int4_tbl' u from int4_tbl limit 1)
|
|
|
|
insert into upsert values (1, 'x'), (999, 'y')
|
|
|
|
on conflict (key) do update set val = (select u from aa)
|
|
|
|
returning *;
|
|
|
|
|
2012-10-11 18:20:56 +02:00
|
|
|
--
|
|
|
|
-- Test case for cross-type partial matching in hashed subplan (bug #7597)
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table outer_7597 (f1 int4, f2 int4);
|
|
|
|
insert into outer_7597 values (0, 0);
|
|
|
|
insert into outer_7597 values (1, 0);
|
|
|
|
insert into outer_7597 values (0, null);
|
|
|
|
insert into outer_7597 values (1, null);
|
|
|
|
|
|
|
|
create temp table inner_7597(c1 int8, c2 int8);
|
|
|
|
insert into inner_7597 values(0, null);
|
|
|
|
|
|
|
|
select * from outer_7597 where (f1, f2) not in (select * from inner_7597);
|
|
|
|
|
2010-07-28 06:50:50 +02:00
|
|
|
--
|
|
|
|
-- Test case for premature memory release during hashing of subplan output
|
|
|
|
--
|
|
|
|
|
|
|
|
select '1'::text in (select '1'::name union all select '1'::name);
|
2011-06-20 20:33:20 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test case for planner bug with nested EXISTS handling
|
|
|
|
--
|
|
|
|
select a.thousand from tenk1 a, tenk1 b
|
|
|
|
where a.thousand = b.thousand
|
|
|
|
and exists ( select 1 from tenk1 c where b.hundred = c.hundred
|
|
|
|
and not exists ( select 1 from tenk1 d
|
|
|
|
where a.thousand = d.thousand ) );
|
2013-11-08 17:36:57 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Check that nested sub-selects are not pulled up if they contain volatiles
|
|
|
|
--
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select x, x from
|
|
|
|
(select (select now()) as x from (values(1),(2)) v(y)) ss;
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select x, x from
|
|
|
|
(select (select random()) as x from (values(1),(2)) v(y)) ss;
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select x, x from
|
|
|
|
(select (select now() where y=y) as x from (values(1),(2)) v(y)) ss;
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select x, x from
|
|
|
|
(select (select random() where y=y) as x from (values(1),(2)) v(y)) ss;
|
2013-12-03 02:28:45 +01:00
|
|
|
|
2019-03-01 23:57:20 +01:00
|
|
|
--
|
|
|
|
-- Check we don't misoptimize a NOT IN where the subquery returns no rows.
|
|
|
|
--
|
|
|
|
create temp table notinouter (a int);
|
|
|
|
create temp table notininner (b int not null);
|
|
|
|
insert into notinouter values (null), (1);
|
|
|
|
|
|
|
|
select * from notinouter where a not in (select b from notininner);
|
|
|
|
|
2013-12-03 02:28:45 +01:00
|
|
|
--
|
|
|
|
-- Check we behave sanely in corner case of empty SELECT list (bug #8648)
|
|
|
|
--
|
|
|
|
create temp table nocolumns();
|
|
|
|
select exists(select * from nocolumns);
|
2013-12-10 22:10:17 +01:00
|
|
|
|
2017-11-25 20:15:48 +01:00
|
|
|
--
|
|
|
|
-- Check behavior with a SubPlan in VALUES (bug #14924)
|
|
|
|
--
|
|
|
|
select val.x
|
|
|
|
from generate_series(1,10) as s(i),
|
|
|
|
lateral (
|
|
|
|
values ((select s.i + 1)), (s.i + 101)
|
|
|
|
) as val(x)
|
|
|
|
where s.i < 10 and (select val.x) < 110;
|
|
|
|
|
2013-12-10 22:10:17 +01:00
|
|
|
--
|
|
|
|
-- Check sane behavior with nested IN SubLinks
|
|
|
|
--
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select * from int4_tbl where
|
|
|
|
(case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
|
|
|
|
(select ten from tenk1 b);
|
|
|
|
select * from int4_tbl where
|
|
|
|
(case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
|
|
|
|
(select ten from tenk1 b);
|
2014-06-27 20:08:48 +02:00
|
|
|
|
2014-07-08 20:03:14 +02:00
|
|
|
--
|
|
|
|
-- Check for incorrect optimization when IN subquery contains a SRF
|
|
|
|
--
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select * from int4_tbl o where (f1, f1) in
|
2019-02-10 00:32:23 +01:00
|
|
|
(select f1, generate_series(1,50) / 10 g from int4_tbl i group by f1);
|
2014-07-08 20:03:14 +02:00
|
|
|
select * from int4_tbl o where (f1, f1) in
|
2019-02-10 00:32:23 +01:00
|
|
|
(select f1, generate_series(1,50) / 10 g from int4_tbl i group by f1);
|
2014-07-08 20:03:14 +02:00
|
|
|
|
2014-07-12 01:12:35 +02:00
|
|
|
--
|
|
|
|
-- check for over-optimization of whole-row Var referencing an Append plan
|
|
|
|
--
|
|
|
|
select (select q from
|
|
|
|
(select 1,2,3 where f1 > 0
|
|
|
|
union all
|
|
|
|
select 4,5,6.0 where f1 <= 0
|
|
|
|
) q )
|
|
|
|
from int4_tbl;
|
|
|
|
|
2014-06-27 20:08:48 +02:00
|
|
|
--
|
|
|
|
-- Check that volatile quals aren't pushed down past a DISTINCT:
|
|
|
|
-- nextval() should not be called more than the nominal number of times
|
|
|
|
--
|
|
|
|
create temp sequence ts1;
|
|
|
|
|
|
|
|
select * from
|
|
|
|
(select distinct ten from tenk1) ss
|
|
|
|
where ten < 10 + nextval('ts1')
|
|
|
|
order by 1;
|
|
|
|
|
|
|
|
select nextval('ts1');
|
Disallow pushing volatile quals past set-returning functions.
Pushing an upper-level restriction clause into an unflattened
subquery-in-FROM is okay when the subquery contains no SRFs in its
targetlist, or when it does but the SRFs are unreferenced by the clause
*and the clause is not volatile*. Otherwise, we're changing the number
of times the clause is evaluated, which is bad for volatile quals, and
possibly changing the result, since a volatile qual might succeed for some
SRF output rows and not others despite not referencing any of the changing
columns. (Indeed, if the clause is something like "random() > 0.5", the
user is probably expecting exactly that behavior.)
We had most of these restrictions down, but not the one about the upper
clause not being volatile. Fix that, and add a regression test to
illustrate the expected behavior.
Although this is definitely a bug, it doesn't seem like back-patch
material, since possibly some users don't realize that the broken
behavior is broken and are relying on what happens now. Also, while
the added test is quite cheap in the wake of commit a4c35ea1c, it would
be much more expensive (or else messier) in older branches.
Per report from Tom van Tilburg.
Discussion: <CAP3PPDiucxYCNev52=YPVkrQAPVF1C5PFWnrQPT7iMzO1fiKFQ@mail.gmail.com>
2016-09-28 00:43:36 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Check that volatile quals aren't pushed down past a set-returning function;
|
|
|
|
-- while a nonvolatile qual can be, if it doesn't reference the SRF.
|
|
|
|
--
|
|
|
|
create function tattle(x int, y int) returns bool
|
|
|
|
volatile language plpgsql as $$
|
|
|
|
begin
|
|
|
|
raise notice 'x = %, y = %', x, y;
|
|
|
|
return x > y;
|
|
|
|
end$$;
|
|
|
|
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select * from
|
|
|
|
(select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
|
|
|
|
where tattle(x, 8);
|
|
|
|
|
|
|
|
select * from
|
|
|
|
(select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
|
|
|
|
where tattle(x, 8);
|
|
|
|
|
|
|
|
-- if we pretend it's stable, we get different results:
|
|
|
|
alter function tattle(x int, y int) stable;
|
|
|
|
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select * from
|
|
|
|
(select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
|
|
|
|
where tattle(x, 8);
|
|
|
|
|
|
|
|
select * from
|
|
|
|
(select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
|
|
|
|
where tattle(x, 8);
|
|
|
|
|
|
|
|
-- although even a stable qual should not be pushed down if it references SRF
|
|
|
|
explain (verbose, costs off)
|
|
|
|
select * from
|
|
|
|
(select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
|
|
|
|
where tattle(x, u);
|
|
|
|
|
|
|
|
select * from
|
|
|
|
(select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
|
|
|
|
where tattle(x, u);
|
|
|
|
|
|
|
|
drop function tattle(x int, y int);
|
2017-08-21 20:43:01 +02:00
|
|
|
|
|
|
|
--
|
2017-08-24 19:39:51 +02:00
|
|
|
-- Test that LIMIT can be pushed to SORT through a subquery that just projects
|
|
|
|
-- columns. We check for that having happened by looking to see if EXPLAIN
|
|
|
|
-- ANALYZE shows that a top-N sort was used. We must suppress or filter away
|
|
|
|
-- all the non-invariant parts of the EXPLAIN ANALYZE output.
|
2017-08-21 20:43:01 +02:00
|
|
|
--
|
2017-08-29 19:22:49 +02:00
|
|
|
create table sq_limit (pk int primary key, c1 int, c2 int);
|
2017-08-21 20:43:01 +02:00
|
|
|
insert into sq_limit values
|
2017-08-24 19:39:51 +02:00
|
|
|
(1, 1, 1),
|
|
|
|
(2, 2, 2),
|
|
|
|
(3, 3, 3),
|
|
|
|
(4, 4, 4),
|
|
|
|
(5, 1, 1),
|
|
|
|
(6, 2, 2),
|
|
|
|
(7, 3, 3),
|
|
|
|
(8, 4, 4);
|
|
|
|
|
|
|
|
create function explain_sq_limit() returns setof text language plpgsql as
|
|
|
|
$$
|
|
|
|
declare ln text;
|
|
|
|
begin
|
|
|
|
for ln in
|
|
|
|
explain (analyze, summary off, timing off, costs off)
|
|
|
|
select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3
|
|
|
|
loop
|
|
|
|
ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx');
|
2017-08-29 19:22:49 +02:00
|
|
|
-- this case might occur if force_parallel_mode is on:
|
|
|
|
ln := regexp_replace(ln, 'Worker 0: Sort Method', 'Sort Method');
|
2017-08-24 19:39:51 +02:00
|
|
|
return next ln;
|
|
|
|
end loop;
|
|
|
|
end;
|
2017-08-21 20:43:01 +02:00
|
|
|
$$;
|
|
|
|
|
2017-08-24 19:39:51 +02:00
|
|
|
select * from explain_sq_limit();
|
|
|
|
|
2017-08-21 20:43:01 +02:00
|
|
|
select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3;
|
|
|
|
|
2017-08-24 19:39:51 +02:00
|
|
|
drop function explain_sq_limit();
|
2017-08-29 19:22:49 +02:00
|
|
|
|
|
|
|
drop table sq_limit;
|
2018-08-17 16:04:26 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Ensure that backward scan direction isn't propagated into
|
|
|
|
-- expression subqueries (bug #15336)
|
|
|
|
--
|
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
|
|
declare c1 scroll cursor for
|
|
|
|
select * from generate_series(1,4) i
|
|
|
|
where i <> all (values (2),(3));
|
|
|
|
|
|
|
|
move forward all in c1;
|
|
|
|
fetch backward all in c1;
|
|
|
|
|
|
|
|
commit;
|
Allow user control of CTE materialization, and change the default behavior.
Historically we've always materialized the full output of a CTE query,
treating WITH as an optimization fence (so that, for example, restrictions
from the outer query cannot be pushed into it). This is appropriate when
the CTE query is INSERT/UPDATE/DELETE, or is recursive; but when the CTE
query is non-recursive and side-effect-free, there's no hazard of changing
the query results by pushing restrictions down.
Another argument for materialization is that it can avoid duplicate
computation of an expensive WITH query --- but that only applies if
the WITH query is called more than once in the outer query. Even then
it could still be a net loss, if each call has restrictions that
would allow just a small part of the WITH query to be computed.
Hence, let's change the behavior for WITH queries that are non-recursive
and side-effect-free. By default, we will inline them into the outer
query (removing the optimization fence) if they are called just once.
If they are called more than once, we will keep the old behavior by
default, but the user can override this and force inlining by specifying
NOT MATERIALIZED. Lastly, the user can force the old behavior by
specifying MATERIALIZED; this would mainly be useful when the query had
deliberately been employing WITH as an optimization fence to prevent a
poor choice of plan.
Andreas Karlsson, Andrew Gierth, David Fetter
Discussion: https://postgr.es/m/87sh48ffhb.fsf@news-spur.riddles.org.uk
2019-02-16 22:11:12 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Tests for CTE inlining behavior
|
|
|
|
--
|
|
|
|
|
|
|
|
-- Basic subquery that can be inlined
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from (select f1 from subselect_tbl) ss)
|
|
|
|
select * from x where f1 = 1;
|
|
|
|
|
|
|
|
-- Explicitly request materialization
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as materialized (select * from (select f1 from subselect_tbl) ss)
|
|
|
|
select * from x where f1 = 1;
|
|
|
|
|
|
|
|
-- Stable functions are safe to inline
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from (select f1, now() from subselect_tbl) ss)
|
|
|
|
select * from x where f1 = 1;
|
|
|
|
|
|
|
|
-- Volatile functions prevent inlining
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from (select f1, random() from subselect_tbl) ss)
|
|
|
|
select * from x where f1 = 1;
|
|
|
|
|
|
|
|
-- SELECT FOR UPDATE cannot be inlined
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from (select f1 from subselect_tbl for update) ss)
|
|
|
|
select * from x where f1 = 1;
|
|
|
|
|
|
|
|
-- Multiply-referenced CTEs are inlined only when requested
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from (select f1, now() as n from subselect_tbl) ss)
|
|
|
|
select * from x, x x2 where x.n = x2.n;
|
|
|
|
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as not materialized (select * from (select f1, now() as n from subselect_tbl) ss)
|
|
|
|
select * from x, x x2 where x.n = x2.n;
|
|
|
|
|
|
|
|
-- Check handling of outer references
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from int4_tbl)
|
|
|
|
select * from (with y as (select * from x) select * from y) ss;
|
|
|
|
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as materialized (select * from int4_tbl)
|
|
|
|
select * from (with y as (select * from x) select * from y) ss;
|
|
|
|
|
|
|
|
-- Ensure that we inline the currect CTE when there are
|
|
|
|
-- multiple CTEs with the same name
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select 1 as y)
|
|
|
|
select * from (with x as (select 2 as y) select * from x) ss;
|
|
|
|
|
|
|
|
-- Row marks are not pushed into CTEs
|
|
|
|
explain (verbose, costs off)
|
|
|
|
with x as (select * from subselect_tbl)
|
|
|
|
select * from x for update;
|