Re: pgsql: Improve memory management for external sorts.

Lists: pgsql-committerspgsql-hackers
From: Robert Haas <rhaas(at)postgresql(dot)org>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Improve memory management for external sorts.
Date: 2016-03-17 20:11:00
Message-ID: E1ageG4-0001Sj-RW@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

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(-)


From: Aleksander Alekseev <a(dot)alekseev(at)postgrespro(dot)ru>
To: Robert Haas <rhaas(at)postgresql(dot)org>
Cc: pgsql-committers(at)postgresql(dot)org
Subject: Re: pgsql: Improve memory management for external sorts.
Date: 2016-03-18 07:48:22
Message-ID: 20160318104822.334d3806@fujitsu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

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 Content-Type Size
typo.diff text/x-patch 609 bytes

From: Aleksander Alekseev <a(dot)alekseev(at)postgrespro(dot)ru>
To: Robert Haas <rhaas(at)postgresql(dot)org>
Cc: pgsql-committers(at)postgresql(dot)org
Subject: Re: pgsql: Improve memory management for external sorts.
Date: 2016-03-18 07:54:49
Message-ID: 20160318105449.5abb40dc@fujitsu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

> There is also a typo in commit message: s/management/management/

Oops. I meant "mangement" :)

commit c27033ff7c17b5100d02c454a0eebb95ec7b91cc
Author: Robert Haas <rhaas(at)postgresql(dot)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/


From: Andres Freund <andres(at)anarazel(dot)de>
To: Robert Haas <rhaas(at)postgresql(dot)org>
Cc: pgsql-committers(at)postgresql(dot)org
Subject: Re: pgsql: Improve memory management for external sorts.
Date: 2016-03-18 18:25:05
Message-ID: 20160318182505.tfmduy5istafshhp@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

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 ‘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 "
^
/home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c:2695:34: warning: format ‘%ld’ expects argument of type ‘long int’, 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


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [COMMITTERS] pgsql: Improve memory management for external sorts.
Date: 2016-03-18 18:39:55
Message-ID: CA+Tgmoa4xYEi+Dn7tjFBJBC9C7fpouNNW_L7NBUWw4mdVHSaEw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

On Fri, Mar 18, 2016 at 2:25 PM, Andres Freund <andres(at)anarazel(dot)de> wrote:
> 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 ‘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 "
> ^
> /home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c:2695:34: warning: format ‘%ld’ expects argument of type ‘long int’, 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.

Oops. Thanks for the report. Does this fix it?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [COMMITTERS] pgsql: Improve memory management for external sorts.
Date: 2016-03-18 18:40:33
Message-ID: CA+TgmobkDRj0eBa9WG17YcwSmJfF25NLf0X4Y_=iVkX80S5bqw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

On Fri, Mar 18, 2016 at 2:39 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Fri, Mar 18, 2016 at 2:25 PM, Andres Freund <andres(at)anarazel(dot)de> wrote:
>> 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 ‘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 "
>> ^
>> /home/andres/src/postgresql/src/backend/utils/sort/tuplesort.c:2695:34: warning: format ‘%ld’ expects argument of type ‘long int’, 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.
>
> Oops. Thanks for the report. Does this fix it?

Trying again to attach the patch.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

Attachment Content-Type Size
int64-fmt-fixup.patch application/x-download 787 bytes

From: Andres Freund <andres(at)anarazel(dot)de>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [COMMITTERS] pgsql: Improve memory management for external sorts.
Date: 2016-03-18 18:43:35
Message-ID: 20160318184335.smvhkxikrg4fqyb6@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

On 2016-03-18 14:40:33 -0400, Robert Haas wrote:
> > Oops. Thanks for the report. Does this fix it?
>
> Trying again to attach the patch.

Yes, that removes the warning, and looks correct.

Andres Freund


From: Peter Geoghegan <pg(at)heroku(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [COMMITTERS] pgsql: Improve memory management for external sorts.
Date: 2016-03-18 20:22:59
Message-ID: CAM3SWZQ-=KCcrcrjRmN2j181n8d=5ME+AZj9TitQgrPj4QF3nA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

On Fri, Mar 18, 2016 at 11:43 AM, Andres Freund <andres(at)anarazel(dot)de> wrote:
> Yes, that removes the warning, and looks correct.

Thanks. We should be careful to not repeat this mistake when the
quicksort patch goes in.

--
Peter Geoghegan


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Peter Geoghegan <pg(at)heroku(dot)com>
Cc: Andres Freund <andres(at)anarazel(dot)de>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [COMMITTERS] pgsql: Improve memory management for external sorts.
Date: 2016-03-19 13:38:38
Message-ID: CA+TgmoY423xKaRX7J5G+nsjmA-YJ_K1NV_K12nbmWE-b5oKO5Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

On Fri, Mar 18, 2016 at 4:22 PM, Peter Geoghegan <pg(at)heroku(dot)com> wrote:
> On Fri, Mar 18, 2016 at 11:43 AM, Andres Freund <andres(at)anarazel(dot)de> wrote:
>> Yes, that removes the warning, and looks correct.
>
> Thanks. We should be careful to not repeat this mistake when the
> quicksort patch goes in.

It would be helpful if you could either (a) confirm that that patch
still applies and that it has no issues of this type or (b) post an
updated version.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Peter Geoghegan <pg(at)heroku(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Andres Freund <andres(at)anarazel(dot)de>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [COMMITTERS] pgsql: Improve memory management for external sorts.
Date: 2016-03-19 18:21:31
Message-ID: CAM3SWZQEGTc4_cJ=ug65ypyO=2A249va1=3DoY0Z=wvuh9iMSQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-committers pgsql-hackers

On Sat, Mar 19, 2016 at 6:38 AM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> It would be helpful if you could either (a) confirm that that patch
> still applies and that it has no issues of this type or (b) post an
> updated version.

I don't think that it has a problem with lacking the right int64
format specifiers. However, I had a bad feeling about integer overflow
of state->currentRun, and think I'll need to address that. After all,
if runs are now no longer 2x work_mem on average, it's not completely
ridiculous to imagine that being an issue on a misconfigured system.

--
Peter Geoghegan