Fix reorganize table without sorting.

Commit 5adff6ff0b separated the
data copy from creating table. This is a cause of bug that
pg_repack doesn't actually sort table during reorganization.
This commit fixes this issue by adding ORDER BY clause to Copy
SQL rather than CREATE TABLE SQL.

Reported by acmzero on issue #138.
This commit is contained in:
Masahiko Sawada
2017-08-05 02:31:47 +09:00
parent cac8179299
commit 33f4c30563
4 changed files with 130 additions and 12 deletions

View File

@ -772,6 +772,7 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
for (i = 0; i < num; i++)
{
repack_table table;
StringInfoData copy_sql;
const char *create_table_1;
const char *create_table_2;
const char *tablespace;
@ -814,17 +815,27 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
table.sql_pop = getstr(res, i, c++);
tablespace = getstr(res, i, c++);
/* Craft CREATE TABLE SQL */
resetStringInfo(&sql);
appendStringInfoString(&sql, create_table_1);
appendStringInfoString(&sql, tablespace);
appendStringInfoString(&sql, create_table_2);
/* Always append WITH NOT DATA to CREATE TABLE SQL*/
appendStringInfoString(&sql, " WITH NO DATA");
table.create_table = sql.data;
/* Craft Copy SQL */
initStringInfo(&copy_sql);
appendStringInfoString(&copy_sql, table.copy_data);
if (!orderby)
{
if (ckey != NULL)
{
/* CLUSTER mode */
appendStringInfoString(&sql, " ORDER BY ");
appendStringInfoString(&sql, ckey);
appendStringInfoString(&copy_sql, " ORDER BY ");
appendStringInfoString(&copy_sql, ckey);
}
/* else, VACUUM FULL mode (non-clustered tables) */
@ -836,13 +847,10 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
else
{
/* User specified ORDER BY */
appendStringInfoString(&sql, " ORDER BY ");
appendStringInfoString(&sql, orderby);
appendStringInfoString(&copy_sql, " ORDER BY ");
appendStringInfoString(&copy_sql, orderby);
}
/* Always append WITH NOT DATA */
appendStringInfoString(&sql, " WITH NO DATA");
table.create_table = sql.data;
table.copy_data = copy_sql.data;
repack_one_table(&table, orderby);
}