PostgreSQL Source Code git master
Data Structures | Typedefs | Functions
relnode.c File Reference
#include "postgres.h"
#include <limits.h>
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/inherit.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteManip.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
Include dependency graph for relnode.c:

Go to the source code of this file.

Data Structures

struct   JoinHashEntry
 

Typedefs

typedef struct JoinHashEntry  JoinHashEntry
 

Functions

static void  build_joinrel_tlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, bool can_null)
 
static Listbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
 
static void  build_joinrel_joinlist (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static Listsubbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
 
static Listsubbuild_joinrel_joinlist (RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
 
static void  set_foreign_rel_properties (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void  add_join_rel (PlannerInfo *root, RelOptInfo *joinrel)
 
static void  build_joinrel_partition_info (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static bool  have_partkey_equi_join (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
 
static int  match_expr_to_partition_keys (Expr *expr, RelOptInfo *rel, bool strict_op)
 
static void  set_joinrel_partition_key_exprs (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
 
static void  build_child_join_reltarget (PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
 
 
 
 
 
 
 
 
 
RelOptInfobuild_join_rel (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, List **restrictlist_ptr)
 
RelOptInfobuild_child_join_rel (PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, int nappinfos, AppendRelInfo **appinfos)
 
Relids  min_join_parameterization (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
 
 
 
ParamPathInfoget_joinrel_parampathinfo (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
 
 
 
 

Typedef Documentation

JoinHashEntry

typedef struct JoinHashEntry JoinHashEntry

Function Documentation

add_join_rel()

static void add_join_rel ( PlannerInforoot,
RelOptInfojoinrel 
)
static

Definition at line 629 of file relnode.c.

630{
631 /* GEQO requires us to append the new joinrel to the end of the list! */
632 root->join_rel_list = lappend(root->join_rel_list, joinrel);
633
634 /* store it into the auxiliary hashtable if there is one. */
635 if (root->join_rel_hash)
636 {
637 JoinHashEntry *hentry;
638 bool found;
639
640 hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
641 &(joinrel->relids),
643 &found);
644 Assert(!found);
645 hentry->join_rel = joinrel;
646 }
647}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:952
Assert(PointerIsAligned(start, uint64))
@ HASH_ENTER
Definition: hsearch.h:114
List * lappend(List *list, void *datum)
Definition: list.c:339
tree ctl root
Definition: radixtree.h:1857
RelOptInfo * join_rel
Definition: relnode.c:41
Relids relids
Definition: pathnodes.h:908

References Assert(), HASH_ENTER, hash_search(), JoinHashEntry::join_rel, lappend(), RelOptInfo::relids, and root.

Referenced by build_child_join_rel(), and build_join_rel().

build_child_join_rel()

RelOptInfo * build_child_join_rel ( PlannerInforoot,
RelOptInfoouter_rel,
RelOptInfoinner_rel,
RelOptInfoparent_joinrel,
Listrestrictlist,
SpecialJoinInfosjinfo,
int  nappinfos,
AppendRelInfo **  appinfos 
)

Definition at line 886 of file relnode.c.

890{
891 RelOptInfo *joinrel = makeNode(RelOptInfo);
892
893 /* Only joins between "other" relations land here. */
894 Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
895
896 /* The parent joinrel should have consider_partitionwise_join set. */
897 Assert(parent_joinrel->consider_partitionwise_join);
898
900 joinrel->relids = adjust_child_relids(parent_joinrel->relids,
901 nappinfos, appinfos);
902 joinrel->rows = 0;
903 /* cheap startup cost is interesting iff not all tuples to be retrieved */
904 joinrel->consider_startup = (root->tuple_fraction > 0);
905 joinrel->consider_param_startup = false;
906 joinrel->consider_parallel = false;
908 joinrel->pathlist = NIL;
909 joinrel->ppilist = NIL;
910 joinrel->partial_pathlist = NIL;
911 joinrel->cheapest_startup_path = NULL;
912 joinrel->cheapest_total_path = NULL;
914 joinrel->direct_lateral_relids = NULL;
915 joinrel->lateral_relids = NULL;
916 joinrel->relid = 0; /* indicates not a baserel */
917 joinrel->rtekind = RTE_JOIN;
918 joinrel->min_attr = 0;
919 joinrel->max_attr = 0;
920 joinrel->attr_needed = NULL;
921 joinrel->attr_widths = NULL;
922 joinrel->notnullattnums = NULL;
923 joinrel->nulling_relids = NULL;
924 joinrel->lateral_vars = NIL;
925 joinrel->lateral_referencers = NULL;
926 joinrel->indexlist = NIL;
927 joinrel->pages = 0;
928 joinrel->tuples = 0;
929 joinrel->allvisfrac = 0;
930 joinrel->eclass_indexes = NULL;
931 joinrel->subroot = NULL;
932 joinrel->subplan_params = NIL;
933 joinrel->amflags = 0;
934 joinrel->serverid = InvalidOid;
935 joinrel->userid = InvalidOid;
936 joinrel->useridiscurrent = false;
937 joinrel->fdwroutine = NULL;
938 joinrel->fdw_private = NULL;
939 joinrel->unique_rel = NULL;
940 joinrel->unique_pathkeys = NIL;
941 joinrel->unique_groupclause = NIL;
942 joinrel->baserestrictinfo = NIL;
943 joinrel->baserestrictcost.startup = 0;
944 joinrel->baserestrictcost.per_tuple = 0;
945 joinrel->joininfo = NIL;
946 joinrel->has_eclass_joins = false;
947 joinrel->consider_partitionwise_join = false; /* might get changed later */
948 joinrel->parent = parent_joinrel;
949 joinrel->top_parent = parent_joinrel->top_parent ? parent_joinrel->top_parent : parent_joinrel;
950 joinrel->top_parent_relids = joinrel->top_parent->relids;
951 joinrel->part_scheme = NULL;
952 joinrel->nparts = -1;
953 joinrel->boundinfo = NULL;
954 joinrel->partbounds_merged = false;
955 joinrel->partition_qual = NIL;
956 joinrel->part_rels = NULL;
957 joinrel->live_parts = NULL;
958 joinrel->all_partrels = NULL;
959 joinrel->partexprs = NULL;
960 joinrel->nullable_partexprs = NULL;
961
962 /* Compute information relevant to foreign relations. */
963 set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
964
965 /* Set up reltarget struct */
966 build_child_join_reltarget(root, parent_joinrel, joinrel,
967 nappinfos, appinfos);
968
969 /* Construct joininfo list. */
971 (Node *) parent_joinrel->joininfo,
972 nappinfos,
973 appinfos);
974
975 /*
976 * Lateral relids referred in child join will be same as that referred in
977 * the parent relation.
978 */
979 joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
980 joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
981
982 /*
983 * If the parent joinrel has pending equivalence classes, so does the
984 * child.
985 */
986 joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
987
988 /* Is the join between partitions itself partitioned? */
989 build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
990 restrictlist);
991
992 /* Child joinrel is parallel safe if parent is parallel safe. */
993 joinrel->consider_parallel = parent_joinrel->consider_parallel;
994
995 /* Set estimates of the child-joinrel's size. */
996 set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
997 sjinfo, restrictlist);
998
999 /* We build the join only once. */
1000 Assert(!find_join_rel(root, joinrel->relids));
1001
1002 /* Add the relation to the PlannerInfo. */
1003 add_join_rel(root, joinrel);
1004
1005 /*
1006 * We might need EquivalenceClass members corresponding to the child join,
1007 * so that we can represent sort pathkeys for it. As with children of
1008 * baserels, we shouldn't need this unless there are relevant eclass joins
1009 * (implying that a merge join might be possible) or pathkeys to sort by.
1010 */
1011 if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
1013 nappinfos, appinfos,
1014 parent_joinrel, joinrel);
1015
1016 return joinrel;
1017}
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:200
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:574
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:5463
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2941
#define makeNode(_type_)
Definition: nodes.h:161
@ RTE_JOIN
Definition: parsenodes.h:1045
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:2319
Bitmapset * Relids
Definition: pathnodes.h:30
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:867
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:891
#define NIL
Definition: pg_list.h:68
#define InvalidOid
Definition: postgres_ext.h:37
static void build_joinrel_partition_info(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: relnode.c:1996
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:529
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2509
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:591
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:629
Definition: pg_list.h:54
Definition: nodes.h:135
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
List * baserestrictinfo
Definition: pathnodes.h:1027
bool consider_param_startup
Definition: pathnodes.h:922
List * subplan_params
Definition: pathnodes.h:986
List * ppilist
Definition: pathnodes.h:936
bool useridiscurrent
Definition: pathnodes.h:1000
uint32 amflags
Definition: pathnodes.h:990
List * joininfo
Definition: pathnodes.h:1033
Bitmapset * notnullattnums
Definition: pathnodes.h:968
List * partition_qual
Definition: pathnodes.h:1069
struct PathTarget * reltarget
Definition: pathnodes.h:930
Index relid
Definition: pathnodes.h:954
List * lateral_vars
Definition: pathnodes.h:972
List * unique_pathkeys
Definition: pathnodes.h:1019
int nparts
Definition: pathnodes.h:1063
Cardinality tuples
Definition: pathnodes.h:981
bool consider_parallel
Definition: pathnodes.h:924
Relids top_parent_relids
Definition: pathnodes.h:1051
bool partbounds_merged
Definition: pathnodes.h:1067
BlockNumber pages
Definition: pathnodes.h:980
Relids lateral_relids
Definition: pathnodes.h:949
List * cheapest_parameterized_paths
Definition: pathnodes.h:940
List * pathlist
Definition: pathnodes.h:935
RelOptKind reloptkind
Definition: pathnodes.h:902
List * indexlist
Definition: pathnodes.h:976
Relids lateral_referencers
Definition: pathnodes.h:974
struct Path * cheapest_startup_path
Definition: pathnodes.h:938
QualCost baserestrictcost
Definition: pathnodes.h:1029
struct Path * cheapest_total_path
Definition: pathnodes.h:939
List * unique_groupclause
Definition: pathnodes.h:1021
Oid userid
Definition: pathnodes.h:998
Bitmapset * eclass_indexes
Definition: pathnodes.h:984
Relids all_partrels
Definition: pathnodes.h:1083
Relids direct_lateral_relids
Definition: pathnodes.h:947
bool has_eclass_joins
Definition: pathnodes.h:1035
Oid serverid
Definition: pathnodes.h:996
bool consider_startup
Definition: pathnodes.h:920
Bitmapset * live_parts
Definition: pathnodes.h:1081
bool consider_partitionwise_join
Definition: pathnodes.h:1041
List * partial_pathlist
Definition: pathnodes.h:937
PlannerInfo * subroot
Definition: pathnodes.h:985
AttrNumber max_attr
Definition: pathnodes.h:962
Relids nulling_relids
Definition: pathnodes.h:970
double allvisfrac
Definition: pathnodes.h:982
struct RelOptInfo * unique_rel
Definition: pathnodes.h:1017
Cardinality rows
Definition: pathnodes.h:914
AttrNumber min_attr
Definition: pathnodes.h:960
RTEKind rtekind
Definition: pathnodes.h:958
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:681

References add_child_join_rel_equivalences(), add_join_rel(), adjust_appendrel_attrs(), adjust_child_relids(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert(), RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), build_child_join_reltarget(), build_joinrel_partition_info(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, find_join_rel(), RelOptInfo::has_eclass_joins, has_useful_pathkeys(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, RelOptInfo::relid, RelOptInfo::relids, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, root, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, RelOptInfo::tuples, RelOptInfo::unique_groupclause, RelOptInfo::unique_pathkeys, RelOptInfo::unique_rel, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by try_partitionwise_join().

build_child_join_reltarget()

static void build_child_join_reltarget ( PlannerInforoot,
RelOptInfoparentrel,
RelOptInfochildrel,
int  nappinfos,
AppendRelInfo **  appinfos 
)
static

Definition at line 2509 of file relnode.c.

2514{
2515 /* Build the targetlist */
2516 childrel->reltarget->exprs = (List *)
2518 (Node *) parentrel->reltarget->exprs,
2519 nappinfos, appinfos);
2520
2521 /* Set the cost and width fields */
2522 childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2523 childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2524 childrel->reltarget->width = parentrel->reltarget->width;
2525}
int width
Definition: pathnodes.h:1700
List * exprs
Definition: pathnodes.h:1691
QualCost cost
Definition: pathnodes.h:1697

References adjust_appendrel_attrs(), PathTarget::cost, PathTarget::exprs, QualCost::per_tuple, RelOptInfo::reltarget, root, QualCost::startup, and PathTarget::width.

Referenced by build_child_join_rel().

build_join_rel()

RelOptInfo * build_join_rel ( PlannerInforoot,
Relids  joinrelids,
RelOptInfoouter_rel,
RelOptInfoinner_rel,
SpecialJoinInfosjinfo,
Listpushed_down_joins,
List **  restrictlist_ptr 
)

Definition at line 667 of file relnode.c.

674{
675 RelOptInfo *joinrel;
676 List *restrictlist;
677
678 /* This function should be used only for join between parents. */
679 Assert(!IS_OTHER_REL(outer_rel) && !IS_OTHER_REL(inner_rel));
680
681 /*
682 * See if we already have a joinrel for this set of base rels.
683 */
684 joinrel = find_join_rel(root, joinrelids);
685
686 if (joinrel)
687 {
688 /*
689 * Yes, so we only need to figure the restrictlist for this particular
690 * pair of component relations.
691 */
692 if (restrictlist_ptr)
693 *restrictlist_ptr = build_joinrel_restrictlist(root,
694 joinrel,
695 outer_rel,
696 inner_rel,
697 sjinfo);
698 return joinrel;
699 }
700
701 /*
702 * Nope, so make one.
703 */
704 joinrel = makeNode(RelOptInfo);
705 joinrel->reloptkind = RELOPT_JOINREL;
706 joinrel->relids = bms_copy(joinrelids);
707 joinrel->rows = 0;
708 /* cheap startup cost is interesting iff not all tuples to be retrieved */
709 joinrel->consider_startup = (root->tuple_fraction > 0);
710 joinrel->consider_param_startup = false;
711 joinrel->consider_parallel = false;
713 joinrel->pathlist = NIL;
714 joinrel->ppilist = NIL;
715 joinrel->partial_pathlist = NIL;
716 joinrel->cheapest_startup_path = NULL;
717 joinrel->cheapest_total_path = NULL;
719 /* init direct_lateral_relids from children; we'll finish it up below */
720 joinrel->direct_lateral_relids =
722 inner_rel->direct_lateral_relids);
724 outer_rel, inner_rel);
725 joinrel->relid = 0; /* indicates not a baserel */
726 joinrel->rtekind = RTE_JOIN;
727 joinrel->min_attr = 0;
728 joinrel->max_attr = 0;
729 joinrel->attr_needed = NULL;
730 joinrel->attr_widths = NULL;
731 joinrel->notnullattnums = NULL;
732 joinrel->nulling_relids = NULL;
733 joinrel->lateral_vars = NIL;
734 joinrel->lateral_referencers = NULL;
735 joinrel->indexlist = NIL;
736 joinrel->statlist = NIL;
737 joinrel->pages = 0;
738 joinrel->tuples = 0;
739 joinrel->allvisfrac = 0;
740 joinrel->eclass_indexes = NULL;
741 joinrel->subroot = NULL;
742 joinrel->subplan_params = NIL;
743 joinrel->rel_parallel_workers = -1;
744 joinrel->amflags = 0;
745 joinrel->serverid = InvalidOid;
746 joinrel->userid = InvalidOid;
747 joinrel->useridiscurrent = false;
748 joinrel->fdwroutine = NULL;
749 joinrel->fdw_private = NULL;
750 joinrel->unique_for_rels = NIL;
751 joinrel->non_unique_for_rels = NIL;
752 joinrel->unique_rel = NULL;
753 joinrel->unique_pathkeys = NIL;
754 joinrel->unique_groupclause = NIL;
755 joinrel->baserestrictinfo = NIL;
756 joinrel->baserestrictcost.startup = 0;
757 joinrel->baserestrictcost.per_tuple = 0;
758 joinrel->baserestrict_min_security = UINT_MAX;
759 joinrel->joininfo = NIL;
760 joinrel->has_eclass_joins = false;
761 joinrel->consider_partitionwise_join = false; /* might get changed later */
762 joinrel->parent = NULL;
763 joinrel->top_parent = NULL;
764 joinrel->top_parent_relids = NULL;
765 joinrel->part_scheme = NULL;
766 joinrel->nparts = -1;
767 joinrel->boundinfo = NULL;
768 joinrel->partbounds_merged = false;
769 joinrel->partition_qual = NIL;
770 joinrel->part_rels = NULL;
771 joinrel->live_parts = NULL;
772 joinrel->all_partrels = NULL;
773 joinrel->partexprs = NULL;
774 joinrel->nullable_partexprs = NULL;
775
776 /* Compute information relevant to the foreign relations. */
777 set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
778
779 /*
780 * Fill the joinrel's tlist with just the Vars and PHVs that need to be
781 * output from this join (ie, are needed for higher joinclauses or final
782 * output).
783 *
784 * NOTE: the tlist order for a join rel will depend on which pair of outer
785 * and inner rels we first try to build it from. But the contents should
786 * be the same regardless.
787 */
788 build_joinrel_tlist(root, joinrel, outer_rel, sjinfo, pushed_down_joins,
789 (sjinfo->jointype == JOIN_FULL));
790 build_joinrel_tlist(root, joinrel, inner_rel, sjinfo, pushed_down_joins,
791 (sjinfo->jointype != JOIN_INNER));
792 add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel, sjinfo);
793
794 /*
795 * add_placeholders_to_joinrel also took care of adding the ph_lateral
796 * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
797 * now we can finish computing that. This is much like the computation of
798 * the transitively-closed lateral_relids in min_join_parameterization,
799 * except that here we *do* have to consider the added PHVs.
800 */
801 joinrel->direct_lateral_relids =
802 bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
803
804 /*
805 * Construct restrict and join clause lists for the new joinrel. (The
806 * caller might or might not need the restrictlist, but I need it anyway
807 * for set_joinrel_size_estimates().)
808 */
809 restrictlist = build_joinrel_restrictlist(root, joinrel,
810 outer_rel, inner_rel,
811 sjinfo);
812 if (restrictlist_ptr)
813 *restrictlist_ptr = restrictlist;
814 build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
815
816 /*
817 * This is also the right place to check whether the joinrel has any
818 * pending EquivalenceClass joins.
819 */
821
822 /* Store the partition information. */
823 build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
824 restrictlist);
825
826 /*
827 * Set estimates of the joinrel's size.
828 */
829 set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
830 sjinfo, restrictlist);
831
832 /*
833 * Set the consider_parallel flag if this joinrel could potentially be
834 * scanned within a parallel worker. If this flag is false for either
835 * inner_rel or outer_rel, then it must be false for the joinrel also.
836 * Even if both are true, there might be parallel-restricted expressions
837 * in the targetlist or quals.
838 *
839 * Note that if there are more than two rels in this relation, they could
840 * be divided between inner_rel and outer_rel in any arbitrary way. We
841 * assume this doesn't matter, because we should hit all the same baserels
842 * and joinclauses while building up to this joinrel no matter which we
843 * take; therefore, we should make the same decision here however we get
844 * here.
845 */
846 if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
847 is_parallel_safe(root, (Node *) restrictlist) &&
848 is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
849 joinrel->consider_parallel = true;
850
851 /* Add the joinrel to the PlannerInfo. */
852 add_join_rel(root, joinrel);
853
854 /*
855 * Also, if dynamic-programming join search is active, add the new joinrel
856 * to the appropriate sublist. Note: you might think the Assert on number
857 * of members should be for equality, but some of the level 1 rels might
858 * have been joinrels already, so we can only assert <=.
859 */
860 if (root->join_rel_level)
861 {
862 Assert(root->join_cur_level > 0);
863 Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
864 root->join_rel_level[root->join_cur_level] =
865 lappend(root->join_rel_level[root->join_cur_level], joinrel);
866 }
867
868 return joinrel;
869}
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:757
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:3446
@ JOIN_FULL
Definition: nodes.h:305
@ JOIN_INNER
Definition: nodes.h:303
@ RELOPT_JOINREL
Definition: pathnodes.h:865
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: placeholder.c:400
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, bool can_null)
Definition: relnode.c:1106
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1028
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: relnode.c:1291
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1328
List * statlist
Definition: pathnodes.h:978
List * unique_for_rels
Definition: pathnodes.h:1009
List * non_unique_for_rels
Definition: pathnodes.h:1011
int rel_parallel_workers
Definition: pathnodes.h:988
Index baserestrict_min_security
Definition: pathnodes.h:1031
JoinType jointype
Definition: pathnodes.h:3033

References add_join_rel(), add_placeholders_to_joinrel(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert(), RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_del_members(), bms_num_members(), bms_union(), build_joinrel_joinlist(), build_joinrel_partition_info(), build_joinrel_restrictlist(), build_joinrel_tlist(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, PathTarget::exprs, find_join_rel(), RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, is_parallel_safe(), JOIN_FULL, JOIN_INNER, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lappend(), RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, min_join_parameterization(), NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, root, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::unique_groupclause, RelOptInfo::unique_pathkeys, RelOptInfo::unique_rel, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by make_join_rel().

build_join_rel_hash()

static void build_join_rel_hash ( PlannerInforoot )
static

Definition at line 488 of file relnode.c.

489{
490 HTAB *hashtab;
491 HASHCTL hash_ctl;
492 ListCell *l;
493
494 /* Create the hash table */
495 hash_ctl.keysize = sizeof(Relids);
496 hash_ctl.entrysize = sizeof(JoinHashEntry);
497 hash_ctl.hash = bitmap_hash;
498 hash_ctl.match = bitmap_match;
499 hash_ctl.hcxt = CurrentMemoryContext;
500 hashtab = hash_create("JoinRelHashTable",
501 256L,
502 &hash_ctl,
504
505 /* Insert all the already-existing joinrels */
506 foreach(l, root->join_rel_list)
507 {
508 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
509 JoinHashEntry *hentry;
510 bool found;
511
512 hentry = (JoinHashEntry *) hash_search(hashtab,
513 &(rel->relids),
515 &found);
516 Assert(!found);
517 hentry->join_rel = rel;
518 }
519
520 root->join_rel_hash = hashtab;
521}
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1436
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1446
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:358
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_COMPARE
Definition: hsearch.h:99
#define HASH_FUNCTION
Definition: hsearch.h:98
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define lfirst(lc)
Definition: pg_list.h:172
struct JoinHashEntry JoinHashEntry
Definition: hsearch.h:66
Size keysize
Definition: hsearch.h:75
HashValueFunc hash
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:76
HashCompareFunc match
Definition: hsearch.h:80
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:222
Definition: pg_list.h:46

References Assert(), bitmap_hash(), bitmap_match(), CurrentMemoryContext, HASHCTL::entrysize, HASHCTL::hash, HASH_COMPARE, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FUNCTION, hash_search(), HASHCTL::hcxt, JoinHashEntry::join_rel, HASHCTL::keysize, lfirst, HASHCTL::match, RelOptInfo::relids, and root.

Referenced by find_join_rel().

build_joinrel_joinlist()

static void build_joinrel_joinlist ( RelOptInfojoinrel,
RelOptInfoouter_rel,
RelOptInfoinner_rel 
)
static

Definition at line 1328 of file relnode.c.

1331{
1332 List *result;
1333
1334 /*
1335 * Collect all the clauses that syntactically belong above this level,
1336 * eliminating any duplicates (important since we will see many of the
1337 * same clauses arriving from both input relations).
1338 */
1339 result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1340 result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1341
1342 joinrel->joininfo = result;
1343}
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1412

References RelOptInfo::joininfo, NIL, and subbuild_joinrel_joinlist().

Referenced by build_join_rel().

build_joinrel_partition_info()

static void build_joinrel_partition_info ( PlannerInforoot,
RelOptInfojoinrel,
RelOptInfoouter_rel,
RelOptInfoinner_rel,
SpecialJoinInfosjinfo,
Listrestrictlist 
)
static

Definition at line 1996 of file relnode.c.

2000{
2001 PartitionScheme part_scheme;
2002
2003 /* Nothing to do if partitionwise join technique is disabled. */
2005 {
2006 Assert(!IS_PARTITIONED_REL(joinrel));
2007 return;
2008 }
2009
2010 /*
2011 * We can only consider this join as an input to further partitionwise
2012 * joins if (a) the input relations are partitioned and have
2013 * consider_partitionwise_join=true, (b) the partition schemes match, and
2014 * (c) we can identify an equi-join between the partition keys. Note that
2015 * if it were possible for have_partkey_equi_join to return different
2016 * answers for the same joinrel depending on which join ordering we try
2017 * first, this logic would break. That shouldn't happen, though, because
2018 * of the way the query planner deduces implied equalities and reorders
2019 * the joins. Please see optimizer/README for details.
2020 */
2021 if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
2022 !outer_rel->consider_partitionwise_join ||
2023 !inner_rel->consider_partitionwise_join ||
2024 outer_rel->part_scheme != inner_rel->part_scheme ||
2025 !have_partkey_equi_join(root, joinrel, outer_rel, inner_rel,
2026 sjinfo->jointype, restrictlist))
2027 {
2028 Assert(!IS_PARTITIONED_REL(joinrel));
2029 return;
2030 }
2031
2032 part_scheme = outer_rel->part_scheme;
2033
2034 /*
2035 * This function will be called only once for each joinrel, hence it
2036 * should not have partitioning fields filled yet.
2037 */
2038 Assert(!joinrel->part_scheme && !joinrel->partexprs &&
2039 !joinrel->nullable_partexprs && !joinrel->part_rels &&
2040 !joinrel->boundinfo);
2041
2042 /*
2043 * If the join relation is partitioned, it uses the same partitioning
2044 * scheme as the joining relations.
2045 *
2046 * Note: we calculate the partition bounds, number of partitions, and
2047 * child-join relations of the join relation in try_partitionwise_join().
2048 */
2049 joinrel->part_scheme = part_scheme;
2050 set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel,
2051 sjinfo->jointype);
2052
2053 /*
2054 * Set the consider_partitionwise_join flag.
2055 */
2058 joinrel->consider_partitionwise_join = true;
2059}
bool enable_partitionwise_join
Definition: costsize.c:159
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1104
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:2365
static bool have_partkey_equi_join(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:2069

References Assert(), RelOptInfo::consider_partitionwise_join, enable_partitionwise_join, have_partkey_equi_join(), IS_PARTITIONED_REL, SpecialJoinInfo::jointype, root, and set_joinrel_partition_key_exprs().

Referenced by build_child_join_rel(), and build_join_rel().

build_joinrel_restrictlist()

static List * build_joinrel_restrictlist ( PlannerInforoot,
RelOptInfojoinrel,
RelOptInfoouter_rel,
RelOptInfoinner_rel,
SpecialJoinInfosjinfo 
)
static

Definition at line 1291 of file relnode.c.

1296{
1297 List *result;
1298 Relids both_input_relids;
1299
1300 both_input_relids = bms_union(outer_rel->relids, inner_rel->relids);
1301
1302 /*
1303 * Collect all the clauses that syntactically belong at this level,
1304 * eliminating any duplicates (important since we will see many of the
1305 * same clauses arriving from both input relations).
1306 */
1307 result = subbuild_joinrel_restrictlist(root, joinrel, outer_rel,
1308 both_input_relids, NIL);
1309 result = subbuild_joinrel_restrictlist(root, joinrel, inner_rel,
1310 both_input_relids, result);
1311
1312 /*
1313 * Add on any clauses derived from EquivalenceClasses. These cannot be
1314 * redundant with the clauses in the joininfo lists, so don't bother
1315 * checking.
1316 */
1317 result = list_concat(result,
1319 joinrel->relids,
1320 outer_rel->relids,
1321 inner_rel,
1322 sjinfo));
1323
1324 return result;
1325}
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: equivclass.c:1550
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
static List * subbuild_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
Definition: relnode.c:1346

References bms_union(), generate_join_implied_equalities(), list_concat(), NIL, RelOptInfo::relids, root, and subbuild_joinrel_restrictlist().

Referenced by build_join_rel().

build_joinrel_tlist()

static void build_joinrel_tlist ( PlannerInforoot,
RelOptInfojoinrel,
RelOptInfoinput_rel,
SpecialJoinInfosjinfo,
Listpushed_down_joins,
bool  can_null 
)
static

Definition at line 1106 of file relnode.c.

1111{
1112 Relids relids = joinrel->relids;
1113 int64 tuple_width = joinrel->reltarget->width;
1114 ListCell *vars;
1115 ListCell *lc;
1116
1117 foreach(vars, input_rel->reltarget->exprs)
1118 {
1119 Var *var = (Var *) lfirst(vars);
1120
1121 /*
1122 * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
1123 */
1124 if (IsA(var, PlaceHolderVar))
1125 {
1126 PlaceHolderVar *phv = (PlaceHolderVar *) var;
1128
1129 /* Is it still needed above this joinrel? */
1130 if (bms_nonempty_difference(phinfo->ph_needed, relids))
1131 {
1132 /*
1133 * Yup, add it to the output. If this join potentially nulls
1134 * this input, we have to update the PHV's phnullingrels,
1135 * which means making a copy.
1136 */
1137 if (can_null)
1138 {
1139 phv = copyObject(phv);
1140 /* See comments above to understand this logic */
1141 if (sjinfo->ojrelid != 0 &&
1142 bms_is_member(sjinfo->ojrelid, relids) &&
1143 (bms_is_subset(phv->phrels, sjinfo->syn_righthand) ||
1144 (sjinfo->jointype == JOIN_FULL &&
1145 bms_is_subset(phv->phrels, sjinfo->syn_lefthand))))
1147 sjinfo->ojrelid);
1148 foreach(lc, pushed_down_joins)
1149 {
1150 SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
1151
1152 Assert(bms_is_member(othersj->ojrelid, relids));
1153 if (bms_is_subset(phv->phrels, othersj->syn_righthand))
1155 othersj->ojrelid);
1156 }
1157 phv->phnullingrels =
1160 relids));
1161 }
1162
1163 joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1164 phv);
1165 /* Bubbling up the precomputed result has cost zero */
1166 tuple_width += phinfo->ph_width;
1167 }
1168 continue;
1169 }
1170
1171 /*
1172 * Otherwise, anything in a baserel or joinrel targetlist ought to be
1173 * a Var. (More general cases can only appear in appendrel child
1174 * rels, which will never be seen here.)
1175 */
1176 if (!IsA(var, Var))
1177 elog(ERROR, "unexpected node type in rel targetlist: %d",
1178 (int) nodeTag(var));
1179
1180 if (var->varno == ROWID_VAR)
1181 {
1182 /* UPDATE/DELETE/MERGE row identity vars are always needed */
1184 list_nth(root->row_identity_vars, var->varattno - 1);
1185
1186 /* Update reltarget width estimate from RowIdentityVarInfo */
1187 tuple_width += ridinfo->rowidwidth;
1188 }
1189 else
1190 {
1191 RelOptInfo *baserel;
1192 int ndx;
1193
1194 /* Get the Var's original base rel */
1195 baserel = find_base_rel(root, var->varno);
1196
1197 /* Is it still needed above this joinrel? */
1198 ndx = var->varattno - baserel->min_attr;
1199 if (!bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1200 continue; /* nope, skip it */
1201
1202 /* Update reltarget width estimate from baserel's attr_widths */
1203 tuple_width += baserel->attr_widths[ndx];
1204 }
1205
1206 /*
1207 * Add the Var to the output. If this join potentially nulls this
1208 * input, we have to update the Var's varnullingrels, which means
1209 * making a copy. But note that we don't ever add nullingrel bits to
1210 * row identity Vars (cf. comments in setrefs.c).
1211 */
1212 if (can_null && var->varno != ROWID_VAR)
1213 {
1214 var = copyObject(var);
1215 /* See comments above to understand this logic */
1216 if (sjinfo->ojrelid != 0 &&
1217 bms_is_member(sjinfo->ojrelid, relids) &&
1218 (bms_is_member(var->varno, sjinfo->syn_righthand) ||
1219 (sjinfo->jointype == JOIN_FULL &&
1220 bms_is_member(var->varno, sjinfo->syn_lefthand))))
1221 var->varnullingrels = bms_add_member(var->varnullingrels,
1222 sjinfo->ojrelid);
1223 foreach(lc, pushed_down_joins)
1224 {
1225 SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
1226
1227 Assert(bms_is_member(othersj->ojrelid, relids));
1228 if (bms_is_member(var->varno, othersj->syn_righthand))
1229 var->varnullingrels = bms_add_member(var->varnullingrels,
1230 othersj->ojrelid);
1231 }
1232 var->varnullingrels =
1233 bms_join(var->varnullingrels,
1235 relids));
1236 }
1237
1238 joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1239 var);
1240
1241 /* Vars have cost zero, so no need to adjust reltarget->cost */
1242 }
1243
1244 joinrel->reltarget->width = clamp_width_est(tuple_width);
1245}
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:1230
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
int64_t int64
Definition: c.h:535
int32 clamp_width_est(int64 tuple_width)
Definition: costsize.c:242
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define copyObject(obj)
Definition: nodes.h:232
#define nodeTag(nodeptr)
Definition: nodes.h:139
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:83
#define ROWID_VAR
Definition: primnodes.h:245
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:416
Relids ph_needed
Definition: pathnodes.h:3229
int32 ph_width
Definition: pathnodes.h:3232
Relids phnullingrels
Definition: pathnodes.h:2931
Relids commute_above_r
Definition: pathnodes.h:3036
Relids syn_lefthand
Definition: pathnodes.h:3031
Index ojrelid
Definition: pathnodes.h:3034
Relids syn_righthand
Definition: pathnodes.h:3032
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Definition: regcomp.c:282

References Assert(), bms_add_member(), bms_intersect(), bms_is_member(), bms_is_subset(), bms_join(), bms_nonempty_difference(), clamp_width_est(), SpecialJoinInfo::commute_above_r, copyObject, elog, ERROR, PathTarget::exprs, find_base_rel(), find_placeholder_info(), IsA, JOIN_FULL, SpecialJoinInfo::jointype, lappend(), lfirst, list_nth(), RelOptInfo::min_attr, nodeTag, SpecialJoinInfo::ojrelid, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_width, PlaceHolderVar::phnullingrels, RelOptInfo::relids, RelOptInfo::reltarget, root, ROWID_VAR, RowIdentityVarInfo::rowidwidth, SpecialJoinInfo::syn_lefthand, SpecialJoinInfo::syn_righthand, Var::varattno, Var::varno, and PathTarget::width.

Referenced by build_join_rel().

build_simple_rel()

RelOptInfo * build_simple_rel ( PlannerInforoot,
int  relid,
RelOptInfoparent 
)

Definition at line 192 of file relnode.c.

193{
194 RelOptInfo *rel;
195 RangeTblEntry *rte;
196
197 /* Rel should not exist already */
198 Assert(relid > 0 && relid < root->simple_rel_array_size);
199 if (root->simple_rel_array[relid] != NULL)
200 elog(ERROR, "rel %d already exists", relid);
201
202 /* Fetch RTE for relation */
203 rte = root->simple_rte_array[relid];
204 Assert(rte != NULL);
205
206 rel = makeNode(RelOptInfo);
208 rel->relids = bms_make_singleton(relid);
209 rel->rows = 0;
210 /* cheap startup cost is interesting iff not all tuples to be retrieved */
211 rel->consider_startup = (root->tuple_fraction > 0);
212 rel->consider_param_startup = false; /* might get changed later */
213 rel->consider_parallel = false; /* might get changed later */
215 rel->pathlist = NIL;
216 rel->ppilist = NIL;
217 rel->partial_pathlist = NIL;
218 rel->cheapest_startup_path = NULL;
219 rel->cheapest_total_path = NULL;
221 rel->relid = relid;
222 rel->rtekind = rte->rtekind;
223 /* min_attr, max_attr, attr_needed, attr_widths are set below */
224 rel->notnullattnums = NULL;
225 rel->lateral_vars = NIL;
226 rel->indexlist = NIL;
227 rel->statlist = NIL;
228 rel->pages = 0;
229 rel->tuples = 0;
230 rel->allvisfrac = 0;
231 rel->eclass_indexes = NULL;
232 rel->subroot = NULL;
233 rel->subplan_params = NIL;
234 rel->rel_parallel_workers = -1; /* set up in get_relation_info */
235 rel->amflags = 0;
236 rel->serverid = InvalidOid;
237 if (rte->rtekind == RTE_RELATION)
238 {
239 Assert(parent == NULL ||
240 parent->rtekind == RTE_RELATION ||
241 parent->rtekind == RTE_SUBQUERY);
242
243 /*
244 * For any RELATION rte, we need a userid with which to check
245 * permission access. Baserels simply use their own
246 * RTEPermissionInfo's checkAsUser.
247 *
248 * For otherrels normally there's no RTEPermissionInfo, so we use the
249 * parent's, which normally has one. The exceptional case is that the
250 * parent is a subquery, in which case the otherrel will have its own.
251 */
252 if (rel->reloptkind == RELOPT_BASEREL ||
254 parent->rtekind == RTE_SUBQUERY))
255 {
256 RTEPermissionInfo *perminfo;
257
258 perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
259 rel->userid = perminfo->checkAsUser;
260 }
261 else
262 rel->userid = parent->userid;
263 }
264 else
265 rel->userid = InvalidOid;
266 rel->useridiscurrent = false;
267 rel->fdwroutine = NULL;
268 rel->fdw_private = NULL;
269 rel->unique_for_rels = NIL;
271 rel->unique_rel = NULL;
272 rel->unique_pathkeys = NIL;
273 rel->unique_groupclause = NIL;
274 rel->baserestrictinfo = NIL;
275 rel->baserestrictcost.startup = 0;
277 rel->baserestrict_min_security = UINT_MAX;
278 rel->joininfo = NIL;
279 rel->has_eclass_joins = false;
280 rel->consider_partitionwise_join = false; /* might get changed later */
281 rel->part_scheme = NULL;
282 rel->nparts = -1;
283 rel->boundinfo = NULL;
284 rel->partbounds_merged = false;
285 rel->partition_qual = NIL;
286 rel->part_rels = NULL;
287 rel->live_parts = NULL;
288 rel->all_partrels = NULL;
289 rel->partexprs = NULL;
290 rel->nullable_partexprs = NULL;
291
292 /*
293 * Pass assorted information down the inheritance hierarchy.
294 */
295 if (parent)
296 {
297 /* We keep back-links to immediate parent and topmost parent. */
298 rel->parent = parent;
299 rel->top_parent = parent->top_parent ? parent->top_parent : parent;
300 rel->top_parent_relids = rel->top_parent->relids;
301
302 /*
303 * A child rel is below the same outer joins as its parent. (We
304 * presume this info was already calculated for the parent.)
305 */
306 rel->nulling_relids = parent->nulling_relids;
307
308 /*
309 * Also propagate lateral-reference information from appendrel parent
310 * rels to their child rels. We intentionally give each child rel the
311 * same minimum parameterization, even though it's quite possible that
312 * some don't reference all the lateral rels. This is because any
313 * append path for the parent will have to have the same
314 * parameterization for every child anyway, and there's no value in
315 * forcing extra reparameterize_path() calls. Similarly, a lateral
316 * reference to the parent prevents use of otherwise-movable join rels
317 * for each child.
318 *
319 * It's possible for child rels to have their own children, in which
320 * case the topmost parent's lateral info propagates all the way down.
321 */
323 rel->lateral_relids = parent->lateral_relids;
325 }
326 else
327 {
328 rel->parent = NULL;
329 rel->top_parent = NULL;
330 rel->top_parent_relids = NULL;
331 rel->nulling_relids = NULL;
332 rel->direct_lateral_relids = NULL;
333 rel->lateral_relids = NULL;
334 rel->lateral_referencers = NULL;
335 }
336
337 /* Check type of rtable entry */
338 switch (rte->rtekind)
339 {
340 case RTE_RELATION:
341 /* Table --- retrieve statistics from the system catalogs */
342 get_relation_info(root, rte->relid, rte->inh, rel);
343 break;
344 case RTE_SUBQUERY:
345 case RTE_FUNCTION:
346 case RTE_TABLEFUNC:
347 case RTE_VALUES:
348 case RTE_CTE:
350
351 /*
352 * Subquery, function, tablefunc, values list, CTE, or ENR --- set
353 * up attr range and arrays
354 *
355 * Note: 0 is included in range to support whole-row Vars
356 */
357 rel->min_attr = 0;
358 rel->max_attr = list_length(rte->eref->colnames);
359 rel->attr_needed = (Relids *)
360 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
361 rel->attr_widths = (int32 *)
362 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
363 break;
364 case RTE_RESULT:
365 /* RTE_RESULT has no columns, nor could it have whole-row Var */
366 rel->min_attr = 0;
367 rel->max_attr = -1;
368 rel->attr_needed = NULL;
369 rel->attr_widths = NULL;
370 break;
371 default:
372 elog(ERROR, "unrecognized RTE kind: %d",
373 (int) rte->rtekind);
374 break;
375 }
376
377 /*
378 * We must apply the partially filled in RelOptInfo before calling
379 * apply_child_basequals due to some transformations within that function
380 * which require the RelOptInfo to be available in the simple_rel_array.
381 */
382 root->simple_rel_array[relid] = rel;
383
384 /*
385 * Apply the parent's quals to the child, with appropriate substitution of
386 * variables. If the resulting clause is constant-FALSE or NULL after
387 * applying transformations, apply_child_basequals returns false to
388 * indicate that scanning this relation won't yield any rows. In this
389 * case, we mark the child as dummy right away. (We must do this
390 * immediately so that pruning works correctly when recursing in
391 * expand_partitioned_rtentry.)
392 */
393 if (parent)
394 {
395 AppendRelInfo *appinfo = root->append_rel_array[relid];
396
397 Assert(appinfo != NULL);
398 if (!apply_child_basequals(root, parent, rel, rte, appinfo))
399 {
400 /*
401 * Restriction clause reduced to constant FALSE or NULL. Mark as
402 * dummy so we won't scan this relation.
403 */
404 mark_dummy_rel(rel);
405 }
406 }
407
408 return rel;
409}
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
int32_t int32
Definition: c.h:534
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition: inherit.c:848
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1325
void * palloc0(Size size)
Definition: mcxt.c:1395
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ RTE_CTE
Definition: parsenodes.h:1049
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1050
@ RTE_VALUES
Definition: parsenodes.h:1048
@ RTE_SUBQUERY
Definition: parsenodes.h:1044
@ RTE_RESULT
Definition: parsenodes.h:1051
@ RTE_FUNCTION
Definition: parsenodes.h:1046
@ RTE_TABLEFUNC
Definition: parsenodes.h:1047
@ RTE_RELATION
Definition: parsenodes.h:1043
@ RELOPT_BASEREL
Definition: pathnodes.h:864
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:866
static int list_length(const List *l)
Definition: pg_list.h:152
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:124
RTEKind rtekind
Definition: parsenodes.h:1078

References RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, apply_child_basequals(), Assert(), RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_make_singleton(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RTEPermissionInfo::checkAsUser, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, elog, ERROR, get_relation_info(), getRTEPermissionInfo(), RelOptInfo::has_eclass_joins, RelOptInfo::indexlist, RangeTblEntry::inh, InvalidOid, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, list_length(), RelOptInfo::live_parts, makeNode, mark_dummy_rel(), RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, palloc0(), RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RelOptInfo::relids, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, root, RelOptInfo::rows, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RelOptInfo::rtekind, RelOptInfo::serverid, QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::unique_groupclause, RelOptInfo::unique_pathkeys, RelOptInfo::unique_rel, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by add_base_rels_to_query(), expand_appendrel_subquery(), expand_inherited_rtentry(), expand_partitioned_rtentry(), plan_cluster_use_sort(), plan_create_index_workers(), query_planner(), and recurse_set_operations().

expand_planner_arrays()

void expand_planner_arrays ( PlannerInforoot,
int  add_size 
)

Definition at line 163 of file relnode.c.

164{
165 int new_size;
166
167 Assert(add_size > 0);
168
169 new_size = root->simple_rel_array_size + add_size;
170
171 root->simple_rel_array =
172 repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
173
174 root->simple_rte_array =
175 repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
176
177 if (root->append_rel_array)
178 root->append_rel_array =
179 repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
180 else
181 root->append_rel_array =
182 palloc0_array(AppendRelInfo *, new_size);
183
184 root->simple_rel_array_size = new_size;
185}
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109
Size add_size(Size s1, Size s2)
Definition: shmem.c:493

References add_size(), Assert(), palloc0_array, repalloc0_array, and root.

Referenced by expand_inherited_rtentry(), and expand_partitioned_rtentry().

fetch_upper_rel()

RelOptInfo * fetch_upper_rel ( PlannerInforoot,
Relids  relids 
)

Definition at line 1464 of file relnode.c.

1465{
1466 RelOptInfo *upperrel;
1467 ListCell *lc;
1468
1469 /*
1470 * For the moment, our indexing data structure is just a List for each
1471 * relation kind. If we ever get so many of one kind that this stops
1472 * working well, we can improve it. No code outside this function should
1473 * assume anything about how to find a particular upperrel.
1474 */
1475
1476 /* If we already made this upperrel for the query, return it */
1477 foreach(lc, root->upper_rels[kind])
1478 {
1479 upperrel = (RelOptInfo *) lfirst(lc);
1480
1481 if (bms_equal(upperrel->relids, relids))
1482 return upperrel;
1483 }
1484
1485 upperrel = makeNode(RelOptInfo);
1486 upperrel->reloptkind = RELOPT_UPPER_REL;
1487 upperrel->relids = bms_copy(relids);
1488
1489 /* cheap startup cost is interesting iff not all tuples to be retrieved */
1490 upperrel->consider_startup = (root->tuple_fraction > 0);
1491 upperrel->consider_param_startup = false;
1492 upperrel->consider_parallel = false; /* might get changed later */
1493 upperrel->reltarget = create_empty_pathtarget();
1494 upperrel->pathlist = NIL;
1495 upperrel->cheapest_startup_path = NULL;
1496 upperrel->cheapest_total_path = NULL;
1498
1499 root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1500
1501 return upperrel;
1502}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
@ RELOPT_UPPER_REL
Definition: pathnodes.h:868

References bms_copy(), bms_equal(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_startup, create_empty_pathtarget(), lappend(), lfirst, makeNode, NIL, RelOptInfo::pathlist, RelOptInfo::relids, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, and root.

Referenced by add_rtes_to_flat_rtable(), build_setop_child_paths(), create_distinct_paths(), create_ordered_paths(), create_partial_distinct_paths(), create_partial_grouping_paths(), create_window_paths(), generate_nonunion_paths(), generate_recursion_path(), generate_union_paths(), grouping_planner(), make_grouping_rel(), make_subplan(), preprocess_minmax_aggregates(), set_subquery_pathlist(), set_subquery_size_estimates(), SS_process_ctes(), standard_planner(), and subquery_planner().

find_base_rel()

RelOptInfo * find_base_rel ( PlannerInforoot,
int  relid 
)

Definition at line 416 of file relnode.c.

417{
418 RelOptInfo *rel;
419
420 /* use an unsigned comparison to prevent negative array element access */
421 if ((uint32) relid < (uint32) root->simple_rel_array_size)
422 {
423 rel = root->simple_rel_array[relid];
424 if (rel)
425 return rel;
426 }
427
428 elog(ERROR, "no relation entry for relid %d", relid);
429
430 return NULL; /* keep compiler quiet */
431}
uint32_t uint32
Definition: c.h:538

References elog, ERROR, and root.

Referenced by add_base_clause_to_rel(), add_placeholders_to_base_rels(), add_vars_to_attr_needed(), add_vars_to_targetlist(), build_joinrel_tlist(), clause_selectivity_ext(), create_lateral_join_info(), distribute_row_identity_vars(), examine_simple_variable(), examine_variable(), finalize_plan(), find_childrel_parents(), find_join_input_rel(), find_single_rel_for_clauses(), get_foreign_key_join_selectivity(), get_matching_part_pairs(), get_rel_all_updated_cols(), get_translated_update_targetlist(), grouping_planner(), join_is_removable(), make_partition_pruneinfo(), make_partitionedrel_pruneinfo(), make_rel_from_joinlist(), postgresPlanForeignModify(), reduce_unique_semijoins(), remove_leftjoinrel_from_query(), set_append_rel_size(), set_base_rel_consider_startup(), set_subquery_size_estimates(), set_subqueryscan_references(), and var_is_nonnullable().

find_base_rel_ignore_join()

RelOptInfo * find_base_rel_ignore_join ( PlannerInforoot,
int  relid 
)

Definition at line 456 of file relnode.c.

457{
458 /* use an unsigned comparison to prevent negative array element access */
459 if ((uint32) relid < (uint32) root->simple_rel_array_size)
460 {
461 RelOptInfo *rel;
462 RangeTblEntry *rte;
463
464 rel = root->simple_rel_array[relid];
465 if (rel)
466 return rel;
467
468 /*
469 * We could just return NULL here, but for debugging purposes it seems
470 * best to actually verify that the relid is an outer join and not
471 * something weird.
472 */
473 rte = root->simple_rte_array[relid];
474 if (rte && rte->rtekind == RTE_JOIN && rte->jointype != JOIN_INNER)
475 return NULL;
476 }
477
478 elog(ERROR, "no relation entry for relid %d", relid);
479
480 return NULL; /* keep compiler quiet */
481}
JoinType jointype
Definition: parsenodes.h:1182

References elog, ERROR, JOIN_INNER, RangeTblEntry::jointype, root, RTE_JOIN, and RangeTblEntry::rtekind.

Referenced by add_join_clause_to_rels(), create_lateral_join_info(), find_appinfos_by_relids(), and remove_join_clause_from_rels().

find_base_rel_noerr()

RelOptInfo * find_base_rel_noerr ( PlannerInforoot,
int  relid 
)

Definition at line 438 of file relnode.c.

439{
440 /* use an unsigned comparison to prevent negative array element access */
441 if ((uint32) relid < (uint32) root->simple_rel_array_size)
442 return root->simple_rel_array[relid];
443 return NULL;
444}

References root.

Referenced by all_rows_selectable().

find_childrel_parents()

Relids find_childrel_parents ( PlannerInforoot,
RelOptInforel 
)

Definition at line 1514 of file relnode.c.

1515{
1516 Relids result = NULL;
1517
1519 Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1520
1521 do
1522 {
1523 AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1524 Index prelid = appinfo->parent_relid;
1525
1526 result = bms_add_member(result, prelid);
1527
1528 /* traverse up to the parent rel, loop if it's also a child rel */
1529 rel = find_base_rel(root, prelid);
1530 } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1531
1533
1534 return result;
1535}
unsigned int Index
Definition: c.h:619
Index parent_relid
Definition: pathnodes.h:3104

References Assert(), bms_add_member(), find_base_rel(), AppendRelInfo::parent_relid, RelOptInfo::relid, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and root.

Referenced by check_index_predicates(), and generate_implied_equalities_for_column().

find_join_rel()

RelOptInfo * find_join_rel ( PlannerInforoot,
Relids  relids 
)

Definition at line 529 of file relnode.c.

530{
531 /*
532 * Switch to using hash lookup when list grows "too long". The threshold
533 * is arbitrary and is known only here.
534 */
535 if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
537
538 /*
539 * Use either hashtable lookup or linear search, as appropriate.
540 *
541 * Note: the seemingly redundant hashkey variable is used to avoid taking
542 * the address of relids; unless the compiler is exceedingly smart, doing
543 * so would force relids out of a register and thus probably slow down the
544 * list-search case.
545 */
546 if (root->join_rel_hash)
547 {
548 Relids hashkey = relids;
549 JoinHashEntry *hentry;
550
551 hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
552 &hashkey,
553 HASH_FIND,
554 NULL);
555 if (hentry)
556 return hentry->join_rel;
557 }
558 else
559 {
560 ListCell *l;
561
562 foreach(l, root->join_rel_list)
563 {
564 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
565
566 if (bms_equal(rel->relids, relids))
567 return rel;
568 }
569 }
570
571 return NULL;
572}
@ HASH_FIND
Definition: hsearch.h:113
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:488

References bms_equal(), build_join_rel_hash(), HASH_FIND, hash_search(), JoinHashEntry::join_rel, lfirst, list_length(), RelOptInfo::relids, and root.

Referenced by build_child_join_rel(), build_join_rel(), examine_variable(), find_join_input_rel(), get_matching_part_pairs(), and postgresPlanDirectModify().

find_param_path_info()

ParamPathInfo * find_param_path_info ( RelOptInforel,
Relids  required_outer 
)

Definition at line 1894 of file relnode.c.

1895{
1896 ListCell *lc;
1897
1898 foreach(lc, rel->ppilist)
1899 {
1900 ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1901
1902 if (bms_equal(ppi->ppi_req_outer, required_outer))
1903 return ppi;
1904 }
1905
1906 return NULL;
1907}
Relids ppi_req_outer
Definition: pathnodes.h:1737

References bms_equal(), lfirst, ParamPathInfo::ppi_req_outer, and RelOptInfo::ppilist.

Referenced by get_appendrel_parampathinfo(), get_baserel_parampathinfo(), get_joinrel_parampathinfo(), and reparameterize_path_by_child().

get_appendrel_parampathinfo()

ParamPathInfo * get_appendrel_parampathinfo ( RelOptInfoappendrel,
Relids  required_outer 
)

Definition at line 1861 of file relnode.c.

1862{
1863 ParamPathInfo *ppi;
1864
1865 /* If rel has LATERAL refs, every path for it should account for them */
1866 Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1867
1868 /* Unparameterized paths have no ParamPathInfo */
1869 if (bms_is_empty(required_outer))
1870 return NULL;
1871
1872 Assert(!bms_overlap(appendrel->relids, required_outer));
1873
1874 /* If we already have a PPI for this parameterization, just return it */
1875 if ((ppi = find_param_path_info(appendrel, required_outer)))
1876 return ppi;
1877
1878 /* Else build the ParamPathInfo */
1879 ppi = makeNode(ParamPathInfo);
1880 ppi->ppi_req_outer = required_outer;
1881 ppi->ppi_rows = 0;
1882 ppi->ppi_clauses = NIL;
1883 ppi->ppi_serials = NULL;
1884 appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1885
1886 return ppi;
1887}
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
#define bms_is_empty(a)
Definition: bitmapset.h:118
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1894
Cardinality ppi_rows
Definition: pathnodes.h:1738
List * ppi_clauses
Definition: pathnodes.h:1739
Bitmapset * ppi_serials
Definition: pathnodes.h:1740

References Assert(), bms_is_empty, bms_is_subset(), bms_overlap(), find_param_path_info(), lappend(), RelOptInfo::lateral_relids, makeNode, NIL, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, ParamPathInfo::ppi_serials, RelOptInfo::ppilist, and RelOptInfo::relids.

Referenced by create_append_path().

get_baserel_parampathinfo()

ParamPathInfo * get_baserel_parampathinfo ( PlannerInforoot,
RelOptInfobaserel,
Relids  required_outer 
)

Definition at line 1550 of file relnode.c.

1552{
1553 ParamPathInfo *ppi;
1554 Relids joinrelids;
1555 List *pclauses;
1556 List *eqclauses;
1557 Bitmapset *pserials;
1558 double rows;
1559 ListCell *lc;
1560
1561 /* If rel has LATERAL refs, every path for it should account for them */
1562 Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1563
1564 /* Unparameterized paths have no ParamPathInfo */
1565 if (bms_is_empty(required_outer))
1566 return NULL;
1567
1568 Assert(!bms_overlap(baserel->relids, required_outer));
1569
1570 /* If we already have a PPI for this parameterization, just return it */
1571 if ((ppi = find_param_path_info(baserel, required_outer)))
1572 return ppi;
1573
1574 /*
1575 * Identify all joinclauses that are movable to this base rel given this
1576 * parameterization.
1577 */
1578 joinrelids = bms_union(baserel->relids, required_outer);
1579 pclauses = NIL;
1580 foreach(lc, baserel->joininfo)
1581 {
1582 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1583
1585 baserel->relids,
1586 joinrelids))
1587 pclauses = lappend(pclauses, rinfo);
1588 }
1589
1590 /*
1591 * Add in joinclauses generated by EquivalenceClasses, too. (These
1592 * necessarily satisfy join_clause_is_movable_into; but in assert-enabled
1593 * builds, let's verify that.)
1594 */
1596 joinrelids,
1597 required_outer,
1598 baserel,
1599 NULL);
1600#ifdef USE_ASSERT_CHECKING
1601 foreach(lc, eqclauses)
1602 {
1603 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1604
1606 baserel->relids,
1607 joinrelids));
1608 }
1609#endif
1610 pclauses = list_concat(pclauses, eqclauses);
1611
1612 /* Compute set of serial numbers of the enforced clauses */
1613 pserials = NULL;
1614 foreach(lc, pclauses)
1615 {
1616 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1617
1618 pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1619 }
1620
1621 /* Estimate the number of rows returned by the parameterized scan */
1622 rows = get_parameterized_baserel_size(root, baserel, pclauses);
1623
1624 /* And now we can build the ParamPathInfo */
1625 ppi = makeNode(ParamPathInfo);
1626 ppi->ppi_req_outer = required_outer;
1627 ppi->ppi_rows = rows;
1628 ppi->ppi_clauses = pclauses;
1629 ppi->ppi_serials = pserials;
1630 baserel->ppilist = lappend(baserel->ppilist, ppi);
1631
1632 return ppi;
1633}
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:5414
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:661
int rinfo_serial
Definition: pathnodes.h:2776

References Assert(), bms_add_member(), bms_is_empty, bms_is_subset(), bms_overlap(), bms_union(), find_param_path_info(), generate_join_implied_equalities(), get_parameterized_baserel_size(), join_clause_is_movable_into(), RelOptInfo::joininfo, lappend(), RelOptInfo::lateral_relids, lfirst, list_concat(), makeNode, NIL, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, ParamPathInfo::ppi_serials, RelOptInfo::ppilist, RelOptInfo::relids, RestrictInfo::rinfo_serial, and root.

Referenced by create_append_path(), create_bitmap_and_path(), create_bitmap_heap_path(), create_bitmap_or_path(), create_ctescan_path(), create_foreignscan_path(), create_functionscan_path(), create_gather_merge_path(), create_gather_path(), create_index_path(), create_namedtuplestorescan_path(), create_resultscan_path(), create_samplescan_path(), create_seqscan_path(), create_subqueryscan_path(), create_tablefuncscan_path(), create_tidrangescan_path(), create_tidscan_path(), create_valuesscan_path(), create_worktablescan_path(), postgresGetForeignPaths(), and reparameterize_path().

get_joinrel_parampathinfo()

ParamPathInfo * get_joinrel_parampathinfo ( PlannerInforoot,
RelOptInfojoinrel,
Pathouter_path,
Pathinner_path,
SpecialJoinInfosjinfo,
Relids  required_outer,
List **  restrict_clauses 
)

Definition at line 1664 of file relnode.c.

1670{
1671 ParamPathInfo *ppi;
1672 Relids join_and_req;
1673 Relids outer_and_req;
1674 Relids inner_and_req;
1675 List *pclauses;
1676 List *eclauses;
1677 List *dropped_ecs;
1678 double rows;
1679 ListCell *lc;
1680
1681 /* If rel has LATERAL refs, every path for it should account for them */
1682 Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1683
1684 /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1685 if (bms_is_empty(required_outer))
1686 return NULL;
1687
1688 Assert(!bms_overlap(joinrel->relids, required_outer));
1689
1690 /*
1691 * Identify all joinclauses that are movable to this join rel given this
1692 * parameterization. These are the clauses that are movable into this
1693 * join, but not movable into either input path. Treat an unparameterized
1694 * input path as not accepting parameterized clauses (because it won't,
1695 * per the shortcut exit above), even though the joinclause movement rules
1696 * might allow the same clauses to be moved into a parameterized path for
1697 * that rel.
1698 */
1699 join_and_req = bms_union(joinrel->relids, required_outer);
1700 if (outer_path->param_info)
1701 outer_and_req = bms_union(outer_path->parent->relids,
1702 PATH_REQ_OUTER(outer_path));
1703 else
1704 outer_and_req = NULL; /* outer path does not accept parameters */
1705 if (inner_path->param_info)
1706 inner_and_req = bms_union(inner_path->parent->relids,
1707 PATH_REQ_OUTER(inner_path));
1708 else
1709 inner_and_req = NULL; /* inner path does not accept parameters */
1710
1711 pclauses = NIL;
1712 foreach(lc, joinrel->joininfo)
1713 {
1714 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1715
1717 joinrel->relids,
1718 join_and_req) &&
1720 outer_path->parent->relids,
1721 outer_and_req) &&
1723 inner_path->parent->relids,
1724 inner_and_req))
1725 pclauses = lappend(pclauses, rinfo);
1726 }
1727
1728 /* Consider joinclauses generated by EquivalenceClasses, too */
1730 join_and_req,
1731 required_outer,
1732 joinrel,
1733 NULL);
1734 /* We only want ones that aren't movable to lower levels */
1735 dropped_ecs = NIL;
1736 foreach(lc, eclauses)
1737 {
1738 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1739
1741 joinrel->relids,
1742 join_and_req));
1744 outer_path->parent->relids,
1745 outer_and_req))
1746 continue; /* drop if movable into LHS */
1748 inner_path->parent->relids,
1749 inner_and_req))
1750 {
1751 /* drop if movable into RHS, but remember EC for use below */
1752 Assert(rinfo->left_ec == rinfo->right_ec);
1753 dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1754 continue;
1755 }
1756 pclauses = lappend(pclauses, rinfo);
1757 }
1758
1759 /*
1760 * EquivalenceClasses are harder to deal with than we could wish, because
1761 * of the fact that a given EC can generate different clauses depending on
1762 * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1763 * LHS and RHS of the current join and Z is in required_outer, and further
1764 * suppose that the inner_path is parameterized by both X and Z. The code
1765 * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1766 * and in the latter case will have discarded it as being movable into the
1767 * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1768 * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1769 * not have produced both, and we can't readily tell from here which one
1770 * it did pick. If we add no clause to this join, we'll end up with
1771 * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1772 * constrained to be equal to the other members of the EC. (When we come
1773 * to join Z to this X/Y path, we will certainly drop whichever EC clause
1774 * is generated at that join, so this omission won't get fixed later.)
1775 *
1776 * To handle this, for each EC we discarded such a clause from, try to
1777 * generate a clause connecting the required_outer rels to the join's LHS
1778 * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1779 * the clause can't be moved to the LHS, add it to the current join's
1780 * restriction clauses. (If an EC cannot generate such a clause then it
1781 * has nothing that needs to be enforced here, while if the clause can be
1782 * moved into the LHS then it should have been enforced within that path.)
1783 *
1784 * Note that we don't need similar processing for ECs whose clause was
1785 * considered to be movable into the LHS, because the LHS can't refer to
1786 * the RHS so there is no comparable ambiguity about what it might
1787 * actually be enforcing internally.
1788 */
1789 if (dropped_ecs)
1790 {
1791 Relids real_outer_and_req;
1792
1793 real_outer_and_req = bms_union(outer_path->parent->relids,
1794 required_outer);
1795 eclauses =
1797 dropped_ecs,
1798 real_outer_and_req,
1799 required_outer,
1800 outer_path->parent);
1801 foreach(lc, eclauses)
1802 {
1803 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1804
1806 outer_path->parent->relids,
1807 real_outer_and_req));
1808 if (!join_clause_is_movable_into(rinfo,
1809 outer_path->parent->relids,
1810 outer_and_req))
1811 pclauses = lappend(pclauses, rinfo);
1812 }
1813 }
1814
1815 /*
1816 * Now, attach the identified moved-down clauses to the caller's
1817 * restrict_clauses list. By using list_concat in this order, we leave
1818 * the original list structure of restrict_clauses undamaged.
1819 */
1820 *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1821
1822 /* If we already have a PPI for this parameterization, just return it */
1823 if ((ppi = find_param_path_info(joinrel, required_outer)))
1824 return ppi;
1825
1826 /* Estimate the number of rows returned by the parameterized join */
1827 rows = get_parameterized_joinrel_size(root, joinrel,
1828 outer_path,
1829 inner_path,
1830 sjinfo,
1831 *restrict_clauses);
1832
1833 /*
1834 * And now we can build the ParamPathInfo. No point in saving the
1835 * input-pair-dependent clause list, though.
1836 *
1837 * Note: in GEQO mode, we'll be called in a temporary memory context, but
1838 * the joinrel structure is there too, so no problem.
1839 */
1840 ppi = makeNode(ParamPathInfo);
1841 ppi->ppi_req_outer = required_outer;
1842 ppi->ppi_rows = rows;
1843 ppi->ppi_clauses = NIL;
1844 ppi->ppi_serials = NULL;
1845 joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1846
1847 return ppi;
1848}
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:5495
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1650
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1828

References Assert(), bms_is_empty, bms_is_subset(), bms_overlap(), bms_union(), find_param_path_info(), generate_join_implied_equalities(), generate_join_implied_equalities_for_ecs(), get_parameterized_joinrel_size(), join_clause_is_movable_into(), RelOptInfo::joininfo, lappend(), RelOptInfo::lateral_relids, lfirst, list_concat(), makeNode, NIL, PATH_REQ_OUTER, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, ParamPathInfo::ppi_serials, RelOptInfo::ppilist, RelOptInfo::relids, and root.

Referenced by create_hashjoin_path(), create_mergejoin_path(), and create_nestloop_path().

get_param_path_clause_serials()

Bitmapset * get_param_path_clause_serials ( Pathpath )

Definition at line 1915 of file relnode.c.

1916{
1917 if (path->param_info == NULL)
1918 return NULL; /* not parameterized */
1919
1920 /*
1921 * We don't currently support parameterized MergeAppend paths, as
1922 * explained in the comments for generate_orderedappend_paths.
1923 */
1924 Assert(!IsA(path, MergeAppendPath));
1925
1926 if (IsA(path, NestPath) ||
1927 IsA(path, MergePath) ||
1928 IsA(path, HashPath))
1929 {
1930 /*
1931 * For a join path, combine clauses enforced within either input path
1932 * with those enforced as joinrestrictinfo in this path. Note that
1933 * joinrestrictinfo may include some non-pushed-down clauses, but for
1934 * current purposes it's okay if we include those in the result. (To
1935 * be more careful, we could check for clause_relids overlapping the
1936 * path parameterization, but it's not worth the cycles for now.)
1937 */
1938 JoinPath *jpath = (JoinPath *) path;
1939 Bitmapset *pserials;
1940 ListCell *lc;
1941
1942 pserials = NULL;
1943 pserials = bms_add_members(pserials,
1945 pserials = bms_add_members(pserials,
1947 foreach(lc, jpath->joinrestrictinfo)
1948 {
1949 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1950
1951 pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1952 }
1953 return pserials;
1954 }
1955 else if (IsA(path, AppendPath))
1956 {
1957 /*
1958 * For an appendrel, take the intersection of the sets of clauses
1959 * enforced in each input path.
1960 */
1961 AppendPath *apath = (AppendPath *) path;
1962 Bitmapset *pserials;
1963 ListCell *lc;
1964
1965 pserials = NULL;
1966 foreach(lc, apath->subpaths)
1967 {
1968 Path *subpath = (Path *) lfirst(lc);
1969 Bitmapset *subserials;
1970
1972 if (lc == list_head(apath->subpaths))
1973 pserials = bms_copy(subserials);
1974 else
1975 pserials = bms_int_members(pserials, subserials);
1976 }
1977 return pserials;
1978 }
1979 else
1980 {
1981 /*
1982 * Otherwise, it's a baserel path and we can use the
1983 * previously-computed set of serial numbers.
1984 */
1985 return path->param_info->ppi_serials;
1986 }
1987}
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:311
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
Bitmapset * get_param_path_clause_serials(Path *path)
Definition: relnode.c:1915
List * subpaths
Definition: pathnodes.h:2091
Path * outerjoinpath
Definition: pathnodes.h:2207
Path * innerjoinpath
Definition: pathnodes.h:2208
List * joinrestrictinfo
Definition: pathnodes.h:2210
Definition: pathnodes.h:1778

References Assert(), bms_add_member(), bms_add_members(), bms_copy(), bms_int_members(), get_param_path_clause_serials(), JoinPath::innerjoinpath, IsA, JoinPath::joinrestrictinfo, lfirst, list_head(), JoinPath::outerjoinpath, RestrictInfo::rinfo_serial, subpath(), and AppendPath::subpaths.

Referenced by create_nestloop_path(), and get_param_path_clause_serials().

have_partkey_equi_join()

static bool have_partkey_equi_join ( PlannerInforoot,
RelOptInfojoinrel,
RelOptInforel1,
RelOptInforel2,
JoinType  jointype,
Listrestrictlist 
)
static

Definition at line 2069 of file relnode.c.

2072{
2073 PartitionScheme part_scheme = rel1->part_scheme;
2074 bool pk_known_equal[PARTITION_MAX_KEYS];
2075 int num_equal_pks;
2076 ListCell *lc;
2077
2078 /*
2079 * This function must only be called when the joined relations have same
2080 * partitioning scheme.
2081 */
2082 Assert(rel1->part_scheme == rel2->part_scheme);
2083 Assert(part_scheme);
2084
2085 /* We use a bool array to track which partkey columns are known equal */
2086 memset(pk_known_equal, 0, sizeof(pk_known_equal));
2087 /* ... as well as a count of how many are known equal */
2088 num_equal_pks = 0;
2089
2090 /* First, look through the join's restriction clauses */
2091 foreach(lc, restrictlist)
2092 {
2094 OpExpr *opexpr;
2095 Expr *expr1;
2096 Expr *expr2;
2097 bool strict_op;
2098 int ipk1;
2099 int ipk2;
2100
2101 /* If processing an outer join, only use its own join clauses. */
2102 if (IS_OUTER_JOIN(jointype) &&
2103 RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
2104 continue;
2105
2106 /* Skip clauses which can not be used for a join. */
2107 if (!rinfo->can_join)
2108 continue;
2109
2110 /* Skip clauses which are not equality conditions. */
2111 if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
2112 continue;
2113
2114 /* Should be OK to assume it's an OpExpr. */
2115 opexpr = castNode(OpExpr, rinfo->clause);
2116
2117 /* Match the operands to the relation. */
2118 if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
2119 bms_is_subset(rinfo->right_relids, rel2->relids))
2120 {
2121 expr1 = linitial(opexpr->args);
2122 expr2 = lsecond(opexpr->args);
2123 }
2124 else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
2125 bms_is_subset(rinfo->right_relids, rel1->relids))
2126 {
2127 expr1 = lsecond(opexpr->args);
2128 expr2 = linitial(opexpr->args);
2129 }
2130 else
2131 continue;
2132
2133 /*
2134 * Now we need to know whether the join operator is strict; see
2135 * comments in pathnodes.h.
2136 */
2137 strict_op = op_strict(opexpr->opno);
2138
2139 /*
2140 * Vars appearing in the relation's partition keys will not have any
2141 * varnullingrels, but those in expr1 and expr2 will if we're above
2142 * outer joins that could null the respective rels. It's okay to
2143 * match anyway, if the join operator is strict.
2144 */
2145 if (strict_op)
2146 {
2147 if (bms_overlap(rel1->relids, root->outer_join_rels))
2148 expr1 = (Expr *) remove_nulling_relids((Node *) expr1,
2149 root->outer_join_rels,
2150 NULL);
2151 if (bms_overlap(rel2->relids, root->outer_join_rels))
2152 expr2 = (Expr *) remove_nulling_relids((Node *) expr2,
2153 root->outer_join_rels,
2154 NULL);
2155 }
2156
2157 /*
2158 * Only clauses referencing the partition keys are useful for
2159 * partitionwise join.
2160 */
2161 ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
2162 if (ipk1 < 0)
2163 continue;
2164 ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
2165 if (ipk2 < 0)
2166 continue;
2167
2168 /*
2169 * If the clause refers to keys at different ordinal positions, it can
2170 * not be used for partitionwise join.
2171 */
2172 if (ipk1 != ipk2)
2173 continue;
2174
2175 /* Ignore clause if we already proved these keys equal. */
2176 if (pk_known_equal[ipk1])
2177 continue;
2178
2179 /* Reject if the partition key collation differs from the clause's. */
2180 if (rel1->part_scheme->partcollation[ipk1] != opexpr->inputcollid)
2181 return false;
2182
2183 /*
2184 * The clause allows partitionwise join only if it uses the same
2185 * operator family as that specified by the partition key.
2186 */
2187 if (part_scheme->strategy == PARTITION_STRATEGY_HASH)
2188 {
2189 if (!OidIsValid(rinfo->hashjoinoperator) ||
2190 !op_in_opfamily(rinfo->hashjoinoperator,
2191 part_scheme->partopfamily[ipk1]))
2192 continue;
2193 }
2194 else if (!list_member_oid(rinfo->mergeopfamilies,
2195 part_scheme->partopfamily[ipk1]))
2196 continue;
2197
2198 /* Mark the partition key as having an equi-join clause. */
2199 pk_known_equal[ipk1] = true;
2200
2201 /* We can stop examining clauses once we prove all keys equal. */
2202 if (++num_equal_pks == part_scheme->partnatts)
2203 return true;
2204 }
2205
2206 /*
2207 * Also check to see if any keys are known equal by equivclass.c. In most
2208 * cases there would have been a join restriction clause generated from
2209 * any EC that had such knowledge, but there might be no such clause, or
2210 * it might happen to constrain other members of the ECs than the ones we
2211 * are looking for.
2212 */
2213 for (int ipk = 0; ipk < part_scheme->partnatts; ipk++)
2214 {
2215 Oid btree_opfamily;
2216
2217 /* Ignore if we already proved these keys equal. */
2218 if (pk_known_equal[ipk])
2219 continue;
2220
2221 /*
2222 * We need a btree opfamily to ask equivclass.c about. If the
2223 * partopfamily is a hash opfamily, look up its equality operator, and
2224 * select some btree opfamily that that operator is part of. (Any
2225 * such opfamily should be good enough, since equivclass.c will track
2226 * multiple opfamilies as appropriate.)
2227 */
2228 if (part_scheme->strategy == PARTITION_STRATEGY_HASH)
2229 {
2230 Oid eq_op;
2231 List *eq_opfamilies;
2232
2233 eq_op = get_opfamily_member(part_scheme->partopfamily[ipk],
2234 part_scheme->partopcintype[ipk],
2235 part_scheme->partopcintype[ipk],
2237 if (!OidIsValid(eq_op))
2238 break; /* we're not going to succeed */
2239 eq_opfamilies = get_mergejoin_opfamilies(eq_op);
2240 if (eq_opfamilies == NIL)
2241 break; /* we're not going to succeed */
2242 btree_opfamily = linitial_oid(eq_opfamilies);
2243 }
2244 else
2245 btree_opfamily = part_scheme->partopfamily[ipk];
2246
2247 /*
2248 * We consider only non-nullable partition keys here; nullable ones
2249 * would not be treated as part of the same equivalence classes as
2250 * non-nullable ones.
2251 */
2252 foreach(lc, rel1->partexprs[ipk])
2253 {
2254 Node *expr1 = (Node *) lfirst(lc);
2255 ListCell *lc2;
2256 Oid partcoll1 = rel1->part_scheme->partcollation[ipk];
2257 Oid exprcoll1 = exprCollation(expr1);
2258
2259 foreach(lc2, rel2->partexprs[ipk])
2260 {
2261 Node *expr2 = (Node *) lfirst(lc2);
2262
2263 if (exprs_known_equal(root, expr1, expr2, btree_opfamily))
2264 {
2265 /*
2266 * Ensure that the collation of the expression matches
2267 * that of the partition key. Checking just one collation
2268 * (partcoll1 and exprcoll1) suffices because partcoll1
2269 * and partcoll2, as well as exprcoll1 and exprcoll2,
2270 * should be identical. This holds because both rel1 and
2271 * rel2 use the same PartitionScheme and expr1 and expr2
2272 * are equal.
2273 */
2274 if (partcoll1 == exprcoll1)
2275 {
2276 Oid partcoll2 PG_USED_FOR_ASSERTS_ONLY =
2277 rel2->part_scheme->partcollation[ipk];
2278 Oid exprcoll2 PG_USED_FOR_ASSERTS_ONLY =
2279 exprCollation(expr2);
2280
2281 Assert(partcoll2 == exprcoll2);
2282 pk_known_equal[ipk] = true;
2283 break;
2284 }
2285 }
2286 }
2287 if (pk_known_equal[ipk])
2288 break;
2289 }
2290
2291 if (pk_known_equal[ipk])
2292 {
2293 /* We can stop examining keys once we prove all keys equal. */
2294 if (++num_equal_pks == part_scheme->partnatts)
2295 return true;
2296 }
2297 else
2298 break; /* no chance to succeed, give up */
2299 }
2300
2301 return false;
2302}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:223
#define OidIsValid(objectId)
Definition: c.h:774
bool exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2, Oid opfamily)
Definition: equivclass.c:2648
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
bool op_strict(Oid opno)
Definition: lsyscache.c:1644
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:168
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:435
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:68
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:348
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:902
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2861
#define PARTITION_MAX_KEYS
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
#define linitial_oid(l)
Definition: pg_list.h:180
unsigned int Oid
Definition: postgres_ext.h:32
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: relnode.c:2316
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
Definition: rewriteManip.c:1331
#define HTEqualStrategyNumber
Definition: stratnum.h:41
Definition: primnodes.h:189
Definition: primnodes.h:846
Oid opno
Definition: primnodes.h:850
List * args
Definition: primnodes.h:868
Oid * partopfamily
Definition: pathnodes.h:613
Oid * partopcintype
Definition: pathnodes.h:614
Expr * clause
Definition: pathnodes.h:2704

References OpExpr::args, Assert(), bms_is_subset(), bms_overlap(), castNode, RestrictInfo::clause, exprCollation(), exprs_known_equal(), get_mergejoin_opfamilies(), get_opfamily_member(), HTEqualStrategyNumber, IS_OUTER_JOIN, lfirst, lfirst_node, linitial, linitial_oid, list_member_oid(), lsecond, match_expr_to_partition_keys(), NIL, OidIsValid, op_in_opfamily(), op_strict(), OpExpr::opno, PARTITION_MAX_KEYS, PARTITION_STRATEGY_HASH, PartitionSchemeData::partnatts, PartitionSchemeData::partopcintype, PartitionSchemeData::partopfamily, PG_USED_FOR_ASSERTS_ONLY, RelOptInfo::relids, remove_nulling_relids(), RINFO_IS_PUSHED_DOWN, root, and PartitionSchemeData::strategy.

Referenced by build_joinrel_partition_info().

match_expr_to_partition_keys()

static int match_expr_to_partition_keys ( Exprexpr,
RelOptInforel,
bool  strict_op 
)
static

Definition at line 2316 of file relnode.c.

2317{
2318 int cnt;
2319
2320 /* This function should be called only for partitioned relations. */
2321 Assert(rel->part_scheme);
2322 Assert(rel->partexprs);
2323 Assert(rel->nullable_partexprs);
2324
2325 /* Remove any relabel decorations. */
2326 while (IsA(expr, RelabelType))
2327 expr = (Expr *) (castNode(RelabelType, expr))->arg;
2328
2329 for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
2330 {
2331 ListCell *lc;
2332
2333 /* We can always match to the non-nullable partition keys. */
2334 foreach(lc, rel->partexprs[cnt])
2335 {
2336 if (equal(lfirst(lc), expr))
2337 return cnt;
2338 }
2339
2340 if (!strict_op)
2341 continue;
2342
2343 /*
2344 * If it's a strict join operator then a NULL partition key on one
2345 * side will not join to any partition key on the other side, and in
2346 * particular such a row can't join to a row from a different
2347 * partition on the other side. So, it's okay to search the nullable
2348 * partition keys as well.
2349 */
2350 foreach(lc, rel->nullable_partexprs[cnt])
2351 {
2352 if (equal(lfirst(lc), expr))
2353 return cnt;
2354 }
2355 }
2356
2357 return -1;
2358}
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
void * arg

References arg, Assert(), castNode, equal(), IsA, and lfirst.

Referenced by have_partkey_equi_join().

min_join_parameterization()

Relids min_join_parameterization ( PlannerInforoot,
Relids  joinrelids,
RelOptInfoouter_rel,
RelOptInfoinner_rel 
)

Definition at line 1028 of file relnode.c.

1032{
1033 Relids result;
1034
1035 /*
1036 * Basically we just need the union of the inputs' lateral_relids, less
1037 * whatever is already in the join.
1038 *
1039 * It's not immediately obvious that this is a valid way to compute the
1040 * result, because it might seem that we're ignoring possible lateral refs
1041 * of PlaceHolderVars that are due to be computed at the join but not in
1042 * either input. However, because create_lateral_join_info() already
1043 * charged all such PHV refs to each member baserel of the join, they'll
1044 * be accounted for already in the inputs' lateral_relids. Likewise, we
1045 * do not need to worry about doing transitive closure here, because that
1046 * was already accounted for in the original baserel lateral_relids.
1047 */
1048 result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
1049 result = bms_del_members(result, joinrelids);
1050 return result;
1051}

References bms_del_members(), bms_union(), and RelOptInfo::lateral_relids.

Referenced by build_join_rel(), and join_is_legal().

set_foreign_rel_properties()

static void set_foreign_rel_properties ( RelOptInfojoinrel,
RelOptInfoouter_rel,
RelOptInfoinner_rel 
)
static

Definition at line 591 of file relnode.c.

593{
594 if (OidIsValid(outer_rel->serverid) &&
595 inner_rel->serverid == outer_rel->serverid)
596 {
597 if (inner_rel->userid == outer_rel->userid)
598 {
599 joinrel->serverid = outer_rel->serverid;
600 joinrel->userid = outer_rel->userid;
601 joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
602 joinrel->fdwroutine = outer_rel->fdwroutine;
603 }
604 else if (!OidIsValid(inner_rel->userid) &&
605 outer_rel->userid == GetUserId())
606 {
607 joinrel->serverid = outer_rel->serverid;
608 joinrel->userid = outer_rel->userid;
609 joinrel->useridiscurrent = true;
610 joinrel->fdwroutine = outer_rel->fdwroutine;
611 }
612 else if (!OidIsValid(outer_rel->userid) &&
613 inner_rel->userid == GetUserId())
614 {
615 joinrel->serverid = outer_rel->serverid;
616 joinrel->userid = inner_rel->userid;
617 joinrel->useridiscurrent = true;
618 joinrel->fdwroutine = outer_rel->fdwroutine;
619 }
620 }
621}
Oid GetUserId(void)
Definition: miscinit.c:469

References GetUserId(), OidIsValid, RelOptInfo::serverid, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by build_child_join_rel(), and build_join_rel().

set_joinrel_partition_key_exprs()

static void set_joinrel_partition_key_exprs ( RelOptInfojoinrel,
RelOptInfoouter_rel,
RelOptInfoinner_rel,
JoinType  jointype 
)
static

Definition at line 2365 of file relnode.c.

2368{
2369 PartitionScheme part_scheme = joinrel->part_scheme;
2370 int partnatts = part_scheme->partnatts;
2371
2372 joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
2373 joinrel->nullable_partexprs =
2374 (List **) palloc0(sizeof(List *) * partnatts);
2375
2376 /*
2377 * The joinrel's partition expressions are the same as those of the input
2378 * rels, but we must properly classify them as nullable or not in the
2379 * joinrel's output. (Also, we add some more partition expressions if
2380 * it's a FULL JOIN.)
2381 */
2382 for (int cnt = 0; cnt < partnatts; cnt++)
2383 {
2384 /* mark these const to enforce that we copy them properly */
2385 const List *outer_expr = outer_rel->partexprs[cnt];
2386 const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
2387 const List *inner_expr = inner_rel->partexprs[cnt];
2388 const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
2389 List *partexpr = NIL;
2390 List *nullable_partexpr = NIL;
2391 ListCell *lc;
2392
2393 switch (jointype)
2394 {
2395 /*
2396 * A join relation resulting from an INNER join may be
2397 * regarded as partitioned by either of the inner and outer
2398 * relation keys. For example, A INNER JOIN B ON A.a = B.b
2399 * can be regarded as partitioned on either A.a or B.b. So we
2400 * add both keys to the joinrel's partexpr lists. However,
2401 * anything that was already nullable still has to be treated
2402 * as nullable.
2403 */
2404 case JOIN_INNER:
2405 partexpr = list_concat_copy(outer_expr, inner_expr);
2406 nullable_partexpr = list_concat_copy(outer_null_expr,
2407 inner_null_expr);
2408 break;
2409
2410 /*
2411 * A join relation resulting from a SEMI or ANTI join may be
2412 * regarded as partitioned by the outer relation keys. The
2413 * inner relation's keys are no longer interesting; since they
2414 * aren't visible in the join output, nothing could join to
2415 * them.
2416 */
2417 case JOIN_SEMI:
2418 case JOIN_ANTI:
2419 partexpr = list_copy(outer_expr);
2420 nullable_partexpr = list_copy(outer_null_expr);
2421 break;
2422
2423 /*
2424 * A join relation resulting from a LEFT OUTER JOIN likewise
2425 * may be regarded as partitioned on the (non-nullable) outer
2426 * relation keys. The inner (nullable) relation keys are okay
2427 * as partition keys for further joins as long as they involve
2428 * strict join operators.
2429 */
2430 case JOIN_LEFT:
2431 partexpr = list_copy(outer_expr);
2432 nullable_partexpr = list_concat_copy(inner_expr,
2433 outer_null_expr);
2434 nullable_partexpr = list_concat(nullable_partexpr,
2435 inner_null_expr);
2436 break;
2437
2438 /*
2439 * For FULL OUTER JOINs, both relations are nullable, so the
2440 * resulting join relation may be regarded as partitioned on
2441 * either of inner and outer relation keys, but only for joins
2442 * that involve strict join operators.
2443 */
2444 case JOIN_FULL:
2445 nullable_partexpr = list_concat_copy(outer_expr,
2446 inner_expr);
2447 nullable_partexpr = list_concat(nullable_partexpr,
2448 outer_null_expr);
2449 nullable_partexpr = list_concat(nullable_partexpr,
2450 inner_null_expr);
2451
2452 /*
2453 * Also add CoalesceExprs corresponding to each possible
2454 * full-join output variable (that is, left side coalesced to
2455 * right side), so that we can match equijoin expressions
2456 * using those variables. We really only need these for
2457 * columns merged by JOIN USING, and only with the pairs of
2458 * input items that correspond to the data structures that
2459 * parse analysis would build for such variables. But it's
2460 * hard to tell which those are, so just make all the pairs.
2461 * Extra items in the nullable_partexprs list won't cause big
2462 * problems. (It's possible that such items will get matched
2463 * to user-written COALESCEs, but it should still be valid to
2464 * partition on those, since they're going to be either the
2465 * partition column or NULL; it's the same argument as for
2466 * partitionwise nesting of any outer join.) We assume no
2467 * type coercions are needed to make the coalesce expressions,
2468 * since columns of different types won't have gotten
2469 * classified as the same PartitionScheme. Note that we
2470 * intentionally leave out the varnullingrels decoration that
2471 * would ordinarily appear on the Vars inside these
2472 * CoalesceExprs, because have_partkey_equi_join will strip
2473 * varnullingrels from the expressions it will compare to the
2474 * partexprs.
2475 */
2476 foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
2477 {
2478 Node *larg = (Node *) lfirst(lc);
2479 ListCell *lc2;
2480
2481 foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
2482 {
2483 Node *rarg = (Node *) lfirst(lc2);
2485
2486 c->coalescetype = exprType(larg);
2487 c->coalescecollid = exprCollation(larg);
2488 c->args = list_make2(larg, rarg);
2489 c->location = -1;
2490 nullable_partexpr = lappend(nullable_partexpr, c);
2491 }
2492 }
2493 break;
2494
2495 default:
2496 elog(ERROR, "unrecognized join type: %d", (int) jointype);
2497 }
2498
2499 joinrel->partexprs[cnt] = partexpr;
2500 joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2501 }
2502}
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
List * list_copy(const List *oldlist)
Definition: list.c:1573
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
@ JOIN_SEMI
Definition: nodes.h:317
@ JOIN_LEFT
Definition: nodes.h:304
@ JOIN_ANTI
Definition: nodes.h:318
#define list_make2(x1, x2)
Definition: pg_list.h:214
c
char * c
Definition: preproc-cursor.c:31

References elog, ERROR, exprCollation(), exprType(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, lappend(), lfirst, list_concat(), list_concat_copy(), list_copy(), list_make2, makeNode, NIL, palloc0(), and PartitionSchemeData::partnatts.

Referenced by build_joinrel_partition_info().

setup_simple_rel_arrays()

void setup_simple_rel_arrays ( PlannerInforoot )

Definition at line 94 of file relnode.c.

95{
96 int size;
97 Index rti;
98 ListCell *lc;
99
100 /* Arrays are accessed using RT indexes (1..N) */
101 size = list_length(root->parse->rtable) + 1;
102 root->simple_rel_array_size = size;
103
104 /*
105 * simple_rel_array is initialized to all NULLs, since no RelOptInfos
106 * exist yet. It'll be filled by later calls to build_simple_rel().
107 */
108 root->simple_rel_array = (RelOptInfo **)
109 palloc0(size * sizeof(RelOptInfo *));
110
111 /* simple_rte_array is an array equivalent of the rtable list */
112 root->simple_rte_array = (RangeTblEntry **)
113 palloc0(size * sizeof(RangeTblEntry *));
114 rti = 1;
115 foreach(lc, root->parse->rtable)
116 {
117 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
118
119 root->simple_rte_array[rti++] = rte;
120 }
121
122 /* append_rel_array is not needed if there are no AppendRelInfos */
123 if (root->append_rel_list == NIL)
124 {
125 root->append_rel_array = NULL;
126 return;
127 }
128
129 root->append_rel_array = (AppendRelInfo **)
130 palloc0(size * sizeof(AppendRelInfo *));
131
132 /*
133 * append_rel_array is filled with any already-existing AppendRelInfos,
134 * which currently could only come from UNION ALL flattening. We might
135 * add more later during inheritance expansion, but it's the
136 * responsibility of the expansion code to update the array properly.
137 */
138 foreach(lc, root->append_rel_list)
139 {
141 int child_relid = appinfo->child_relid;
142
143 /* Sanity check */
144 Assert(child_relid < size);
145
146 if (root->append_rel_array[child_relid])
147 elog(ERROR, "child relation already exists");
148
149 root->append_rel_array[child_relid] = appinfo;
150 }
151}
Index child_relid
Definition: pathnodes.h:3105

References Assert(), AppendRelInfo::child_relid, elog, ERROR, lfirst, lfirst_node, list_length(), NIL, palloc0(), and root.

Referenced by plan_cluster_use_sort(), plan_create_index_workers(), plan_set_operations(), and query_planner().

subbuild_joinrel_joinlist()

static List * subbuild_joinrel_joinlist ( RelOptInfojoinrel,
Listjoininfo_list,
Listnew_joininfo 
)
static

Definition at line 1412 of file relnode.c.

1415{
1416 ListCell *l;
1417
1418 /* Expected to be called only for join between parent relations. */
1419 Assert(joinrel->reloptkind == RELOPT_JOINREL);
1420
1421 foreach(l, joininfo_list)
1422 {
1423 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1424
1425 if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1426 {
1427 /*
1428 * This clause becomes a restriction clause for the joinrel, since
1429 * it refers to no outside rels. So we can ignore it in this
1430 * routine.
1431 */
1432 }
1433 else
1434 {
1435 /*
1436 * This clause is still a join clause at this level, so add it to
1437 * the new joininfo list, being careful to eliminate duplicates.
1438 * (Since RestrictInfo nodes in different joinlists will have been
1439 * multiply-linked rather than copied, pointer equality should be
1440 * a sufficient test.)
1441 */
1442 new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1443 }
1444 }
1445
1446 return new_joininfo;
1447}
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1356
Relids required_relids
Definition: pathnodes.h:2735

References Assert(), bms_is_subset(), lfirst, list_append_unique_ptr(), RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, and RestrictInfo::required_relids.

Referenced by build_joinrel_joinlist().

subbuild_joinrel_restrictlist()

static List * subbuild_joinrel_restrictlist ( PlannerInforoot,
RelOptInfojoinrel,
RelOptInfoinput_rel,
Relids  both_input_relids,
Listnew_restrictlist 
)
static

Definition at line 1346 of file relnode.c.

1351{
1352 ListCell *l;
1353
1354 foreach(l, input_rel->joininfo)
1355 {
1356 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1357
1358 if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1359 {
1360 /*
1361 * This clause should become a restriction clause for the joinrel,
1362 * since it refers to no outside rels. However, if it's a clone
1363 * clause then it might be too late to evaluate it, so we have to
1364 * check. (If it is too late, just ignore the clause, taking it
1365 * on faith that another clone was or will be selected.) Clone
1366 * clauses should always be outer-join clauses, so we compare
1367 * against both_input_relids.
1368 */
1369 if (rinfo->has_clone || rinfo->is_clone)
1370 {
1371 Assert(!RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids));
1372 if (!bms_is_subset(rinfo->required_relids, both_input_relids))
1373 continue;
1374 if (bms_overlap(rinfo->incompatible_relids, both_input_relids))
1375 continue;
1376 }
1377 else
1378 {
1379 /*
1380 * For non-clone clauses, we just Assert it's OK. These might
1381 * be either join or filter clauses; if it's a join clause
1382 * then it should not refer to the current join's output.
1383 * (There is little point in checking incompatible_relids,
1384 * because it'll be NULL.)
1385 */
1386 Assert(RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids) ||
1388 both_input_relids));
1389 }
1390
1391 /*
1392 * OK, so add it to the list, being careful to eliminate
1393 * duplicates. (Since RestrictInfo nodes in different joinlists
1394 * will have been multiply-linked rather than copied, pointer
1395 * equality should be a sufficient test.)
1396 */
1397 new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1398 }
1399 else
1400 {
1401 /*
1402 * This clause is still a join clause at this level, so we ignore
1403 * it in this routine.
1404 */
1405 }
1406 }
1407
1408 return new_restrictlist;
1409}
bool is_clone
Definition: pathnodes.h:2717
Relids incompatible_relids
Definition: pathnodes.h:2738
bool has_clone
Definition: pathnodes.h:2716

References Assert(), bms_is_subset(), bms_overlap(), RestrictInfo::has_clone, RestrictInfo::incompatible_relids, RestrictInfo::is_clone, RelOptInfo::joininfo, lfirst, list_append_unique_ptr(), RelOptInfo::relids, RestrictInfo::required_relids, and RINFO_IS_PUSHED_DOWN.

Referenced by build_joinrel_restrictlist().

AltStyle によって変換されたページ (->オリジナル) /