fix: no longer retries the transaction on 40001 errors

This commit is contained in:
Laurence Isla
2026-04-27 14:30:10 -05:00
parent bf758698b3
commit 41b86fffa5
8 changed files with 46 additions and 6 deletions
+4
View File
@@ -10,6 +10,10 @@ $$ language sql;
create table replica.items as select x as id from generate_series(1, 10) x;
create table replica.conflict as select x as id from generate_series(1, 1000000) x;
create view replica.conflict_view as select * from replica.conflict where (pg_sleep(0.01) is not null);
DROP ROLE IF EXISTS postgrest_test_anonymous;
CREATE ROLE postgrest_test_anonymous;
+34
View File
@@ -1,6 +1,10 @@
"IO tests for PostgREST started on replicas"
import os
import time
from postgrest import run
from util import Thread
def test_sanity_replica(replicaenv):
@@ -22,3 +26,33 @@ def test_sanity_replica(replicaenv):
response = postgrest.session.get("/items?select=count")
assert response.text == '[{"count":10}]'
def test_conflict_replica(replicaenv):
"Test that PostgREST does not retry the transaction on conflict with recovery (PG error code 40001)"
with run(env=replicaenv["replica"]) as postgrest:
def conflict():
response = postgrest.session.get("/conflict_view")
# Checks that the transaction stops and returns the 40001 error instead of retrying
assert response.json()["code"] == "40001"
assert response.status_code == 500
t = Thread(target=conflict)
t.start()
# make sure the request has started
time.sleep(0.1)
prienv = replicaenv["primary"]
connopts = f'-d {prienv["PGDATABASE"]} -U postgres -h {prienv["PGHOST"]}'
# Delete the table data while the request with the lock is running to trigger the recovery conflict
os.system(
f'psql {connopts} --set ON_ERROR_STOP=1 -a -c "DELETE FROM replica.conflict;"'
)
# Vacuum the table to accelerate the process
os.system(f"vacuumdb {connopts} -t replica.conflict")
t.join()