Skip to content

Commit 129b18a

Browse files
author
Nicolas ESTRADA
committed
fix: rolling back on managing conn lifecycle using context mgrs: it doesn't work as per https://www.psycopg.org/docs/usage.html#with-statement
1 parent 41f8ded commit 129b18a

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

sources/pg_legacy_replication/helpers.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def get_max_lsn(credentials: ConnectionStringCredentials) -> Optional[int]:
160160
Returns None if the replication slot is empty.
161161
Does not consume the slot, i.e. messages are not flushed.
162162
"""
163-
with _get_conn(credentials) as conn:
164-
cur = conn.cursor()
163+
cur = _get_conn(credentials).cursor()
164+
try:
165165
loc_fn = (
166166
"pg_current_xlog_location"
167167
if get_pg_version(cur) < 100000
@@ -171,6 +171,8 @@ def get_max_lsn(credentials: ConnectionStringCredentials) -> Optional[int]:
171171
cur.execute(f"SELECT {loc_fn}() - '0/0' as max_lsn;")
172172
lsn: int = cur.fetchone()[0]
173173
return lsn
174+
finally:
175+
cur.connection.close()
174176

175177

176178
def lsn_int_to_hex(lsn: int) -> str:
@@ -192,13 +194,15 @@ def advance_slot(
192194
the behavior of that method seems odd when used outside of `consume_stream`.
193195
"""
194196
assert upto_lsn > 0
195-
with _get_conn(credentials) as conn:
196-
cur = conn.cursor()
197+
cur = _get_conn(credentials).cursor()
198+
try:
197199
# There is unfortunately no way in pg9.6 to manually advance the replication slot
198200
if get_pg_version(cur) > 100000:
199201
cur.execute(
200202
f"SELECT * FROM pg_replication_slot_advance('{slot_name}', '{lsn_int_to_hex(upto_lsn)}');"
201203
)
204+
finally:
205+
cur.connection.close()
202206

203207

204208
def _get_conn(
@@ -382,19 +386,20 @@ def __iter__(self) -> Iterator[TableItems]:
382386
Maintains LSN of last consumed commit message in object state.
383387
Advances the slot only when all messages have been consumed.
384388
"""
385-
with get_rep_conn(self.credentials) as conn:
386-
cur = conn.cursor()
389+
cur = get_rep_conn(self.credentials).cursor()
390+
consumer = MessageConsumer(
391+
upto_lsn=self.upto_lsn,
392+
table_qnames=self.table_qnames,
393+
repl_options=self.repl_options,
394+
target_batch_size=self.target_batch_size,
395+
)
396+
try:
387397
cur.start_replication(slot_name=self.slot_name, start_lsn=self.start_lsn)
388-
consumer = MessageConsumer(
389-
upto_lsn=self.upto_lsn,
390-
table_qnames=self.table_qnames,
391-
repl_options=self.repl_options,
392-
target_batch_size=self.target_batch_size,
393-
)
394-
try:
395-
cur.consume_stream(consumer)
396-
except StopReplication: # completed batch or reached `upto_lsn`
397-
yield from self.flush_batch(cur, consumer)
398+
cur.consume_stream(consumer)
399+
except StopReplication: # completed batch or reached `upto_lsn`
400+
yield from self.flush_batch(cur, consumer)
401+
finally:
402+
cur.connection.close()
398403

399404
def flush_batch(
400405
self, cur: ReplicationCursor, consumer: MessageConsumer

0 commit comments

Comments
 (0)