Hi all,

Please see attached a patch that finishes the support for currval.
All the structure was in place in GTM but there was still one missing call
in sequence.c when calling the function.

Now it is possible to get current values for sequences in the whole cluster.

Regards,

-- 
Michael Paquier
NIPPON TELEGRAPH AND
TELEPHONE CORPORATION
NTT Open Source Software Center
diff --git a/src/backend/access/transam/gtm.c b/src/backend/access/transam/gtm.c
index c3cb72b..3be8d20 100644
--- a/src/backend/access/transam/gtm.c
+++ b/src/backend/access/transam/gtm.c
@@ -172,7 +172,7 @@ GetSnapshotGTM(GlobalTransactionId gxid, bool canbe_grouped)
 }
 
 
-/**
+/*
  * Create a sequence on the GTM.
  *
  * 
@@ -189,7 +189,30 @@ CreateSequenceGTM(char *seqname, GTM_Sequence increment, GTM_Sequence minval,
 	return conn ? open_sequence(conn, &seqkey, increment, minval, maxval, startval, cycle) : 0;
 }
 
-/**
+/*
+ * get the current sequence value
+ */
+
+GTM_Sequence
+GetCurrentValGTM(char *seqname)
+{
+	GTM_Sequence ret = -1;
+	GTM_SequenceKeyData seqkey;
+	CheckConnection();
+	seqkey.gsk_keylen = strlen(seqname);
+	seqkey.gsk_key = seqname;
+
+	if (conn)
+		ret =  get_current(conn, &seqkey);
+	if (ret < 0)
+	{
+		CloseGTM();
+		InitGTM();
+	}
+	return ret;
+}
+
+/*
  * Get the next sequence value
  */
 GTM_Sequence
@@ -211,7 +234,7 @@ GetNextValGTM(char *seqname)
 	return ret;
 }
 
-/**
+/*
  * Drop the sequence
  */
 int
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index ba9a932..5df50a3 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -533,8 +533,8 @@ nextval_internal(Oid relid)
 		result = (int64) GetNextValGTM(RelationGetRelationName(seqrel));
 		if (result < 0)
 			ereport(ERROR,
-				(errcode(ERRCODE_CONNECTION_FAILURE),
-				 errmsg("GTM error, could not obtain sequence value")));
+					(errcode(ERRCODE_CONNECTION_FAILURE),
+					 errmsg("GTM error, could not obtain sequence value")));
 	} else
 	{
 #endif
@@ -714,6 +714,19 @@ currval_oid(PG_FUNCTION_ARGS)
 	/* open and AccessShareLock sequence */
 	init_sequence(relid, &elm, &seqrel);
 
+#ifdef PGXC
+	if (IS_PGXC_COORDINATOR)
+	{
+		result = (int64) GetCurrentValGTM(RelationGetRelationName(seqrel));
+		if (result < 0)
+			ereport(ERROR,
+					(errcode(ERRCODE_CONNECTION_FAILURE),
+					 errmsg("GTM error, could not obtain sequence value")));
+	}
+	else
+	{
+#endif
+
 	if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_SELECT) != ACLCHECK_OK &&
 		pg_class_aclcheck(elm->relid, GetUserId(), ACL_USAGE) != ACLCHECK_OK)
 		ereport(ERROR,
@@ -729,6 +742,10 @@ currval_oid(PG_FUNCTION_ARGS)
 
 	result = elm->last;
 
+#ifdef PGXC
+	}
+#endif
+
 	relation_close(seqrel, NoLock);
 
 	PG_RETURN_INT64(result);
diff --git a/src/include/access/gtm.h b/src/include/access/gtm.h
index 66ca3f1..88de4d8 100644
--- a/src/include/access/gtm.h
+++ b/src/include/access/gtm.h
@@ -25,6 +25,9 @@ extern GlobalTransactionId BeginTranAutovacuumGTM(void);
 extern int CommitTranGTM(GlobalTransactionId gxid);
 extern int RollbackTranGTM(GlobalTransactionId gxid);
 extern GTM_Snapshot GetSnapshotGTM(GlobalTransactionId gxid, bool canbe_grouped);
+
+/* Sequence interface APIs with GTM */
+extern GTM_Sequence GetCurrentValGTM(char *seqname);
 extern GTM_Sequence GetNextValGTM(char *seqname);
 extern int CreateSequenceGTM(char *seqname, GTM_Sequence increment, 
 		GTM_Sequence minval, GTM_Sequence maxval, GTM_Sequence startval,
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to