diff --git a/src/backend/commands/portalcmds.c b/src/backend/commands/portalcmds.c
index 47e2829b7b3563e52dc21e9f6c0a75cb552b220e..a5b7370acd287ba77f65eba473514fdc4e24e57c 100644
--- a/src/backend/commands/portalcmds.c
+++ b/src/backend/commands/portalcmds.c
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.51 2006/08/29 02:11:29 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.52 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -177,7 +177,7 @@ PerformPortalFetch(FetchStmt *stmt,
 				   char *completionTag)
 {
 	Portal		portal;
-	long		nprocessed;
+	int64		nprocessed;
 
 	/*
 	 * Disallow empty-string cursor name (conflicts with protocol-level
@@ -210,7 +210,7 @@ PerformPortalFetch(FetchStmt *stmt,
 
 	/* Return command status if wanted */
 	if (completionTag)
-		snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s %ld",
+		snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s " INT64_FORMAT,
 				 stmt->ismove ? "MOVE" : "FETCH",
 				 nprocessed);
 }
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 3db05898871fdf581d822b1e767a1cce1ef04eef..e6268da6e543ac7ca316ea703611a7199aca5451 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.159 2006/08/29 02:11:29 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.160 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -45,7 +45,7 @@ static int	_SPI_pquery(QueryDesc *queryDesc, long tcount);
 
 static void _SPI_error_callback(void *arg);
 
-static void _SPI_cursor_operation(Portal portal, bool forward, long count,
+static void _SPI_cursor_operation(Portal portal, bool forward, int64 count,
 					  DestReceiver *dest);
 
 static _SPI_plan *_SPI_copy_plan(_SPI_plan *plan, int location);
@@ -980,7 +980,7 @@ SPI_cursor_find(const char *name)
  *	Fetch rows in a cursor
  */
 void
-SPI_cursor_fetch(Portal portal, bool forward, long count)
+SPI_cursor_fetch(Portal portal, bool forward, int64 count)
 {
 	_SPI_cursor_operation(portal, forward, count,
 						  CreateDestReceiver(DestSPI, NULL));
@@ -994,7 +994,7 @@ SPI_cursor_fetch(Portal portal, bool forward, long count)
  *	Move in a cursor
  */
 void
-SPI_cursor_move(Portal portal, bool forward, long count)
+SPI_cursor_move(Portal portal, bool forward, int64 count)
 {
 	_SPI_cursor_operation(portal, forward, count, None_Receiver);
 }
@@ -1639,10 +1639,10 @@ _SPI_error_callback(void *arg)
  *	Do a FETCH or MOVE in a cursor
  */
 static void
-_SPI_cursor_operation(Portal portal, bool forward, long count,
+_SPI_cursor_operation(Portal portal, bool forward, int64 count,
 					  DestReceiver *dest)
 {
-	long		nfetched;
+	int64		nfetched;
 
 	/* Check that the portal is valid */
 	if (!PortalIsValid(portal))
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 4a0ce515b8a74272c6c09c2b2bcf1c54bde88c83..f53ba9ce60d21c1f355366cc14cfeedf481b44dd 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.559 2006/08/30 23:34:21 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.560 2006/09/02 18:17:17 momjian Exp $
  *
  * HISTORY
  *	  AUTHOR			DATE			MAJOR EVENT
@@ -116,6 +116,7 @@ static void doNegateFloat(Value *v);
 %union
 {
 	int					ival;
+	int64				i64val; 
 	char				chr;
 	char				*str;
 	const char			*keyword;
@@ -322,6 +323,7 @@ static void doNegateFloat(Value *v);
 %type <boolean> opt_varying opt_timezone
 
 %type <ival>	Iconst SignedIconst
+%type <i64val>  SignedI64const
 %type <str>		Sconst comment_text
 %type <str>		RoleId opt_granted_by opt_boolean ColId_or_Sconst
 %type <list>	var_list var_list_or_default
@@ -446,6 +448,7 @@ static void doNegateFloat(Value *v);
 /* Special token types, not actually keywords - see the "lex" file */
 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
 %token <ival>	ICONST PARAM
+%token <i64val> I64CONST 
 
 /* precedence: lowest to highest */
 %nonassoc	SET				/* see relation_expr_opt_alias */
@@ -3354,6 +3357,27 @@ fetch_direction:
 					n->howMany = $1;
 					$$ = (Node *)n;
 				}
+			| ABSOLUTE_P SignedI64const
+				{
+						FetchStmt *n = makeNode(FetchStmt);
+						n->direction = FETCH_ABSOLUTE;
+						n->howMany = $2;
+						$$ = (Node *)n;
+				}
+			| RELATIVE_P SignedI64const
+				{
+						FetchStmt *n = makeNode(FetchStmt);
+						n->direction = FETCH_RELATIVE;
+						n->howMany = $2;
+						$$ = (Node *)n;
+				}
+			| SignedI64const
+				{
+						FetchStmt *n = makeNode(FetchStmt);
+						n->direction = FETCH_FORWARD;
+						n->howMany = $1;
+						$$ = (Node *)n;
+				}
 			| ALL
 				{
 					FetchStmt *n = makeNode(FetchStmt);
@@ -3375,6 +3399,13 @@ fetch_direction:
 					n->howMany = $2;
 					$$ = (Node *)n;
 				}
+			| FORWARD SignedI64const
+				{
+						FetchStmt *n = makeNode(FetchStmt);
+						n->direction = FETCH_FORWARD;
+						n->howMany = $2;
+						$$ = (Node *)n;
+				}
 			| FORWARD ALL
 				{
 					FetchStmt *n = makeNode(FetchStmt);
@@ -3396,6 +3427,13 @@ fetch_direction:
 					n->howMany = $2;
 					$$ = (Node *)n;
 				}
+			| BACKWARD SignedI64const
+				{
+						FetchStmt *n = makeNode(FetchStmt);
+						n->direction = FETCH_BACKWARD;
+						n->howMany = $2;
+						$$ = (Node *)n;
+				}
 			| BACKWARD ALL
 				{
 					FetchStmt *n = makeNode(FetchStmt);
@@ -8441,6 +8479,9 @@ RoleId:		ColId									{ $$ = $1; };
 SignedIconst: ICONST								{ $$ = $1; }
 			| '-' ICONST							{ $$ = - $2; }
 		;
+SignedI64const: I64CONST							{ $$ = $1; }
+			| '-' I64CONST							{ $$ = - $2; }
+		;
 
 /*
  * Name classification hierarchy.
diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l
index 13e9604e38e3f4f90e516755efa35fd9e0dc33fb..2a8ac8de86820b3ae5b4dc101e95db58395cef18 100644
--- a/src/backend/parser/scan.l
+++ b/src/backend/parser/scan.l
@@ -24,7 +24,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.135 2006/05/21 20:10:42 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.136 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -666,6 +666,22 @@ other			.
 #endif
 						)
 					{
+						/* For Fetch/Move stmt, convert the string into int64 value */
+						if((strcmp(yylval.keyword, "fetch")==0) || (strcmp(yylval.keyword, "move")==0))
+						{
+								int64 int64Val;
+								errno = 0;
+	
+								int64Val =	strtoll(yytext, &endptr, 10);
+								if (*endptr != '\0' || errno == ERANGE)
+								{
+											yylval.str = pstrdup(yytext);
+											return FCONST;
+								}
+								yylval.i64val = int64Val; 
+								return I64CONST;
+						}
+
 						/* integer too large, treat it as a float */
 						yylval.str = pstrdup(yytext);
 						return FCONST;
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 6552d943431b77d0debade190c636f128377db45..62ad35bc00c2b1b1879c477393af8e51894bf2ff 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.503 2006/08/30 18:22:02 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.504 2006/09/02 18:17:17 momjian Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -1687,7 +1687,7 @@ exec_bind_message(StringInfo input_message)
  * Process an "Execute" message for a portal
  */
 static void
-exec_execute_message(const char *portal_name, long max_rows)
+exec_execute_message(const char *portal_name, int64 max_rows)
 {
 	CommandDest dest;
 	DestReceiver *receiver;
@@ -3308,13 +3308,13 @@ PostgresMain(int argc, char *argv[], const char *username)
 			case 'E':			/* execute */
 				{
 					const char *portal_name;
-					int			max_rows;
+					int64			max_rows;
 
 					/* Set statement_timestamp() */
 					SetCurrentStatementStartTimestamp();
 
 					portal_name = pq_getmsgstring(&input_message);
-					max_rows = pq_getmsgint(&input_message, 4);
+					max_rows = pq_getmsgint64(&input_message);
 					pq_getmsgend(&input_message);
 
 					exec_execute_message(portal_name, max_rows);
diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c
index 3ede40570ab8e0181abe33cbceb6832544313ffa..257655ddb0f2dd34f7a0118e3ca94351ad2781f8 100644
--- a/src/backend/tcop/pquery.c
+++ b/src/backend/tcop/pquery.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.107 2006/08/14 22:57:15 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.108 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -38,18 +38,18 @@ static void ProcessQuery(Query *parsetree,
 			 DestReceiver *dest,
 			 char *completionTag);
 static void FillPortalStore(Portal portal);
-static uint32 RunFromStore(Portal portal, ScanDirection direction, long count,
+static uint64 RunFromStore(Portal portal, ScanDirection direction, int64 count,
 			 DestReceiver *dest);
-static long PortalRunSelect(Portal portal, bool forward, long count,
+static int64 PortalRunSelect(Portal portal, bool forward, int64 count,
 				DestReceiver *dest);
 static void PortalRunUtility(Portal portal, Query *query,
 				 DestReceiver *dest, char *completionTag);
 static void PortalRunMulti(Portal portal,
 			   DestReceiver *dest, DestReceiver *altdest,
 			   char *completionTag);
-static long DoPortalRunFetch(Portal portal,
+static int64 DoPortalRunFetch(Portal portal,
 				 FetchDirection fdirection,
-				 long count,
+				 int64 count,
 				 DestReceiver *dest);
 static void DoPortalRewind(Portal portal);
 
@@ -581,7 +581,7 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
  * suspended due to exhaustion of the count parameter.
  */
 bool
-PortalRun(Portal portal, long count,
+PortalRun(Portal portal, int64 count,
 		  DestReceiver *dest, DestReceiver *altdest,
 		  char *completionTag)
 {
@@ -773,15 +773,15 @@ PortalRun(Portal portal, long count,
  *
  * Returns number of rows processed (suitable for use in result tag)
  */
-static long
+static int64
 PortalRunSelect(Portal portal,
 				bool forward,
-				long count,
+				int64 count,
 				DestReceiver *dest)
 {
 	QueryDesc  *queryDesc;
 	ScanDirection direction;
-	uint32		nprocessed;
+	uint64		nprocessed;
 
 	/*
 	 * NB: queryDesc will be NULL if we are fetching from a held cursor or a
@@ -834,12 +834,12 @@ PortalRunSelect(Portal portal,
 
 		if (!ScanDirectionIsNoMovement(direction))
 		{
-			long		oldPos;
+			int64		oldPos;
 
 			if (nprocessed > 0)
 				portal->atStart = false;		/* OK to go backward now */
 			if (count == 0 ||
-				(unsigned long) nprocessed < (unsigned long) count)
+				(uint64) nprocessed < (uint64) count)
 				portal->atEnd = true;	/* we retrieved 'em all */
 			oldPos = portal->portalPos;
 			portal->portalPos += nprocessed;
@@ -882,7 +882,7 @@ PortalRunSelect(Portal portal,
 				portal->portalPos++;	/* adjust for endpoint case */
 			}
 			if (count == 0 ||
-				(unsigned long) nprocessed < (unsigned long) count)
+				(uint64) nprocessed < (uint64) count)
 			{
 				portal->atStart = true; /* we retrieved 'em all */
 				portal->portalPos = 0;
@@ -890,7 +890,7 @@ PortalRunSelect(Portal portal,
 			}
 			else
 			{
-				long		oldPos;
+				int64		oldPos;
 
 				oldPos = portal->portalPos;
 				portal->portalPos -= nprocessed;
@@ -958,11 +958,11 @@ FillPortalStore(Portal portal)
  * are run in the caller's memory context (since we have no estate).  Watch
  * out for memory leaks.
  */
-static uint32
-RunFromStore(Portal portal, ScanDirection direction, long count,
+static uint64
+RunFromStore(Portal portal, ScanDirection direction, int64 count,
 			 DestReceiver *dest)
 {
-	long		current_tuple_count = 0;
+	int64		current_tuple_count = 0;
 	TupleTableSlot *slot;
 
 	slot = MakeSingleTupleTableSlot(portal->tupDesc);
@@ -1010,7 +1010,7 @@ RunFromStore(Portal portal, ScanDirection direction, long count,
 
 	ExecDropSingleTupleTableSlot(slot);
 
-	return (uint32) current_tuple_count;
+	return (uint64) current_tuple_count;
 }
 
 /*
@@ -1200,13 +1200,13 @@ PortalRunMulti(Portal portal,
  *
  * Returns number of rows processed (suitable for use in result tag)
  */
-long
+int64
 PortalRunFetch(Portal portal,
 			   FetchDirection fdirection,
-			   long count,
+			   int64 count,
 			   DestReceiver *dest)
 {
-	long		result;
+	int64		result;
 	Portal		saveActivePortal;
 	Snapshot	saveActiveSnapshot;
 	ResourceOwner saveResourceOwner;
@@ -1307,10 +1307,10 @@ PortalRunFetch(Portal portal,
  *
  * Returns number of rows processed (suitable for use in result tag)
  */
-static long
+static int64
 DoPortalRunFetch(Portal portal,
 				 FetchDirection fdirection,
-				 long count,
+				 int64 count,
 				 DestReceiver *dest)
 {
 	bool		forward;
@@ -1347,7 +1347,7 @@ DoPortalRunFetch(Portal portal,
 				 * we are.	In any case, we arrange to fetch the target row
 				 * going forwards.
 				 */
-				if (portal->posOverflow || portal->portalPos == LONG_MAX ||
+				if (portal->posOverflow || portal->portalPos == LLONG_MAX ||
 					count - 1 <= portal->portalPos / 2)
 				{
 					DoPortalRewind(portal);
@@ -1357,7 +1357,7 @@ DoPortalRunFetch(Portal portal,
 				}
 				else
 				{
-					long		pos = portal->portalPos;
+					int64		pos = portal->portalPos;
 
 					if (portal->atEnd)
 						pos++;	/* need one extra fetch if off end */
@@ -1469,7 +1469,7 @@ DoPortalRunFetch(Portal portal,
 	 */
 	if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
 	{
-		long		result = portal->portalPos;
+		int64		result = portal->portalPos;
 
 		if (result > 0 && !portal->atEnd)
 			result--;
diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h
index 0d91290968dabe3b8ac6ae228a9a013e8ca9d822..b8833b355a1456f02ef0283d54a4da33db1bc316 100644
--- a/src/include/executor/spi.h
+++ b/src/include/executor/spi.h
@@ -2,7 +2,7 @@
  *
  * spi.h
  *
- * $PostgreSQL: pgsql/src/include/executor/spi.h,v 1.55 2006/08/27 23:47:58 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/executor/spi.h,v 1.56 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -126,8 +126,8 @@ extern void SPI_freetuptable(SPITupleTable *tuptable);
 extern Portal SPI_cursor_open(const char *name, void *plan,
 				Datum *Values, const char *Nulls, bool read_only);
 extern Portal SPI_cursor_find(const char *name);
-extern void SPI_cursor_fetch(Portal portal, bool forward, long count);
-extern void SPI_cursor_move(Portal portal, bool forward, long count);
+extern void SPI_cursor_fetch(Portal portal, bool forward, int64 count);
+extern void SPI_cursor_move(Portal portal, bool forward, int64 count);
 extern void SPI_cursor_close(Portal portal);
 
 extern void AtEOXact_SPI(bool isCommit);
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 9e808396734416fc69b5d695edd9af8715222700..acdb05ed4fe436b4d402f12a92502a689affd017 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.326 2006/08/30 23:34:22 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.327 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1439,13 +1439,13 @@ typedef enum FetchDirection
 	FETCH_RELATIVE
 } FetchDirection;
 
-#define FETCH_ALL	LONG_MAX
+#define FETCH_ALL	LLONG_MAX
 
 typedef struct FetchStmt
 {
 	NodeTag		type;
 	FetchDirection direction;	/* see above */
-	long		howMany;		/* number of rows, or position argument */
+	int64		howMany;		/* number of rows, or position argument */
 	char	   *portalname;		/* name of portal (cursor) */
 	bool		ismove;			/* TRUE if MOVE */
 } FetchStmt;
diff --git a/src/include/tcop/pquery.h b/src/include/tcop/pquery.h
index 9a52009d69355939a371ec6da3c3cfa470f22657..eeeaf70a6b179669f158696660671fcbbb642b2f 100644
--- a/src/include/tcop/pquery.h
+++ b/src/include/tcop/pquery.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/tcop/pquery.h,v 1.37 2006/03/05 15:59:00 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/tcop/pquery.h,v 1.38 2006/09/02 18:17:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -30,13 +30,13 @@ extern void PortalStart(Portal portal, ParamListInfo params,
 extern void PortalSetResultFormat(Portal portal, int nFormats,
 					  int16 *formats);
 
-extern bool PortalRun(Portal portal, long count,
+extern bool PortalRun(Portal portal, int64 count,
 		  DestReceiver *dest, DestReceiver *altdest,
 		  char *completionTag);
 
-extern long PortalRunFetch(Portal portal,
+extern int64 PortalRunFetch(Portal portal,
 			   FetchDirection fdirection,
-			   long count,
+			   int64 count,
 			   DestReceiver *dest);
 
 #endif   /* PQUERY_H */
diff --git a/src/include/utils/portal.h b/src/include/utils/portal.h
index bf3d503004cf6dd3354ec06c238a28e4477e91ca..8104edbca8f7d2473204c88a925f1a06559231ff 100644
--- a/src/include/utils/portal.h
+++ b/src/include/utils/portal.h
@@ -39,7 +39,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/portal.h,v 1.67 2006/08/29 02:11:30 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/portal.h,v 1.68 2006/09/02 18:17:18 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -173,7 +173,7 @@ typedef struct PortalData
 	bool		atStart;
 	bool		atEnd;
 	bool		posOverflow;
-	long		portalPos;
+	int64		portalPos;
 
 	/* Presentation data, primarily used by the pg_cursors system view */
 	TimestampTz	creation_time;	/* time at which this portal was defined */