Skip to content

Fix PostgreSQL proxy timestamp text output adding trailing .0#38566

Open
daguimu wants to merge 1 commit intoapache:masterfrom
daguimu:fix/postgresql-timestamp-trailing-zero-37437
Open

Fix PostgreSQL proxy timestamp text output adding trailing .0#38566
daguimu wants to merge 1 commit intoapache:masterfrom
daguimu:fix/postgresql-timestamp-trailing-zero-37437

Conversation

@daguimu
Copy link
Copy Markdown

@daguimu daguimu commented Mar 27, 2026

Problem

When querying a TIMESTAMP column without fractional seconds through ShardingSphere-Proxy (PostgreSQL protocol), the result includes a trailing .0 (e.g., 1973-06-03 10:30:01.0), whereas native PostgreSQL returns 1973-06-03 10:30:01. This formatting mismatch can break clients and tests relying on stable textual output.

Root Cause

In PostgreSQLDataRowPacket#writeTextValue, non-binary column values fall through to the generic each.toString() branch. When the value is a java.sql.Timestamp, Timestamp.toString() always emits a fractional part (at minimum .0), which does not match PostgreSQL's text protocol behavior of omitting trailing zero fractional seconds.

Fix

  • Added a type-aware branch for java.sql.Timestamp in writeTextValue that formats the timestamp according to PostgreSQL text output rules:
    • Omits the fractional part entirely when there are no fractional seconds
    • Uses microsecond precision (matching PostgreSQL's internal precision) and strips trailing zeros from the fractional part
  • The formatting logic is encapsulated in a private formatTimestamp method within PostgreSQLDataRowPacket

Tests Added

Change Point Test
Timestamp without fractional seconds omits .0 timestamp_no_fractional — verifies 1973-06-03 10:30:01 (exact reproduction from issue)
Timestamp with fractional seconds preserved timestamp_with_fractional — verifies 2024-01-15 14:30:45.123
Trailing zeros in fractional part stripped timestamp_trailing_zeros_stripped — verifies .120 becomes .12
Full microsecond precision supported timestamp_microsecond_precision — verifies 2024-01-15 14:30:45.123456

Impact

Only affects PostgreSQL text protocol timestamp formatting in the proxy. No changes to binary protocol, other data types, or any other module. Existing behavior for all non-Timestamp types is unchanged.

Fixes #37437

@daguimu daguimu force-pushed the fix/postgresql-timestamp-trailing-zero-37437 branch from 0f0f9c1 to 3f3924a Compare March 27, 2026 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL proxy: timestamp output adds trailing ".0" compared to native PostgreSQL

1 participant