Обсуждение: Replacing an index item

Поиск
Список
Период
Сортировка

Replacing an index item

От
Carsten Kropf
Дата:
Hello everybody,
I have a question about the replacement of an item in an index page. I want to overwrite an existing item inside an index page. However, I noticed that each time, I replaced an item, the free space in the given page was decreasing. I didn't want to delete the existing item and insert a new one properly, however this seems to be the only option I have (based on this observation).
My code (in C) was the following before:

// key tuple of child element
iid
= PageGetItemId(state->stack->parent->page, state->stack->parent->childoffnum);
ItemIdSetUnused(iid);
PageIndexTupleDelete(state->stack->parent->page,
state->stack->parent->childoffnum);
// key tuple for parent is first tuple of children entry
keyTup
= (IndexTuple) PageGetItem(state->stack->page, PageGetItemId(state->stack->page, FirstOffsetNumber));


PageAddItem(state->stack->parent->page, (Item) keyTup,
IndexTupleSize(keyTup), state->stack->parent->childoffnum,
true, false);

However, this resulted (as I already mentioned) in an ever decreasing free space in the page which after that resulted in a page overflow at some point (resulting in splits etc).
The code, I now use looks like the following:
PageIndexTupleDelete(state->stack->parent->page,
state->stack->parent->childoffnum);
//PageIndexTupleDelete(state->stack->parent->page, state->stack->parent->childoffnum);
// key tuple for parent is first tuple of children entry
keyTup
= (IndexTuple) PageGetItem(state->stack->page, PageGetItemId(state->stack->page, FirstOffsetNumber));

itupvec[0] = (IndexTuple) palloc0(IndexTupleSize(keyTup));
memcpy(itupvec[0], keyTup, IndexTupleSize(keyTup));

putTuple(state->r, state->stack->parent->page, state->stack->parent->buffer, itupvec, 1, state->stack->parent->childoffnum, btreestate);

Whereas the putTuple refers to a function that puts the tuple and shifts existing tuples, if necessary. However, I want to avoid this, because it still requires a little bit of overhead to reorganize it properly. I would prefer the first option, but it does not seem to have good results based on the "waste" of space. Could anyone please give me some help according to this issue?

Best regards
Carsten Kropf

Re: Replacing an index item

От
Tom Lane
Дата:
Carsten Kropf <ckropf2@fh-hof.de> writes:
> I have a question about the replacement of an item in an index page. I
> want to overwrite an existing item inside an index page.

Why exactly do you want to do that?  How are you going to make it
transactionally correct or crash-safe?

FWIW, I think the ItemIdSetUnused call is wrong or at least unnecessary.
Perhaps it is confusing PageIndexTupleDelete.

            regards, tom lane