diff --git a/src/backend/port/unix_latch.c b/src/backend/port/unix_latch.c
index 29ef38226aaa16e575161604120568b5d964b270..d3b2247f0527613b56e20540e03afefc905d74ae 100644
--- a/src/backend/port/unix_latch.c
+++ b/src/backend/port/unix_latch.c
@@ -33,6 +33,7 @@
 #include "postgres.h"
 
 #include <fcntl.h>
+#include <limits.h>
 #include <signal.h>
 #include <unistd.h>
 #include <sys/time.h>
@@ -176,9 +177,10 @@ DisownLatch(volatile Latch *latch)
  * to wait for. If the latch is already set (and WL_LATCH_SET is given), the
  * function returns immediately.
  *
- * The 'timeout' is given in milliseconds. It must be >= 0 if WL_TIMEOUT flag
- * is given.  Note that some extra overhead is incurred when WL_TIMEOUT is
- * given, so avoid using a timeout if possible.
+ * The "timeout" is given in milliseconds. It must be >= 0 if WL_TIMEOUT flag
+ * is given.  Although it is declared as "long", we don't actually support
+ * timeouts longer than INT_MAX milliseconds.  Note that some extra overhead
+ * is incurred when WL_TIMEOUT is given, so avoid using a timeout if possible.
  *
  * The latch must be owned by the current process, ie. it must be a
  * backend-local latch initialized with InitLatch, or a shared latch
@@ -243,7 +245,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
 	if (wakeEvents & WL_TIMEOUT)
 	{
 		INSTR_TIME_SET_CURRENT(start_time);
-		Assert(timeout >= 0);
+		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 
 #ifndef HAVE_POLL
diff --git a/src/backend/port/win32_latch.c b/src/backend/port/win32_latch.c
index 95370d9d58ddc49f56de54f7593db3f5c6354c19..575035c28d400fe470bca4caaabb12ff89adfa56 100644
--- a/src/backend/port/win32_latch.c
+++ b/src/backend/port/win32_latch.c
@@ -20,6 +20,7 @@
 #include "postgres.h"
 
 #include <fcntl.h>
+#include <limits.h>
 #include <signal.h>
 #include <unistd.h>
 
@@ -130,7 +131,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
 	if (wakeEvents & WL_TIMEOUT)
 	{
 		INSTR_TIME_SET_CURRENT(start_time);
-		Assert(timeout >= 0);
+		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
 	else