From 9e1c7ebf72838fab367b4c192b3ebd479396f100 Mon Sep 17 00:00:00 2001 From: Brian Davis Date: Fri, 31 May 2024 15:43:20 -0500 Subject: [PATCH] Allow synchronous snapshot on replicas and in RDS/Aurora Synchronized snapshots are supported on replicas since PostgreSQL 10. They're also supported in RDS/Aurora. Postgres 10 and 11 are out of support, so it should be reasonable to simply allow without checking for version number. --- src/postgres_scanner.cpp | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/postgres_scanner.cpp b/src/postgres_scanner.cpp index 75c029fc..1f03d8dd 100644 --- a/src/postgres_scanner.cpp +++ b/src/postgres_scanner.cpp @@ -62,40 +62,21 @@ struct PostgresGlobalState : public GlobalTableFunctionState { PostgresConnection connection; }; -static void PostgresGetSnapshot(PostgresVersion version, const PostgresBindData &bind_data, - PostgresGlobalState &gstate) { +static void PostgresGetSnapshot(const PostgresBindData &bind_data, PostgresGlobalState &gstate) { unique_ptr result; // by default disable snapshotting gstate.snapshot = string(); if (gstate.max_threads <= 1) { return; } - if (version.type_v == PostgresInstanceType::AURORA) { - return; - } + // reader threads can use the same snapshot auto &con = gstate.GetConnection(); - // pg_stat_wal_receiver was introduced in PostgreSQL 9.6 - if (version < PostgresVersion(9, 6, 0)) { - result = con.TryQuery("SELECT pg_is_in_recovery(), pg_export_snapshot()"); - if (result) { - auto in_recovery = result->GetBool(0, 0); - if (!in_recovery) { - gstate.snapshot = result->GetString(0, 1); - } - } - return; - } result = - con.TryQuery("SELECT pg_is_in_recovery(), pg_export_snapshot(), (select count(*) from pg_stat_wal_receiver)"); + con.TryQuery("SELECT pg_export_snapshot()"); if (result) { - auto in_recovery = result->GetBool(0, 0) || result->GetInt64(0, 2) > 0; - gstate.snapshot = ""; - if (!in_recovery) { - gstate.snapshot = result->GetString(0, 1); - } - return; + gstate.snapshot = result->GetString(0, 0); } } @@ -306,7 +287,7 @@ static unique_ptr PostgresInitGlobalState(ClientContex result->collection->InitializeScan(result->scan_state); } else { // we create a transaction here, and get the snapshot id to enable transaction-safe parallelism - PostgresGetSnapshot(bind_data.version, bind_data, *result); + PostgresGetSnapshot(bind_data, *result); } return std::move(result); }