Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries

Lists: pgsql-performance
From: Petr Praus <petr(at)praus(dot)net>
To: pgsql-performance(at)postgresql(dot)org
Subject: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-10-30 19:08:56
Message-ID: CACezXZ_w7HbqSxZ=5SJH=kxb4nBDNbpDejttSaU6EC1AeO4PYg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Hello,

I have a PostgreSQL 9.2 instance running on RHEL 6.3, 8-core machine with
16GB of RAM. The server is dedicated to this database, the disks are local
RAID10. Given that the default postgresql.conf is quite conservative
regarding memory settings, I thought it might be a good idea to allow
Postgres to use more memory. To my surprise, following advice in the
performance tuning guide on Postgres wiki[2] significantly slowed down
practically every query I run but it's more noticeable on the more complex
queries.

I also tried running pgtune[1] which gave the following recommendation with
more parameters tuned, but that didn't change anything. It suggests
shared_buffers of 1/4 of RAM size which seems to in line with advice
elsewhere (and on PG wiki in particular).

default_statistics_target = 50
maintenance_work_mem = 960MB
constraint_exclusion = on
checkpoint_completion_target = 0.9
effective_cache_size = 11GB
work_mem = 96MB
wal_buffers = 8MB
checkpoint_segments = 16
shared_buffers = 3840MB
max_connections = 80

I tried reindexing the whole database after changing the settings (using
REINDEX DATABASE), but that didn't help either. I played around with
shared_buffers and work_mem. Gradually changing them from the very
conservative default values (128k / 1MB) also gradually decreased
performance.

I ran EXPLAIN (ANALYZE,BUFFERS) on a few queries and the culprit seems to
be that Hash Join is significantly slower. It's not clear to me why.

To give some specific example, I have the following query. It runs in
~2100ms on the default configuration and ~3300ms on the configuration with
increased buffer sizes:

select count(*) from contest c
left outer join contestparticipant cp on c.id=cp.contestId
left outer join teammember tm on tm.contestparticipantid=cp.id
left outer join staffmember sm on cp.id=sm.contestparticipantid
left outer join person p on p.id=cp.personid
left outer join personinfo pi on pi.id=cp.personinfoid
where pi.lastname like '%b%' or pi.firstname like '%a%';

EXPLAIN (ANALYZE,BUFFERS) for the query above:

- Default buffers: http://explain.depesz.com/s/xaHJ
- Bigger buffers: http://explain.depesz.com/s/Plk

The tables don't have anything special in them

The question is why am I observing decreased performance when I increase
buffer sizes? The machine is definitely not running out of memory.
Allocation if shared memory in OS is (`shmmax` and `shmall`) is set to very
large values, that should not be a problem. I'm not getting any errors in
the Postgres log either. I'm running autovacuum in the default
configuration but I don't expect that has anything to do with it. All
queries were run on the same machine few seconds apart, just with changed
configuration (and restarted PG).

I also found a blog post [3] which experiments with various work_mem values
that run into similar behavior I'm experiencing but it doesn't really
explain it.

[1]: http://pgfoundry.org/projects/pgtune/
[2]: http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
[3]:
http://www.depesz.com/2011/07/03/understanding-postgresql-conf-work_mem/

Thanks,
Petr Praus

PS:
I also posted the question here:
http://dba.stackexchange.com/questions/27893/increasing-work-mem-and-shared-buffers-on-postgres-9-2-significantly-slows-downbut
a few people suggested


From: Petr Praus <petr(at)praus(dot)net>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-10-30 19:44:53
Message-ID: CACezXZ_UYbzvNEP_V6-bsyzGij-kJdeNz04-o=kbMXmcLMHnqw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

I just found one particularly interesting fact: when I perform the same
test on my mid-2010 iMac (OSX 10.7.5) also with Postgres 9.2.1 and 16GB
RAM, I don't experience the slow down.
Specifically:
set work_mem='1MB';
select ...; // running time is ~1800 ms
set work_mem='96MB';
select ...' // running time is ~1500 ms

When I do exactly the same query (the one from my previous post) with
exactly the same data on the server:
I get 2100 ms with work_mem=1MB and 3200 ms with 96 MB.

The Mac has SSD so it's understandably faster, but it exhibits a behavior I
would expect. What am I doing wrong here?

Thanks.

On 30 October 2012 14:08, Petr Praus <petr(at)praus(dot)net> wrote:

> Hello,
>
> I have a PostgreSQL 9.2 instance running on RHEL 6.3, 8-core machine with
> 16GB of RAM. The server is dedicated to this database, the disks are local
> RAID10. Given that the default postgresql.conf is quite conservative
> regarding memory settings, I thought it might be a good idea to allow
> Postgres to use more memory. To my surprise, following advice in the
> performance tuning guide on Postgres wiki[2] significantly slowed down
> practically every query I run but it's more noticeable on the more complex
> queries.
>
> I also tried running pgtune[1] which gave the following recommendation
> with more parameters tuned, but that didn't change anything. It suggests
> shared_buffers of 1/4 of RAM size which seems to in line with advice
> elsewhere (and on PG wiki in particular).
>
> default_statistics_target = 50
> maintenance_work_mem = 960MB
> constraint_exclusion = on
> checkpoint_completion_target = 0.9
> effective_cache_size = 11GB
> work_mem = 96MB
> wal_buffers = 8MB
> checkpoint_segments = 16
> shared_buffers = 3840MB
> max_connections = 80
>
> I tried reindexing the whole database after changing the settings (using
> REINDEX DATABASE), but that didn't help either. I played around with
> shared_buffers and work_mem. Gradually changing them from the very
> conservative default values (128k / 1MB) also gradually decreased
> performance.
>
> I ran EXPLAIN (ANALYZE,BUFFERS) on a few queries and the culprit seems to
> be that Hash Join is significantly slower. It's not clear to me why.
>
> To give some specific example, I have the following query. It runs in
> ~2100ms on the default configuration and ~3300ms on the configuration with
> increased buffer sizes:
>
> select count(*) from contest c
> left outer join contestparticipant cp on c.id=cp.contestId
> left outer join teammember tm on tm.contestparticipantid=cp.id
> left outer join staffmember sm on cp.id=sm.contestparticipantid
> left outer join person p on p.id=cp.personid
> left outer join personinfo pi on pi.id=cp.personinfoid
> where pi.lastname like '%b%' or pi.firstname like '%a%';
>
> EXPLAIN (ANALYZE,BUFFERS) for the query above:
>
> - Default buffers: http://explain.depesz.com/s/xaHJ
> - Bigger buffers: http://explain.depesz.com/s/Plk
>
> The tables don't have anything special in them
>
> The question is why am I observing decreased performance when I increase
> buffer sizes? The machine is definitely not running out of memory.
> Allocation if shared memory in OS is (`shmmax` and `shmall`) is set to very
> large values, that should not be a problem. I'm not getting any errors in
> the Postgres log either. I'm running autovacuum in the default
> configuration but I don't expect that has anything to do with it. All
> queries were run on the same machine few seconds apart, just with changed
> configuration (and restarted PG).
>
> I also found a blog post [3] which experiments with various work_mem
> values that run into similar behavior I'm experiencing but it doesn't
> really explain it.
>
> [1]: http://pgfoundry.org/projects/pgtune/
> [2]: http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
> [3]:
> http://www.depesz.com/2011/07/03/understanding-postgresql-conf-work_mem/
>
> Thanks,
> Petr Praus
>
> PS:
> I also posted the question here:
> http://dba.stackexchange.com/questions/27893/increasing-work-mem-and-shared-buffers-on-postgres-9-2-significantly-slows-downbut a few people suggested
>


From: Petr Praus <petr(at)praus(dot)net>
To: Marcos Ortiz <mlortiz(at)uci(dot)cu>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-01 19:53:13
Message-ID: CACezXZ8hJdhaoJDaJGnq_0KN9ecsedtizn5npHsErQqjyjr47g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

- I'm using ext4
- Kernel: Linux 2.6.32-279.9.1.el6.x86_64 #1 SMP Fri Aug 31 09:04:24 EDT
2012 x86_64 x86_64 x86_64 GNU/Linux
- I haven't tuned kernel in any way except setting kernel.shmmax and
kernel.shmall to:
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
- We are using 15k drives (magnetic) connected through SAS in RAID10 setup,
I don't know precise model numbers (I can find out),

On 1 November 2012 15:40, Marcos Ortiz <mlortiz(at)uci(dot)cu> wrote:

> Regards, Petr.
> Tuning PostgreSQL is not just change the postgresql.conf, it includes more
> things like:
> - the filesystem that you are using
> - the kernel version that you using (particularly in Linux systems)
> - the tuning to kernel variables
> - the type of discs that you are using (SSDs are very fast, like you saw
> in your iMac system)
>
>
> On 10/30/2012 02:44 PM, Petr Praus wrote:
>
> I just found one particularly interesting fact: when I perform the same
> test on my mid-2010 iMac (OSX 10.7.5) also with Postgres 9.2.1 and 16GB
> RAM, I don't experience the slow down.
> Specifically:
> set work_mem='1MB';
> select ...; // running time is ~1800 ms
> set work_mem='96MB';
> select ...' // running time is ~1500 ms
>
> When I do exactly the same query (the one from my previous post) with
> exactly the same data on the server:
> I get 2100 ms with work_mem=1MB and 3200 ms with 96 MB.
>
> The Mac has SSD so it's understandably faster, but it exhibits a
> behavior I would expect. What am I doing wrong here?
>
> Thanks.
>
> On 30 October 2012 14:08, Petr Praus <petr(at)praus(dot)net> wrote:
>
>> Hello,
>>
>> I have a PostgreSQL 9.2 instance running on RHEL 6.3, 8-core machine
>> with 16GB of RAM. The server is dedicated to this database, the disks are
>> local RAID10. Given that the default postgresql.conf is quite conservative
>> regarding memory settings, I thought it might be a good idea to allow
>> Postgres to use more memory. To my surprise, following advice in the
>> performance tuning guide on Postgres wiki[2] significantly slowed down
>> practically every query I run but it's more noticeable on the more complex
>> queries.
>>
>> I also tried running pgtune[1] which gave the following recommendation
>> with more parameters tuned, but that didn't change anything. It suggests
>> shared_buffers of 1/4 of RAM size which seems to in line with advice
>> elsewhere (and on PG wiki in particular).
>>
>> default_statistics_target = 50
>> maintenance_work_mem = 960MB
>> constraint_exclusion = on
>> checkpoint_completion_target = 0.9
>> effective_cache_size = 11GB
>> work_mem = 96MB
>> wal_buffers = 8MB
>> checkpoint_segments = 16
>> shared_buffers = 3840MB
>> max_connections = 80
>>
>> I tried reindexing the whole database after changing the settings
>> (using REINDEX DATABASE), but that didn't help either. I played around with
>> shared_buffers and work_mem. Gradually changing them from the very
>> conservative default values (128k / 1MB) also gradually decreased
>> performance.
>>
>> I ran EXPLAIN (ANALYZE,BUFFERS) on a few queries and the culprit seems
>> to be that Hash Join is significantly slower. It's not clear to me why.
>>
>> To give some specific example, I have the following query. It runs in
>> ~2100ms on the default configuration and ~3300ms on the configuration with
>> increased buffer sizes:
>>
>> select count(*) from contest c
>> left outer join contestparticipant cp on c.id=cp.contestId
>> left outer join teammember tm on tm.contestparticipantid=cp.id
>> left outer join staffmember sm on cp.id=sm.contestparticipantid
>> left outer join person p on p.id=cp.personid
>> left outer join personinfo pi on pi.id=cp.personinfoid
>> where pi.lastname like '%b%' or pi.firstname like '%a%';
>>
>> EXPLAIN (ANALYZE,BUFFERS) for the query above:
>>
>> - Default buffers: http://explain.depesz.com/s/xaHJ
>> - Bigger buffers: http://explain.depesz.com/s/Plk
>>
>> The tables don't have anything special in them
>>
>> The question is why am I observing decreased performance when I
>> increase buffer sizes? The machine is definitely not running out of memory.
>> Allocation if shared memory in OS is (`shmmax` and `shmall`) is set to very
>> large values, that should not be a problem. I'm not getting any errors in
>> the Postgres log either. I'm running autovacuum in the default
>> configuration but I don't expect that has anything to do with it. All
>> queries were run on the same machine few seconds apart, just with changed
>> configuration (and restarted PG).
>>
>> I also found a blog post [3] which experiments with various work_mem
>> values that run into similar behavior I'm experiencing but it doesn't
>> really explain it.
>>
>> [1]: http://pgfoundry.org/projects/pgtune/
>> [2]: http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
>> [3]:
>> http://www.depesz.com/2011/07/03/understanding-postgresql-conf-work_mem/
>>
>> Thanks,
>> Petr Praus
>>
>> PS:
>> I also posted the question here:
>> http://dba.stackexchange.com/questions/27893/increasing-work-mem-and-shared-buffers-on-postgres-9-2-significantly-slows-downbut a few people suggested
>>
>
>
> --
> **
>
> Marcos Luis Ortíz Valmaseda
> about.me/marcosortiz
> @marcosluis2186 <http://twitter.com/marcosluis2186>
> **
>
> <http://www.uci.cu/>
>
>


From: Marcos Ortiz <mlortiz(at)uci(dot)cu>
To: Petr Praus <petr(at)praus(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-01 20:40:34
Message-ID: 5092DE42.5060206@uci.cu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Regards, Petr.
Tuning PostgreSQL is not just change the postgresql.conf, it includes
more things like:
- the filesystem that you are using
- the kernel version that you using (particularly in Linux systems)
- the tuning to kernel variables
- the type of discs that you are using (SSDs are very fast, like you saw
in your iMac system)

On 10/30/2012 02:44 PM, Petr Praus wrote:
> I just found one particularly interesting fact: when I perform the
> same test on my mid-2010 iMac (OSX 10.7.5) also with Postgres 9.2.1
> and 16GB RAM, I don't experience the slow down.
> Specifically:
> set work_mem='1MB';
> select ...; // running time is ~1800 ms
> set work_mem='96MB';
> select ...' // running time is ~1500 ms
>
> When I do exactly the same query (the one from my previous post) with
> exactly the same data on the server:
> I get 2100 ms with work_mem=1MB and 3200 ms with 96 MB.
>
> The Mac has SSD so it's understandably faster, but it exhibits a
> behavior I would expect. What am I doing wrong here?
>
> Thanks.
>
> On 30 October 2012 14:08, Petr Praus <petr(at)praus(dot)net
> <mailto:petr(at)praus(dot)net>> wrote:
>
> Hello,
>
> I have a PostgreSQL 9.2 instance running on RHEL 6.3, 8-core
> machine with 16GB of RAM. The server is dedicated to this
> database, the disks are local RAID10. Given that the default
> postgresql.conf is quite conservative regarding memory settings, I
> thought it might be a good idea to allow Postgres to use more
> memory. To my surprise, following advice in the performance tuning
> guide on Postgres wiki[2] significantly slowed down practically
> every query I run but it's more noticeable on the more complex
> queries.
>
> I also tried running pgtune[1] which gave the following
> recommendation with more parameters tuned, but that didn't change
> anything. It suggests shared_buffers of 1/4 of RAM size which
> seems to in line with advice elsewhere (and on PG wiki in particular).
>
> default_statistics_target = 50
> maintenance_work_mem = 960MB
> constraint_exclusion = on
> checkpoint_completion_target = 0.9
> effective_cache_size = 11GB
> work_mem = 96MB
> wal_buffers = 8MB
> checkpoint_segments = 16
> shared_buffers = 3840MB
> max_connections = 80
>
> I tried reindexing the whole database after changing the settings
> (using REINDEX DATABASE), but that didn't help either. I played
> around with shared_buffers and work_mem. Gradually changing them
> from the very conservative default values (128k / 1MB) also
> gradually decreased performance.
>
> I ran EXPLAIN (ANALYZE,BUFFERS) on a few queries and the culprit
> seems to be that Hash Join is significantly slower. It's not clear
> to me why.
>
> To give some specific example, I have the following query. It runs
> in ~2100ms on the default configuration and ~3300ms on the
> configuration with increased buffer sizes:
>
> select count(*) from contest c
> left outer join contestparticipant cp on c.id
> <http://c.id>=cp.contestId
> left outer join teammember tm on tm.contestparticipantid=cp.id
> <http://cp.id>
> left outer join staffmember sm on cp.id
> <http://cp.id>=sm.contestparticipantid
> left outer join person p on p.id <http://p.id>=cp.personid
> left outer join personinfo pi on pi.id
> <http://pi.id>=cp.personinfoid
> where pi.lastname like '%b%' or pi.firstname like '%a%';
>
> EXPLAIN (ANALYZE,BUFFERS) for the query above:
>
> - Default buffers: http://explain.depesz.com/s/xaHJ
> - Bigger buffers: http://explain.depesz.com/s/Plk
>
> The tables don't have anything special in them
>
> The question is why am I observing decreased performance when I
> increase buffer sizes? The machine is definitely not running out
> of memory. Allocation if shared memory in OS is (`shmmax` and
> `shmall`) is set to very large values, that should not be a
> problem. I'm not getting any errors in the Postgres log either.
> I'm running autovacuum in the default configuration but I don't
> expect that has anything to do with it. All queries were run on
> the same machine few seconds apart, just with changed
> configuration (and restarted PG).
>
> I also found a blog post [3] which experiments with various
> work_mem values that run into similar behavior I'm experiencing
> but it doesn't really explain it.
>
> [1]: http://pgfoundry.org/projects/pgtune/
> [2]: http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
> [3]:
> http://www.depesz.com/2011/07/03/understanding-postgresql-conf-work_mem/
>
> Thanks,
> Petr Praus
>
> PS:
> I also posted the question here:
> http://dba.stackexchange.com/questions/27893/increasing-work-mem-and-shared-buffers-on-postgres-9-2-significantly-slows-down
> but a few people suggested
>
>

--

Marcos Luis Ortíz Valmaseda
about.me/marcosortiz <http://about.me/marcosortiz>
@marcosluis2186 <http://twitter.com/marcosluis2186>

10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS...
CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION

http://www.uci.cu
http://www.facebook.com/universidad.uci
http://www.flickr.com/photos/universidad_uci


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Marcos Ortiz <mlortiz(at)uci(dot)cu>
Cc: Petr Praus <petr(at)praus(dot)net>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-01 23:25:16
Message-ID: 509304DC.1090809@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 01.11.2012 21:40, schrieb Marcos Ortiz:
> Regards, Petr.
> Tuning PostgreSQL is not just change the postgresql.conf, it includes
> more things like:
> - the filesystem that you are using
> - the kernel version that you using (particularly in Linux systems)
> - the tuning to kernel variables
> - the type of discs that you are using (SSDs are very fast, like you
> saw in your iMac system)
>
> On 10/30/2012 02:44 PM, Petr Praus wrote:
>> I just found one particularly interesting fact: when I perform the
>> same test on my mid-2010 iMac (OSX 10.7.5) also with Postgres 9.2.1
>> and 16GB RAM, I don't experience the slow down.
>> Specifically:
>> set work_mem='1MB';
>> select ...; // running time is ~1800 ms
>> set work_mem='96MB';
>> select ...' // running time is ~1500 ms
>>
>> When I do exactly the same query (the one from my previous post) with
>> exactly the same data on the server:
>> I get 2100 ms with work_mem=1MB and 3200 ms with 96 MB.
>>
Just some thoughts (interested in this, once seen a Sybase ASE come
close to a halt when we threw a huge lot of SHM at it...).

8 cores, so probably on 2 sockets? What CPU generation?

Both explain outputs show an amount of "read" buffers. Did you warm the
caches before testing?

Maybe you're hitting a NUMA issue there? If those reads come from the
OS' cache, the scheduler might decide to move your process to a
different core (that can access the cache better), then moves it back
when you access the SHM segment more (the ~4GB get allocated at startup,
so probably "close" to the CPU the postmaster ist running on). A
migration to a different cacheline is very expensive.

The temp reads/writes (i.e., the OS cache for the temp files) would
probably be allocated close to the CPU requesting the temp file.

Just groping about in the dark though... but the iMac is obviously not
affected by this, with one socket/memory channel/cache line.

Might be worth to
- manually pin (with taskset) the session you test this in to a
particular CPU (once on each socket) to see if the times change
- try reducing work_mem in the session you're testing in (so you have
large SHM, but small work mem)

Cheers,

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Petr Praus <petr(at)praus(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-02 05:39:59
Message-ID: CAOR=d=3to4+6Ca9u-Sp8Rs4Yp=gMfa0AH+h6e56-pQqJ-tXqEg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Two possibilities:

caching. make sure to run each query several times in a row.

zone reclaim mode. If this has gotten turned on turn it back off.

How to tell:

sysctl -n vm.zone_reclaim_mode

Output should be 0. If it's not, then add this to /etc/sysctl.conf:

vm.zone_reclaim_mode=0

and run: sudo sysctl -p

and see if that helps.


From: Petr Praus <petr(at)praus(dot)net>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-02 14:09:52
Message-ID: CACezXZ9aeCB2dUN7mO2NQk+GoPBXk4d1MorvpYDTDu9W3EWCJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

I did run each query several times, the results I posted are for ~10th run
of the query.

The zone reclaim mode is 0.

On 2 November 2012 00:39, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> wrote:

> Two possibilities:
>
> caching. make sure to run each query several times in a row.
>
> zone reclaim mode. If this has gotten turned on turn it back off.
>
> How to tell:
>
> sysctl -n vm.zone_reclaim_mode
>
> Output should be 0. If it's not, then add this to /etc/sysctl.conf:
>
> vm.zone_reclaim_mode=0
>
> and run: sudo sysctl -p
>
> and see if that helps.
>


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-02 16:12:22
Message-ID: CACezXZ-p1UK0K4EJH8b+EBuiD5HY0vNaQvxrnMcjwvjf9cb+oQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 1 November 2012 18:25, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 01.11.2012 21:40, schrieb Marcos Ortiz:
>
> Regards, Petr.
> Tuning PostgreSQL is not just change the postgresql.conf, it includes more
> things like:
> - the filesystem that you are using
> - the kernel version that you using (particularly in Linux systems)
> - the tuning to kernel variables
> - the type of discs that you are using (SSDs are very fast, like you saw
> in your iMac system)
>
> On 10/30/2012 02:44 PM, Petr Praus wrote:
>
> I just found one particularly interesting fact: when I perform the same
> test on my mid-2010 iMac (OSX 10.7.5) also with Postgres 9.2.1 and 16GB
> RAM, I don't experience the slow down.
> Specifically:
> set work_mem='1MB';
> select ...; // running time is ~1800 ms
> set work_mem='96MB';
> select ...' // running time is ~1500 ms
>
> When I do exactly the same query (the one from my previous post) with
> exactly the same data on the server:
> I get 2100 ms with work_mem=1MB and 3200 ms with 96 MB.
>
> Just some thoughts (interested in this, once seen a Sybase ASE come
> close to a halt when we threw a huge lot of SHM at it...).
>
> 8 cores, so probably on 2 sockets? What CPU generation?
>

The processors are two quad core Intel x7350 Xeon at 2.93Ghz. It's somewhat
older (released late 2007) but it's not absolute speed I'm after - it's the
difference in speed when increasing work_mem.

> Both explain outputs show an amount of "read" buffers. Did you warm the
> caches before testing?
>

I did warm the caches before testing.

>
> Maybe you're hitting a NUMA issue there? If those reads come from the OS'
> cache, the scheduler might decide to move your process to a different core
> (that can access the cache better), then moves it back when you access the
> SHM segment more (the ~4GB get allocated at startup, so probably "close" to
> the CPU the postmaster ist running on). A migration to a different
> cacheline is very expensive.
>
> The temp reads/writes (i.e., the OS cache for the temp files) would
> probably be allocated close to the CPU requesting the temp file.
>
> Just groping about in the dark though... but the iMac is obviously not
> affected by this, with one socket/memory channel/cache line.
>

I made a test with Ubuntu 12.04 VM machine (vmware workstation 4.1.3 on the
same iMac) with 4GB memory and shared_buffers=1GB. To my slight surprise,
the query is faster on Ubuntu VM machine then on the OSX (~1050ms vs.
~1500ms with work_mem=1MB). This might be caused
by effective_io_concurrency which is enabled on Ubuntu but can't be enabled
on OSX because postgres does not support it there. The interesting thing is
that increasing work_mem to 96MB on Ubuntu slows down the query to about
~1250ms from ~1050ms.

>
> Might be worth to
> - manually pin (with taskset) the session you test this in to a particular
> CPU (once on each socket) to see if the times change
>

I tested this and it does not seem to have any effect (assuming I used
taskset correctly but I think so: taskset 02 psql to pin down to CPU #1 and
taskset 01 psql to pin to CPU #0).

> - try reducing work_mem in the session you're testing in (so you have
> large SHM, but small work mem)
>

Did this and it indicates to me that shared_buffers setting actually does
not have an effect on this behaviour as I previously thought it has. It
really boils down to work_mem: when I set shared_buffers to something large
(say 4GB) and just play with work_mem the problem persists.

>
> Cheers,
>
> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> __________________________________________________________________________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Petr Praus <petr(at)praus(dot)net>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-03 10:31:28
Message-ID: 5094F280.6010606@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 02.11.2012 17:12, schrieb Petr Praus:

Your CPUs are indeed pretty oldschool. FSB based, IIRC, not NUMA. A
process migration would be even more expensive there.

> Might be worth to
> - manually pin (with taskset) the session you test this in to a
> particular CPU (once on each socket) to see if the times change
>
>
> I tested this and it does not seem to have any effect (assuming I used
> taskset correctly but I think so: taskset 02 psql to pin down to CPU
> #1 and taskset 01 psql to pin to CPU #0).
Well, that pinned your _client_ to the CPUs, not the server side session ;-)
You'd have to spot for the PID of the new "IDLE" server process and pin
that using "taskset -p". Also, 01 and 02 are probably cores in the same
package/socket. Try "lscpu" first and spot for "NUMA node*" lines at the
bottom.
But anyway... let's try something else first:
>
> - try reducing work_mem in the session you're testing in (so you
> have large SHM, but small work mem)
>
>
> Did this and it indicates to me that shared_buffers setting actually
> does not have an effect on this behaviour as I previously thought it
> has. It really boils down to work_mem: when I set shared_buffers to
> something large (say 4GB) and just play with work_mem the problem
> persists.
This only confirms what we've seen before. As soon as your work_mem
permits an in-memory sort of the intermediate result set (which at that
point in time is where? In the SHM, or in the private memory of the
backend? I can't tell, tbth), the sort takes longer than when it's using
a temp file.

What if you reduce the shared_buffers to your original value and only
increase/decrease the session's work_mem? Same behaviour?

Cheers,

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-03 15:20:59
Message-ID: CACezXZ9SV58CbFM3-MpKGm1z-J0Ho1zERMUBuLH6QhC7RJ5T8Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 3 November 2012 05:31, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 02.11.2012 17:12, schrieb Petr Praus:
>
> Your CPUs are indeed pretty oldschool. FSB based, IIRC, not NUMA. A
> process migration would be even more expensive there.
>
> Might be worth to
>> - manually pin (with taskset) the session you test this in to a
>> particular CPU (once on each socket) to see if the times change
>>
>
> I tested this and it does not seem to have any effect (assuming I used
> taskset correctly but I think so: taskset 02 psql to pin down to CPU #1 and
> taskset 01 psql to pin to CPU #0).
>
> Well, that pinned your _client_ to the CPUs, not the server side session
> ;-)
> You'd have to spot for the PID of the new "IDLE" server process and pin
> that using "taskset -p". Also, 01 and 02 are probably cores in the same
> package/socket. Try "lscpu" first and spot for "NUMA node*" lines at the
> bottom.
>
Ah, stupid me :)

> But anyway... let's try something else first:
>
>
>
>> - try reducing work_mem in the session you're testing in (so you have
>> large SHM, but small work mem)
>>
>
> Did this and it indicates to me that shared_buffers setting actually
> does not have an effect on this behaviour as I previously thought it has.
> It really boils down to work_mem: when I set shared_buffers to something
> large (say 4GB) and just play with work_mem the problem persists.
>
> This only confirms what we've seen before. As soon as your work_mem
> permits an in-memory sort of the intermediate result set (which at that
> point in time is where? In the SHM, or in the private memory of the
> backend? I can't tell, tbth), the sort takes longer than when it's using a
> temp file.
>
> What if you reduce the shared_buffers to your original value and only
> increase/decrease the session's work_mem? Same behaviour?
>

Yes, same behaviour. I let the shared_buffers be the default (which is
8MB). With work_mem 1MB the query runs fast, with 96MB it runs slow (same
times as before). It really seems that the culprit is work_mem.

>
> Cheers,
>
> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> __________________________________________________________________________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Petr Praus <petr(at)praus(dot)net>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-03 17:09:03
Message-ID: 50954FAF.3060905@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 03.11.2012 16:20, schrieb Petr Praus:
>
> Your CPUs are indeed pretty oldschool. FSB based, IIRC, not NUMA.
> A process migration would be even more expensive there.
>

Ok, I've actually looked these up now... at the time these were current,
I was in the lucky situation to only deal with Opterons. And actually,
with these CPUs it is pretty possible that Scott Marlowe's hint (check
vm.zone_reclaim_mode) was pointing in the right direction. Did you check
that?

> Yes, same behaviour. I let the shared_buffers be the default
> (which is 8MB). With work_mem 1MB the query runs fast, with 96MB
> it runs slow (same times as before). It really seems that the
> culprit is work_mem.
>
>

Well, I'm pretty sure that having more work_mem is a good thing (tm)
normally ;-)

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-03 17:19:06
Message-ID: CACezXZ-NBmEd-Ur8TKvi2fhJkLAC=7w4y93ZPPzoFa1rZH+nmg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 3 November 2012 12:09, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 03.11.2012 16:20, schrieb Petr Praus:
>
>
> Your CPUs are indeed pretty oldschool. FSB based, IIRC, not NUMA. A
>> process migration would be even more expensive there.
>>
>>
> Ok, I've actually looked these up now... at the time these were current, I
> was in the lucky situation to only deal with Opterons. And actually, with
> these CPUs it is pretty possible that Scott Marlowe's hint (check
> vm.zone_reclaim_mode) was pointing in the right direction. Did you check
> that?
>

I did check that, it's zero. I responded to his message, but my messages to
the mailing list are getting delayed by ~24 hours because somebody has to
always bless them.

>
>
> Yes, same behaviour. I let the shared_buffers be the default (which
>> is 8MB). With work_mem 1MB the query runs fast, with 96MB it runs slow
>> (same times as before). It really seems that the culprit is work_mem.
>>
>
>
> Well, I'm pretty sure that having more work_mem is a good thing (tm)
> normally ;-)
>

Well, that's what I always thought too! :-)

> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> __________________________________________________________________________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Petr Praus <petr(at)praus(dot)net>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-04 08:48:11
Message-ID: 50962BCB.7020709@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 03.11.2012 18:19, schrieb Petr Praus:
> On 3 November 2012 12:09, Gunnar "Nick" Bluth
> <gunnar(dot)bluth(at)pro-open(dot)de <mailto:gunnar(dot)bluth(at)pro-open(dot)de>> wrote:
>
> Am 03.11.2012 16:20, schrieb Petr Praus:
>>
>> Your CPUs are indeed pretty oldschool. FSB based, IIRC, not
>> NUMA. A process migration would be even more expensive there.
>>
>
> Ok, I've actually looked these up now... at the time these were
> current, I was in the lucky situation to only deal with Opterons.
> And actually, with these CPUs it is pretty possible that Scott
> Marlowe's hint (check vm.zone_reclaim_mode) was pointing in the
> right direction. Did you check that?
>
>
> I did check that, it's zero. I responded to his message, but my
> messages to the mailing list are getting delayed by ~24 hours because
> somebody has to always bless them.
>
>
>
>> Yes, same behaviour. I let the shared_buffers be the default
>> (which is 8MB). With work_mem 1MB the query runs fast, with
>> 96MB it runs slow (same times as before). It really seems
>> that the culprit is work_mem.
>>
>>
>
> Well, I'm pretty sure that having more work_mem is a good thing
> (tm) normally ;-)
>
>
> Well, that's what I always thought too! :-)
>
So, to sum this up (and make someone more competent bite on it maybe
;-), on your SMP, FSB, "fake-multicore" system all "hash"-related works
that potentially switch to different implementations internally (but
w/out telling us so) when given more work_mem are slower.

I'm pretty sure you're hitting some subtle, memory-access-related
cornercase here.

The L2 cache of your X7350 CPUs is 2MB, could you run the tests with,
say, 1, 2, 4 and 8MB of work_mem and post the results?

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Petr Praus <petr(at)praus(dot)net>, Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-05 16:44:34
Message-ID: CAMkU=1wkUvrJGSj7kJq3CqoSBmKr7qtAMsqEPEBCnggwv4Yn2A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On Sat, Nov 3, 2012 at 10:09 AM, Gunnar "Nick" Bluth
<gunnar(dot)bluth(at)pro-open(dot)de> wrote:

> Well, I'm pretty sure that having more work_mem is a good thing (tm)
> normally ;-)

In my experience when doing sorts in isolation, having more work_mem
is a bad thing, unless it enables you to remove a layer of
tape-merging. I always blamed it on the L1/L2 etc. levels of caching.

Cheers,

Jeff


From: Claudio Freire <klaussfreire(at)gmail(dot)com>
To: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
Cc: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>, Petr Praus <petr(at)praus(dot)net>, Marcos Ortiz <mlortiz(at)uci(dot)cu>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-05 16:48:55
Message-ID: CAGTBQpYGwEu__NzmrHXG865CL7thGESDaLp1D6SA6HHYT4r5yQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On Mon, Nov 5, 2012 at 1:44 PM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
>> Well, I'm pretty sure that having more work_mem is a good thing (tm)
>> normally ;-)
>
> In my experience when doing sorts in isolation, having more work_mem
> is a bad thing, unless it enables you to remove a layer of
> tape-merging. I always blamed it on the L1/L2 etc. levels of caching.

Blame it on quicksort, which is quite cache-unfriendly.

Perhaps PG should consider using in-memory mergesort for the bigger chunks.


From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Claudio Freire <klaussfreire(at)gmail(dot)com>
Cc: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>, Petr Praus <petr(at)praus(dot)net>, Marcos Ortiz <mlortiz(at)uci(dot)cu>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-05 17:09:08
Message-ID: CAMkU=1w+oWOvV51pRhikzUL99+WN5BJ234jDYJ1Ut5c5wK4RZQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On Mon, Nov 5, 2012 at 8:48 AM, Claudio Freire <klaussfreire(at)gmail(dot)com> wrote:
> On Mon, Nov 5, 2012 at 1:44 PM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
>>> Well, I'm pretty sure that having more work_mem is a good thing (tm)
>>> normally ;-)
>>
>> In my experience when doing sorts in isolation, having more work_mem
>> is a bad thing, unless it enables you to remove a layer of
>> tape-merging. I always blamed it on the L1/L2 etc. levels of caching.
>
> Blame it on quicksort, which is quite cache-unfriendly.

The observation applies to heap sort. If you can't set work_mem large
enough to do the sort in memory, then you want to set it just barely
large enough to avoid two layers of tape sorting. Any larger than
that reduces performance rather than increasing it. Of course that
assumes you have the luxury of knowing ahead of time exactly how large
your sort will be and can set work_mem accordingly on a case by case
basis, which is unlikely in the real world.

> Perhaps PG should consider using in-memory mergesort for the bigger chunks.

Cheers,

Jeff


From: Claudio Freire <klaussfreire(at)gmail(dot)com>
To: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
Cc: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>, Petr Praus <petr(at)praus(dot)net>, Marcos Ortiz <mlortiz(at)uci(dot)cu>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-05 17:40:31
Message-ID: CAGTBQpbuF0JHdure3N=UKFRLf8WW_hnQOKqCPgOK4UZ1pY1zQg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On Mon, Nov 5, 2012 at 2:09 PM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
>>> In my experience when doing sorts in isolation, having more work_mem
>>> is a bad thing, unless it enables you to remove a layer of
>>> tape-merging. I always blamed it on the L1/L2 etc. levels of caching.
>>
>> Blame it on quicksort, which is quite cache-unfriendly.
>
> The observation applies to heap sort.

Well, heapsort is worse, but quicksort is also quite bad.


From: Claudio Freire <klaussfreire(at)gmail(dot)com>
To: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
Cc: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>, Petr Praus <petr(at)praus(dot)net>, Marcos Ortiz <mlortiz(at)uci(dot)cu>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-05 17:59:12
Message-ID: CAGTBQpY-976TKn1Gx53WSPYsjaWOtuTurPywR7Doasji_6cPXA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On Mon, Nov 5, 2012 at 2:40 PM, Claudio Freire <klaussfreire(at)gmail(dot)com> wrote:
> On Mon, Nov 5, 2012 at 2:09 PM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
>>>> In my experience when doing sorts in isolation, having more work_mem
>>>> is a bad thing, unless it enables you to remove a layer of
>>>> tape-merging. I always blamed it on the L1/L2 etc. levels of caching.
>>>
>>> Blame it on quicksort, which is quite cache-unfriendly.
>>
>> The observation applies to heap sort.
>
> Well, heapsort is worse, but quicksort is also quite bad.

Here[0], an interesting analysis. I really believe quicksort in PG
(due to its more complex datatypes) fares a lot worse.

[0] http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CD0QFjAB&url=http%3A%2F%2Fwww.cs.auckland.ac.nz%2F~mcw%2FTeaching%2Frefs%2Fsorting%2Fladner-lamarca-cach-sorting.pdf&ei=PPqXUMnEL9PaqQHntoDgDQ&usg=AFQjCNE3mDf6ydj1MHUzfQw13TccOa895A


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, klaussfreire(at)gmail(dot)com, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 17:38:41
Message-ID: CACezXZ96RGOOR=vzCOA+Zv-froXSMr9QmJQCJRp_9ndtkszbzQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 4 November 2012 02:48, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 03.11.2012 18:19, schrieb Petr Praus:
>
> On 3 November 2012 12:09, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:
>
>> Am 03.11.2012 16:20, schrieb Petr Praus:
>>
>>
>> Your CPUs are indeed pretty oldschool. FSB based, IIRC, not NUMA. A
>>> process migration would be even more expensive there.
>>>
>>>
>> Ok, I've actually looked these up now... at the time these were current,
>> I was in the lucky situation to only deal with Opterons. And actually, with
>> these CPUs it is pretty possible that Scott Marlowe's hint (check
>> vm.zone_reclaim_mode) was pointing in the right direction. Did you check
>> that?
>>
>
> I did check that, it's zero. I responded to his message, but my messages
> to the mailing list are getting delayed by ~24 hours because somebody has
> to always bless them.
>
>
>>
>>
>> Yes, same behaviour. I let the shared_buffers be the default (which
>>> is 8MB). With work_mem 1MB the query runs fast, with 96MB it runs slow
>>> (same times as before). It really seems that the culprit is work_mem.
>>>
>>
>>
>> Well, I'm pretty sure that having more work_mem is a good thing (tm)
>> normally ;-)
>>
>
> Well, that's what I always thought too! :-)
>
>
> So, to sum this up (and make someone more competent bite on it maybe
> ;-), on your SMP, FSB, "fake-multicore" system all "hash"-related works
> that potentially switch to different implementations internally (but w/out
> telling us so) when given more work_mem are slower.
>
Yes, but note that this happens only in Linux. Increasing work_mem on my
iMac increases performance (but the queries are slower under OSX than on
virtualized Ubuntu on the same machine). Over the weekend, I tried the same
test on my Ubuntu home machine with Ivy Bridge i5 3570K and it also slows
down (from ~900ms with work_mem=1MB to ~1200ms with work_mem=96MB).

>
> I'm pretty sure you're hitting some subtle, memory-access-related
> cornercase here.
>
> The L2 cache of your X7350 CPUs is 2MB, could you run the tests with, say,
> 1, 2, 4 and 8MB of work_mem and post the results?
>
I made a pgbench test with the same query and run it 25 times (5 clients, 5
transactions each):
work_mem speed
1MB 1794ms
2MB 1877ms
4MB 2084ms
8MB 2141ms
10MB 2124ms
12MB 3018ms
16MB 3004ms
32MB 2999ms
64MB 3015ms

It seems that there is some sort of "plateau".

>
> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> __________________________________________________________________________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
Cc: Claudio Freire <klaussfreire(at)gmail(dot)com>, Petr Praus <petr(at)praus(dot)net>, Marcos Ortiz <mlortiz(at)uci(dot)cu>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 19:08:26
Message-ID: 5099602A.8020103@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 05.11.2012 18:09, schrieb Jeff Janes:
> On Mon, Nov 5, 2012 at 8:48 AM, Claudio Freire <klaussfreire(at)gmail(dot)com> wrote:
>> On Mon, Nov 5, 2012 at 1:44 PM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
>>>> Well, I'm pretty sure that having more work_mem is a good thing (tm)
>>>> normally ;-)
>>> In my experience when doing sorts in isolation, having more work_mem
>>> is a bad thing, unless it enables you to remove a layer of
>>> tape-merging. I always blamed it on the L1/L2 etc. levels of caching.
>> Blame it on quicksort, which is quite cache-unfriendly.
> The observation applies to heap sort. If you can't set work_mem large
> enough to do the sort in memory, then you want to set it just barely
> large enough to avoid two layers of tape sorting. Any larger than
> that reduces performance rather than increasing it. Of course that
> assumes you have the luxury of knowing ahead of time exactly how large
> your sort will be and can set work_mem accordingly on a case by case
> basis, which is unlikely in the real world.
>
>> Perhaps PG should consider using in-memory mergesort for the bigger chunks.
I don't want to be the party pooper here, but when you have another look
at the EXPLAINs, you'll realize that there's not a single sort involved.
The expensive parts are HASH, HASH JOIN and HASH RIGHT JOIN (although
the SeqScan takes longer as well, for whatever reason). In those parts,
the difference is clearly in the # of buckets and batches. So to a
degree, PG even does tell us that it uses a different code path (sorry,
PG ;-)...

Greg Smith mentions an optimization wrt. Hash Joins that can become a
pitfall. His advise is to increase the statistic targets on the hashed
outer relation. Might be worth a try.

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Petr Praus <petr(at)praus(dot)net>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, klaussfreire(at)gmail(dot)com, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 19:38:46
Message-ID: 50996746.5010508@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 06.11.2012 18:38, schrieb Petr Praus:
>
> Yes, but note that this happens only in Linux. Increasing work_mem on
> my iMac increases performance (but the queries are slower under OSX
> than on virtualized Ubuntu on the same machine). Over the weekend, I
> tried the same test on my Ubuntu home machine with Ivy Bridge i5 3570K
> and it also slows down (from ~900ms with work_mem=1MB to ~1200ms with
> work_mem=96MB).

OS X is rather different from a memory access point of view, IIRC. So
the direct comparison actually only shows how well the Linux FS cache
works (for the temp files created with small work_mem ;-).

The i5 puzzles me a bit though...

>
> I'm pretty sure you're hitting some subtle, memory-access-related
> cornercase here.
>
> The L2 cache of your X7350 CPUs is 2MB, could you run the tests
> with, say, 1, 2, 4 and 8MB of work_mem and post the results?
>
> I made a pgbench test with the same query and run it 25 times (5
> clients, 5 transactions each):
> work_mem speed
> 1MB 1794ms
> 2MB 1877ms
> 4MB 2084ms
> 8MB 2141ms
> 10MB 2124ms
> 12MB 3018ms
> 16MB 3004ms
> 32MB 2999ms
> 64MB 3015ms
>
> It seems that there is some sort of "plateau".
Two, afaics. The 1->2 change hints towards occasionally breaching your
L2 cache, so it can probably be ignored. The actual plateaus thus seem
to be 0-2, 2-12, >= 12.
It'd be interesting to see the EXPLAIN ANALYSE outputs for these levels,
the buckets and batches in particular. I'd reckon we'll see significant
changes at 2->4 and 10->12MB work_mem.

> So, to sum this up (and make someone more competent bite on it maybe
> ;-), on your SMP, FSB, "fake-multicore" system all "hash"-related
> works that potentially switch to different implementations internally
> (but w/out telling us so) when given more work_mem are slower.
See other post... it actually does tell us (# of buckets/batches).
However, the result is not good and could potentially be improved be
twealing the statistic_targets of the joined tables/columns.

I wonder why noone actually understanding the implementation chipped in
yet... Andres, Greg, Tom, whoever actually understands what's happening
here, anyone reading this? ;-)

Cheers,

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, klaussfreire(at)gmail(dot)com, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 20:08:48
Message-ID: CACezXZ8Tp90V+ahNbkZPZtiSFDZpWnQ+6WoBxoPMXAKbMn+GEQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 6 November 2012 13:38, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 06.11.2012 18:38, schrieb Petr Praus:
>
>
> Yes, but note that this happens only in Linux. Increasing work_mem on my
> iMac increases performance (but the queries are slower under OSX than on
> virtualized Ubuntu on the same machine). Over the weekend, I tried the same
> test on my Ubuntu home machine with Ivy Bridge i5 3570K and it also slows
> down (from ~900ms with work_mem=1MB to ~1200ms with work_mem=96MB).
>
>
> OS X is rather different from a memory access point of view, IIRC. So the
> direct comparison actually only shows how well the Linux FS cache works
> (for the temp files created with small work_mem ;-).
>
> The i5 puzzles me a bit though...
>
>
>
>> I'm pretty sure you're hitting some subtle, memory-access-related
>> cornercase here.
>>
>> The L2 cache of your X7350 CPUs is 2MB, could you run the tests with,
>> say, 1, 2, 4 and 8MB of work_mem and post the results?
>>
> I made a pgbench test with the same query and run it 25 times (5 clients,
> 5 transactions each):
> work_mem speed
> 1MB 1794ms
> 2MB 1877ms
> 4MB 2084ms
> 8MB 2141ms
> 10MB 2124ms
> 12MB 3018ms
> 16MB 3004ms
> 32MB 2999ms
> 64MB 3015ms
>
> It seems that there is some sort of "plateau".
>
> Two, afaics. The 1->2 change hints towards occasionally breaching your L2
> cache, so it can probably be ignored. The actual plateaus thus seem to be
> 0-2, 2-12, >= 12.
> It'd be interesting to see the EXPLAIN ANALYSE outputs for these levels,
> the buckets and batches in particular. I'd reckon we'll see significant
> changes at 2->4 and 10->12MB work_mem.
>

Here are the explains, I run the query a few times before actually taking
the explain to warm up the caches. (I also noticed that explain slows down
the query execution which is probably to be expected.)

2MB: http://explain.depesz.com/s/ul1
4MB: http://explain.depesz.com/s/IlVu
10MB: http://explain.depesz.com/s/afx3
12MB: http://explain.depesz.com/s/i0vQ

So, to sum this up (and make someone more competent bite on it maybe ;-),
> on your SMP, FSB, "fake-multicore" system all "hash"-related works that
> potentially switch to different implementations internally (but w/out
> telling us so) when given more work_mem are slower.
>
> See other post... it actually does tell us (# of buckets/batches).
> However, the result is not good and could potentially be improved be
> twealing the statistic_targets of the joined tables/columns.
>
> I wonder why noone actually understanding the implementation chipped in
> yet... Andres, Greg, Tom, whoever actually understands what's happening
> here, anyone reading this? ;-)
>
> Cheers,
>
> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> __________________________________________________________________________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Petr Praus <petr(at)praus(dot)net>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, klaussfreire(at)gmail(dot)com, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 20:17:59
Message-ID: 50997077.1020500@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 06.11.2012 21:08, schrieb Petr Praus:
>
> 2MB: http://explain.depesz.com/s/ul1
> 4MB: http://explain.depesz.com/s/IlVu
> 10MB: http://explain.depesz.com/s/afx3
> 12MB: http://explain.depesz.com/s/i0vQ
>
See the change in the plan between 10MB and 12MB, directly at top level?
That narrows the thing down quite a bit.

Though I wonder why this didn't show in the original plans...

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, klaussfreire(at)gmail(dot)com, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 20:24:27
Message-ID: CACezXZ9OL7BmngxwcYzGouw-Jz-0AkBQCQLGwOUvvPiHbebiyA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 6 November 2012 14:17, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 06.11.2012 21:08, schrieb Petr Praus:
>
>
>> 2MB: http://explain.depesz.com/s/**ul1 <http://explain.depesz.com/s/ul1>
>> 4MB: http://explain.depesz.com/s/**IlVu<http://explain.depesz.com/s/IlVu>
>> 10MB: http://explain.depesz.com/s/**afx3<http://explain.depesz.com/s/afx3>
>> 12MB: http://explain.depesz.com/s/**i0vQ<http://explain.depesz.com/s/i0vQ>
>>
>> See the change in the plan between 10MB and 12MB, directly at top level?
> That narrows the thing down quite a bit.
>
> Though I wonder why this didn't show in the original plans...

Yes, the 2,4 and 10 are the same, the only difference is number of buckets.
But with 12, it makes completely different choices, it decides to make
sequential scans and hash right joins instead of merge joins. And those
sequential scans take a loong time. Could this be caused by some missing
indices perhaps?

The original plans I posted at the start are the same as the 12MB plan, I'm
not sure why is that, I really hope I didn't make some sort of mistake
there.

Thanks for your help by the way! :-)

>
>
> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> ______________________________**______________________________**
> ______________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: Petr Praus <petr(at)praus(dot)net>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, klaussfreire(at)gmail(dot)com, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-06 20:50:21
Message-ID: 5099780D.8060606@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Am 06.11.2012 21:24, schrieb Petr Praus:
> On 6 November 2012 14:17, Gunnar "Nick" Bluth
> <gunnar(dot)bluth(at)pro-open(dot)de <mailto:gunnar(dot)bluth(at)pro-open(dot)de>> wrote:
>
> Am 06.11.2012 21:08, schrieb Petr Praus:
>
>
> 2MB: http://explain.depesz.com/s/ul1
> 4MB: http://explain.depesz.com/s/IlVu
> 10MB: http://explain.depesz.com/s/afx3
> 12MB: http://explain.depesz.com/s/i0vQ
>
> See the change in the plan between 10MB and 12MB, directly at top
> level? That narrows the thing down quite a bit.
>
> Though I wonder why this didn't show in the original plans...
>
>
> Yes, the 2,4 and 10 are the same, the only difference is number of
> buckets. But with 12, it makes completely different choices, it
> decides to make sequential scans and hash right joins instead of merge
> joins. And those sequential scans take a loong time. Could this be
> caused by some missing indices perhaps?

Well, you do have indices, as we can clearly see.

> The original plans I posted at the start are the same as the 12MB
> plan, I'm not sure why is that, I really hope I didn't make some sort
> of mistake there.

I had been wondering why you didn't have any indices, tbth. However, the
execution times still grow with work_mem, which is interesting
independent of the actual plan change...

>
> Thanks for your help by the way! :-)

Oh, no worries there... this is by far the most interesting challenge
I've encountered in months ;-)

But I do admit that I've reached the end of the ladder now. No idea how
you can improve your runtime yet. Probably
- using full text search on "personinfo"
- try different join_collapse_limit / from_collapse_limit /
enable_hashjoin values

The most pragmatic approach is probably to just stick with work_mem =
1MB (or less) ;-), but that may potentially bite you later.

--
Gunnar "Nick" Bluth
RHCE/SCLA

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
In 1984 mainstream users were choosing VMS over UNIX. Ten years later
they are choosing Windows over UNIX. What part of that message aren't you
getting? - Tom Payne


From: Petr Praus <petr(at)praus(dot)net>
To: Gunnar Nick Bluth <gunnar(dot)bluth(at)pro-open(dot)de>
Cc: Marcos Ortiz <mlortiz(at)uci(dot)cu>, pgsql-performance(at)postgresql(dot)org, Claudio Freire <klaussfreire(at)gmail(dot)com>, jeff(dot)janes(at)gmail(dot)com
Subject: Re: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-08 18:23:03
Message-ID: CACezXZ-ka2RgGQ42NhEmqVXpBjFAw-sKi=qdXoJKsM5URCWnqA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 6 November 2012 14:50, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:

> Am 06.11.2012 21:24, schrieb Petr Praus:
>
> On 6 November 2012 14:17, Gunnar "Nick" Bluth <gunnar(dot)bluth(at)pro-open(dot)de>wrote:
>
>> Am 06.11.2012 21:08, schrieb Petr Praus:
>>
>>
>>> 2MB: http://explain.depesz.com/s/ul1
>>> 4MB: http://explain.depesz.com/s/IlVu
>>> 10MB: http://explain.depesz.com/s/afx3
>>> 12MB: http://explain.depesz.com/s/i0vQ
>>>
>>> See the change in the plan between 10MB and 12MB, directly at top
>> level? That narrows the thing down quite a bit.
>>
>> Though I wonder why this didn't show in the original plans...
>
>
> Yes, the 2,4 and 10 are the same, the only difference is number of
> buckets. But with 12, it makes completely different choices, it decides to
> make sequential scans and hash right joins instead of merge joins. And
> those sequential scans take a loong time. Could this be caused by some
> missing indices perhaps?
>
>
> Well, you do have indices, as we can clearly see.
>
>
> The original plans I posted at the start are the same as the 12MB plan,
> I'm not sure why is that, I really hope I didn't make some sort of mistake
> there.
>
>
> I had been wondering why you didn't have any indices, tbth. However, the
> execution times still grow with work_mem, which is interesting independent
> of the actual plan change...
>
>
>
> Thanks for your help by the way! :-)
>
>
>
> Oh, no worries there... this is by far the most interesting challenge I've
> encountered in months ;-)
>
> But I do admit that I've reached the end of the ladder now. No idea how
> you can improve your runtime yet. Probably
> - using full text search on "personinfo"
> - try different join_collapse_limit / from_collapse_limit /
> enable_hashjoin values
>
> The most pragmatic approach is probably to just stick with work_mem = 1MB
> (or less) ;-), but that may potentially bite you later.
>

Yes, that's what I'm running now in production :) When I have more time I
may come up with more queries to test overall system better.
We'll see if anyone else comes up with something but I am out of things to
try, too. So I guess I'll put this sideways for now.

>
>
> --
> Gunnar "Nick" Bluth
> RHCE/SCLA
>
> Mobil +49 172 8853339
> Email: gunnar(dot)bluth(at)pro-open(dot)de
> __________________________________________________________________________
> In 1984 mainstream users were choosing VMS over UNIX. Ten years later
> they are choosing Windows over UNIX. What part of that message aren't you
> getting? - Tom Payne
>
>


From: Andres Freund <andres(at)2ndquadrant(dot)com>
To: Petr Praus <petr(at)praus(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Increasing work_mem and shared_buffers on Postgres 9.2 significantly slows down queries
Date: 2012-11-09 17:53:45
Message-ID: 20121109175345.GA16999@awork2.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 2012-10-30 14:08:56 -0500, Petr Praus wrote:
> select count(*) from contest c
> left outer join contestparticipant cp on c.id=cp.contestId
> left outer join teammember tm on tm.contestparticipantid=cp.id
> left outer join staffmember sm on cp.id=sm.contestparticipantid
> left outer join person p on p.id=cp.personid
> left outer join personinfo pi on pi.id=cp.personinfoid
> where pi.lastname like '%b%' or pi.firstname like '%a%';

Btw, not really related to the question, but the way you use left joins
here doesn't really make sense and does lead to inferior plans.
As you restrict on 'pi', the rightmost table in a chain of left joins,
there is no point in all those left joins. I would guess the overall
plan is better if use straight joins.

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services