|
10 | 10 | * Portions Copyright (c) 1994, Regents of the University of California
|
11 | 11 | *
|
12 | 12 | * IDENTIFICATION
|
13 |
| - * $PostgreSQL: pgsql/src/backend/optimizer/path/equivclass.c,v 1.19 2009/06/11 14:48:58 momjian Exp $ |
| 13 | + * $PostgreSQL: pgsql/src/backend/optimizer/path/equivclass.c,v 1.20 2009/09/12 00:04:58 tgl Exp $ |
14 | 14 | *
|
15 | 15 | *-------------------------------------------------------------------------
|
16 | 16 | */
|
@@ -357,7 +357,7 @@ add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
|
357 | 357 | * EquivalenceClass for it.
|
358 | 358 | *
|
359 | 359 | * sortref is the SortGroupRef of the originating SortGroupClause, if any,
|
360 |
| - * or zero if not. |
| 360 | + * or zero if not. (It should never be zero if the expression is volatile!) |
361 | 361 | *
|
362 | 362 | * This can be used safely both before and after EquivalenceClass merging;
|
363 | 363 | * since it never causes merging it does not invalidate any existing ECs
|
@@ -388,8 +388,12 @@ get_eclass_for_sort_expr(PlannerInfo *root,
|
388 | 388 | EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
|
389 | 389 | ListCell *lc2;
|
390 | 390 |
|
391 |
| - /* Never match to a volatile EC */ |
392 |
| - if (cur_ec->ec_has_volatile) |
| 391 | + /* |
| 392 | + * Never match to a volatile EC, except when we are looking at another |
| 393 | + * reference to the same volatile SortGroupClause. |
| 394 | + */ |
| 395 | + if (cur_ec->ec_has_volatile && |
| 396 | + (sortref == 0 || sortref != cur_ec->ec_sortref)) |
393 | 397 | continue;
|
394 | 398 |
|
395 | 399 | if (!equal(opfamilies, cur_ec->ec_opfamilies))
|
@@ -433,6 +437,10 @@ get_eclass_for_sort_expr(PlannerInfo *root,
|
433 | 437 | newec->ec_broken = false;
|
434 | 438 | newec->ec_sortref = sortref;
|
435 | 439 | newec->ec_merged = NULL;
|
| 440 | + |
| 441 | + if (newec->ec_has_volatile && sortref == 0) /* should not happen */ |
| 442 | + elog(ERROR, "volatile EquivalenceClass has no sortref"); |
| 443 | + |
436 | 444 | newem = add_eq_member(newec, expr, pull_varnos((Node *) expr),
|
437 | 445 | false, expr_datatype);
|
438 | 446 |
|
|
0 commit comments