pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Hot Standby Conflict on pg_attribute
7+ messages / 4 participants
[nested] [flat]

* Hot Standby Conflict on pg_attribute
@ 2019-05-09 20:03  Erik Jones <mage2k@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Erik Jones @ 2019-05-09 20:03 UTC (permalink / raw)
  To: pgsql-general

Hello,

A client has recently had a couple of hot standby query conflict pile-ups
around AccessShare lock waits on pg_attribute.  Here is an example from the
log:

Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-1]  sql_error_code = 00000 LOG:  process 118946 still waiting for
AccessShareLock on relation 1249 of database 16401 after 1000.127 ms at
character 92
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-2]  sql_error_code = 00000 DETAIL:  Process holding the lock: 9. Wait
queue: 118948, 118950, 118708, 118818, 118886, 118961, 118960, 118806,
118963, 118959, 118881, 118887, 118878, 118896, 118964, 118965, 118945,
118949, 118946, 118743, 118966, 118947, 118967, 118968.
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-3]  sql_error_code = 00000 STATEMENT:  SELECT uc.id,
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-4]       uc.some_id,
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-5]       uc.utr_id,
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-6]       utr.name
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-7]     FROM usertable1 uc
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-8]     INNER JOIN usertable2 utr
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-9]       ON uc.utr_id = utr.id
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-10]     WHERE uc.some_id = $1
Mar 27 12:06:37 ip-10-0-125-5 7dc68e48_fbd9_41d7_9ab1_65599036dd75[118946]:
[9-11]     ORDER BY name

Relation 1249 is pg_attribute and process 9 that was holding the lock was
RecoveryWalAll process.  I've confirmed that autovacuum had removed some
pages from pg_attribute shortly before this, which happens somewhat
regularly since this client runs a couple thousand REFERSH MATARIALIZED
VIEW queries per day which look to cause inserts and deletes there so it
having an exclusive lock on pg_attribute makes sense.

The question then is: Why would these user queries be waiting on an
AccessShare lock on pg_attribute?  Thus far we've been unable to recreate
any transacitons with the above query (and others) that show any
pg_attribute locks.  There is no ORM in play here and these queries are
being sent as single query transactions via this Node.js postgres adapter:
https://github.com/brianc/node-postgres which is pretty bare bones.

-- 
Erik Jones
mage2k@gmail.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Hot Standby Conflict on pg_attribute
@ 2019-05-10 19:14  Andres Freund <andres@anarazel.de>
  parent: Erik Jones <mage2k@gmail.com>
  0 siblings, 2 replies; 7+ messages in thread

From: Andres Freund @ 2019-05-10 19:14 UTC (permalink / raw)
  To: Erik Jones <mage2k@gmail.com>; +Cc: pgsql-general

Hi,

On 2019-05-09 13:03:50 -0700, Erik Jones wrote:
> The question then is: Why would these user queries be waiting on an
> AccessShare lock on pg_attribute?  Thus far we've been unable to recreate
> any transacitons with the above query (and others) that show any
> pg_attribute locks.  There is no ORM in play here and these queries are
> being sent as single query transactions via this Node.js postgres adapter:
> https://github.com/brianc/node-postgres which is pretty bare bones.

Queries that access a table for the *first* time after DDL happened
(including truncating the relation), need an AccessShareLock on
pg_attribute (and pg_class, pg_index, ...) for a short time.

You can reproduce that fairly easily:

S1: CREATE TABLE foo();
S2: BEGIN; LOCK pg_attribute;
S1: SELECT * FROM foo;
S2: COMMIT;

S1 could execute the select, because it has a cached view of the way the
relation looks.

S2: ALTER TABLE foo ADD COLUMN bar INT;
S2: BEGIN; LOCK pg_attribute;
S1: SELECT * FROM foo;

Here S1 is blocked, because it needs to look at pg_attribute to figure
out the "shape" of the table, but it's currently locked.

Greetings,

Andres Freund





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Hot Standby Conflict on pg_attribute
@ 2019-05-10 19:23  Erik Jones <mage2k@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 0 replies; 7+ messages in thread

From: Erik Jones @ 2019-05-10 19:23 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-general

Hi Andres,

Thank you very much!  That's exactly what I needed.

On Fri, May 10, 2019 at 12:14 PM Andres Freund <andres@anarazel.de> wrote:

> Hi,
>
> On 2019-05-09 13:03:50 -0700, Erik Jones wrote:
> > The question then is: Why would these user queries be waiting on an
> > AccessShare lock on pg_attribute?  Thus far we've been unable to recreate
> > any transacitons with the above query (and others) that show any
> > pg_attribute locks.  There is no ORM in play here and these queries are
> > being sent as single query transactions via this Node.js postgres
> adapter:
> > https://github.com/brianc/node-postgres which is pretty bare bones.
>
> Queries that access a table for the *first* time after DDL happened
> (including truncating the relation), need an AccessShareLock on
> pg_attribute (and pg_class, pg_index, ...) for a short time.
>
> You can reproduce that fairly easily:
>
> S1: CREATE TABLE foo();
> S2: BEGIN; LOCK pg_attribute;
> S1: SELECT * FROM foo;
> S2: COMMIT;
>
> S1 could execute the select, because it has a cached view of the way the
> relation looks.
>
> S2: ALTER TABLE foo ADD COLUMN bar INT;
> S2: BEGIN; LOCK pg_attribute;
> S1: SELECT * FROM foo;
>
> Here S1 is blocked, because it needs to look at pg_attribute to figure
> out the "shape" of the table, but it's currently locked.
>
> Greetings,
>
> Andres Freund
>


-- 
Erik Jones
mage2k@gmail.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Hot Standby Conflict on pg_attribute
@ 2019-05-10 19:40  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 1 reply; 7+ messages in thread

From: Tom Lane @ 2019-05-10 19:40 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Erik Jones <mage2k@gmail.com>; pgsql-general

Andres Freund <andres@anarazel.de> writes:
> On 2019-05-09 13:03:50 -0700, Erik Jones wrote:
>> The question then is: Why would these user queries be waiting on an
>> AccessShare lock on pg_attribute?

> Queries that access a table for the *first* time after DDL happened
> (including truncating the relation), need an AccessShareLock on
> pg_attribute (and pg_class, pg_index, ...) for a short time.

Also, it seems likely that what's really triggering the issue is
autovacuum on pg_attribute trying to truncate off empty pages
in pg_attribute (after a bunch of dead rows were generated there
by DDL activity).  That requires exclusive lock on pg_attribute,
which would propagate down to the standby.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Hot Standby Conflict on pg_attribute
@ 2019-05-10 19:46  Erik Jones <mage2k@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Erik Jones @ 2019-05-10 19:46 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-general

On Fri, May 10, 2019 at 12:41 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Andres Freund <andres@anarazel.de> writes:
> > On 2019-05-09 13:03:50 -0700, Erik Jones wrote:
> >> The question then is: Why would these user queries be waiting on an
> >> AccessShare lock on pg_attribute?
>
> > Queries that access a table for the *first* time after DDL happened
> > (including truncating the relation), need an AccessShareLock on
> > pg_attribute (and pg_class, pg_index, ...) for a short time.
>
> Also, it seems likely that what's really triggering the issue is
> autovacuum on pg_attribute trying to truncate off empty pages
> in pg_attribute (after a bunch of dead rows were generated there
> by DDL activity).  That requires exclusive lock on pg_attribute,
> which would propagate down to the standby.
>
>                         regards, tom lane
>

Right, that part I understood after checking out pg_attribute's
insert/delete counts in pg_stat_sys_tables before and after some REFRESH
MATERIALIZED VIEW runs on an otherwise idle server.  With them running 2k+
refreshes per day autovac is regularly working on their catalog tables.

Thanks!
-- 
Erik Jones
mage2k@gmail.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Hot Standby Conflict on pg_attribute
@ 2019-05-11 16:15  Jeremy Schneider <schneider@ardentperf.com>
  parent: Erik Jones <mage2k@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Jeremy Schneider @ 2019-05-11 16:15 UTC (permalink / raw)
  To: Erik Jones <mage2k@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andres Freund <andres@anarazel.de>; pgsql-general

Just a quick footnote: If autovac truncations are frequently causing replica lag, and if this is a problem for you, IIUC one way you can stop autovac from doing the truncations even on older versions is setting old_snapshot_threshold to any value at all besides zero.  (On 12+ you can directly control the truncation behavior.)

-Jeremy

Sent from my TI-83

> On May 10, 2019, at 12:46, Erik Jones <mage2k@gmail.com> wrote:
> 
>> On Fri, May 10, 2019 at 12:41 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
>> Andres Freund <andres@anarazel.de> writes:
>> > On 2019-05-09 13:03:50 -0700, Erik Jones wrote:
>> >> The question then is: Why would these user queries be waiting on an
>> >> AccessShare lock on pg_attribute?
>> 
>> > Queries that access a table for the *first* time after DDL happened
>> > (including truncating the relation), need an AccessShareLock on
>> > pg_attribute (and pg_class, pg_index, ...) for a short time.
>> 
>> Also, it seems likely that what's really triggering the issue is
>> autovacuum on pg_attribute trying to truncate off empty pages
>> in pg_attribute (after a bunch of dead rows were generated there
>> by DDL activity).  That requires exclusive lock on pg_attribute,
>> which would propagate down to the standby.
>> 
>>                         regards, tom lane
> 
> Right, that part I understood after checking out pg_attribute's insert/delete counts in pg_stat_sys_tables before and after some REFRESH MATERIALIZED VIEW runs on an otherwise idle server.  With them running 2k+ refreshes per day autovac is regularly working on their catalog tables.
> 
> Thanks!
> -- 
> Erik Jones
> mage2k@gmail.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Hot Standby Conflict on pg_attribute
@ 2019-05-11 17:19  Erik Jones <mage2k@gmail.com>
  parent: Jeremy Schneider <schneider@ardentperf.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Erik Jones @ 2019-05-11 17:19 UTC (permalink / raw)
  To: Jeremy Schneider <schneider@ardentperf.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andres Freund <andres@anarazel.de>; pgsql-general

Thanks for the ti

On Sat, May 11, 2019 at 9:15 AM Jeremy Schneider <schneider@ardentperf.com>
wrote:

> Just a quick footnote: If autovac truncations are frequently causing
> replica lag, and if this is a problem for you, IIUC one way you can stop
> autovac from doing the truncations even on older versions is setting
> old_snapshot_threshold to any value at all besides zero.  (On 12+ you can
> directly control the truncation behavior.)
>
> -Jeremy
>

Thanks for the tip!

-- 
Erik Jones
mage2k@gmail.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2019-05-11 17:19 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-05-09 20:03 Hot Standby Conflict on pg_attribute Erik Jones <mage2k@gmail.com>
2019-05-10 19:14 ` Andres Freund <andres@anarazel.de>
2019-05-10 19:23   ` Erik Jones <mage2k@gmail.com>
2019-05-10 19:40   ` Tom Lane <tgl@sss.pgh.pa.us>
2019-05-10 19:46     ` Erik Jones <mage2k@gmail.com>
2019-05-11 16:15       ` Jeremy Schneider <schneider@ardentperf.com>
2019-05-11 17:19         ` Erik Jones <mage2k@gmail.com>

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