Skip to content

Commit ff53fa7

Browse files
committed
Added test for synchronous_commit option
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 11ad66e commit ff53fa7

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

python/psqlpy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ReadVariant,
1212
SingleQueryResult,
1313
SslMode,
14+
SynchronousCommit,
1415
TargetSessionAttrs,
1516
Transaction,
1617
connect,
@@ -32,4 +33,5 @@
3233
"SslMode",
3334
"KeepaliveConfig",
3435
"ConnectionPoolBuilder",
36+
"SynchronousCommit",
3537
]

python/psqlpy/_internal/__init__.pyi

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,38 @@ class SingleQueryResult:
152152
Type that return passed function.
153153
"""
154154

155+
class SynchronousCommit(Enum):
156+
"""
157+
Class for synchronous_commit option for transactions.
158+
159+
### Variants:
160+
- `On`: The meaning may change based on whether you have
161+
a synchronous standby or not.
162+
If there is a synchronous standby,
163+
setting the value to on will result in waiting till “remote flush”.
164+
- `Off`: As the name indicates, the commit acknowledgment can come before
165+
flushing the records to disk.
166+
This is generally called as an asynchronous commit.
167+
If the PostgreSQL instance crashes,
168+
the last few asynchronous commits might be lost.
169+
- `Local`: WAL records are written and flushed to local disks.
170+
In this case, the commit will be acknowledged after the
171+
local WAL Write and WAL flush completes.
172+
- `RemoteWrite`: WAL records are successfully handed over to
173+
remote instances which acknowledged back
174+
about the write (not flush).
175+
- `RemoteApply`: This will result in commits waiting until replies from the
176+
current synchronous standby(s) indicate they have received
177+
the commit record of the transaction and applied it so
178+
that it has become visible to queries on the standby(s).
179+
"""
180+
181+
On = 1
182+
Off = 2
183+
Local = 3
184+
RemoteWrite = 4
185+
RemoteApply = 5
186+
155187
class IsolationLevel(Enum):
156188
"""Class for Isolation Level for transactions."""
157189

@@ -1050,13 +1082,15 @@ class Connection:
10501082
isolation_level: IsolationLevel | None = None,
10511083
read_variant: ReadVariant | None = None,
10521084
deferrable: bool | None = None,
1085+
synchronous_commit: SynchronousCommit | None = None,
10531086
) -> Transaction:
10541087
"""Create new transaction.
10551088
10561089
### Parameters:
10571090
- `isolation_level`: configure isolation level of the transaction.
10581091
- `read_variant`: configure read variant of the transaction.
10591092
- `deferrable`: configure deferrable of the transaction.
1093+
- `synchronous_commit`: configure synchronous_commit option for transaction.
10601094
"""
10611095
def cursor(
10621096
self: Self,

python/tests/test_transaction.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
from pyarrow import parquet
1010
from tests.helpers import count_rows_in_test_table
1111

12-
from psqlpy import ConnectionPool, Cursor, IsolationLevel, ReadVariant
12+
from psqlpy import (
13+
ConnectionPool,
14+
Cursor,
15+
IsolationLevel,
16+
ReadVariant,
17+
SynchronousCommit,
18+
)
1319
from psqlpy.exceptions import (
1420
RustPSQLDriverPyBaseError,
1521
TransactionBeginError,
@@ -401,3 +407,27 @@ async def test_execute_batch_method(psql_pool: ConnectionPool) -> None:
401407
await transaction.execute_batch(querystring=query)
402408
await transaction.execute(querystring="SELECT * FROM execute_batch")
403409
await transaction.execute(querystring="SELECT * FROM execute_batch2")
410+
411+
412+
@pytest.mark.parametrize(
413+
"synchronous_commit",
414+
(
415+
SynchronousCommit.On,
416+
SynchronousCommit.Off,
417+
SynchronousCommit.Local,
418+
SynchronousCommit.RemoteWrite,
419+
SynchronousCommit.RemoteApply,
420+
),
421+
)
422+
async def test_synchronous_commit(
423+
synchronous_commit: SynchronousCommit,
424+
psql_pool: ConnectionPool,
425+
table_name: str,
426+
number_database_records: int,
427+
) -> None:
428+
async with psql_pool.acquire() as conn, conn.transaction(synchronous_commit=synchronous_commit) as trans:
429+
res = await trans.execute(
430+
f"SELECT * FROM {table_name}",
431+
)
432+
433+
assert len(res.result()) == number_database_records

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn psqlpy(py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyResult<()> {
2626
pymod.add_class::<driver::transaction::Transaction>()?;
2727
pymod.add_class::<driver::cursor::Cursor>()?;
2828
pymod.add_class::<driver::transaction_options::IsolationLevel>()?;
29+
pymod.add_class::<driver::transaction_options::SynchronousCommit>()?;
2930
pymod.add_class::<driver::transaction_options::ReadVariant>()?;
3031
pymod.add_class::<driver::common_options::ConnRecyclingMethod>()?;
3132
pymod.add_class::<driver::common_options::LoadBalanceHosts>()?;

0 commit comments

Comments
 (0)