Re: [Proposal] Adding TRIM_SPACE option to COPY - Mailing list pgsql-hackers

From daidewei1970@163.com
Subject Re: [Proposal] Adding TRIM_SPACE option to COPY
Date
Msg-id 202511251251504038821@163.com
Whole thread Raw
In response to [Proposal] Adding TRIM_SPACE option to COPY  (河田達也 <kawatatatsuya0913@gmail.com>)
List pgsql-hackers
HI,

This change seems very useful, I participated in a related project before. After reviewing the patch, 
I have a suggestion, would it be better to restrict the change to only "copy from"?


daidewei1970@163.com
 
Date: 2025-11-25 00:17
Subject: [Proposal] Adding TRIM_SPACE option to COPY
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

pgsql-hackers by date:

Previous
From: Dilip Kumar
Date:
Subject: Re: Patch: dumping tables data in multiple chunks in pg_dump
Next
From: Fujii Masao
Date:
Subject: Re: [Proposal] Adding TRIM_SPACE option to COPY