Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1obH7U-0001rd-8k for pgsql-general@arkaria.postgresql.org; Thu, 22 Sep 2022 07:59:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1obH7T-000267-6l for pgsql-general@arkaria.postgresql.org; Thu, 22 Sep 2022 07:59:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1obH7S-00025y-Qd for pgsql-general@lists.postgresql.org; Thu, 22 Sep 2022 07:59:38 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1obH7L-0002ul-R0 for pgsql-general@postgresql.org; Thu, 22 Sep 2022 07:59:37 +0000 Received: by mail-ej1-x632.google.com with SMTP id y3so19214310ejc.1 for ; Thu, 22 Sep 2022 00:59:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date; bh=uVVS9WAwh3uc+T86KWgjTEADhoL2v+NJuAVt8fStAbU=; b=tFmR2s1ZpogqaNBAU012ezxUbartLFX9gdSvl2bR5OPwd8kklxz7Yf/yXIaRRBjQA1 eTGKtxlYx+ounE7In5fElXEIMDqAwD+LDupfEsPnvFM2I8QuAovTnQV5SBf3/6SHgrr4 0nyUBLVkzGEyEWrUrtyu6FnWoX4DibdtHXfTbkdLcquNxXlk3EKX/pG9twbZR9iQaL5D wIZKfuYg6Hxy906OMAikfnH5JZgumzkasNJPn7x/iRYjJRv/bQwWW3UJ6myyaRoPAK1o CwPFhJYku2OLl/9HcZ8SxF757gwK4YPP8RMnXF+tgFTbuHOlPxSo7ITKz+X0pYClZZSj 7LNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date; bh=uVVS9WAwh3uc+T86KWgjTEADhoL2v+NJuAVt8fStAbU=; b=5JwC62cldin615sHAKfsanjT0P2D0CZ1yp/G49v00nLQyTS534JMyHmC9nNXNIq8Dj 06ff+Kivl6tHy+MIpy6bga9W/K4/jTmgDbRGJxDyzX8nDhW0EG0bVQ3+UH5InJk5lY1t OoOX/ITLSnActn3k92H2x2ppOozsq9jJ3Gxye7LQeL8ek7l/NphMAKo0Fp/BPlu0sqKV rO+C2pF83BdwVvDGt8vrfK31/MkC7Y0F0pqattcIcGQuNPh9enBbT5/0QBdm6cCDGokE iZTAQB9S7QevGvBD8VXQtfufZRQ1OYNHehnfAEuYg2yJFgEgKuWtJcAZOOXYXBhbTdQE fqJg== X-Gm-Message-State: ACrzQf0w9Kt/VsSh0f4CzMan8S9vl2FwRkKSTfQWnC+Kp/7f/K+AfIGF rswNNr+Rm4cbJ4ea9EKjc2DR7g== X-Google-Smtp-Source: AMsMyM5ZVj7MFVOlI2qtq2+k29J4o7NGGcR9ujUfetI/u6N6f2Qp11hEGoFAOblKBDJE3p2md6Bawg== X-Received: by 2002:a17:907:c10:b0:782:386f:f558 with SMTP id ga16-20020a1709070c1000b00782386ff558mr1618025ejc.739.1663833570383; Thu, 22 Sep 2022 00:59:30 -0700 (PDT) Received: from localhost.localdomain ([88.116.133.170]) by smtp.gmail.com with ESMTPSA id l2-20020a1709063d2200b00731582babcasm2320600ejf.71.2022.09.22.00.59.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 00:59:29 -0700 (PDT) Message-ID: Subject: Re: [EXT] pg_stat_activity.backend_xmin From: Laurenz Albe To: "Dirschel, Steve" , Rob Sargent Cc: "pgsql-general@postgresql.org" , "Medarametla, Pavan (TR Technology)" Date: Thu, 22 Sep 2022 09:59:28 +0200 In-Reply-To: References: <73c8c16fbee732725f53e291002b694d15932516.camel@cybertec.at> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2022-09-21 at 16:22 +0000, Dirschel, Steve wrote: > > > > > We are troubleshooting an issue where autovacuum is not cleaning up a table. > > > > > The application using this database runs with autocommit turned off. > > > > > We can see in pg_stat_activity lots of sessions “idle in transaction” > > > > > even though those sessions have not executed any DML-  they have > > > > > executed selects but no DML.  The database’s isolation level is set to read committed. > > > > > > > > "backend_xmin" is set when the session has an active snapshot.  Such a snapshot is held > > > > for the whole duration of a transaction in the REPEATABLE READ isolation level, but > > > > there are cases where you can see that in READ COMMITTED isolation level as well: > > > > > > > > - if there is a long running query > > > > > > > > - if there is a cursor open > > > > > > > > Perhaps you could ask your developers if they have long running read-only transactions with cursors. > > > > > > Thanks for the reply Laurenz.  For an application session in this "state" pg_stat_activity > > > shows the state of "idle in transaction" and backend_xmin is populated.  The query shows the > > > last select query it ran.  It is not currently executing a query.  And dev has said they are > > > not using a cursor for the query.  So it does not appear they have long running read-only > > > transactions with cursors. That does not follow. You can execute: DECLARE c CURSOR FOR SELECT /* whatever */; FETCH 50 FROM c; SELECT /* something entirely different */ So you have an open cursor (portal), even though the last statement executed does not use a cursor at all. > > > Outside that scenario can you think of any others where a session: > > > 1. Login to the database > > > 2. Set autocommit off > > > 3. Run select query, query completes, session does nothing after that query completes. > > > 4.  transaction isolation level is read committed No. > > > That session sitting there idle in transaction has backend_xmin populated.  When I run that > > > test backend_xmin does not get populated unless I set my transaction isolation level to > > > repeatable read.  We have enabled statement logging so we can see if their sessions are > > > changing that transaction isolation level behind the scenes that they are not aware of > > > but so far we have not seen that type of command logged. > > > > What stack is the application using?  Anything like Spring or Hibernate involved? > > Java is the stack. I'm not saying that you shouldn't trust your developers, but they may be using a cursor without being aware of it. If they use "setFetchSize()" to set a fetch size different from 0, they *are* using a cursor. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com