postgresql/src/include
Peter Geoghegan 0d861bbb70 Add deduplication to nbtree.
Deduplication reduces the storage overhead of duplicates in indexes that
use the standard nbtree index access method.  The deduplication process
is applied lazily, after the point where opportunistic deletion of
LP_DEAD-marked index tuples occurs.  Deduplication is only applied at
the point where a leaf page split would otherwise be required.  New
posting list tuples are formed by merging together existing duplicate
tuples.  The physical representation of the items on an nbtree leaf page
is made more space efficient by deduplication, but the logical contents
of the page are not changed.  Even unique indexes make use of
deduplication as a way of controlling bloat from duplicates whose TIDs
point to different versions of the same logical table row.

The lazy approach taken by nbtree has significant advantages over a GIN
style eager approach.  Most individual inserts of index tuples have
exactly the same overhead as before.  The extra overhead of
deduplication is amortized across insertions, just like the overhead of
page splits.  The key space of indexes works in the same way as it has
since commit dd299df8 (the commit that made heap TID a tiebreaker
column).

Testing has shown that nbtree deduplication can generally make indexes
with about 10 or 15 tuples for each distinct key value about 2.5X - 4X
smaller, even with single column integer indexes (e.g., an index on a
referencing column that accompanies a foreign key).  The final size of
single column nbtree indexes comes close to the final size of a similar
contrib/btree_gin index, at least in cases where GIN's posting list
compression isn't very effective.  This can significantly improve
transaction throughput, and significantly reduce the cost of vacuuming
indexes.

A new index storage parameter (deduplicate_items) controls the use of
deduplication.  The default setting is 'on', so all new B-Tree indexes
automatically use deduplication where possible.  This decision will be
reviewed at the end of the Postgres 13 beta period.

There is a regression of approximately 2% of transaction throughput with
synthetic workloads that consist of append-only inserts into a table
with several non-unique indexes, where all indexes have few or no
repeated values.  The underlying issue is that cycles are wasted on
unsuccessful attempts at deduplicating items in non-unique indexes.
There doesn't seem to be a way around it short of disabling
deduplication entirely.  Note that deduplication of items in unique
indexes is fairly well targeted in general, which avoids the problem
there (we can use a special heuristic to trigger deduplication passes in
unique indexes, since we're specifically targeting "version bloat").

Bump XLOG_PAGE_MAGIC because xl_btree_vacuum changed.

No bump in BTREE_VERSION, since the representation of posting list
tuples works in a way that's backwards compatible with version 4 indexes
(i.e. indexes built on PostgreSQL 12).  However, users must still
REINDEX a pg_upgrade'd index to use deduplication, regardless of the
Postgres version they've upgraded from.  This is the only way to set the
new nbtree metapage flag indicating that deduplication is generally
safe.

Author: Anastasia Lubennikova, Peter Geoghegan
Reviewed-By: Peter Geoghegan, Heikki Linnakangas
Discussion:
    https://postgr.es/m/55E4051B.7020209@postgrespro.ru
    https://postgr.es/m/4ab6e2db-bcee-f4cf-0916-3a06e6ccbb55@postgrespro.ru
2020-02-26 13:05:30 -08:00
..
access Add deduplication to nbtree. 2020-02-26 13:05:30 -08:00
bootstrap Update copyrights for 2020 2020-01-01 12:21:45 -05:00
catalog Add equalimage B-Tree support functions. 2020-02-26 11:28:25 -08:00
commands Invent "trusted" extensions, and remove the pg_pltemplate catalog. 2020-01-29 18:42:43 -05:00
common Move jsonapi.c and jsonapi.h to src/common. 2020-01-29 10:22:51 -05:00
datatype Update copyrights for 2020 2020-01-01 12:21:45 -05:00
executor expression eval: Reduce number of steps for agg transition invocations. 2020-02-24 15:09:09 -08:00
fe_utils Reduce size of backend scanner's tables. 2020-01-13 15:04:31 -05:00
foreign Update copyrights for 2020 2020-01-01 12:21:45 -05:00
jit jit: Reference expression step functions via llvmjit_types. 2020-02-06 22:29:14 -08:00
lib Skip memcpy(x, x) in qunique(). 2020-01-04 11:31:42 -08:00
libpq Allow building without default socket directory 2020-01-31 16:28:43 +01:00
mb Rationalize code placement between wchar.c, encnames.c, and mbutils.c. 2020-01-16 18:08:21 -05:00
nodes Move bitmap_hash and bitmap_match to bitmapset.c. 2020-02-24 17:17:43 +05:30
optimizer Update copyrights for 2020 2020-01-01 12:21:45 -05:00
parser Fill in extraUpdatedCols in logical replication 2020-02-17 15:20:57 +01:00
partitioning Update copyrights for 2020 2020-01-01 12:21:45 -05:00
port Assume that we have utime() and <utime.h>. 2020-02-21 14:30:47 -05:00
portability Update copyrights for 2020 2020-01-01 12:21:45 -05:00
postmaster Update copyrights for 2020 2020-01-01 12:21:45 -05:00
regex Assume that we have <wchar.h>. 2020-02-21 14:30:47 -05:00
replication Set ReorderBufferTXN->final_lsn more eagerly 2020-01-17 18:00:39 -03:00
rewrite Update copyrights for 2020 2020-01-01 12:21:45 -05:00
snowball Update copyrights for 2020 2020-01-01 12:21:45 -05:00
statistics Update copyrights for 2020 2020-01-01 12:21:45 -05:00
storage Account explicitly for long-lived FDs that are allocated outside fd.c. 2020-02-24 17:28:33 -05:00
tcop Fix problems with "read only query" checks, and refactor the code. 2020-01-16 12:11:31 -05:00
tsearch Assume that we have <wchar.h>. 2020-02-21 14:30:47 -05:00
utils Adapt hashfn.c and hashutils.h for frontend use. 2020-02-24 17:27:15 +05:30
.gitignore Refactor dlopen() support 2018-09-06 11:33:04 +02:00
c.h Assume that we have signed integral types and flexible array members. 2020-02-21 14:30:48 -05:00
fmgr.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
funcapi.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
getaddrinfo.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
getopt_long.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
Makefile Get rid of jsonpath_gram.h and jsonpath_scanner.h 2019-03-20 11:13:34 +03:00
miscadmin.h Make vacuum buffer counters 64 bits wide 2020-02-05 16:59:29 -03:00
pg_config.h.in Assume that we have signed integral types and flexible array members. 2020-02-21 14:30:48 -05:00
pg_config_ext.h.in Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_manual.h Allow building without default socket directory 2020-01-31 16:28:43 +01:00
pg_getopt.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_trace.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pgstat.h Add description about GSSOpenServer wait event into document. 2020-02-17 16:16:08 +09:00
pgtar.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pgtime.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
port.h Assume that we have rint(). 2020-02-21 14:30:47 -05:00
postgres.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
postgres_ext.h Phase 2 of pgindent updates. 2017-06-21 15:19:25 -04:00
postgres_fe.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
rusagestub.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
windowapi.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00