Subject: Re: Please implement a catch-all error handler per row, for COPY

From: Tom Lane <[email-masked]>
Date: Sat, 01 Mar 2025 11:20:12 -0500

me nefcanto <[email-masked]> writes:
> Can you please provide a row-level catch-all handler for the copy command?

Very unlikely to happen.  COPY is not intended as a general purpose
ETL mechanism, and we don't want to load it down with features that
would create substantial performance penalties.  Which that would.

Even ignoring the performance angle, this request seems remarkably
ill-specified.  What is a "row-level handler" for errors that have
to do with identifying row boundaries?

			regards, tom lane

---

From: "David G. Johnston" <[email-masked]>
Date: Sat, 1 Mar 2025 09:35:54 -0700

> would create substantial performance penalties.  Which that would.

Maybe it isn't a general purpose ETL tool but there is no reasonable way to
do some things unless COPY can be put into a mode that doesn't have the
same performance requirements it needs to serve as our dump/restore tool of
choice.

I have to imagine such a mode, if not enabled, would have little to no
impact on how COPY behaves compared to today.  It's kinda like VACUUM FULL
existing doesn't impact how VACUUM behaves.  Seems more desirable than
inventing a whole new SQL Command to do this and copy-paste all of the COPY
code since it does mostly the same thing.

In short, it's probably worth giving it a try if someone wants to.
Rejecting it without seeing the proposal seems premature.

Now, it's probably challenging enough that if the person requesting the
feature isn't driving its development the odds of it getting worked on is
fairly low.

David J.

---

From: Greg Sabino Mullane <[email-masked]>
Date: Sat, 1 Mar 2025 12:14:37 -0500

FYI the -bugs thread in question:

https://www.postgresql.org/message-id/flat/CAEHBEOBCweDWGNHDaUk4%3D10HG0QXXJJAGXbEnFLMB30M%2BQw%2Bdg%40mail.gmail.com

seems to imply the primary blocker was a unique constraint.

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support

---

From: me nefcanto <[email-masked]>
Date: Mon, 3 Mar 2025 07:14:52 +0330

Thank you all for responding. With respect, I don't understand why COPY
gets related to ETL. All of the requirements I mentioned above have nothing
to do with ETL. We have a table of categories. A hierarchical table. This
is a multitenant app. Hundreds of thousands of records are in it. We want
to calculate the hierarchical properties (nested set models, node depth,
determining leaves, materialized paths, etc.) and then update the entire
table. What does this have to do with ETL? Or as I said we create millions
of records for products, attributes of products, and pricing and media of
products to load test our system. Again, what does that have to do with ETL?

The point is, that there is already an `on_error ignore` clause there. This
means that somewhere there is a try/catch per row. If I'm wrong, please let
me know. But when the `on_error ignore` catches problem x for each row,
then it can catch all problems for each row without any performance problem.

Let me give you an example in C#:

try
{
}
catch (SomeException ex)
{
}

becomes:

try
{
}
catch (Exception ex)
{
    if (ex is SomeException)
    {
    }
}

The last catch clause catches everything. How does it affect performance?
Running a simple if for hundreds of millions of iterations is literally
nothing in time complexity.

As I have specified in the bug thread, from 11 RDBMSs, 7 support this. Thus
it's not an uncommon weird request.

Regards
Saeed

---

From: Christophe Pettus <[email-masked]>
Date: Sun, 2 Mar 2025 19:54:20 -0800

> On Mar 2, 2025, at 19:44, me nefcanto <[email-masked]> wrote:
> 
> As I have specified in the bug thread, from 11 RDBMSs, 7 support this. Thus it's not an uncommon weird request.

If your organization is interested in producing a design and a patch, or paying a developer or organization to do so, that would be the best way forward.  Everyone who works on the core PostgreSQL code is either a volunteer or extremely busy.  Those that are paid to work on PostgreSQL usually have their priorities mapped out for months (years?) already.  The best way to convince the community to adopt a feature to come bearing a high-quality patch.  That's no guarantee, of course, but the probability is much higher that way.

---

From: Tom Lane <[email-masked]>
Date: Sun, 02 Mar 2025 23:07:27 -0500

me nefcanto <[email-masked]> writes:
> me know.

You are wrong.  See

https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=d9f7f5d32

https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=9e2d87011

and a ton of related commits.

It's possible that the specific case of unique-index violations
could be handled in a similar style.  But "catch any error whatever"
is simply not going to happen here, because a subtransaction per
row is not practical.

			regards, tom lane

---

From: Adrian Klaver <[email-masked]>
Date: Sun, 2 Mar 2025 21:33:07 -0800

On 3/2/25 19:44, me nefcanto wrote:
> gets related to ETL. All of the requirements I mentioned above have 
> nothing to do with ETL. We have a table of categories. A hierarchical 
> table. This is a multitenant app. Hundreds of thousands of records are 
> in it. We want to calculate the hierarchical properties (nested set 
> models, node depth, determining leaves, materialized paths, etc.) and 
> then update the entire table. What does this have to do with ETL? Or as

Pretty sure it defines ETL, extract data from a source, transform it to 
meet the properties and load it.

At any rate:

1) ON_ERROR IGNORE only just appeared in Postgres 17.

2) Any further changes to it would constitute a feature change that can 
only happen in major version upgrade. The next major version is due this 
Fall and development is already well under way. The chances of it making 
it in given the time frame is slim, so that pushes a practicable 
deadline until Fall of 2026.

3) As I mentioned in a previous post there are packages already out 
there that may get you want you want now.

-- 
Adrian Klaver
[email-masked]

---

From: Laurenz Albe <[email-masked]>
Date: Mon, 03 Mar 2025 09:55:03 +0100

On Mon, 2025-03-03 at 07:14 +0330, me nefcanto wrote:
> The point is, that there is already an `on_error ignore` clause there. This means that
> somewhere there is a try/catch per row. If I'm wrong, please let me know.

The crucial point that Tom referred to is the "per row".  What is a row?

Imagine you have a COPY FROM statement for a table with three columns,
and the data look like this:

1,Smith,John
2,Lewis,Jerry
Lee
3,Prince
4,Albe,Laurenz

We may be able to guess what is meant, but how shall the machine know
where the line boundaries are, which data to ignore and which to process?

Currently, that is no problem, because errors are only identified
after the data have been successfully parsed.

Yours,
Laurenz Albe