Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hVfsH-0000E5-5r for pgsql-general@arkaria.postgresql.org; Tue, 28 May 2019 17:26:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hVfsE-0005Wu-4k for pgsql-general@arkaria.postgresql.org; Tue, 28 May 2019 17:26:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hVfsC-0005Vc-84 for pgsql-general@lists.postgresql.org; Tue, 28 May 2019 17:26:53 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hVfs4-0000Jp-8B for pgsql-general@postgresql.org; Tue, 28 May 2019 17:26:50 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2DD13222AF; Tue, 28 May 2019 13:26:43 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 28 May 2019 13:26:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=y 1gRFv9w2ear+DG3OE4L8OD2IXqmOtcBr3HhmaO+4bw=; b=bDNUJBeEqO0a/tH81 n8h7RJK3vb1Fmft4YmmNuYss9fo7cjeXLkUpGCqwosC2s/WjUKaKSmPSk/dHLhpp Mugbt2z5fp3PRuXEKFxe+4Rn3MKp0r9qIInnlL7g3Xsxy+ikI0NNyPmgstWLLdjA XtMYIJb3TZ923B2FZmd87NyWg7IjtRCq/TJzFPVwoUKh967cGh8PCPcljNR9SZ+w eIyRojE1AWcPaDgkdRNzUw/1wwEXLlyDsdhnBMckheUWRaHlye8l+8Am0JGP/Kvo t4PJX7U3EJP75x91vJhGFocOl8BBebvsxPzXJBr2RCpJqUlMMgqKcHm+bidsuHtQ 4nnxg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=y1gRFv9w2ear+DG3OE4L8OD2IXqmOtcBr3HhmaO+4 bw=; b=7hOZ9mG8LtpygcgBecNk4IMgfCSHdrFjWAZBm0vv2n8kemw89MW0QjqzI ftVmEUXhKsr6jGjc39lrU2FSzVV8EfVGZF4JawSXm1GbOhn2Q8m1jmZUUq0Inrwf ahtZGLXDx/STF7dq/UQTLoXtY7JTxdAOA7eZguGhlBEXW/R+Ql7puoC6xuYOJdVN Z17azVZRMkt2cuTHfi/SsnZqHwgLOh8d83KSzmilQgkfHG3WgenOOeTKCXprINb4 huFE1YCisUremhBxOYW3DaLGe3vxgY/4j3yIlpZ3fXeZ7/QvZLekwbUp4k8D5Npo Ypdk/+KIyt5IUwCwa70Yi9E5O7/mA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddvhedgudduvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpegsrdhiugdpphhoshhtghhrvghsqhhlrdhorhhgpdgrrdhi ugenucfkphepjeegrdelgedrjeefrddvvddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfu ihiivgeptd X-ME-Proxy: Received: from ranger.suse (unknown [74.94.73.222]) by mail.messagingengine.com (Postfix) with ESMTPA id 5039C380084; Tue, 28 May 2019 13:26:42 -0400 (EDT) Subject: Re: Alternate methods for multiple rows input/output to a function. To: RAJIN RAJ K References: Cc: pgsql-general From: Adrian Klaver Message-ID: <6e492dc3-c1dd-2a97-643d-411cc0655fd6@aklaver.com> Date: Tue, 28 May 2019 10:26:41 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/28/19 8:06 AM, RAJIN RAJ K wrote: Please reply to list also. Ccing list. > Thanks for the response. > > CTE is not useful in my case. Here i want to pass the table to a > function and get the filtered results back from the function. > I tried few but not use full. > 1. Pass table input --> Ref cursor is the only option but which again > require loop to fetch the records. (FETCH ALL results cannot be stored > in a variable) >     Here im creating temp table withe required input data before the > function call. I'm going to take a stab at this though I do not entirely follow the logic. Definitely not tested: 1) create function filter_id(tbl_name varchar) return table (id bigint) begin --> Assuming input table is already created #temp_input_id return query EXECUTE format('select id ' 'from tbl a ' 'inner join' '%I b on (a.id = b.id)' 'where a.', tbl_name); end; 2) In calling function: WITH temp_tbl AS (select id from tbla... ), filter_tbl AS (select * from filter_id(temp_bl)) select a.* from tb3 a inner join tb4 inner join tb 5 inner join filter_tbl; > > > On Tue, May 28, 2019 at 8:29 PM Adrian Klaver > wrote: > > On 5/28/19 7:36 AM, RAJIN RAJ K wrote: > > --> Function ' filter_id ' filters the ID's based on some conditions. > > --> Input is set of ID's. (Not directly taking the input since > there is > > no provision to pass multiple rows to a function) > > To be honest I cannot follow what you are trying to achieve below. I do > have one suggestion as to creating temp tables. > > Why not use a  CTE: > > https://www.postgresql.org/docs/11/queries-with.html > > in the function to build a 'temp' table on the fly? > > > > > create function filter_id() > > return table (id bigint) > > begin > > > > --> Assuming input table is already created #temp_input_id > > > > retun query as select id > > from tbl a > > inner join > > #temp_input_id b on (a.id = b.id > ) > > where a.; > > > > end; > > > > > > --> Calling Function: > > > > create function caller() > > return table (id bigint,col1 bigint, col2 bigint) > > begin > > > > --> do some processing > > > > --> Find out the input id's for filtering. > > > > --> Create temp table for providing input for the filtering function > > > > create temp table #TEMP1 > > as select id from tbla........; > > (Cannot move the input id logic to  filter_function) > > > > --> calling the filter function > > create temp table #TEMP2 > > as select * from filter_id(); --> This is a generic function used in > > many functions. > > > > > > return query > > as select a.* > > from tb3 a inner join tb4 inner join tb 5 inner join #TEMP2; > > end; > > > > > > Is there any alternate way of achieving this? Passing multiple > records > > to a function im creating a temp table before invoking the function. > > For receiving an output of multiple rows i'm creating a temp > table to > > reuse further in the code. > > > > Can this be done using Refcursor? Is it possible to convert > refcursor to > > a temp table and use it as normal  table in query? > > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com