Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4D128B5DBC7 for ; Thu, 6 Oct 2011 14:59:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 53168-06 for ; Thu, 6 Oct 2011 17:59:37 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-yw0-f46.google.com (mail-yw0-f46.google.com [209.85.213.46]) by mail.postgresql.org (Postfix) with ESMTP id 2E086B5DBC8 for ; Thu, 6 Oct 2011 14:59:37 -0300 (ADT) Received: by ywb5 with SMTP id 5so2855263ywb.19 for ; Thu, 06 Oct 2011 10:59:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=3CuM/IX7fkYWsrWE8E74F9bqWPDg7ztAV9Hkp1+Hbso=; b=STZ2zq2ZVpwGsAvj8H4khPWE3xdPsR+ktGXJ8QrZz/FFPyx0Vu9AcNoklHlgvZ33EU i1TwiJJYxinDwkkO7rnLg4EVUXmvyjz8c1i0jXkA7HFg63xo1nA1RhxfcwDdNokMqAmM HEGh01ODm0d3uanTDuqcS0dtNgROI2Srl1rWs= Received: by 10.236.192.197 with SMTP id i45mr4963233yhn.128.1317923976477; Thu, 06 Oct 2011 10:59:36 -0700 (PDT) Received: from [10.1.0.104] ([64.9.30.2]) by mx.google.com with ESMTPS id h20sm16751552ani.16.2011.10.06.10.59.34 (version=SSLv3 cipher=OTHER); Thu, 06 Oct 2011 10:59:34 -0700 (PDT) Message-ID: <4E8DEC80.3010706@gmail.com> Date: Thu, 06 Oct 2011 13:59:28 -0400 From: Steve Northamer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.16) Gecko/20101125 Lightning/1.0b1 Thunderbird/3.0.11 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: plpgsql function executed multiple times for each return value Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.889 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201110/4 X-Sequence-Number: 35888 We have a plpgsql function called paymentcalc, which calculates the payment necessary to pay off a loan. It's defined like this: CREATE OR REPLACE FUNCTION paymentcalc(IN amount numeric, IN interestrate numeric, IN termmonths integer, IN paymentfreq integer, IN dueday1 integer, IN dueday2 integer, IN borrowdate date, IN firstdue date, IN gapins character, IN lifeins character, IN disabilityins character, OUT payment numeric, OUT finalpayment numeric, OUT finaldue date) RETURNS record AS ... LANGUAGE 'plpgsql' STABLE; We want to execute this function, with inputs from a table, and return the calculated values as separate columns: select (p).payment, (p).finalpayment, (p).finaldue from (select paymentcalc(amount, interestrate / 100, termmonths, paymentfreq, dueday1, dueday2, borrowdate, firstdue, gapins, lifeins, disins) as p from apps where id = 100) s This works, but using "RAISE NOTICE" we've determined that this executes the paymentcalc function 3 times! It seems to execute the function once for each return value. The function is time consuming, and we want to execute it only once. Thinking that maybe a table returning function would work better, we tried this: CREATE OR REPLACE FUNCTION paymentcalc2(IN amount numeric, IN interestrate numeric, IN termmonths integer, IN paymentfreq integer, IN dueday1 integer, IN dueday2 integer, IN borrowdate date, IN firstdue date, IN gapins character, IN lifeins character, IN disabilityins character) RETURNS SETOF paymentcalc_return AS ... LANGUAGE 'plpgsql' STABLE; However, we now have difficulty trying to send parameters from a table as inputs to this function. We want to do something like: select * from paymentcalc2(amount, interestrate / 100, termmonths, paymentfreq, dueday1, dueday2, borrowdate, firstdue, gapins, lifeins, disins) from apps where id = 100 But obviously this query won't work with two "from"s. We can't do a join between paymentcalc2 and apps, because we get "invalid reference to FROM-clause entry for table apps". So my questions are: 1) How do we cause the paymentcalc function to be executed only once? and 2) How do we call a table returning function with inputs from a table? Thank you very much! Steve