From a34f357e4d2a8edc1947b5e38bab1e20fad06235 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Wed, 4 Jan 2023 15:40:38 +0900
Subject: [PATCH v2] Fix showing transaction id of a spectoken in an incorrect
 field of pg_locks view.

A transaction id is now displayed in the transactionid field. A
speculative insertion toke is displayed in the objid field.
---
 doc/src/sgml/system-views.sgml    |  6 ++++++
 src/backend/utils/adt/lockfuncs.c | 12 ++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml
index 143ae5b7bb..c5595032b1 100644
--- a/doc/src/sgml/system-views.sgml
+++ b/doc/src/sgml/system-views.sgml
@@ -1578,6 +1578,12 @@
    permanent transaction ID of the current holder of that row lock.
   </para>
 
+  <para>
+   A specualtive insertion lock consists of a transaction ID and a speculative
+   insertion token. The speculative insertion token is displayed in the
+   <structfield>objid</structfield> column.
+  </para>
+
   <para>
    Advisory locks can be acquired on keys consisting of either a single
    <type>bigint</type> value or two integer values.
diff --git a/src/backend/utils/adt/lockfuncs.c b/src/backend/utils/adt/lockfuncs.c
index 3cce4bdbc1..ab99924558 100644
--- a/src/backend/utils/adt/lockfuncs.c
+++ b/src/backend/utils/adt/lockfuncs.c
@@ -312,6 +312,18 @@ pg_lock_status(PG_FUNCTION_ARGS)
 				nulls[8] = true;
 				nulls[9] = true;
 				break;
+			case LOCKTAG_SPECULATIVE_TOKEN:
+				values[6] =
+					TransactionIdGetDatum(instance->locktag.locktag_field1);
+				values[8] = ObjectIdGetDatum(instance->locktag.locktag_field2);
+				nulls[1] = true;
+				nulls[2] = true;
+				nulls[3] = true;
+				nulls[4] = true;
+				nulls[5] = true;
+				nulls[7] = true;
+				nulls[9] = true;
+				break;
 			case LOCKTAG_OBJECT:
 			case LOCKTAG_USERLOCK:
 			case LOCKTAG_ADVISORY:
-- 
2.31.1

