Skip to content

Commit ce1c97e

Browse files
committed
Change checkpoint_data and restore_connection_on_error to subtransaction
checkpoint_data and restore_connection_on_error used to commit() and rollback() the db connection. This causes additional I/O and load. Changed them to use 'SAVEPOINT name' and 'ROLLBACK TO name' to get a faster method for handling errors within a tranaction. One thing to note is that postgresql (unlike SQL std) doesn't overwrite an older savepoint with he same name. It keeps all savepoints but only rolls back to the newest one with a given name. This could be a resource issue. I left a commented out release statement in case somebody runs into an issue due to too many savepoints. I expect it to slow down the import but....
1 parent 7743ca7 commit ce1c97e

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

roundup/backends/back_postgresql.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,33 @@ def open_connection(self):
203203
self._add_fts_table()
204204
self.commit()
205205

206-
def checkpoint_data(self):
207-
"""Commit the state of the database. Allows recovery/retry
208-
of operation in exception handler because postgres
209-
requires a rollback in case of error generating exception
206+
def checkpoint_data(self, savepoint="importing"):
207+
"""Create a subtransaction savepoint. Allows recovery/retry
208+
of operation in exception handler because
209+
postgres requires a rollback in case of error
210+
generating exception. Used with
211+
restore_connecion_on_error to handle uniqueness
212+
conflict in import_table().
210213
"""
211-
self.commit()
212-
213-
def restore_connection_on_error(self):
214-
"""Postgres leaves a cursor in an unusable state after
215-
an error. Rollback the transaction to recover and
216-
permit a retry of the failed statement. Used with
217-
checkpoint_data to handle uniqueness conflict in
218-
import_table()
214+
# Savepoints take resources. Postgres keeps all
215+
# savepoints (rather than overwriting) until a
216+
# commit(). If an import fails because of a resource
217+
# issue with savepoints, uncomment this line. I
218+
# expect it will slow down the import but it should
219+
# eliminate any issue with stored savepoints and
220+
# resource use.
221+
#
222+
# self.sql('RELEASE SAVEPOINT %s' % savepoint)
223+
self.sql('SAVEPOINT %s' % savepoint)
224+
225+
def restore_connection_on_error(self, savepoint="importing"):
226+
"""Postgres leaves a connection/cursor in an unusable state
227+
after an error. Rollback the transaction to a
228+
previous savepoint and permit a retry of the
229+
failed statement. Used with checkpoint_data to
230+
handle uniqueness conflict in import_table().
219231
"""
220-
self.rollback()
232+
self.sql('ROLLBACK TO %s' % savepoint)
221233

222234
def create_version_2_tables(self):
223235
# OTK store

0 commit comments

Comments
 (0)