postgresql/src/backend
David Rowley 19c60ad69a Optimize compactify_tuples function
This function could often be seen in profiles of vacuum and could often
be a significant bottleneck during recovery. The problem was that a qsort
was performed in order to sort an array of item pointers in reverse offset
order so that we could use that to safely move tuples up to the end of the
page without overwriting the memory of yet-to-be-moved tuples. i.e. we
used to compact the page starting at the back of the page and move towards
the front. The qsort that this required could be expensive for pages with
a large number of tuples.

In this commit, we take another approach to tuple compactification.

Now, instead of sorting the remaining item pointers array we first check
if the array is presorted and only memmove() the tuples that need to be
moved. This presorted check can be done very cheaply in the calling
functions when the array is being populated. This presorted case is very
fast.

When the item pointer array is not presorted we must copy tuples that need
to be moved into a temp buffer before copying them back into the page
again. This differs from what we used to do here as we're now copying the
tuples back into the page in reverse line pointer order. Previously we
left the existing order alone.  Reordering the tuples results in an
increased likelihood of hitting the pre-sorted case the next time around.
Any newly added tuple which consumes a new line pointer will also maintain
the correct sort order of tuples in the page which will also result in the
presorted case being hit the next time.  Only consuming an unused line
pointer can cause the order of tuples to go out again, but that will be
corrected next time the function is called for the page.

Benchmarks have shown that the non-presorted case is at least equally as
fast as the original qsort method even when the page just has a few
tuples. As the number of tuples becomes larger the new method maintains
its performance whereas the original qsort method became much slower when
the number of tuples on the page became large.

Author: David Rowley
Reviewed-by: Thomas Munro
Tested-by: Jakub Wartak
Discussion: https://postgr.es/m/CA+hUKGKMQFVpjr106gRhwk6R-nXv0qOcTreZuQzxgpHESAL6dw@mail.gmail.com
2020-09-16 13:22:20 +12:00
..
access Report resource usage at the end of recovery 2020-09-16 11:25:46 +12:00
bootstrap Improve some ancient, crufty code in bootstrap + initdb. 2020-09-05 16:20:04 -04:00
catalog Make index_set_state_flags() transactional 2020-09-14 13:56:41 +09:00
commands Fix use-after-free bug with event triggers in an extension script 2020-09-15 21:03:14 -03:00
executor logtape.c: do not preallocate for tapes when sorting 2020-09-11 17:10:02 -07:00
foreign Update copyrights for 2020 2020-01-01 12:21:45 -05:00
jit Fix a few typos in JIT comments and README 2020-08-21 09:33:56 +12:00
lib Use pg_bitutils for HyperLogLog. 2020-07-30 09:14:23 -07:00
libpq Message fixes and style improvements 2020-09-14 06:42:30 +02:00
main Clean up includes of s_lock.h. 2020-06-18 19:41:05 -07:00
nodes Message fixes and style improvements 2020-09-14 06:42:30 +02:00
optimizer Allow incremental sorts for windowing functions 2020-09-15 23:44:45 +12:00
parser Message fixes and style improvements 2020-09-14 06:42:30 +02:00
partitioning Clean up some code and comments in partbounds.c. 2020-09-10 18:00:00 +09:00
po Translation updates 2020-05-18 12:49:30 +02:00
port Add huge_page_size setting for use on Linux. 2020-07-17 14:33:00 +12:00
postmaster Fix inconsistency in determining the timestamp of the db statfile. 2020-09-12 08:02:54 +05:30
regex Dial back -Wimplicit-fallthrough to level 3 2020-05-13 15:31:14 -04:00
replication Make walsenders show their replication commands in pg_stat_activity. 2020-09-14 12:35:00 -04:00
rewrite Redefine pg_class.reltuples to be -1 before the first VACUUM or ANALYZE. 2020-08-30 12:21:51 -04:00
snowball code: replace most remaining uses of 'master'. 2020-07-08 13:24:35 -07:00
statistics Remove some more useless assignments. 2020-09-04 14:32:19 -04:00
storage Optimize compactify_tuples function 2020-09-16 13:22:20 +12:00
tcop Use the properly transformed RangeVar for expandTableLikeClause(). 2020-09-13 12:51:21 -04:00
tsearch Yet more elimination of dead stores and useless initializations. 2020-09-05 13:17:32 -04:00
utils Fix compiler warning 2020-09-15 15:07:57 +12:00
.gitignore
common.mk
Makefile Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nls.mk Add missing gettext triggers 2020-04-28 13:35:40 +02:00