PostgreSQL의 실제 데이터 쓰기 시작 위치

19512 단어 PostgreSQL
기본 관계식:
Background WriterMain 순환 중 BgBuffer Sync () -->SyncOneBuffer -->FlushBuffer -->smgrwrite 호출
코드 보기:
/*                    

 * Main entry point for bgwriter process                    

 *                    

 * This is invoked from AuxiliaryProcessMain, which has already created the                    

 * basic execution environment, but not enabled signals yet.                    

 */                    

void                    

BackgroundWriterMain(void)                    

{                    

    ……                

    /*                

     * Loop forever                

     */                

    for (;;)                

    {                

        ……            

                    

        /*            

         * Do one cycle of dirty-buffer writing.            

         */            

        can_hibernate = BgBufferSync();            

        ……            

    }                

}                    

다시 보기:
/*                            

 * BgBufferSync -- Write out some dirty buffers in the pool.                            

 *                            

 * This is called periodically by the background writer process.                            

 *                            

 * Returns true if it's appropriate for the bgwriter process to go into                            

 * low-power hibernation mode.    (This happens if the strategy clock sweep                        

 * has been "lapped" and no buffer allocations have occurred recently,                            

 * or if the bgwriter has been effectively disabled by setting                            

 * bgwriter_lru_maxpages to 0.)                            

 */                            

bool                            

BgBufferSync(void)                            

{                            

    ……                        

    /* Execute the LRU scan */                        

    while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)                        

    {                        

        int    buffer_state = SyncOneBuffer(next_to_clean, true);                

                            

        if (++next_to_clean >= NBuffers)                    

        {                    

            next_to_clean = 0;                

            next_passes++;                

        }                    

        num_to_scan--;                    

                            

        if (buffer_state & BUF_WRITTEN)                    

        {                    

            reusable_buffers++;                

            if (++num_written >= bgwriter_lru_maxpages)                

            {                

                BgWriterStats.m_maxwritten_clean++;            

                break;            

            }                

        }                    

        else if (buffer_state & BUF_REUSABLE)                    

            reusable_buffers++;                

    }                        

    ……                        

}                            

다시 보기:
/*                        

 * SyncOneBuffer -- process a single buffer during syncing.                        

 *                        

 * If skip_recently_used is true, we don't write currently-pinned buffers, nor                        

 * buffers marked recently used, as these are not replacement candidates.                        

 *                        

 * Returns a bitmask containing the following flag bits:                        

 *    BUF_WRITTEN: we wrote the buffer.                    

 *    BUF_REUSABLE: buffer is available for replacement, ie, it has                    

 *        pin count 0 and usage count 0.                

 *                        

 * (BUF_WRITTEN could be set in error if FlushBuffers finds the buffer clean                        

 * after locking it, but we don't care all that much.)                        

 *                        

 * Note: caller must have done ResourceOwnerEnlargeBuffers.                        

 */                        

static int                        

SyncOneBuffer(int buf_id, bool skip_recently_used)                        

{                        

    volatile BufferDesc *bufHdr = &BufferDescriptors[buf_id];                    

    int            result = 0;        

                        

    /*                    

     * Check whether buffer needs writing.                    

     *                    

     * We can make this check without taking the buffer content lock so long                    

     * as we mark pages dirty in access methods *before* logging changes with                    

     * XLogInsert(): if someone marks the buffer dirty just after our check we                    

     * don't worry because our checkpoint.redo points before log record for                    

     * upcoming changes and so we are not required to write such dirty buffer.                    

     */                    

    LockBufHdr(bufHdr);                    

                        

    if (bufHdr->refcount == 0 && bufHdr->usage_count == 0)                    

        result |= BUF_REUSABLE;                

    else if (skip_recently_used)                    

    {                    

        /* Caller told us not to write recently-used buffers */                

        UnlockBufHdr(bufHdr);                

        return result;                

    }                    

                        

    if (!(bufHdr->flags & BM_VALID) || !(bufHdr->flags & BM_DIRTY))                    

    {                    

        /* It's clean, so nothing to do */                

        UnlockBufHdr(bufHdr);                

        return result;                

    }                    

                        

    /*                    

     * Pin it, share-lock it, write it.  (FlushBuffer will do nothing if the                    

     * buffer is clean by the time we've locked it.)                    

     */                    

    PinBuffer_Locked(bufHdr);                    

    LWLockAcquire(bufHdr->content_lock, LW_SHARED);                    

                        

    FlushBuffer(bufHdr, NULL);                    

                        

    LWLockRelease(bufHdr->content_lock);                    

    UnpinBuffer(bufHdr, true);                    

                        

    return result | BUF_WRITTEN;                    

}                        

다시 보기:
/*                        

 * FlushBuffer                        

 *        Physically write out a shared buffer.                

 *                        

 * NOTE: this actually just passes the buffer contents to the kernel; the                        

 * real write to disk won't happen until the kernel feels like it.  This                        

 * is okay from our point of view since we can redo the changes from WAL.                        

 * However, we will need to force the changes to disk via fsync before                        

 * we can checkpoint WAL.                        

 *                        

 * The caller must hold a pin on the buffer and have share-locked the                        

 * buffer contents.  (Note: a share-lock does not prevent updates of                        

 * hint bits in the buffer, so the page could change while the write                        

 * is in progress, but we assume that that will not invalidate the data                        

 * written.)                        

 *                        

 * If the caller has an smgr reference for the buffer's relation, pass it                        

 * as the second parameter.  If not, pass NULL.  In the latter case, the                        

 * relation will be marked as "transient" so that the corresponding                        

 * kernel-level file descriptors are closed when the current transaction ends,                        

 * if any.                        

 */                        

static void                        

FlushBuffer(volatile BufferDesc *buf, SMgrRelation reln)                        

{                        

    XLogRecPtr    recptr;                

    ErrorContextCallback errcontext;                    

    instr_time    io_start,                

                io_time;        

                        

    /*                    

     * Acquire the buffer's io_in_progress lock.  If StartBufferIO returns                    

     * false, then someone else flushed the buffer before we could, so we need                    

     * not do anything.                    

     */                    

    if (!StartBufferIO(buf, false))                    

        return;                

                        

    /* Setup error traceback support for ereport() */                    

    errcontext.callback = shared_buffer_write_error_callback;                    

    errcontext.arg = (void *) buf;                    

    errcontext.previous = error_context_stack;                    

    error_context_stack = &errcontext;                    

                        

    /* Find smgr relation for buffer, and mark it as transient */                    

    if (reln == NULL)                    

    {                    

        reln = smgropen(buf->tag.rnode, InvalidBackendId);                

        smgrsettransient(reln);                

    }                    

                        

    TRACE_POSTGRESQL_BUFFER_FLUSH_START(buf->tag.forkNum,                    

                    buf->tag.blockNum,    

                    reln->smgr_rnode.node.spcNode,    

                    reln->smgr_rnode.node.dbNode,    

                    reln->smgr_rnode.node.relNode);    

                        

    /*                    

     * Force XLOG flush up to buffer's LSN.  This implements the basic WAL                    

     * rule that log updates must hit disk before any of the data-file changes                    

     * they describe do.                    

     */                    

    recptr = BufferGetLSN(buf);                    

    XLogFlush(recptr);                    

                        

    /*                    

     * Now it's safe to write buffer to disk. Note that no one else should                    

     * have been able to write it while we were busy with log flushing because                    

     * we have the io_in_progress lock.                    

     */                    

                        

    /* To check if block content changes while flushing. - vadim 01/17/97 */                    

    LockBufHdr(buf);                    

    buf->flags &= ~BM_JUST_DIRTIED;                    

    UnlockBufHdr(buf);                    

                        

    if (track_io_timing)                    

        INSTR_TIME_SET_CURRENT(io_start);                

                        

    smgrwrite(reln,                    

              buf->tag.forkNum,            

              buf->tag.blockNum,            

              (char *) BufHdrGetBlock(buf),            

              false);            

                        

    if (track_io_timing)                    

    {                    

        INSTR_TIME_SET_CURRENT(io_time);                

        INSTR_TIME_SUBTRACT(io_time, io_start);                

        pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));                

        INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);                

    }                    

                        

    pgBufferUsage.shared_blks_written++;                    

                        

    /*                    

     * Mark the buffer as clean (unless BM_JUST_DIRTIED has become set) and                    

     * end the io_in_progress state.                    

     */                    

    TerminateBufferIO(buf, true, 0);                    

                        

    TRACE_POSTGRESQL_BUFFER_FLUSH_DONE(buf->tag.forkNum,                    

                           buf->tag.blockNum,

                           reln->smgr_rnode.node.spcNode,

                           reln->smgr_rnode.node.dbNode,

                           reln->smgr_rnode.node.relNode);

                        

    /* Pop the error context stack */                    

    error_context_stack = errcontext.previous;                    

}                        

순환 안에 버퍼를 한 번씩 써요. 와, 이상하지 않아요?어쩌면 조금만 썼을지도 몰라, 디자이너가 일부러 그런 거야?

좋은 웹페이지 즐겨찾기