COPY fails when the table has a row trigger

Open
#176,172 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
go, postgresql, sql

Research direction

Start by locating CockroachDB's COPY implementation and the row-level trigger handling it invokes; the issue does not name specific files or tests. Reproduce the supplied SQL case, then trace why COPY reports fewer inserted rows without an error when the trigger fires. Done means all three target rows and three log rows persist, matching the equivalent multi-row INSERT.

Written by the indexing model from the issue text.

Description

C-bug O-community T-sql-foundations X-blathers-triaged X-move-internal

Describe the problem

Copying multiple rows into a table with a row-level trigger fails with an
internal error. The same inserts succeed when expressed as an ordinary
INSERT. The entire COPY is rolled back.

The reproduced error is:

ERROR: internal error: COPY didn't insert all buffered rows and yet no error was reported. Inserted 2 out of 3 rows.

To Reproduce

psql 'host=127.0.0.1 port=26257 user=root dbname=test sslmode=disable' <<'SQL'
DROP TABLE IF EXISTS copy_trigger_target;
DROP TABLE IF EXISTS copy_trigger_log;
DROP FUNCTION IF EXISTS copy_trigger_fn();

CREATE TABLE copy_trigger_target (a INT PRIMARY KEY, b INT);
CREATE TABLE copy_trigger_log (x TEXT);

CREATE FUNCTION copy_trigger_fn() RETURNS TRIGGER AS $$
BEGIN
    INSERT INTO copy_trigger_log VALUES ('i');
    RETURN NEW;
END
$$ LANGUAGE plpgsql;

CREATE TRIGGER copy_trigger
AFTER INSERT ON copy_trigger_target
FOR EACH ROW EXECUTE FUNCTION copy_trigger_fn();
SQL

printf '1,10\n2,20\n3,30\n' |
  psql 'host=127.0.0.1 port=26257 user=root dbname=test sslmode=disable' \
    -c 'COPY copy_trigger_target FROM STDIN WITH (FORMAT csv);'
# ERROR: internal error: COPY didn't insert all buffered rows and yet no error was
# reported. Inserted 2 out of 3 rows.

psql 'host=127.0.0.1 port=26257 user=root dbname=test sslmode=disable' \
  -c 'SELECT count(*) FROM copy_trigger_target;' \
  -c 'SELECT count(*) FROM copy_trigger_log;'
# count
# -------
#      0
# count
# -------
#      0

Expected behavior

The COPY should insert all three rows and fire the trigger once per inserted
row, matching the behavior of an equivalent multi-row INSERT. Both counts
shown above should be 3.

Environment:

  • CockroachDB version: v26.3.0-alpha.00000000-dev-13cb3eb27674b4a981e5c526d04c2387e81efd0b
  • Server OS: Linux amd64
  • Client app: psql 16.15

Jira issue: CRDB-70500

Dominant language
Go
Stars
32.6k
Forks
4.2k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cockroachdb/cockroach

All issues in cockroachdb/cockroach

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.