diff -ruN postgresql-7.4.9-old/src/backend/storage/file/fd.c postgresql-7.4.9/src/backend/storage/file/fd.c
--- postgresql-7.4.9-old/src/backend/storage/file/fd.c	2005-08-07 20:48:00.000000000 +0200
+++ postgresql-7.4.9/src/backend/storage/file/fd.c	2005-10-06 21:53:26.000000000 +0200
@@ -50,6 +50,7 @@
 #include "storage/fd.h"
 #include "storage/ipc.h"
 
+#include <sys/resource.h>
 
 /* Filename components for OpenTemporaryFile */
 #define PG_TEMP_FILES_DIR "pgsql_tmp"
@@ -278,15 +279,21 @@
 	int			used = 0;
 	int			highestfd = 0;
 	int			j;
+  	struct rlimit	    rlim;
 
 	size = 1024;
 	fd = (int *) palloc(size * sizeof(int));
+  	getrlimit(RLIMIT_NOFILE, &rlim);
 
 	/* dup until failure or probe limit reached */
 	for (;;)
 	{
 		int		thisfd;
 
+                /* Don't go beyond RLIMIT_NOFILE */
+		if (highestfd >= rlim.rlim_cur - 1)
+ 		    break;
+
 		thisfd = dup(0);
 		if (thisfd < 0)
 		{
