This will allow us to replace the SQL commands with protocol messages.
yield from self._exec_command(b"ROLLBACK")
self._prepared.clear()
- for cmd in self._prepared.get_maintenance_commands():
- yield from self._exec_command(cmd)
+ yield from self._prepared.maintain_gen(self)
if self._pipeline:
yield from self._pipeline._sync_gen()
yield from self._conn._pipeline._communicate_gen()
self._last_query = query
-
- for cmd in self._conn._prepared.get_maintenance_commands():
- yield from self._conn._exec_command(cmd)
+ yield from self._conn._prepared.maintain_gen(self._conn)
def _executemany_gen_pipeline(
self, query: Query, params_seq: Iterable[Params], returning: bool
if returning:
yield from pipeline._fetch_gen(flush=True)
- for cmd in self._conn._prepared.get_maintenance_commands():
- yield from self._conn._exec_command(cmd)
+ yield from self._conn._prepared.maintain_gen(self._conn)
def _executemany_gen_no_pipeline(
self, query: Query, params_seq: Iterable[Params], returning: bool
yield from self._maybe_prepare_gen(pgq, prepare=True)
self._last_query = query
-
- for cmd in self._conn._prepared.get_maintenance_commands():
- yield from self._conn._exec_command(cmd)
+ yield from self._conn._prepared.maintain_gen(self._conn)
def _maybe_prepare_gen(
self,
# Copyright (C) 2020 The Psycopg Team
from enum import IntEnum, auto
-from typing import Iterator, Optional, Sequence, Tuple, TYPE_CHECKING
+from typing import Optional, Sequence, Tuple, TYPE_CHECKING
from collections import OrderedDict
from . import pq
+from .abc import PQGen
from ._compat import Deque, TypeAlias
from ._queries import PostgresQuery
if TYPE_CHECKING:
+ from typing import Any
from .pq.abc import PGresult
+ from ._connection_base import BaseConnection
Key: TypeAlias = Tuple[bytes, Tuple[int, ...]]
else:
return False
- def get_maintenance_commands(self) -> Iterator[bytes]:
+ def maintain_gen(self, conn: "BaseConnection[Any]") -> PQGen[None]:
"""
- Iterate over the commands needed to align the server state to our state
+ Generator to send the commands to perform periodic maintenance
+
+ Deallocate unneeded command in the server, or flush the prepared
+ statements server state entirely if necessary.
"""
while self._maint_commands:
- yield self._maint_commands.popleft()
+ cmd = self._maint_commands.popleft()
+ yield from conn._exec_command(cmd)
for command in self._get_rollback_commands():
yield from self._conn._exec_command(command)
+ # Also clear the prepared statements cache.
+ self._conn._prepared.clear()
+ yield from self._conn._prepared.maintain_gen(self._conn)
+
if isinstance(exc_val, Rollback):
if not exc_val.transaction or exc_val.transaction is self:
return True # Swallow the exception
assert not self._conn._num_transactions
yield b"ROLLBACK"
- # Also clear the prepared statements cache.
- if self._conn._prepared.clear():
- yield from self._conn._prepared.get_maintenance_commands()
-
def _push_savepoint(self) -> None:
"""
Push the transaction on the connection transactions stack.