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 1hH8Mb-0004MP-SO for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2019 14:50:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hH8Ma-0005Pc-5e for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2019 14:50:08 +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 1hH8MY-0005PT-Ap for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2019 14:50:07 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hH8MU-0000dD-R3 for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2019 14:50:04 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 46D9025EA3; Thu, 18 Apr 2019 10:50:01 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 18 Apr 2019 10:50:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=C MRCnlnZOx6EzY1vlt9TfzIRS6wejIRcoLsEC531Iz4=; b=CVY8Yfh18gnfEtnD+ XzXALfmVdYOsqqVfwZ8T31Hjte7a4a48PA25Nq2erlYfatv+Zc0jXFyoTmoL0+Qn rycWyHpsyuRom8+AiqnxzN5+rcfgfS4fwknmu0Y0/CNPlzDtkUBtR+m5qtKP8C/Y F4Ie+kCC42MjtMEyiDUeWAzWPZPHn9S+y53Np9QNiDZtHjNMhAueSzNWTj95Rnm/ HzSlQlRsxgNc52yYjOwMp7aSmqDk7AE0w5nB1T52fi6hCmSJGhn2k52D0SItdF52 zhd7t8If15sCYuG27RMtYwaSUr66huh/b9ipWrzQZDTDcKOYPesu36eACs/xrWwK Dqpww== 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=CMRCnlnZOx6EzY1vlt9TfzIRS6wejIRcoLsEC531I z4=; b=S1tCmMc8vQNOAJgofhdEdhajTZqP/N+xMbsaRjMKH/1BZRXf4bfaMOAzZ /nSoJ4RPL3n9O4d5OR3oC76kGKnobC+zOncTUYeL8y+oPDcFUl8p9d3VFTFVTZn1 DnpLs5OInfCehDUyT5nBHiVKhCB69izq6S1Lfh5uNC+FZAiVMDqli/rA6NScsvvi YSYZ3PbfgLqgix6tYbAluaQjrjoVRwSeqL54EA6Zd/PjMth9YvKG2pBzxDYZ94JS mFBHLz1egwVJ6SbW6SUzUwMIcMOqo++BwrXzhOwfPhshCdmdO39i+1kW69JEZM5c 3/3oywj2kXIEls27OrakenLoU+bKw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrfeehgdefiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddvuddv rdduhedtrddvtddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id E5A75E44A1; Thu, 18 Apr 2019 10:49:59 -0400 (EDT) Subject: Re: Method to pass data between queries in a multi-statement transaction To: Souvik Bhattacherjee , "David G. Johnston" Cc: "pgsql-generallists.postgresql.org" References: From: Adrian Klaver Message-ID: <7abb82aa-d5c2-4d14-e57c-6e3267bd2085@aklaver.com> Date: Thu, 18 Apr 2019 07:49:56 -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 4/17/19 5:30 PM, Souvik Bhattacherjee wrote: > There are few if any situations where you need to immediately and > completely pass all values from one query to another in the same > transaction where the queries cannot just be combined into a single > statement.  Your representative example is one that is easily combined > into a single statement. > > > What if I need the result of the join to be stored into table3 as > well as the tuples that participated in the query to be deleted from > table1. The following can be done without the need to transfer values > from the previous query into the next: > > begin; > insert into table3 (id, attr1, attr2) (select t1.cid, t1.empname, > t2.dept from table1 t1, table2 t2 where t1.cid = t2.cid); > delete from table1 where cid in (select c.cid from table1 t1, table2 t2 > where t1.cid = t2.cid); Well the DELETE is not going to work as c.cid will error as undefined. > commit; > > However note that we have to perform the join twice, which is not > efficient. Now to make things worse, increase the number of tables to > join while imposing the requirement of tuple deletion to apply to all or > to a subset of the tables that participate in join. You might want to take a look at CTE's: https://www.postgresql.org/docs/11/queries-with.html > > Now, the stuff you are trying seems to indicate you are trying to do > something in C, inside the engine itself, with all of this.  If that is > the case you may want to be more clear as to what you are attempting to > do.  But as far as server SQL goes the only persistence area are > tables/relations - including temporary ones. > >> I'm trying to modify the engine here. > > -SB > > On Wed, Apr 17, 2019 at 6:16 PM David G. Johnston > > wrote: > > > On Wed, Apr 17, 2019 at 3:04 PM Souvik Bhattacherjee > > wrote: > > Hello, > > I'm trying to pass some values between queries in a > multi-statement transaction. For example, consider the following > representative multi-statement transaction: > > begin; > select * from table1 t1, table2 t2 where t1.cid = t2.cid; > delete from table1 where cid in > (values-to-be-populated-from-the-previous-query); > commit; > > > There are few if any situations where you need to immediately and > completely pass all values from one query to another in the same > transaction where the queries cannot just be combined into a single > statement.  Your representative example is one that is easily > combined into a single statement. > > Now, the stuff you are trying seems to indicate you are trying to do > something in C, inside the engine itself, with all of this.  If that > is the case you may want to be more clear as to what you are > attempting to do.  But as far as server SQL goes the only > persistence area are tables/relations - including temporary ones. > > David J. > -- Adrian Klaver adrian.klaver@aklaver.com