Skip to content

Commit 0b46b4c

Browse files
committed
Start work on AlterIndex
1 parent 066d246 commit 0b46b4c

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
@@ -275,6 +275,140 @@ CheckIndexCompatible(Oid oldId,
275275
return ret;
276276
}
277277

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