Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E65796331C0 for ; Tue, 2 Feb 2010 06:13:28 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 37946-01-10 for ; Tue, 2 Feb 2010 10:13:11 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f222.google.com (mail-ew0-f222.google.com [209.85.219.222]) by mail.postgresql.org (Postfix) with ESMTP id C648B634621 for ; Tue, 2 Feb 2010 06:06:04 -0400 (AST) Received: by ewy22 with SMTP id 22so590950ewy.34 for ; Tue, 02 Feb 2010 02:06:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=GVGBYNUr/aq8rur/7tzNu/W4Az9lNDU7Bh7SsJ8ZAuU=; b=ExTp7kIRNHHMkO5k3LyKa8VNyeArXf2rRCfn1F/m3HRo5ZXFqINIgkvdK1o+Rur+TE emoaox5SnBfdzvV3ZWaLvPRjMzdBplG/4+kX3rD7hkXsjSY5C0LP39i6ljtl5jIx6INd rsjgyQM7qxM9EbDiRUOyMdOxZpp+cYuQJEIYQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=n5+0/aQ6PzZ5cR3A+0h2hyKWJXvAEEtdCNC2u0o/Hy1ZXGeLW38HYX4H5IODw7F1XE LkEtfOYo9K9p8av8vivn4KstqTlGEAvPDeegGVpdrQ9ba2254EbCk9t9L7h2s/9TGn8p lENtAg3HmBBqMALyvwNmIA7J0iDjz9xu+MGlU= Received: by 10.213.41.1 with SMTP id m1mr507803ebe.1.1265105162812; Tue, 02 Feb 2010 02:06:02 -0800 (PST) Received: from ?0.0.0.0? (5357DA75.cable.casema.nl [83.87.218.117]) by mx.google.com with ESMTPS id 13sm4355387ewy.9.2010.02.02.02.06.01 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 02 Feb 2010 02:06:02 -0800 (PST) Message-ID: <4B67F908.1060507@gmail.com> Date: Tue, 02 Feb 2010 11:06:00 +0100 From: Yeb Havinga User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: Glenn Maynard CC: pgsql-performance@postgresql.org Subject: Re: Slow query: table iteration (8.3) References: <4B66B7E0.3020605@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.67 tagged_above=-10 required=5 tests=AWL=0.930, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/10 X-Sequence-Number: 37431 Glenn Maynard wrote: > On Mon, Feb 1, 2010 at 6:15 AM, Yeb Havinga wrote: > >> Stomp_steps is analyzed to 2902 rows but when you run the query the actual >> rows are 0. This means that the highscore function is not called or the >> number 0 is incorrect. >> > This SELECT returns 0 rows: it calls the function 1500 times, and each > time it returns no data, because there simply aren't any results for > these parameters. > Hmm.. first posting on a pg mailing list and I make this mistake.. What an introduction :-[ Checked the source and indeed for every plan node the number of tuples that result from it are counted. In most cases this is the number of records that match the qualifiers (where clause/join conditions) so that was in my head: actual rows = rows that match where, and without where I'd expected the actual rows to reflect the total number of rows in the table. But with a set returning functions this number is something completely different. >> below. The truth might be that you probably got that result by explaining >> the query in the function with actual parameter values. This plan differs >> from the one that is made when the function is called from sql and is >> planned (once) without parameters, and in that case the plan is probably >> different. >> > > Yeah. It would help a lot if EXPLAIN could show query plans of > functions used by the statement and not just the top-level query. > Like subplans are, yes. Sounds like a great future. > Squinting at the output, it definitely looks like a less optimized > plan; it's using a SEQSCAN instead of BITMAPHEAPSCAN. (I've attached > the output.) > > Does the planner not optimize functions based on context? I believe it does for (re) binding of parameter values to prepared statements, but not in the case of an sql function. To test an idea, there might be a workaround where you could write a pl/pgsql function that makes a string with the query and actual parameter values and executes that new query everytime. It's not as pretty as a sql function, but would give an idea of how fast things would run with each loop replanned. Another idea is that maybe you could 'hint' the planner at planning time of the sql function by giving it some extra set commands (like set search_path but then set enable_seqscan = off) - I don't know if planning of the sql function occurs in the environment given by it's set commands, but its worth a try. Again, certainly not pretty. > I can't see how to apply WITH to this. Non-recursive WITH seems like > syntax sugar that doesn't do anything a plain SELECT can't do, and I > don't think what I'm doing here can be done with a regular SELECT. > With indeed is not a solution because the with query is executed once, so it cannot take a parameter. What about a window function on a join of stomp_steps and stomp_round with partition by on steps_id and user_card is and order by score and with row_number() < your third parameter. From the docs I read that window functions cannot be part of the where clause: an extra subselect leven is needed then to filter the correct row numbers. Regards, Yeb Havinga