Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZD2i3-0002xe-I3 for pgsql-performance@arkaria.postgresql.org; Thu, 09 Jul 2015 03:41:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZD2i2-0005BO-UR for pgsql-performance@arkaria.postgresql.org; Thu, 09 Jul 2015 03:41:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZD2fr-0003IG-0n for pgsql-performance@postgresql.org; Thu, 09 Jul 2015 03:38:59 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZD2fj-0004tO-QJ for pgsql-performance@postgresql.org; Thu, 09 Jul 2015 03:38:58 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t693ccTO015703; Wed, 8 Jul 2015 23:38:39 -0400 From: Tom Lane To: andres@anarazel.de (Andres Freund) cc: Craig James , Merlin Moncure , "Joshua D. Drake" , "Graeme B. Bell" , postgres performance list Subject: Re: Hmmm... why does CPU-intensive pl/pgsql code parallelise so badly when queries parallelise fine? Anyone else seen this? In-reply-to: <20150708224518.GT10242@alap3.anarazel.de> References: <6E55113B-FA05-433C-9B21-623F798EE935@skogoglandskap.no> <559CB5BF.2080509@commandprompt.com> <20150708202733.GS10242@alap3.anarazel.de> <20150708224518.GT10242@alap3.anarazel.de> Comments: In-reply-to andres@anarazel.de (Andres Freund) message dated "Thu, 09 Jul 2015 00:45:18 +0200" Date: Wed, 08 Jul 2015 23:38:38 -0400 Message-ID: <15702.1436413118@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org andres@anarazel.de (Andres Freund) writes: > On 2015-07-08 15:38:24 -0700, Craig James wrote: >> From my admittedly naive point of view, it's hard to see why any of this >> matters. I have functions that do purely CPU-intensive mathematical >> calculations ... you could imagine something like is_prime(N) that >> determines if N is a prime number. I have eight clients that connect to >> eight backends. Each client issues an SQL command like, "select >> is_prime(N)" where N is a simple number. > I mostly replied to Merlin's general point (additionally in the context of > plpgsql). > But I have a hard time seing that postgres would be the bottleneck for a > is_prime() function (or something with similar characteristics) that's > written in C where the average runtime is more than, say, a couple > thousand cyles. I'd like to see a profile of that. But that was not the case that Graeme was complaining about. He's talking about simple-arithmetic-and-looping written in plpgsql, in a volatile function that is going to take a new snapshot for every statement, even if that's only "n := n+1". So it's going to spend a substantial fraction of its runtime banging on the ProcArray, and that doesn't scale. If you write your is_prime function purely in plpgsql, and don't bother to mark it nonvolatile, *it will not scale*. It'll be slow even in single-thread terms, but it'll be particularly bad if you're saturating a multicore machine with it. One of my Salesforce colleagues has been looking into ways that we could decide to skip the per-statement snapshot acquisition even in volatile functions, if we could be sure that a particular statement isn't going to do anything that would need a snapshot. Now, IMO that doesn't really do much for properly written plpgsql; but there's an awful lot of bad plpgsql code out there, and it can make a huge difference for that. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance