pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Dirschel, Steve <steve.dirschel@thomsonreuters.com>
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Cc: Medarametla, Pavan (TR Technology) <pavan.medarametla@thomsonreuters.com>
Subject: Re: [EXT] pg_stat_activity.backend_xmin
Date: Thu, 22 Sep 2022 09:59:28 +0200
Message-ID: <a4eee22d4ffb259fd8122766e83eba6c2ec6ecdf.camel@cybertec.at> (raw)
In-Reply-To: <DM6PR03MB4332604541D21887E44142A3FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com>
References: <DM6PR03MB433207EC1AA2B6E5C3445C49FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com>
	<73c8c16fbee732725f53e291002b694d15932516.camel@cybertec.at>
	<DM6PR03MB4332F78D8B98D802279600C5FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com>
	<A4A3B63D-CB16-4DBC-B134-C57DB8379A7C@gmail.com>
	<DM6PR03MB4332604541D21887E44142A3FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com>

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





view thread (6+ messages)

Message-ID: <a4eee22d4ffb259fd8122766e83eba6c2ec6ecdf.camel@cybertec.at>
Permalink:  ../a4eee22d4ffb259fd8122766e83eba6c2ec6ecdf.camel@cybertec.at/
Also on:    postgresql.org/message-id/a4eee22d4ffb259fd8122766e83eba6c2ec6ecdf.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, steve.dirschel@thomsonreuters.com, robjsargent@gmail.com, pavan.medarametla@thomsonreuters.com
  Subject: Re: [EXT] pg_stat_activity.backend_xmin
  In-Reply-To: <a4eee22d4ffb259fd8122766e83eba6c2ec6ecdf.camel@cybertec.at>
  References: <DM6PR03MB433207EC1AA2B6E5C3445C49FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com> <73c8c16fbee732725f53e291002b694d15932516.camel@cybertec.at> <DM6PR03MB4332F78D8B98D802279600C5FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com> <A4A3B63D-CB16-4DBC-B134-C57DB8379A7C@gmail.com> <DM6PR03MB4332604541D21887E44142A3FA4F9@DM6PR03MB4332.namprd03.prod.outlook.com> <a4eee22d4ffb259fd8122766e83eba6c2ec6ecdf.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox