Skip to content

Unlogged index fix v15 #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions src/backend/commands/sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static HTAB *seqhashtab = NULL; /* hash table for SeqTable items */
static SeqTableData *last_used_seq = NULL;

static void fill_seq_with_data(Relation rel, HeapTuple tuple);
static void fill_seq_fork_with_data(Relation rel, HeapTuple tuple, ForkNumber forkNum);
static void fill_seq_fork_with_data(Relation rel, HeapTuple tuple, ForkNumber forkNum, Buffer buf);
static Relation lock_and_open_sequence(SeqTable seq);
static void create_seq_hashtable(void);
static void init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel);
Expand Down Expand Up @@ -351,7 +351,7 @@ ResetSequence(Oid seq_relid)
static void
fill_seq_with_data(Relation rel, HeapTuple tuple)
{
fill_seq_fork_with_data(rel, tuple, MAIN_FORKNUM);
fill_seq_fork_with_data(rel, tuple, MAIN_FORKNUM, InvalidBuffer);

if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
{
Expand All @@ -360,7 +360,7 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
srel = smgropen(rel->rd_node, InvalidBackendId, rel->rd_rel->relpersistence);
smgrcreate(srel, INIT_FORKNUM, false);
log_smgrcreate(&rel->rd_node, INIT_FORKNUM);
fill_seq_fork_with_data(rel, tuple, INIT_FORKNUM);
fill_seq_fork_with_data(rel, tuple, INIT_FORKNUM, InvalidBuffer);
FlushRelationBuffers(rel);
smgrclose(srel);
}
Expand All @@ -370,28 +370,28 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
* Initialize a sequence's relation fork with the specified tuple as content
*/
static void
fill_seq_fork_with_data(Relation rel, HeapTuple tuple, ForkNumber forkNum)
fill_seq_fork_with_data(Relation rel, HeapTuple tuple, ForkNumber forkNum, Buffer buf)
{
Buffer buf;
Page page;
sequence_magic *sm;
OffsetNumber offnum;
bool lockBuffer = false;

/* Initialize first page of relation with special magic number */

buf = ReadBufferExtended(rel, forkNum, P_NEW, RBM_NORMAL, NULL);
Assert(BufferGetBlockNumber(buf) == 0);

if (buf == InvalidBuffer)
{
buf = ReadBufferExtended(rel, forkNum, P_NEW, RBM_NORMAL, NULL);
Assert(BufferGetBlockNumber(buf) == 0);
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
lockBuffer = true;
}
page = BufferGetPage(buf);

PageInit(page, BufferGetPageSize(buf), sizeof(sequence_magic));
sm = (sequence_magic *) PageGetSpecialPointer(page);
sm->magic = SEQ_MAGIC;

/* Now insert sequence tuple */

LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);

/*
* Since VACUUM does not process sequences, we have to force the tuple to
* have xmin = FrozenTransactionId now. Otherwise it would become
Expand Down Expand Up @@ -440,7 +440,8 @@ fill_seq_fork_with_data(Relation rel, HeapTuple tuple, ForkNumber forkNum)

END_CRIT_SECTION();

UnlockReleaseBuffer(buf);
if (lockBuffer)
UnlockReleaseBuffer(buf);
}

/*
Expand Down Expand Up @@ -1215,9 +1216,29 @@ read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
sm = (sequence_magic *) PageGetSpecialPointer(page);

if (sm->magic != SEQ_MAGIC)
elog(ERROR, "bad magic number in sequence \"%s\": %08X",
RelationGetRelationName(rel), sm->magic);

{
/* NEON: reinitialize unlogged sequence */
if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
{
Datum value[SEQ_COL_LASTCOL] = {0};
bool null[SEQ_COL_LASTCOL] = {false};
HeapTuple tuple;
Form_pg_sequence pgsform;

tuple = SearchSysCache1(SEQRELID, RelationGetRelid(rel));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for sequence %u", RelationGetRelid(rel));
pgsform = (Form_pg_sequence) GETSTRUCT(tuple);
value[SEQ_COL_LASTVAL-1] = Int64GetDatumFast(pgsform->seqstart);
ReleaseSysCache(tuple);

tuple = heap_form_tuple(RelationGetDescr(rel), value, null);
fill_seq_fork_with_data(rel, tuple, MAIN_FORKNUM, *buf);
}
else
elog(ERROR, "bad magic number in sequence \"%s\": %08X",
RelationGetRelationName(rel), sm->magic);
}
lp = PageGetItemId(page, FirstOffsetNumber);
Assert(ItemIdIsNormal(lp));

Expand Down
38 changes: 37 additions & 1 deletion src/backend/optimizer/util/plancat.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_statistic_ext.h"
Expand All @@ -47,6 +48,8 @@
#include "rewrite/rewriteManip.h"
#include "statistics/statistics.h"
#include "storage/bufmgr.h"
#include "storage/buf_internals.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/partcache.h"
Expand Down Expand Up @@ -81,6 +84,39 @@ static void set_baserel_partition_key_exprs(Relation relation,
static void set_baserel_partition_constraint(Relation relation,
RelOptInfo *rel);

static bool
is_index_valid(Relation index, LOCKMODE lmode)
{
if (!index->rd_index->indisvalid)
return false;

if (index->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
{
while (true)
{
Buffer metapage = ReadBuffer(index, 0);
bool isNew = PageIsNew(BufferGetPage(metapage));
ReleaseBuffer(metapage);
if (isNew)
{
Relation heap;
if (lmode != ExclusiveLock)
{
UnlockRelation(index, lmode);
LockRelation(index, ExclusiveLock);
lmode = ExclusiveLock;
continue;
}
DropRelFileNodesAllBuffers(&index->rd_smgr, 1);
heap = RelationIdGetRelation(index->rd_index->indrelid);
index->rd_indam->ambuild(heap, index, BuildIndexInfo(index));
RelationClose(heap);
}
break;
}
}
return true;
}

/*
* get_relation_info -
Expand Down Expand Up @@ -224,7 +260,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
* still needs to insert into "invalid" indexes, if they're marked
* indisready.
*/
if (!index->indisvalid)
if (!is_index_valid(indexRelation, lmode))
{
index_close(indexRelation, NoLock);
continue;
Expand Down
25 changes: 23 additions & 2 deletions src/backend/storage/smgr/md.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)

fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);

/*
* NEON: unlogged relation files are lost after compute restart - we need to implicitly recreate them
* to allow data insertion
*/
if (fd < 0 && (behavior & EXTENSION_CREATE))
fd = PathNameOpenFile(path, O_RDWR | O_CREAT | PG_BINARY);

if (fd < 0)
{
if ((behavior & EXTENSION_RETURN_NULL) &&
Expand Down Expand Up @@ -681,9 +688,23 @@ mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
reln->smgr_rnode.node.relNode,
reln->smgr_rnode.backend);

/* NEON: md smgr is used in Neon for unlogged and temp relations.
* After compute node restart their data is deleted but unlogged tables are still present in system catalog.
* This is a difference with Vanilla Postgres where unlogged relations are truncated only after abnormal termination.
* To avoid "could not open file" we have to use EXTENSION_RETURN_NULL hear instead of EXTENSION_FAIL
*/
v = _mdfd_getseg(reln, forknum, blocknum, false,
EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY);

RelFileNodeBackendIsTemp(reln->smgr_rnode)
? EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY
: EXTENSION_RETURN_NULL);
if (v == NULL)
{
char* path = relpath(reln->smgr_rnode, forknum);
(void)PathNameOpenFile(path, O_RDWR | O_CREAT | PG_BINARY);
pfree(path);
MemSet(buffer, 0, BLCKSZ);
return;
}
seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));

Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
Expand Down