From 0372cba1ea87d6eb10e21133461c1db525dbcf06 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Thu, 7 Jul 2022 13:02:29 +1200 Subject: [PATCH v1 1/2] Track NOT NULL columns in RelOptInfo for base relations Author: Andy Fan Discussion: https://postgr.es/m/CAKU4AWpUA8dyVSU1nfCJz71mu6VEjbGS1uy8azrt5CdyoZqGQA%40mail.gmail.com --- src/backend/optimizer/util/plancat.c | 10 ++++++++++ src/backend/optimizer/util/relnode.c | 2 ++ src/include/nodes/pathnodes.h | 2 ++ 3 files changed, 14 insertions(+) diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 5012bfe142..7111284221 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -472,6 +472,16 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, if (inhparent && relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) set_relation_partition_info(root, rel, relation); + Assert(rel->notnullattrs == NULL); + for(int i = 0; i < relation->rd_att->natts; i++) + { + FormData_pg_attribute *attr = &relation->rd_att->attrs[i]; + + if (attr->attnotnull && !attr->attisdropped) + rel->notnullattrs = bms_add_member(rel->notnullattrs, + attr->attnum - FirstLowInvalidHeapAttributeNumber); + } + table_close(relation, NoLock); /* diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index 520409f4ba..f7be6548ef 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -227,6 +227,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent) rel->lateral_vars = NIL; rel->indexlist = NIL; rel->statlist = NIL; + rel->notnullattrs = NULL; rel->pages = 0; rel->tuples = 0; rel->allvisfrac = 0; @@ -641,6 +642,7 @@ build_join_rel(PlannerInfo *root, joinrel->lateral_referencers = NULL; joinrel->indexlist = NIL; joinrel->statlist = NIL; + joinrel->notnullattrs = NULL; joinrel->pages = 0; joinrel->tuples = 0; joinrel->allvisfrac = 0; diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index b88cfb8dc0..5f81ecc3b5 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -758,6 +758,8 @@ typedef struct RelOptInfo List *indexlist; /* list of StatisticExtInfo */ List *statlist; + /* NOT NULL attributes offset by FirstLowInvalidHeapAttributeNumber */ + Bitmapset *notnullattrs; /* size estimates derived from pg_class */ BlockNumber pages; Cardinality tuples; -- 2.35.1.windows.2