Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x4dMY-007I27-0U for pgsql-general@arkaria.postgresql.org; Thu, 10 Sep 2026 11:54:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x4dMX-005FYl-0W for pgsql-general@arkaria.postgresql.org; Thu, 10 Sep 2026 11:54:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x4dMW-005FYb-2U for pgsql-general@lists.postgresql.org; Thu, 10 Sep 2026 11:54:40 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x4dMU-00000004wtt-3ZJh for pgsql-general@lists.postgresql.org; Thu, 10 Sep 2026 11:54:39 +0000 Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-c262204cc80so843063566b.3 for ; Thu, 10 Sep 2026 04:54:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1789041277; x=1789646077; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:to:from:subject:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=FZB6ykaraWSo8ROVzpeWIYvSdtvYS22dIVgWNEuBZUk=; b=YpkzulCbeb/Gb24u4uvnEfwmdxBaqOx+n2rT1Led0e3dom2Dsr+ID+H9SKEZJm9U3K fJmO3jDRPZI9iegOMH1Tn1xNuQ98zmGj0Z3HNrC/sx0H3eEccWqyZy2NJdRgenUCTvBK mCmCev5zvHAIvGQQonPbC6FiPdIl8izDhrJ+OorJUHQda/l4IwCChNzje+lSV7hnsAzZ HSqA2j1smz96d642nRKORePSrOUQz2GwhJlyLLtHqChoVasgLZByij6sXtuHRj9RsVGS N9tVpVTSR5T0IpZdKi+kLidURcnXY+CtOii9Q9Tb21c+34rTyGexPgCa+hJCiGiUWWOH ZEZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789041277; x=1789646077; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=FZB6ykaraWSo8ROVzpeWIYvSdtvYS22dIVgWNEuBZUk=; b=Hxc68bRlHbX8XanXHI6xaU9+Ar3xOOZ6l+OyzFVjt2MWzwHeSv841+e6G707qbO1qn vY1KfLZYWyZ7uOWne0qhC28dzYIgQ2t75Vtq/3VhXxhmDEoXfrlWCLUfvqdUCIlABAu5 FDuLlm4KixVESULoiTVTnC8P6Vjl3jw1npgW7fW0+eI25eivgLlGIprAUGz6n8+XL9u5 7BWfj6y+W7FY+u33DuPWl/7RK4ZJgE3a6w6gYk/E/K93rWf3ItK7CIzvf2t9g34qMmfq Y6hjsO5Wp/P58nCd1J6kQnsGrcyvm8Xlsu6trhZlT3mD2ptz2DB0JwLGPGx2HEyvSEmA fGug== X-Forwarded-Encrypted: i=1; AKwUvBzAi3hISgwGfBG/IR1bOCGkCfoEpM7qGG5pSQp2VAqEEzCxiVIBvZx1vrDom6XJIcUm8AeLV7uLVgcZNJyV@lists.postgresql.org X-Gm-Message-State: AFuF++mUgTsmPa119ouZsPQgzdFI3Qfhu2Mf6kluaSqNfKFwoa5hI8XA Xfe04My4BwnqjvAaoZZWe+TRfyhO0t2If4gpT9lhxsmtlN2Zo/TLdtaSRfPKgJdmbP9BCD0RMZg hU/I8sYI= X-Gm-Gg: AYBFou3rGrIVtFp7qBw5EyO6loGFV97B7I9oXnZcNYHaUfvioaj0FJV5JJgc1W7wwNT t7+LhvomHb4hVoC4NBg5cwC3xsDXtUrpZyQcp+g4gXEU4IQ1Z65ivlyzZQCObkSJ7OE8ToG43w4 04Sb1c4LWqy+d4EPhIy5pbPszy97WV75DDzdn5IJUgdKEBt0em29MVqsM6AYEw9+BuYDRdudTzm 0F2GDdXjJ1Tt2K5fD4OSPEbKomwUNhvjGqS1hs9cscBKm1lKtb7JJYTmOeDYGS6ctfWUs6XFlln REoYrMiPfx/qyFrI6oKYmpg8LDbCmIF6MnpFodIMm6OJl+qaDtdBLKF25dRQTt6+TwUDEP5X2Mz 9ozQUkbDXpwuaywS8yq81mbs5mEgThE8y2ifjwM4Fg7PvBz/EM7Jrm5PM4v7JkjkDW6Yv+aGGlV nzqRGB/LFGjFkuyGmix0OM9DNaXByY4FGwz2XedDS29S82q7sD5FEzO5gvePnwqhgIuaCyppNX2 pYL4lFVoGPF4oWVrml7qEjLobI= X-Received: by 2002:a17:907:ca85:b0:c29:3c97:c994 with SMTP id a640c23a62f3a-c293c97d339mr347646366b.35.1789041277205; Thu, 10 Sep 2026 04:54:37 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:70:af3d:69e0:1842:3f90:2f57]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c260d4a9cbbsm911977866b.13.2026.09.10.04.54.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2026 04:54:36 -0700 (PDT) Message-ID: <3af6e9e8bb4018eade44c4ebf7290fa8fdad40d7.camel@cybertec.at> Subject: Re: What is the cost of a tx? From: Laurenz Albe To: Ron Johnson , "pgsql-generallists.postgresql.org" Date: Thu, 10 Sep 2026 13:54:36 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.60.2 (3.60.2-1.fc44) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2026-09-09 at 17:55 -0400, Ron Johnson wrote: > On Wed, Sep 9, 2026 at 4:35=E2=80=AFPM Mantas Gridinas wrote: > > In the current system that i am working on the feature in question requ= ires between > > 1 and 4 requests (individual tx) to a read replica for extra data (assu= me orm usage > > where entity b must be fetched if entity a doesn=E2=80=99t exist ant so= on). At scale this > > ends up being between 250k (best case) to 1m (worst case) requests per = minute. The > > individual tx setup uses "read committed=E2=80=9D tx mode. I am conside= ring to move those 4 > > requests into single transaction to reduce the amount of transactions i= n general, > > but without behavior change there would still be up to 4 individual que= ries during > > that transaction, so database doesn=E2=80=99t really have full informat= ion about what I > > want from it. Is the logic sound to perform the change or (at my scale)= the overhead > > is negligible where it doesn=E2=80=99t really matter that it=E2=80=99s = 1 larger transaction that takes > > 4 time units compared to 4 smaller transactions that take 1 time unit? > >=20 > > I suspect that using repeatable read would push the change into single = tx direction > > since I wouldn=E2=80=99t be snapshotting the database per query. >=20 > The ultimate "single transaction" would be putting all the logic in a sin= gle (anonymous > or named) DO procedure.=C2=A0 I don't remember if that alone would make i= t effectively > Repeatable Read (since the DB engine would see it as one single, very lar= ge statement) > without explicitly setting the transaction mode, but it sure would cut do= wn on BIND > overhead, etc (though at the cost of extra load on the DB server, since t= he application > logic would happen there instead of in the app server.) That's not entirely accurate; still the idea to use a DO statement or a fun= ction is often a good one. But let me answer the question first. You mentioned that you want to run these transactions on a read replica. Y= ou cannot modify any data there, so PostgreSQL won't assign a transaction ID, and it = won't have to go through a commit, flush WAL etc. So that would be cheap, and it does= n't matter much if you run three statements in one or in three (read-only) transaction= s. Taking a snapshot per query is an overhead, granted. To reduce that overhead, you would have to run the three queries in a singl= e REPEATABLE READ transaction. But the overhead of taking a snapshot is not = huge, unless you have gazillions of database connections. Running the three queries in a single DO statement would not change anythin= g, because with the default READ COMMITTED isolation level every query would s= till take its own snapshot. You'd have to start an explicit REPEATABLE READ tra= nsaction first. The big advantage of using a DO statement or a function is that you safe yo= urself a number of client-server round trips. Calling a function (or executing a = DO statement) is a single client-server round trip. That can be a big advantage, particu= larly on connections with a high latency. The big disadvantage of a DO statement would be that it cannot return any r= esults. You'd have to create a function for that. So, in short: - running several queries in a single REPEATABLE READ trasaction gives you = a small performance benefit (perhaps less than you hope) - running several queries in a function saves you client-server round trips= , which can be a big improvement Yours, Laurenz Albe