diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index ac8cd1d..feb7011 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -38,6 +38,7 @@
 #include "storage/fd.h"
 #include "storage/buffile.h"
 #include "storage/buf_internals.h"
+#include "utils/resowner.h"
 
 /*
  * We break BufFiles into gigabyte-sized segments, regardless of RELSEG_SIZE.
@@ -67,6 +68,7 @@ struct BufFile
 	bool		isTemp;			/* can only add files if this is TRUE */
 	bool		isInterXact;	/* keep open over transactions? */
 	bool		dirty;			/* does buffer need to be written? */
+	ResourceOwner resowner;		/* Resowner to use when extending */
 
 	/*
 	 * "current pos" is position of start of buffer within the logical file.
@@ -118,11 +120,21 @@ static void
 extendBufFile(BufFile *file)
 {
 	File		pfile;
+	ResourceOwner saved = CurrentResourceOwner;
+
+	/*
+	 * Temporarily set resource owner to the one used for
+	 * BufFileCreateTemp so the new segment has the same lifetime as
+	 * the first.
+	 */
+	CurrentResourceOwner = file->resowner;
 
 	Assert(file->isTemp);
 	pfile = OpenTemporaryFile(file->isInterXact);
 	Assert(pfile >= 0);
 
+	CurrentResourceOwner = saved;
+
 	file->files = (File *) repalloc(file->files,
 									(file->numFiles + 1) * sizeof(File));
 	file->offsets = (off_t *) repalloc(file->offsets,
@@ -155,7 +167,7 @@ BufFileCreateTemp(bool interXact)
 	file = makeBufFile(pfile);
 	file->isTemp = true;
 	file->isInterXact = interXact;
-
+	file->resowner = CurrentResourceOwner;
 	return file;
 }
 
