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.94.2) (envelope-from ) id 1sECDL-000A5f-Sq for pgsql-general@arkaria.postgresql.org; Mon, 03 Jun 2024 18:15:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sECDL-00C1Kb-QQ for pgsql-general@arkaria.postgresql.org; Mon, 03 Jun 2024 18:15:23 +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.94.2) (envelope-from ) id 1sECDL-00C1KT-AC for pgsql-general@lists.postgresql.org; Mon, 03 Jun 2024 18:15:23 +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.94.2) (envelope-from ) id 1sECDI-003HpM-K0 for pgsql-general@postgresql.org; Mon, 03 Jun 2024 18:15:22 +0000 Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a6267778b3aso423384766b.3 for ; Mon, 03 Jun 2024 11:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717438519; x=1718043319; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YqWtkUE3hXUR00Gw0vO717g6TN/UvEr5bAIM+WEF4pM=; b=L7BuIU/KRfTWfbO1LCLCA+D1dyQ/2EZq1YkeP/CkBq0p05uXcg4gjcQpXUzXJqcqoA TTn0v0oSeh1fp2fLRa95zQlOXh2nDxt0ZvOCo7/HCF9GmTKS7vmZxQNl1mg88aJjgWwN KPodyd6oAyEfFt7HTNPHcNVicQK0fKYzSNi7XWrZ57G7f7n7UhEMYDGqTMqebF/q8B2q YZkgAIwxb2MEjMwXLlAsZBEwfqxYzJWrfNpRSwV6hwpco949Y0bPGYFVp87Krp8VFs4y dbs73oEmXnocJu0SUlLExFynP0+Pw72k9Bit24URu+rQY9DcaEINs6+ns73Xkvgd3rC2 Jrwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717438519; x=1718043319; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YqWtkUE3hXUR00Gw0vO717g6TN/UvEr5bAIM+WEF4pM=; b=jZgPZiJcWmbGN23vP3Yn8kBA8QUX68iJim0NYXy2HYCcfmuDBx/klVBi0p7CxR+Dt2 VYr6GXNkWUCISQBqvJMRTI9IZVCAjvXLjczveBeee+1nJA/Mp25WTXD2ASmWMMyGzWky JMXmvXy5Nj0BbeZlaTbMQf8GmW/MN0RtjuZkb5eNRWDTC9LmW6/nAwYzzQ5pnOByGN05 tRBvXiEXBGP5LeOhIXd2t9rjbhRrmihG1z2fNz2Abybucpun1cO1wyxJGKasnJZjWqc+ VQpD4bzynLzGEqIZmKeTCU60OJ1dgvjb1iJol7/jMLSqHuNaib/IcZD/A7VXOXWb5WcU 6sAg== X-Gm-Message-State: AOJu0YwEWBE4uEt5igt+NOUpzj+LOA0oVPP3qvS689sNKIBcb3S60nFc u4XygBAv9GYlGWz0w7Ag3h7927pYVnza9uPB6JA51QgEhL0vi+/n6pbFyCMj0NmJ74spXtK/upg lBI8013nCOZrzGO2j5BJZtowEuRo= X-Google-Smtp-Source: AGHT+IEqzmE49oBU0II86iHR/jKag6Ba3NfeqC1/JCyFkzsqobwrSwTIiGeihfgElVn7/6O2uPONL51RnZvb73FmKj4= X-Received: by 2002:a17:906:f585:b0:a68:f0ce:9f2e with SMTP id a640c23a62f3a-a68f0cea079mr354511866b.44.1717438518858; Mon, 03 Jun 2024 11:15:18 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Victor Yegorov Date: Mon, 3 Jun 2024 21:15:07 +0300 Message-ID: Subject: Re: Unexpected results from CALL and AUTOCOMMIT=off To: Pierre Forstmann Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000d71b56061a0050a0" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000d71b56061a0050a0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =D0=BF=D0=BD, 3 =D0=B8=D1=8E=D0=BD. 2024=E2=80=AF=D0=B3. =D0=B2 20:40, Pier= re Forstmann : > You declared function f_get_x as stable which means: > > =E2=80=A6 > > If you remove stable from function declaration, it works as expected: > Well, I checked https://www.postgresql.org/docs/current/xfunc-volatility.html There's a paragraph describing why STABLE (and IMMUTABLE) use different snapshots: > For functions written in SQL or in any of the standard procedural languages, there is a second important property determined by the volatility category, namely the visibility of any data changes that have been made by the SQL command that is calling the function. A > VOLATILE function will see such changes, a STABLE or IMMUTABLE function will not. This behavior is implemented using the snapshotting behavior of MVCC (see Chapter 13): STABLE and IMMUTABLE functions use a snapshot established as of the start of the > calling query, whereas VOLATILE functions obtain a fresh snapshot at the start of each query they execute. But later, docs state, that > Because of this snapshotting behavior, a function containing only SELECT commands can safely be marked STABLE, even if it selects from tables that might be undergoing modifications by concurrent queries. PostgreSQL will execute all commands of a STABLE function using the snapshot established for the calling query, and so it will see a fixed view of the database throughout that query. And therefore I assume STABLE should work in this case. Well, it seems not to. I assume there's smth to do with implicit BEGIN issued in non-AUTOCOMMIT mode and non-atomic DO block behaviour. --=20 Victor Yegorov --000000000000d71b56061a0050a0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
=D0=BF=D0=BD, 3 =D0=B8=D1=8E=D0=BD. 2024= =E2=80=AF=D0=B3. =D0=B2 20:40, Pierre Forstmann <pierre.forstmann@gmail.com>:
You declared function f_get_x as stable which means:
=

=E2=80=A6

If you remove stable= from function declaration, it works as expected:
<= div>
Well, I checked=C2=A0https://www.postgresql.org/docs/current/xfunc-v= olatility.html
There's a paragraph describing why STABLE (and IM= MUTABLE) use different snapshots:

> For functions written in SQL = or in any of the standard procedural languages, there is a second important= property determined by the volatility category, namely the visibility of a= ny data changes that have been made by the SQL command that is calling the = function. A > VOLATILE function will see such changes, a STABLE or IMMUT= ABLE function will not. This behavior is implemented using the snapshotting= behavior of MVCC (see Chapter 13): STABLE and IMMUTABLE functions use a sn= apshot established as of the start of the
> calling query, whereas VO= LATILE functions obtain a fresh snapshot at the start of each query they ex= ecute.

But later, docs state, that

> Because of this snaps= hotting behavior, a function containing only SELECT commands can safely be = marked STABLE, even if it selects from tables that might be undergoing modi= fications by concurrent queries. PostgreSQL will execute all commands of a = STABLE function using the snapshot established for the calling query, and s= o it will see a fixed view of the database throughout that query.

An= d therefore I assume STABLE should work in this case. Well, it seems not to= .

I assume there's smth to do with implicit BEGIN issued in non-= AUTOCOMMIT mode and non-atomic DO block behaviour.


--
Victor Yegor= ov
--000000000000d71b56061a0050a0--