diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 3f8a950f37dc57ff70db340a6ee7e808fb42a2d1..ad95b1bc55d78b67df6f74634b17f9079e459c92 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -624,16 +624,28 @@ partition_bounds_equal(PartitionKey key,
 		{
 			int32		cmpval;
 
+			/* For range partitions, the bounds might not be finite. */
+			if (b1->content != NULL)
+			{
+				/*
+				 * A finite bound always differs from an infinite bound, and
+				 * different kinds of infinities differ from each other.
+				 */
+				if (b1->content[i][j] != b2->content[i][j])
+					return false;
+
+				/* Non-finite bounds are equal without further examination. */
+				if (b1->content[i][j] != RANGE_DATUM_FINITE)
+					continue;
+			}
+
+			/* Compare the actual values */
 			cmpval = DatumGetInt32(FunctionCall2Coll(&key->partsupfunc[j],
 													 key->partcollation[j],
 													 b1->datums[i][j],
 													 b2->datums[i][j]));
 			if (cmpval != 0)
 				return false;
-
-			/* Range partitions can have infinite datums */
-			if (b1->content != NULL && b1->content[i][j] != b2->content[i][j])
-				return false;
 		}
 
 		if (b1->indexes[i] != b2->indexes[i])