<commit_before>/*
 * brin_xlog.c
 *		XLog replay routines for BRIN indexes
 *
 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/access/brin/brin_xlog.c
 */
#include "postgres.h"

#include "access/brin_page.h"
#include "access/brin_pageops.h"
#include "access/brin_xlog.h"
#include "access/xlogutils.h"


/*
 * xlog replay routines
 */
static void
brin_xlog_createidx(XLogRecPtr lsn, XLogRecord *record)
{
	xl_brin_createidx *xlrec = (xl_brin_createidx *) XLogRecGetData(record);
	Buffer		buf;
	Page		page;

	/* Backup blocks are not used in create_index records */
	Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));

	/* create the index' metapage */
	buf = XLogReadBuffer(xlrec->node, BRIN_METAPAGE_BLKNO, true);
	Assert(BufferIsValid(buf));
	page = (Page) BufferGetPage(buf);
	brin_metapage_init(page, xlrec->pagesPerRange, xlrec->version);
	PageSetLSN(page, lsn);
	MarkBufferDirty(buf);
	UnlockReleaseBuffer(buf);
}

/*
 * Common part of an insert or update. Inserts the new tuple and updates the
 * revmap.
 */
static void
brin_xlog_insert_update(XLogRecPtr lsn, XLogRecord *record,
						xl_brin_insert *xlrec, BrinTuple *tuple)
{
	BlockNumber blkno;
	Buffer		buffer;
	Page		page;
	XLogRedoAction action;

	blkno = ItemPointerGetBlockNumber(&xlrec->tid);

	/*
	 * If we inserted the first and only tuple on the page, re-initialize the
	 * page from scratch.
	 */
	if (record->xl_info & XLOG_BRIN_INIT_PAGE)
	{
		XLogReadBufferForRedoExtended(lsn, record, 0,
									  xlrec->node, MAIN_FORKNUM, blkno,
									  RBM_ZERO, false, &buffer);
		page = BufferGetPage(buffer);
		brin_page_init(page, BRIN_PAGETYPE_REGULAR);
		action = BLK_NEEDS_REDO;
	}
	else
	{
		action = XLogReadBufferForRedo(lsn, record, 0,
									   xlrec->node, blkno, &buffer);
	}

	/* insert the index item into the page */
	if (action == BLK_NEEDS_REDO)
	{
		OffsetNumber offnum;

		Assert(tuple->bt_blkno == xlrec->heapBlk);

		page = (Page) BufferGetPage(buffer);
		offnum = ItemPointerGetOffsetNumber(&(xlrec->tid));
		if (PageGetMaxOffsetNumber(page) + 1 < offnum)
			elog(PANIC, "brin_xlog_insert_update: invalid max offset number");

		offnum = PageAddItem(page, (Item) tuple, xlrec->tuplen, offnum, true,
							 false);
		if (offnum == InvalidOffsetNumber)
			elog(PANIC, "brin_xlog_insert_update: failed to add tuple");

		PageSetLSN(page, lsn);
		MarkBufferDirty(buffer);
	}
	if (BufferIsValid(buffer))
		UnlockReleaseBuffer(buffer);

	/* update the revmap */
	action = XLogReadBufferForRedo(lsn, record, 1, xlrec->node,
								   xlrec->revmapBlk, &buffer);
	if (action == BLK_NEEDS_REDO)
	{
		page = (Page) BufferGetPage(buffer);

		brinSetHeapBlockItemptr(buffer, xlrec->pagesPerRange, xlrec->heapBlk,
								xlrec->tid);<commit_msg>BRIN: fix bug in xlog backup block counting<commit_after>/*
 * brin_xlog.c
 *		XLog replay routines for BRIN indexes
 *
 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/access/brin/brin_xlog.c
 */
#include "postgres.h"

#include "access/brin_page.h"
#include "access/brin_pageops.h"
#include "access/brin_xlog.h"
#include "access/xlogutils.h"


/*
 * xlog replay routines
 */
static void
brin_xlog_createidx(XLogRecPtr lsn, XLogRecord *record)
{
	xl_brin_createidx *xlrec = (xl_brin_createidx *) XLogRecGetData(record);
	Buffer		buf;
	Page		page;

	/* Backup blocks are not used in create_index records */
	Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));

	/* create the index' metapage */
	buf = XLogReadBuffer(xlrec->node, BRIN_METAPAGE_BLKNO, true);
	Assert(BufferIsValid(buf));
	page = (Page) BufferGetPage(buf);
	brin_metapage_init(page, xlrec->pagesPerRange, xlrec->version);
	PageSetLSN(page, lsn);
	MarkBufferDirty(buf);
	UnlockReleaseBuffer(buf);
}

/*
 * Common part of an insert or update. Inserts the new tuple and updates the
 * revmap.
 */
static void
brin_xlog_insert_update(XLogRecPtr lsn, XLogRecord *record,
						xl_brin_insert *xlrec, BrinTuple *tuple)
{
	BlockNumber blkno;
	Buffer		buffer;
	Page		page;
	XLogRedoAction action;

	blkno = ItemPointerGetBlockNumber(&xlrec->tid);

	/*
	 * If we inserted the first and only tuple on the page, re-initialize the
	 * page from scratch.
	 */
	if (record->xl_info & XLOG_BRIN_INIT_PAGE)
	{
		/*
		 * No full-page image here.  Don't try to read it, because there
		 * might be one for the revmap buffer, below.
		 */
		buffer = XLogReadBuffer(xlrec->node, blkno, true);
		page = BufferGetPage(buffer);
		brin_page_init(page, BRIN_PAGETYPE_REGULAR);
		action = BLK_NEEDS_REDO;
	}
	else
	{
		action = XLogReadBufferForRedo(lsn, record, 0,
									   xlrec->node, blkno, &buffer);
	}

	/* insert the index item into the page */
	if (action == BLK_NEEDS_REDO)
	{
		OffsetNumber offnum;

		Assert(tuple->bt_blkno == xlrec->heapBlk);

		page = (Page) BufferGetPage(buffer);
		offnum = ItemPointerGetOffsetNumber(&(xlrec->tid));
		if (PageGetMaxOffsetNumber(page) + 1 < offnum)
			elog(PANIC, "brin_xlog_insert_update: invalid max offset number");

		offnum = PageAddItem(page, (Item) tuple, xlrec->tuplen, offnum, true,
							 false);
		if (offnum == InvalidOffsetNumber)
			elog(PANIC, "brin_xlog_insert_update: failed to add tuple");

		PageSetLSN(page, lsn);
		MarkBufferDirty(buffer);
	}
	if (BufferIsValid(buffer))
		UnlockReleaseBuffer(buffer);

	/* update the revmap */
	action = XLogReadBufferForRedo(lsn, record,
								   record->xl_info & XLOG_BRIN_INIT_PAGE ? 0 : 1,
								   xlrec->node,
								   xlrec->revmapBlk, &buffer);
	if (action == BLK_NEEDS_REDO)
	{
		page = (Page) BufferGetPage(buffer);

		brinSetHeapBlockItemptr(buffer, xlrec->pagesPerRange, xlrec->heapBlk,
								xlrec->tid);
{'fraction_non_alphanumeric': 0.11736608665984306, 'fraction_numerical': 0.005458887751620608, 'mean_word_length': 4.305882352941176, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4023211', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2169, 'n_words': 489}