pgsql: Speed up ruleutils' name de-duplication code, and fix overlength - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Speed up ruleutils' name de-duplication code, and fix overlength
Date
Msg-id E1ZyOmV-0003Hn-Cm@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Speed up ruleutils' name de-duplication code, and fix overlength-name case.

Since commit 11e131854f8231a21613f834c40fe9d046926387, ruleutils.c has
attempted to ensure that each RTE in a query or plan tree has a unique
alias name.  However, the code that was added for this could be quite slow,
even as bad as O(N^3) if N identical RTE names must be replaced, as noted
by Jeff Janes.  Improve matters by building a transient hash table within
set_rtable_names.  The hash table in itself reduces the cost of detecting a
duplicate from O(N) to O(1), and we can save another factor of N by storing
the number of de-duplicated names already created for each entry, so that
we don't have to re-try names already created.  This way is probably a bit
slower overall for small range tables, but almost by definition, such cases
should not be a performance problem.

In principle the same problem applies to the column-name-de-duplication
code; but in practice that seems to be less of a problem, first because
N is limited since we don't support extremely wide tables, and second
because duplicate column names within an RTE are fairly rare, so that in
practice the cost is more like O(N^2) not O(N^3).  It would be very much
messier to fix the column-name code, so for now I've left that alone.

An independent problem in the same area was that the de-duplication code
paid no attention to the identifier length limit, and would happily produce
identifiers that were longer than NAMEDATALEN and wouldn't be unique after
truncation to NAMEDATALEN.  This could result in dump/reload failures, or
perhaps even views that silently behaved differently than before.  We can
fix that by shortening the base name as needed.  Fix it for both the
relation and column name cases.

In passing, check for interrupts in set_rtable_names, just in case it's
still slow enough to be an issue.

Back-patch to 9.3 where this code was introduced.

Branch
------
REL9_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/a6c4c07fc5cd0665568ff48ada3b65900fafa1af

Modified Files
--------------
src/backend/utils/adt/ruleutils.c         |  171 +++++++++++++++++++++--------
src/test/regress/expected/create_view.out |   27 +++++
src/test/regress/sql/create_view.sql      |    9 ++
3 files changed, 161 insertions(+), 46 deletions(-)


pgsql-committers by date:

Previous
From: Robert Haas
Date:
Subject: pgsql: Remove accidentally-committed debugging code.
Next
From: Tom Lane
Date:
Subject: pgsql: Speed up ruleutils' name de-duplication code, and fix overlength