Skip to content

Add materialized_postgresql_use_extended_date_and_time_types setting - #107428

Merged
alexey-milovidov merged 9 commits into
masterfrom
matpg-date-time-types-setting
Jun 24, 2026
Merged

alexey-milovidov merged 9 commits into
masterfrom
matpg-date-time-types-setting

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 14, 2026 •

Copy link
Copy Markdown
Member

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 carry sub-second precision) than ClickHouse Date (up to 2149) and DateTime (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 (default 1 = current behavior). When set to 0, the narrower Date/DateTime types 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 Nullable and Array, so it covers array columns too. It is a create-time / type-inference setting for the MaterializedPostgreSQL database engine: it only chooses the column types when the nested tables are created. It cannot be changed afterwards with ALTER 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 the MaterializedPostgreSQL table 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 date column becomes Date, a timestamp becomes DateTime (microseconds truncated), array columns are narrowed, and ongoing replication keeps working. Further tests cover the rejection paths for the table engine and for ALTER 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):

  • Improvement

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_types setting for the MaterializedPostgreSQL database engine. By default (enabled), PostgreSQL date/timestamp columns are inferred as Date32/DateTime64; setting it to 0 at CREATE DATABASE time infers the narrower Date/DateTime types. The setting is not applicable to the MaterializedPostgreSQL table engine.

Documentation entry for user-facing changes

  • Documentation is written

🤖 Generated with Claude Code

Version info

  • Merged into: 26.6.1.1197

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]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0400ae6]

Summary: ✅


AI Review

Summary

This PR adds the create-time materialized_postgresql_use_extended_date_and_time_types setting for MaterializedPostgreSQL, keeping the existing Date32/DateTime64 inference by default and allowing database-engine creation to infer narrower Date/DateTime types instead. I found no remaining correctness issues: the setting is rejected where it would be a no-op, rejected on later ALTER DATABASE ... MODIFY SETTING, applied consistently to inferred physical/key/replica-identity column types, and covered by focused integration tests for scalar, array, replication, table-engine rejection, and atomic alter validation.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 14, 2026
alexey-milovidov and others added 2 commits June 14, 2026 00:51
- 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 murphy-4o left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please fix the @clickhouse-gh's review comments as well

@murphy-4o murphy-4o self-assigned this Jun 16, 2026
alexey-milovidov and others added 2 commits June 16, 2026 10:34
…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]>
alexey-milovidov and others added 2 commits June 19, 2026 21:01
`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.
@clickhouse-gh

clickhouse-gh Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.30% +0.10%
Functions 92.50% 92.50% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 45/80 (56.25%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 24, 2026
Merged via the queue into master with commit b735fb1 Jun 24, 2026
327 of 328 checks passed
@alexey-milovidov
alexey-milovidov deleted the matpg-date-time-types-setting branch June 24, 2026 02:28
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 24, 2026
alexey-milovidov added a commit that referenced this pull request Jun 24, 2026
Add changelog entries for PRs that landed on the 26.6 release line after
the first pass: #82414, #108042 (New Feature), #107428 (Improvement),
and #108128, #108288, #100205, #108029 (Bug Fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…types-setting

Add materialized_postgresql_use_extended_date_and_time_types setting
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MaterializedPostgreSQL] Use Date32 and Datetime64 by default

3 participants