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 1rZHa9-003Hc4-9q for pgsql-general@arkaria.postgresql.org; Sun, 11 Feb 2024 21:41:50 +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 1rZHa7-00579T-LU for pgsql-general@arkaria.postgresql.org; Sun, 11 Feb 2024 21:41:47 +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 1rZHa6-00579K-NO for pgsql-general@lists.postgresql.org; Sun, 11 Feb 2024 21:41:47 +0000 Received: from wfout7-smtp.messagingengine.com ([64.147.123.150]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rZHa3-006GIw-Eb for pgsql-general@lists.postgresql.org; Sun, 11 Feb 2024 21:41:45 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id D01D11C00067; Sun, 11 Feb 2024 16:41:40 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sun, 11 Feb 2024 16:41:41 -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=fm2; t=1707687700; x=1707774100; bh=t3tjj9XH1N6UCxWvydhlPGgXrtFtBH/AI4Hxnc0Nu3E=; b= L0OYsOwRvPezE5DCspbcKrlSEQTMvyUC5cDHdusYjWyi0CNmopkad41AQKVyjZm9 hBFPlczemZdsUuve5HpuATTwUHExrFiEf86uH5qKdW0pkSWyIwjUFG5vRxhUTLsC WiBeTnclM2WChjtPuh37JEH2JWReYW9CGxbaMgXrqdstNdcsHU2xlj224LD7s59o XI5PubaW2iTc7LQI8NLuc49p1++PMUArtK8qQi0jWk7PzA40bl1539DG68Ma5xEZ jH7zO07LbHSVGTWF0BuB3X8vl9h6f6HIB0tasOpCg3jkOG3S7BVa4OiliAlLiSiG EgXDcL4sJ22ORiXgL8+ZJw== 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-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1707687700; x= 1707774100; bh=t3tjj9XH1N6UCxWvydhlPGgXrtFtBH/AI4Hxnc0Nu3E=; b=M ZsTcgXK8SyVqIMN7Noeqp8vKL0PIcEiNOoZ3ANvzcJg5sLF/rzGNWYNfOA3a2S2l PHCLKZLMvQBy/siiyOaRWPZx9/I9zASzOIegFNdxSTF0jjgNsa+/hGsqvEHCuKDb HRUJBRlB3D9RLzkNAYXzivL4Aer/UwD752szD3yc8NtAa/PJFBHJyF1zHwgw2o6P DTIBGWnrO1cuifeJEN6cayTrjOfpCHM16TWyuNc2gQZZcoQP+w7G32wIUaQXCRSK GDXV6l4ekYZ20t5wE7NJ24BUi2wTD2U5nNeUGJEZmOBBmARszSpZuxoHSG5/j+sc BPm077ObipMPqqlyhED0w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddugdduheefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtke ertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepkedthfelje eivddtjeetfeekieevhfdthfefffejudffjedvkeegtddvleejfeevnecuffhomhgrihhn pehsthgrtghkohhvvghrfhhlohifrdgtohhmpdhpohhsthhgrhgvshhqlhdrohhrghenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgr nhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 11 Feb 2024 16:41:39 -0500 (EST) Message-ID: <98a7e782-96bc-4c55-a919-bda0d5d35f87@aklaver.com> Date: Sun, 11 Feb 2024 13:41:38 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Query hangs (and then timeout) after using COPY to import data To: steott@gmail.com, pgsql-general@lists.postgresql.org References: <002401da5d32$8637c200$92a74600$@gmail.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: <002401da5d32$8637c200$92a74600$@gmail.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 2/11/24 13:37, steott@gmail.com wrote: > Hello, > > I’m new to PostgreSQL. I’m trying to migrate an app from SqlServer to > Postgres (it’s written in C# and uses Npgsql) > > I’ve tried with Postgres 16.1 and 16.2 on Windows Server 2019. > > This app used SqlServer’s Bulk Insert to import some tables (about 50 > tables) from another database, I replaced it with Postgres’ COPY > function: this part works correctly. > > After the import, I execute sequentially (not in parallel) some queries > in these tables, to update some data and to make some validations. > > At some point, systematically, one of these queries hangs, and after 10 > minutes (the CommandTimeout that I set) it throws this exception: > > Exception while reading from stream ---> System.TimeoutException: > Timeout during reading attempt > >    at Npgsql.Internal.NpgsqlConnector > > The query is this one: > > > SELECT Id FROM Item > > WHERE Id NOT IN ( > > SELECT ItemId FROM ItemUom) > > LIMIT 100 > > The same query, executed from pgAdmin, returns the result in less than a > second (even if it’s executed while the query from my app is running). > > (actually the result are 0 record, but it’s correct: the query it’s just > a validation that there are no records in that query) > > While the query is running from my app, I noticed that the CPU goes > beyond 95%, even up to 100%, due to 3 postgres.exe processes. > > The RAM usage is less than 70%. > > In pgAdmin I’ve executed a query to list the running queries, and I can > see that one. > > My issue seems to be very similar to this one: > https://stackoverflow.com/questions/77195107/npgsql-timeout-during-reading-attempt > but I didn’t understand how that guy solved the problem. > > If I import less tables from the external database, the query doesn’t > hang and runs correctly, so this make me think about some resources that > could “finish”, but I haven’t understood which one (for example the > connections used to import the tables and all the commands and > datareader used to execute the queries seem disposed correctly). > > I don’t know if it could be due to some Postgres parameter. > > Do you have any suggestions to solve this problem? > Run ANALYZE on the tables/database. See: https://www.postgresql.org/docs/current/sql-analyze.html -- Adrian Klaver adrian.klaver@aklaver.com