why does txid_current() assign new transaction-id?

Started by Naoya Anzaiover 10 years ago8 messages
#1Naoya Anzai
nao-anzai@xc.jp.nec.com
1 attachment(s)

Hi,hackers!

I have a question about txid_current().
it is "Why does txid_current() assign new transaction-id?".

When we executes txid_current() outside of transaction block, it assigns new transaction-id.
I guess it doesn't need to assign a new txid because txid_current() is just a read-only function.

I found a replaceable function by walking through pg-code, that is GetStableLatestTransactionId(void).

I attached a patch which changing just 1-line.
Could you please check the code?

Regards,

Naoya

---
Naoya Anzai
Engineering Department
NEC Solution Inovetors, Ltd.
E-Mail: nao-anzai@xc.jp.nec.com
---

Attachments:

txid_current.patchapplication/octet-stream; name=txid_current.patchDownload
diff --git a/src/backend/utils/adt/txid.c b/src/backend/utils/adt/txid.c
index ce1d9ab..6206f7a 100644
--- a/src/backend/utils/adt/txid.c
+++ b/src/backend/utils/adt/txid.c
@@ -368,7 +368,7 @@ txid_current(PG_FUNCTION_ARGS)
 
 	load_xid_epoch(&state);
 
-	val = convert_xid(GetTopTransactionId(), &state);
+	val = convert_xid(GetStableLatestTransactionId(), &state);
 
 	PG_RETURN_INT64(val);
 }
#2Michael Paquier
michael.paquier@gmail.com
In reply to: Naoya Anzai (#1)
Re: why does txid_current() assign new transaction-id?

On Tue, May 26, 2015 at 4:12 PM, Naoya Anzai <nao-anzai@xc.jp.nec.com> wrote:

I have a question about txid_current().
it is "Why does txid_current() assign new transaction-id?".

When we executes txid_current() outside of transaction block, it assigns new transaction-id.
I guess it doesn't need to assign a new txid because txid_current() is just a read-only function.

I found a replaceable function by walking through pg-code, that is GetStableLatestTransactionId(void).

I attached a patch which changing just 1-line.
Could you please check the code?

txid_current has had the behavior of assigning a new transaction XID
when one is not assigned since its introduction. I don't think that it
is wise to change it now the way you do as many applications surely
rely on this assumption. Perhaps we could make the documentation
clearer about those things though, changing the description of this
function to "get current transaction ID, and assign a new one if one
is not assigned yet":
http://www.postgresql.org/docs/devel/static/functions-info.html

Regards,
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Naoya Anzai
nao-anzai@xc.jp.nec.com
In reply to: Michael Paquier (#2)
Re: why does txid_current() assign new transaction-id?

Thank you for comments.

I understand your points.

For only to read a current transaction-id, I know we just have to use
txid_current_snapshot and that is a best way, but I feel a little bit
hassle to explain columns of txid_current_snapshot for my supporting customers..
(Xmin is .... and Xmax is ....) ..

txid_current has had the behavior of assigning a new transaction XID
when one is not assigned since its introduction. I don't think that it
is wise to change it now the way you do as many applications surely
rely on this assumption. Perhaps we could make the documentation
clearer about those things though, changing the description of this
function to "get current transaction ID, and assign a new one if one
is not assigned yet":
http://www.postgresql.org/docs/devel/static/functions-info.html

+1

I think that a description of txid_current is too rough and it might
be confused some users.
txid_current is a different operation depending on session situations,
I feel if detail of txid_current is documented then it will be better.

For example...
Inside of the transaction-block(begin..end), returns a transaction-id used by this block.
Outside of the transaction-block, returns a next transaction-id(but it is consumed by this function).

Regards,

Naoya

---
Naoya Anzai
Engineering Department
NEC Solution Inovetors, Ltd.
E-Mail: nao-anzai@xc.jp.nec.com
---

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Michael Paquier
michael.paquier@gmail.com
In reply to: Naoya Anzai (#3)
1 attachment(s)
Re: why does txid_current() assign new transaction-id?

On Wed, May 27, 2015 at 5:13 PM, Naoya Anzai <nao-anzai@xc.jp.nec.com> wrote:

Perhaps we could make the documentation
clearer about those things though, changing the description of this
function to "get current transaction ID, and assign a new one if one
is not assigned yet":
http://www.postgresql.org/docs/devel/static/functions-info.html

+1

I think that a description of txid_current is too rough and it might
be confused some users.
txid_current is a different operation depending on session situations,
I feel if detail of txid_current is documented then it will be better.

For example...
Inside of the transaction-block(begin..end), returns a transaction-id used by this block.
Outside of the transaction-block, returns a next transaction-id(but it is consumed by this function).

Attached is a doc patch among those lines.
--
Michael

Attachments:

20150528_txid_current_docs.patchtext/x-patch; charset=US-ASCII; name=20150528_txid_current_docs.patchDownload
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 89a609f..6485b42 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -16233,7 +16233,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
       <row>
        <entry><literal><function>txid_current()</function></literal></entry>
        <entry><type>bigint</type></entry>
-       <entry>get current transaction ID</entry>
+       <entry>get current transaction ID, assigning a new one if current transaction does not have one</entry>
       </row>
       <row>
        <entry><literal><function>txid_current_snapshot()</function></literal></entry>
#5Christoph Berg
myon@debian.org
In reply to: Michael Paquier (#4)
Re: why does txid_current() assign new transaction-id?

Re: Michael Paquier 2015-05-28 <CAB7nPqSOx2TEgsypk8gbfa=_hJn7WNLf_tRZexY+SnN3fVF_nA@mail.gmail.com>

Attached is a doc patch among those lines.

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 89a609f..6485b42 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -16233,7 +16233,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
<row>
<entry><literal><function>txid_current()</function></literal></entry>
<entry><type>bigint</type></entry>
-       <entry>get current transaction ID</entry>
+       <entry>get current transaction ID, assigning a new one if current transaction does not have one</entry>

^ the

Christoph
--
cb@df7cb.de | http://www.df7cb.de/

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#6Naoya Anzai
nao-anzai@xc.jp.nec.com
In reply to: Christoph Berg (#5)
Re: why does txid_current() assign new transaction-id?
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 89a609f..6485b42 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -16233,7 +16233,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
<row>
<entry><literal><function>txid_current()</function></literal></entry>
<entry><type>bigint</type></entry>
-       <entry>get current transaction ID</entry>
+       <entry>get current transaction ID, assigning a new one if current transaction does not have one</entry>

^ the

That looks good.

Regards,

Naoya

---
Naoya Anzai
Engineering Department
NEC Solution Inovetors, Ltd.
E-Mail: nao-anzai@xc.jp.nec.com
---

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#7Fujii Masao
masao.fujii@gmail.com
In reply to: Naoya Anzai (#6)
Re: why does txid_current() assign new transaction-id?

On Wed, Jun 3, 2015 at 9:04 AM, Naoya Anzai <nao-anzai@xc.jp.nec.com> wrote:

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 89a609f..6485b42 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -16233,7 +16233,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
<row>
<entry><literal><function>txid_current()</function></literal></entry>
<entry><type>bigint</type></entry>
-       <entry>get current transaction ID</entry>
+       <entry>get current transaction ID, assigning a new one if current transaction does not have one</entry>

^ the

That looks good.

Committed. Thanks!

Regards,

--
Fujii Masao

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#8Michael Paquier
michael.paquier@gmail.com
In reply to: Fujii Masao (#7)
Re: why does txid_current() assign new transaction-id?

On Wed, Jun 3, 2015 at 12:13 PM, Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, Jun 3, 2015 at 9:04 AM, Naoya Anzai <nao-anzai@xc.jp.nec.com>
wrote:

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 89a609f..6485b42 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -16233,7 +16233,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
<row>

<entry><literal><function>txid_current()</function></literal></entry>

<entry><type>bigint</type></entry>
-       <entry>get current transaction ID</entry>
+       <entry>get current transaction ID, assigning a new one if

current transaction does not have one</entry>

^

the

That looks good.

Committed. Thanks!

Thanks for picking up the fix.
--
Michael