SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
Buffer Management in a DBMS
                   Page Requests from Higher Levels

                   BUFFER POOL


     disk page


      free frame

     MAIN MEMORY

     DISK                               choice of frame dictated
                                 DB     by replacement policy

• Data must be in RAM for DBMS to operate on it!
• Buffer Mgr hides the fact that not all data is in RAM
When a Page is Requested ...
  • Buffer pool information table contains:
    <frame#, pageid, pin_count, dirty>

  • If requested page is not in pool:
     – Choose a frame for replacement
       (only un-pinned pages are candidates)
     – If frame is “dirty”, write it to disk
     – Read requested page into chosen frame
  • Pin the page and return its address.
  If requests can be predicted (e.g., sequential scans)
pages can be pre-fetched several pages at a time!
More on Buffer Management
• Requestor of page must eventually unpin it,
  and indicate whether page has been modified:
   – dirty bit is used for this.
• Page in pool may be requested many times,
   – a pin count is used.
   – To pin a page, pin_count++
   – A page is a candidate for replacement iff
     pin_count == 0 (“unpinned”)
• CC & recovery may entail additional I/O when
  a frame is chosen for replacement. (Write-
  Ahead Log protocol; more later.)
Buffer Replacement Policy

• Frame is chosen for replacement by a
  replacement policy:
   – Least-recently-used (LRU), MRU, Clock, etc.
• Policy can have big impact on # of I/O’s;
  depends on the access pattern.
LRU Replacement Policy
• Least Recently Used (LRU)
   – for each page in buffer pool, keep track of time last
     unpinned
   – replace the frame that has the oldest (earliest) time
   – very common policy: intuitive and simple
• Problems?
• Problem: Sequential flooding
   – LRU + repeated sequential scans.
   – # buffer frames < # pages in file means each page
     request causes an I/O. MRU much better in this
     situation (but not in all situations, of course).
A(1)
“Clock” Replacement Policy
                                                  D(1)              B(p)

• An approximation of LRU
• Arrange frames into a cycle, store one                     C(1)
  reference bit per frame
  – Can think of this as the 2nd chance bit
• When pin count reduces to 0, turn on ref. bit
• When replacement necessary
      do for each page in cycle {
               if (pincount == 0 && ref bit is on)
                         turn off ref bit;
               else if (pincount == 0 && ref bit is off)
                         choose this page for replacement;
      } until a page is chosen;
Code Navigation
ctags
    – Allows you to use your favorite editor to find declarations
    –   1. cd to
              Hw1/MyStuff/MyCode
    – 2. Run ctags -R ../../postgresql-7.2.2/
    –   3. Start vi filename.c
    –   4. ^] - Look up the location of the identifier under
                 the cursor in the tags file, and move to
                 that location.
           ^T - Return to the previous location in the tag stack, i.e.,
            pop off one element.
    –   Also Emacs support (ctags). Talk amongst yourselves
Code Debugging
•   ddd
    – Two ways to debug postgres
       • interactive mode
       • bare backend mode (described here)
    – Initialize postgres by typing
       • /local_tmp/$USER/postgresql-7.2.2-
           clock/bin/initdb –D <data directory>
    – Startup ddd with the newly complied and
       installed version of postgres
       • ddd /local_tmp/$USER/postgresql-7.2.2-
           clock/bin/postgres
    – You can now examine functions
       • Type in the search field the name of the function to
           be examined (e.g., GetFreeBuffer()).
       • Then click the Lookup button.
Code Debugging
•   ddd (cont‘d
    –   Run postgres by hitting F2 or by using the
        menu option Program -> Run
    –   A dialog box will appear where you enter
        -D <data dir> template1
    –   Click on Run and then the backend
        prompt should appears in the upper pane.
    –   Place a breakpoint in GetFreeBuffer() on the first line of the code
        in the function (You can either right click your mouse and choose the
        set breakpoint menu or click on the line and then click on the Break
        button).
    –   Type at the backend prompt
        Select * from pg_user; to run your query.
    –   Now you can start debugging your code by either
        •   Looking at the current state of your stack (Status -> Backtrace).
        •   Display local variables (Data -> Display Local Variables) and function
            arguments
            (Data -> Display Arguments).
        •   Display any expression you want to know about more (highlight the expression
            and click on the Display Button).
Stuff About Assignment 3
•   Methods to know about
    – AddBufferToFreeList(BufferDesc) and
       InitFreeList(bool) can be ignored for implementing the
       Clock Algorithm.
    – BufferDescriptorGetBuffer(BufferDesc) – returns
       position of a page in buffer.
    – GetFreeBuffer() – method that needs to be re-implemented.
•   Data structures to know about
    – BufferDescriptors[] – Array representing the buffer pool
       of size NBuffers.
    – BufferDesc – represents a buffer page; stores among other
       things information like whether the page is currently pinned or
       unpinned (struct variable refcount).
Buffer 0   Buffer 1    Buffer 2..   Buffer N-2   Buffer N-1




   0          1        2 … N-3       N-2         N-1            N

freeNext    freeNext       …         freeNext    freeNext     freeNext
                                                                         N=Nbuffers
                                                                         Data_Descriptors=N
freePrev    freePrev                 freePrev    freePrev     freePrev   Free_List_Descriptor=N




           BufferDescriptors Array after buf_init.c:212 in InitBufferPool()
  • InitBufferPool() creates BufferDescriptors Array of size N=Nbuffers+1 and creates a
  circular doubly linked list with first N items.
  • BufferDescriptors 0 to N-1 have pointers to buffer blocks in memory.
  • BufferDescriptors[N] serves as a dummy element and does not point to a valid buffer
  block.
Reference History
Buffer 0   Buffer 1    Buffer 2..   Buffer N-2   Buffer N-1
                                                                                         1
                                                                                         2
                                                                                        …
                                                                           time         N-3
                                                                                        N-2
   0          1        2 … N-3       N-2         N-1              N                     N-1
                                                                                         0

freeNext    freeNext       …         freeNext    freeNext       freeNext
                                                                           N=Nbuffers
                                                                           Data_Descriptors=N
freePrev    freePrev                 freePrev    freePrev       freePrev   Free_List_Descriptor=N




                                                              SharedFreeList

             BufferDescriptors Array after freelist.c:243 in InitFreeList()
•BufferDescriptors[N] is only used as a placeholder element in the linked list. InitFreeList()
points SharedFreeList to BufferDescriptors[N] and inserts the latter between
BufferDescriptors[0] and BufferDescriptors[1].
•SharedFreeList->FreeNext will serve as the handle to the first element in freelist.
Freelist links the unpinned buffers, i.e. those that are eligible for eviction.
Buffer 0      Buffer 1      Buffer 2    Buffer 3..   Buffer N-1

                                                                                     N

                                                                                   freeNext
    0             1             2       3…N-2        N-1            N
                                                                                   freePrev
              This buffer
 freeNext     is deleted     freeNext                freeNext     freeNext
                 from                       …
               freelist.
 freePrev                    freePrev                freePrev     freePrev



                                                                              SharedFreeList
                                                                              freelist.c:198, No
                                                                              free elements left
                                                                              condition check.
                                                            SharedFreeList

            BufferDescriptors Array after freelist.c:233 in GetFreeBuffer()
• GetFreeBuffer() returns the bufferDescriptor pointed by SharedFreeList->FreeNext.
Since this buffer is no longer free, it is removed from freelist by rearranging the freeNext
and freePrev pointers of the adjacent elements.
• GetFreeBuffer() also checks that there are free elements available in the freelist.
Buffer 0    Buffer 1      Buffer 2    Buffer 3..   Buffer N-1                     Buffer i




    0           1             2       3…N-2        N-1              N                  i
            This buffer
 freeNext   is deleted     freeNext                freeNext       freeNext          freeNext
               from                       …
             freelist.
 freePrev                  freePrev                freePrev       freePrev          freePrev




                                                                SharedFreeList


  LRU policy insert (default), after freelist.c:81 in AddBufferToFreelist(),
                          continued from GetFreeBuffer() slide.
• AddBufferToFreelist() adds the input bufferDescriptor[i] to end of the freelist, i.e as
SharedFreeList->freePrev. The freeNext and freePrev pointers of the adjacent
elements are rearranged appropriately.
Buffer 0      Buffer 1      Buffer 2    Buffer 3..   Buffer N-1                    Buffer i




    0             1             2       3…N-2        N-1              N                 i
              This buffer
 freeNext     is deleted     freeNext                freeNext       freeNext        freeNext
                 from                       …
               freelist.
 freePrev                    freePrev                freePrev       freePrev        freePrev




                                                                  SharedFreeList

            MRU policy insert (freelist.c.mru), after freelist.c.mru:81 in
             AddBufferToFreelist(), continued from GetFreeBuffer() slide.
• AddBufferToFreelist() adds the input BufferDescriptor[i] to start of the freelist, i.e as
SharedFreeList->freeNext. Compare with LRU to understand the modifications in
pointers. Note that this is the only change needed to switch from LRU to MRU.
Reference History

                                                                         1

                 LRU                             MRU            time
                                                                        …
                                                                         2

                                                                        N-3
                                                                        N-2
                                                                        N-1
                                                                         0



                  0         1                     1         0

                 freeNext                        freeNext




                 freePrev                        freePrev




SharedFreeList     0        1   SharedFreeList    1         0

Mais conteúdo relacionado

Destaque

Buffer management --database buffering
Buffer management --database buffering Buffer management --database buffering
Buffer management --database buffering julia121214
 
Database , 12 Reliability
Database , 12 ReliabilityDatabase , 12 Reliability
Database , 12 ReliabilityAli Usman
 
Scalability and Reliability in the Cloud
Scalability and Reliability in the CloudScalability and Reliability in the Cloud
Scalability and Reliability in the Cloudgmthomps
 
5. spooling and buffering
5. spooling and buffering 5. spooling and buffering
5. spooling and buffering myrajendra
 
Software reliability
Software reliabilitySoftware reliability
Software reliabilityAnand Kumar
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMSkoolkampus
 

Destaque (9)

Buffer management --database buffering
Buffer management --database buffering Buffer management --database buffering
Buffer management --database buffering
 
Database , 12 Reliability
Database , 12 ReliabilityDatabase , 12 Reliability
Database , 12 Reliability
 
Scalability and Reliability in the Cloud
Scalability and Reliability in the CloudScalability and Reliability in the Cloud
Scalability and Reliability in the Cloud
 
Availability and reliability
Availability and reliabilityAvailability and reliability
Availability and reliability
 
5. spooling and buffering
5. spooling and buffering 5. spooling and buffering
5. spooling and buffering
 
Reliability
ReliabilityReliability
Reliability
 
Software reliability
Software reliabilitySoftware reliability
Software reliability
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
 

Semelhante a Buffer Management in a DBMS: CLOCK Replacement Policy

[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017Yu-Hsun (lymanblue) Lin
 
PyTorch Tutorial for NTU Machine Learing Course 2017
PyTorch Tutorial for NTU Machine Learing Course 2017PyTorch Tutorial for NTU Machine Learing Course 2017
PyTorch Tutorial for NTU Machine Learing Course 2017Yu-Hsun (lymanblue) Lin
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce AlgorithmsAmund Tveit
 
Funddamentals of data structures
Funddamentals of data structuresFunddamentals of data structures
Funddamentals of data structuresGlobalidiots
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptxjack881
 
Memory allocation
Memory allocationMemory allocation
Memory allocationsanya6900
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprogramsbaran19901990
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12Rabia Khalid
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyTravis Oliphant
 
Linux fundamental - Chap 05 filter
Linux fundamental - Chap 05 filterLinux fundamental - Chap 05 filter
Linux fundamental - Chap 05 filterKenny (netman)
 
Pytroch-basic.pptx
Pytroch-basic.pptxPytroch-basic.pptx
Pytroch-basic.pptxrebeen4
 
Londiste Replication system for PostgreSQL
Londiste Replication system for PostgreSQLLondiste Replication system for PostgreSQL
Londiste Replication system for PostgreSQLelliando dias
 
Parquet performance tuning: the missing guide
Parquet performance tuning: the missing guideParquet performance tuning: the missing guide
Parquet performance tuning: the missing guideRyan Blue
 

Semelhante a Buffer Management in a DBMS: CLOCK Replacement Policy (20)

[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
 
PyTorch Tutorial for NTU Machine Learing Course 2017
PyTorch Tutorial for NTU Machine Learing Course 2017PyTorch Tutorial for NTU Machine Learing Course 2017
PyTorch Tutorial for NTU Machine Learing Course 2017
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
14 query processing-sorting
14 query processing-sorting14 query processing-sorting
14 query processing-sorting
 
Funddamentals of data structures
Funddamentals of data structuresFunddamentals of data structures
Funddamentals of data structures
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 
Unit v
Unit vUnit v
Unit v
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPy
 
MIPS Architecture
MIPS ArchitectureMIPS Architecture
MIPS Architecture
 
Linux fundamental - Chap 05 filter
Linux fundamental - Chap 05 filterLinux fundamental - Chap 05 filter
Linux fundamental - Chap 05 filter
 
Pytroch-basic.pptx
Pytroch-basic.pptxPytroch-basic.pptx
Pytroch-basic.pptx
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Londiste Replication system for PostgreSQL
Londiste Replication system for PostgreSQLLondiste Replication system for PostgreSQL
Londiste Replication system for PostgreSQL
 
Parquet performance tuning: the missing guide
Parquet performance tuning: the missing guideParquet performance tuning: the missing guide
Parquet performance tuning: the missing guide
 

Último

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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 Servicegiselly40
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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.pptxMalak Abu Hammad
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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...Miguel Araújo
 
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...Igalia
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 

Último (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
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...
 
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...
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 

Buffer Management in a DBMS: CLOCK Replacement Policy

  • 1. Buffer Management in a DBMS Page Requests from Higher Levels BUFFER POOL disk page free frame MAIN MEMORY DISK choice of frame dictated DB by replacement policy • Data must be in RAM for DBMS to operate on it! • Buffer Mgr hides the fact that not all data is in RAM
  • 2. When a Page is Requested ... • Buffer pool information table contains: <frame#, pageid, pin_count, dirty> • If requested page is not in pool: – Choose a frame for replacement (only un-pinned pages are candidates) – If frame is “dirty”, write it to disk – Read requested page into chosen frame • Pin the page and return its address. If requests can be predicted (e.g., sequential scans) pages can be pre-fetched several pages at a time!
  • 3. More on Buffer Management • Requestor of page must eventually unpin it, and indicate whether page has been modified: – dirty bit is used for this. • Page in pool may be requested many times, – a pin count is used. – To pin a page, pin_count++ – A page is a candidate for replacement iff pin_count == 0 (“unpinned”) • CC & recovery may entail additional I/O when a frame is chosen for replacement. (Write- Ahead Log protocol; more later.)
  • 4. Buffer Replacement Policy • Frame is chosen for replacement by a replacement policy: – Least-recently-used (LRU), MRU, Clock, etc. • Policy can have big impact on # of I/O’s; depends on the access pattern.
  • 5. LRU Replacement Policy • Least Recently Used (LRU) – for each page in buffer pool, keep track of time last unpinned – replace the frame that has the oldest (earliest) time – very common policy: intuitive and simple • Problems? • Problem: Sequential flooding – LRU + repeated sequential scans. – # buffer frames < # pages in file means each page request causes an I/O. MRU much better in this situation (but not in all situations, of course).
  • 6. A(1) “Clock” Replacement Policy D(1) B(p) • An approximation of LRU • Arrange frames into a cycle, store one C(1) reference bit per frame – Can think of this as the 2nd chance bit • When pin count reduces to 0, turn on ref. bit • When replacement necessary do for each page in cycle { if (pincount == 0 && ref bit is on) turn off ref bit; else if (pincount == 0 && ref bit is off) choose this page for replacement; } until a page is chosen;
  • 7. Code Navigation ctags – Allows you to use your favorite editor to find declarations – 1. cd to Hw1/MyStuff/MyCode – 2. Run ctags -R ../../postgresql-7.2.2/ – 3. Start vi filename.c – 4. ^] - Look up the location of the identifier under the cursor in the tags file, and move to that location. ^T - Return to the previous location in the tag stack, i.e., pop off one element. – Also Emacs support (ctags). Talk amongst yourselves
  • 8. Code Debugging • ddd – Two ways to debug postgres • interactive mode • bare backend mode (described here) – Initialize postgres by typing • /local_tmp/$USER/postgresql-7.2.2- clock/bin/initdb –D <data directory> – Startup ddd with the newly complied and installed version of postgres • ddd /local_tmp/$USER/postgresql-7.2.2- clock/bin/postgres – You can now examine functions • Type in the search field the name of the function to be examined (e.g., GetFreeBuffer()). • Then click the Lookup button.
  • 9. Code Debugging • ddd (cont‘d – Run postgres by hitting F2 or by using the menu option Program -> Run – A dialog box will appear where you enter -D <data dir> template1 – Click on Run and then the backend prompt should appears in the upper pane. – Place a breakpoint in GetFreeBuffer() on the first line of the code in the function (You can either right click your mouse and choose the set breakpoint menu or click on the line and then click on the Break button). – Type at the backend prompt Select * from pg_user; to run your query. – Now you can start debugging your code by either • Looking at the current state of your stack (Status -> Backtrace). • Display local variables (Data -> Display Local Variables) and function arguments (Data -> Display Arguments). • Display any expression you want to know about more (highlight the expression and click on the Display Button).
  • 10. Stuff About Assignment 3 • Methods to know about – AddBufferToFreeList(BufferDesc) and InitFreeList(bool) can be ignored for implementing the Clock Algorithm. – BufferDescriptorGetBuffer(BufferDesc) – returns position of a page in buffer. – GetFreeBuffer() – method that needs to be re-implemented. • Data structures to know about – BufferDescriptors[] – Array representing the buffer pool of size NBuffers. – BufferDesc – represents a buffer page; stores among other things information like whether the page is currently pinned or unpinned (struct variable refcount).
  • 11. Buffer 0 Buffer 1 Buffer 2.. Buffer N-2 Buffer N-1 0 1 2 … N-3 N-2 N-1 N freeNext freeNext … freeNext freeNext freeNext N=Nbuffers Data_Descriptors=N freePrev freePrev freePrev freePrev freePrev Free_List_Descriptor=N BufferDescriptors Array after buf_init.c:212 in InitBufferPool() • InitBufferPool() creates BufferDescriptors Array of size N=Nbuffers+1 and creates a circular doubly linked list with first N items. • BufferDescriptors 0 to N-1 have pointers to buffer blocks in memory. • BufferDescriptors[N] serves as a dummy element and does not point to a valid buffer block.
  • 12. Reference History Buffer 0 Buffer 1 Buffer 2.. Buffer N-2 Buffer N-1 1 2 … time N-3 N-2 0 1 2 … N-3 N-2 N-1 N N-1 0 freeNext freeNext … freeNext freeNext freeNext N=Nbuffers Data_Descriptors=N freePrev freePrev freePrev freePrev freePrev Free_List_Descriptor=N SharedFreeList BufferDescriptors Array after freelist.c:243 in InitFreeList() •BufferDescriptors[N] is only used as a placeholder element in the linked list. InitFreeList() points SharedFreeList to BufferDescriptors[N] and inserts the latter between BufferDescriptors[0] and BufferDescriptors[1]. •SharedFreeList->FreeNext will serve as the handle to the first element in freelist. Freelist links the unpinned buffers, i.e. those that are eligible for eviction.
  • 13. Buffer 0 Buffer 1 Buffer 2 Buffer 3.. Buffer N-1 N freeNext 0 1 2 3…N-2 N-1 N freePrev This buffer freeNext is deleted freeNext freeNext freeNext from … freelist. freePrev freePrev freePrev freePrev SharedFreeList freelist.c:198, No free elements left condition check. SharedFreeList BufferDescriptors Array after freelist.c:233 in GetFreeBuffer() • GetFreeBuffer() returns the bufferDescriptor pointed by SharedFreeList->FreeNext. Since this buffer is no longer free, it is removed from freelist by rearranging the freeNext and freePrev pointers of the adjacent elements. • GetFreeBuffer() also checks that there are free elements available in the freelist.
  • 14. Buffer 0 Buffer 1 Buffer 2 Buffer 3.. Buffer N-1 Buffer i 0 1 2 3…N-2 N-1 N i This buffer freeNext is deleted freeNext freeNext freeNext freeNext from … freelist. freePrev freePrev freePrev freePrev freePrev SharedFreeList LRU policy insert (default), after freelist.c:81 in AddBufferToFreelist(), continued from GetFreeBuffer() slide. • AddBufferToFreelist() adds the input bufferDescriptor[i] to end of the freelist, i.e as SharedFreeList->freePrev. The freeNext and freePrev pointers of the adjacent elements are rearranged appropriately.
  • 15. Buffer 0 Buffer 1 Buffer 2 Buffer 3.. Buffer N-1 Buffer i 0 1 2 3…N-2 N-1 N i This buffer freeNext is deleted freeNext freeNext freeNext freeNext from … freelist. freePrev freePrev freePrev freePrev freePrev SharedFreeList MRU policy insert (freelist.c.mru), after freelist.c.mru:81 in AddBufferToFreelist(), continued from GetFreeBuffer() slide. • AddBufferToFreelist() adds the input BufferDescriptor[i] to start of the freelist, i.e as SharedFreeList->freeNext. Compare with LRU to understand the modifications in pointers. Note that this is the only change needed to switch from LRU to MRU.
  • 16. Reference History 1 LRU MRU time … 2 N-3 N-2 N-1 0 0 1 1 0 freeNext freeNext freePrev freePrev SharedFreeList 0 1 SharedFreeList 1 0