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 1iLUuF-0002uO-V3 for pgsql-performance@arkaria.postgresql.org; Fri, 18 Oct 2019 16:15:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLUuE-0007wd-IO for pgsql-performance@arkaria.postgresql.org; Fri, 18 Oct 2019 16:15:10 +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 1iLUuE-0007wN-59 for pgsql-performance@lists.postgresql.org; Fri, 18 Oct 2019 16:15:10 +0000 Received: from mail-il1-x143.google.com ([2607:f8b0:4864:20::143]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLUuA-0005qL-OK for pgsql-performance@postgresql.org; Fri, 18 Oct 2019 16:15:09 +0000 Received: by mail-il1-x143.google.com with SMTP id y5so6034092ilb.5 for ; Fri, 18 Oct 2019 09:15:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=o6YAx/HU9t2H8XxkwZTrXKaeTszjh047uzQ5aXko4Hk=; b=nOCM1JBIssJsfE5vLekLznXXyHJJEMth5MTzf7Uo47Wu+AaNK+VBF5OC7+LbfxSz2G LTXSJj6ORsJVxC5mvWwuYGOtssv+JuFLxdYO1+nmrPymDw2u1l5KbRH+Yx+rasGN61yk IIYnKRIpPyoTeNvJWXncUtJRThgv1yRooWYoIQjgVfZ6wmam16vvqliRgjJfK5tsgeei f+ECWOZmWjtVXhVbgfwXLoAx3footYSLwu7laESDdhzqch2VkZWWswFJx14MKzaItYnC mLizDUhjV6OI9TozBvi6osb0mX29OImG3pK2iSYNu5s0I3Y3moQd5Vq5J+Tc6OqVidtp jgNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=o6YAx/HU9t2H8XxkwZTrXKaeTszjh047uzQ5aXko4Hk=; b=f5CSzasDuSEbQEIoQk7hgZdPijrGF7SOsEdO1ZCfsxiYBf37VbLxUbk1r9eKpFZmDn gaGLvxyUsJ62YNMTxwLX74YUvNezHKqEGJT/lSFLN8JsZ83gWFVpw/Boilupw9YMr086 +RxA2v7GGy05e7YFLPL8Ln2uboUN3uo8HQepJPSENfsiBVjixVIRttLkuw8xw1sjlNuW PPoPxpbsl9NmGzISD3ylv28d7mb4CDfjGCAr67spFGaruN9hBzb2kmtoLzlRm/s25R3h Zi/I8t0VBytKDz1QuVeqOkQpRGAJOU+xOUxNiMyLKidbEK4MVqRpu7fBkn7oA+NYTz/5 cZbg== X-Gm-Message-State: APjAAAXJKwVTaWroqvyHrGBAL/TDqKFv1LvUUNhqmminGqINV4cqFvkS EKtEkBxb8n8BeUJdWMtcB6WLF8OfhHE= X-Google-Smtp-Source: APXvYqywcw3cZcNZrq4Gsi9aMgZNHTsHPwGy8WvojnMIizrHBHuQPaDeaamXl1hihN0EeXrorUzJ7Q== X-Received: by 2002:a92:410c:: with SMTP id o12mr11467712ila.287.1571415304368; Fri, 18 Oct 2019 09:15:04 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id 26sm2430296ilx.47.2019.10.18.09.15.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Oct 2019 09:15:03 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 612748015C0; Fri, 18 Oct 2019 11:15:02 -0500 (CDT) Date: Fri, 18 Oct 2019 11:15:02 -0500 From: Justin Pryzby To: M Tarkeshwar Rao Cc: "pgsql-performance@postgresql.org" Subject: Re: Can you please tell us how set this prefetch attribute in following lines. Message-ID: <20191018161502.GF3599@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Oct 18, 2019 at 03:47:49AM +0000, M Tarkeshwar Rao wrote: > How to fetch certain number of tuples from a postgres table. > > Same I am doing in oracle using following lines by setting prefetch attribute. > > For oracle > // Prepare query > if( OCIStmtPrepare( myOciStatement, myOciError, (text *)aSqlStatement, > // Get statement type > OCIAttrGet( (void *)myOciStatement, OCI_HTYPE_STMT, &statement_type, 0, OCI_ATTR_STMT_TYPE, myOciError ); > // Set prefetch count > OCIAttrSet( myOciStatement, OCI_HTYPE_STMT, &prefetch, 0, OCI_ATTR_PREFETCH_ROWS, myOciError ); > // Execute query > status = OCIStmtExecute( myOciServerCtx, myOciStatement, myOciError, iters, 0, NULL, NULL, OCI_DEFAULT ); > > For Postgres > Can you please tell us how set this prefetch attribute in following lines. Is PQexec returns all the rows from the table? Yes, PQexec reads everything at once into a buffer on the library. https://www.postgresql.org/docs/current/libpq-exec.html I think you want this: https://www.postgresql.org/docs/current/libpq-async.html |Another frequently-desired feature that can be obtained with PQsendQuery and PQgetResult is retrieving large query results a row at a time. This is discussed in Section 33.5. https://www.postgresql.org/docs/current/libpq-single-row-mode.html Note this does not naively send "get one row" requests to the server on each call. Rather, I believe it behaves at a protocol layer exactly the same as PQexec(), but each library call returns only a single row. When it runs out of rows, it requests from the server another packet full of rows, which are saved for future iterations. The effect is constant memory use for arbitrarily large result set with same number of network roundtrips as PQexec(). You'd do something like: PQsendQuery(conn) PQsetSingleRowMode(conn) while(res = PQgetResult(conn)) { ... PQclear(res) } Justin