diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aade30..254bfdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to pgclone are documented in this file. +## [Unreleased] + +No SQL-level changes; after `make install`, restart PostgreSQL. + +### Added +- `"on_error": "continue"` (discussion #41) for `pgclone.schema()` / `pgclone.database()`. A table that fails (for example a COPY into a table whose definition differs) is rolled back in its own subtransaction, reported with a `WARNING` (message and SQLSTATE), skipped from the foreign-key and trigger passes, and the clone carries on with the next table. The function returns `COMPLETED WITH ERRORS: N table(s) failed: ...` instead of `OK`. Default stays `"abort"`. Cancels and shutdowns are never swallowed. `schema_async()` already continued past failed tables, so it needs no option. +- New `test/test_continue_on_error.sh`. + ## [4.8.0] This release has no SQL-level changes; after `make install`, **restart PostgreSQL**, then `ALTER EXTENSION pgclone UPDATE`. diff --git a/docs/USAGE.md b/docs/USAGE.md index 652e812..c9802f7 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -446,6 +446,23 @@ SELECT pgclone.schema_async(conn, 'sales', true, --- +## Continue on Error (discussion #41) + +By default `pgclone.schema()` and `pgclone.database()` stop at the first failing table. Like `psql` without `ON_ERROR_STOP`, you can ask them to carry on: + +```sql +SELECT pgclone.schema('host=src dbname=app user=u', 'public', true, + '{"on_error": "continue"}'); +-- COMPLETED WITH ERRORS: 1 table(s) failed: orders +``` + +- A failing table raises a `WARNING` with the error message and SQLSTATE, then cloning continues with the next table. +- Foreign keys and triggers are not created for a failed table; its dependents are still cloned. +- A table whose `CREATE TABLE` succeeded but whose data load failed is left in place (empty), so you can fix the cause and re-run with `"conflict": "ignore"` or a `"tables"` filter. pgclone never drops it for you. +- The result is `OK` only when nothing failed; with failures it starts with `COMPLETED WITH ERRORS`, so scripts should check for it. +- Query cancel and server shutdown always abort. Values: `"abort"` (default) or `"continue"`. +- Applies to the synchronous functions. `schema_async()` already continues past a failed table and marks the job `failed` at the end; check `pgclone.progress()` / `pgclone.jobs()` for it. + ## Materialized Views (v1.2.0) Materialized views are cloned automatically during schema clone, including their indexes and data. Disable with: diff --git a/src/pgclone.c b/src/pgclone.c index 432a618..8e93697 100644 --- a/src/pgclone.c +++ b/src/pgclone.c @@ -11,6 +11,8 @@ #include "funcapi.h" #include "utils/builtins.h" #include "utils/elog.h" +#include "access/xact.h" +#include "utils/resowner.h" #include "executor/spi.h" #include "libpq-fe.h" #include "miscadmin.h" @@ -124,6 +126,13 @@ typedef struct CloneOptions * that don't use the option keep that exact legacy behavior. */ bool conflict_specified; PgcloneConflictStrategy conflict; + + /* v4.9.0 (discussion #41): "on_error": "continue" makes the + * synchronous schema/database clone skip a table that fails + * (rolling back that table's work), record it, and carry on with + * the next object instead of aborting the whole run. Default is + * false ("abort"), which keeps the historical behavior. */ + bool continue_on_error; } CloneOptions; /* Default: everything enabled, no column/where/mask filter */ @@ -144,6 +153,7 @@ pgclone_default_options(void) opts.snapshot_id[0] = '\0'; opts.conflict_specified = false; opts.conflict = PGCLONE_CONFLICT_ERROR; + opts.continue_on_error = false; return opts; } @@ -365,6 +375,19 @@ pgclone_parse_options(const char *json_str) opts.conflict_specified = true; } + /* v4.9.0 (discussion #41): "on_error": "continue" / "abort" */ + if (strstr(json_str, "\"on_error\": \"continue\"") != NULL || + strstr(json_str, "\"on_error\":\"continue\"") != NULL) + opts.continue_on_error = true; + else if (strstr(json_str, "\"on_error\": \"abort\"") != NULL || + strstr(json_str, "\"on_error\":\"abort\"") != NULL) + opts.continue_on_error = false; + else if (strstr(json_str, "\"on_error\"") != NULL) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("pgclone: invalid \"on_error\" option"), + errhint("Use \"on_error\": \"abort\" (default) or \"continue\"."))); + /* v4.3.0: opt-out of consistent-snapshot wrapping */ if (strstr(json_str, "\"consistent\": false") != NULL || strstr(json_str, "\"consistent\":false") != NULL) @@ -2464,6 +2487,24 @@ pgclone_sync_handle_conflict(PGconn *local_conn, const char *kind, } } +/* v4.9.0: connections currently open inside pgclone_table(). When a + * table clone fails and on_error=continue, the caller closes these so + * the failed table does not leak two libpq connections. Cleared + * whenever pgclone_table closes them itself before raising an error. */ +static PGconn *pgclone_active_src = NULL; +static PGconn *pgclone_active_lcl = NULL; + +static void +pgclone_close_active_conns(void) +{ + if (pgclone_active_lcl != NULL) + PQfinish(pgclone_active_lcl); + if (pgclone_active_src != NULL) + PQfinish(pgclone_active_src); + pgclone_active_lcl = NULL; + pgclone_active_src = NULL; +} + PG_FUNCTION_INFO_V1(pgclone_table); Datum @@ -2522,6 +2563,7 @@ pgclone_table(PG_FUNCTION_ARGS) * If a parent op (pgclone_schema) already exported a snapshot via * opts.snapshot_id, this connection imports it instead. */ source_conn = pgclone_connect(source_conninfo); + pgclone_active_src = source_conn; pgclone_setup_source_txn(source_conn, &opts); /* ---- Step 1: Get CREATE TABLE DDL from source ---- */ @@ -2596,6 +2638,7 @@ pgclone_table(PG_FUNCTION_ARGS) PQclear(res); pgclone_setup_source_txn_done(source_conn, &opts); PQfinish(source_conn); + pgclone_active_src = NULL; ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("pgclone: table \"%s.%s\" not found on source", @@ -2639,6 +2682,7 @@ pgclone_table(PG_FUNCTION_ARGS) /* Use loopback libpq connection for ALL local operations */ local_conn = pgclone_connect_local(); + pgclone_active_lcl = local_conn; /* ---- Step 1b: Create schema locally ---- */ { @@ -2658,6 +2702,8 @@ pgclone_table(PG_FUNCTION_ARGS) PQfinish(local_conn); pgclone_setup_source_txn_done(source_conn, &opts); PQfinish(source_conn); + pgclone_active_lcl = NULL; + pgclone_active_src = NULL; PG_RETURN_TEXT_P(cstring_to_text_with_len("SKIPPED", 7)); } @@ -2755,6 +2801,8 @@ pgclone_table(PG_FUNCTION_ARGS) PQfinish(local_conn); pgclone_setup_source_txn_done(source_conn, &opts); PQfinish(source_conn); + pgclone_active_lcl = NULL; + pgclone_active_src = NULL; pfree(ddl); ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), @@ -2935,6 +2983,8 @@ pgclone_table(PG_FUNCTION_ARGS) PQfinish(local_conn); pgclone_setup_source_txn_done(source_conn, &opts); PQfinish(source_conn); + pgclone_active_lcl = NULL; + pgclone_active_src = NULL; PG_RETURN_TEXT_P(cstring_to_text_with_len("OK", 2)); } @@ -2975,6 +3025,9 @@ pgclone_schema(PG_FUNCTION_ARGS) StringInfoData buf; int i, ntables; char **table_names = NULL; + bool *table_failed = NULL; /* on_error=continue bookkeeping */ + int nfailed = 0; + StringInfoData failed_list; /* Arg 3: JSON options (only for the 4-arg overload where arg 3 is TEXT). * MUST use == 4, NOT >= 4: when called from pgclone_schema_ex with @@ -3028,6 +3081,7 @@ pgclone_schema(PG_FUNCTION_ARGS) /* ---- Step 1: Create schema ---- */ initStringInfo(&buf); + initStringInfo(&failed_list); appendStringInfo(&buf, "CREATE SCHEMA IF NOT EXISTS %s", quote_identifier(schema_name)); pgclone_exec_conn(local_conn, buf.data); @@ -3252,6 +3306,7 @@ pgclone_schema(PG_FUNCTION_ARGS) if (ntables > 0) { + table_failed = palloc0(sizeof(bool) * ntables); table_names = palloc(sizeof(char *) * ntables); for (i = 0; i < ntables; i++) table_names[i] = pstrdup(PQgetvalue(res, i, 0)); @@ -3329,14 +3384,82 @@ pgclone_schema(PG_FUNCTION_ARGS) pgclone_keeper_ping(source_conn); /* Call 6-arg version: conninfo, schema, table, include_data, target_name, options */ - result = DirectFunctionCall6(pgclone_table, - CStringGetTextDatum(source_conninfo), - CStringGetTextDatum(schema_name), - CStringGetTextDatum(table_names[i]), - BoolGetDatum(include_data), - CStringGetTextDatum(table_names[i]), /* target = source name */ - CStringGetTextDatum(tbl_opts.data)); - (void) result; + if (!opts.continue_on_error) + { + result = DirectFunctionCall6(pgclone_table, + CStringGetTextDatum(source_conninfo), + CStringGetTextDatum(schema_name), + CStringGetTextDatum(table_names[i]), + BoolGetDatum(include_data), + CStringGetTextDatum(table_names[i]), /* target = source name */ + CStringGetTextDatum(tbl_opts.data)); + (void) result; + } + else + { + /* v4.9.0 (discussion #41): on_error=continue. Run the + * table inside a subtransaction so a failure leaves no + * backend-side residue, remember it, and move on. */ + MemoryContext oldcxt = CurrentMemoryContext; + ResourceOwner oldowner = CurrentResourceOwner; + + pgclone_active_src = NULL; + pgclone_active_lcl = NULL; + BeginInternalSubTransaction(NULL); + MemoryContextSwitchTo(oldcxt); + + PG_TRY(); + { + result = DirectFunctionCall6(pgclone_table, + CStringGetTextDatum(source_conninfo), + CStringGetTextDatum(schema_name), + CStringGetTextDatum(table_names[i]), + BoolGetDatum(include_data), + CStringGetTextDatum(table_names[i]), + CStringGetTextDatum(tbl_opts.data)); + (void) result; + ReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(oldcxt); + CurrentResourceOwner = oldowner; + } + PG_CATCH(); + { + ErrorData *edata; + + MemoryContextSwitchTo(oldcxt); + edata = CopyErrorData(); + + /* Never swallow cancels or shutdowns. */ + if (edata->sqlerrcode == ERRCODE_QUERY_CANCELED || + edata->sqlerrcode == ERRCODE_ADMIN_SHUTDOWN || + edata->sqlerrcode == ERRCODE_CRASH_SHUTDOWN) + { + pgclone_close_active_conns(); + PG_RE_THROW(); + } + + FlushErrorState(); + RollbackAndReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(oldcxt); + CurrentResourceOwner = oldowner; + + pgclone_close_active_conns(); + + table_failed[i] = true; + nfailed++; + appendStringInfo(&failed_list, "%s%s", + failed_list.len > 0 ? ", " : "", + table_names[i]); + ereport(WARNING, + (errmsg("pgclone: table %s.%s failed, continuing (on_error=continue): %s", + schema_name, table_names[i], + edata->message ? edata->message : "unknown error"), + errdetail("SQLSTATE %s", + unpack_sql_state(edata->sqlerrcode)))); + FreeErrorData(edata); + } + PG_END_TRY(); + } } pfree(tbl_opts.data); @@ -3365,6 +3488,8 @@ pgclone_schema(PG_FUNCTION_ARGS) for (i = 0; i < ntables; i++) { + if (table_failed[i]) + continue; /* on_error=continue: skip failed table */ resetStringInfo(&buf); appendStringInfo(&buf, "SELECT conname, pg_get_constraintdef(con.oid, true) AS condef " @@ -3597,7 +3722,11 @@ pgclone_schema(PG_FUNCTION_ARGS) for (i = 0; i < ntables; i++) { - int n = pgclone_triggers(src_trig, lcl_trig, + int n; + + if (table_failed[i]) + continue; /* on_error=continue: skip failed table */ + n = pgclone_triggers(src_trig, lcl_trig, schema_name, table_names[i], table_names[i]); trig_total += n; @@ -3622,9 +3751,20 @@ pgclone_schema(PG_FUNCTION_ARGS) /* v4.3.0: close the snapshot keeper. Every importer that needed * the snapshot has long since called SET TRANSACTION SNAPSHOT * (or finished its own clone), so committing here is safe. */ + if (table_failed) + pfree(table_failed); + pgclone_setup_source_txn_done(source_conn, &opts); PQfinish(source_conn); + if (nfailed > 0) + { + char *msg = psprintf("COMPLETED WITH ERRORS: %d table(s) failed: %s", + nfailed, failed_list.data); + + PG_RETURN_TEXT_P(cstring_to_text(msg)); + } + PG_RETURN_TEXT_P(cstring_to_text_with_len("OK", 2)); } @@ -3709,6 +3849,10 @@ pgclone_database(PG_FUNCTION_ARGS) PGresult *res; int i, nschemas; char **schema_names; + int db_errors = 0; + StringInfoData db_err_list; + + initStringInfo(&db_err_list); /* Arg 2: JSON options (optional) */ if (PG_NARGS() >= 3 && !PG_ARGISNULL(2)) @@ -3793,11 +3937,15 @@ pgclone_database(PG_FUNCTION_ARGS) appendStringInfo(&opts_json, ", \"conflict\": \"%s\"", pgclone_conflict_strategy_name(opts.conflict)); + if (opts.continue_on_error) + appendStringInfoString(&opts_json, ", \"on_error\": \"continue\""); + appendStringInfoChar(&opts_json, '}'); for (i = 0; i < nschemas; i++) { Datum result; + char *rtxt; /* v4.3.1: keeper liveness check between schemas (issue #9). */ if (opts.consistent && opts.snapshot_id[0] != '\0') @@ -3811,7 +3959,15 @@ pgclone_database(PG_FUNCTION_ARGS) CStringGetTextDatum(schema_names[i]), BoolGetDatum(include_data), CStringGetTextDatum(opts_json.data)); - (void) result; + rtxt = TextDatumGetCString(result); + if (strncmp(rtxt, "COMPLETED WITH ERRORS", 21) == 0) + { + db_errors++; + appendStringInfo(&db_err_list, "%s%s: %s", + db_err_list.len > 0 ? "; " : "", + schema_names[i], rtxt + 23); + } + pfree(rtxt); } pfree(opts_json.data); @@ -3828,6 +3984,11 @@ pgclone_database(PG_FUNCTION_ARGS) pgclone_setup_source_txn_done(source_conn, &opts); PQfinish(source_conn); + if (db_errors > 0) + PG_RETURN_TEXT_P(cstring_to_text( + psprintf("COMPLETED WITH ERRORS: %d schema(s) had failures: %s", + db_errors, db_err_list.data))); + PG_RETURN_TEXT_P(cstring_to_text_with_len("OK", 2)); } diff --git a/test/run_tests.sh b/test/run_tests.sh index 30fdd3e..029ac6e 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -124,6 +124,16 @@ if echo "$SPL" | grep -q "pgclone"; then TEST_EXIT=1 fi + echo "" + echo "============================================" + echo "Running pgclone continue-on-error tests (discussion #41)..." + echo "============================================" + bash /build/pgclone/test/test_continue_on_error.sh 2>&1 + COE_EXIT=$? + if [ $COE_EXIT -ne 0 ]; then + TEST_EXIT=1 + fi + echo "" echo "============================================" echo "Running pgclone chaos tests (worker kill, slot saturation)..." diff --git a/test/test_continue_on_error.sh b/test/test_continue_on_error.sh new file mode 100755 index 0000000..b91add9 --- /dev/null +++ b/test/test_continue_on_error.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# ============================================================ +# pgclone "on_error": "continue" tests (discussion #41) +# +# A table whose target definition is incompatible with the source makes +# COPY fail. Default behavior aborts the whole schema clone; with +# on_error=continue the failing table is reported and the remaining +# tables (and their foreign keys) are still cloned. +# Requires shared_preload_libraries = 'pgclone'. +# ============================================================ + +set -euo pipefail + +SOURCE_CONNINFO="host=source-db dbname=source_db user=postgres password=testpass" +PASS=0 +FAIL=0 + +check() { + local name="$1" expected="$2" actual="$3" + if [ "$expected" = "$actual" ]; then + echo "PASS: $name"; PASS=$((PASS + 1)) + else + echo "FAIL: $name"; echo " expected: '$expected'"; echo " actual: '$actual'" + FAIL=$((FAIL + 1)) + fi +} + +q() { psql -U postgres -d target_db -tAc "$1" 2>&1 | tail -1 | tr -d '\r'; } +src() { PGPASSWORD=testpass psql -h source-db -U postgres -d source_db -q -c "$1" >/dev/null; } + +PGPASSWORD=testpass psql -h source-db -U postgres -d source_db -q >/dev/null </dev/null 2>&1 +} + +echo "---- TEST 1: default aborts ----" +reset_target +OUT=$(psql -U postgres -d target_db -tAc "SELECT pgclone.schema('${SOURCE_CONNINFO}', 'ps_coe', true)" 2>&1 || true) +check "default: error raised" "1" "$(echo "$OUT" | grep -c 'COPY completed with error' || true)" +check "default: good table not cloned" "f" "$(q "SELECT to_regclass('ps_coe.good') IS NOT NULL")" + +echo "---- TEST 2: on_error=continue ----" +reset_target +OUT=$(q "SELECT pgclone.schema('${SOURCE_CONNINFO}', 'ps_coe', true, '{\"on_error\": \"continue\"}')") +check "continue: result reports failure" "COMPLETED WITH ERRORS: 1 table(s) failed: bad" "$OUT" +check "continue: good rows copied" "2" "$(q "SELECT count(*) FROM ps_coe.good")" +check "continue: good FK present" "1" "$(q "SELECT count(*) FROM pg_constraint WHERE conrelid='ps_coe.good'::regclass AND contype='f'")" + +echo "---- TEST 3: database clone aggregates ----" +reset_target +OUT=$(q "SELECT pgclone.database('${SOURCE_CONNINFO}', true, '{\"on_error\": \"continue\"}')") +check "database: result reports failure" "1" "$(echo "$OUT" | grep -c 'COMPLETED WITH ERRORS' || true)" + +echo "---- TEST 4: invalid value rejected ----" +OUT=$(psql -U postgres -d target_db -tAc "SELECT pgclone.schema('${SOURCE_CONNINFO}', 'ps_coe', true, '{\"on_error\": \"bogus\"}')" 2>&1 || true) +check "invalid on_error rejected" "1" "$(echo "$OUT" | grep -c 'invalid \"on_error\"' || true)" + +src "DROP SCHEMA IF EXISTS ps_coe CASCADE" +psql -U postgres -d target_db -q -c "DROP SCHEMA IF EXISTS ps_coe CASCADE" >/dev/null 2>&1 + +echo "" +echo "CONTINUE-ON-ERROR TEST RESULTS: $PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ] || { echo "SOME TESTS FAILED"; exit 1; } +echo "ALL CONTINUE-ON-ERROR TESTS PASSED"