Re: Tracking per-RelOptInfo uniqueness during planning

Поиск
Список
Период
Сортировка
Искать

Re: Tracking per-RelOptInfo uniqueness during planning

От:
Antonin Houska <ah@cybertec.at>
Дата:
Вложения:
Richard Guo  wrote:

> This has been proposed several times over the years.  The most recent
> and most developed attempts are the work by Andy Fan and David Rowley
> [1].  This patch takes a lot from that work and the discussion around
> it: the basic shape of a per-RelOptInfo list of unique keys, built
> bottom-up, with the keys expressed over ECs in the same spirit as
> pathkeys.

Thanks for working on this. Following is my initial review.

What makes it a bit difficult for me to think about the logic is the
'nullable' field of the UniqueKey structure. I don't think the name is very
descriptive. It seems to indicate that an OJ generated problematic NULL values
that affect uniqueness of the output, however populate_plain_rel_uniquekeys()
appears to set the sometimes as well.

I think the field name should rather give a hint about potential use of the
unique key, rather then telling how it was derived. Would something like
'null_equality' or 'consider_nulls_equal' make sense? In such a case, TRUE
value would then mean "the path output is unique even if NULL values are
considered equal".


Another thing I don't understand is in get_interesting_unique_ecs():

        base_relids = bms_difference(ec->ec_relids, root->outer_join_rels);

        if (bms_membership(base_relids) == BMS_MULTIPLE)
                result = add_base_ec_positions(root, result, ec);

Do we really need more than one non-nullable rels? Note that the function
calls add_base_ec_positions(), for wich IMO a single non-nullable relation is
sufficient:

        /* If ec is already a base EC, we add its own position */
        if (!bms_overlap(ec->ec_relids, root->outer_join_rels))
                return bms_add_member(result, ec->ec_index);


Besides that, why do you create the ECs for unique keys in advance
("eagerly"), rather than creating them when actually needed ("lazily"?  Is the
reason that you only want to construct new ECs from the existing ones (because
it's easier to retrieve the operator info), and thus the set of new ECs is
does not depend on the uniqueness-related properties of relations?

I think that in the earlier versions of the patch, creation of the ECs was
driven by target expressions. This way we can generate more unique keys, but
construction of the corresponding ECs may be more difficult.


A few comments on coding:

* in populate_subquery_rel_uniquekeys(), I think the test

        if (subquery->hasTargetSRFs)
                return;

should appear at the very beginning, even before calling
get_interesting_unique_ecs(), because it's very cheap.


* populate_joinrel_uniquekeys()

        /*
         * Preservation of the RHS keys, if the LHS is unique for the clauses.
         * Across a left join they become nullable, and an empty key does not
         * survive null-extension (one null-extended row per unmatched LHS row).
         */
        if (rhs->uniquekeys != NIL &&
                !bms_overlap(rhs->lateral_relids, lhs->relids) &&
                side_is_unique(root, joinrel, lhs, rhs, jointype, restrictlist))
        {
                foreach_node(UniqueKey, ukey, rhs->uniquekeys)
                {
                        if (jointype == JOIN_INNER)
                                add_uniquekey(joinrel, ukey->eclass_indexes, ukey->nullable);
                        else if (!bms_is_empty(ukey->eclass_indexes))
                                add_uniquekey(joinrel, ukey->eclass_indexes, true);
                }
        }

1. If jointype != JOIN_INNER, shouldn't we assert that it's JOIN_LEFT?

2. For the left join, should add_uniquekey() alwyas be called with
nullable=true? If the LHS has uniquekeys at the same time, then the
null-extended rows should be unique too, so the uniquekeys of the join might
still be usable as a proof that the DISTINCT step is not necessary. (Of
course, the 'nullable' attribute of the LHS uniquekeys matters in this case.)


* translate_subquery_uniquekeys()

A comment explaining why 'subroot' can be NULL would make sense:

        if (subroot == NULL)
                return;

A few more minor suggestions are attached.


A suggestion for future improvement (or have I missed that the patch already
does that?): we can add unique keys to the grouping paths created due to eager
aggregation.

-- 
Antonin Houska
Web: https://www.cybertec-postgresql.com

Re: Tracking per-RelOptInfo uniqueness during planning

От:
Ilia Evdokimov <ilidock95@gmail.com>
Дата:
Would it make sense to add an `enable_uniquekeys GUC`, enabled by 
default? UniqueKeys can eliminate DISTINCT/GROUP BY steps and mark joins 
as inner-unique, so an incorrect uniqueness proof could affect query 
results. A switch would help diagnose regressions and temporarily 
disable the optimization if an issue is found. Eager aggregation and 
self-join elimination already have such switches: 
`enable_eager_aggregate` and `enable_self_join_elimination`.

--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/



Re: Tracking per-RelOptInfo uniqueness during planning

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
Вложения:
(Sorry it took me so long to get back to this work.)

While working through Antonin's review of v4, I became less and less
happy with keeping UniqueKeys on join relations.

The number of keys on a join relation grows quickly, as Antonin
pointed out, because the union of a key from each side is a key of the
join.  v4 caps the list at 8, but then which keys survive depends on
the order they are added in.  Worse, a join relation's keys are
derived from whichever pair of input relations that the join search
tries first, and a different pair can give different keys.  And we
build all these keys for every join relation in every query, though
most of them are ever looked at.

I've been trying a new idea on this.  The idea is that we keep
UniqueKeys for base relations only.  For a join relation, we work out
whether it is distinct for a given set of ECs at the time the question
is asked.  If some base relation in the join has a UniqueKey within
that set, only one of its rows is possible, so the ECs of all its
columns can be added to the set, which may in turn cover a key of
another relation.  We repeat that until every base relation is
accounted for or we get stuck.  For example, in

  SELECT * FROM a LEFT JOIN (b JOIN c ON b.x = c.id) ON a.x = b.id;

with unique indexes on b.id and c.id, the clause "a.x = b.id" fixes
b.id, that leaves one row of b, which fixes b.x and so c.id, and that
leaves one row of c.  So the join of b and c is unique for a.

There is nothing to cap, the answer doesn't depend on the join order,
and a query that never asks pays nothing.  The loop is just bitmapset
operations on the keys of the base relations, so it should be cheap.

I've split the implementation into small steps.  Attached are the
first three I've got so far.

0001 detects unique inner sides of joins when the inner side is a
join, for plain tables and inner joins only.

0002 skips DISTINCT and GROUP BY steps whose input is already
distinct.  A UniqueKey is marked null_aware if it also holds with
NULLs considered equal.

0003 skips a DISTINCT step that follows grouping or window functions.

There is still a lot of work to do, including outer joins, semi/anti
joins, child relations, partially aggregated relations and subqueries.
But I'd like to hear what people think of the approach first.

- Richard
FAQ