Thread: pgsql: Improve memory management for external sorts.

pgsql: Improve memory management for external sorts.

From
Robert Haas
Date:
Improve memory management for external sorts.

Introduce a new memory context which stores tuple data, and reset it
at the end of each merge pass; this helps avoid memory fragmentation
and, consequently, overallocation.  Also, for the final merge patch,
eliminate memory context chunk header overhead entirely by allocating
all of the memory used for buffering tuples during the merge in a
single chunk.  Since this modestly increases the number of tuples we
can store, grow the memtuples array a bit so that we're less likely to
run short of slots there.

Peter Geoghegan.  Review and testing of patches in this series by
Jeff Janes, Greg Stark, Mithun Cy, and me.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/0011c0091e886b874e485a46ff2c94222ffbf550

Modified Files
--------------
src/backend/utils/sort/tuplesort.c | 556 ++++++++++++++++++++++++++++++++++---
1 file changed, 516 insertions(+), 40 deletions(-)


Re: pgsql: Improve memory management for external sorts.

From
Aleksander Alekseev
Date:
There is a typo in a comment. See attachment.

There is also a typo in commit message: s/management/management/. But it
is my understanding that we don't fix such things.

Attachment

Re: pgsql: Improve memory management for external sorts.

From
Aleksander Alekseev
Date:
> There is also a typo in commit message: s/management/management/

Oops. I meant "mangement" :)

commit c27033ff7c17b5100d02c454a0eebb95ec7b91cc
Author: Robert Haas <rhaas@postgresql.org>
Date:   Thu Mar 17 16:11:14 2016 -0400

    Update tuplesort.c comments for memory mangement improvements.



--
Best regards,
Aleksander Alekseev
http://eax.me/


Re: pgsql: Improve memory management for external sorts.

From
Andres Freund
Date:
On 2016-03-17 20:11:00 +0000, Robert Haas wrote:
> Improve memory management for external sorts.
>
> Introduce a new memory context which stores tuple data, and reset it
> at the end of each merge pass; this helps avoid memory fragmentation
> and, consequently, overallocation.  Also, for the final merge patch,
> eliminate memory context chunk header overhead entirely by allocating
> all of the memory used for buffering tuples during the merge in a
> single chunk.  Since this modestly increases the number of tuples we
> can store, grow the memtuples array a bit so that we're less likely to
> run short of slots there.
>
> Peter Geoghegan.  Review and testing of patches in this series by
> Jeff Janes, Greg Stark, Mithun Cy, and me.

Cross compiling for windows results in:
/home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c: In function ‘beginmerge’:
/home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c:2695:34: warning: format ‘%ld’ expects argument of type
‘longint’, but argument 4 has type ‘int64 {aka long long int}’ [-Wformat=] 
     elog(LOG, "tape %d initially used %ld KB of %ld KB batch "
                                  ^
/home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c:2695:34: warning: format ‘%ld’ expects argument of type
‘longint’, but argument 5 has type ‘int64 {aka long long int}’ [-Wformat=] 
config.status: creating src/interfaces/ecpg/include/ecpg_config.h

/home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c: In
function ‘beginmerge’:
/home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c:2695:34:
warning: format ‘%ld’ expects argument of type ‘long int’, but argument
4 has type ‘int64 {aka long long int}’ [-Wformat=]
     elog(LOG, "tape %d initially used %ld KB of %ld KB batch "

Which seems like a valid complain on a LLP64 platform (i.e. where long
is 32bit) like windows.

Andres