From eb1b54c034b99552be55723d8047347a8cb1c87a Mon Sep 17 00:00:00 2001 From: Babak Farrokhi Date: Fri, 24 Mar 2023 17:36:30 +0100 Subject: [PATCH] Add "-X" option to avoid waiting for other transactions There was a conservative wait[1] for all existing transaction to finish before repacking a table. This may not be desired on a busy database on which you have long running transaction, which may block pg_repack to do the job. This option will display a warning message, but continues the process in such cases. [1] http://lists.pgfoundry.org/pipermail/reorg-general/2015-October/000318.html --- bin/pg_repack.c | 12 ++++++++++++ doc/pg_repack.rst | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/bin/pg_repack.c b/bin/pg_repack.c index 5bcee125..4204ad54 100644 --- a/bin/pg_repack.c +++ b/bin/pg_repack.c @@ -240,6 +240,7 @@ static bool sqlstate_equals(PGresult *res, const char *state) static bool analyze = true; static bool alldb = false; +static bool ignore_transactions = false; static bool noorder = false; static SimpleStringList parent_table_list = {NULL, NULL}; static SimpleStringList table_list = {NULL, NULL}; @@ -269,6 +270,7 @@ utoa(unsigned int value, char *buffer) static pgut_option options[] = { { 'b', 'a', "all", &alldb }, + { 'b', 'X', "ignore-transactions", &ignore_transactions }, { 'l', 't', "table", &table_list }, { 'l', 'I', "parent-table", &parent_table_list }, { 'l', 'c', "schema", &schema_list }, @@ -1565,6 +1567,15 @@ repack_one_table(repack_table *table, const char *orderby) res = execute(SQL_XID_ALIVE, 1, params); num = PQntuples(res); + /* display a warning if there are still one or more transactions + * alive, but continue + */ + if (ignore_transactions) + { + elog(NOTICE, "There are %d transactions waiting to finish, but skipping.", num); + break; + } + if (num > 0) { /* Wait for old transactions. @@ -2355,4 +2366,5 @@ pgut_help(bool details) printf(" -Z, --no-analyze don't analyze at end\n"); printf(" -k, --no-superuser-check skip superuser checks in client\n"); printf(" -C, --exclude-extension don't repack tables which belong to specific extension\n"); + printf(" -X, --ignore-transactions don't wait for running transactions (use with care)\n"); } diff --git a/doc/pg_repack.rst b/doc/pg_repack.rst index 2aecb26e..3936a85e 100644 --- a/doc/pg_repack.rst +++ b/doc/pg_repack.rst @@ -123,6 +123,7 @@ Options: -Z, --no-analyze don't analyze at end -k, --no-superuser-check skip superuser checks in client -C, --exclude-extension don't repack tables which belong to specific extension + -X, --ignore-transactions don't wait for running transactions (use with care) Connection options: -d, --dbname=DBNAME database to connect @@ -224,6 +225,12 @@ Reorg Options Skip tables that belong to the specified extension(s). Some extensions may heavily depend on such tables at planning time etc. +``-X``, ``--ignore-transactions`` + Proceed without waiting for running transactions to finish. + Default behavior might not be desired on busy databases with + long-running transactions + + Connection Options ^^^^^^^^^^^^^^^^^^