mirror of
https://github.com/postgres/postgres.git
synced 2025-07-09 22:41:56 +03:00
Fix multiple problems in WAL replay.
Most of the replay functions for WAL record types that modify more than one page failed to ensure that those pages were locked correctly to ensure that concurrent queries could not see inconsistent page states. This is a hangover from coding decisions made long before Hot Standby was added, when it was hardly necessary to acquire buffer locks during WAL replay at all, let alone hold them for carefully-chosen periods. The key problem was that RestoreBkpBlocks was written to hold lock on each page restored from a full-page image for only as long as it took to update that page. This was guaranteed to break any WAL replay function in which there was any update-ordering constraint between pages, because even if the nominal order of the pages is the right one, any mixture of full-page and non-full-page updates in the same record would result in out-of-order updates. Moreover, it wouldn't work for situations where there's a requirement to maintain lock on one page while updating another. Failure to honor an update ordering constraint in this way is thought to be the cause of bug #7648 from Daniel Farina: what seems to have happened there is that a btree page being split was rewritten from a full-page image before the new right sibling page was written, and because lock on the original page was not maintained it was possible for hot standby queries to try to traverse the page's right-link to the not-yet-existing sibling page. To fix, get rid of RestoreBkpBlocks as such, and instead create a new function RestoreBackupBlock that restores just one full-page image at a time. This function can be invoked by WAL replay functions at the points where they would otherwise perform non-full-page updates; in this way, the physical order of page updates remains the same no matter which pages are replaced by full-page images. We can then further adjust the logic in individual replay functions if it is necessary to hold buffer locks for overlapping periods. A side benefit is that we can simplify the handling of concurrency conflict resolution by moving that code into the record-type-specfic functions; there's no more need to contort the code layout to keep conflict resolution in front of the RestoreBkpBlocks call. In connection with that, standardize on zero-based numbering rather than one-based numbering for referencing the full-page images. In HEAD, I removed the macros XLR_BKP_BLOCK_1 through XLR_BKP_BLOCK_4. They are still there in the header files in previous branches, but are no longer used by the code. In addition, fix some other bugs identified in the course of making these changes: spgRedoAddNode could fail to update the parent downlink at all, if the parent tuple is in the same page as either the old or new split tuple and we're not doing a full-page image: it would get fooled by the LSN having been advanced already. This would result in permanent index corruption, not just transient failure of concurrent queries. Also, ginHeapTupleFastInsert's "merge lists" case failed to mark the old tail page as a candidate for a full-page image; in the worst case this could result in torn-page corruption. heap_xlog_freeze() was inconsistent about using a cleanup lock or plain exclusive lock: it did the former in the normal path but the latter for a full-page image. A plain exclusive lock seems sufficient, so change to that. Also, remove gistRedoPageDeleteRecord(), which has been dead code since VACUUM FULL was rewritten. Back-patch to 9.0, where hot standby was introduced. Note however that 9.0 had a significantly different WAL-logging scheme for GIST index updates, and it doesn't appear possible to make that scheme safe for concurrent hot standby queries, because it can leave inconsistent states in the index even between WAL records. Given the lack of complaints from the field, we won't work too hard on fixing that branch.
This commit is contained in:
@ -71,11 +71,7 @@ typedef struct XLogRecord
|
||||
*/
|
||||
#define XLR_BKP_BLOCK_MASK 0x0F /* all info bits used for bkp blocks */
|
||||
#define XLR_MAX_BKP_BLOCKS 4
|
||||
#define XLR_SET_BKP_BLOCK(iblk) (0x08 >> (iblk))
|
||||
#define XLR_BKP_BLOCK_1 XLR_SET_BKP_BLOCK(0) /* 0x08 */
|
||||
#define XLR_BKP_BLOCK_2 XLR_SET_BKP_BLOCK(1) /* 0x04 */
|
||||
#define XLR_BKP_BLOCK_3 XLR_SET_BKP_BLOCK(2) /* 0x02 */
|
||||
#define XLR_BKP_BLOCK_4 XLR_SET_BKP_BLOCK(3) /* 0x01 */
|
||||
#define XLR_BKP_BLOCK(iblk) (0x08 >> (iblk)) /* iblk in 0..3 */
|
||||
|
||||
/* Sync methods */
|
||||
#define SYNC_METHOD_FSYNC 0
|
||||
@ -94,13 +90,13 @@ extern int sync_method;
|
||||
* If buffer is valid then XLOG will check if buffer must be backed up
|
||||
* (ie, whether this is first change of that page since last checkpoint).
|
||||
* If so, the whole page contents are attached to the XLOG record, and XLOG
|
||||
* sets XLR_BKP_BLOCK_X bit in xl_info. Note that the buffer must be pinned
|
||||
* sets XLR_BKP_BLOCK(N) bit in xl_info. Note that the buffer must be pinned
|
||||
* and exclusive-locked by the caller, so that it won't change under us.
|
||||
* NB: when the buffer is backed up, we DO NOT insert the data pointed to by
|
||||
* this XLogRecData struct into the XLOG record, since we assume it's present
|
||||
* in the buffer. Therefore, rmgr redo routines MUST pay attention to
|
||||
* XLR_BKP_BLOCK_X to know what is actually stored in the XLOG record.
|
||||
* The i'th XLR_BKP_BLOCK bit corresponds to the i'th distinct buffer
|
||||
* XLR_BKP_BLOCK(N) to know what is actually stored in the XLOG record.
|
||||
* The N'th XLR_BKP_BLOCK bit corresponds to the N'th distinct buffer
|
||||
* value (ignoring InvalidBuffer) appearing in the rdata chain.
|
||||
*
|
||||
* When buffer is valid, caller must set buffer_std to indicate whether the
|
||||
@ -274,7 +270,9 @@ extern int XLogFileOpen(XLogSegNo segno);
|
||||
extern void XLogGetLastRemoved(XLogSegNo *segno);
|
||||
extern void XLogSetAsyncXactLSN(XLogRecPtr record);
|
||||
|
||||
extern void RestoreBkpBlocks(XLogRecPtr lsn, XLogRecord *record, bool cleanup);
|
||||
extern Buffer RestoreBackupBlock(XLogRecPtr lsn, XLogRecord *record,
|
||||
int block_index,
|
||||
bool get_cleanup_lock, bool keep_buffer);
|
||||
|
||||
extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
|
||||
extern void xlog_desc(StringInfo buf, uint8 xl_info, char *rec);
|
||||
|
Reference in New Issue
Block a user