Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres.CopyToTable: Robustize resources cleanup #3072

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

LinqLover
Copy link

@LinqLover LinqLover commented Apr 15, 2021

This PR aims to make connections using the contrib.postgres module more robust. If an error occurs during copying the rows, psycopg2 objects previously could be left opened without a proper clean-up, causing further connection attempts to the database to fail as well (psycopg2.errors.ObjectInUse).

By ensuring proper use of try...catch blocks and context managers, issues of this kind should be fixed now. Also, this PR includes regression tests.

For more information about releasing psycopg2 objects, visit: https://www.psycopg.org/docs/usage.html#with-statement

LBNL, a more precise exception class is used in the default copy() implementation of CopyToTable (f66534c).

Kind of replaces #2934, which I unfortunately never had managed to complete.

Preferably please squash when merging.

Instead of a generic exception, raise a specific ValueError. The former
is considered an anti-pattern because generic exceptions are harder to
handle.
Make sure to rollback and close connections as well as to release
cursors when an error occurs during
CopyToTable.copy(). This can happen, for example, when rows have an
invalid format or the method is overriden in a subclass.

Tests are added, too (see DailyCopyToTableTest.test_cleanup_on_error()).
@LinqLover LinqLover requested review from dlstadther, Tarrasch and a team as code owners April 15, 2021 10:26
luigi/contrib/postgres.py Outdated Show resolved Hide resolved
luigi/contrib/postgres.py Outdated Show resolved Hide resolved
@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If closed, you may revisit when your time allows and reopen! Thank you for your contributions.

@stale stale bot added the wontfix label Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants