diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index a7e0cc9f32..2ca37e70d9 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1528,7 +1528,11 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags)
 	if (fsplan->scan.scanrelid > 0)
 		rtindex = fsplan->scan.scanrelid;
 	else
+	{
 		rtindex = bms_next_member(fsplan->fs_base_relids, -1);
+		if (rtindex < 0)
+			elog(ERROR, "could not find index relation");
+	}
 	rte = exec_rt_fetch(rtindex, estate);
 
 	/* Get info about foreign table. */
@@ -2877,6 +2881,8 @@ postgresExplainForeignScan(ForeignScanState *node, ExplainState *es)
 				ptr++;
 		}
 		rtoffset = bms_next_member(plan->fs_base_relids, -1) - minrti;
+		if (rtoffset < 0)
+			elog(ERROR, "could not find offset relation");
 
 		/* Now we can translate the string */
 		relations = makeStringInfo();
