[Proposal] Adding TRIM_SPACE option to COPY

From: 河田達也 <kawatatatsuya0913(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [Proposal] Adding TRIM_SPACE option to COPY
Date: 2025-11-24 16:17:19
Message-ID: CAHza6qfd1gNZAAj63AsCfisoUdxu=Y7QZP4vtojmk-YCvQnkxA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I'd like to propose adding a new option, TRIM_SPACE, to the COPY command.

Other data warehouse systems such as Snowflake provide similar functionality
(TRIM_SPACE) to improve robustness when loading CSV data. PostgreSQL does
not currently have
such an option, although it would be consistent with other user-friendly
features already present in COPY (e.g., FORCE_NULL, FORCE_NOT_NULL,
ON_ERROR).

Proposed feature
----------------
Add a boolean option:

TRIM_SPACE = true | false
(default: false)

When enabled, COPY FROM with FORMAT text or csv will trim leading and
trailing
ASCII whitespace from each column value before NULL processing and type
conversion. This is applied only to FORMAT text and csv.(not binary)

Example usage:

COPY mytable
FROM '/tmp/data.csv'
WITH (FORMAT csv, HEADER true, TRIM_SPACE true);

This would transform(trim leading and trailing ASCII whitespace):
" AAA ", " BBB", "CCC "

into:
'AAA', 'BBB', 'CCC'
----------------

Thanks in advance for your comments.

Best regards,
Tatsuya Kawata

Attachment Content-Type Size
v1-0001-Adding-TRIM_SPACE-option-to-COPY.patch application/octet-stream 4.3 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2025-11-24 16:19:42 Re: get rid of Pointer type, mostly
Previous Message Robert Haas 2025-11-24 16:14:27 Re: pg_plan_advice