Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tP2RQ-009phT-SY for psycopg@arkaria.postgresql.org; Sat, 21 Dec 2024 16:35:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tP2RQ-00AIKG-2K for psycopg@arkaria.postgresql.org; Sat, 21 Dec 2024 16:34:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tP2RO-00AIEs-MI for psycopg@lists.postgresql.org; Sat, 21 Dec 2024 16:34:59 +0000 Received: from fout-b8-smtp.messagingengine.com ([202.12.124.151]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tP2RL-000hZ5-7s for psycopg@postgresql.org; Sat, 21 Dec 2024 16:34:57 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfout.stl.internal (Postfix) with ESMTP id 8406211400F6; Sat, 21 Dec 2024 11:34:53 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Sat, 21 Dec 2024 11:34:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= 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:subject:subject:to:to; s=fm1; t=1734798893; x=1734885293; bh=/pgCO6hoV91LJNJesdv9/Qy5AO5i9UpPnx5HcNAtp4c=; b= trMJUhUyIKUzEPyI1qJbBTtyZ5o4b1eNPnGeXUqbsXN7a6gEiRIfO2S/2cs4lHnv b3qfSXfY866tHYRiUHlyXb8flb9CzseZzxMPZhcVo/Fgn+BFZq0RsCYIoqLKazl7 NBgmhIWv1MWyRAd9xp3rCvV8B88KFvmvvCuzEIsxqSW0YhlM8TnpTyYS734n1Ho/ iY8oddsU4oitCSkw95pwV5+PvtYHTm1SfmHBkBpjj3xjxGFMNSXRqfJdOU09xM1V gJqEF9thXP9as2Eha02998FppTEdOniH4FJAe1eyZuTpjSF9cbR6wgRAEB3yCJnE XUeBBd5KAg48mk5EmAOzbw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1734798893; x=1734885293; bh=/ pgCO6hoV91LJNJesdv9/Qy5AO5i9UpPnx5HcNAtp4c=; b=FCQI1rx8M615O9Y1l WEhqjN6tKPjUEy/6bRCM/xP7VEFwqC9d9Jn8YXFuLvwh9MHjHC3MYCgelYdgluyh /cAvZyqJSuWiXXeZ+tZ8lauZ/g1Lw39u0E9K2MKpSJPk1e1IEK+UGz3Z8cdzCABt DCIYrCNU3iT8nIbiEnnIXkw0XjKllO5UpBSJwxt9z+K63yuk3q1Teg2gEtDo+IGZ rru2psHA6WjJvtDuEWHSeo4qkAMMsc5mcYez4lxPSZ/ICthfJB9UnQaBwVKJGkSA FOcb7rKaQSNC+FLTqoAf2STt+BGn7Ui0TBir3s7eDTgGtABxcnRRlB4eOl5VNT9U CWH5w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddruddthedgieeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfg fuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghr uceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrth htvghrnhepudelgffhleegueetleefteeuieffgedtkedvledtueeuledvtdekgfdviefg iefgnecuffhomhgrihhnpehpshihtghophhgrdhorhhgnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpd hrtghpthhtohepghhrvggrthhvohhvrghnsehgmhgrihhlrdgtohhmpdhrtghpthhtohep phhshigtohhpghesphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 21 Dec 2024 11:34:52 -0500 (EST) Message-ID: Date: Sat, 21 Dec 2024 08:34:51 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Memory To: Vladimir Ryabtsev , psycopg@postgresql.org References: Content-Language: en-US From: Adrian Klaver In-Reply-To: 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 12/21/24 02:45, Vladimir Ryabtsev wrote: > Hi community, > > I am reading a big dataset using code similar to this: > > query = ''' > SELECT timestamp, data_source, tag, agg_value > FROM my_table > '''I > batch_size = 10_000_000 > > with psycopg.connect(cs, cursor_factory=psycopg.ClientrCursor) as conn: FYI, ClientCursor. > > I looked the documentation, but did not find specifics related to > performance differences between Server and Client cursors. > > I am fine with ServerCursor, but I need to ask, is it by design that > with ClientCursor the result set is copied into memory despite > fetchmany() limit? ClientCursor is the default class, so may be worth > documenting the difference (sorry, if I missed that). Client side cursor https://www.psycopg.org/psycopg3/docs/advanced/cursors.html#client-side-cursors "In such querying pattern, after a cursor sends a query to the server (usually calling execute()), the server replies transferring to the client the whole set of results requested, which is stored in the state of the same cursor and from where it can be read from Python code (using methods such as fetchone() and siblings)." https://www.psycopg.org/psycopg3/docs/api/cursors.html#psycopg.Cursor.fetchmany "fetchmany(size: int = 0) → list[+Row] Return the next size records from the current recordset. size default to self.arraysize if not specified. Return type: Sequence[Row], with Row defined by row_factory " Server side cursor https://www.psycopg.org/psycopg3/docs/advanced/cursors.html#server-side-cursors "PostgreSQL has its own concept of cursor too (sometimes also called portal). When a database cursor is created, the query is not necessarily completely processed: the server might be able to produce results only as they are needed. Only the results requested are transmitted to the client: if the query result is very large but the client only needs the first few records it is possible to transmit only them. The downside is that the server needs to keep track of the partially processed results, so it uses more memory and resources on the server." > > Thank you. > -- Adrian Klaver adrian.klaver@aklaver.com