SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
Understanding PostgreSQL LWLocks



                  Jignesh Shah
           Staff Engineer, VMware Inc

              PgCon 2011 - Ottawa
About MySelf

§  Joined	
  VMware	
  in	
  	
  2010	
  	
  
     q  	
  PostgreSQL	
  performance	
  on	
  vSphere	
  

§  Previously	
  at	
  Sun	
  Microsystems	
  from	
  2000-­‐2010	
  
     q  Database	
  Performance	
  on	
  Solaris/Sun	
  Systems	
  

§  Work	
  with	
  	
  PostgreSQL	
  Performance	
  Community	
  	
  
     q  Scaling,	
  BoIlenecks	
  using	
  various	
  workloads	
  

§  My	
  Blog:	
  hIp://jkshah.blogspot.com	
  
	
  
	
  
	
  


                           © 2011 VMware Inc                             2
Content

v What	
  are	
  PostgreSQL	
  LWLocks	
  ?	
  
v Understanding	
  modes	
  of	
  LWLock	
  
v LWLocks	
  API	
  
v Architecture	
  of	
  the	
  LWLocks	
  Framework	
  	
  
v 	
  Internals	
  of	
  LWLock	
  Wait	
  List	
  	
  
v Ways	
  to	
  monitors	
  the	
  events	
  related	
  to	
  LWLocks	
  
v Top	
  LWLocks.	
  	
  
	
  
	
  
	
  
	
  
	
                            © 2011 VMware Inc                              3
What are PostgreSQL LWLocks?

§  LWLocks	
  –	
  Light	
  Weight	
  Locks	
  of	
  PostgreSQL	
  
§  Primary	
  Intent	
  
     q  Mutually	
  Exclusive	
  access	
  to	
  shared	
  memory	
  structures	
  

§  Offers	
  Shared	
  and	
  Exclusive	
  mode	
  
§  Not	
  to	
  be	
  confused	
  with	
  the	
  PostgreSQL	
  Locks	
  	
  
§  PostgreSQL	
  Locks	
  depends	
  on	
  LWLocks	
  to	
  protect	
  its	
  shared	
  
    state	
  
§  Uses	
  SpinLockAcquire/SpinLockRelease	
  and	
  
    PGSemaphoreLock/PGSemaphoreUnlock	
  



                             © 2011 VMware Inc                                   4
Understanding Modes of LWLocks

ExclusiveLock	
  
§ 	
   For	
  a	
  parcular	
  lockid	
  	
  there	
  can	
  be	
  only	
  1	
  exclusivelock	
  held	
  
     	
   (no	
  shared	
  locks	
  and	
  no	
  other	
  exclusive	
  lock	
  for	
  the	
  lockid)	
  
	
   	
  
SharedLock	
  
•  For	
  a	
  parcular	
  lockid	
  there	
  can	
  be	
  1	
  or	
  more	
  SharedLock	
  held	
  
          (no	
  exclusive	
  lock	
  for	
  the	
  lockid)	
  
	
  
	
  
	
  
	
  

                                    © 2011 VMware Inc                                            5
LWLocks API

§  LWLockAssign(void)	
  
       §  Get	
  a	
  dynamic	
  LWLock	
  number	
  (mostly	
  at	
  module	
  load)	
  
§  LWLockAcquire(LWLockId	
  lockid,	
  LWLockMode	
  mode)	
  
       §  Waits	
  ll	
  Lock	
  is	
  acquired	
  	
  in	
  EXCLUSIVE	
  or	
  SHARED	
  mode	
  
§  LWLockCondionalAcquire(LWLockId	
  lockid,	
  LWLockMode	
  mode)	
  
       §  Returns	
  False	
  if	
  it	
  not	
  available	
  (Non-­‐blocking)	
  
§  LWLockRelease(LWLockId	
  lockid)	
  
       §  Releases	
  the	
  lock	
  
§  LWLockReleaseAll	
  (void)	
  
       §  Used	
  aaer	
  an	
  ERROR	
  to	
  cleanup	
  
§  LWLockHeldByMe	
  (LWLockId	
  lockid)	
  
       §  Debug	
  support	
  test	
  
	
  
	
                                  © 2011 VMware Inc                                                  6
Architecture Flow of LWLocks

LWLockCondionalAcquire	
  	
  
•  Easy	
  lock	
  it	
  if	
  available	
  else	
  return	
  false	
  (Non-­‐blocking)	
  
	
  
LWLockAcquire	
  
	
  
•  Lock	
  	
  if	
  available	
  or	
  else	
  put	
  me	
  in	
  	
  wait	
  queue	
  (FIFO)	
  and	
  	
  got	
  
	
  
     to	
  sleep	
  on	
  a	
  semaphore	
  (and	
  loop)	
  
•  Only	
  return	
  when	
  lock	
  is	
  available	
  (Blocking)	
  
LWLockRelease	
  
•  Release	
  the	
  Lock	
  and	
  if	
  there	
  are	
  no	
  more	
  shared	
  locks	
  
     remaining	
  then	
  wake	
  up	
  the	
  next	
  process	
  waing	
  in	
  the	
  wait	
  
     queue	
  
	
  
	
  
	
                                  © 2011 VMware Inc                                               7
Wait Queue List

•  Wait	
  Queue	
  is	
  protected	
  by	
  a	
  mutex	
  which	
  is	
  accessed	
  using	
  
	
   SpinLocks	
  
•  When	
  the	
  lock	
  is	
  released	
  and	
  there	
  are	
  no	
  more	
  shared	
  locks	
  
	
  
     pending	
  then	
  the	
  process	
  releasing	
  the	
  lock	
  will	
  wake	
  the	
  next	
  
	
  
     waiter	
  as	
  follows	
  
        •  If	
  the	
  next	
  process	
  in	
  wait	
  queue	
  is	
  waing	
  for	
  an	
  
           EXCLUSIVE	
  mode	
  ,	
  only	
  that	
  process	
  is	
  removed	
  from	
  
           the	
  queue	
  and	
  wakes	
  them	
  up	
  
        •  Or	
  since	
  the	
  next	
  process	
  is	
  waing	
  for	
  SHARED	
  mode,	
  it	
  
           will	
  try	
  to	
  	
  remove	
  	
  as	
  many	
  consecuve	
  SHARED	
  mode	
  
           processes	
  and	
  wake	
  them	
  up	
  


                                © 2011 VMware Inc                                        8
Lock Acquisition Internals

	
  	
     Spins	
  on	
  	
  ‘lock’	
  mutex	
                     Sleep	
  on	
  	
  its	
  process	
  
                                                                    wait	
  semaphore
	
  
	
  
	
                        Lock	
                         •    If	
  Not	
  Add	
  to	
  Wait	
  Queue	
  List	
  
	
                        Available	
                    •    Release	
  ‘lock’	
  mutex	
  

	
  


            Release	
  	
  ‘lock’	
  mutex	
  




                                                    © 2011 VMware Inc                                               9
Lock Release Internals

	
  	
   Spins	
  on	
  	
  ‘lock’	
  mutex	
  
	
  
	
   Release	
  Lock	
  
	
  
	
                                                      •    Wake	
  Up	
  	
  process	
  next	
  in	
  FIFO	
  

	
          Shared	
                                    •    If	
  it	
  is	
  Shared	
  wake	
  up	
  all	
  
                                                             sequenal	
  shared	
  waiters
                 Locks	
  ?	
  




        Release	
  	
  ‘lock’	
  mutex	
  



                                                  © 2011 VMware Inc                                                10
LWLocks Wait Queue List FIFO Example

•  Consider	
  something	
  80-­‐20	
  Shared	
  /Exclusive	
  rao	
  
	
   	
  
	
   	
   S S S S E S E S S S
     S

	
   	
  
     It	
  will	
  wake	
  up	
  first	
  5	
  shared	
  locks	
  then	
  wait	
  ll	
  all	
  of	
  them	
  
           releases	
  the	
  lock	
  before	
  waking	
  up	
  the	
  process	
  asking	
  
           for	
  Exclusive	
  
     It	
  will	
  do	
  only	
  exclusive	
  one	
  then	
  and	
  one	
  it	
  releases	
  the	
  
           lock,	
  wakes	
  up	
  the	
  next	
  1	
  shared	
  one	
  
     	
  



                                  © 2011 VMware Inc                                              11
Observations about LWLocks Wait Queue List

•  All	
  Operaons	
  on	
  Wait	
  Queue	
  List	
  are	
  serialized	
  	
  
	
        •  Not	
  scalable	
  on	
  SMP	
  architecture	
  
•  Currently	
  only	
  FIFO	
  supported	
  	
  
	
  
•  No	
  restricon	
  on	
  shared	
  wakeups	
  	
  
	
  
          	
  




                               © 2011 VMware Inc                                  12
LWLocks – Defined in lwlock.h
typedef enum LWLockId
{
    BufFreelistLock,          ShmemIndexLock,       OidGenLock,
    	
  
    XidGenLock,
    SInvalWriteLock,
                              ProcArrayLock,
                              WALInsertLock,
                                                    SInvalReadLock,
                                                    WALWriteLock,
    ControlFileLock,          CheckpointLock,       CLogControlLock,
    	
  
    SubtransControlLock,      MultiXactGenLock,     MultiXactOffsetControlLock,
    MultiXactMemberControlLock,   RelCacheInitLock, BgWriterCommLock,
    	
  
    TwoPhaseStateLock,        TablespaceCreateLock, BtreeVacuumLock,
    AddinShmemInitLock,       AutovacuumLock,       AutovacuumScheduleLock,
    SyncScanLock,             RelationMappingLock, AsyncCtlLock,
    AsyncQueueLock,      SerializableXactHashLock, SerializableFinishedListLock,
    SerializablePredicateLockListLock, OldSerXidLock,   SyncRepLock,
    /* Individual lock IDs end here */
    FirstBufMappingLock,
    FirstLockMgrLock = FirstBufMappingLock + NUM_BUFFER_PARTITIONS, /*16 */
    FirstPredicateLockMgrLock = FirstLockMgrLock + NUM_LOCK_PARTITIONS,
    /* must be last except for MaxDynamicLWLock: */
    NumFixedLWLocks = FirstPredicateLockMgrLock + NUM_PREDICATELOCK_PARTITIONS,
    MaxDynamicLWLock = 1000000000
} LWLockId;




                           © 2011 VMware Inc                           13
Monitoring

LWLOCK_STAT	
  
•  Special	
  build	
  with	
  LWLOCK_STAT	
  defined	
  
	
  
LOCK_DEBUG	
  
	
  
•  Puts	
  debug	
  messages	
  in	
  system	
  alert	
  log	
  
	
  
DYNAMIC	
  TRACING	
  (DTrace,	
  SystemTap)	
  	
  
•  Recommended	
  
•  Useful	
  to	
  find	
  hot	
  locks	
  
•  Not	
  for	
  producon	
  use,	
  because	
  it	
  can	
  make	
  the	
  server	
  
     unstable	
  




                              © 2011 VMware Inc                                       14
Dynamic Tracing for LWLocks

•      postgresql-­‐lwlock-­‐wait-­‐start(lockid,mode)	
  
• 
	
     Postgresql-­‐lwlock-­‐wait-­‐done(lockid,mode)	
  
• 
	
     Postgresql-­‐lwlock-­‐acquire(lockid,mode)	
  
• 
	
     Postgresql-­‐lwlock-­‐condacquire(lockid,mode)	
  
•      Postgresql-­‐lwlock-­‐condacquire-­‐fail(lockid,mode)	
  
•      Postgresql-­‐lwlock-­‐release(lockid)	
  




                           © 2011 VMware Inc                       15
Example Monitoring using Systemtap
(DBT2)

       LOCKNAME   LWID   M W/A       COUNT      SUM-TIME(us) MAX-TIME(us) AVG-TIME(us)
  WALInsertLock      7 Ex     W      14013           2746505       3955        195
   WALWriteLock      8 Ex     W      10006          25508653     286749       2549
    LockMgrLock     55 Ex     W       2035            203429       3323         99
    LockMgrLock     45 Ex     W        932             54297       2860         58
    LockMgrLock     54 Ex     W        673             24362       1062         36
  ProcArrayLock      4 Ex     W        515             15907        666         30
  ProcArrayLock      4 Sh     W        176              6064         97         34
    LockMgrLock     56 Ex     W        171              5826        376         34
CLogControlLock     11 Sh     W        111             22490       6127        202
    LockMgrLock     57 Ex     W        101              5524       1326         54
    LockMgrLock     59 Ex     W         79              2883        347         36
CLogControlLock     11 Ex     W         58              8543       4439        147
    LockMgrLock     49 Ex     W         57              1848         76         32
    LockMgrLock     47 Ex     W         57              3166       1468         55




                            © 2011 VMware Inc                                  16
Top Locks - WALWriteLock

•  Protects	
  	
  writes	
  on	
  WAL	
  
•  Acquired	
  when	
  WAL	
  records	
  flushed	
  to	
  disk	
  
	
  
•  Acquired	
  when	
  WAL	
  Log	
  switch	
  occurs	
  
	
  
•  Improve	
  the	
  underlying	
  storage	
  of	
  pg_xlog	
  
	
  
•  Synchronous_commit=off	
  helps	
  indirectly	
  (	
  Do	
  not	
  wait	
  for	
  
   flush	
  to	
  the	
  disk)	
  
•  Full_page_writes=off	
  also	
  helps	
  reduce	
  the	
  stress	
  (but	
  not	
  
   recommended	
  since	
  resiliency	
  goes	
  down)	
  




                            © 2011 VMware Inc                                  17
Top Locks - WALInsertLock

•  Protects	
  WAL	
  Buffers	
  
•  Increasing	
  wal	
  buffers	
  may	
  help	
  though	
  to	
  only	
  certain	
  extent	
  
	
  
•  Snychnrous_commit	
  off	
  will	
  lead	
  to	
  increased	
  pressure	
  on	
  this	
  
	
  
	
   lock	
  (	
  Not	
  a	
  bad	
  thing	
  )	
  
•  However	
  eventually	
  not	
  much	
  can	
  be	
  done	
  once	
  it	
  gets	
  to	
  a	
  
     big	
  problem	
  without	
  new	
  commits	
  
•  Full_page_writes=off	
  certainly	
  helps	
  (again	
  not	
  recommended	
  
     since	
  it	
  reduces	
  the	
  resiliency	
  of	
  the	
  database	
  from	
  write	
  
     errors)	
  




                              © 2011 VMware Inc                                      18
Top Locks - ProcArrayLock

•  Protects	
  ProcArray	
  structure	
  
•  It	
  used	
  to	
  be	
  that	
  every	
  transacon	
  actually	
  acquired	
  this	
  lock	
  
	
  
	
   in	
  exclusive	
  mode	
  before	
  commit	
  causing	
  it	
  to	
  be	
  a	
  top	
  lock	
  
•  Fixed	
  in	
  9.0	
  	
  
	
  




                               © 2011 VMware Inc                                          19
Top Locks - SInvalidReadLock

•  Protects	
  sinval	
  array	
  	
  
•  Readers	
  take	
  “Shared”	
  SInvalReadLock	
  
	
  
•  SICleanupQueue	
  and	
  other	
  array-­‐wide	
  updates	
  take	
  
	
  
	
   “Excluslive”	
  SInvalReadLock	
  to	
  lock	
  out	
  all	
  readers	
  
•  Long	
  wait	
  mes	
  to	
  acquire	
  SInvalidReadLock	
  	
  generally	
  results	
  
     when	
  the	
  	
  Shared	
  Buffer	
  pool	
  is	
  being	
  stressed	
  	
  
•  Increase	
  shared_buffers	
  in	
  postgresql.conf	
  corresponding	
  to	
  
     acve	
  data	
  size	
  
	
  




                             © 2011 VMware Inc                                   20
Top Locks - CLogControlLock

•  Protects	
  CLogControl	
  structure	
  
•  Generally	
  not	
  a	
  problem	
  	
  
	
  
•  If	
  it	
  shows	
  on	
  the	
  top	
  lists	
  check	
  
	
  
	
         •  $PGDATA/pg_clog	
  should	
  be	
  on	
  buffered	
  file	
  system	
  




                           © 2011 VMware Inc                                  21
Example Monitoring using Systemtap
(Sysbench simple read)

     LOCKNAME   LWID   M W/A       COUNT     SUM-TIME(us) MAX-TIME(us) AVG-TIME(us)
  LockMgrLock    45 Ex     W      85343        469682510      13152       5503
  LockMgrLock    57 Ex     W      57547         30903727       8313        537
  LockMgrLock    44 Ex     W        390            34061       1670         87
  LockMgrLock    59 Ex     W        375            41570       2032        110
  LockMgrLock    56 Ex     W        361            39685       1889        109
  LockMgrLock    47 Ex     W        344            24548       1564         71
  LockMgrLock    54 Ex     W        335            67770       2319        202
  LockMgrLock    50 Ex     W        325            44213       1690        136
  LockMgrLock    49 Ex     W        325            39280       1475        120
  LockMgrLock    55 Ex     W        323            39448       1584        122
  LockMgrLock    48 Ex     W        323            26982       1669         83




                         © 2011 VMware Inc                                 22
Top Locks - LockMgrLocks

•  Protects	
  relaons	
  
•  Sets	
  of	
  about	
  16	
  Lock	
  Parons	
  by	
  default	
  to	
  handle	
  all	
  
	
  
	
   relaons	
  
•  Each	
  relaon	
  is	
  part	
  of	
  only	
  one	
  paron	
  (irrespecve	
  of	
  size)	
  
	
  
	
  




                               © 2011 VMware Inc                                        23
Other Locks - BufMappingLocks

•  Protects	
  regions	
  of	
  Buffers	
  
•  Sets	
  of	
  about	
  16	
  Regions	
  of	
  Buffers	
  by	
  default	
  to	
  handle	
  the	
  
	
  
	
   whole	
  Bufferpool	
  
•  Only	
  taken	
  shared	
  access	
  
	
  

	
  




                                © 2011 VMware Inc                                          24
Problems with LWLock

•  Overall	
  system	
  gravitates	
  to	
  certain	
  top	
  locks	
  
•  Single	
  mutex	
  lock	
  protects	
  adding	
  and	
  depleng	
  the	
  wait	
  
	
  
	
   queue	
  	
  
	
      •  Not	
  SMP	
  Scalable	
  
        •  Chance	
  for	
  opmizaon	
  out	
  there	
  
        •  Performance	
  limited	
  to	
  serialized	
  rate	
  the	
  locks	
  are	
  
           processed	
  
	
  




                             © 2011 VMware Inc                                      25
Questions / More Information

v Email:	
  jshah@vmware.com	
  
v Learn	
  more	
  about	
  PostgreSQL	
  
    q  hIp://www.postgresql.org	
  

v Blog:	
  hIp://jkshah.blogspot.com	
  	
  
	
  




                         © 2011 VMware Inc      26

Contenu connexe

Tendances

Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
Simon Huang
 
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
PostgreSQL-Consulting
 

Tendances (20)

PostgreSQLのfull_page_writesについて(第24回PostgreSQLアンカンファレンス@オンライン 発表資料)
PostgreSQLのfull_page_writesについて(第24回PostgreSQLアンカンファレンス@オンライン 発表資料)PostgreSQLのfull_page_writesについて(第24回PostgreSQLアンカンファレンス@オンライン 発表資料)
PostgreSQLのfull_page_writesについて(第24回PostgreSQLアンカンファレンス@オンライン 発表資料)
 
YugaByte DB Internals - Storage Engine and Transactions
YugaByte DB Internals - Storage Engine and Transactions YugaByte DB Internals - Storage Engine and Transactions
YugaByte DB Internals - Storage Engine and Transactions
 
PostgreSQLの統計情報について(第26回PostgreSQLアンカンファレンス@オンライン 発表資料)
PostgreSQLの統計情報について(第26回PostgreSQLアンカンファレンス@オンライン 発表資料)PostgreSQLの統計情報について(第26回PostgreSQLアンカンファレンス@オンライン 発表資料)
PostgreSQLの統計情報について(第26回PostgreSQLアンカンファレンス@オンライン 発表資料)
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
 
RocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesRocksDB Performance and Reliability Practices
RocksDB Performance and Reliability Practices
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
MyRocks Deep Dive
MyRocks Deep DiveMyRocks Deep Dive
MyRocks Deep Dive
 
PostgreSQLの運用・監視にまつわるエトセトラ
PostgreSQLの運用・監視にまつわるエトセトラPostgreSQLの運用・監視にまつわるエトセトラ
PostgreSQLの運用・監視にまつわるエトセトラ
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
押さえておきたい、PostgreSQL 13 の新機能!!(Open Source Conference 2021 Online/Hokkaido 発表資料)
押さえておきたい、PostgreSQL 13 の新機能!!(Open Source Conference 2021 Online/Hokkaido 発表資料)押さえておきたい、PostgreSQL 13 の新機能!!(Open Source Conference 2021 Online/Hokkaido 発表資料)
押さえておきたい、PostgreSQL 13 の新機能!!(Open Source Conference 2021 Online/Hokkaido 発表資料)
 
Problems with PostgreSQL on Multi-core Systems with MultiTerabyte Data
Problems with PostgreSQL on Multi-core Systems with MultiTerabyte DataProblems with PostgreSQL on Multi-core Systems with MultiTerabyte Data
Problems with PostgreSQL on Multi-core Systems with MultiTerabyte Data
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
 
InnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick FiguresInnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick Figures
 
Wait! What’s going on inside my database?
Wait! What’s going on inside my database?Wait! What’s going on inside my database?
Wait! What’s going on inside my database?
 
祝!PostgreSQLレプリケーション10周年!徹底紹介!!
祝!PostgreSQLレプリケーション10周年!徹底紹介!!祝!PostgreSQLレプリケーション10周年!徹底紹介!!
祝!PostgreSQLレプリケーション10周年!徹底紹介!!
 
Introduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparoundIntroduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparound
 
Redefining tables online without surprises
Redefining tables online without surprisesRedefining tables online without surprises
Redefining tables online without surprises
 
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
 
Enabling Vectorized Engine in Apache Spark
Enabling Vectorized Engine in Apache SparkEnabling Vectorized Engine in Apache Spark
Enabling Vectorized Engine in Apache Spark
 

En vedette

OLTP Performance Benchmark Review
OLTP Performance Benchmark ReviewOLTP Performance Benchmark Review
OLTP Performance Benchmark Review
Jignesh Shah
 
Introduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System AdministratorsIntroduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System Administrators
Jignesh Shah
 
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized EnvironmentsBest Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Jignesh Shah
 
Inside vacuum - 第一回PostgreSQLプレ勉強会
Inside vacuum - 第一回PostgreSQLプレ勉強会Inside vacuum - 第一回PostgreSQLプレ勉強会
Inside vacuum - 第一回PostgreSQLプレ勉強会
Masahiko Sawada
 

En vedette (16)

Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 
Tuning DB2 in a Solaris Environment
Tuning DB2 in a Solaris EnvironmentTuning DB2 in a Solaris Environment
Tuning DB2 in a Solaris Environment
 
Best Practices with PostgreSQL on Solaris
Best Practices with PostgreSQL on SolarisBest Practices with PostgreSQL on Solaris
Best Practices with PostgreSQL on Solaris
 
SFPUG - DVDStore Performance Benchmark and PostgreSQL
SFPUG - DVDStore Performance Benchmark and PostgreSQLSFPUG - DVDStore Performance Benchmark and PostgreSQL
SFPUG - DVDStore Performance Benchmark and PostgreSQL
 
OLTP Performance Benchmark Review
OLTP Performance Benchmark ReviewOLTP Performance Benchmark Review
OLTP Performance Benchmark Review
 
Introduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System AdministratorsIntroduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System Administrators
 
My experience with embedding PostgreSQL
 My experience with embedding PostgreSQL My experience with embedding PostgreSQL
My experience with embedding PostgreSQL
 
Best Practices of running PostgreSQL in Virtual Environments
Best Practices of running PostgreSQL in Virtual EnvironmentsBest Practices of running PostgreSQL in Virtual Environments
Best Practices of running PostgreSQL in Virtual Environments
 
PostgreSQL and Linux Containers
PostgreSQL and Linux ContainersPostgreSQL and Linux Containers
PostgreSQL and Linux Containers
 
Introduction to PgBench
Introduction to PgBenchIntroduction to PgBench
Introduction to PgBench
 
PostgreSQL and Benchmarks
PostgreSQL and BenchmarksPostgreSQL and Benchmarks
PostgreSQL and Benchmarks
 
PostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldPostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized World
 
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized EnvironmentsBest Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
 
Inside vacuum - 第一回PostgreSQLプレ勉強会
Inside vacuum - 第一回PostgreSQLプレ勉強会Inside vacuum - 第一回PostgreSQLプレ勉強会
Inside vacuum - 第一回PostgreSQLプレ勉強会
 
画像処理ライブラリ OpenCV で 出来ること・出来ないこと
画像処理ライブラリ OpenCV で 出来ること・出来ないこと画像処理ライブラリ OpenCV で 出来ること・出来ないこと
画像処理ライブラリ OpenCV で 出来ること・出来ないこと
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 

Similaire à Understanding PostgreSQL LW Locks

Nexus1000V on KVM and OpenStack Integration
Nexus1000V on KVM and OpenStack IntegrationNexus1000V on KVM and OpenStack Integration
Nexus1000V on KVM and OpenStack Integration
openstackindia
 
[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex
[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex
[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex
James Chen
 
READPAST & Furious: Locking
READPAST & Furious: Locking READPAST & Furious: Locking
READPAST & Furious: Locking
Mark Broadbent
 
A Practical Look at SystemVerilog Coverage
A Practical Look at SystemVerilog CoverageA Practical Look at SystemVerilog Coverage
A Practical Look at SystemVerilog Coverage
DVClub
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
mukul bhardwaj
 

Similaire à Understanding PostgreSQL LW Locks (20)

The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!
 
Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
 
sanlock overview and its consensus algorithms
sanlock overview and its consensus algorithmssanlock overview and its consensus algorithms
sanlock overview and its consensus algorithms
 
OSGi & Java EE in GlassFish - Best of both worlds
OSGi & Java EE in GlassFish - Best of both worldsOSGi & Java EE in GlassFish - Best of both worlds
OSGi & Java EE in GlassFish - Best of both worlds
 
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
 
Locks
LocksLocks
Locks
 
[4DEV][Łódź] Michał Szynkiewicz - WildFly Swarm: JavaEE w świecie mikroserwisów
[4DEV][Łódź] Michał Szynkiewicz - WildFly Swarm: JavaEE w świecie mikroserwisów[4DEV][Łódź] Michał Szynkiewicz - WildFly Swarm: JavaEE w świecie mikroserwisów
[4DEV][Łódź] Michał Szynkiewicz - WildFly Swarm: JavaEE w świecie mikroserwisów
 
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
OSGi-enabled Java EE Applications using GlassFish at JCertif 2011
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!
 
Persistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed DurabilityPersistence Is Futile - Implementing Delayed Durability
Persistence Is Futile - Implementing Delayed Durability
 
OSGi-enabled Java EE Applications using GlassFish
OSGi-enabled Java EE Applications using GlassFishOSGi-enabled Java EE Applications using GlassFish
OSGi-enabled Java EE Applications using GlassFish
 
Nexus1000V on KVM and OpenStack Integration
Nexus1000V on KVM and OpenStack IntegrationNexus1000V on KVM and OpenStack Integration
Nexus1000V on KVM and OpenStack Integration
 
OSGi-enabled Java EE applications in GlassFish
OSGi-enabled Java EE applications in GlassFishOSGi-enabled Java EE applications in GlassFish
OSGi-enabled Java EE applications in GlassFish
 
[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex
[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex
[Hic2011] using hadoop lucene-solr-for-large-scale-search by systex
 
lock, block & two smoking barrels
lock, block & two smoking barrelslock, block & two smoking barrels
lock, block & two smoking barrels
 
The nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levelsThe nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levels
 
READPAST & Furious: Locking
READPAST & Furious: Locking READPAST & Furious: Locking
READPAST & Furious: Locking
 
Doulos coverage-tips-tricks
Doulos coverage-tips-tricksDoulos coverage-tips-tricks
Doulos coverage-tips-tricks
 
A Practical Look at SystemVerilog Coverage
A Practical Look at SystemVerilog CoverageA Practical Look at SystemVerilog Coverage
A Practical Look at SystemVerilog Coverage
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Understanding PostgreSQL LW Locks

  • 1. Understanding PostgreSQL LWLocks Jignesh Shah Staff Engineer, VMware Inc PgCon 2011 - Ottawa
  • 2. About MySelf §  Joined  VMware  in    2010     q   PostgreSQL  performance  on  vSphere   §  Previously  at  Sun  Microsystems  from  2000-­‐2010   q  Database  Performance  on  Solaris/Sun  Systems   §  Work  with    PostgreSQL  Performance  Community     q  Scaling,  BoIlenecks  using  various  workloads   §  My  Blog:  hIp://jkshah.blogspot.com         © 2011 VMware Inc 2
  • 3. Content v What  are  PostgreSQL  LWLocks  ?   v Understanding  modes  of  LWLock   v LWLocks  API   v Architecture  of  the  LWLocks  Framework     v   Internals  of  LWLock  Wait  List     v Ways  to  monitors  the  events  related  to  LWLocks   v Top  LWLocks.               © 2011 VMware Inc 3
  • 4. What are PostgreSQL LWLocks? §  LWLocks  –  Light  Weight  Locks  of  PostgreSQL   §  Primary  Intent   q  Mutually  Exclusive  access  to  shared  memory  structures   §  Offers  Shared  and  Exclusive  mode   §  Not  to  be  confused  with  the  PostgreSQL  Locks     §  PostgreSQL  Locks  depends  on  LWLocks  to  protect  its  shared   state   §  Uses  SpinLockAcquire/SpinLockRelease  and   PGSemaphoreLock/PGSemaphoreUnlock   © 2011 VMware Inc 4
  • 5. Understanding Modes of LWLocks ExclusiveLock   §    For  a  parcular  lockid    there  can  be  only  1  exclusivelock  held     (no  shared  locks  and  no  other  exclusive  lock  for  the  lockid)       SharedLock   •  For  a  parcular  lockid  there  can  be  1  or  more  SharedLock  held   (no  exclusive  lock  for  the  lockid)           © 2011 VMware Inc 5
  • 6. LWLocks API §  LWLockAssign(void)   §  Get  a  dynamic  LWLock  number  (mostly  at  module  load)   §  LWLockAcquire(LWLockId  lockid,  LWLockMode  mode)   §  Waits  ll  Lock  is  acquired    in  EXCLUSIVE  or  SHARED  mode   §  LWLockCondionalAcquire(LWLockId  lockid,  LWLockMode  mode)   §  Returns  False  if  it  not  available  (Non-­‐blocking)   §  LWLockRelease(LWLockId  lockid)   §  Releases  the  lock   §  LWLockReleaseAll  (void)   §  Used  aaer  an  ERROR  to  cleanup   §  LWLockHeldByMe  (LWLockId  lockid)   §  Debug  support  test       © 2011 VMware Inc 6
  • 7. Architecture Flow of LWLocks LWLockCondionalAcquire     •  Easy  lock  it  if  available  else  return  false  (Non-­‐blocking)     LWLockAcquire     •  Lock    if  available  or  else  put  me  in    wait  queue  (FIFO)  and    got     to  sleep  on  a  semaphore  (and  loop)   •  Only  return  when  lock  is  available  (Blocking)   LWLockRelease   •  Release  the  Lock  and  if  there  are  no  more  shared  locks   remaining  then  wake  up  the  next  process  waing  in  the  wait   queue         © 2011 VMware Inc 7
  • 8. Wait Queue List •  Wait  Queue  is  protected  by  a  mutex  which  is  accessed  using     SpinLocks   •  When  the  lock  is  released  and  there  are  no  more  shared  locks     pending  then  the  process  releasing  the  lock  will  wake  the  next     waiter  as  follows   •  If  the  next  process  in  wait  queue  is  waing  for  an   EXCLUSIVE  mode  ,  only  that  process  is  removed  from   the  queue  and  wakes  them  up   •  Or  since  the  next  process  is  waing  for  SHARED  mode,  it   will  try  to    remove    as  many  consecuve  SHARED  mode   processes  and  wake  them  up   © 2011 VMware Inc 8
  • 9. Lock Acquisition Internals     Spins  on    ‘lock’  mutex   Sleep  on    its  process   wait  semaphore       Lock   •  If  Not  Add  to  Wait  Queue  List     Available   •  Release  ‘lock’  mutex     Release    ‘lock’  mutex   © 2011 VMware Inc 9
  • 10. Lock Release Internals     Spins  on    ‘lock’  mutex       Release  Lock       •  Wake  Up    process  next  in  FIFO     Shared   •  If  it  is  Shared  wake  up  all   sequenal  shared  waiters Locks  ?   Release    ‘lock’  mutex   © 2011 VMware Inc 10
  • 11. LWLocks Wait Queue List FIFO Example •  Consider  something  80-­‐20  Shared  /Exclusive  rao           S S S S E S E S S S S     It  will  wake  up  first  5  shared  locks  then  wait  ll  all  of  them   releases  the  lock  before  waking  up  the  process  asking   for  Exclusive   It  will  do  only  exclusive  one  then  and  one  it  releases  the   lock,  wakes  up  the  next  1  shared  one     © 2011 VMware Inc 11
  • 12. Observations about LWLocks Wait Queue List •  All  Operaons  on  Wait  Queue  List  are  serialized       •  Not  scalable  on  SMP  architecture   •  Currently  only  FIFO  supported       •  No  restricon  on  shared  wakeups         © 2011 VMware Inc 12
  • 13. LWLocks – Defined in lwlock.h typedef enum LWLockId { BufFreelistLock, ShmemIndexLock, OidGenLock,   XidGenLock, SInvalWriteLock, ProcArrayLock, WALInsertLock, SInvalReadLock, WALWriteLock, ControlFileLock, CheckpointLock, CLogControlLock,   SubtransControlLock, MultiXactGenLock, MultiXactOffsetControlLock, MultiXactMemberControlLock, RelCacheInitLock, BgWriterCommLock,   TwoPhaseStateLock, TablespaceCreateLock, BtreeVacuumLock, AddinShmemInitLock, AutovacuumLock, AutovacuumScheduleLock, SyncScanLock, RelationMappingLock, AsyncCtlLock, AsyncQueueLock, SerializableXactHashLock, SerializableFinishedListLock, SerializablePredicateLockListLock, OldSerXidLock, SyncRepLock, /* Individual lock IDs end here */ FirstBufMappingLock, FirstLockMgrLock = FirstBufMappingLock + NUM_BUFFER_PARTITIONS, /*16 */ FirstPredicateLockMgrLock = FirstLockMgrLock + NUM_LOCK_PARTITIONS, /* must be last except for MaxDynamicLWLock: */ NumFixedLWLocks = FirstPredicateLockMgrLock + NUM_PREDICATELOCK_PARTITIONS, MaxDynamicLWLock = 1000000000 } LWLockId; © 2011 VMware Inc 13
  • 14. Monitoring LWLOCK_STAT   •  Special  build  with  LWLOCK_STAT  defined     LOCK_DEBUG     •  Puts  debug  messages  in  system  alert  log     DYNAMIC  TRACING  (DTrace,  SystemTap)     •  Recommended   •  Useful  to  find  hot  locks   •  Not  for  producon  use,  because  it  can  make  the  server   unstable   © 2011 VMware Inc 14
  • 15. Dynamic Tracing for LWLocks •  postgresql-­‐lwlock-­‐wait-­‐start(lockid,mode)   •    Postgresql-­‐lwlock-­‐wait-­‐done(lockid,mode)   •    Postgresql-­‐lwlock-­‐acquire(lockid,mode)   •    Postgresql-­‐lwlock-­‐condacquire(lockid,mode)   •  Postgresql-­‐lwlock-­‐condacquire-­‐fail(lockid,mode)   •  Postgresql-­‐lwlock-­‐release(lockid)   © 2011 VMware Inc 15
  • 16. Example Monitoring using Systemtap (DBT2) LOCKNAME LWID M W/A COUNT SUM-TIME(us) MAX-TIME(us) AVG-TIME(us) WALInsertLock 7 Ex W 14013 2746505 3955 195 WALWriteLock 8 Ex W 10006 25508653 286749 2549 LockMgrLock 55 Ex W 2035 203429 3323 99 LockMgrLock 45 Ex W 932 54297 2860 58 LockMgrLock 54 Ex W 673 24362 1062 36 ProcArrayLock 4 Ex W 515 15907 666 30 ProcArrayLock 4 Sh W 176 6064 97 34 LockMgrLock 56 Ex W 171 5826 376 34 CLogControlLock 11 Sh W 111 22490 6127 202 LockMgrLock 57 Ex W 101 5524 1326 54 LockMgrLock 59 Ex W 79 2883 347 36 CLogControlLock 11 Ex W 58 8543 4439 147 LockMgrLock 49 Ex W 57 1848 76 32 LockMgrLock 47 Ex W 57 3166 1468 55 © 2011 VMware Inc 16
  • 17. Top Locks - WALWriteLock •  Protects    writes  on  WAL   •  Acquired  when  WAL  records  flushed  to  disk     •  Acquired  when  WAL  Log  switch  occurs     •  Improve  the  underlying  storage  of  pg_xlog     •  Synchronous_commit=off  helps  indirectly  (  Do  not  wait  for   flush  to  the  disk)   •  Full_page_writes=off  also  helps  reduce  the  stress  (but  not   recommended  since  resiliency  goes  down)   © 2011 VMware Inc 17
  • 18. Top Locks - WALInsertLock •  Protects  WAL  Buffers   •  Increasing  wal  buffers  may  help  though  to  only  certain  extent     •  Snychnrous_commit  off  will  lead  to  increased  pressure  on  this       lock  (  Not  a  bad  thing  )   •  However  eventually  not  much  can  be  done  once  it  gets  to  a   big  problem  without  new  commits   •  Full_page_writes=off  certainly  helps  (again  not  recommended   since  it  reduces  the  resiliency  of  the  database  from  write   errors)   © 2011 VMware Inc 18
  • 19. Top Locks - ProcArrayLock •  Protects  ProcArray  structure   •  It  used  to  be  that  every  transacon  actually  acquired  this  lock       in  exclusive  mode  before  commit  causing  it  to  be  a  top  lock   •  Fixed  in  9.0       © 2011 VMware Inc 19
  • 20. Top Locks - SInvalidReadLock •  Protects  sinval  array     •  Readers  take  “Shared”  SInvalReadLock     •  SICleanupQueue  and  other  array-­‐wide  updates  take       “Excluslive”  SInvalReadLock  to  lock  out  all  readers   •  Long  wait  mes  to  acquire  SInvalidReadLock    generally  results   when  the    Shared  Buffer  pool  is  being  stressed     •  Increase  shared_buffers  in  postgresql.conf  corresponding  to   acve  data  size     © 2011 VMware Inc 20
  • 21. Top Locks - CLogControlLock •  Protects  CLogControl  structure   •  Generally  not  a  problem       •  If  it  shows  on  the  top  lists  check       •  $PGDATA/pg_clog  should  be  on  buffered  file  system   © 2011 VMware Inc 21
  • 22. Example Monitoring using Systemtap (Sysbench simple read) LOCKNAME LWID M W/A COUNT SUM-TIME(us) MAX-TIME(us) AVG-TIME(us) LockMgrLock 45 Ex W 85343 469682510 13152 5503 LockMgrLock 57 Ex W 57547 30903727 8313 537 LockMgrLock 44 Ex W 390 34061 1670 87 LockMgrLock 59 Ex W 375 41570 2032 110 LockMgrLock 56 Ex W 361 39685 1889 109 LockMgrLock 47 Ex W 344 24548 1564 71 LockMgrLock 54 Ex W 335 67770 2319 202 LockMgrLock 50 Ex W 325 44213 1690 136 LockMgrLock 49 Ex W 325 39280 1475 120 LockMgrLock 55 Ex W 323 39448 1584 122 LockMgrLock 48 Ex W 323 26982 1669 83 © 2011 VMware Inc 22
  • 23. Top Locks - LockMgrLocks •  Protects  relaons   •  Sets  of  about  16  Lock  Parons  by  default  to  handle  all       relaons   •  Each  relaon  is  part  of  only  one  paron  (irrespecve  of  size)       © 2011 VMware Inc 23
  • 24. Other Locks - BufMappingLocks •  Protects  regions  of  Buffers   •  Sets  of  about  16  Regions  of  Buffers  by  default  to  handle  the       whole  Bufferpool   •  Only  taken  shared  access       © 2011 VMware Inc 24
  • 25. Problems with LWLock •  Overall  system  gravitates  to  certain  top  locks   •  Single  mutex  lock  protects  adding  and  depleng  the  wait       queue       •  Not  SMP  Scalable   •  Chance  for  opmizaon  out  there   •  Performance  limited  to  serialized  rate  the  locks  are   processed     © 2011 VMware Inc 25
  • 26. Questions / More Information v Email:  jshah@vmware.com   v Learn  more  about  PostgreSQL   q  hIp://www.postgresql.org   v Blog:  hIp://jkshah.blogspot.com       © 2011 VMware Inc 26