From 90dbfbb9c7b72f257689baa4502df39d043fa77d Mon Sep 17 00:00:00 2001
From: JackTan25 <jacktby@gmail.com>
Date: Sat, 26 Nov 2022 05:55:44 +0200
Subject: [PATCH 3/3] fix limit and count bugs

---
 src/backend/optimizer/path/indxpath.c | 4 ++++
 src/include/nodes/pathnodes.h         | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index fbcb625..fb25b20 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -3181,6 +3181,10 @@ match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
 												   pathkey->pk_opfamily);
 				if (expr)
 				{
+					if (pathkey->limitCount != -1)
+					{
+						((OpExpr*)expr)->KNNValue = pathkey->limitCount;
+					}
 					orderby_clauses = lappend(orderby_clauses, expr);
 					clause_columns = lappend_int(clause_columns, indexcol);
 					found = true;
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 8256e4b..228aa9b 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -1371,8 +1371,8 @@ typedef struct PathKey
 	int			pk_strategy;	/* sort direction (ASC or DESC) */
 	bool		pk_nulls_first; /* do NULLs come before normal values? */
 	
-	int64 limitCount;
-	int64 limitOffset;
+	int limitCount;
+	int limitOffset;
 } PathKey;
 
 /*
-- 
1.8.3.1

