Repository navigation
Fix server abort on PostgreSQL value type mismatch - #110264
PedroTadim merged 7 commits into
Conversation
Reading a PostgreSQL text value into a numeric declared type (e.g. a column declared Int32 over a text result) made pqxx::from_string throw pqxx::conversion_error, a foreign std::exception that escaped insertPostgreSQLValue and aborted the server (signal 6 / LOGICAL_ERROR in debug and sanitizer builds) instead of failing the query. Convert pqxx::conversion_error (covering the conversion_overrun overflow subclass) into a BAD_ARGUMENTS query error. Applies to both the query-passing and the plain table-name PostgreSQL paths. Add integration coverage for the query-passing feature edge paths: missing permissions, empty result sets, and type mismatches. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-6:20260713-121100 |
|
cc @kssenii @scanhex12 — could you review this? A PostgreSQL text value read into a numeric declared type made |
|
Workflow [PR], commit [10829d5] Summary: ✅
AI ReviewSummaryThis PR normalizes PR MetadataThe chosen Findings❌ Blockers [src/Core/PostgreSQL/insertPostgreSQLValue.cpp:206] The new translation layer promises to turn bad Final VerdictChanges requested: the remaining |
…-parse errors insertPostgreSQLValue now translates a foreign pqxx::conversion_error into a DB::Exception(BAD_ARGUMENTS). MaterializedPostgreSQLConsumer::insertValue only caught the raw pqxx::conversion_error, so the translated exception escaped to readTupleData, which inserts a default and rethrows. consume() then returns false without advancing the replication slot, and the buffered row is re-inserted on every retry, duplicating replicated rows indefinitely. Broaden the caller's catch to also handle the translated BAD_ARGUMENTS the same way as the raw conversion error (log and insert a default), so replication keeps advancing. Other DB::Exceptions still propagate unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch, addressed in 1092dad. Broadening the raw- |
| @@ -1186,6 +1186,63 @@ def test_postgres_query_passing(started_cluster): | |||
| cursor.execute(f"DROP TABLE {dim_name}") | |||
|
|
|||
|
|
|||
| def test_postgres_query_passing_edge_cases(started_cluster): | |||
| cursor = started_cluster.postgres_conn.cursor() | |||
There was a problem hiding this comment.
@groeneai add the same test for MySQL and SQLite. Test a number of query projections that mismatch with the number of columns in the table.
insertPostgreSQLValue previously only caught pqxx::conversion_error. Text values read into declared UUID/Date/DateTime/DateTime64/Decimal types throw DB::Exception (CANNOT_PARSE_*) or, for Date/Date32, a foreign std::runtime_error from LocalDate::init. The latter escaped the catch entirely and aborted the server (STD_EXCEPTION), and the former reached readTupleData in the MaterializedPostgreSQL consumer as a non-BAD_ARGUMENTS code, bypassing the consumer catch that keeps replication advancing. Broaden the value-layer catch: parse errors (isParseError) and BAD_ARGUMENTS become a single BAD_ARGUMENTS query error; foreign std::exception is likewise translated; genuine failures (LOGICAL_ERROR, memory limit) still propagate. Add regression coverage for declared-type and projection-count mismatches across the postgresql, mysql and sqlite query-passing paths. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed fc3efeb. @clickhouse-gh The value-layer catch is broadened.
So all declared-type mismatches reach the consumer as @PedroTadim Added MySQL and SQLite coverage for projection-count and type mismatch:
Session id: cron:clickhouse-worker-slot-3:20260713-142300 |
| "Cannot parse PostgreSQL value '{}' as {}: {}", value, data_type->getName(), e.message()); | ||
| throw; | ||
| } | ||
| catch (const std::exception & e) |
There was a problem hiding this comment.
catch (const std::exception &) is broader than the foreign parse failures this helper needs to normalize. This function does ordinary heap allocations itself (std::string(value) for Date/Date32, String(value) in the Int256 overflow branch), and ClickHouse's global operator new can throw std::bad_alloc (src/Common/AllocationInterceptors.cpp:50-100).
After this translation, the query path reports that as BAD_ARGUMENTS, and MaterializedPostgreSQLConsumer::insertValue treats it as a bad source value and inserts a default. That turns memory pressure into silent data loss instead of propagating the real failure. Can we rethrow std::bad_alloc here, and ideally narrow this catch to the specific foreign parse exceptions we actually expect?
Fewer declared columns than the passed query returns is not an error: the PostgreSQL and MySQL engines take the first N result columns positionally and drop the rest. The TOO_MANY_COLUMNS / NUMBER_OF_COLUMNS_DOESNT_MATCH assertions were wrong and failed deterministically (the query succeeds). Keep the empty- result, missing-permission and type-mismatch coverage, which is the point. Co-Authored-By: Claude Opus 4.8 <[email protected]>
A PostgreSQL numeric that parses but does not fit the declared ClickHouse Decimal is rejected by the Decimal text reader with ARGUMENT_OUT_OF_BOUND (already in isParseError), which the shared value layer normalizes to BAD_ARGUMENTS. It therefore surfaces as a query error and reaches the MaterializedPostgreSQL catch, never a foreign exception or server abort. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Verified against source and a live PostgreSQL 16: The Decimal text reader guards precision before the scale multiply. Empirical check confirms this. An exhaustive sweep over P in {9,18,38,76} and S in {0, P/2, P-1, P} with boundary/e-notation values produced zero That reaches the Since there is no reproducible |
A prefix-valid PostgreSQL text value like '1abc' read into a declared Decimal does not break MaterializedPostgreSQL replication: SerializationDecimal::deserializeText push_back's the parsed prefix and throwUnexpectedDataAfterParsedValue popBack(1)'s it before throwing, so the destination column stays consistent. The consumer inserts a default and replication keeps advancing. Test-only change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Verified against source: this path does not break replication.
Confirmed end to end with an integration test: a PostgreSQL No source change is warranted here. Added the regression test ( |
…ng edge test Inserting 99999999999999999999999999999999999999 through ClickHouse typed the literal as Float64 and failed the INSERT with DECIMAL_OVERFLOW before the read path under test was reached. Seed the value via a PostgreSQL-side INSERT so it stays an exact numeric and the declared-Decimal read surfaces BAD_ARGUMENTS as intended. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
CI update: the With the value seeded PG-side, the declared- |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 10/18 (55.56%) · Uncovered code |
CI finish ledger — 10829d5All CI checks are green, including all four Bugfix validation checks (SUCCESS). The
Session id: cron:our-pr-ci-monitor:20260714-093000 |
5d2993a
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRFix server abort on PostgreSQL value type mismatch ClickGap verified: reproduced it with the script below on Reproduction-- On the PostgreSQL side:
CREATE TABLE t (a integer, b text);
INSERT INTO t VALUES (1, 'name_0');
-- On ClickHouse: declare the text column b as Int32 (type mismatch).
CREATE TABLE m (a Int32, b Int32)
ENGINE = PostgreSQL('host:5432', 'db', 't', 'user', 'pass');
SELECT * FROM m;
-- Pre-fix: pqxx::conversion_error (a foreign std::exception) escapes
-- insertPostgreSQLValue -> server abort (signal 6) on debug/sanitizer
-- builds; Code: 1001 (STD_EXCEPTION) on release/RelWithDebInfo builds.
-- Post-fix: Code: 36 (BAD_ARGUMENTS) "Cannot parse PostgreSQL value 'name_0' as Int32".Bisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR author: @groeneai ClickGap · Finding: Triage metadata
|
…ry-type-mismatch-abort Fix server abort on PostgreSQL value type mismatch
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server abort (in debug and sanitizer builds) when a value from a
mysql/postgresql/sqlite-family PostgreSQL source is read into a declared type it cannot be parsed into (for example atextcolumn declared asInt32). Such a type mismatch is now reported as a query error instead of aborting the server.Description
Follow-up to #107740 (which added passing a query to the
postgresqltable function and engine). While extending the integration coverage that PR asked about, the type-mismatch edge path was found to abort the server.Reading a PostgreSQL text value into a numeric declared type makes
pqxx::from_stringthrowpqxx::conversion_error. That is a foreignstd::exception(not aDB::Exception), so it escapedinsertPostgreSQLValueand hitabortOnFailedAssertion(signal 6 /LOGICAL_ERROR) in debug and sanitizer builds instead of failing the query.The fix wraps
insertPostgreSQLValuein a function-try-block that convertspqxx::conversion_error(which also covers theconversion_overrunoverflow subclass) into aBAD_ARGUMENTSerror. The fix is in the value layer shared by the query-passing and the plain table-name PostgreSQL read paths, so it covers both.Repro (aborts the server before the fix):
Also adds integration coverage for the query-passing feature edge paths @PedroTadim asked about on #107740: missing permissions, empty result sets, and type mismatches (
tests/integration/test_storage_postgresql/test.py::test_postgres_query_passing_edge_cases).Version info
26.7.1.905(included in26.7and later)