Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcYF1-0008LF-T1 for pgsql-general@arkaria.postgresql.org; Wed, 15 Mar 2023 21:01:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pcYF0-0004GB-EQ for pgsql-general@arkaria.postgresql.org; Wed, 15 Mar 2023 21:00:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcYEy-0004Fu-Qx for pgsql-general@lists.postgresql.org; Wed, 15 Mar 2023 21:00:58 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcYEv-0001Lc-KL for pgsql-general@lists.postgresql.org; Wed, 15 Mar 2023 21:00:55 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id E3A385C02C4; Wed, 15 Mar 2023 17:00:50 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Wed, 15 Mar 2023 17:00:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1678914050; x=1679000450; bh=8F+ly4j5p2I9LKgLjVQhBjxy7amYKD2dRAW ccIHVSj0=; b=lmiERjJtU+t78E+XihZUgeC7Dk4bRod9tH4HbZZXTf0fqW53zHY 9jK2+MWzQon9YIK7FjmkjCzj1MnXCXtPwsr9u5yyojnnqVEYrR9QLYcqJTT0db57 ylfISzPXXSzTGn8OXaHb9/qfNIS5Ms266T0IKBSqLDTcbv7bh5kt48IWonhpmVwO IbxkrT/h/N4KjQDgNbVYyP66aEXUTQgbRXbxgg31sHhVmEC8cgBLATTbEHs5vAGr 5AhFlbBTwpDJRT+1R6HmfYL0Fah9uDBYi/fvf0ANIhCx2VBocIMPuWOSyLwOpO55 QIuXS21zemeOcfEPxbbAOR4gx4O2Pvg51Fg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1678914050; x=1679000450; bh=8F+ly4j5p2I9LKgLjVQhBjxy7amYKD2dRAW ccIHVSj0=; b=mBXmUde5V6sjLXko8ZmTAAUv5rZwUo9rQtUrCnf3rTEFZb8r9Cl 2FcZiajWLpoeiNpKUZzEALAOpU+B5KgXJZxh/qRHp59+KbSnrgwjEKq0Q69+r4P0 Yz2jc43vaKpvaCCZyxHtR0GE0YbU/vsFoGiVvqLRY1+hnc20kwMUH5deOkH+F730 24tW0e6gNeypREjk0+A+75umQhLekGpO4vvEnPXwkjOvoKLWRxOfZarPeawR5iwP HGBCng8tpS7fwHSPU+Xxt8xcwQJeKxAsRoouLtJwCdWVi1i7vfz0JlyAdR7ZRMo4 pc1oMn3Bk5bHASoate+zq80H7cancAXVKLg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvddvkedgudegfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttdefjeenucfhrhhomheptegu rhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrd gtohhmqeenucggtffrrghtthgvrhhnpeehudfgtefhtedtheeiveetueektdejvdfhveek gfdufeegveetuddvuedvgeeggeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 15 Mar 2023 17:00:49 -0400 (EDT) Message-ID: Date: Wed, 15 Mar 2023 14:00:49 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: Is the PL/pgSQL refcursor useful in a modern three-tier app? To: Bryn Llewellyn , Laurenz Albe Cc: pgsql-general list References: <6A73AAEA-7A9F-42C9-A842-88213FA41003@yugabyte.com> <7992035C-058A-4AB7-915A-41A3914E3A63@yugabyte.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: <7992035C-058A-4AB7-915A-41A3914E3A63@yugabyte.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/15/23 13:37, Bryn Llewellyn wrote: >> laurenz.albe@cybertec.at wrote: >> > Re « You seem to think that a client request corresponds to a single database request », I meant no more than what psql models when you hit "return"after terminating an ordinary SQL statement with semi-colon (i.e. not a meta-command). I used "client" to mean a program that connects directly to the PostgreSQL server (using TCP/IP)—and not the ultimate human client who's using, say, a browser. Perhaps I should have said "top-level SQL call". (To keep this simple, let's assume that I don't use "backslash-semicolon" to send two or more top-level SQL calls in one round trip.) > > So in this example: > > begin; > declare cur cursor for select k, v from s.t order by k; > fetch forward 10 in cur; > fetch absolute 90 in cur; > fetch forward 10 in cur; > commit; > > where I hit "return" after each semicolon, I'd say that I made six top-level SQL calls—and that lines up with the fact that each of "begin". "declare", "fetch", and "commit" has its own page in the "SQL Commands" section of the PG doc. > > It sounds like you prefer "database request" for this. Is that right? > > I was trying to get at the distinction between issuing a particular SQL statement as a top-level SQL call and issuing it from a PL/pgSQL subprogram. > > I've read wording like what you wrote following "Cursors shine wherever you need …" elsewhere. But I can't picture a concrete use case where, not withstanding the "where" restriction that my "select" used, I can't tell how much of the result set I'll need or where reading result #n1 informs me that I next need to scroll and read result #n2. So I was looking for a convincing example. Huh? You provided your own example earlier: "Of course, it all falls into place now. I can see how I could write a client app in, say, Python to write a humongous report to a file by fetching manageably-sized chunks, time and again until done with a function like my "g()" here, from a cursor that I'd opened using a function like my "f()"." > > > > > > -- Adrian Klaver adrian.klaver@aklaver.com