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 1gSlFb-0001mF-Rj for pgsql-general@arkaria.postgresql.org; Fri, 30 Nov 2018 16:02:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gSlFY-00048B-Pg for pgsql-general@arkaria.postgresql.org; Fri, 30 Nov 2018 16:02:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gSlFW-000484-TI for pgsql-general@lists.postgresql.org; Fri, 30 Nov 2018 16:02:40 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gSlFP-0004AC-3s for pgsql-general@lists.postgresql.org; Fri, 30 Nov 2018 16:02:37 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2CB3221FE8; Fri, 30 Nov 2018 11:02:30 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 30 Nov 2018 11:02:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=C 36e1/+5/o5ULW3K0OmrulbpjB/oPf5sCpfYxXwoOF0=; b=aydvou1dzttJ+INQB GpQhWU87AGKAQkDD8x0SJ2GXJckZeW8SjMRzZKQPJ1yXsLoYJ6z3P4/MpZJfuBaP bdKCVOJVm4Jw1ky4PvTq+Md5ZK79QRIKXxDx/dSMPeUx0+cH2ljkcrlh26LEUuMr Z3NvUzbaV+T8xtZXNHdo1fVH3NKoogru2E/ZJcUQgFUKSIzx8LQs031gI3JXsv0b WQehXzw+U/VyJsATe9lou0sjQihLQhQjPcKskFg8sl+JAgJ9LclnBhe5cHG1/v/s jOhX1naSD0pH636plbesf4XHfL9IvhQ2urX07J0grRhYqTUpuS5IzdIckBtJzLuQ 5dCPg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=C36e1/+5/o5ULW3K0OmrulbpjB/oPf5sCpfYxXwoO F0=; b=I3aCoEaIBnVb0zf5sMFOdKuMB8mWfTRX73gzR5leKlT40T/XFmHzu6VUq PqXbGV0KM7R/47E2b4IAhsUW2hB/Vp3O4nzCXIK71sVV7HJklQikvxizS4zXOxUa hNhMIFjwu/8gFSTGYrxHPDMLMPlLJEYu5tDFZT4lRcDhDj9c9RRm8aC+V+TKJgQI a9k0F2J8vvCZwhwdedDijLyOxETsxMlXLWxelFBNDrVD3WRdzPIYX/T2H7f747kG qeTALUeW7biROqEVGTzHDNmmE8bBeBAC9kPckXN/7K1TJMvtctpIeaMMc2xZSSNI aIDow2MFDJwr6uiGlUUlvUxtc7jjg== X-ME-Sender: X-ME-Proxy: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id D992D1030F; Fri, 30 Nov 2018 11:02:28 -0500 (EST) Subject: Re: psql is hanging To: John Smith , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <3e868033-67ef-cd99-3c62-55da5d164322@aklaver.com> Date: Fri, 30 Nov 2018 08:02:27 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/30/18 7:53 AM, John Smith wrote: > We have a long script of sql that we run, several thousand lines of sql. > If I execute the script from start to finish, somewhere in the middle of > it, one sql command will hang and take 2 to 3 hours. During this time, > "htop" shows 100% cpu usage with a postgresql process, so it appears to > be doing something. First, a naming note. psql is the name of the Postgres command line client. What you are actually referring to is the Postgres server. Second, without the actual commands solving this is going to be difficult to impossible. That being said I would suspect some sort of locking issue. I would start with pg_stat_activity: https://www.postgresql.org/docs/10/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW See what it says when you are in the 'hung' phase. > > If I stop that command, then immediately rerun the rest of the script > starting from the command that "hung", the "hung" command then completes > in 5 seconds and the rest of the script just continues on perfectly. I > can interrupt the command within 30 mins of hanging, or 2 hours of > hanging; regardless of when I interrrupt the hung command, it always > immediately completes successully in < 5 seconds when I rerun the script > immediately after that. > > I have noticed this in multiple scripts, multiple commands; one was an > "insert into", a different script hung on a "update" and a third one > would hang on a "create table x as (select ... )". These are all short > running commands normally, and we have indexes on all the columns for > the appropriate "where" clauses. > > I can't send the sql (forbidden), but the actual command does not appear > to be the issue. Rerunning the same little section of sql manually which > includes the hung command, always takes less than a minute, but the > entire sql which should only take 30 minutes in total will always hang > at the same spot; and then interrupting it (regardless of how long it > was running) and rerunning immediately completes. > > The "hung" command does eventually finish at times after 2 to 3 hours, > most often we can't afford to wait for it to run since this batch of sql > has to run daily. > > Some other notes: > - Environment is Ubuntu 16.04; postgresql version was originaly 9.5(?) > but we have upgraded to 11.1 using postgresql packages for ubuntu and > the problem has persisted across postgresql versions. > - There are no other queries running on the postgresql server during > this time, and server load is idling (other than whatever psql is doing). > - It is an amazon server, with ZFS as the files system, it has 50% disk > free on this storage partition that postgresql database is on. > - We have 60 gigs of ram total, 8 cores. > - We have run the entire script using "create unlogged table" everywhere > as well, no difference. > - We have made tunings to the postgresql configuration to make it use > more RAM. > - All sql commands are sent through a custom Java application; so we use > java + JDBC to connect to postgresql for this. The script has custom > commands which are interpreted by the Java app, so we can't test running > the exact script start to finish in psql native tools since the native > tools won't understand our custom sql. However the java app is very > simple. All individual sql commands are committed immediately after they > run so there is no long transaction in play here. The psql jdbc driver > is "postgresql-42.2.4". > - One may suspect the custom java app; but again, htop shows postgresql > using 100% CPU usage on one core, so postgresql is chewing on the > command, doing something. > > I have attached our current postgresql.conf file. > > We're kind of pulling out our hair here, any ideas? > > Thanks! -- Adrian Klaver adrian.klaver@aklaver.com