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 1i3NLv-0003iQ-U1 for pgsql-sql@arkaria.postgresql.org; Thu, 29 Aug 2019 16:32:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i3NLt-0005eU-0i for pgsql-sql@arkaria.postgresql.org; Thu, 29 Aug 2019 16:32:49 +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 1i3NLs-0005cW-NO for pgsql-sql@lists.postgresql.org; Thu, 29 Aug 2019 16:32:48 +0000 Received: from mail150.strasbourg.4js.com ([77.159.205.150]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3NLq-0001KW-8k for pgsql-sql@lists.postgresql.org; Thu, 29 Aug 2019 16:32:47 +0000 Received: from [10.0.40.61] (toro.strasbourg.4js.com [10.0.40.61]) (authenticated bits=0) by mail150.strasbourg.4js.com (8.14.4/8.14.4/Debian-4+deb7u1) with ESMTP id x7TGWiRQ032538 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT) for ; Thu, 29 Aug 2019 18:32:44 +0200 To: pgsql-sql@lists.postgresql.org From: Sebastien FLAESCH Subject: libpq: How are result sets fetch behind the scene? Organization: Four Js Development Tools Message-ID: <6f2f210b-53e6-f350-4850-83f49b2a1354@4js.com> Date: Thu, 29 Aug 2019 18:32:44 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Virus-Scanned: clamav-milter 0.99.4 at mail150 X-Virus-Status: Clean X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (mail150.strasbourg.4js.com [10.10.0.1]); Thu, 29 Aug 2019 18:32:44 +0200 (CEST) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi all, We are using libpq in our C program. I was wondering how the client lib / protocol manage to fetch the result set to the client app memory. Is there some optimization ? Are rows fetched in sequence until the requested row? I mean if I do a SELECT that produces a huge result set, and then call PQgetvalue() with a row_number = 5000, are all 5000 rows fetched from the server or is there some smart fast-forward done because I did not ask the 4999 previous rows? We know about server cursors (declare + fetch forward), and we do already fetch blocks of rows with this. But server cursors are slow (and certainly use more server resources), compared to direct SELECT execution. So we are wondering what would be the impact in terms of resources on the client application. Is there any doc link or blog that would explain best practices with libpq result set programming? Thanks! Seb