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 1hH9Vj-0001M1-QS for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2019 16:03:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hH9Vi-0008W4-Kq for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2019 16:03:38 +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 1hH9Vi-0008SF-0w for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2019 16:03:38 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hH9Vf-000273-49 for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2019 16:03:36 +0000 Received: by mail-lj1-x241.google.com with SMTP id p14so2381235ljg.5 for ; Thu, 18 Apr 2019 09:03:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QzObsuRszvsRfKfvqFuxlsGxJOYK0S0gha7K06FItEs=; b=C6WYl0k3485O9PA6h2uLlNSnxElmLARWycww2mYe8ZqzSRopzSLjR/+upJB/Vg3RyR xB2t5GuQDjJooZbaj9tfV+jPJMJfFGMUBCm4igAxAePjDuzJsEm9AmdRv+HpT6v0FM0H jWQXhhLjRMJorAj4B8cpLtxKsEgOmT6sT5zKsUGPktiTusy7oaYfU7duhHc6GX3q8ZNi JiMwg+LJnQmqOr6OyxXWpBQHsbT6Pd3lBIFhKlwqK2XJcRnN8hZzBbvyjD//EuJdsmWy 363umaoNlX90XqdbC0LJoJ4lI8swFlmm6BpVPRGJQUiXzjHnRJKWP45RMkK5XCdknKJs shPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QzObsuRszvsRfKfvqFuxlsGxJOYK0S0gha7K06FItEs=; b=sHF/RzsPVoi+VfdYFDyySovQeuV7tFaMDUNmizl1C7KGpGAXQxiIOl4yrfDo+L0TRr 6LMHF1NAVx1ZGGzq90P06H8UEhAXWbEcqM4qL6QHnV3tdpvTBKJxfpHPFm4bOwpmte1i 7DY12nFe3uGZDqYiRWt4gW8fwaQGK9MQ2siqNOfz+b5SSynlTq3WrV0pqFrmSNJRxKN0 lQjGXRm1njouqqpqageyVVQmcLf6562Yj6cGo3ziLKd6/C4eoIDJZwSQnuapvH6IOaio pxdNnlhtTI6CrM98svkuwjKhWd9CzoirccNDgeLBMYHDMW+GycwaW67x+98jZFRsou/X cN1Q== X-Gm-Message-State: APjAAAURnf182MSgmdGtTajm2yVy05cu+8XgDYUzyDv/L9B1NBqNvx1N vrk0llJ/mj+l4u8dKDn/994WuPS9+Rvw8c21idjsoJcH X-Google-Smtp-Source: APXvYqyUMrzllyqqb2ytpt9WmY6VjhIecHm4diYHGSeo/ajfne9TAmFM0/6ngT1rT0PGKw/JBUAc4x8Us4PuU4AYjRY= X-Received: by 2002:a2e:9013:: with SMTP id h19mr8533509ljg.136.1555603413447; Thu, 18 Apr 2019 09:03:33 -0700 (PDT) MIME-Version: 1.0 References: <7abb82aa-d5c2-4d14-e57c-6e3267bd2085@aklaver.com> In-Reply-To: <7abb82aa-d5c2-4d14-e57c-6e3267bd2085@aklaver.com> From: Souvik Bhattacherjee Date: Thu, 18 Apr 2019 12:03:22 -0400 Message-ID: Subject: Re: Method to pass data between queries in a multi-statement transaction To: Adrian Klaver Cc: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000debd270586d0246d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000debd270586d0246d Content-Type: text/plain; charset="UTF-8" Well the DELETE is not going to work as c.cid will error as undefined. > Yes, that's a typo. I haven't tested it out before typing; just wanted to convey the general idea. -SB On Thu, Apr 18, 2019 at 10:50 AM Adrian Klaver wrote: > 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 > --000000000000debd270586d0246d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Well the DELETE is not going to work as c.cid will er= ror as undefined.

> Yes, that's a typo. I ha= ven't tested it out before typing; just wanted to convey the general id= ea.

-SB

On Thu, Apr 18, 2019 at 10:50 AM Adrian Kla= ver <adrian.klaver@aklaver.= com> wrote:
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.=C2=A0 Your representative example is one that is easily com= bined
> into a single statement.
>
>=C2=A0 > What if I need the result of the join to be stored into tab= le3 as
> well as the tuples that participated in the query to be deleted from <= br> > 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 =3D t2.cid);
> delete from table1 where cid in (select c.cid from table1 t1, table2 t= 2
> where t1.cid =3D 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 <= br> > something in C, inside the engine itself, with all of this.=C2=A0 If t= hat is
> the case you may want to be more clear as to what you are attempting t= o
> do.=C2=A0 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
> <da= vid.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>> wrote:<= br> >
>
>=C2=A0 =C2=A0 =C2=A0On Wed, Apr 17, 2019 at 3:04 PM Souvik Bhattacherje= e
>=C2=A0 =C2=A0 =C2=A0<kivuosb@gmail.com <mailto:kivuosb@gmail.com>> wrote:
>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Hello,
>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0I'm trying to pass some values be= tween queries in a
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0multi-statement transaction. For exam= ple, consider the following
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0representative multi-statement transa= ction:
>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0begin;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0select * from table1 t1, table2 t2 wh= ere t1.cid =3D t2.cid;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0delete from table1 where cid in
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(values-to-be-populated-from-the-prev= ious-query);
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0commit;
>
>
>=C2=A0 =C2=A0 =C2=A0There are few if any situations where you need to i= mmediately and
>=C2=A0 =C2=A0 =C2=A0completely pass all values from one query to anothe= r in the same
>=C2=A0 =C2=A0 =C2=A0transaction where the queries cannot just be combin= ed into a single
>=C2=A0 =C2=A0 =C2=A0statement.=C2=A0 Your representative example is one= that is easily
>=C2=A0 =C2=A0 =C2=A0combined into a single statement.
>
>=C2=A0 =C2=A0 =C2=A0Now, the stuff you are trying seems to indicate you= are trying to do
>=C2=A0 =C2=A0 =C2=A0something in C, inside the engine itself, with all = of this.=C2=A0 If that
>=C2=A0 =C2=A0 =C2=A0is the case you may want to be more clear as to wha= t you are
>=C2=A0 =C2=A0 =C2=A0attempting to do.=C2=A0 But as far as server SQL go= es the only
>=C2=A0 =C2=A0 =C2=A0persistence area are tables/relations - including t= emporary ones.
>
>=C2=A0 =C2=A0 =C2=A0David J.
>


--
Adrian Klaver
adrian.klave= r@aklaver.com
--000000000000debd270586d0246d--