~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/optimizer/path/indxpath.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * indxpath.c
 
4
 *        Routines to determine which indices are usable for scanning a
 
5
 *        given relation, and create IndexPaths accordingly.
 
6
 *
 
7
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
8
 * Portions Copyright (c) 1994, Regents of the University of California
 
9
 *
 
10
 *
 
11
 * IDENTIFICATION
 
12
 *        $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.167.4.2 2005-04-20 21:48:12 tgl Exp $
 
13
 *
 
14
 *-------------------------------------------------------------------------
 
15
 */
 
16
#include "postgres.h"
 
17
 
 
18
#include <math.h>
 
19
 
 
20
#include "access/nbtree.h"
 
21
#include "catalog/pg_amop.h"
 
22
#include "catalog/pg_namespace.h"
 
23
#include "catalog/pg_opclass.h"
 
24
#include "catalog/pg_operator.h"
 
25
#include "catalog/pg_proc.h"
 
26
#include "catalog/pg_type.h"
 
27
#include "executor/executor.h"
 
28
#include "nodes/makefuncs.h"
 
29
#include "optimizer/clauses.h"
 
30
#include "optimizer/cost.h"
 
31
#include "optimizer/pathnode.h"
 
32
#include "optimizer/paths.h"
 
33
#include "optimizer/restrictinfo.h"
 
34
#include "optimizer/var.h"
 
35
#include "parser/parse_expr.h"
 
36
#include "rewrite/rewriteManip.h"
 
37
#include "utils/builtins.h"
 
38
#include "utils/catcache.h"
 
39
#include "utils/lsyscache.h"
 
40
#include "utils/pg_locale.h"
 
41
#include "utils/selfuncs.h"
 
42
#include "utils/syscache.h"
 
43
 
 
44
 
 
45
/*
 
46
 * DoneMatchingIndexKeys() - MACRO
 
47
 */
 
48
#define DoneMatchingIndexKeys(classes)  (classes[0] == InvalidOid)
 
49
 
 
50
#define is_indexable_operator(clause,opclass,indexkey_on_left) \
 
51
        (indexable_operator(clause,opclass,indexkey_on_left) != InvalidOid)
 
52
 
 
53
 
 
54
static List *group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index);
 
55
static List *group_clauses_by_indexkey_for_join(Query *root,
 
56
                                                                   RelOptInfo *rel, IndexOptInfo *index,
 
57
                                                                   Relids outer_relids,
 
58
                                                                   JoinType jointype, bool isouterjoin);
 
59
static bool match_clause_to_indexcol(RelOptInfo *rel, IndexOptInfo *index,
 
60
                                                 int indexcol, Oid opclass,
 
61
                                                 RestrictInfo *rinfo);
 
62
static bool match_join_clause_to_indexcol(RelOptInfo *rel, IndexOptInfo *index,
 
63
                                                          int indexcol, Oid opclass,
 
64
                                                          RestrictInfo *rinfo);
 
65
static Oid indexable_operator(Expr *clause, Oid opclass,
 
66
                                   bool indexkey_on_left);
 
67
static bool pred_test_restrict_list(Expr *predicate, List *restrictinfo_list);
 
68
static bool pred_test_recurse_restrict(Expr *predicate, Node *clause);
 
69
static bool pred_test_recurse_pred(Expr *predicate, Node *clause);
 
70
static bool pred_test_simple_clause(Expr *predicate, Node *clause);
 
71
static Relids indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index);
 
72
static Path *make_innerjoin_index_path(Query *root,
 
73
                                                  RelOptInfo *rel, IndexOptInfo *index,
 
74
                                                  List *clausegroups);
 
75
static bool match_index_to_operand(Node *operand, int indexcol,
 
76
                                           RelOptInfo *rel, IndexOptInfo *index);
 
77
static bool match_special_index_operator(Expr *clause, Oid opclass,
 
78
                                                         bool indexkey_on_left);
 
79
static List *expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass);
 
80
static List *prefix_quals(Node *leftop, Oid opclass,
 
81
                         Const *prefix, Pattern_Prefix_Status pstatus);
 
82
static List *network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass,
 
83
                                         Datum rightop);
 
84
static Datum string_to_datum(const char *str, Oid datatype);
 
85
static Const *string_to_const(const char *str, Oid datatype);
 
86
 
 
87
 
 
88
/*
 
89
 * create_index_paths()
 
90
 *        Generate all interesting index paths for the given relation.
 
91
 *        Candidate paths are added to the rel's pathlist (using add_path).
 
92
 *
 
93
 * To be considered for an index scan, an index must match one or more
 
94
 * restriction clauses or join clauses from the query's qual condition,
 
95
 * or match the query's ORDER BY condition.
 
96
 *
 
97
 * There are two basic kinds of index scans.  A "plain" index scan uses
 
98
 * only restriction clauses (possibly none at all) in its indexqual,
 
99
 * so it can be applied in any context.  An "innerjoin" index scan uses
 
100
 * join clauses (plus restriction clauses, if available) in its indexqual.
 
101
 * Therefore it can only be used as the inner relation of a nestloop
 
102
 * join against an outer rel that includes all the other rels mentioned
 
103
 * in its join clauses.  In that context, values for the other rels'
 
104
 * attributes are available and fixed during any one scan of the indexpath.
 
105
 *
 
106
 * An IndexPath is generated and submitted to add_path() for each plain index
 
107
 * scan this routine deems potentially interesting for the current query.
 
108
 *
 
109
 * We also determine the set of other relids that participate in join
 
110
 * clauses that could be used with each index.  The actually best innerjoin
 
111
 * path will be generated for each outer relation later on, but knowing the
 
112
 * set of potential otherrels allows us to identify equivalent outer relations
 
113
 * and avoid repeated computation.
 
114
 *
 
115
 * 'rel' is the relation for which we want to generate index paths
 
116
 *
 
117
 * Note: check_partial_indexes() must have been run previously.
 
118
 */
 
119
void
 
120
create_index_paths(Query *root, RelOptInfo *rel)
 
121
{
 
122
        Relids          all_join_outerrelids = NULL;
 
123
        ListCell   *ilist;
 
124
 
 
125
        foreach(ilist, rel->indexlist)
 
126
        {
 
127
                IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
 
128
                List       *restrictclauses;
 
129
                List       *index_pathkeys;
 
130
                List       *useful_pathkeys;
 
131
                bool            index_is_ordered;
 
132
                Relids          join_outerrelids;
 
133
 
 
134
                /* Ignore partial indexes that do not match the query */
 
135
                if (index->indpred != NIL && !index->predOK)
 
136
                        continue;
 
137
 
 
138
                /*
 
139
                 * 1. Match the index against non-OR restriction clauses. (OR
 
140
                 * clauses will be considered later by orindxpath.c.)
 
141
                 */
 
142
                restrictclauses = group_clauses_by_indexkey(rel, index);
 
143
 
 
144
                /*
 
145
                 * 2. Compute pathkeys describing index's ordering, if any, then
 
146
                 * see how many of them are actually useful for this query.
 
147
                 */
 
148
                index_is_ordered = OidIsValid(index->ordering[0]);
 
149
                if (index_is_ordered)
 
150
                {
 
151
                        index_pathkeys = build_index_pathkeys(root, rel, index,
 
152
                                                                                                  ForwardScanDirection);
 
153
                        useful_pathkeys = truncate_useless_pathkeys(root, rel,
 
154
                                                                                                                index_pathkeys);
 
155
                }
 
156
                else
 
157
                        useful_pathkeys = NIL;
 
158
 
 
159
                /*
 
160
                 * 3. Generate an indexscan path if there are relevant restriction
 
161
                 * clauses OR the index ordering is potentially useful for later
 
162
                 * merging or final output ordering.
 
163
                 *
 
164
                 * If there is a predicate, consider it anyway since the index
 
165
                 * predicate has already been found to match the query.  The
 
166
                 * selectivity of the predicate might alone make the index useful.
 
167
                 *
 
168
                 * Note: not all index AMs support scans with no restriction clauses.
 
169
                 * We assume here that the AM does so if and only if it supports
 
170
                 * ordered scans.  (It would probably be better if there were a
 
171
                 * specific flag for this in pg_am, but there's not.)
 
172
                 */
 
173
                if (restrictclauses != NIL ||
 
174
                        useful_pathkeys != NIL ||
 
175
                        (index->indpred != NIL && index_is_ordered))
 
176
                        add_path(rel, (Path *)
 
177
                                         create_index_path(root, rel, index,
 
178
                                                                           restrictclauses,
 
179
                                                                           useful_pathkeys,
 
180
                                                                           index_is_ordered ?
 
181
                                                                           ForwardScanDirection :
 
182
                                                                           NoMovementScanDirection));
 
183
 
 
184
                /*
 
185
                 * 4. If the index is ordered, a backwards scan might be
 
186
                 * interesting. Currently this is only possible for a DESC query
 
187
                 * result ordering.
 
188
                 */
 
189
                if (index_is_ordered)
 
190
                {
 
191
                        index_pathkeys = build_index_pathkeys(root, rel, index,
 
192
                                                                                                  BackwardScanDirection);
 
193
                        useful_pathkeys = truncate_useless_pathkeys(root, rel,
 
194
                                                                                                                index_pathkeys);
 
195
                        if (useful_pathkeys != NIL)
 
196
                                add_path(rel, (Path *)
 
197
                                                 create_index_path(root, rel, index,
 
198
                                                                                   restrictclauses,
 
199
                                                                                   useful_pathkeys,
 
200
                                                                                   BackwardScanDirection));
 
201
                }
 
202
 
 
203
                /*
 
204
                 * 5. Examine join clauses to see which ones are potentially
 
205
                 * usable with this index, and generate the set of all other
 
206
                 * relids that participate in such join clauses.  We'll use this
 
207
                 * set later to recognize outer rels that are equivalent for
 
208
                 * joining purposes. We compute both per-index and
 
209
                 * overall-for-relation sets.
 
210
                 */
 
211
                join_outerrelids = indexable_outerrelids(rel, index);
 
212
                index->outer_relids = join_outerrelids;
 
213
                all_join_outerrelids = bms_add_members(all_join_outerrelids,
 
214
                                                                                           join_outerrelids);
 
215
        }
 
216
 
 
217
        rel->index_outer_relids = all_join_outerrelids;
 
218
}
 
219
 
 
220
 
 
221
/****************************************************************************
 
222
 *                              ----  ROUTINES TO CHECK RESTRICTIONS  ----
 
223
 ****************************************************************************/
 
224
 
 
225
 
 
226
/*
 
227
 * group_clauses_by_indexkey
 
228
 *        Find restriction clauses that can be used with an index.
 
229
 *
 
230
 * 'rel' is the node of the relation itself.
 
231
 * 'index' is a index on 'rel'.
 
232
 *
 
233
 * Returns a list of sublists of RestrictInfo nodes for clauses that can be
 
234
 * used with this index.  Each sublist contains clauses that can be used
 
235
 * with one index key (in no particular order); the top list is ordered by
 
236
 * index key.  (This is depended on by expand_indexqual_conditions().)
 
237
 *
 
238
 * Note that in a multi-key index, we stop if we find a key that cannot be
 
239
 * used with any clause.  For example, given an index on (A,B,C), we might
 
240
 * return ((C1 C2) (C3 C4)) if we find that clauses C1 and C2 use column A,
 
241
 * clauses C3 and C4 use column B, and no clauses use column C.  But if
 
242
 * no clauses match B we will return ((C1 C2)), whether or not there are
 
243
 * clauses matching column C, because the executor couldn't use them anyway.
 
244
 * Therefore, there are no empty sublists in the result.
 
245
 */
 
246
static List *
 
247
group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index)
 
248
{
 
249
        List       *clausegroup_list = NIL;
 
250
        List       *restrictinfo_list = rel->baserestrictinfo;
 
251
        int                     indexcol = 0;
 
252
        Oid                *classes = index->classlist;
 
253
 
 
254
        if (restrictinfo_list == NIL)
 
255
                return NIL;
 
256
 
 
257
        do
 
258
        {
 
259
                Oid                     curClass = classes[0];
 
260
                List       *clausegroup = NIL;
 
261
                ListCell   *l;
 
262
 
 
263
                foreach(l, restrictinfo_list)
 
264
                {
 
265
                        RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
266
 
 
267
                        if (match_clause_to_indexcol(rel,
 
268
                                                                                 index,
 
269
                                                                                 indexcol,
 
270
                                                                                 curClass,
 
271
                                                                                 rinfo))
 
272
                                clausegroup = lappend(clausegroup, rinfo);
 
273
                }
 
274
 
 
275
                /*
 
276
                 * If no clauses match this key, we're done; we don't want to look
 
277
                 * at keys to its right.
 
278
                 */
 
279
                if (clausegroup == NIL)
 
280
                        break;
 
281
 
 
282
                clausegroup_list = lappend(clausegroup_list, clausegroup);
 
283
 
 
284
                indexcol++;
 
285
                classes++;
 
286
 
 
287
        } while (!DoneMatchingIndexKeys(classes));
 
288
 
 
289
        return clausegroup_list;
 
290
}
 
291
 
 
292
/*
 
293
 * group_clauses_by_indexkey_for_join
 
294
 *        Generate a list of sublists of clauses that can be used with an index
 
295
 *        to scan the inner side of a nestloop join.
 
296
 *
 
297
 * This is much like group_clauses_by_indexkey(), but we consider both
 
298
 * join and restriction clauses.  Any joinclause that uses only otherrels
 
299
 * in the specified outer_relids is fair game.  But there must be at least
 
300
 * one such joinclause in the final list, otherwise we return NIL indicating
 
301
 * that this index isn't interesting as an inner indexscan.  (A scan using
 
302
 * only restriction clauses shouldn't be created here, because a regular Path
 
303
 * will already have been generated for it.)
 
304
 */
 
305
static List *
 
306
group_clauses_by_indexkey_for_join(Query *root,
 
307
                                                                   RelOptInfo *rel, IndexOptInfo *index,
 
308
                                                                   Relids outer_relids,
 
309
                                                                   JoinType jointype, bool isouterjoin)
 
310
{
 
311
        List       *clausegroup_list = NIL;
 
312
        bool            jfound = false;
 
313
        int                     indexcol = 0;
 
314
        Oid                *classes = index->classlist;
 
315
 
 
316
        do
 
317
        {
 
318
                Oid                     curClass = classes[0];
 
319
                List       *clausegroup = NIL;
 
320
                int                     numsources;
 
321
                ListCell   *l;
 
322
 
 
323
                /*
 
324
                 * We can always use plain restriction clauses for the rel.  We
 
325
                 * scan these first because we want them first in the clausegroup
 
326
                 * list for the convenience of remove_redundant_join_clauses,
 
327
                 * which can never remove non-join clauses and hence won't be able
 
328
                 * to get rid of a non-join clause if it appears after a join
 
329
                 * clause it is redundant with.
 
330
                 */
 
331
                foreach(l, rel->baserestrictinfo)
 
332
                {
 
333
                        RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
334
 
 
335
                        /* Can't use pushed-down clauses in outer join */
 
336
                        if (isouterjoin && rinfo->is_pushed_down)
 
337
                                continue;
 
338
 
 
339
                        if (match_clause_to_indexcol(rel,
 
340
                                                                                 index,
 
341
                                                                                 indexcol,
 
342
                                                                                 curClass,
 
343
                                                                                 rinfo))
 
344
                                clausegroup = lappend(clausegroup, rinfo);
 
345
                }
 
346
 
 
347
                /* found anything in base restrict list? */
 
348
                numsources = (clausegroup != NIL) ? 1 : 0;
 
349
 
 
350
                /* Look for joinclauses that are usable with given outer_relids */
 
351
                foreach(l, rel->joininfo)
 
352
                {
 
353
                        JoinInfo   *joininfo = (JoinInfo *) lfirst(l);
 
354
                        bool            jfoundhere = false;
 
355
                        ListCell   *j;
 
356
 
 
357
                        if (!bms_is_subset(joininfo->unjoined_relids, outer_relids))
 
358
                                continue;
 
359
 
 
360
                        foreach(j, joininfo->jinfo_restrictinfo)
 
361
                        {
 
362
                                RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
 
363
 
 
364
                                /* Can't use pushed-down clauses in outer join */
 
365
                                if (isouterjoin && rinfo->is_pushed_down)
 
366
                                        continue;
 
367
 
 
368
                                if (match_join_clause_to_indexcol(rel,
 
369
                                                                                                  index,
 
370
                                                                                                  indexcol,
 
371
                                                                                                  curClass,
 
372
                                                                                                  rinfo))
 
373
                                {
 
374
                                        clausegroup = lappend(clausegroup, rinfo);
 
375
                                        if (!jfoundhere)
 
376
                                        {
 
377
                                                jfoundhere = true;
 
378
                                                jfound = true;
 
379
                                                numsources++;
 
380
                                        }
 
381
                                }
 
382
                        }
 
383
                }
 
384
 
 
385
                /*
 
386
                 * If we found clauses in more than one list, we may now have
 
387
                 * clauses that are known redundant.  Get rid of 'em.
 
388
                 */
 
389
                if (numsources > 1)
 
390
                {
 
391
                        clausegroup = remove_redundant_join_clauses(root,
 
392
                                                                                                                clausegroup,
 
393
                                                                                                                jointype);
 
394
                }
 
395
 
 
396
                /*
 
397
                 * If no clauses match this key, we're done; we don't want to look
 
398
                 * at keys to its right.
 
399
                 */
 
400
                if (clausegroup == NIL)
 
401
                        break;
 
402
 
 
403
                clausegroup_list = lappend(clausegroup_list, clausegroup);
 
404
 
 
405
                indexcol++;
 
406
                classes++;
 
407
 
 
408
        } while (!DoneMatchingIndexKeys(classes));
 
409
 
 
410
        /* if no join clause was matched then forget it, per comments above */
 
411
        if (!jfound)
 
412
                return NIL;
 
413
 
 
414
        return clausegroup_list;
 
415
}
 
416
 
 
417
 
 
418
/*
 
419
 * group_clauses_by_indexkey_for_or
 
420
 *        Generate a list of sublists of clauses that can be used with an index
 
421
 *        to find rows matching an OR subclause.
 
422
 *
 
423
 * This is essentially just like group_clauses_by_indexkey() except that
 
424
 * we can use the given clause (or any AND subclauses of it) as well as
 
425
 * top-level restriction clauses of the relation.  Furthermore, we demand
 
426
 * that at least one such use be made, otherwise we fail and return NIL.
 
427
 * (Any path we made without such a use would be redundant with non-OR
 
428
 * indexscans.  Compare also group_clauses_by_indexkey_for_join.)
 
429
 *
 
430
 * XXX When we generate an indexqual list that uses both the OR subclause
 
431
 * and top-level restriction clauses, we end up with a slightly inefficient
 
432
 * plan because create_indexscan_plan is not very bright about figuring out
 
433
 * which restriction clauses are implied by the generated indexqual condition.
 
434
 * Currently we'll end up rechecking both the OR clause and the top-level
 
435
 * restriction clause as qpquals.  FIXME someday.
 
436
 */
 
437
List *
 
438
group_clauses_by_indexkey_for_or(RelOptInfo *rel,
 
439
                                                                 IndexOptInfo *index,
 
440
                                                                 Expr *orsubclause)
 
441
{
 
442
        List       *clausegroup_list = NIL;
 
443
        bool            matched = false;
 
444
        int                     indexcol = 0;
 
445
        Oid                *classes = index->classlist;
 
446
 
 
447
        do
 
448
        {
 
449
                Oid                     curClass = classes[0];
 
450
                List       *clausegroup = NIL;
 
451
                ListCell   *item;
 
452
 
 
453
                /* Try to match the OR subclause to the index key */
 
454
                if (IsA(orsubclause, RestrictInfo))
 
455
                {
 
456
                        if (match_clause_to_indexcol(rel, index,
 
457
                                                                                 indexcol, curClass,
 
458
                                                                                 (RestrictInfo *) orsubclause))
 
459
                        {
 
460
                                clausegroup = lappend(clausegroup, orsubclause);
 
461
                                matched = true;
 
462
                        }
 
463
                }
 
464
                else if (and_clause((Node *) orsubclause))
 
465
                {
 
466
                        foreach(item, ((BoolExpr *) orsubclause)->args)
 
467
                        {
 
468
                                RestrictInfo *subsubclause = (RestrictInfo *) lfirst(item);
 
469
 
 
470
                                if (IsA(subsubclause, RestrictInfo) &&
 
471
                                        match_clause_to_indexcol(rel, index,
 
472
                                                                                         indexcol, curClass,
 
473
                                                                                         subsubclause))
 
474
                                {
 
475
                                        clausegroup = lappend(clausegroup, subsubclause);
 
476
                                        matched = true;
 
477
                                }
 
478
                        }
 
479
                }
 
480
 
 
481
                /*
 
482
                 * If we found no clauses for this indexkey in the OR subclause
 
483
                 * itself, try looking in the rel's top-level restriction list.
 
484
                 *
 
485
                 * XXX should we always search the top-level list?      Slower but could
 
486
                 * sometimes yield a better plan.
 
487
                 */
 
488
                if (clausegroup == NIL)
 
489
                {
 
490
                        foreach(item, rel->baserestrictinfo)
 
491
                        {
 
492
                                RestrictInfo *rinfo = (RestrictInfo *) lfirst(item);
 
493
 
 
494
                                if (match_clause_to_indexcol(rel, index,
 
495
                                                                                         indexcol, curClass,
 
496
                                                                                         rinfo))
 
497
                                        clausegroup = lappend(clausegroup, rinfo);
 
498
                        }
 
499
                }
 
500
 
 
501
                /*
 
502
                 * If still no clauses match this key, we're done; we don't want
 
503
                 * to look at keys to its right.
 
504
                 */
 
505
                if (clausegroup == NIL)
 
506
                        break;
 
507
 
 
508
                clausegroup_list = lappend(clausegroup_list, clausegroup);
 
509
 
 
510
                indexcol++;
 
511
                classes++;
 
512
        } while (!DoneMatchingIndexKeys(classes));
 
513
 
 
514
        /* if OR clause was not used then forget it, per comments above */
 
515
        if (!matched)
 
516
                return NIL;
 
517
 
 
518
        return clausegroup_list;
 
519
}
 
520
 
 
521
 
 
522
/*
 
523
 * match_clause_to_indexcol()
 
524
 *        Determines whether a restriction clause matches a column of an index.
 
525
 *
 
526
 *        To match, the clause:
 
527
 *
 
528
 *        (1)  must be in the form (indexkey op const) or (const op indexkey);
 
529
 *                 and
 
530
 *        (2)  must contain an operator which is in the same class as the index
 
531
 *                 operator for this column, or is a "special" operator as recognized
 
532
 *                 by match_special_index_operator().
 
533
 *
 
534
 *        Presently, the executor can only deal with indexquals that have the
 
535
 *        indexkey on the left, so we can only use clauses that have the indexkey
 
536
 *        on the right if we can commute the clause to put the key on the left.
 
537
 *        We do not actually do the commuting here, but we check whether a
 
538
 *        suitable commutator operator is available.
 
539
 *
 
540
 * 'rel' is the relation of interest.
 
541
 * 'index' is an index on 'rel'.
 
542
 * 'indexcol' is a column number of 'index' (counting from 0).
 
543
 * 'opclass' is the corresponding operator class.
 
544
 * 'rinfo' is the clause to be tested (as a RestrictInfo node).
 
545
 *
 
546
 * Returns true if the clause can be used with this index key.
 
547
 *
 
548
 * NOTE:  returns false if clause is an OR or AND clause; it is the
 
549
 * responsibility of higher-level routines to cope with those.
 
550
 */
 
551
static bool
 
552
match_clause_to_indexcol(RelOptInfo *rel,
 
553
                                                 IndexOptInfo *index,
 
554
                                                 int indexcol,
 
555
                                                 Oid opclass,
 
556
                                                 RestrictInfo *rinfo)
 
557
{
 
558
        Expr       *clause = rinfo->clause;
 
559
        Node       *leftop,
 
560
                           *rightop;
 
561
 
 
562
        /* Clause must be a binary opclause. */
 
563
        if (!is_opclause(clause))
 
564
                return false;
 
565
        leftop = get_leftop(clause);
 
566
        rightop = get_rightop(clause);
 
567
        if (!leftop || !rightop)
 
568
                return false;
 
569
 
 
570
        /*
 
571
         * Check for clauses of the form: (indexkey operator constant) or
 
572
         * (constant operator indexkey). Anything that is a "pseudo constant"
 
573
         * expression will do.
 
574
         */
 
575
        if (match_index_to_operand(leftop, indexcol, rel, index) &&
 
576
                is_pseudo_constant_clause_relids(rightop, rinfo->right_relids))
 
577
        {
 
578
                if (is_indexable_operator(clause, opclass, true))
 
579
                        return true;
 
580
 
 
581
                /*
 
582
                 * If we didn't find a member of the index's opclass, see whether
 
583
                 * it is a "special" indexable operator.
 
584
                 */
 
585
                if (match_special_index_operator(clause, opclass, true))
 
586
                        return true;
 
587
                return false;
 
588
        }
 
589
 
 
590
        if (match_index_to_operand(rightop, indexcol, rel, index) &&
 
591
                is_pseudo_constant_clause_relids(leftop, rinfo->left_relids))
 
592
        {
 
593
                if (is_indexable_operator(clause, opclass, false))
 
594
                        return true;
 
595
 
 
596
                /*
 
597
                 * If we didn't find a member of the index's opclass, see whether
 
598
                 * it is a "special" indexable operator.
 
599
                 */
 
600
                if (match_special_index_operator(clause, opclass, false))
 
601
                        return true;
 
602
                return false;
 
603
        }
 
604
 
 
605
        return false;
 
606
}
 
607
 
 
608
/*
 
609
 * match_join_clause_to_indexcol()
 
610
 *        Determines whether a join clause matches a column of an index.
 
611
 *
 
612
 *        To match, the clause:
 
613
 *
 
614
 *        (1)  must be in the form (indexkey op others) or (others op indexkey),
 
615
 *                 where others is an expression involving only vars of the other
 
616
 *                 relation(s); and
 
617
 *        (2)  must contain an operator which is in the same class as the index
 
618
 *                 operator for this column, or is a "special" operator as recognized
 
619
 *                 by match_special_index_operator().
 
620
 *
 
621
 *        As above, we must be able to commute the clause to put the indexkey
 
622
 *        on the left.
 
623
 *
 
624
 *        Note that we already know that the clause as a whole uses vars from
 
625
 *        the interesting set of relations.  But we need to defend against
 
626
 *        expressions like (a.f1 OP (b.f2 OP a.f3)); that's not processable by
 
627
 *        an indexscan nestloop join, whereas (a.f1 OP (b.f2 OP c.f3)) is.
 
628
 *
 
629
 * 'rel' is the relation of interest.
 
630
 * 'index' is an index on 'rel'.
 
631
 * 'indexcol' is a column number of 'index' (counting from 0).
 
632
 * 'opclass' is the corresponding operator class.
 
633
 * 'rinfo' is the clause to be tested (as a RestrictInfo node).
 
634
 *
 
635
 * Returns true if the clause can be used with this index key.
 
636
 *
 
637
 * NOTE:  returns false if clause is an OR or AND clause; it is the
 
638
 * responsibility of higher-level routines to cope with those.
 
639
 */
 
640
static bool
 
641
match_join_clause_to_indexcol(RelOptInfo *rel,
 
642
                                                          IndexOptInfo *index,
 
643
                                                          int indexcol,
 
644
                                                          Oid opclass,
 
645
                                                          RestrictInfo *rinfo)
 
646
{
 
647
        Expr       *clause = rinfo->clause;
 
648
        Node       *leftop,
 
649
                           *rightop;
 
650
 
 
651
        /* Clause must be a binary opclause. */
 
652
        if (!is_opclause(clause))
 
653
                return false;
 
654
        leftop = get_leftop(clause);
 
655
        rightop = get_rightop(clause);
 
656
        if (!leftop || !rightop)
 
657
                return false;
 
658
 
 
659
        /*
 
660
         * Check for an indexqual that could be handled by a nestloop join. We
 
661
         * need the index key to be compared against an expression that uses
 
662
         * none of the indexed relation's vars and contains no volatile
 
663
         * functions.
 
664
         */
 
665
        if (match_index_to_operand(leftop, indexcol, rel, index))
 
666
        {
 
667
                Relids          othervarnos = rinfo->right_relids;
 
668
                bool            isIndexable;
 
669
 
 
670
                isIndexable =
 
671
                        !bms_overlap(rel->relids, othervarnos) &&
 
672
                        !contain_volatile_functions(rightop) &&
 
673
                        is_indexable_operator(clause, opclass, true);
 
674
                return isIndexable;
 
675
        }
 
676
 
 
677
        if (match_index_to_operand(rightop, indexcol, rel, index))
 
678
        {
 
679
                Relids          othervarnos = rinfo->left_relids;
 
680
                bool            isIndexable;
 
681
 
 
682
                isIndexable =
 
683
                        !bms_overlap(rel->relids, othervarnos) &&
 
684
                        !contain_volatile_functions(leftop) &&
 
685
                        is_indexable_operator(clause, opclass, false);
 
686
                return isIndexable;
 
687
        }
 
688
 
 
689
        return false;
 
690
}
 
691
 
 
692
/*
 
693
 * indexable_operator
 
694
 *        Does a binary opclause contain an operator matching the index opclass?
 
695
 *
 
696
 * If the indexkey is on the right, what we actually want to know
 
697
 * is whether the operator has a commutator operator that matches
 
698
 * the index's opclass.
 
699
 *
 
700
 * Returns the OID of the matching operator, or InvalidOid if no match.
 
701
 * (Formerly, this routine might return a binary-compatible operator
 
702
 * rather than the original one, but that kluge is history.)
 
703
 */
 
704
static Oid
 
705
indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
 
706
{
 
707
        Oid                     expr_op = ((OpExpr *) clause)->opno;
 
708
        Oid                     commuted_op;
 
709
 
 
710
        /* Get the commuted operator if necessary */
 
711
        if (indexkey_on_left)
 
712
                commuted_op = expr_op;
 
713
        else
 
714
                commuted_op = get_commutator(expr_op);
 
715
        if (commuted_op == InvalidOid)
 
716
                return InvalidOid;
 
717
 
 
718
        /* OK if the (commuted) operator is a member of the index's opclass */
 
719
        if (op_in_opclass(commuted_op, opclass))
 
720
                return expr_op;
 
721
 
 
722
        return InvalidOid;
 
723
}
 
724
 
 
725
/****************************************************************************
 
726
 *                              ----  ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS      ----
 
727
 ****************************************************************************/
 
728
 
 
729
/*
 
730
 * check_partial_indexes
 
731
 *              Check each partial index of the relation, and mark it predOK or not
 
732
 *              depending on whether the predicate is satisfied for this query.
 
733
 */
 
734
void
 
735
check_partial_indexes(Query *root, RelOptInfo *rel)
 
736
{
 
737
        List       *restrictinfo_list = rel->baserestrictinfo;
 
738
        ListCell   *ilist;
 
739
 
 
740
        foreach(ilist, rel->indexlist)
 
741
        {
 
742
                IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
 
743
 
 
744
                /*
 
745
                 * If this is a partial index, we can only use it if it passes the
 
746
                 * predicate test.
 
747
                 */
 
748
                if (index->indpred == NIL)
 
749
                        continue;                       /* ignore non-partial indexes */
 
750
 
 
751
                index->predOK = pred_test(index->indpred, restrictinfo_list);
 
752
        }
 
753
}
 
754
 
 
755
/*
 
756
 * pred_test
 
757
 *        Does the "predicate inclusion test" for partial indexes.
 
758
 *
 
759
 *        Recursively checks whether the clauses in restrictinfo_list imply
 
760
 *        that the given predicate is true.
 
761
 *
 
762
 *        This routine (together with the routines it calls) iterates over
 
763
 *        ANDs in the predicate first, then breaks down the restriction list
 
764
 *        to its constituent AND/OR elements, and iterates over ORs
 
765
 *        in the predicate last.  This order is important to make the test
 
766
 *        succeed whenever possible. --Nels, Jan '93
 
767
 *
 
768
 *        For example, a restriction (a OR b) certainly implies a predicate
 
769
 *        (a OR b OR c), but no one element of the predicate is individually
 
770
 *        implied by the restriction.  By expanding the predicate ORs last
 
771
 *        we are able to prove that the whole predicate is implied by each arm
 
772
 *        of the restriction.  Conversely consider predicate (a AND b) with
 
773
 *        restriction (a AND b AND c).  This should be implied but we will
 
774
 *        fail to prove it if we dissect the restriction first.
 
775
 *
 
776
 *        The top-level List structure of each list corresponds to an AND list.
 
777
 *        We assume that canonicalize_qual() has been applied and so there
 
778
 *        are no explicit ANDs immediately below the top-level List structure.
 
779
 *        (If this is not true we might fail to prove an implication that is
 
780
 *        valid, but no worse consequences will ensue.)
 
781
 */
 
782
bool
 
783
pred_test(List *predicate_list, List *restrictinfo_list)
 
784
{
 
785
        ListCell   *pred;
 
786
 
 
787
        /*
 
788
         * Note: if Postgres tried to optimize queries by forming equivalence
 
789
         * classes over equi-joined attributes (i.e., if it recognized that a
 
790
         * qualification such as "where a.b=c.d and a.b=5" could make use of
 
791
         * an index on c.d), then we could use that equivalence class info
 
792
         * here with joininfo_list to do more complete tests for the usability
 
793
         * of a partial index.  For now, the test only uses restriction
 
794
         * clauses (those in restrictinfo_list). --Nels, Dec '92
 
795
         *
 
796
         * XXX as of 7.1, equivalence class info *is* available.  Consider
 
797
         * improving this code as foreseen by Nels.
 
798
         */
 
799
 
 
800
        if (predicate_list == NIL)
 
801
                return true;                    /* no predicate: the index is usable */
 
802
        if (restrictinfo_list == NIL)
 
803
                return false;                   /* no restriction clauses: the test must
 
804
                                                                 * fail */
 
805
 
 
806
        /* Take care of the AND semantics of the top-level predicate list */
 
807
        foreach(pred, predicate_list)
 
808
        {
 
809
                /*
 
810
                 * if any clause is not implied, the whole predicate is not
 
811
                 * implied.
 
812
                 */
 
813
                if (!pred_test_restrict_list(lfirst(pred), restrictinfo_list))
 
814
                        return false;
 
815
        }
 
816
        return true;
 
817
}
 
818
 
 
819
 
 
820
/*
 
821
 * pred_test_restrict_list
 
822
 *        Does the "predicate inclusion test" for one AND clause of a predicate
 
823
 *        expression.  Here we take care of the AND semantics of the top-level
 
824
 *        restrictinfo list.
 
825
 */
 
826
static bool
 
827
pred_test_restrict_list(Expr *predicate, List *restrictinfo_list)
 
828
{
 
829
        ListCell   *item;
 
830
 
 
831
        foreach(item, restrictinfo_list)
 
832
        {
 
833
                /* if any clause implies the predicate, return true */
 
834
                if (pred_test_recurse_restrict(predicate,
 
835
                                                                           (Node *) lfirst(item)))
 
836
                        return true;
 
837
        }
 
838
        return false;
 
839
}
 
840
 
 
841
 
 
842
/*
 
843
 * pred_test_recurse_restrict
 
844
 *        Does the "predicate inclusion test" for one AND clause of a predicate
 
845
 *        expression.  Here we recursively deal with the possibility that the
 
846
 *        restriction-list element is itself an AND or OR structure; also,
 
847
 *        we strip off RestrictInfo nodes to find bare qualifier expressions.
 
848
 */
 
849
static bool
 
850
pred_test_recurse_restrict(Expr *predicate, Node *clause)
 
851
{
 
852
        List       *items;
 
853
        ListCell   *item;
 
854
 
 
855
        Assert(clause != NULL);
 
856
        if (IsA(clause, RestrictInfo))
 
857
        {
 
858
                RestrictInfo *restrictinfo = (RestrictInfo *) clause;
 
859
 
 
860
                return pred_test_recurse_restrict(predicate,
 
861
                                                                                  (Node *) restrictinfo->clause);
 
862
        }
 
863
        else if (or_clause(clause))
 
864
        {
 
865
                items = ((BoolExpr *) clause)->args;
 
866
                foreach(item, items)
 
867
                {
 
868
                        /* if any OR item doesn't imply the predicate, clause doesn't */
 
869
                        if (!pred_test_recurse_restrict(predicate, lfirst(item)))
 
870
                                return false;
 
871
                }
 
872
                return true;
 
873
        }
 
874
        else if (and_clause(clause))
 
875
        {
 
876
                items = ((BoolExpr *) clause)->args;
 
877
                foreach(item, items)
 
878
                {
 
879
                        /*
 
880
                         * if any AND item implies the predicate, the whole clause
 
881
                         * does
 
882
                         */
 
883
                        if (pred_test_recurse_restrict(predicate, lfirst(item)))
 
884
                                return true;
 
885
                }
 
886
                return false;
 
887
        }
 
888
        else
 
889
                return pred_test_recurse_pred(predicate, clause);
 
890
}
 
891
 
 
892
 
 
893
/*
 
894
 * pred_test_recurse_pred
 
895
 *        Does the "predicate inclusion test" for one conjunct of a predicate
 
896
 *        expression.  Here we recursively deal with the possibility that the
 
897
 *        predicate conjunct is itself an AND or OR structure.
 
898
 */
 
899
static bool
 
900
pred_test_recurse_pred(Expr *predicate, Node *clause)
 
901
{
 
902
        List       *items;
 
903
        ListCell   *item;
 
904
 
 
905
        Assert(predicate != NULL);
 
906
        if (or_clause((Node *) predicate))
 
907
        {
 
908
                items = ((BoolExpr *) predicate)->args;
 
909
                foreach(item, items)
 
910
                {
 
911
                        /* if any item is implied, the whole predicate is implied */
 
912
                        if (pred_test_recurse_pred(lfirst(item), clause))
 
913
                                return true;
 
914
                }
 
915
                return false;
 
916
        }
 
917
        else if (and_clause((Node *) predicate))
 
918
        {
 
919
                items = ((BoolExpr *) predicate)->args;
 
920
                foreach(item, items)
 
921
                {
 
922
                        /*
 
923
                         * if any item is not implied, the whole predicate is not
 
924
                         * implied
 
925
                         */
 
926
                        if (!pred_test_recurse_pred(lfirst(item), clause))
 
927
                                return false;
 
928
                }
 
929
                return true;
 
930
        }
 
931
        else
 
932
                return pred_test_simple_clause(predicate, clause);
 
933
}
 
934
 
 
935
 
 
936
/*
 
937
 * Define an "operator implication table" for btree operators ("strategies").
 
938
 *
 
939
 * The strategy numbers defined by btree indexes (see access/skey.h) are:
 
940
 *              (1) <   (2) <=   (3) =   (4) >=   (5) >
 
941
 * and in addition we use (6) to represent <>.  <> is not a btree-indexable
 
942
 * operator, but we assume here that if the equality operator of a btree
 
943
 * opclass has a negator operator, the negator behaves as <> for the opclass.
 
944
 *
 
945
 * The interpretation of:
 
946
 *
 
947
 *              test_op = BT_implic_table[given_op-1][target_op-1]
 
948
 *
 
949
 * where test_op, given_op and target_op are strategy numbers (from 1 to 6)
 
950
 * of btree operators, is as follows:
 
951
 *
 
952
 *       If you know, for some ATTR, that "ATTR given_op CONST1" is true, and you
 
953
 *       want to determine whether "ATTR target_op CONST2" must also be true, then
 
954
 *       you can use "CONST2 test_op CONST1" as a test.  If this test returns true,
 
955
 *       then the target expression must be true; if the test returns false, then
 
956
 *       the target expression may be false.
 
957
 *
 
958
 * An entry where test_op == 0 means the implication cannot be determined,
 
959
 * i.e., this test should always be considered false.
 
960
 */
 
961
 
 
962
#define BTLT BTLessStrategyNumber
 
963
#define BTLE BTLessEqualStrategyNumber
 
964
#define BTEQ BTEqualStrategyNumber
 
965
#define BTGE BTGreaterEqualStrategyNumber
 
966
#define BTGT BTGreaterStrategyNumber
 
967
#define BTNE 6
 
968
 
 
969
static const StrategyNumber
 
970
                        BT_implic_table[6][6] = {
 
971
/*
 
972
 *                      The target operator:
 
973
 *
 
974
 *         LT   LE         EQ    GE    GT        NE
 
975
 */
 
976
        {BTGE, BTGE, 0, 0, 0, BTGE},    /* LT */
 
977
        {BTGT, BTGE, 0, 0, 0, BTGT},    /* LE */
 
978
        {BTGT, BTGE, BTEQ, BTLE, BTLT, BTNE},           /* EQ */
 
979
        {0, 0, 0, BTLE, BTLT, BTLT},    /* GE */
 
980
        {0, 0, 0, BTLE, BTLE, BTLE},    /* GT */
 
981
        {0, 0, 0, 0, 0, BTEQ}           /* NE */
 
982
};
 
983
 
 
984
 
 
985
/*----------
 
986
 * pred_test_simple_clause
 
987
 *        Does the "predicate inclusion test" for a "simple clause" predicate
 
988
 *        and a "simple clause" restriction.
 
989
 *
 
990
 * We have three strategies for determining whether one simple clause
 
991
 * implies another:
 
992
 *
 
993
 * A simple and general way is to see if they are equal(); this works for any
 
994
 * kind of expression.  (Actually, there is an implied assumption that the
 
995
 * functions in the expression are immutable, ie dependent only on their input
 
996
 * arguments --- but this was checked for the predicate by CheckPredicate().)
 
997
 *
 
998
 * When the predicate is of the form "foo IS NOT NULL", we can conclude that
 
999
 * the predicate is implied if the clause is a strict operator or function
 
1000
 * that has "foo" as an input.  In this case the clause must yield NULL when
 
1001
 * "foo" is NULL, which we can take as equivalent to FALSE because we know
 
1002
 * we are within an AND/OR subtree of a WHERE clause.  (Again, "foo" is
 
1003
 * already known immutable, so the clause will certainly always fail.)
 
1004
 *
 
1005
 * Our other way works only for binary boolean opclauses of the form
 
1006
 * "foo op constant", where "foo" is the same in both clauses.  The operators
 
1007
 * and constants can be different but the operators must be in the same btree
 
1008
 * operator class.      We use the above operator implication table to be able to
 
1009
 * derive implications between nonidentical clauses.  (Note: "foo" is known
 
1010
 * immutable, and constants are surely immutable, but we have to check that
 
1011
 * the operators are too.  As of 8.0 it's possible for opclasses to contain
 
1012
 * operators that are merely stable, and we dare not make deductions with
 
1013
 * these.)
 
1014
 *
 
1015
 * Eventually, rtree operators could also be handled by defining an
 
1016
 * appropriate "RT_implic_table" array.
 
1017
 *----------
 
1018
 */
 
1019
static bool
 
1020
pred_test_simple_clause(Expr *predicate, Node *clause)
 
1021
{
 
1022
        Node       *leftop,
 
1023
                           *rightop;
 
1024
        Node       *pred_var,
 
1025
                           *clause_var;
 
1026
        Const      *pred_const,
 
1027
                           *clause_const;
 
1028
        bool            pred_var_on_left,
 
1029
                                clause_var_on_left,
 
1030
                                pred_op_negated;
 
1031
        Oid                     pred_op,
 
1032
                                clause_op,
 
1033
                                pred_op_negator,
 
1034
                                clause_op_negator,
 
1035
                                test_op = InvalidOid;
 
1036
        Oid                     opclass_id;
 
1037
        bool            found = false;
 
1038
        StrategyNumber pred_strategy,
 
1039
                                clause_strategy,
 
1040
                                test_strategy;
 
1041
        Oid                     clause_subtype;
 
1042
        Expr       *test_expr;
 
1043
        ExprState  *test_exprstate;
 
1044
        Datum           test_result;
 
1045
        bool            isNull;
 
1046
        CatCList   *catlist;
 
1047
        int                     i;
 
1048
        EState     *estate;
 
1049
        MemoryContext oldcontext;
 
1050
 
 
1051
        /* First try the equal() test */
 
1052
        if (equal((Node *) predicate, clause))
 
1053
                return true;
 
1054
 
 
1055
        /* Next try the IS NOT NULL case */
 
1056
        if (predicate && IsA(predicate, NullTest) &&
 
1057
                ((NullTest *) predicate)->nulltesttype == IS_NOT_NULL)
 
1058
        {
 
1059
                Expr       *nonnullarg = ((NullTest *) predicate)->arg;
 
1060
 
 
1061
                if (is_opclause(clause) &&
 
1062
                        list_member(((OpExpr *) clause)->args, nonnullarg) &&
 
1063
                        op_strict(((OpExpr *) clause)->opno))
 
1064
                        return true;
 
1065
                if (is_funcclause(clause) &&
 
1066
                        list_member(((FuncExpr *) clause)->args, nonnullarg) &&
 
1067
                        func_strict(((FuncExpr *) clause)->funcid))
 
1068
                        return true;
 
1069
                return false;                   /* we can't succeed below... */
 
1070
        }
 
1071
 
 
1072
        /*
 
1073
         * Can't do anything more unless they are both binary opclauses with a
 
1074
         * Const on one side, and identical subexpressions on the other sides.
 
1075
         * Note we don't have to think about binary relabeling of the Const
 
1076
         * node, since that would have been folded right into the Const.
 
1077
         *
 
1078
         * If either Const is null, we also fail right away; this assumes that
 
1079
         * the test operator will always be strict.
 
1080
         */
 
1081
        if (!is_opclause(predicate))
 
1082
                return false;
 
1083
        leftop = get_leftop(predicate);
 
1084
        rightop = get_rightop(predicate);
 
1085
        if (rightop == NULL)
 
1086
                return false;                   /* not a binary opclause */
 
1087
        if (IsA(rightop, Const))
 
1088
        {
 
1089
                pred_var = leftop;
 
1090
                pred_const = (Const *) rightop;
 
1091
                pred_var_on_left = true;
 
1092
        }
 
1093
        else if (IsA(leftop, Const))
 
1094
        {
 
1095
                pred_var = rightop;
 
1096
                pred_const = (Const *) leftop;
 
1097
                pred_var_on_left = false;
 
1098
        }
 
1099
        else
 
1100
                return false;                   /* no Const to be found */
 
1101
        if (pred_const->constisnull)
 
1102
                return false;
 
1103
 
 
1104
        if (!is_opclause(clause))
 
1105
                return false;
 
1106
        leftop = get_leftop((Expr *) clause);
 
1107
        rightop = get_rightop((Expr *) clause);
 
1108
        if (rightop == NULL)
 
1109
                return false;                   /* not a binary opclause */
 
1110
        if (IsA(rightop, Const))
 
1111
        {
 
1112
                clause_var = leftop;
 
1113
                clause_const = (Const *) rightop;
 
1114
                clause_var_on_left = true;
 
1115
        }
 
1116
        else if (IsA(leftop, Const))
 
1117
        {
 
1118
                clause_var = rightop;
 
1119
                clause_const = (Const *) leftop;
 
1120
                clause_var_on_left = false;
 
1121
        }
 
1122
        else
 
1123
                return false;                   /* no Const to be found */
 
1124
        if (clause_const->constisnull)
 
1125
                return false;
 
1126
 
 
1127
        /*
 
1128
         * Check for matching subexpressions on the non-Const sides.  We used
 
1129
         * to only allow a simple Var, but it's about as easy to allow any
 
1130
         * expression.  Remember we already know that the pred expression does
 
1131
         * not contain any non-immutable functions, so identical expressions
 
1132
         * should yield identical results.
 
1133
         */
 
1134
        if (!equal(pred_var, clause_var))
 
1135
                return false;
 
1136
 
 
1137
        /*
 
1138
         * Okay, get the operators in the two clauses we're comparing. Commute
 
1139
         * them if needed so that we can assume the variables are on the left.
 
1140
         */
 
1141
        pred_op = ((OpExpr *) predicate)->opno;
 
1142
        if (!pred_var_on_left)
 
1143
        {
 
1144
                pred_op = get_commutator(pred_op);
 
1145
                if (!OidIsValid(pred_op))
 
1146
                        return false;
 
1147
        }
 
1148
 
 
1149
        clause_op = ((OpExpr *) clause)->opno;
 
1150
        if (!clause_var_on_left)
 
1151
        {
 
1152
                clause_op = get_commutator(clause_op);
 
1153
                if (!OidIsValid(clause_op))
 
1154
                        return false;
 
1155
        }
 
1156
 
 
1157
        /*
 
1158
         * Try to find a btree opclass containing the needed operators.
 
1159
         *
 
1160
         * We must find a btree opclass that contains both operators, else the
 
1161
         * implication can't be determined.  Also, the pred_op has to be of
 
1162
         * default subtype (implying left and right input datatypes are the
 
1163
         * same); otherwise it's unsafe to put the pred_const on the left side
 
1164
         * of the test.  Also, the opclass must contain a suitable test
 
1165
         * operator matching the clause_const's type (which we take to mean
 
1166
         * that it has the same subtype as the original clause_operator).
 
1167
         *
 
1168
         * If there are multiple matching opclasses, assume we can use any one to
 
1169
         * determine the logical relationship of the two operators and the
 
1170
         * correct corresponding test operator.  This should work for any
 
1171
         * logically consistent opclasses.
 
1172
         */
 
1173
        catlist = SearchSysCacheList(AMOPOPID, 1,
 
1174
                                                                 ObjectIdGetDatum(pred_op),
 
1175
                                                                 0, 0, 0);
 
1176
 
 
1177
        /*
 
1178
         * If we couldn't find any opclass containing the pred_op, perhaps it
 
1179
         * is a <> operator.  See if it has a negator that is in an opclass.
 
1180
         */
 
1181
        pred_op_negated = false;
 
1182
        if (catlist->n_members == 0)
 
1183
        {
 
1184
                pred_op_negator = get_negator(pred_op);
 
1185
                if (OidIsValid(pred_op_negator))
 
1186
                {
 
1187
                        pred_op_negated = true;
 
1188
                        ReleaseSysCacheList(catlist);
 
1189
                        catlist = SearchSysCacheList(AMOPOPID, 1,
 
1190
                                                                           ObjectIdGetDatum(pred_op_negator),
 
1191
                                                                                 0, 0, 0);
 
1192
                }
 
1193
        }
 
1194
 
 
1195
        /* Also may need the clause_op's negator */
 
1196
        clause_op_negator = get_negator(clause_op);
 
1197
 
 
1198
        /* Now search the opclasses */
 
1199
        for (i = 0; i < catlist->n_members; i++)
 
1200
        {
 
1201
                HeapTuple       pred_tuple = &catlist->members[i]->tuple;
 
1202
                Form_pg_amop pred_form = (Form_pg_amop) GETSTRUCT(pred_tuple);
 
1203
                HeapTuple       clause_tuple;
 
1204
 
 
1205
                opclass_id = pred_form->amopclaid;
 
1206
 
 
1207
                /* must be btree */
 
1208
                if (!opclass_is_btree(opclass_id))
 
1209
                        continue;
 
1210
                /* predicate operator must be default within this opclass */
 
1211
                if (pred_form->amopsubtype != InvalidOid)
 
1212
                        continue;
 
1213
 
 
1214
                /* Get the predicate operator's btree strategy number */
 
1215
                pred_strategy = (StrategyNumber) pred_form->amopstrategy;
 
1216
                Assert(pred_strategy >= 1 && pred_strategy <= 5);
 
1217
 
 
1218
                if (pred_op_negated)
 
1219
                {
 
1220
                        /* Only consider negators that are = */
 
1221
                        if (pred_strategy != BTEqualStrategyNumber)
 
1222
                                continue;
 
1223
                        pred_strategy = BTNE;
 
1224
                }
 
1225
 
 
1226
                /*
 
1227
                 * From the same opclass, find a strategy number for the
 
1228
                 * clause_op, if possible
 
1229
                 */
 
1230
                clause_tuple = SearchSysCache(AMOPOPID,
 
1231
                                                                          ObjectIdGetDatum(clause_op),
 
1232
                                                                          ObjectIdGetDatum(opclass_id),
 
1233
                                                                          0, 0);
 
1234
                if (HeapTupleIsValid(clause_tuple))
 
1235
                {
 
1236
                        Form_pg_amop clause_form = (Form_pg_amop) GETSTRUCT(clause_tuple);
 
1237
 
 
1238
                        /* Get the restriction clause operator's strategy/subtype */
 
1239
                        clause_strategy = (StrategyNumber) clause_form->amopstrategy;
 
1240
                        Assert(clause_strategy >= 1 && clause_strategy <= 5);
 
1241
                        clause_subtype = clause_form->amopsubtype;
 
1242
                        ReleaseSysCache(clause_tuple);
 
1243
                }
 
1244
                else if (OidIsValid(clause_op_negator))
 
1245
                {
 
1246
                        clause_tuple = SearchSysCache(AMOPOPID,
 
1247
                                                                         ObjectIdGetDatum(clause_op_negator),
 
1248
                                                                                  ObjectIdGetDatum(opclass_id),
 
1249
                                                                                  0, 0);
 
1250
                        if (HeapTupleIsValid(clause_tuple))
 
1251
                        {
 
1252
                                Form_pg_amop clause_form = (Form_pg_amop) GETSTRUCT(clause_tuple);
 
1253
 
 
1254
                                /* Get the restriction clause operator's strategy/subtype */
 
1255
                                clause_strategy = (StrategyNumber) clause_form->amopstrategy;
 
1256
                                Assert(clause_strategy >= 1 && clause_strategy <= 5);
 
1257
                                clause_subtype = clause_form->amopsubtype;
 
1258
                                ReleaseSysCache(clause_tuple);
 
1259
 
 
1260
                                /* Only consider negators that are = */
 
1261
                                if (clause_strategy != BTEqualStrategyNumber)
 
1262
                                        continue;
 
1263
                                clause_strategy = BTNE;
 
1264
                        }
 
1265
                        else
 
1266
                                continue;
 
1267
                }
 
1268
                else
 
1269
                        continue;
 
1270
 
 
1271
                /*
 
1272
                 * Look up the "test" strategy number in the implication table
 
1273
                 */
 
1274
                test_strategy = BT_implic_table[clause_strategy - 1][pred_strategy - 1];
 
1275
                if (test_strategy == 0)
 
1276
                {
 
1277
                        /* Can't determine implication using this interpretation */
 
1278
                        continue;
 
1279
                }
 
1280
 
 
1281
                /*
 
1282
                 * See if opclass has an operator for the test strategy and the
 
1283
                 * clause datatype.
 
1284
                 */
 
1285
                if (test_strategy == BTNE)
 
1286
                {
 
1287
                        test_op = get_opclass_member(opclass_id, clause_subtype,
 
1288
                                                                                 BTEqualStrategyNumber);
 
1289
                        if (OidIsValid(test_op))
 
1290
                                test_op = get_negator(test_op);
 
1291
                }
 
1292
                else
 
1293
                {
 
1294
                        test_op = get_opclass_member(opclass_id, clause_subtype,
 
1295
                                                                                 test_strategy);
 
1296
                }
 
1297
                if (OidIsValid(test_op))
 
1298
                {
 
1299
                        /*
 
1300
                         * Last check: test_op must be immutable.
 
1301
                         *
 
1302
                         * Note that we require only the test_op to be immutable, not the
 
1303
                         * original clause_op.  (pred_op must be immutable, else it
 
1304
                         * would not be allowed in an index predicate.)  Essentially
 
1305
                         * we are assuming that the opclass is consistent even if it
 
1306
                         * contains operators that are merely stable.
 
1307
                         */
 
1308
                        if (op_volatile(test_op) == PROVOLATILE_IMMUTABLE)
 
1309
                        {
 
1310
                                found = true;
 
1311
                                break;
 
1312
                        }
 
1313
                }
 
1314
        }
 
1315
 
 
1316
        ReleaseSysCacheList(catlist);
 
1317
 
 
1318
        if (!found)
 
1319
        {
 
1320
                /* couldn't find a btree opclass to interpret the operators */
 
1321
                return false;
 
1322
        }
 
1323
 
 
1324
        /*
 
1325
         * Evaluate the test.  For this we need an EState.
 
1326
         */
 
1327
        estate = CreateExecutorState();
 
1328
 
 
1329
        /* We can use the estate's working context to avoid memory leaks. */
 
1330
        oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
 
1331
 
 
1332
        /* Build expression tree */
 
1333
        test_expr = make_opclause(test_op,
 
1334
                                                          BOOLOID,
 
1335
                                                          false,
 
1336
                                                          (Expr *) pred_const,
 
1337
                                                          (Expr *) clause_const);
 
1338
 
 
1339
        /* Prepare it for execution */
 
1340
        test_exprstate = ExecPrepareExpr(test_expr, estate);
 
1341
 
 
1342
        /* And execute it. */
 
1343
        test_result = ExecEvalExprSwitchContext(test_exprstate,
 
1344
                                                                                  GetPerTupleExprContext(estate),
 
1345
                                                                                        &isNull, NULL);
 
1346
 
 
1347
        /* Get back to outer memory context */
 
1348
        MemoryContextSwitchTo(oldcontext);
 
1349
 
 
1350
        /* Release all the junk we just created */
 
1351
        FreeExecutorState(estate);
 
1352
 
 
1353
        if (isNull)
 
1354
        {
 
1355
                /* Treat a null result as false ... but it's a tad fishy ... */
 
1356
                elog(DEBUG2, "null predicate test result");
 
1357
                return false;
 
1358
        }
 
1359
        return DatumGetBool(test_result);
 
1360
}
 
1361
 
 
1362
 
 
1363
/****************************************************************************
 
1364
 *                              ----  ROUTINES TO CHECK JOIN CLAUSES  ----
 
1365
 ****************************************************************************/
 
1366
 
 
1367
/*
 
1368
 * indexable_outerrelids
 
1369
 *        Finds all other relids that participate in any indexable join clause
 
1370
 *        for the specified index.      Returns a set of relids.
 
1371
 *
 
1372
 * 'rel' is the relation for which 'index' is defined
 
1373
 */
 
1374
static Relids
 
1375
indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index)
 
1376
{
 
1377
        Relids          outer_relids = NULL;
 
1378
        ListCell   *l;
 
1379
 
 
1380
        foreach(l, rel->joininfo)
 
1381
        {
 
1382
                JoinInfo   *joininfo = (JoinInfo *) lfirst(l);
 
1383
                bool            match_found = false;
 
1384
                ListCell   *j;
 
1385
 
 
1386
                /*
 
1387
                 * Examine each joinclause in the JoinInfo node's list to see if
 
1388
                 * it matches any key of the index.  If so, add the JoinInfo's
 
1389
                 * otherrels to the result.  We can skip examining other
 
1390
                 * joinclauses in the same list as soon as we find a match (since
 
1391
                 * by definition they all have the same otherrels).
 
1392
                 */
 
1393
                foreach(j, joininfo->jinfo_restrictinfo)
 
1394
                {
 
1395
                        RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
 
1396
                        int                     indexcol = 0;
 
1397
                        Oid                *classes = index->classlist;
 
1398
 
 
1399
                        do
 
1400
                        {
 
1401
                                Oid                     curClass = classes[0];
 
1402
 
 
1403
                                if (match_join_clause_to_indexcol(rel,
 
1404
                                                                                                  index,
 
1405
                                                                                                  indexcol,
 
1406
                                                                                                  curClass,
 
1407
                                                                                                  rinfo))
 
1408
                                {
 
1409
                                        match_found = true;
 
1410
                                        break;
 
1411
                                }
 
1412
 
 
1413
                                indexcol++;
 
1414
                                classes++;
 
1415
 
 
1416
                        } while (!DoneMatchingIndexKeys(classes));
 
1417
 
 
1418
                        if (match_found)
 
1419
                                break;
 
1420
                }
 
1421
 
 
1422
                if (match_found)
 
1423
                {
 
1424
                        outer_relids = bms_add_members(outer_relids,
 
1425
                                                                                   joininfo->unjoined_relids);
 
1426
                }
 
1427
        }
 
1428
 
 
1429
        return outer_relids;
 
1430
}
 
1431
 
 
1432
/*
 
1433
 * best_inner_indexscan
 
1434
 *        Finds the best available inner indexscan for a nestloop join
 
1435
 *        with the given rel on the inside and the given outer_relids outside.
 
1436
 *        May return NULL if there are no possible inner indexscans.
 
1437
 *
 
1438
 * We ignore ordering considerations (since a nestloop's inner scan's order
 
1439
 * is uninteresting).  Also, we consider only total cost when deciding which
 
1440
 * of two possible paths is better --- this assumes that all indexpaths have
 
1441
 * negligible startup cost.  (True today, but someday we might have to think
 
1442
 * harder.)  Therefore, there is only one dimension of comparison and so it's
 
1443
 * sufficient to return a single "best" path.
 
1444
 */
 
1445
Path *
 
1446
best_inner_indexscan(Query *root, RelOptInfo *rel,
 
1447
                                         Relids outer_relids, JoinType jointype)
 
1448
{
 
1449
        Path       *cheapest = NULL;
 
1450
        bool            isouterjoin;
 
1451
        ListCell   *ilist;
 
1452
        ListCell   *jlist;
 
1453
        InnerIndexscanInfo *info;
 
1454
        MemoryContext oldcontext;
 
1455
 
 
1456
        /*
 
1457
         * Nestloop only supports inner, left, and IN joins.
 
1458
         */
 
1459
        switch (jointype)
 
1460
        {
 
1461
                case JOIN_INNER:
 
1462
                case JOIN_IN:
 
1463
                case JOIN_UNIQUE_OUTER:
 
1464
                        isouterjoin = false;
 
1465
                        break;
 
1466
                case JOIN_LEFT:
 
1467
                        isouterjoin = true;
 
1468
                        break;
 
1469
                default:
 
1470
                        return NULL;
 
1471
        }
 
1472
 
 
1473
        /*
 
1474
         * If there are no indexable joinclauses for this rel, exit quickly.
 
1475
         */
 
1476
        if (bms_is_empty(rel->index_outer_relids))
 
1477
                return NULL;
 
1478
 
 
1479
        /*
 
1480
         * Otherwise, we have to do path selection in the memory context of
 
1481
         * the given rel, so that any created path can be safely attached to
 
1482
         * the rel's cache of best inner paths.  (This is not currently an
 
1483
         * issue for normal planning, but it is an issue for GEQO planning.)
 
1484
         */
 
1485
        oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
 
1486
 
 
1487
        /*
 
1488
         * Intersect the given outer_relids with index_outer_relids to find
 
1489
         * the set of outer relids actually relevant for this index. If there
 
1490
         * are none, again we can fail immediately.
 
1491
         */
 
1492
        outer_relids = bms_intersect(rel->index_outer_relids, outer_relids);
 
1493
        if (bms_is_empty(outer_relids))
 
1494
        {
 
1495
                bms_free(outer_relids);
 
1496
                MemoryContextSwitchTo(oldcontext);
 
1497
                return NULL;
 
1498
        }
 
1499
 
 
1500
        /*
 
1501
         * Look to see if we already computed the result for this set of
 
1502
         * relevant outerrels.  (We include the isouterjoin status in the
 
1503
         * cache lookup key for safety.  In practice I suspect this is not
 
1504
         * necessary because it should always be the same for a given
 
1505
         * innerrel.)
 
1506
         */
 
1507
        foreach(jlist, rel->index_inner_paths)
 
1508
        {
 
1509
                info = (InnerIndexscanInfo *) lfirst(jlist);
 
1510
                if (bms_equal(info->other_relids, outer_relids) &&
 
1511
                        info->isouterjoin == isouterjoin)
 
1512
                {
 
1513
                        bms_free(outer_relids);
 
1514
                        MemoryContextSwitchTo(oldcontext);
 
1515
                        return info->best_innerpath;
 
1516
                }
 
1517
        }
 
1518
 
 
1519
        /*
 
1520
         * For each index of the rel, find the best path; then choose the best
 
1521
         * overall.  We cache the per-index results as well as the overall
 
1522
         * result.      (This is useful because different indexes may have
 
1523
         * different relevant outerrel sets, so different overall outerrel
 
1524
         * sets might still map to the same computation for a given index.)
 
1525
         */
 
1526
        foreach(ilist, rel->indexlist)
 
1527
        {
 
1528
                IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
 
1529
                Relids          index_outer_relids;
 
1530
                Path       *path = NULL;
 
1531
 
 
1532
                /* identify set of relevant outer relids for this index */
 
1533
                index_outer_relids = bms_intersect(index->outer_relids, outer_relids);
 
1534
                /* skip if none */
 
1535
                if (bms_is_empty(index_outer_relids))
 
1536
                {
 
1537
                        bms_free(index_outer_relids);
 
1538
                        continue;
 
1539
                }
 
1540
 
 
1541
                /*
 
1542
                 * Look to see if we already computed the result for this index.
 
1543
                 */
 
1544
                foreach(jlist, index->inner_paths)
 
1545
                {
 
1546
                        info = (InnerIndexscanInfo *) lfirst(jlist);
 
1547
                        if (bms_equal(info->other_relids, index_outer_relids) &&
 
1548
                                info->isouterjoin == isouterjoin)
 
1549
                        {
 
1550
                                path = info->best_innerpath;
 
1551
                                bms_free(index_outer_relids);   /* not needed anymore */
 
1552
                                break;
 
1553
                        }
 
1554
                }
 
1555
 
 
1556
                if (jlist == NULL)              /* failed to find a match? */
 
1557
                {
 
1558
                        List       *clausegroups;
 
1559
 
 
1560
                        /* find useful clauses for this index and outerjoin set */
 
1561
                        clausegroups = group_clauses_by_indexkey_for_join(root,
 
1562
                                                                                                                          rel,
 
1563
                                                                                                                          index,
 
1564
                                                                                                          index_outer_relids,
 
1565
                                                                                                                          jointype,
 
1566
                                                                                                                        isouterjoin);
 
1567
                        if (clausegroups)
 
1568
                        {
 
1569
                                /* make the path */
 
1570
                                path = make_innerjoin_index_path(root, rel, index,
 
1571
                                                                                                 clausegroups);
 
1572
                        }
 
1573
 
 
1574
                        /* Cache the result --- whether positive or negative */
 
1575
                        info = makeNode(InnerIndexscanInfo);
 
1576
                        info->other_relids = index_outer_relids;
 
1577
                        info->isouterjoin = isouterjoin;
 
1578
                        info->best_innerpath = path;
 
1579
                        index->inner_paths = lcons(info, index->inner_paths);
 
1580
                }
 
1581
 
 
1582
                if (path != NULL &&
 
1583
                        (cheapest == NULL ||
 
1584
                         compare_path_costs(path, cheapest, TOTAL_COST) < 0))
 
1585
                        cheapest = path;
 
1586
        }
 
1587
 
 
1588
        /* Cache the result --- whether positive or negative */
 
1589
        info = makeNode(InnerIndexscanInfo);
 
1590
        info->other_relids = outer_relids;
 
1591
        info->isouterjoin = isouterjoin;
 
1592
        info->best_innerpath = cheapest;
 
1593
        rel->index_inner_paths = lcons(info, rel->index_inner_paths);
 
1594
 
 
1595
        MemoryContextSwitchTo(oldcontext);
 
1596
 
 
1597
        return cheapest;
 
1598
}
 
1599
 
 
1600
/****************************************************************************
 
1601
 *                              ----  PATH CREATION UTILITIES  ----
 
1602
 ****************************************************************************/
 
1603
 
 
1604
/*
 
1605
 * make_innerjoin_index_path
 
1606
 *        Create an index path node for a path to be used as an inner
 
1607
 *        relation in a nestloop join.
 
1608
 *
 
1609
 * 'rel' is the relation for which 'index' is defined
 
1610
 * 'clausegroups' is a list of lists of RestrictInfos that can use 'index'
 
1611
 */
 
1612
static Path *
 
1613
make_innerjoin_index_path(Query *root,
 
1614
                                                  RelOptInfo *rel, IndexOptInfo *index,
 
1615
                                                  List *clausegroups)
 
1616
{
 
1617
        IndexPath  *pathnode = makeNode(IndexPath);
 
1618
        List       *indexquals,
 
1619
                           *allclauses;
 
1620
 
 
1621
        /* XXX perhaps this code should be merged with create_index_path? */
 
1622
 
 
1623
        pathnode->path.pathtype = T_IndexScan;
 
1624
        pathnode->path.parent = rel;
 
1625
 
 
1626
        /*
 
1627
         * There's no point in marking the path with any pathkeys, since it
 
1628
         * will only ever be used as the inner path of a nestloop, and so its
 
1629
         * ordering does not matter.
 
1630
         */
 
1631
        pathnode->path.pathkeys = NIL;
 
1632
 
 
1633
        /* Convert clauses to indexquals the executor can handle */
 
1634
        indexquals = expand_indexqual_conditions(index, clausegroups);
 
1635
 
 
1636
        /* Flatten the clausegroups list to produce indexclauses list */
 
1637
        allclauses = flatten_clausegroups_list(clausegroups);
 
1638
 
 
1639
        /*
 
1640
         * Note that we are making a pathnode for a single-scan indexscan;
 
1641
         * therefore, indexinfo etc should be single-element lists.
 
1642
         */
 
1643
        pathnode->indexinfo = list_make1(index);
 
1644
        pathnode->indexclauses = list_make1(allclauses);
 
1645
        pathnode->indexquals = list_make1(indexquals);
 
1646
 
 
1647
        pathnode->isjoininner = true;
 
1648
 
 
1649
        /* We don't actually care what order the index scans in ... */
 
1650
        pathnode->indexscandir = NoMovementScanDirection;
 
1651
 
 
1652
        /*
 
1653
         * We must compute the estimated number of output rows for the
 
1654
         * indexscan.  This is less than rel->rows because of the additional
 
1655
         * selectivity of the join clauses.  Since clausegroups may contain
 
1656
         * both restriction and join clauses, we have to do a set union to get
 
1657
         * the full set of clauses that must be considered to compute the
 
1658
         * correct selectivity.  (Without the union operation, we might have
 
1659
         * some restriction clauses appearing twice, which'd mislead
 
1660
         * clauselist_selectivity into double-counting their selectivity.
 
1661
         * However, since RestrictInfo nodes aren't copied when linking them
 
1662
         * into different lists, it should be sufficient to use pointer
 
1663
         * comparison to remove duplicates.)
 
1664
         *
 
1665
         * Always assume the join type is JOIN_INNER; even if some of the join
 
1666
         * clauses come from other contexts, that's not our problem.
 
1667
         */
 
1668
        allclauses = list_union_ptr(rel->baserestrictinfo, allclauses);
 
1669
        pathnode->rows = rel->tuples *
 
1670
                clauselist_selectivity(root,
 
1671
                                                           allclauses,
 
1672
                                                           rel->relid,          /* do not use 0! */
 
1673
                                                           JOIN_INNER);
 
1674
        /* Like costsize.c, force estimate to be at least one row */
 
1675
        pathnode->rows = clamp_row_est(pathnode->rows);
 
1676
 
 
1677
        cost_index(&pathnode->path, root, rel, index, indexquals, true);
 
1678
 
 
1679
        return (Path *) pathnode;
 
1680
}
 
1681
 
 
1682
/*
 
1683
 * flatten_clausegroups_list
 
1684
 *        Given a list of lists of RestrictInfos, flatten it to a list
 
1685
 *        of RestrictInfos.
 
1686
 *
 
1687
 * This is used to flatten out the result of group_clauses_by_indexkey()
 
1688
 * or one of its sibling routines, to produce an indexclauses list.
 
1689
 */
 
1690
List *
 
1691
flatten_clausegroups_list(List *clausegroups)
 
1692
{
 
1693
        List       *allclauses = NIL;
 
1694
        ListCell   *l;
 
1695
 
 
1696
        foreach(l, clausegroups)
 
1697
                allclauses = list_concat(allclauses, list_copy((List *) lfirst(l)));
 
1698
        return allclauses;
 
1699
}
 
1700
 
 
1701
/*
 
1702
 * make_expr_from_indexclauses()
 
1703
 *        Given an indexclauses structure, produce an ordinary boolean expression.
 
1704
 *
 
1705
 * This consists of stripping out the RestrictInfo nodes and inserting
 
1706
 * explicit AND and OR nodes as needed.  There's not much to it, but
 
1707
 * the functionality is needed in a few places, so centralize the logic.
 
1708
 */
 
1709
Expr *
 
1710
make_expr_from_indexclauses(List *indexclauses)
 
1711
{
 
1712
        List       *orclauses = NIL;
 
1713
        ListCell   *orlist;
 
1714
 
 
1715
        /* There's no such thing as an indexpath with zero scans */
 
1716
        Assert(indexclauses != NIL);
 
1717
 
 
1718
        foreach(orlist, indexclauses)
 
1719
        {
 
1720
                List       *andlist = (List *) lfirst(orlist);
 
1721
 
 
1722
                /* Strip RestrictInfos */
 
1723
                andlist = get_actual_clauses(andlist);
 
1724
                /* Insert AND node if needed, and add to orclauses list */
 
1725
                orclauses = lappend(orclauses, make_ands_explicit(andlist));
 
1726
        }
 
1727
 
 
1728
        if (list_length(orclauses) > 1)
 
1729
                return make_orclause(orclauses);
 
1730
        else
 
1731
                return (Expr *) linitial(orclauses);
 
1732
}
 
1733
 
 
1734
 
 
1735
/****************************************************************************
 
1736
 *                              ----  ROUTINES TO CHECK OPERANDS  ----
 
1737
 ****************************************************************************/
 
1738
 
 
1739
/*
 
1740
 * match_index_to_operand()
 
1741
 *        Generalized test for a match between an index's key
 
1742
 *        and the operand on one side of a restriction or join clause.
 
1743
 *
 
1744
 * operand: the nodetree to be compared to the index
 
1745
 * indexcol: the column number of the index (counting from 0)
 
1746
 * rel: the parent relation
 
1747
 * index: the index of interest
 
1748
 */
 
1749
static bool
 
1750
match_index_to_operand(Node *operand,
 
1751
                                           int indexcol,
 
1752
                                           RelOptInfo *rel,
 
1753
                                           IndexOptInfo *index)
 
1754
{
 
1755
        int                     indkey;
 
1756
 
 
1757
        /*
 
1758
         * Ignore any RelabelType node above the operand.       This is needed to
 
1759
         * be able to apply indexscanning in binary-compatible-operator cases.
 
1760
         * Note: we can assume there is at most one RelabelType node;
 
1761
         * eval_const_expressions() will have simplified if more than one.
 
1762
         */
 
1763
        if (operand && IsA(operand, RelabelType))
 
1764
                operand = (Node *) ((RelabelType *) operand)->arg;
 
1765
 
 
1766
        indkey = index->indexkeys[indexcol];
 
1767
        if (indkey != 0)
 
1768
        {
 
1769
                /*
 
1770
                 * Simple index column; operand must be a matching Var.
 
1771
                 */
 
1772
                if (operand && IsA(operand, Var) &&
 
1773
                        rel->relid == ((Var *) operand)->varno &&
 
1774
                        indkey == ((Var *) operand)->varattno)
 
1775
                        return true;
 
1776
        }
 
1777
        else
 
1778
        {
 
1779
                /*
 
1780
                 * Index expression; find the correct expression.  (This search
 
1781
                 * could be avoided, at the cost of complicating all the callers
 
1782
                 * of this routine; doesn't seem worth it.)
 
1783
                 */
 
1784
                ListCell   *indexpr_item;
 
1785
                int                     i;
 
1786
                Node       *indexkey;
 
1787
 
 
1788
                indexpr_item = list_head(index->indexprs);
 
1789
                for (i = 0; i < indexcol; i++)
 
1790
                {
 
1791
                        if (index->indexkeys[i] == 0)
 
1792
                        {
 
1793
                                if (indexpr_item == NULL)
 
1794
                                        elog(ERROR, "wrong number of index expressions");
 
1795
                                indexpr_item = lnext(indexpr_item);
 
1796
                        }
 
1797
                }
 
1798
                if (indexpr_item == NULL)
 
1799
                        elog(ERROR, "wrong number of index expressions");
 
1800
                indexkey = (Node *) lfirst(indexpr_item);
 
1801
 
 
1802
                /*
 
1803
                 * Does it match the operand?  Again, strip any relabeling.
 
1804
                 */
 
1805
                if (indexkey && IsA(indexkey, RelabelType))
 
1806
                        indexkey = (Node *) ((RelabelType *) indexkey)->arg;
 
1807
 
 
1808
                if (equal(indexkey, operand))
 
1809
                        return true;
 
1810
        }
 
1811
 
 
1812
        return false;
 
1813
}
 
1814
 
 
1815
/****************************************************************************
 
1816
 *                      ----  ROUTINES FOR "SPECIAL" INDEXABLE OPERATORS  ----
 
1817
 ****************************************************************************/
 
1818
 
 
1819
/*----------
 
1820
 * These routines handle special optimization of operators that can be
 
1821
 * used with index scans even though they are not known to the executor's
 
1822
 * indexscan machinery.  The key idea is that these operators allow us
 
1823
 * to derive approximate indexscan qual clauses, such that any tuples
 
1824
 * that pass the operator clause itself must also satisfy the simpler
 
1825
 * indexscan condition(s).      Then we can use the indexscan machinery
 
1826
 * to avoid scanning as much of the table as we'd otherwise have to,
 
1827
 * while applying the original operator as a qpqual condition to ensure
 
1828
 * we deliver only the tuples we want.  (In essence, we're using a regular
 
1829
 * index as if it were a lossy index.)
 
1830
 *
 
1831
 * An example of what we're doing is
 
1832
 *                      textfield LIKE 'abc%'
 
1833
 * from which we can generate the indexscanable conditions
 
1834
 *                      textfield >= 'abc' AND textfield < 'abd'
 
1835
 * which allow efficient scanning of an index on textfield.
 
1836
 * (In reality, character set and collation issues make the transformation
 
1837
 * from LIKE to indexscan limits rather harder than one might think ...
 
1838
 * but that's the basic idea.)
 
1839
 *
 
1840
 * Two routines are provided here, match_special_index_operator() and
 
1841
 * expand_indexqual_conditions().  match_special_index_operator() is
 
1842
 * just an auxiliary function for match_clause_to_indexcol(); after
 
1843
 * the latter fails to recognize a restriction opclause's operator
 
1844
 * as a member of an index's opclass, it asks match_special_index_operator()
 
1845
 * whether the clause should be considered an indexqual anyway.
 
1846
 * expand_indexqual_conditions() converts a list of lists of RestrictInfo
 
1847
 * nodes (with implicit AND semantics across list elements) into
 
1848
 * a list of clauses that the executor can actually handle.  For operators
 
1849
 * that are members of the index's opclass this transformation is a no-op,
 
1850
 * but operators recognized by match_special_index_operator() must be
 
1851
 * converted into one or more "regular" indexqual conditions.
 
1852
 *----------
 
1853
 */
 
1854
 
 
1855
/*
 
1856
 * match_special_index_operator
 
1857
 *        Recognize restriction clauses that can be used to generate
 
1858
 *        additional indexscanable qualifications.
 
1859
 *
 
1860
 * The given clause is already known to be a binary opclause having
 
1861
 * the form (indexkey OP pseudoconst) or (pseudoconst OP indexkey),
 
1862
 * but the OP proved not to be one of the index's opclass operators.
 
1863
 * Return 'true' if we can do something with it anyway.
 
1864
 */
 
1865
static bool
 
1866
match_special_index_operator(Expr *clause, Oid opclass,
 
1867
                                                         bool indexkey_on_left)
 
1868
{
 
1869
        bool            isIndexable = false;
 
1870
        Node       *rightop;
 
1871
        Oid                     expr_op;
 
1872
        Const      *patt;
 
1873
        Const      *prefix = NULL;
 
1874
        Const      *rest = NULL;
 
1875
 
 
1876
        /*
 
1877
         * Currently, all known special operators require the indexkey on the
 
1878
         * left, but this test could be pushed into the switch statement if
 
1879
         * some are added that do not...
 
1880
         */
 
1881
        if (!indexkey_on_left)
 
1882
                return false;
 
1883
 
 
1884
        /* we know these will succeed */
 
1885
        rightop = get_rightop(clause);
 
1886
        expr_op = ((OpExpr *) clause)->opno;
 
1887
 
 
1888
        /* again, required for all current special ops: */
 
1889
        if (!IsA(rightop, Const) ||
 
1890
                ((Const *) rightop)->constisnull)
 
1891
                return false;
 
1892
        patt = (Const *) rightop;
 
1893
 
 
1894
        switch (expr_op)
 
1895
        {
 
1896
                case OID_TEXT_LIKE_OP:
 
1897
                case OID_BPCHAR_LIKE_OP:
 
1898
                case OID_NAME_LIKE_OP:
 
1899
                        /* the right-hand const is type text for all of these */
 
1900
                        isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
 
1901
                                                                  &prefix, &rest) != Pattern_Prefix_None;
 
1902
                        break;
 
1903
 
 
1904
                case OID_BYTEA_LIKE_OP:
 
1905
                        isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
 
1906
                                                                  &prefix, &rest) != Pattern_Prefix_None;
 
1907
                        break;
 
1908
 
 
1909
                case OID_TEXT_ICLIKE_OP:
 
1910
                case OID_BPCHAR_ICLIKE_OP:
 
1911
                case OID_NAME_ICLIKE_OP:
 
1912
                        /* the right-hand const is type text for all of these */
 
1913
                        isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
 
1914
                                                                  &prefix, &rest) != Pattern_Prefix_None;
 
1915
                        break;
 
1916
 
 
1917
                case OID_TEXT_REGEXEQ_OP:
 
1918
                case OID_BPCHAR_REGEXEQ_OP:
 
1919
                case OID_NAME_REGEXEQ_OP:
 
1920
                        /* the right-hand const is type text for all of these */
 
1921
                        isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex,
 
1922
                                                                  &prefix, &rest) != Pattern_Prefix_None;
 
1923
                        break;
 
1924
 
 
1925
                case OID_TEXT_ICREGEXEQ_OP:
 
1926
                case OID_BPCHAR_ICREGEXEQ_OP:
 
1927
                case OID_NAME_ICREGEXEQ_OP:
 
1928
                        /* the right-hand const is type text for all of these */
 
1929
                        isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
 
1930
                                                                  &prefix, &rest) != Pattern_Prefix_None;
 
1931
                        break;
 
1932
 
 
1933
                case OID_INET_SUB_OP:
 
1934
                case OID_INET_SUBEQ_OP:
 
1935
                case OID_CIDR_SUB_OP:
 
1936
                case OID_CIDR_SUBEQ_OP:
 
1937
                        isIndexable = true;
 
1938
                        break;
 
1939
        }
 
1940
 
 
1941
        if (prefix)
 
1942
        {
 
1943
                pfree(DatumGetPointer(prefix->constvalue));
 
1944
                pfree(prefix);
 
1945
        }
 
1946
 
 
1947
        /* done if the expression doesn't look indexable */
 
1948
        if (!isIndexable)
 
1949
                return false;
 
1950
 
 
1951
        /*
 
1952
         * Must also check that index's opclass supports the operators we will
 
1953
         * want to apply.  (A hash index, for example, will not support ">=".)
 
1954
         * Currently, only btree supports the operators we need.
 
1955
         *
 
1956
         * We insist on the opclass being the specific one we expect, else we'd
 
1957
         * do the wrong thing if someone were to make a reverse-sort opclass
 
1958
         * with the same operators.
 
1959
         */
 
1960
        switch (expr_op)
 
1961
        {
 
1962
                case OID_TEXT_LIKE_OP:
 
1963
                case OID_TEXT_ICLIKE_OP:
 
1964
                case OID_TEXT_REGEXEQ_OP:
 
1965
                case OID_TEXT_ICREGEXEQ_OP:
 
1966
                        /* text operators will be used for varchar inputs, too */
 
1967
                        isIndexable =
 
1968
                                (opclass == TEXT_PATTERN_BTREE_OPS_OID) ||
 
1969
                                (opclass == TEXT_BTREE_OPS_OID && lc_collate_is_c()) ||
 
1970
                                (opclass == VARCHAR_PATTERN_BTREE_OPS_OID) ||
 
1971
                                (opclass == VARCHAR_BTREE_OPS_OID && lc_collate_is_c());
 
1972
                        break;
 
1973
 
 
1974
                case OID_BPCHAR_LIKE_OP:
 
1975
                case OID_BPCHAR_ICLIKE_OP:
 
1976
                case OID_BPCHAR_REGEXEQ_OP:
 
1977
                case OID_BPCHAR_ICREGEXEQ_OP:
 
1978
                        isIndexable =
 
1979
                                (opclass == BPCHAR_PATTERN_BTREE_OPS_OID) ||
 
1980
                                (opclass == BPCHAR_BTREE_OPS_OID && lc_collate_is_c());
 
1981
                        break;
 
1982
 
 
1983
                case OID_NAME_LIKE_OP:
 
1984
                case OID_NAME_ICLIKE_OP:
 
1985
                case OID_NAME_REGEXEQ_OP:
 
1986
                case OID_NAME_ICREGEXEQ_OP:
 
1987
                        isIndexable =
 
1988
                                (opclass == NAME_PATTERN_BTREE_OPS_OID) ||
 
1989
                                (opclass == NAME_BTREE_OPS_OID && lc_collate_is_c());
 
1990
                        break;
 
1991
 
 
1992
                case OID_BYTEA_LIKE_OP:
 
1993
                        isIndexable = (opclass == BYTEA_BTREE_OPS_OID);
 
1994
                        break;
 
1995
 
 
1996
                case OID_INET_SUB_OP:
 
1997
                case OID_INET_SUBEQ_OP:
 
1998
                        isIndexable = (opclass == INET_BTREE_OPS_OID);
 
1999
                        break;
 
2000
 
 
2001
                case OID_CIDR_SUB_OP:
 
2002
                case OID_CIDR_SUBEQ_OP:
 
2003
                        isIndexable = (opclass == CIDR_BTREE_OPS_OID);
 
2004
                        break;
 
2005
        }
 
2006
 
 
2007
        return isIndexable;
 
2008
}
 
2009
 
 
2010
/*
 
2011
 * expand_indexqual_conditions
 
2012
 *        Given a list of sublists of RestrictInfo nodes, produce a flat list
 
2013
 *        of index qual clauses.  Standard qual clauses (those in the index's
 
2014
 *        opclass) are passed through unchanged.  "Special" index operators
 
2015
 *        are expanded into clauses that the indexscan machinery will know
 
2016
 *        what to do with.
 
2017
 *
 
2018
 * The input list is ordered by index key, and so the output list is too.
 
2019
 * (The latter is not depended on by any part of the planner, so far as I can
 
2020
 * tell; but some parts of the executor do assume that the indxqual list
 
2021
 * ultimately delivered to the executor is so ordered.  One such place is
 
2022
 * _bt_preprocess_keys() in the btree support.  Perhaps that ought to be fixed
 
2023
 * someday --- tgl 7/00)
 
2024
 */
 
2025
List *
 
2026
expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
 
2027
{
 
2028
        List       *resultquals = NIL;
 
2029
        ListCell   *clausegroup_item;
 
2030
        Oid                *classes = index->classlist;
 
2031
 
 
2032
        if (clausegroups == NIL)
 
2033
                return NIL;
 
2034
 
 
2035
        clausegroup_item = list_head(clausegroups);
 
2036
        do
 
2037
        {
 
2038
                Oid                     curClass = classes[0];
 
2039
                ListCell   *l;
 
2040
 
 
2041
                foreach(l, (List *) lfirst(clausegroup_item))
 
2042
                {
 
2043
                        RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
2044
 
 
2045
                        resultquals = list_concat(resultquals,
 
2046
                                                                          expand_indexqual_condition(rinfo,
 
2047
                                                                                                                          curClass));
 
2048
                }
 
2049
 
 
2050
                clausegroup_item = lnext(clausegroup_item);
 
2051
                classes++;
 
2052
        } while (clausegroup_item != NULL && !DoneMatchingIndexKeys(classes));
 
2053
 
 
2054
        Assert(clausegroup_item == NULL);       /* else more groups than indexkeys */
 
2055
 
 
2056
        return resultquals;
 
2057
}
 
2058
 
 
2059
/*
 
2060
 * expand_indexqual_condition --- expand a single indexqual condition
 
2061
 *
 
2062
 * The input is a single RestrictInfo, the output a list of RestrictInfos
 
2063
 */
 
2064
static List *
 
2065
expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass)
 
2066
{
 
2067
        Expr       *clause = rinfo->clause;
 
2068
 
 
2069
        /* we know these will succeed */
 
2070
        Node       *leftop = get_leftop(clause);
 
2071
        Node       *rightop = get_rightop(clause);
 
2072
        Oid                     expr_op = ((OpExpr *) clause)->opno;
 
2073
        Const      *patt = (Const *) rightop;
 
2074
        Const      *prefix = NULL;
 
2075
        Const      *rest = NULL;
 
2076
        Pattern_Prefix_Status pstatus;
 
2077
        List       *result;
 
2078
 
 
2079
        switch (expr_op)
 
2080
        {
 
2081
                        /*
 
2082
                         * LIKE and regex operators are not members of any index
 
2083
                         * opclass, so if we find one in an indexqual list we can
 
2084
                         * assume that it was accepted by
 
2085
                         * match_special_index_operator().
 
2086
                         */
 
2087
                case OID_TEXT_LIKE_OP:
 
2088
                case OID_BPCHAR_LIKE_OP:
 
2089
                case OID_NAME_LIKE_OP:
 
2090
                case OID_BYTEA_LIKE_OP:
 
2091
                        pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
 
2092
                                                                                   &prefix, &rest);
 
2093
                        result = prefix_quals(leftop, opclass, prefix, pstatus);
 
2094
                        break;
 
2095
 
 
2096
                case OID_TEXT_ICLIKE_OP:
 
2097
                case OID_BPCHAR_ICLIKE_OP:
 
2098
                case OID_NAME_ICLIKE_OP:
 
2099
                        /* the right-hand const is type text for all of these */
 
2100
                        pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
 
2101
                                                                                   &prefix, &rest);
 
2102
                        result = prefix_quals(leftop, opclass, prefix, pstatus);
 
2103
                        break;
 
2104
 
 
2105
                case OID_TEXT_REGEXEQ_OP:
 
2106
                case OID_BPCHAR_REGEXEQ_OP:
 
2107
                case OID_NAME_REGEXEQ_OP:
 
2108
                        /* the right-hand const is type text for all of these */
 
2109
                        pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex,
 
2110
                                                                                   &prefix, &rest);
 
2111
                        result = prefix_quals(leftop, opclass, prefix, pstatus);
 
2112
                        break;
 
2113
 
 
2114
                case OID_TEXT_ICREGEXEQ_OP:
 
2115
                case OID_BPCHAR_ICREGEXEQ_OP:
 
2116
                case OID_NAME_ICREGEXEQ_OP:
 
2117
                        /* the right-hand const is type text for all of these */
 
2118
                        pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
 
2119
                                                                                   &prefix, &rest);
 
2120
                        result = prefix_quals(leftop, opclass, prefix, pstatus);
 
2121
                        break;
 
2122
 
 
2123
                case OID_INET_SUB_OP:
 
2124
                case OID_INET_SUBEQ_OP:
 
2125
                case OID_CIDR_SUB_OP:
 
2126
                case OID_CIDR_SUBEQ_OP:
 
2127
                        result = network_prefix_quals(leftop, expr_op, opclass,
 
2128
                                                                                  patt->constvalue);
 
2129
                        break;
 
2130
 
 
2131
                default:
 
2132
                        result = list_make1(rinfo);
 
2133
                        break;
 
2134
        }
 
2135
 
 
2136
        return result;
 
2137
}
 
2138
 
 
2139
/*
 
2140
 * Given a fixed prefix that all the "leftop" values must have,
 
2141
 * generate suitable indexqual condition(s).  opclass is the index
 
2142
 * operator class; we use it to deduce the appropriate comparison
 
2143
 * operators and operand datatypes.
 
2144
 */
 
2145
static List *
 
2146
prefix_quals(Node *leftop, Oid opclass,
 
2147
                         Const *prefix_const, Pattern_Prefix_Status pstatus)
 
2148
{
 
2149
        List       *result;
 
2150
        Oid                     datatype;
 
2151
        Oid                     oproid;
 
2152
        Expr       *expr;
 
2153
        Const      *greaterstr;
 
2154
 
 
2155
        Assert(pstatus != Pattern_Prefix_None);
 
2156
 
 
2157
        switch (opclass)
 
2158
        {
 
2159
                case TEXT_BTREE_OPS_OID:
 
2160
                case TEXT_PATTERN_BTREE_OPS_OID:
 
2161
                        datatype = TEXTOID;
 
2162
                        break;
 
2163
 
 
2164
                case VARCHAR_BTREE_OPS_OID:
 
2165
                case VARCHAR_PATTERN_BTREE_OPS_OID:
 
2166
                        datatype = VARCHAROID;
 
2167
                        break;
 
2168
 
 
2169
                case BPCHAR_BTREE_OPS_OID:
 
2170
                case BPCHAR_PATTERN_BTREE_OPS_OID:
 
2171
                        datatype = BPCHAROID;
 
2172
                        break;
 
2173
 
 
2174
                case NAME_BTREE_OPS_OID:
 
2175
                case NAME_PATTERN_BTREE_OPS_OID:
 
2176
                        datatype = NAMEOID;
 
2177
                        break;
 
2178
 
 
2179
                case BYTEA_BTREE_OPS_OID:
 
2180
                        datatype = BYTEAOID;
 
2181
                        break;
 
2182
 
 
2183
                default:
 
2184
                        /* shouldn't get here */
 
2185
                        elog(ERROR, "unexpected opclass: %u", opclass);
 
2186
                        return NIL;
 
2187
        }
 
2188
 
 
2189
        /*
 
2190
         * If necessary, coerce the prefix constant to the right type. The
 
2191
         * given prefix constant is either text or bytea type.
 
2192
         */
 
2193
        if (prefix_const->consttype != datatype)
 
2194
        {
 
2195
                char       *prefix;
 
2196
 
 
2197
                switch (prefix_const->consttype)
 
2198
                {
 
2199
                        case TEXTOID:
 
2200
                                prefix = DatumGetCString(DirectFunctionCall1(textout,
 
2201
                                                                                          prefix_const->constvalue));
 
2202
                                break;
 
2203
                        case BYTEAOID:
 
2204
                                prefix = DatumGetCString(DirectFunctionCall1(byteaout,
 
2205
                                                                                          prefix_const->constvalue));
 
2206
                                break;
 
2207
                        default:
 
2208
                                elog(ERROR, "unexpected const type: %u",
 
2209
                                         prefix_const->consttype);
 
2210
                                return NIL;
 
2211
                }
 
2212
                prefix_const = string_to_const(prefix, datatype);
 
2213
                pfree(prefix);
 
2214
        }
 
2215
 
 
2216
        /*
 
2217
         * If we found an exact-match pattern, generate an "=" indexqual.
 
2218
         */
 
2219
        if (pstatus == Pattern_Prefix_Exact)
 
2220
        {
 
2221
                oproid = get_opclass_member(opclass, InvalidOid,
 
2222
                                                                        BTEqualStrategyNumber);
 
2223
                if (oproid == InvalidOid)
 
2224
                        elog(ERROR, "no = operator for opclass %u", opclass);
 
2225
                expr = make_opclause(oproid, BOOLOID, false,
 
2226
                                                         (Expr *) leftop, (Expr *) prefix_const);
 
2227
                result = list_make1(make_restrictinfo(expr, true, true));
 
2228
                return result;
 
2229
        }
 
2230
 
 
2231
        /*
 
2232
         * Otherwise, we have a nonempty required prefix of the values.
 
2233
         *
 
2234
         * We can always say "x >= prefix".
 
2235
         */
 
2236
        oproid = get_opclass_member(opclass, InvalidOid,
 
2237
                                                                BTGreaterEqualStrategyNumber);
 
2238
        if (oproid == InvalidOid)
 
2239
                elog(ERROR, "no >= operator for opclass %u", opclass);
 
2240
        expr = make_opclause(oproid, BOOLOID, false,
 
2241
                                                 (Expr *) leftop, (Expr *) prefix_const);
 
2242
        result = list_make1(make_restrictinfo(expr, true, true));
 
2243
 
 
2244
        /*-------
 
2245
         * If we can create a string larger than the prefix, we can say
 
2246
         * "x < greaterstr".
 
2247
         *-------
 
2248
         */
 
2249
        greaterstr = make_greater_string(prefix_const);
 
2250
        if (greaterstr)
 
2251
        {
 
2252
                oproid = get_opclass_member(opclass, InvalidOid,
 
2253
                                                                        BTLessStrategyNumber);
 
2254
                if (oproid == InvalidOid)
 
2255
                        elog(ERROR, "no < operator for opclass %u", opclass);
 
2256
                expr = make_opclause(oproid, BOOLOID, false,
 
2257
                                                         (Expr *) leftop, (Expr *) greaterstr);
 
2258
                result = lappend(result, make_restrictinfo(expr, true, true));
 
2259
        }
 
2260
 
 
2261
        return result;
 
2262
}
 
2263
 
 
2264
/*
 
2265
 * Given a leftop and a rightop, and a inet-class sup/sub operator,
 
2266
 * generate suitable indexqual condition(s).  expr_op is the original
 
2267
 * operator, and opclass is the index opclass.
 
2268
 */
 
2269
static List *
 
2270
network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop)
 
2271
{
 
2272
        bool            is_eq;
 
2273
        Oid                     datatype;
 
2274
        Oid                     opr1oid;
 
2275
        Oid                     opr2oid;
 
2276
        Datum           opr1right;
 
2277
        Datum           opr2right;
 
2278
        List       *result;
 
2279
        Expr       *expr;
 
2280
 
 
2281
        switch (expr_op)
 
2282
        {
 
2283
                case OID_INET_SUB_OP:
 
2284
                        datatype = INETOID;
 
2285
                        is_eq = false;
 
2286
                        break;
 
2287
                case OID_INET_SUBEQ_OP:
 
2288
                        datatype = INETOID;
 
2289
                        is_eq = true;
 
2290
                        break;
 
2291
                case OID_CIDR_SUB_OP:
 
2292
                        datatype = CIDROID;
 
2293
                        is_eq = false;
 
2294
                        break;
 
2295
                case OID_CIDR_SUBEQ_OP:
 
2296
                        datatype = CIDROID;
 
2297
                        is_eq = true;
 
2298
                        break;
 
2299
                default:
 
2300
                        elog(ERROR, "unexpected operator: %u", expr_op);
 
2301
                        return NIL;
 
2302
        }
 
2303
 
 
2304
        /*
 
2305
         * create clause "key >= network_scan_first( rightop )", or ">" if the
 
2306
         * operator disallows equality.
 
2307
         */
 
2308
        if (is_eq)
 
2309
        {
 
2310
                opr1oid = get_opclass_member(opclass, InvalidOid,
 
2311
                                                                         BTGreaterEqualStrategyNumber);
 
2312
                if (opr1oid == InvalidOid)
 
2313
                        elog(ERROR, "no >= operator for opclass %u", opclass);
 
2314
        }
 
2315
        else
 
2316
        {
 
2317
                opr1oid = get_opclass_member(opclass, InvalidOid,
 
2318
                                                                         BTGreaterStrategyNumber);
 
2319
                if (opr1oid == InvalidOid)
 
2320
                        elog(ERROR, "no > operator for opclass %u", opclass);
 
2321
        }
 
2322
 
 
2323
        opr1right = network_scan_first(rightop);
 
2324
 
 
2325
        expr = make_opclause(opr1oid, BOOLOID, false,
 
2326
                                                 (Expr *) leftop,
 
2327
                                                 (Expr *) makeConst(datatype, -1, opr1right,
 
2328
                                                                                        false, false));
 
2329
        result = list_make1(make_restrictinfo(expr, true, true));
 
2330
 
 
2331
        /* create clause "key <= network_scan_last( rightop )" */
 
2332
 
 
2333
        opr2oid = get_opclass_member(opclass, InvalidOid,
 
2334
                                                                 BTLessEqualStrategyNumber);
 
2335
        if (opr2oid == InvalidOid)
 
2336
                elog(ERROR, "no <= operator for opclass %u", opclass);
 
2337
 
 
2338
        opr2right = network_scan_last(rightop);
 
2339
 
 
2340
        expr = make_opclause(opr2oid, BOOLOID, false,
 
2341
                                                 (Expr *) leftop,
 
2342
                                                 (Expr *) makeConst(datatype, -1, opr2right,
 
2343
                                                                                        false, false));
 
2344
        result = lappend(result, make_restrictinfo(expr, true, true));
 
2345
 
 
2346
        return result;
 
2347
}
 
2348
 
 
2349
/*
 
2350
 * Handy subroutines for match_special_index_operator() and friends.
 
2351
 */
 
2352
 
 
2353
/*
 
2354
 * Generate a Datum of the appropriate type from a C string.
 
2355
 * Note that all of the supported types are pass-by-ref, so the
 
2356
 * returned value should be pfree'd if no longer needed.
 
2357
 */
 
2358
static Datum
 
2359
string_to_datum(const char *str, Oid datatype)
 
2360
{
 
2361
        /*
 
2362
         * We cheat a little by assuming that textin() will do for bpchar and
 
2363
         * varchar constants too...
 
2364
         */
 
2365
        if (datatype == NAMEOID)
 
2366
                return DirectFunctionCall1(namein, CStringGetDatum(str));
 
2367
        else if (datatype == BYTEAOID)
 
2368
                return DirectFunctionCall1(byteain, CStringGetDatum(str));
 
2369
        else
 
2370
                return DirectFunctionCall1(textin, CStringGetDatum(str));
 
2371
}
 
2372
 
 
2373
/*
 
2374
 * Generate a Const node of the appropriate type from a C string.
 
2375
 */
 
2376
static Const *
 
2377
string_to_const(const char *str, Oid datatype)
 
2378
{
 
2379
        Datum           conval = string_to_datum(str, datatype);
 
2380
 
 
2381
        return makeConst(datatype, ((datatype == NAMEOID) ? NAMEDATALEN : -1),
 
2382
                                         conval, false, false);
 
2383
}