Skip to content

Commit 83fdb2a

Browse files
committed
Added implementation for --moveidx
Note: if original namespace is "foo bar", repack_indexdef gives a bad result. This is weird as apparently skip_ident can deal with spaces in a quoted identifier. Committing as I'm going home, will deal with that later.
1 parent 43dfe22 commit 83fdb2a

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

bin/pg_repack.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ static bool
681681
rebuild_indexes(const repack_table *table)
682682
{
683683
PGresult *res;
684-
const char *params[1];
684+
const char *params[2];
685685
int num_indexes;
686686
int i;
687687
int num_active_workers;
@@ -693,6 +693,7 @@ rebuild_indexes(const repack_table *table)
693693
elog(DEBUG2, "---- create indexes ----");
694694

695695
params[0] = utoa(table->target_oid, buffer);
696+
params[1] = moveidx ? tablespace : NULL;
696697

697698
/* First, just display a warning message for any invalid indexes
698699
* which may be on the table (mostly to match the behavior of 1.1.8).
@@ -708,8 +709,9 @@ rebuild_indexes(const repack_table *table)
708709
}
709710

710711
res = execute("SELECT indexrelid,"
711-
" repack.repack_indexdef(indexrelid, indrelid) "
712-
" FROM pg_index WHERE indrelid = $1 AND indisvalid", 1, params);
712+
" repack.repack_indexdef(indexrelid, indrelid, $2) "
713+
" FROM pg_index WHERE indrelid = $1 AND indisvalid",
714+
2, params);
713715

714716
num_indexes = PQntuples(res);
715717

lib/pg_repack.sql.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ CREATE VIEW repack.tables AS
207207
AND N.nspname NOT IN ('pg_catalog', 'information_schema')
208208
AND N.nspname NOT LIKE E'pg\\_temp\\_%';
209209

210-
CREATE FUNCTION repack.repack_indexdef(oid, oid) RETURNS text AS
210+
CREATE FUNCTION repack.repack_indexdef(oid, oid, name) RETURNS text AS
211211
'MODULE_PATHNAME', 'repack_indexdef'
212-
LANGUAGE C STABLE STRICT;
212+
LANGUAGE C STABLE;
213213

214214
CREATE FUNCTION repack.repack_trigger() RETURNS trigger AS
215215
'MODULE_PATHNAME', 'repack_trigger'

lib/repack.c

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,20 +618,62 @@ repack_get_order_by(PG_FUNCTION_ARGS)
618618
*
619619
* @param index Oid of target index.
620620
* @param table Oid of table of the index.
621+
* @param tablespace Namespace for the index. If NULL keep the original.
621622
* @retval Create index DDL for temp table.
622623
*/
623624
Datum
624625
repack_indexdef(PG_FUNCTION_ARGS)
625626
{
626-
Oid index = PG_GETARG_OID(0);
627-
Oid table = PG_GETARG_OID(1);
627+
Oid index;
628+
Oid table;
629+
Name tablespace = NULL;
628630
IndexDef stmt;
629631
StringInfoData str;
630632

633+
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
634+
PG_RETURN_NULL();
635+
636+
index = PG_GETARG_OID(0);
637+
table = PG_GETARG_OID(1);
638+
639+
if (!PG_ARGISNULL(2))
640+
tablespace = PG_GETARG_NAME(2);
641+
631642
parse_indexdef(&stmt, index, table);
643+
632644
initStringInfo(&str);
633-
appendStringInfo(&str, "%s index_%u ON repack.table_%u USING %s (%s)%s",
634-
stmt.create, index, table, stmt.type, stmt.columns, stmt.options);
645+
appendStringInfo(&str, "%s index_%u ON repack.table_%u USING %s (%s)",
646+
stmt.create, index, table, stmt.type, stmt.columns);
647+
648+
/* Replace the tablespace in the index options */
649+
if (tablespace == NULL)
650+
{
651+
/* tablespace is just fine */
652+
appendStringInfoString(&str, stmt.options);
653+
}
654+
else
655+
{
656+
const char *pos;
657+
if (NULL == (pos = strstr(stmt.options, " TABLESPACE ")))
658+
{
659+
/* tablespace is to append */
660+
appendStringInfoString(&str, " TABLESPACE ");
661+
appendStringInfoString(&str, NameStr(*tablespace));
662+
}
663+
else
664+
{
665+
char *tmp;
666+
667+
/* tablespace is to replace */
668+
tmp = skip_const(index, stmt.options, " TABLESPACE ", NULL);
669+
appendStringInfoString(&str, stmt.options);
670+
appendStringInfoString(&str, NameStr(*tablespace));
671+
/* FIXME: not working if original ts has a space. But skip_ident
672+
* should deal with that. Stupid mistake somewhere? */
673+
tmp = skip_ident(index, tmp);
674+
appendStringInfoString(&str, tmp);
675+
}
676+
}
635677

636678
PG_RETURN_TEXT_P(cstring_to_text(str.data));
637679
}

0 commit comments

Comments
 (0)