Skip to content

Commit 036d06d

Browse files
knizhnikkelvich
authored andcommitted
Start work on AlterIndex
1 parent 04945c0 commit 036d06d

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

src/backend/commands/indexcmds.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,140 @@ CheckIndexCompatible(Oid oldId,
280280
return ret;
281281
}
282282

283+
#if 0
284+
void
285+
AlterIndex(Oid relationId, IndexStmt *stmt, Oid indexRelationId)
286+
{
287+
char* select;
288+
IndexUniqueCheck checkUnique;
289+
bool satisfiesConstraint;
290+
Datum values[INDEX_MAX_KEYS];
291+
bool isnull[INDEX_MAX_KEYS];
292+
Relation heapRelation;
293+
Relation indexRelation;
294+
SPIPlanPtr plan;
295+
Portal portal;
296+
HeapTuple tuple;
297+
TupleDesc tupdesc;
298+
TupleTableSlot *slot;
299+
ItemPointer tupleid;
300+
IndexInfo *indexInfo;
301+
EState *estate;
302+
303+
Assert(stmt->whereClause);
304+
305+
/* Open and lock the parent heap relation */
306+
heapRelation = heap_openrv(stmt->relation, ShareUpdateExclusiveLock);
307+
308+
/* And the target index relation */
309+
indexRelation = index_open(indexRelationId, RowExclusiveLock);
310+
311+
indexInfo = BuildIndexInfo(indexRelation);
312+
Assert(indexInfo->ii_Predicate);
313+
Assert(!indexInfo->ii_ExclusionOps);
314+
315+
/*
316+
* Generate the constraint and default execution states
317+
*/
318+
estate = CreateExecutorState();
319+
320+
checkUnique = indexRelation->rd_index->indisunique ? UNIQUE_CHECK_YES : UNIQUE_CHECK_NO;
321+
322+
SPI_connect();
323+
select = psprintf("select * from %s where %s and not (%s)",
324+
quote_qualified_identifier(get_namespace_name(RelationGetNamespace(heapRelation)),
325+
get_rel_name(relationId)),
326+
nodeToString(indexInfo->ii_Predicate),
327+
nodeToString(stmt->whereClause)
328+
);
329+
plan = SPI_parepare(select, 0, NULL);
330+
if (plan == NULL) {
331+
ereport(ERROR,
332+
(errcode(ERRCODE_INVALID_CURSOR_STATE),
333+
errmsg("Failed to preapre statement ", select)));
334+
}
335+
portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
336+
if (portal == NULL) {
337+
ereport(ERROR,
338+
(errcode(ERRCODE_INVALID_CURSOR_STATE),
339+
errmsg("Failed to open cursor for ", select)));
340+
}
341+
while (true)
342+
{
343+
SPI_cursor_fetch(portal, true, 1);
344+
if (!SPI_processed) {
345+
break;
346+
}
347+
tuple = SPI_tuptable->vals[0];
348+
tupdesc = SPI_tuptable->tupdesc;
349+
slot = TupleDescGetSlot(tupdesc);
350+
tupleid = &tuple->t_datat->t_ctid;
351+
352+
/* delete tuple from index */
353+
}
354+
SPI_cursor_close(portal);
355+
356+
357+
select = psprintf("select * from %s where %s and not (%s)",
358+
quote_qualified_identifier(get_namespace_name(RelationGetNamespace(heapRelation)),
359+
get_rel_name(relationId)),
360+
nodeToString(stmt->whereClause),
361+
nodeToString(indexInfo->ii_Predicate)
362+
);
363+
plan = SPI_parepare(select, 0, NULL);
364+
if (plan == NULL) {
365+
ereport(ERROR,
366+
(errcode(ERRCODE_INVALID_CURSOR_STATE),
367+
errmsg("Failed to preapre statement ", select)));
368+
}
369+
portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
370+
if (portal == NULL) {
371+
ereport(ERROR,
372+
(errcode(ERRCODE_INVALID_CURSOR_STATE),
373+
errmsg("Failed to open cursor for ", select)));
374+
}
375+
while (true)
376+
{
377+
SPI_cursor_fetch(portal, true, 1);
378+
if (!SPI_processed) {
379+
break;
380+
}
381+
tuple = SPI_tuptable->vals[0];
382+
tupdesc = SPI_tuptable->tupdesc;
383+
slot = TupleDescGetSlot(tupdesc);
384+
tupleid = &tuple->t_datat->t_ctid;
385+
386+
FormIndexDatum(indexInfo,
387+
slot,
388+
estate,
389+
values,
390+
isnull);
391+
satisfiesConstraint =
392+
index_insert(indexRelation, /* index relation */
393+
values, /* array of index Datums */
394+
isnull, /* null flags */
395+
tupleid, /* tid of heap tuple */
396+
heapRelation, /* heap relation */
397+
checkUnique); /* type of uniqueness check to do */
398+
399+
if (!satisfiesConstraint)
400+
{
401+
ereport(ERROR,
402+
(errcode(ERRCODE_INTEGRITY_CONSTRAINT_VIOLATION),
403+
errmsg("Index constraint violation")));
404+
}
405+
SPI_freetuple(tuple);
406+
SPI_freetuptable(SPI_tuptable);
407+
}
408+
SPI_cursor_close(portal);
409+
SPI_finish();
410+
411+
/* Close both the relations, but keep the locks */
412+
heap_close(heapRelation, NoLock);
413+
index_close(indexRelation, NoLock);
414+
}
415+
#endif
416+
283417
/*
284418
* DefineIndex
285419
* Creates a new index.

src/include/commands/defrem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern bool CheckIndexCompatible(Oid oldId,
4242
List *attributeList,
4343
List *exclusionOpNames);
4444
extern Oid GetDefaultOpClass(Oid type_id, Oid am_id);
45+
extern void AlterIndex(Oid relationId, IndexStmt *stmt, Oid indexRelationId);
4546

4647
/* commands/functioncmds.c */
4748
extern ObjectAddress CreateFunction(CreateFunctionStmt *stmt, const char *queryString);

0 commit comments

Comments
 (0)