SlideShare a Scribd company logo
An illustrated guide to
  the buffer cache
               Selena Deckelmann
  Based on Greg Smith’s “Inside the Buffer Cache”
http://www.westnet.com/∼gsmith/content/postgresql
Table

Databases                        Table


                                 Table



     Databases are made of tables...
$PGDATA/base/                 Table




    A table is just a directory...
$PGDATA/base/               Table


                            Table

                            Table


  You can have more than one table.
$PGDATA/base/   Table

                Table

                Table   Made of           Made o
                                                   f          8K
                                  File                        8K
                                                              8K
                                  File
                                                              8K
                                                              8K
                                  File
                                                              8K
                                  File                        8K
                                                              ...
                                   ...                 Up to 1 GB

                Files are made up of 8K blocks.*

                                                         * you can change that at compile time.
shared_buffers
shared_buffers

       ptr   ptr   ptr   ptr   ptr   ...

      An array of pointers to 8K blocks
           (and some other stuff)

shared_buffers is configured at database start.
  You have to restart Postgres to change it.
shared_buffers:
 the other stuff
FREE FREE FREE FREE FREE FREE



       In the beginning...
shared_buffers:
 the other stuff
FREE FREE FREE FREE FREE FREE
 456  12   100 612   32   409
shared_buffers:
the other stuff
 X    X     X     X    X     X
456   12   100   612   32   409
shared_buffers:
         the other stuff
         X         X         X         X         X         X
Tag   base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

       456        12       100       612        32       409
shared_buffers:
            the other stuff
            X         X         X         X         X         X
  Tag    base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

          456        12       100       612        32       409
Status   FLAGS FLAGS FLAGS FLAGS FLAGS FLAGS
shared_buffers:
            the other stuff
            X         X         X         X         X         X
  Tag    base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

          456        12       100       612        32       409
Status   PINNED     flags      flags      flags      flags      flags




                    {                                                  }
                           UPDATE mytable (status)
                           VALUES(‘my cat is the best ever’)
                           where user = ‘selenamarie’;
shared_buffers:
            the other stuff
            X         X         X         X         X         X
  Tag    base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

          456        12       100       612        32       409
Status   PINNED    DIRTY flags           flags      flags      flags




                                  {     COMMIT;
                                                                       }
shared_buffers:
            the other stuff
            X         X         X         X         X         X
  Tag    base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

          456        12       100       612        32       409
Status   PINNED    DIRTY OTHER OTHER OTHER OTHER




                                           {     BLAH BLAH BLAH;
                                                                       }
shared_buffers:
            the other stuff
            X         X         X         X         X         X
  Tag    base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

          456        12       100       612        32       409
Status   PINNED    DIRTY OTHER OTHER OTHER OTHER
Usage       1        5     5     4     3     0
shared_buffers:
     the lifecycle
   X         X         X         X         X         X
base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

 456        12       100       612        32       409
PINNED    DIRTY OTHER OTHER OTHER OTHER
   1        5     5     4     3     0



           {      myPgProcess calls:
                  BufferAlloc(‘base/file1’, 100);              }
shared_buffers:
     the lifecycle
   X         X         X         X         X         X
base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

 456        12       100       612        32       409
PINNED    DIRTY     PINNED    OTHER OTHER OTHER
   1        5          5         4         3         0



           {      myPgProcess calls:
                  BufferAlloc(‘base/file1’, 100);              }
shared_buffers:
     the lifecycle
   X         X         X         X         X         X
base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

 456        12       100       612        32       409
PINNED    DIRTY OTHER OTHER OTHER OTHER
   1        5     5     4     3     0



           {      myPgProcess calls:
                  BufferAlloc(‘base/file1’, 101);              }
shared_buffers:
                 the lifecycle
                      Who’s it gonna be?
               X         X         X         X         X         X
            base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

             456        12       100       612        32       409
            PINNED    DIRTY OTHER OTHER OTHER OTHER
               1        5     5     4     3     0


Eviction!              {      myPgProcess calls:
                              BufferAlloc(‘base/file1’, 101);              }
shared_buffers:
                the lifecycle
                     Who’s it gonna be?
              X         X         X         X         X         X
           base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

            456        12       100       612        32       409
           PINNED    DIRTY OTHER OTHER OTHER OTHER
              0        5     5     4     3     0

Scan the cache!
  Usage != 0,
so decrement.
                      {      myPgProcess calls:
                             BufferAlloc(‘base/file1’, 101);              }
shared_buffers:
     the lifecycle
          Who’s it gonna be?
   X         X         X         X         X         X
base/file1 base/file2 base/file1 base/file3 base/file1 base/file1   Usage == 0,
 456        12       100       612        32       409         Goodbye!
PINNED    DIRTY OTHER OTHER OTHER OTHER
   0        4     4     3     2     0



           {      myPgProcess calls:
                  BufferAlloc(‘base/file1’, 101);                      }
shared_buffers:
     the lifecycle
          Who’s it gonna be?
   X         X         X         X         X         X
base/file1 base/file2 base/file1 base/file3 base/file1 base/file1

 456        12       100       612        32       101
PINNED    DIRTY OTHER OTHER OTHER                 PINNED

   0        4     4     3     2                      1



           {      myPgProcess calls:
                  BufferAlloc(‘base/file1’, 101);              }
shared_buffers:
                   optimizations*
         X         X         X         X         X         X         X         ...
      base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 base/file2    ...

       456        12       100       612        32       101       891         ...
      PINNED    DIRTY OTHER OTHER OTHER PINNED OTHER                           ...
         0        4     4     3     2      0     1                             ...



                           {                                                              }
Result is greater
      than:                       SELECT pg_relation_size(‘mytable’);
shared_buffers/4                  VACUUM mytable;
                                                                       * Version 8.3 and later
shared_buffers:
                    optimizations
         X         X         X         X         X         X         X         ...
      base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 base/file2    ...

        456       12       100       612        32       101       891         ...
      PINNED    DIRTY OTHER OTHER OTHER PINNED OTHER                           ...
         0        4     4     3     2      0     1                             ...



   256K ring
buffer allocated!
                           {      SELECT pg_relation_size(‘mytable’);
                                  VACUUM mytable;                                         }
                                                                       * Version 8.3 and later
shared_buffers:
                    optimizations VACUUM
                                  Safe during

        OVERSIMPLIFICATION WARNING!
         X         X         X         X         X         X         X         ...
      base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 base/file2    ...

        456       12       100       612        32       101       891         ...
      PINNED    DIRTY OTHER OTHER OTHER PINNED OTHER                           ...
         0        4     4     3     2      0     1                             ...



   256K ring
buffer allocated!
                           {      SELECT pg_relation_size(‘mytable’);
                                  VACUUM mytable;                                         }
                                                                       * Version 8.3 and later

More Related Content

PPT
Oracle 10g Performance: chapter 06 buffer cache
PDF
Una historia de ds ls en ruby
PDF
Securefile LOBs
PPTX
Master perl io_2011
PDF
Php unit the-mostunknownparts
PDF
MySQL EXPLAIN Explained-Norvald H. Ryeng
PDF
PHP Data Objects
PDF
The first bug on Oracle Database 12c: how to create a pdb by cloning a remote...
Oracle 10g Performance: chapter 06 buffer cache
Una historia de ds ls en ruby
Securefile LOBs
Master perl io_2011
Php unit the-mostunknownparts
MySQL EXPLAIN Explained-Norvald H. Ryeng
PHP Data Objects
The first bug on Oracle Database 12c: how to create a pdb by cloning a remote...

Viewers also liked (12)

PDF
Martin karlssons vykortssamling sigtunastiftelsen
PDF
Measuring Social Media For B2B
PDF
Martin karlssons vykortssamling flygfoton
PDF
Arduino yun × apiで遊んでみる
PPTX
Christmas is for Cookies
PPT
Securing Business: Strategic Enablement of Users
PDF
Chrome拡張開発者のためのFirefox拡張開発
PDF
Product Management: Site & Situation
PPTX
2009 Meet The Principal
PPT
The teacher librarian as resource creator: Developing learning websites
PDF
プッシュからデータ保存まで。アプリ開発でニフティクラウド mobile backendを使う上での良くある質問、疑問にお答えします
PPS
Prietenior Mei...
Martin karlssons vykortssamling sigtunastiftelsen
Measuring Social Media For B2B
Martin karlssons vykortssamling flygfoton
Arduino yun × apiで遊んでみる
Christmas is for Cookies
Securing Business: Strategic Enablement of Users
Chrome拡張開発者のためのFirefox拡張開発
Product Management: Site & Situation
2009 Meet The Principal
The teacher librarian as resource creator: Developing learning websites
プッシュからデータ保存まで。アプリ開発でニフティクラウド mobile backendを使う上での良くある質問、疑問にお答えします
Prietenior Mei...
Ad

Similar to Illustrated buffer cache (20)

PDF
Farewell to Disks: Efficient Processing of Obstinate Data
PDF
Inside PostgreSQL Shared Memory
 
PDF
MongoDB Journaling and the Storage Enginer
PDF
Chapter9
PDF
OpenGL 4.5 Reference Card
PDF
OpenGL 4.6 Reference Guide
PDF
Perl Memory Use 201209
PDF
Design and Concepts of Android Graphics
PPTX
Introduction to computers
PDF
Hadoop World 2011: Leveraging Hadoop for Legacy Systems - Mathias Herberts, C...
PDF
Leveraging Hadoop for Legacy Systems
PDF
Buffer manager
ODP
Emo-Exploitation
PDF
C++primer
PPT
Sga internals
PDF
Process' Virtual Address Space in GNU/Linux
PDF
C++ Quick Reference Sheet from Hoomanb.com
PPTX
unsplitted slideshare
KEY
Sbaw091006
Farewell to Disks: Efficient Processing of Obstinate Data
Inside PostgreSQL Shared Memory
 
MongoDB Journaling and the Storage Enginer
Chapter9
OpenGL 4.5 Reference Card
OpenGL 4.6 Reference Guide
Perl Memory Use 201209
Design and Concepts of Android Graphics
Introduction to computers
Hadoop World 2011: Leveraging Hadoop for Legacy Systems - Mathias Herberts, C...
Leveraging Hadoop for Legacy Systems
Buffer manager
Emo-Exploitation
C++primer
Sga internals
Process' Virtual Address Space in GNU/Linux
C++ Quick Reference Sheet from Hoomanb.com
unsplitted slideshare
Sbaw091006
Ad

More from Selena Deckelmann (20)

PDF
While we're here, let's fix computer science education
KEY
Algorithms are Recipes
PDF
Hire the right way
PDF
Mistakes were made - LCA 2012
PDF
Pg92 HA, LCA 2012, Ballarat
PDF
Managing terabytes
PDF
Mistakes were made
PDF
Postgres needs an aircraft carrier
PDF
Mistakes were made
PDF
Harder, better, faster, stronger: PostgreSQL 9.1
PDF
How to ask for money
PDF
Letters from the open source trenches - Postgres community
PDF
Own it: working with a changing open source community
PDF
Managing terabytes: When Postgres gets big
PDF
Managing terabytes: When PostgreSQL gets big
PDF
Pdxpugday2010 pg90
PDF
Making Software Communities
PDF
PDF
How a bunch of normal people Used Technology To Repair a Rigged Election
PDF
Open Source Bridge Opening Day
While we're here, let's fix computer science education
Algorithms are Recipes
Hire the right way
Mistakes were made - LCA 2012
Pg92 HA, LCA 2012, Ballarat
Managing terabytes
Mistakes were made
Postgres needs an aircraft carrier
Mistakes were made
Harder, better, faster, stronger: PostgreSQL 9.1
How to ask for money
Letters from the open source trenches - Postgres community
Own it: working with a changing open source community
Managing terabytes: When Postgres gets big
Managing terabytes: When PostgreSQL gets big
Pdxpugday2010 pg90
Making Software Communities
How a bunch of normal people Used Technology To Repair a Rigged Election
Open Source Bridge Opening Day

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPT
Teaching material agriculture food technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Modernizing your data center with Dell and AMD
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Monthly Chronicles - July 2025
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Teaching material agriculture food technology
Network Security Unit 5.pdf for BCA BBA.
Modernizing your data center with Dell and AMD

Illustrated buffer cache

  • 1. An illustrated guide to the buffer cache Selena Deckelmann Based on Greg Smith’s “Inside the Buffer Cache” http://www.westnet.com/∼gsmith/content/postgresql
  • 2. Table Databases Table Table Databases are made of tables...
  • 3. $PGDATA/base/ Table A table is just a directory...
  • 4. $PGDATA/base/ Table Table Table You can have more than one table.
  • 5. $PGDATA/base/ Table Table Table Made of Made o f 8K File 8K 8K File 8K 8K File 8K File 8K ... ... Up to 1 GB Files are made up of 8K blocks.* * you can change that at compile time.
  • 7. shared_buffers ptr ptr ptr ptr ptr ... An array of pointers to 8K blocks (and some other stuff) shared_buffers is configured at database start. You have to restart Postgres to change it.
  • 8. shared_buffers: the other stuff FREE FREE FREE FREE FREE FREE In the beginning...
  • 9. shared_buffers: the other stuff FREE FREE FREE FREE FREE FREE 456 12 100 612 32 409
  • 10. shared_buffers: the other stuff X X X X X X 456 12 100 612 32 409
  • 11. shared_buffers: the other stuff X X X X X X Tag base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409
  • 12. shared_buffers: the other stuff X X X X X X Tag base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 Status FLAGS FLAGS FLAGS FLAGS FLAGS FLAGS
  • 13. shared_buffers: the other stuff X X X X X X Tag base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 Status PINNED flags flags flags flags flags { } UPDATE mytable (status) VALUES(‘my cat is the best ever’) where user = ‘selenamarie’;
  • 14. shared_buffers: the other stuff X X X X X X Tag base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 Status PINNED DIRTY flags flags flags flags { COMMIT; }
  • 15. shared_buffers: the other stuff X X X X X X Tag base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 Status PINNED DIRTY OTHER OTHER OTHER OTHER { BLAH BLAH BLAH; }
  • 16. shared_buffers: the other stuff X X X X X X Tag base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 Status PINNED DIRTY OTHER OTHER OTHER OTHER Usage 1 5 5 4 3 0
  • 17. shared_buffers: the lifecycle X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 PINNED DIRTY OTHER OTHER OTHER OTHER 1 5 5 4 3 0 { myPgProcess calls: BufferAlloc(‘base/file1’, 100); }
  • 18. shared_buffers: the lifecycle X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 PINNED DIRTY PINNED OTHER OTHER OTHER 1 5 5 4 3 0 { myPgProcess calls: BufferAlloc(‘base/file1’, 100); }
  • 19. shared_buffers: the lifecycle X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 PINNED DIRTY OTHER OTHER OTHER OTHER 1 5 5 4 3 0 { myPgProcess calls: BufferAlloc(‘base/file1’, 101); }
  • 20. shared_buffers: the lifecycle Who’s it gonna be? X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 PINNED DIRTY OTHER OTHER OTHER OTHER 1 5 5 4 3 0 Eviction! { myPgProcess calls: BufferAlloc(‘base/file1’, 101); }
  • 21. shared_buffers: the lifecycle Who’s it gonna be? X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 409 PINNED DIRTY OTHER OTHER OTHER OTHER 0 5 5 4 3 0 Scan the cache! Usage != 0, so decrement. { myPgProcess calls: BufferAlloc(‘base/file1’, 101); }
  • 22. shared_buffers: the lifecycle Who’s it gonna be? X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 Usage == 0, 456 12 100 612 32 409 Goodbye! PINNED DIRTY OTHER OTHER OTHER OTHER 0 4 4 3 2 0 { myPgProcess calls: BufferAlloc(‘base/file1’, 101); }
  • 23. shared_buffers: the lifecycle Who’s it gonna be? X X X X X X base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 456 12 100 612 32 101 PINNED DIRTY OTHER OTHER OTHER PINNED 0 4 4 3 2 1 { myPgProcess calls: BufferAlloc(‘base/file1’, 101); }
  • 24. shared_buffers: optimizations* X X X X X X X ... base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 base/file2 ... 456 12 100 612 32 101 891 ... PINNED DIRTY OTHER OTHER OTHER PINNED OTHER ... 0 4 4 3 2 0 1 ... { } Result is greater than: SELECT pg_relation_size(‘mytable’); shared_buffers/4 VACUUM mytable; * Version 8.3 and later
  • 25. shared_buffers: optimizations X X X X X X X ... base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 base/file2 ... 456 12 100 612 32 101 891 ... PINNED DIRTY OTHER OTHER OTHER PINNED OTHER ... 0 4 4 3 2 0 1 ... 256K ring buffer allocated! { SELECT pg_relation_size(‘mytable’); VACUUM mytable; } * Version 8.3 and later
  • 26. shared_buffers: optimizations VACUUM Safe during OVERSIMPLIFICATION WARNING! X X X X X X X ... base/file1 base/file2 base/file1 base/file3 base/file1 base/file1 base/file2 ... 456 12 100 612 32 101 891 ... PINNED DIRTY OTHER OTHER OTHER PINNED OTHER ... 0 4 4 3 2 0 1 ... 256K ring buffer allocated! { SELECT pg_relation_size(‘mytable’); VACUUM mytable; } * Version 8.3 and later