Skip to content

Fix COPY OUT losing data when a notice arrives between CopyData messages - #1407

Open
ritojoao wants to merge 1 commit into
MagicStack:masterfrom
ritojoao:fix-copy-notice-data-loss
Open

ritojoao wants to merge 1 commit into
MagicStack:masterfrom
ritojoao:fix-copy-notice-data-loss

Conversation

@ritojoao

Copy link
Copy Markdown

Fixes #1264

Hi! Thanks for maintaining asyncpg.

copy_from_query() / copy_table() can silently lose output when the server sends a NoticeResponse between CopyData messages (for example a function that does RAISE NOTICE, or PostGIS coercion notices like in the issue).

Cause: in _parse_copy_data_msgs (coreproto.pyx) the consumed CopyData bytes are assigned to self.result. If a non-CopyData message is next in the buffer, the result is intentionally not pushed yet. After the notice is handled, the next CopyData batch overwrites self.result, so the earlier bytes (including the CSV header) are dropped.

Fix: append to self.result when it already holds unpushed data.

Test: test_copy_from_query_with_notices (a plpgsql function raising a notice per row, csv with header). Without the fix only 3,4 is returned; with it the header and all rows are returned. tests/test_copy.py and tests/test_listeners.py pass on PostgreSQL 18 (37 passed, 2 skipped).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

copy_from_query drops headers if any postgis NOTICE/LOCATION messages produced

1 participant