Initial import of indexes-only building patch.

Patch from Beena Emerson.
This commit is contained in:
Josh Kupershmidt
2013-06-21 18:41:09 -04:00
parent 5b6a162996
commit ee23ec8ffd
3 changed files with 319 additions and 18 deletions

View File

@ -207,7 +207,7 @@ CREATE VIEW repack.tables AS
AND N.nspname NOT IN ('pg_catalog', 'information_schema')
AND N.nspname NOT LIKE E'pg\\_temp\\_%';
CREATE FUNCTION repack.repack_indexdef(oid, oid, name) RETURNS text AS
CREATE FUNCTION repack.repack_indexdef(oid, oid, name, bool) RETURNS text AS
'MODULE_PATHNAME', 'repack_indexdef'
LANGUAGE C STABLE;
@ -246,3 +246,7 @@ LANGUAGE C VOLATILE STRICT;
CREATE FUNCTION repack.repack_drop(oid) RETURNS void AS
'MODULE_PATHNAME', 'repack_drop'
LANGUAGE C VOLATILE STRICT;
CREATE FUNCTION repack.repack_index_swap(oid) RETURNS void AS
'MODULE_PATHNAME', 'repack_index_swap'
LANGUAGE C STABLE STRICT;

View File

@ -46,6 +46,7 @@ extern Datum PGUT_EXPORT repack_indexdef(PG_FUNCTION_ARGS);
extern Datum PGUT_EXPORT repack_swap(PG_FUNCTION_ARGS);
extern Datum PGUT_EXPORT repack_drop(PG_FUNCTION_ARGS);
extern Datum PGUT_EXPORT repack_disable_autovacuum(PG_FUNCTION_ARGS);
extern Datum PGUT_EXPORT repack_index_swap(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(repack_version);
PG_FUNCTION_INFO_V1(repack_trigger);
@ -55,6 +56,7 @@ PG_FUNCTION_INFO_V1(repack_indexdef);
PG_FUNCTION_INFO_V1(repack_swap);
PG_FUNCTION_INFO_V1(repack_drop);
PG_FUNCTION_INFO_V1(repack_disable_autovacuum);
PG_FUNCTION_INFO_V1(repack_index_swap);
static void repack_init(void);
static SPIPlanPtr repack_prepare(const char *src, int nargs, Oid *argtypes);
@ -674,6 +676,7 @@ repack_indexdef(PG_FUNCTION_ARGS)
Name tablespace = NULL;
IndexDef stmt;
StringInfoData str;
bool concurrent_index = PG_GETARG_BOOL(3);
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
PG_RETURN_NULL();
@ -687,8 +690,12 @@ repack_indexdef(PG_FUNCTION_ARGS)
parse_indexdef(&stmt, index, table);
initStringInfo(&str);
appendStringInfo(&str, "%s index_%u ON repack.table_%u USING %s (%s)%s",
stmt.create, index, table, stmt.type, stmt.columns, stmt.options);
if(concurrent_index)
appendStringInfo(&str, "%s CONCURRENTLY index_%u ON %s USING %s (%s)%s",
stmt.create, index, stmt.table, stmt.type, stmt.columns, stmt.options);
else
appendStringInfo(&str, "%s index_%u ON repack.table_%u USING %s (%s)%s",
stmt.create, index, table, stmt.type, stmt.columns, stmt.options);
/* specify the new tablespace or the original one if any */
if (tablespace || stmt.tablespace)
@ -1186,6 +1193,36 @@ swap_heap_or_index_files(Oid r1, Oid r2)
heap_close(relRelation, RowExclusiveLock);
}
Datum
repack_index_swap(PG_FUNCTION_ARGS)
{
Oid oid = PG_GETARG_OID(0);
Oid idx1, idx2;
StringInfoData str;
SPITupleTable *tuptable;
TupleDesc desc;
HeapTuple tuple;
/* authority check */
must_be_superuser("repack_index_swap");
/* connect to SPI manager */
repack_init();
idx1 = oid;
initStringInfo(&str);
appendStringInfo(&str,"SELECT oid FROM pg_class WHERE relname = 'index_%u'",idx1);
execute(SPI_OK_SELECT, str.data);
tuptable = SPI_tuptable;
desc = tuptable->tupdesc;
tuple = tuptable->vals[0];
idx2 = getoid(tuple, desc, 1);
swap_heap_or_index_files(idx1, idx2);
CommandCounterIncrement();
SPI_finish();
PG_RETURN_VOID();
}
#if PG_VERSION_NUM < 80400
/* XXX: You might need to add PGDLLIMPORT into your miscadmin.h. */