diff --git a/CHANGES b/CHANGES
index f668f17331f2753713ff1ab8f8b610a272db73a9..e5b29d3ffe439fa1bbab193822c7ff1a29f60597 100644
--- a/CHANGES
+++ b/CHANGES
@@ -22,6 +22,7 @@ OpenLDAP 2.4.17 Engineering
 	Fixed slapd-hdb freeing of already freed entries (ITS#6074)
 	Fixed slapd-hdb entryinfo cleanup (ITS#6088)
 	Fixed slapd-hdb dncache lockups (ITS#6095)
+	Fixed slapd-sql with BACKSQL_ARBITRARY_KEY defined (ITS#6100)
 	Added slapo-rwm rwm-drop-unrequested-attrs config option (ITS#6057)
 	Fixed slapo-rwm dn passing (ITS#6070)
 	Fixed slapo-rwm entry free (ITS#6058)
diff --git a/servers/slapd/back-sql/init.c b/servers/slapd/back-sql/init.c
index 6b091a4eda59e15b6331f09c2762b434b922fd1d..5aaa0011f1b47af8f9ae89747bdfcfd3c52f4ffb 100644
--- a/servers/slapd/back-sql/init.c
+++ b/servers/slapd/back-sql/init.c
@@ -41,7 +41,9 @@ sql_back_initialize(
 #ifdef SLAP_CONTROL_X_TREE_DELETE
 		SLAP_CONTROL_X_TREE_DELETE,
 #endif /* SLAP_CONTROL_X_TREE_DELETE */
+#ifndef BACKSQL_ARBITRARY_KEY
 		LDAP_CONTROL_PAGEDRESULTS,
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 		NULL
 	};
 
diff --git a/servers/slapd/back-sql/search.c b/servers/slapd/back-sql/search.c
index a65ffcf027388734073696f83078dd9a92eb739f..0e54ac995d3b35246fc29fe18932c2f877ccd54b 100644
--- a/servers/slapd/back-sql/search.c
+++ b/servers/slapd/back-sql/search.c
@@ -48,6 +48,9 @@ static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f,
    and the other 26 for ldap_entries ID number. If your max(oc_map_id) is more
    than 63, you will need to steal more bits from ldap_entries ID number and
    put them into the OC ID part of the cookie. */
+
+/* NOTE: not supported when BACKSQL_ARBITRARY_KEY is defined */
+#ifndef BACKSQL_ARBITRARY_KEY
 #define SQL_TO_PAGECOOKIE(id, oc) (((id) << 6 ) | ((oc) & 0x3F))
 #define PAGECOOKIE_TO_SQL_ID(pc) ((pc) >> 6)
 #define PAGECOOKIE_TO_SQL_OC(pc) ((pc) & 0x3F)
@@ -58,6 +61,7 @@ static void send_paged_response(
 	Operation *op,
 	SlapReply *rs,
 	ID  *lastid );
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 
 static int
 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
@@ -1551,6 +1555,7 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
 		assert( 0 );
 	}
 
+#ifndef BACKSQL_ARBITRARY_KEY
 	/* If paged results are in effect, ignore low ldap_entries.id numbers */
 	if ( get_pagedresults(bsi->bsi_op) > SLAP_CONTROL_IGNORED ) {
 		unsigned long lowid = 0;
@@ -1574,6 +1579,7 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
 					lowidstring );
 		}
 	}
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 
 	rc = backsql_process_filter( bsi, bsi->bsi_filter );
 	if ( rc > 0 ) {
@@ -1654,6 +1660,7 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi )
 		return BACKSQL_AVL_STOP;
 	}
 
+#ifndef BACKSQL_ARBITRARY_KEY
 	/* If paged results have already completed this objectClass, skip it */
 	if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
 		if ( oc->bom_id < PAGECOOKIE_TO_SQL_OC( ((PagedResultsState *)op->o_pagedresults_state)->ps_cookie ) )
@@ -1661,6 +1668,7 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi )
 			return BACKSQL_AVL_CONTINUE;
 		}
 	}
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 
 	if ( bsi->bsi_n_candidates == -1 ) {
 		Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
@@ -1988,7 +1996,9 @@ backsql_search( Operation *op, SlapReply *rs )
 	backsql_srch_info	bsi = { 0 };
 	backsql_entryID		*eid = NULL;
 	struct berval		nbase = BER_BVNULL;
-	unsigned long 		lastid = 0;
+#ifndef BACKSQL_ARBITRARY_KEY
+	ID			lastid = 0;
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 
 	Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
 		"base=\"%s\", filter=\"%s\", scope=%d,", 
@@ -2125,6 +2135,7 @@ backsql_search( Operation *op, SlapReply *rs )
 		( op->ors_limit->lms_s_unchecked == -1 ? -2 :
 		( op->ors_limit->lms_s_unchecked ) ) );
 
+#ifndef BACKSQL_ARBITRARY_KEY
 	/* If paged results are in effect, check the paging cookie */
 	if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
 		rs->sr_err = parse_paged_cookie( op, rs );
@@ -2133,6 +2144,7 @@ backsql_search( Operation *op, SlapReply *rs )
 			goto done;
 		}
 	}
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 
 	switch ( bsi.bsi_scope ) {
 	case LDAP_SCOPE_BASE:
@@ -2409,6 +2421,7 @@ backsql_search( Operation *op, SlapReply *rs )
 
 		if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE )
 		{
+#ifndef BACKSQL_ARBITRARY_KEY
 			/* If paged results are in effect, see if the page limit was exceeded */
 			if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
 				if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size )
@@ -2419,6 +2432,7 @@ backsql_search( Operation *op, SlapReply *rs )
 				}
 				lastid = SQL_TO_PAGECOOKIE( eid->eid_id, eid->eid_oc_id );
 			}
+#endif /* ! BACKSQL_ARBITRARY_KEY */
 			rs->sr_attrs = op->ors_attrs;
 			rs->sr_operational_attrs = NULL;
 			rs->sr_entry = e;
@@ -2466,9 +2480,12 @@ end_of_search:;
 
 send_results:;
 	if ( rs->sr_err != SLAPD_ABANDON ) {
+#ifndef BACKSQL_ARBITRARY_KEY
 		if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
 			send_paged_response( op, rs, NULL );
-		} else {
+		} else
+#endif /* ! BACKSQL_ARBITRARY_KEY */
+		{
 			send_ldap_result( op, rs );
 		}
 	}
@@ -2663,7 +2680,7 @@ backsql_entry_release(
 	return 0;
 }
 
-
+#ifndef BACKSQL_ARBITRARY_KEY
 /* This function is copied verbatim from back-bdb/search.c */
 static int
 parse_paged_cookie( Operation *op, SlapReply *rs )
@@ -2716,7 +2733,7 @@ static void
 send_paged_response( 
 	Operation	*op,
 	SlapReply	*rs,
-	unsigned long	*lastid )
+	ID		*lastid )
 {
 	LDAPControl	ctrl, *ctrls[2];
 	BerElementBuffer berbuf;
@@ -2767,3 +2784,4 @@ send_paged_response(
 done:
 	(void) ber_free_buf( ber );
 }
+#endif /* ! BACKSQL_ARBITRARY_KEY */