Re: on_error table, saving error info to a table

From: Nishant Sharma <nishant(dot)sharma(at)enterprisedb(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: on_error table, saving error info to a table
Date: 2024-07-15 05:42:40
Message-ID: CADrsxdYb3HZKBnsAUV=H2F_54MUPoqW-VTBbOqFTiNrb+LDN7g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks for the patch!

I was not able to understand why we need a special error table for COPY?
Can't we just log it in a new log error file for COPY in a proper format?
Like
using table approach, PG would be unnecessarily be utilising its resources
like
extra CPU to format the data in pages to store in its table format, writing
and
keeping the table in its store (which may or may not be required), the user
would be required to make sure it creates the error table with proper
columns
to be used in COPY, etc..

Meanwhile, please find some quick review comments:-

1) Patch needs re-base.

2) If the columns of the error table are fixed, then why not create it
internally using
some function or something instead of making the user create the table
correctly
with all the columns?

3) I think, error messages can be improved, which looks big to me.

4) I think no need of below pstrdup, As CStringGetTextDatum creates a text
copy for
the same:-
err_code =
pstrdup(unpack_sql_state(cstate->escontext->error_data->sqlerrcode));

t_values[9] = CStringGetTextDatum(err_code);

5) Should 'on_error_rel' as not NULL be checked along with below, as I can
see it is
passed as NULL from two locations?
+ if (cstate->opts.on_error == COPY_ON_ERROR_TABLE)
+ {

6) Below declarations can be shifted to the if block, where they are used.
Instead of
keeping them as global in function?
+ HeapTuple on_error_tup;
+ TupleDesc on_error_tupDesc;

+ if (cstate->opts.on_error == COPY_ON_ERROR_TABLE)
+ {

7) Below comment going beyond 80 char width:-
* if on_error is specified with 'table', then on_error_rel is the error
saving table

8) Need space after 'false'
err_detail ? false: true;

9) Below call can fit in a single line. No need to keep the 2nd and 3rd
parameter in
nextlines.
+ on_error_tup = heap_form_tuple(on_error_tupDesc,
+ t_values,
+ t_isnull);

10) Variable declarations Tab Spacing issue at multiple places.

11) Comments in the patch are not matched to PG comment style.

Kindly note I have not tested the patch properly yet. Only checked it with
a positive test
case. As I will wait for a conclusion on my opinion of the proposed patch.

Regards,
Nishant Sharma.
EnterpriseDB, Pune.

On Sat, Feb 3, 2024 at 11:52 AM jian he <jian(dot)universality(at)gmail(dot)com> wrote:

> Hi.
> I previously did some work in COPY FROM save error information to a table.
> still based on this suggestion:
> https://www.postgresql.org/message-id/752672.1699474336%40sss.pgh.pa.us
> Now I refactored it.
>
> the syntax:
> ON_ERROR 'table', TABLE 'error_saving_tbl'
>
> if ON_ERROR is not specified with 'table', TABLE is specified, then error.
> if ON_ERROR is specified with 'table', TABLE is not specified or
> error_saving_tbl does not exist, then error.
>
> In BeginCopyFrom, we check the data definition of error_saving_table,
> we also check if the user has INSERT privilege to error_saving_table
> (all the columns).
> We also did a preliminary check of the lock condition of
> error_saving_table.
>
> if it does not meet these conditions, we quickly error out.
> error_saving_table will be the same schema as the copy from table.
>
> Because "table" is a keyword, I have to add the following changes to
> gram.y.
> --- a/src/backend/parser/gram.y
> +++ b/src/backend/parser/gram.y
> @@ -3420,6 +3420,10 @@ copy_opt_item:
> {
> $$ = makeDefElem("null", (Node *) makeString($3), @1);
> }
> + | TABLE opt_as Sconst
> + {
> + $$ = makeDefElem("table", (Node *) makeString($3), @1);
> + }
>
> since "table" is already a keyword, so there is no influence on the
> parsing speed?
>
> demo:
>
> create table err_tbl(
> userid oid, -- the user oid while copy generated this entry
> copy_tbl oid, --copy table
> filename text,
> lineno int8,
> line text,
> colname text,
> raw_field_value text,
> err_message text,
> err_detail text,
> errorcode text
> );
> create table t_copy_tbl(a int, b int, c int);
> COPY t_copy_tbl FROM STDIN WITH (delimiter ',', on_error 'table',
> table err_tbl);
> 1,2,a
> \.
>
> table err_tbl \gx
> -[ RECORD 1 ]---+-------------------------------------------
> userid | 10
> copy_tbl | 17920
> filename | STDIN
> lineno | 1
> line | 1,2,a
> colname | c
> raw_field_value | a
> err_message | invalid input syntax for type integer: "a"
> err_detail |
> errorcode | 22P02
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Антуан Виолин 2024-07-15 05:43:08 Re: Extension for PostgreSQL cast jsonb to hstore WIP
Previous Message Peter Smith 2024-07-15 05:39:01 Re: Pgoutput not capturing the generated columns