🌐 AI搜索 & 代理 主页
Skip to content

Commit e7a2bbd

Browse files
committed
Expand virtual generated columns in constraint expressions
Virtual generated columns in constraint expressions need to be expanded because the optimizer matches these expressions to qual clauses. Failing to do so can cause us to miss opportunities for constraint exclusion. Author: Richard Guo <guofenglinux@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/204804c0-798f-4c72-bd1f-36116024fda3%40eisentraut.org
1 parent 3888391 commit e7a2bbd

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/backend/optimizer/util/plancat.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "parser/parse_relation.h"
4343
#include "parser/parsetree.h"
4444
#include "partitioning/partdesc.h"
45+
#include "rewrite/rewriteHandler.h"
4546
#include "rewrite/rewriteManip.h"
4647
#include "statistics/statistics.h"
4748
#include "storage/bufmgr.h"
@@ -1392,6 +1393,14 @@ get_relation_constraints(PlannerInfo *root,
13921393
result = list_concat(result, rel->partition_qual);
13931394
}
13941395

1396+
/*
1397+
* Expand virtual generated columns in the constraint expressions.
1398+
*/
1399+
if (result)
1400+
result = (List *) expand_generated_columns_in_expr((Node *) result,
1401+
relation,
1402+
varno);
1403+
13951404
table_close(relation, NoLock);
13961405

13971406
return result;

src/test/regress/expected/generated_virtual.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,3 +1614,26 @@ select * from gtest32 t group by grouping sets (a, b, c, d, e) having c = 20;
16141614
-- Ensure that the virtual generated columns in ALTER COLUMN TYPE USING expression are expanded
16151615
alter table gtest32 alter column e type bigint using b;
16161616
drop table gtest32;
1617+
-- Ensure that virtual generated columns in constraint expressions are expanded
1618+
create table gtest33 (a int, b int generated always as (a * 2) virtual not null, check (b > 10));
1619+
set constraint_exclusion to on;
1620+
-- should get a dummy Result, not a seq scan
1621+
explain (costs off)
1622+
select * from gtest33 where b < 10;
1623+
QUERY PLAN
1624+
--------------------------
1625+
Result
1626+
One-Time Filter: false
1627+
(2 rows)
1628+
1629+
-- should get a dummy Result, not a seq scan
1630+
explain (costs off)
1631+
select * from gtest33 where b is null;
1632+
QUERY PLAN
1633+
--------------------------
1634+
Result
1635+
One-Time Filter: false
1636+
(2 rows)
1637+
1638+
reset constraint_exclusion;
1639+
drop table gtest33;

src/test/regress/sql/generated_virtual.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,18 @@ select * from gtest32 t group by grouping sets (a, b, c, d, e) having c = 20;
859859
alter table gtest32 alter column e type bigint using b;
860860

861861
drop table gtest32;
862+
863+
-- Ensure that virtual generated columns in constraint expressions are expanded
864+
create table gtest33 (a int, b int generated always as (a * 2) virtual not null, check (b > 10));
865+
set constraint_exclusion to on;
866+
867+
-- should get a dummy Result, not a seq scan
868+
explain (costs off)
869+
select * from gtest33 where b < 10;
870+
871+
-- should get a dummy Result, not a seq scan
872+
explain (costs off)
873+
select * from gtest33 where b is null;
874+
875+
reset constraint_exclusion;
876+
drop table gtest33;

0 commit comments

Comments
 (0)