]> granicus.if.org Git - postgresql/commit
Support "expanded" objects, particularly arrays, for better performance.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 14 May 2015 16:08:40 +0000 (12:08 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 14 May 2015 16:08:49 +0000 (12:08 -0400)
commit1dc5ebc9077ab742079ce5dac9a6664248d42916
tree68aa827a8be94c16b456d8f78263507fcff9ee4a
parent8a2e1edd2ba0817313c1c0ef76b03a5ab819d17f
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
27 files changed:
doc/src/sgml/storage.sgml
doc/src/sgml/xtypes.sgml
src/backend/access/common/heaptuple.c
src/backend/access/heap/tuptoaster.c
src/backend/executor/execQual.c
src/backend/executor/execTuples.c
src/backend/executor/nodeSubqueryscan.c
src/backend/executor/spi.c
src/backend/utils/adt/Makefile
src/backend/utils/adt/array_expanded.c [new file with mode: 0644]
src/backend/utils/adt/array_userfuncs.c
src/backend/utils/adt/arrayfuncs.c
src/backend/utils/adt/datum.c
src/backend/utils/adt/expandeddatum.c [new file with mode: 0644]
src/backend/utils/mmgr/mcxt.c
src/include/executor/spi.h
src/include/executor/tuptable.h
src/include/nodes/primnodes.h
src/include/postgres.h
src/include/utils/array.h
src/include/utils/arrayaccess.h [new file with mode: 0644]
src/include/utils/datum.h
src/include/utils/expandeddatum.h [new file with mode: 0644]
src/pl/plpgsql/src/pl_comp.c
src/pl/plpgsql/src/pl_exec.c
src/pl/plpgsql/src/pl_gram.y
src/pl/plpgsql/src/plpgsql.h