* MarkBufferDirty * * Marks buffer contents as dirty (actual write happens later). * * Buffer must be pinned and exclusive-locked. (If caller does not hold * exclusive lock, then somebody could be in process of writing the buffer, * leading to risk of bad data written to disk.) */
| 1678 | * leading to risk of bad data written to disk.) |
| 1679 | */ |
| 1680 | void |
| 1681 | MarkBufferDirty(Buffer buffer) |
| 1682 | { |
| 1683 | BufferDesc *bufHdr; |
| 1684 | uint32 buf_state; |
| 1685 | uint32 old_buf_state; |
| 1686 | |
| 1687 | if (!BufferIsValid(buffer)) |
| 1688 | elog(ERROR, "bad buffer ID: %d", buffer); |
| 1689 | |
| 1690 | if (BufferIsLocal(buffer)) |
| 1691 | { |
| 1692 | MarkLocalBufferDirty(buffer); |
| 1693 | return; |
| 1694 | } |
| 1695 | |
| 1696 | bufHdr = GetBufferDescriptor(buffer - 1); |
| 1697 | |
| 1698 | Assert(BufferIsPinned(buffer)); |
| 1699 | Assert(LWLockHeldByMeInMode(BufferDescriptorGetContentLock(bufHdr), |
| 1700 | LW_EXCLUSIVE)); |
| 1701 | |
| 1702 | old_buf_state = pg_atomic_read_u32(&bufHdr->state); |
| 1703 | for (;;) |
| 1704 | { |
| 1705 | if (old_buf_state & BM_LOCKED) |
| 1706 | old_buf_state = WaitBufHdrUnlocked(bufHdr); |
| 1707 | |
| 1708 | buf_state = old_buf_state; |
| 1709 | |
| 1710 | Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0); |
| 1711 | buf_state |= BM_DIRTY | BM_JUST_DIRTIED; |
| 1712 | |
| 1713 | if (pg_atomic_compare_exchange_u32(&bufHdr->state, &old_buf_state, |
| 1714 | buf_state)) |
| 1715 | break; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * If the buffer was not dirty already, do vacuum accounting. |
| 1720 | */ |
| 1721 | if (!(old_buf_state & BM_DIRTY)) |
| 1722 | { |
| 1723 | VacuumPageDirty++; |
| 1724 | pgBufferUsage.shared_blks_dirtied++; |
| 1725 | if (VacuumCostActive) |
| 1726 | VacuumCostBalance += VacuumCostPageDirty; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | /* |
| 1731 | * ReleaseAndReadBuffer -- combine ReleaseBuffer() and ReadBuffer() |
no test coverage detected