AW: Missing constant propagation in planner on hash quals causes join slowdown

From: Hans Buschmann <buschmann(at)nidsa(dot)net>
To: Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: AW: Missing constant propagation in planner on hash quals causes join slowdown
Date: 2019-11-09 15:40:03
Message-ID: 1573313917202.76914@nidsa.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Thanks for looking at it.

I think these two queries are equivalent, as shown by the explain.

In both cases the index scan only selects tuples with xx_season=3 as shown in both explains:

Index Cond: (tmaster.t1_season = 3)
Index Cond: (tfact.t2_season = 3)
So no tuple can have a null value for xx_season.

My point is the construction of the hash table, wich includes the t2_season even if it is constant and not null. From explain:

with overhead:
Hash Cond: ((tmaster.t1_season = tfact.t2_season) AND (tmaster.t1_id_t2 = tfact.id_t2))

optimized:
Hash Cond: (tmaster.t1_id_t2 = tfact.id_t2)

The planner correctly sets the index conditions (knows that the xx_season columns are constant), but fails to apply this constantness to the hash conditions by discarding a constant column in a hash table.

In my real application most of the xx_season columns are declared not null, but this should not change the outcome.

The performance difference is slightly lower when the created tables are previously analyzed (what I forgot).

But the percentual gain is much higher considering only the construction of the hash table, the only part of the query execution altered by this optimization.

In my opinion this scenario could be quite common in multi-tenant cases, in logging, time based data sets etc.

I tried to look at the pg source code but could not yet find the place where the hash conditions are selected and potentially tested.

When optimizing the constants away there my be a special case where all hash conditions are constants, so a hash table has not to be build (or at least one hash cond has to be preserved).

Hans Buschmann

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2019-11-09 16:08:14 Re: CountDBSubscriptions check in dropdb
Previous Message Tomas Vondra 2019-11-09 13:43:30 Re: Missing constant propagation in planner on hash quals causes join slowdown