Repository navigation
Add materialized_postgresql_use_extended_date_and_time_types setting - #107428
Conversation
MaterializedPostgreSQL maps PostgreSQL `date` and `timestamp`/`timestamptz` to ClickHouse `Date32` and `DateTime64(6)` by default, because the PostgreSQL types cover a much wider value range (and timestamps have sub-second precision) than ClickHouse `Date` and `DateTime`. Add a setting `materialized_postgresql_use_extended_date_and_time_types` (default `1`, the current behaviour) that allows falling back to the narrower `Date` and `DateTime` types for users who prefer them (e.g. for storage size or downstream compatibility) and whose values fit their range. The narrowing is applied when fetching the table structure for the nested table, recursing into `Nullable` and `Array`, so it affects both the table and database engines. Closes: #43153 Related: #49045 Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [0400ae6] Summary: ✅
AI ReviewSummaryThis PR adds the create-time Final VerdictStatus: ✅ Approve |
- The `date`/`timestamp` columns are nullable, so they map to `Nullable(Date)` / `Nullable(DateTime)`; fix the assertions accordingly (the previous test expected the non-nullable types). - Add `date[]`/`timestamp[]` columns to cover the Array recursion of the narrowing, and assert no `Date32`/`DateTime64` remains for any column. - Use a kebab-case anchor for the new documentation heading. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…es-setting # Conflicts: # tests/integration/test_postgresql_replica_database_engine/test_3.py
murphy-4o
left a comment
There was a problem hiding this comment.
LGTM, but please fix the @clickhouse-gh's review comments as well
…es-setting # Conflicts: # tests/integration/test_postgresql_replica_database_engine/test_3.py
Two findings from the AI review (which @murphy-4o asked to address): - The ongoing-replication check in `test_use_extended_date_and_time_types_setting` only asserted `count() = 2`. `MaterializedPostgreSQLConsumer::readTupleData` catches conversion failures and still inserts the row with default values, so a broken WAL decoder for the narrowed `Date`/`DateTime` or array types would pass a count-only check. Now assert the replicated `d`, `t`, `arr_d` and `arr_t` values for `key = 2`. - The new `materialized_postgresql_use_extended_date_and_time_types` setting was documented only in the static Markdown page, while the registered database-engine documentation in `DatabaseMaterializedPostgreSQL.cpp` (exposed via `system.database_engines`) still omitted it. Add the same section there. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…e table engine The setting `materialized_postgresql_use_extended_date_and_time_types` is declared in the shared `MaterializedPostgreSQLSettings`, so `ENGINE = MaterializedPostgreSQL` accepts it too. However, the table-engine path builds its nested table from the user-declared column metadata in `StorageMaterializedPostgreSQL::getCreateNestedTableQuery`, and only uses the narrowed `fetchTableStructure` result for the database engine. As a result the setting was a silent no-op for the table engine, even though the PR description implied it covered both engines. For the table engine the user declares the column types explicitly, so the setting can never affect anything there: a user who wants the narrower `Date`/`DateTime` types simply declares them directly. Rather than silently ignore the setting, reject it at table creation with a clear exception that points to the database engine and to declaring the desired types directly. Add a focused integration test that the table engine rejects the setting. Addresses review: #107428 (comment) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`materialized_postgresql_use_extended_date_and_time_types` is only honored while the nested table metadata is inferred at `CREATE DATABASE` time; the already created nested tables keep their fixed column types. Previously an `ALTER DATABASE ... MODIFY SETTING` for it fell through to a bare `Unknown setting` exception in `DatabaseMaterializedPostgreSQL::applySettingsChanges`, which the surrounding docs (saying settings are changeable via `ALTER DATABASE`) made misleading. Reject such a change with an explicit `QUERY_NOT_ALLOWED` exception explaining the create-time/type-inference lifecycle, and document this immutability in both the user-visible docs and the registered database-engine docs. Add an integration test asserting the `ALTER DATABASE` rejection. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The immutable-setting rejection in `DatabaseMaterializedPostgreSQL::applySettingsChanges` was not atomic with the rest of the `ALTER DATABASE ... MODIFY SETTING` list: a mutable setting (such as `materialized_postgresql_max_block_size`) placed before `materialized_postgresql_use_extended_date_and_time_types` in the same statement was already applied to the live `replication_handler` and the in-memory `settings` before the immutable setting aborted the statement, while `modifySettingsMetadata` was skipped. The statement reported an exception, but the live database kept the mutable change until restart. Validate the whole `settings_changes` list (unknown, immutable, and not-allowed settings) before applying anything, so a rejected statement leaves the database unchanged. Added a regression test (`test_use_extended_date_and_time_types_setting_alter_database_atomic`) that places the mutable setting before the immutable one and checks it is not changed.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 45/80 (56.25%) | Lost baseline coverage: none · Uncovered code |
…e table engine The setting `materialized_postgresql_use_extended_date_and_time_types` is declared in the shared `MaterializedPostgreSQLSettings`, so `ENGINE = MaterializedPostgreSQL` accepts it too. However, the table-engine path builds its nested table from the user-declared column metadata in `StorageMaterializedPostgreSQL::getCreateNestedTableQuery`, and only uses the narrowed `fetchTableStructure` result for the database engine. As a result the setting was a silent no-op for the table engine, even though the PR description implied it covered both engines. For the table engine the user declares the column types explicitly, so the setting can never affect anything there: a user who wants the narrower `Date`/`DateTime` types simply declares them directly. Rather than silently ignore the setting, reject it at table creation with a clear exception that points to the database engine and to declaring the desired types directly. Add a focused integration test that the table engine rejects the setting. Addresses review: ClickHouse#107428 (comment) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…types-setting Add materialized_postgresql_use_extended_date_and_time_types setting
Add changelog entries for PRs that landed on the 26.6 release line after the first pass: ClickHouse#82414, ClickHouse#108042 (New Feature), ClickHouse#107428 (Improvement), and ClickHouse#108128, ClickHouse#108288, ClickHouse#100205, ClickHouse#108029 (Bug Fix). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
MaterializedPostgreSQLmaps PostgreSQLdateandtimestamp/timestamptzto ClickHouseDate32andDateTime64(6)by default, because the PostgreSQL types cover a much wider value range (and timestamps carry sub-second precision) than ClickHouseDate(up to 2149) andDateTime(up to 2106, second resolution). This is the right default and stays unchanged.#43153 asked for this to be controllable. This PR adds the setting
materialized_postgresql_use_extended_date_and_time_types(default1= current behavior). When set to0, the narrowerDate/DateTimetypes are used instead, for users who prefer them (smaller storage, downstream compatibility) and whose values fit the narrower range.The narrowing is applied when fetching the table structure used to create the nested table, recursing into
NullableandArray, so it covers array columns too. It is a create-time / type-inference setting for theMaterializedPostgreSQLdatabase engine: it only chooses the column types when the nested tables are created. It cannot be changed afterwards withALTER DATABASE ... MODIFY SETTING(the already created nested tables keep their fixed column types — such a change is rejected with a clear exception), and it is not applicable to theMaterializedPostgreSQLtable engine, where the user declares the column types explicitly (it is rejected at table creation there).An integration test verifies that with the setting disabled a
datecolumn becomesDate, atimestampbecomesDateTime(microseconds truncated), array columns are narrowed, and ongoing replication keeps working. Further tests cover the rejection paths for the table engine and forALTER DATABASE ... MODIFY SETTING(including a multi-setting statement, which is validated atomically so a rejected change leaves no other setting modified).Closes: #43153
Related: #49045
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added the create-time
materialized_postgresql_use_extended_date_and_time_typessetting for theMaterializedPostgreSQLdatabase engine. By default (enabled), PostgreSQLdate/timestampcolumns are inferred asDate32/DateTime64; setting it to0atCREATE DATABASEtime infers the narrowerDate/DateTimetypes. The setting is not applicable to theMaterializedPostgreSQLtable engine.Documentation entry for user-facing changes
🤖 Generated with Claude Code
Version info
26.6.1.1197