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:
@ -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(©_sql);
|
||||
appendStringInfoString(©_sql, table.copy_data);
|
||||
if (!orderby)
|
||||
|
||||
{
|
||||
if (ckey != NULL)
|
||||
{
|
||||
/* CLUSTER mode */
|
||||
appendStringInfoString(&sql, " ORDER BY ");
|
||||
appendStringInfoString(&sql, ckey);
|
||||
appendStringInfoString(©_sql, " ORDER BY ");
|
||||
appendStringInfoString(©_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(©_sql, " ORDER BY ");
|
||||
appendStringInfoString(©_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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user