cockroachdb / cockroachdb/cockroach

Error managing failed copy

Open
#119,113 2 comments 0 reactions 0 assignees View on GitHub
C-bug O-community P-3 T-sql-queries X-blathers-triaged
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

In more recent CRDB than 21.2 (after fixing #81559), `PQputCopyEnd()` works well enough, but the message seems somewhat malformed. The error raised after failed copy appears as:

```
COPY from stdin failed: error from Python: ZeroDivisionError - division by zero
message contents do not agree with length in message type "E"
```

**To Reproduce**

Using psycopg 3.1.x. With PostgreSQL:

```python
>>> import psycopg.crdb
>>> conn = psycopg.crdb.connect("dbname=psycopg3_test")
>>> cur = conn.cursor()
>>> cur.execute("create table whatever (id integer primary key)")
>>> with cur.copy("copy whatever from stdin") as copy:
... 1 / 0
ZeroDivisionError: division by zero
```

With CRDB 23.1:

```python
# docker run -p 26257:26257 --name crdb --rm cockroachdb/cockroach:latest-v23.1 start-single-node --insecure

>>> import psycopg.crdb
>>> conn = psycopg.crdb.connect("host=localhost port=26257 user=root dbname=defaultdb")
>>> cur = conn.cursor()
>>> cur.execute("create table whatever (id integer primary key)")
>>> with cur.copy("copy whatever from stdin") as copy:
... 1 / 0
psycopg.DatabaseError: COPY from stdin failed: error from Python: ZeroDivisionError - division by zero
message contents do not agree with length in message type "E"
```

Note: psycopg recognises the error thrown from Python on the other side of the libpq in [this code](https://github.com/psycopg/psycopg/blob/2695a506975767a16132d25fbe09cd778f3365b6/psycopg/psycopg/copy.py#L370-L387). In postgres, after a `PQputCopyEnd()`, the server will return a `query_canceled` error. I understand that, after fixing #81559, CRDB tries to do the same. However, because of an error in the message length of the error, the libpq shadows this error with a client-side error, which won't have a SQLSTATE. Therefore, the Python code will fail to re-raise the Python exception and will fall back with raising the database error instead.

**Environment:**
- CockroachDB version: 23.1
- Server OS: Linux
- Client app: psycopg
- Client Libpq: 16

Jira issue: CRDB-36036

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.