pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedMassive memory use for star query
10+ messages / 4 participants
[nested] [flat]
* Massive memory use for star query
@ 2011-04-15 04:35 Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
0 siblings, 3 replies; 10+ messages in thread
From: Mark Kirkwood @ 2011-04-15 04:35 UTC (permalink / raw)
To: pgsql-bugs
I've recently seen examples of star-like queries using vast amounts of
memory in one of our production systems. Here's a simplified example
using synthetic data (see attached to generate if desired):
SET geqo_threshold = 14;
SET from_collapse_limit = 14;
SET join_collapse_limit = 14;
EXPLAIN
SELECT
1
FROM node n
JOIN nodekeyword kwn0 ON (n.nodeid = kwn0.nodeid)
JOIN keyword kw0 ON (kwn0.keywordid = kw0.keywordid)
JOIN nodekeyword kwn1 ON (n.nodeid = kwn1.nodeid)
JOIN keyword kw1 ON (kwn1.keywordid = kw1.keywordid)
JOIN nodekeyword kwn2 ON (n.nodeid = kwn2.nodeid)
JOIN keyword kw2 ON (kwn2.keywordid = kw2.keywordid)
JOIN nodekeyword kwn3 ON (n.nodeid = kwn3.nodeid)
JOIN keyword kw3 ON (kwn3.keywordid = kw3.keywordid)
JOIN nodekeyword kwn4 ON (n.nodeid = kwn4.nodeid)
JOIN keyword kw4 ON (kwn4.keywordid = kw4.keywordid)
JOIN nodekeyword kwn5 ON (n.nodeid = kwn5.nodeid)
JOIN keyword kw5 ON (kwn5.keywordid = kw5.keywordid)
WHERE kw0.keyword = 'sscghryv'
AND kw1.keyword = 'sscghryv'
AND kw2.keyword = 'sscghryv'
AND kw3.keyword = 'sscghryv'
AND kw4.keyword = 'sscghryv'
AND kw5.keyword = 'sscghryv'
;
Here's what a ps listing looks like:
VSZ RSS SZ CMD
1849524 1793680 1791144 postgres: postgres test [local] EXPLAIN
So we are using 1.7G doing an *EXPLAIN* - so presumably this is gonna be
the join search planning getting expensive for 13 tables. Is it expected
that this much memory could/would be used? Could this be evidence of a
leak?
Note this is a default 9.1 (2011-04-07) build w/o asserts, with a
default postgresql.conf.
Clearly this particular query is a bit dumb, making the keyword
predicates have different values results in much better behaved planning
memory usage... and also allowing geqo to do the join search for us
prevents the high memory use (however geqo has its own problems.... in
the production variant of this query *one* of the plans it would pick
liked to use >100G of temp space to execute...and there are only 100G
available...sigh). However for these semi ad-hoc systems it is hard to
prevent dumb queries altogether!
regards
Mark
Attachments:
starjoin.tar.gz (application/x-gzip, 1.8K) download
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-15 13:59 Kevin Grittner <Kevin.Grittner@wicourts.gov>
parent: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2 siblings, 2 replies; 10+ messages in thread
From: Kevin Grittner @ 2011-04-15 13:59 UTC (permalink / raw)
To: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>; pgsql-bugs
Mark Kirkwood <mark.kirkwood@catalyst.net.nz> wrote:
> Here's a simplified example using synthetic data (see attached to
> generate if desired):
Doesn't work for me:
kgrittn@kgrittn-desktop:~/work/starjoin$ ./gendata.pl
generate cat
cannot open cat.dat: No such file or directory at ./gendata.pl line
17.
> Here's what a ps listing looks like:
>
> VSZ RSS SZ CMD
> 1849524 1793680 1791144 postgres: postgres test [local] EXPLAIN
If you run pmap -d on the pid, what does the last line look like?
-Kevin
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-15 15:14 Kevin Grittner <Kevin.Grittner@wicourts.gov>
parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
1 sibling, 0 replies; 10+ messages in thread
From: Kevin Grittner @ 2011-04-15 15:14 UTC (permalink / raw)
To: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>; pgsql-bugs
"Kevin Grittner" <Kevin.Grittner@wicourts.gov> wrote:
> Mark Kirkwood <mark.kirkwood@catalyst.net.nz> wrote:
>
>> Here's a simplified example using synthetic data (see attached to
>> generate if desired):
>
> Doesn't work for me:
Edited scripts to change hard-coded directory. Issue confirmed.
> If you run pmap -d on the pid, what does the last line look like?
$ pmap -d 5869|grep '^mapped' ; ps aux|grep '^kgrittn 5869 '
mapped: 1132044K writeable/private: 1084480K shared: 38436K
kgrittn 5869 101 35.4 1139664 1094784 ? Rs 10:01 0:20
postgres: kgrittn test [local] EXPLAIN
This wasn't necessarily at the peak. Attached is `vmstat 1` output
during the EXPLAIN. About 1.1 GB private writeable memory consumed
on my HEAD build on kubuntu 32 bit.
-Kevin
Attachments:
vmstat-during-explain.txt (text/plain, 2.6K) download
kgrittn@kgrittn-desktop:~/work$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 204500 229800 10520 2135820 1 1 11 3 2 10 3 1 95 0
0 0 204500 229668 10520 2135764 0 0 0 0 339 930 0 0 99 0
0 0 204500 225700 10520 2140276 0 0 0 0 400 1083 3 5 92 0
0 0 204500 225700 10520 2140376 0 0 0 0 280 769 1 1 98 0
1 0 204500 213416 10532 2137572 0 0 1452 452 594 613 18 1 72 9
8 0 204496 170380 10532 2138260 0 0 0 0 854 2013 55 7 37 0
1 0 204496 122268 10532 2142536 0 0 0 16 914 1420 50 6 43 0
2 0 205100 82712 10532 2135580 0 604 0 604 1029 458 48 3 50 0
1 0 206032 82208 10532 2090832 32 936 32 936 6430 545 50 6 45 1
2 0 206348 83324 10544 2041104 0 316 0 772 1442 1151 48 4 49 0
1 0 206808 82332 10544 1992428 0 460 0 460 1129 821 47 4 48 0
1 0 206852 83200 10548 1941136 0 44 4 160 1499 782 48 3 49 1
1 0 206908 81340 10548 1891868 0 56 0 56 1555 1082 49 3 48 0
1 0 206912 84068 10532 1837684 0 4 144 4 1515 967 49 3 45 3
1 0 206904 82936 10516 1784644 64 0 504 0 1629 960 48 12 35 6
1 1 206900 82208 10492 1731108 32 0 1092 292 1736 1077 49 4 37 9
2 0 206900 83912 10372 1673624 0 0 244 4 1977 1239 50 7 40 2
2 0 206900 81836 10128 1620852 0 0 0 0 902 863 47 5 48 0
1 0 206912 82332 9084 1563548 0 12 0 12 2364 457 46 6 47 0
1 0 207276 83200 8576 1507156 0 364 0 364 2466 541 48 4 48 0
2 0 207280 82332 8420 1447548 0 4 0 440 2424 959 47 8 45 0
1 0 207344 83820 8332 1384576 0 64 0 64 2342 1391 50 8 42 0
1 0 207344 82828 8272 1323772 0 0 16 0 2279 615 48 5 45 1
1 0 207344 83820 8244 1259092 0 0 56 0 1697 668 46 8 44 3
1 0 207344 81696 8268 1195708 76 0 512 0 1713 655 47 10 28 15
1 0 207408 81340 8252 1158556 0 64 388 520 764 514 46 2 50 2
0 0 207408 1214204 8396 1158640 0 0 252 140 878 710 31 16 46 9
0 0 207408 1214344 8400 1158948 0 0 16 0 404 567 1 3 94 1
0 0 207408 1214344 8400 1158928 0 0 0 0 268 470 0 0 99 0
0 0 207408 1214344 8400 1158912 0 0 0 0 356 458 0 0 100 0
0 1 207408 1214344 8408 1158960 0 0 8 0 256 482 0 0 99 0
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-15 16:43 Tom Lane <tgl@sss.pgh.pa.us>
parent: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2 siblings, 1 reply; 10+ messages in thread
From: Tom Lane @ 2011-04-15 16:43 UTC (permalink / raw)
To: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>; +Cc: pgsql-bugs
Mark Kirkwood <mark.kirkwood@catalyst.net.nz> writes:
> I've recently seen examples of star-like queries using vast amounts of
> memory in one of our production systems. Here's a simplified example
> using synthetic data (see attached to generate if desired):
> SET geqo_threshold = 14;
> SET from_collapse_limit = 14;
> SET join_collapse_limit = 14;
Well, if you're going to do the above, you should be expecting the
planner to eat a lot of memory. There is a reason why the default
values of those parameters are significantly lower than that ...
regards, tom lane
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-16 07:17 Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
1 sibling, 0 replies; 10+ messages in thread
From: Mark Kirkwood @ 2011-04-16 07:17 UTC (permalink / raw)
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: pgsql-bugs
On 16/04/11 01:59, Kevin Grittner wrote:
> Mark Kirkwood<mark.kirkwood@catalyst.net.nz> wrote:
>
>> Here's a simplified example using synthetic data (see attached to
>> generate if desired):
>
> Doesn't work for me:
>
> kgrittn@kgrittn-desktop:~/work/starjoin$ ./gendata.pl
> generate cat
> cannot open cat.dat: No such file or directory at ./gendata.pl line
> 17.
Apologies Kevin, I stuffed up the edit to supposedly make it easier for
you all to choose your own place to write the files (left one hard coded
for the table 'cat').
Cheers
Mark
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-16 07:21 Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 10+ messages in thread
From: Mark Kirkwood @ 2011-04-16 07:21 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On 16/04/11 04:43, Tom Lane wrote:
> Mark Kirkwood<mark.kirkwood@catalyst.net.nz> writes:
>> I've recently seen examples of star-like queries using vast amounts of
>> memory in one of our production systems. Here's a simplified example
>> using synthetic data (see attached to generate if desired):
>> SET geqo_threshold = 14;
>> SET from_collapse_limit = 14;
>> SET join_collapse_limit = 14;
> Well, if you're going to do the above, you should be expecting the
> planner to eat a lot of memory. There is a reason why the default
> values of those parameters are significantly lower than that ...
>
Ok - so with the settings at their defaults geqo chooses a semi-random
plan, and at least one of those (for the production variant of this
query anyway) eat massive (>100G) amounts of temp space - not really a
suitable outcome either.
I guess you have answered my first question - i.e yes this should eat
massive amount of ram as written - however are you sure there is no
memory leaking going on here?
regards
Mark
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-16 11:08 Greg Stark <gsstark@mit.edu>
parent: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
0 siblings, 1 reply; 10+ messages in thread
From: Greg Stark @ 2011-04-16 11:08 UTC (permalink / raw)
To: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs
On Sat, Apr 16, 2011 at 8:21 AM, Mark Kirkwood
<mark.kirkwood@catalyst.net.nz> wrote:
>
> I guess you have answered my first question - i.e yes this should eat
> massive amount of ram as written - however are you sure there is no memory
> leaking going on here?
The planner doesn't try to free up memory while it's working, it
generally assumes that producing a plan is a short process and when
it's done it'll free the whole context and that's enough.
The basic problem is that the number of possible plans blows up
combinatorically. That is with 14 tables there are 14! possible join
orderings and more something like 3^(14!) possible join strategies --
actually more if you include things like whether to materialize and
which keys to use and so on.
The planner uses various heuristics to avoid combinatoric growth
wherever it can but there's no way to completely avoid it.
--
greg
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-16 14:58 Tom Lane <tgl@sss.pgh.pa.us>
parent: Greg Stark <gsstark@mit.edu>
0 siblings, 1 reply; 10+ messages in thread
From: Tom Lane @ 2011-04-16 14:58 UTC (permalink / raw)
To: Greg Stark <gsstark@mit.edu>; +Cc: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>; pgsql-bugs
Greg Stark <gsstark@mit.edu> writes:
> On Sat, Apr 16, 2011 at 8:21 AM, Mark Kirkwood
> <mark.kirkwood@catalyst.net.nz> wrote:
>> I guess you have answered my first question - i.e yes this should eat
>> massive amount of ram as written - however are you sure there is no memory
>> leaking going on here?
> The planner uses various heuristics to avoid combinatoric growth
> wherever it can but there's no way to completely avoid it.
Yeah. The collapse_limit variables can be seen as another heuristic to
deal with this type of problem: they artificially limit the number of
combinations considered by forcing the join search to be broken down
into subproblems. The trouble of course is that this breakdown is
pretty stupid and can easily prevent the best join order from ever being
considered.
If you've got a small number of such query types that you can afford to
spend some manual effort on, here's what I'd do:
1. With those three planner variables cranked up to more than the number
of relations in the query (if possible), run an EXPLAIN, or better
EXPLAIN ANALYZE so you can confirm you get a good plan.
2. Observe the join order selected in the good plan.
3. Rearrange your query so that the tables are explicitly JOINed in that
order. Don't use the FROM-comma-list style.
4. Now, in your production app, *reduce* join_collapse_limit to a small
value, maybe even 1, to force the syntactic JOIN order to be followed.
(Obviously, don't keep it there when running queries you haven't
hand-optimized this way.)
This will force the planner to consider only small subproblems, which
will make it both much faster and much less memory-hungry than when it's
trying to solve a large join problem from scratch.
regards, tom lane
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-16 23:18 Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
parent: Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2 siblings, 0 replies; 10+ messages in thread
From: Mark Kirkwood @ 2011-04-16 23:18 UTC (permalink / raw)
To: pgsql-bugs
On 15/04/11 16:35, Mark Kirkwood wrote:
> Here's a simplified example using synthetic data (see attached to
> generate if desired):
>
For anyone else who might be want to play with this:
Patch with correction to make the directory reassignment work correctly,
plus an additional comment in the README mentioning the need to set this
in the generator and loading scripts.
Thanks
Mark
Attachments:
starjoin-dirs.patch (text/x-patch, 951B) download
diff -c starjoin.orig/gendata.pl starjoin/gendata.pl
*** starjoin.orig/gendata.pl 2011-04-17 10:55:28.252595067 +1200
--- starjoin/gendata.pl 2011-04-17 10:55:48.684595066 +1200
***************
*** 14,20 ****
my %prevcatids;
print "generate cat\n";
! open(FH, "> /data0/dump/cat.dat") || die ("cannot open cat.dat: $!");
for ($i = 0; $i < $catrows; $i++) {
$catid = $i;
--- 14,20 ----
my %prevcatids;
print "generate cat\n";
! open(FH, "> $dir/cat.dat") || die ("cannot open cat.dat: $!");
for ($i = 0; $i < $catrows; $i++) {
$catid = $i;
diff -c starjoin.orig/README starjoin/README
*** starjoin.orig/README 2011-04-17 10:55:28.252595067 +1200
--- starjoin/README 2011-04-17 10:56:29.292595065 +1200
***************
*** 1,5 ****
--- 1,6 ----
Setup for (hopefully) excessive join memory use.
+ $ vi gendata.pl load.sql # set dump/load directory
$ ./gendata.pl
$ psql test < schema.sql
$ psql test < load.sql
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Massive memory use for star query
@ 2011-04-16 23:54 Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 10+ messages in thread
From: Mark Kirkwood @ 2011-04-16 23:54 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Greg Stark <gsstark@mit.edu>; pgsql-bugs
On 17/04/11 02:58, Tom Lane wrote:
> Greg Stark<gsstark@mit.edu> writes:
>> The planner uses various heuristics to avoid combinatoric growth
>> wherever it can but there's no way to completely avoid it.
> Yeah. The collapse_limit variables can be seen as another heuristic to
> deal with this type of problem: they artificially limit the number of
> combinations considered by forcing the join search to be broken down
> into subproblems. The trouble of course is that this breakdown is
> pretty stupid and can easily prevent the best join order from ever being
> considered.
>
> If you've got a small number of such query types that you can afford to
> spend some manual effort on, here's what I'd do:
>
> 1. With those three planner variables cranked up to more than the number
> of relations in the query (if possible), run an EXPLAIN, or better
> EXPLAIN ANALYZE so you can confirm you get a good plan.
>
> 2. Observe the join order selected in the good plan.
>
> 3. Rearrange your query so that the tables are explicitly JOINed in that
> order. Don't use the FROM-comma-list style.
>
> 4. Now, in your production app, *reduce* join_collapse_limit to a small
> value, maybe even 1, to force the syntactic JOIN order to be followed.
> (Obviously, don't keep it there when running queries you haven't
> hand-optimized this way.)
>
> This will force the planner to consider only small subproblems, which
> will make it both much faster and much less memory-hungry than when it's
> trying to solve a large join problem from scratch.
>
We've sort of done an equivalent thing as a temporary fix - restricted
the page generating these queries to one or two keywords to tame the
number of tables joined in.
We are only seeing this type of query being generated in a very specific
part of the application (keyword search), and I've been encouraging a
redesign in that area anyway as I don't believe it is necessary to
require so many joins to achieve what they wish to do - so this is
really the clincher for a redesign.
I will get 'em to reduce the *collapse limits too.
Thanks to all of you for your help, regards
Mark
^ permalink raw reply [nested|flat] 10+ messages in thread
end of thread, other threads:[~2011-04-16 23:54 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-15 04:35 Massive memory use for star query Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2011-04-15 13:59 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-04-15 15:14 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-04-16 07:17 ` Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2011-04-15 16:43 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-04-16 07:21 ` Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2011-04-16 11:08 ` Greg Stark <gsstark@mit.edu>
2011-04-16 14:58 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-04-16 23:54 ` Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
2011-04-16 23:18 ` Mark Kirkwood <mark.kirkwood@catalyst.net.nz>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox