Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 2F445634568 for ; Fri, 28 May 2010 09:22:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 22443-05 for ; Fri, 28 May 2010 12:22:25 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id B8C246341EB for ; Fri, 28 May 2010 09:22:24 -0300 (ADT) Received: by vws9 with SMTP id 9so936148vws.19 for ; Fri, 28 May 2010 05:22:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type; bh=xKmi3n/OqL1NvPS81JndSuS6jWMhXOxBuUQpz0ZkWqk=; b=aKZmrEHetc+35tmidgKlZESGbmXYFXBbnmBq9+U2gK+AVOHOR0fZtj9yhTgBw+0Aen +uv8KHIgEugGx+j3mjTxbwUbjwIpPwLCMwBH5QxGyGHgbpO+eLGpfQ41pqf3U0JMJLh0 ycH60ysZ73reZU0DJRI2clcb2xz7wslzGoz94= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=cb3Lib1RJlLiHI+1cqp7cuT7Lemk7Cd7cBGmMjtvleBjxgyHgItcE7fzKhP7ESP+7N Eo4MekoLRycHNiXu8EMnKabdL3D8UFb5nIWach+4LITSaC+tOmAJPLwqLx3MF4P/usQ9 XBUxzGfkJyrx5EFL0x3dgsg+cGSzIqwgRmJJ8= MIME-Version: 1.0 Received: by 10.220.107.219 with SMTP id c27mr94285vcp.227.1275049342953; Fri, 28 May 2010 05:22:22 -0700 (PDT) Received: by 10.220.87.67 with HTTP; Fri, 28 May 2010 05:22:22 -0700 (PDT) In-Reply-To: References: <20100526161839.GA21875@tamriel.snowman.net> <20100526163251.GB21875@tamriel.snowman.net> Date: Fri, 28 May 2010 08:22:22 -0400 Message-ID: Subject: Re: PostgreSQL Function Language Performance: C vs PL/PGSQL From: Merlin Moncure To: Eliot Gable Cc: Stephen Frost , pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.001 tagged_above=-10 required=5 tests=BAYES_20=-0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201005/253 X-Sequence-Number: 38837 On Wed, May 26, 2010 at 12:41 PM, Eliot Gable wrote: > Ah, that clears things up. Yes, the connections are more or less persistent. > I have a connection manager which doles connections out to the worker > threads and reclaims them when the workers are done with them. It > dynamically adds new connections based on load. Each worker obtains a > connection from the connection manager, performs a transaction which > involves executing the function and pulling back the results from the > cursors, then releases the connection back to the connection manager for > other workers to use. So, this means that even when written in C, the SQL > queries will be planned and cached on each connection after the first > execution. So, I guess the question just becomes whether using SPI in C has > any extra overhead verses using PL/PGSQL which might make it slower for > performing queries. Since PostgreSQL is written in C, I assume there is no > such additional overhead. I assume that the PL/PGSQL implementation at its > heart also uses SPI to perform those executions. Is that a fair statement? At best, if you are a ninja with the marginally documented backend api, you will create code that goes about as fast as your pl/pgsql function for 10 times the amount of input work, unless there are heavy amounts of 'other than sql' code in your function. The reason to write C in the backend is: *) Interface w/3rd party libraries w/C linkage *) Do things that are illegal in regular SQL (write files, etc) *) Make custom types Things like that. If your pl/pgsql function is running slow, it's probably better to look at what's going on there. merlin