pgsql: Invent a "one-shot" variant of CachedPlans for better performanc - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Invent a "one-shot" variant of CachedPlans for better performanc
Date
Msg-id E1TrFyM-0003QY-62@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Invent a "one-shot" variant of CachedPlans for better performance.

SPI_execute() and related functions create a CachedPlan, execute it once,
and immediately discard it, so that the functionality offered by
plancache.c is of no value in this code path.  And performance measurements
show that the extra data copying and invalidation checking done by
plancache.c slows down simple queries by 10% or more compared to 9.1.
However, enough of the SPI code is shared with functions that do need plan
caching that it seems impractical to bypass plancache.c altogether.
Instead, let's invent a variant version of cached plans that preserves
99% of the API but doesn't offer any of the actual functionality, nor the
overhead.  This puts SPI_execute() performance back on par, or maybe even
slightly better, than it was before.  This change should resolve recent
complaints of performance degradation from Dong Ye, Pavel Stehule, and
others.

By avoiding data copying, this change also reduces the amount of memory
needed to execute many-statement SPI_execute() strings, as for instance in
a recent complaint from Tomas Vondra.

An additional benefit of this change is that multi-statement SPI_execute()
query strings are now processed fully serially, that is we complete
execution of earlier statements before running parse analysis and planning
on following ones.  This eliminates a long-standing POLA violation, in that
DDL that affects the behavior of a later statement will now behave as
expected.

Back-patch to 9.2, since this was a performance regression compared to 9.1.
(In 9.2, place the added struct fields so as to avoid changing the offsets
of existing fields.)

Heikki Linnakangas and Tom Lane

Branch
------
REL9_2_STABLE

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

Modified Files
--------------
doc/src/sgml/spi.sgml               |   25 +++--
src/backend/executor/spi.c          |  144 +++++++++++++++++++++++---
src/backend/utils/cache/plancache.c |  191 +++++++++++++++++++++++++++++------
src/include/executor/spi_priv.h     |    7 ++
src/include/utils/plancache.h       |   19 +++-
5 files changed, 329 insertions(+), 57 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Invent a "one-shot" variant of CachedPlans for better performanc
Next
From: Peter Eisentraut
Date:
Subject: pgsql: Make some spelling more consistent