Skip to content

Commit f3fa2fa

Browse files
committed
Fixes and cleanup of the new repack_index_swap():
* Make sure we don't crash if this function is fed a bogus OID * rename idx1, idx2 variables for clarity * tweak query against pg_class to specify reltype = 0 * CommandCounterIncrement() is probably unnecessary, removed
1 parent ee23ec8 commit f3fa2fa

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

lib/repack.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,11 +1193,20 @@ swap_heap_or_index_files(Oid r1, Oid r2)
11931193
heap_close(relRelation, RowExclusiveLock);
11941194
}
11951195

1196+
/**
1197+
* @fn Datum repack_index_swap(PG_FUNCTION_ARGS)
1198+
* @brief Swap out an original index on a table with the newly-created one.
1199+
*
1200+
* repack_index_swap(index)
1201+
*
1202+
* @param index Oid of the *original* index.
1203+
* @retval void
1204+
*/
11961205
Datum
11971206
repack_index_swap(PG_FUNCTION_ARGS)
11981207
{
1199-
Oid oid = PG_GETARG_OID(0);
1200-
Oid idx1, idx2;
1208+
Oid orig_idx_oid = PG_GETARG_OID(0);
1209+
Oid repacked_idx_oid;
12011210
StringInfoData str;
12021211
SPITupleTable *tuptable;
12031212
TupleDesc desc;
@@ -1209,16 +1218,22 @@ repack_index_swap(PG_FUNCTION_ARGS)
12091218
/* connect to SPI manager */
12101219
repack_init();
12111220

1212-
idx1 = oid;
12131221
initStringInfo(&str);
1214-
appendStringInfo(&str,"SELECT oid FROM pg_class WHERE relname = 'index_%u'",idx1);
1222+
1223+
/* Find the OID of our new index. Indexes should have a reltype of 0. */
1224+
appendStringInfo(&str, "SELECT oid FROM pg_class "
1225+
"WHERE relname = 'index_%u' AND reltype = 0",
1226+
orig_idx_oid);
12151227
execute(SPI_OK_SELECT, str.data);
1228+
if (SPI_processed != 1)
1229+
elog(ERROR, "Could not find index 'index_%u', found %d matches",
1230+
orig_idx_oid, SPI_processed);
1231+
12161232
tuptable = SPI_tuptable;
12171233
desc = tuptable->tupdesc;
12181234
tuple = tuptable->vals[0];
1219-
idx2 = getoid(tuple, desc, 1);
1220-
swap_heap_or_index_files(idx1, idx2);
1221-
CommandCounterIncrement();
1235+
repacked_idx_oid = getoid(tuple, desc, 1);
1236+
swap_heap_or_index_files(orig_idx_oid, repacked_idx_oid);
12221237
SPI_finish();
12231238
PG_RETURN_VOID();
12241239
}

0 commit comments

Comments
 (0)