pgsql: Support "expanded" objects, particularly arrays, for better perf

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Support "expanded" objects, particularly arrays, for better perf
Date: 2015-05-14 16:09:01
Message-ID: E1Ysvgz-0000s0-DP@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Support "expanded" objects, particularly arrays, for better performance.

This patch introduces the ability for complex datatypes to have an
in-memory representation that is different from their on-disk format.
On-disk formats are typically optimized for minimal size, and in any case
they can't contain pointers, so they are often not well-suited for
computation. Now a datatype can invent an "expanded" in-memory format
that is better suited for its operations, and then pass that around among
the C functions that operate on the datatype. There are also provisions
(rudimentary as yet) to allow an expanded object to be modified in-place
under suitable conditions, so that operations like assignment to an element
of an array need not involve copying the entire array.

The initial application for this feature is arrays, but it is not hard
to foresee using it for other container types like JSON, XML and hstore.
I have hopes that it will be useful to PostGIS as well.

In this initial implementation, a few heuristics have been hard-wired
into plpgsql to improve performance for arrays that are stored in
plpgsql variables. We would like to generalize those hacks so that
other datatypes can obtain similar improvements, but figuring out some
appropriate APIs is left as a task for future work. (The heuristics
themselves are probably not optimal yet, either, as they sometimes
force expansion of arrays that would be better left alone.)

Preliminary performance testing shows impressive speed gains for plpgsql
functions that do element-by-element access or update of large arrays.
There are other cases that get a little slower, as a result of added array
format conversions; but we can hope to improve anything that's annoyingly
bad. In any case most applications should see a net win.

Tom Lane, reviewed by Andres Freund

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/1dc5ebc9077ab742079ce5dac9a6664248d42916

Modified Files
--------------
doc/src/sgml/storage.sgml | 42 +-
doc/src/sgml/xtypes.sgml | 71 +++
src/backend/access/common/heaptuple.c | 45 +-
src/backend/access/heap/tuptoaster.c | 36 ++
src/backend/executor/execQual.c | 12 +-
src/backend/executor/execTuples.c | 47 ++
src/backend/executor/nodeSubqueryscan.c | 8 +
src/backend/executor/spi.c | 21 +
src/backend/utils/adt/Makefile | 9 +-
src/backend/utils/adt/array_expanded.c | 455 ++++++++++++++
src/backend/utils/adt/array_userfuncs.c | 105 ++--
src/backend/utils/adt/arrayfuncs.c | 991 +++++++++++++++++++------------
src/backend/utils/adt/datum.c | 86 ++-
src/backend/utils/adt/expandeddatum.c | 163 +++++
src/backend/utils/mmgr/mcxt.c | 4 +
src/include/executor/spi.h | 1 +
src/include/executor/tuptable.h | 1 +
src/include/nodes/primnodes.h | 4 +
src/include/postgres.h | 30 +
src/include/utils/array.h | 143 ++++-
src/include/utils/arrayaccess.h | 133 +++++
src/include/utils/datum.h | 8 +-
src/include/utils/expandeddatum.h | 151 +++++
src/pl/plpgsql/src/pl_comp.c | 16 +
src/pl/plpgsql/src/pl_exec.c | 301 +++++++++-
src/pl/plpgsql/src/pl_gram.y | 3 +
src/pl/plpgsql/src/plpgsql.h | 2 +
27 files changed, 2362 insertions(+), 526 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Stephen Frost 2015-05-14 16:10:32 Re: pgsql: Add pg_audit, an auditing extension
Previous Message Alvaro Herrera 2015-05-14 16:01:47 Re: [COMMITTERS] pgsql: Add pg_audit, an auditing extension