Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*-------------------------------------------------------------------------
 *
 * joininfo.c
 *    joininfo list manipulation routines
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    src/backend/optimizer/util/joininfo.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
 
#include "nodes/makefuncs.h"
#include "optimizer/joininfo.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
 
 
/*
 * have_relevant_joinclause
 *      Detect whether there is a joinclause that involves
 *      the two given relations.
 *
 * Note: the joinclause does not have to be evaluable with only these two
 * relations.  This is intentional.  For example consider
 *      SELECT * FROM a, b, c WHERE a.x = (b.y + c.z)
 * If a is much larger than the other tables, it may be worthwhile to
 * cross-join b and c and then use an inner indexscan on a.x.  Therefore
 * we should consider this joinclause as reason to join b to c, even though
 * it can't be applied at that join step.
 */
bool
have_relevant_joinclause(PlannerInfo *root,
                         RelOptInfo *rel1, RelOptInfo *rel2)
{
    bool        result = false;
    List       *joininfo;
    Relids      other_relids;
    ListCell   *l;
 
    /*
     * We could scan either relation's joininfo list; may as well use the
     * shorter one.
     */
    if (list_length(rel1->joininfo) <= list_length(rel2->joininfo))
    {
        joininfo = rel1->joininfo;
        other_relids = rel2->relids;
    }
    else
    {
        joininfo = rel2->joininfo;
        other_relids = rel1->relids;
    }
 
    foreach(l, joininfo)
    {
        RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
        if (bms_overlap(other_relids, rinfo->required_relids))
        {
            result = true;
            break;
        }
    }
 
    /*
     * We also need to check the EquivalenceClass data structure, which might
     * contain relationships not emitted into the joininfo lists.
     */
    if (!result && rel1->has_eclass_joins && rel2->has_eclass_joins)
        result = have_relevant_eclass_joinclause(root, rel1, rel2);
 
    return result;
}
 
 
/*
 * add_join_clause_to_rels
 *    Add 'restrictinfo' to the joininfo list of each relation it requires.
 *
 * Note that the same copy of the restrictinfo node is linked to by all the
 * lists it is in.  This allows us to exploit caching of information about
 * the restriction clause (but we must be careful that the information does
 * not depend on context).
 *
 * 'restrictinfo' describes the join clause
 * 'join_relids' is the set of relations participating in the join clause
 *               (some of these could be outer joins)
 */
void
add_join_clause_to_rels(PlannerInfo *root,
                        RestrictInfo *restrictinfo,
                        Relids join_relids)
{
    int         cur_relid;
 
    /* Don't add the clause if it is always true */
    if (restriction_is_always_true(root, restrictinfo))
        return;
 
    /*
     * Substitute the origin qual with constant-FALSE if it is provably always
     * false.
     *
     * Note that we need to keep the same rinfo_serial, since it is in
     * practice the same condition.  We also need to reset the
     * last_rinfo_serial counter, which is essential to ensure that the
     * RestrictInfos for the "same" qual condition get identical serial
     * numbers (see deconstruct_distribute_oj_quals).
     */
    if (restriction_is_always_false(root, restrictinfo))
    {
        int         save_rinfo_serial = restrictinfo->rinfo_serial;
        int         save_last_rinfo_serial = root->last_rinfo_serial;
 
        restrictinfo = make_restrictinfo(root,
                                         (Expr *) makeBoolConst(false, false),
                                         restrictinfo->is_pushed_down,
                                         restrictinfo->has_clone,
                                         restrictinfo->is_clone,
                                         restrictinfo->pseudoconstant,
                                         0, /* security_level */
                                         restrictinfo->required_relids,
                                         restrictinfo->incompatible_relids,
                                         restrictinfo->outer_relids);
        restrictinfo->rinfo_serial = save_rinfo_serial;
        root->last_rinfo_serial = save_last_rinfo_serial;
    }
 
    cur_relid = -1;
    while ((cur_relid = bms_next_member(join_relids, cur_relid)) >= 0)
    {
        RelOptInfo *rel = find_base_rel_ignore_join(root, cur_relid);
 
        /* We only need to add the clause to baserels */
        if (rel == NULL)
            continue;
        rel->joininfo = lappend(rel->joininfo, restrictinfo);
    }
}