--- src/backend/utils/misc/guc.c
+++ src/backend/utils/misc/guc.c
@@ -8837,6 +8855,11 @@ ShowAllGUCConfig(DestReceiver *dest)
 	end_tup_output(tstate);
 }
 
+/*
+ * Returns an array of modified GUC options to show in EXPLAIN. Only options
+ * related to query planning (marked with GUC_EXPLAIN), with values different
+ * from built-in defaults.
+ */
 struct config_generic **
 get_explain_guc_options(int *num)
 {
@@ -8844,7 +8867,13 @@ get_explain_guc_options(int *num)
 	struct config_generic **result;
 
 	*num = 0;
-	result = palloc(sizeof(struct config_generic *) * num_guc_variables);
+
+	/*
+	 * Allocate enough space to fit all GUC_EXPLAIN options. We may not
+	 * need all the space, but there are fairly few such options so we
+	 * don't waste a lot of memory.
+	 */
+	result = palloc(sizeof(struct config_generic *) * num_guc_explain_variables);
 
 	for (i = 0; i < num_guc_variables; i++)
 	{
@@ -8912,6 +8941,8 @@ get_explain_guc_options(int *num)
 		/* assign to the values array */
 		result[*num] = conf;
 		*num = *num + 1;
+
+		Assert(*num <= num_guc_explain_variables);
 	}
 
 	return result;
