Skip to content

Commit 3b85662

Browse files
committed
Add comment explaining how race condition between CREATE TABLE ... AS SELECT and recording of log entries is avoided.
1 parent d402a73 commit 3b85662

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

bin/pg_repack.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ repack_one_table(const repack_table *table, const char *orderby)
507507
*/
508508
elog(DEBUG2, "---- copy tuples ----");
509509

510+
/* Must use SERIALIZABLE (or at least not READ COMMITTED) to avoid race
511+
* condition between the create_table statement and rows subsequently
512+
* being added to the log.
513+
*/
510514
command("BEGIN ISOLATION LEVEL SERIALIZABLE", 0, NULL);
511515
/* SET work_mem = maintenance_work_mem */
512516
command("SELECT set_config('work_mem', current_setting('maintenance_work_mem'), true)", 0, NULL);
@@ -515,6 +519,12 @@ repack_one_table(const repack_table *table, const char *orderby)
515519
res = execute(SQL_XID_SNAPSHOT, 0, NULL);
516520
vxid = strdup(PQgetvalue(res, 0, 0));
517521
PQclear(res);
522+
523+
/* Delete any existing entries in the log table now, since we have not
524+
* yet run the CREATE TABLE ... AS SELECT, which will take in all existing
525+
* rows from the target table; if we also included prior rows from the
526+
* log we could wind up with duplicates.
527+
*/
518528
command(table->delete_log, 0, NULL);
519529
command(table->create_table, 0, NULL);
520530
printfStringInfo(&sql, "SELECT repack.disable_autovacuum('repack.table_%u')", table->target_oid);

0 commit comments

Comments
 (0)