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 1izMVe-0004Sn-9f for pgsql-general@arkaria.postgresql.org; Wed, 05 Feb 2020 15:22:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1izMVd-0005gI-4M for pgsql-general@arkaria.postgresql.org; Wed, 05 Feb 2020 15:22:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1izMVc-0005gA-N5 for pgsql-general@lists.postgresql.org; Wed, 05 Feb 2020 15:22:32 +0000 Received: from forward101j.mail.yandex.net ([5.45.198.241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1izMVW-0008KY-64 for pgsql-general@lists.postgresql.org; Wed, 05 Feb 2020 15:22:32 +0000 Received: from mxback26j.mail.yandex.net (mxback26j.mail.yandex.net [IPv6:2a02:6b8:0:1619::226]) by forward101j.mail.yandex.net (Yandex) with ESMTP id 8581F1BE0D9D; Wed, 5 Feb 2020 18:22:24 +0300 (MSK) Received: from iva3-dd2bb2ff2b5f.qloud-c.yandex.net (iva3-dd2bb2ff2b5f.qloud-c.yandex.net [2a02:6b8:c0c:7611:0:640:dd2b:b2ff]) by mxback26j.mail.yandex.net (mxback/Yandex) with ESMTP id JBXQrERTdv-MNsWn5jO; Wed, 05 Feb 2020 18:22:24 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ya.ru; s=mail; t=1580916144; bh=71JzabcBzVZqCB50AaBxxyBWTgVysYbEAupZu1aMtvg=; h=To:In-Reply-To:From:Subject:Cc:References:Date:Message-Id; b=YEA5exn3RxTl+MCNYBQpmEwKvx42/irDjVvsN5m/R0pCuAiNrQQNmqJdROu7QEya/ QZvuaGdA3rKCjUqAawm7OG7bUEmqo+frEF2LWJywNc1YjZtJaQQdW769IC+fog0t4U Usk+J3SSpozDm/iM2plHRZZyLZsAXQ4gfco8B7uc= Authentication-Results: mxback26j.mail.yandex.net; dkim=pass header.i=@ya.ru Received: by iva3-dd2bb2ff2b5f.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id XyLJjF3daZ-MNW8gmtJ; Wed, 05 Feb 2020 18:22:23 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) Subject: Re: initialize and use variable in query From: =?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= In-Reply-To: Date: Wed, 5 Feb 2020 18:22:22 +0300 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Glenn Schultz X-Mailer: Apple Mail (2.3608.60.0.2.5) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Just refactor DO block to function that returns row set and put SELECT = inside. > 29 =D0=B4=D0=B5=D0=BA. 2018 =D0=B3., =D0=B2 18:40, Glenn Schultz = =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0): >=20 > All, >=20 > I need to initialize a variable and then use it in query. Ultimately = this will part of a recursive CTE but for now I just need to work this = out. I followed the docs and thought I needed something like this. But = does not work-maybe I have misunderstood. Is this possible? >=20 > SET max_parallel_workers_per_gather =3D 8; > SET random_page_cost =3D 1; > SET enable_partitionwise_aggregate =3D on; > =09 > Do $$ > Declare startdate date; > BEGIN > startdate :=3D (select max(fctrdt) from fnmloan); > END $$; > =09 > select=20 > fnmloan.loanseqnum > ,fnmloan.currrpb > from > fnmloan > =09 > join > fnmloan_data > on > fnmloan_data.loanseqnum =3D fnmloan.loanseqnum > =09 > where > fnmloan.fctrdt =3D (select * from startdate) >=20 > limit 10