diff -ruN postgresql-7.4.7-old/contrib/pg_autovacuum/pg_autovacuum.c postgresql-7.4.7/contrib/pg_autovacuum/pg_autovacuum.c
--- postgresql-7.4.7-old/contrib/pg_autovacuum/pg_autovacuum.c	2004-05-26 20:48:36.000000000 +0200
+++ postgresql-7.4.7/contrib/pg_autovacuum/pg_autovacuum.c	2004-10-31 22:41:28.848679464 +0100
@@ -5,6 +5,12 @@
  */
 
 #include "pg_autovacuum.h"
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
 #define TIMEBUFF 256
 FILE	   *LOGOUTPUT;
 char		timebuffer[TIMEBUFF];
@@ -30,6 +36,15 @@
 daemonize()
 {
 	pid_t		pid;
+	int		nullfd;
+	char*		pgdata = getenv("PGDATA");
+
+	/* Check if PGDATA is set. Needed to create pid file. */
+	if (!pgdata)
+	{
+	    fprintf(stderr, "Error: PGDATA not defined. Could not create pid file. Aborting\n");
+	    _exit(1);
+	}
 
 	pid = fork();
 	if (pid == (pid_t) -1)
@@ -40,10 +55,39 @@
 	}
 	else if (pid)
 	{							/* parent */
+#define PIDFILEBUF 1024
+		char databuf[PIDFILEBUF];
+		char *dataptr = databuf;
+		char *pidfilename = "/autovacuum.pid";
+		FILE *PIDFILE;
+
+		/* If we can determine PGDATA, write a PID file there.
+		 * This is a fudge that depends on Debian's setting PGDATA
+		 * before calling pg_autovacuum in the init script.  It
+		 * could do with being cleaned up for submission upstream. */
+		strncpy(dataptr, pgdata, PIDFILEBUF - 1);
+		databuf[PIDFILEBUF - 1] = '\0';
+		if (strlen(dataptr) > 0 && strlen(dataptr) < PIDFILEBUF - strlen(pidfilename - 1)) {
+			strcat(dataptr, pidfilename);
+			/* Don't bother to report errors in opening PIDFILE */
+			if ((PIDFILE = fopen((const char *) dataptr, "w")) != NULL) {
+				fprintf(PIDFILE, "%d\n", pid);
+				fclose(PIDFILE);
+			}
+		}
 		/* Parent should just exit, without doing any atexit cleanup */
 		_exit(0);
 	}
 
+	/* detach from stdin, stdout and stderr 
+	 * (patched by Martin Pitt <mpitt@debian.org> for the Debian package) */
+	nullfd = open("/dev/null", O_RDWR);
+	dup2(nullfd, 0);
+	dup2(nullfd, 1);
+	dup2(nullfd, 2);
+	if (nullfd != 0 && nullfd != 1 && nullfd != 2)
+	    close(nullfd);
+
 /* GH: If there's no setsid(), we hopefully don't need silent mode.
  * Until there's a better solution.  */
 #ifdef HAVE_SETSID
