Skip site navigation (1) Skip section navigation (2)

pgsql: Add OpenTransientFile, with automatic cleanup at end-of-xact.

From: Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Add OpenTransientFile, with automatic cleanup at end-of-xact.
Date: 2012-11-27 08:37:55
Message-ID: E1TdGfz-0006xu-6I@gemulon.postgresql.org (view raw or flat)
Thread:
Lists: pgsql-committers
Add OpenTransientFile, with automatic cleanup at end-of-xact.

Files opened with BasicOpenFile or PathNameOpenFile are not automatically
cleaned up on error. That puts unnecessary burden on callers that only want
to keep the file open for a short time. There is AllocateFile, but that
returns a buffered FILE * stream, which in many cases is not the nicest API
to work with. So add function called OpenTransientFile, which returns a
unbuffered fd that's cleaned up like the FILE* returned by AllocateFile().

This plugs a few rare fd leaks in error cases:

1. copy_file() - fixed by by using OpenTransientFile instead of BasicOpenFile
2. XLogFileInit() - fixed by adding close() calls to the error cases. Can't
   use OpenTransientFile here because the fd is supposed to persist over
   transaction boundaries.
3. lo_import/lo_export - fixed by using OpenTransientFile instead of
   PathNameOpenFile.

In addition to plugging those leaks, this replaces many BasicOpenFile() calls
with OpenTransientFile() that were not leaking, because the code meticulously
closed the file on error. That wasn't strictly necessary, but IMHO it's good
for robustness.

The same leaks exist in older versions, but given the rarity of the issues,
I'm not backpatching this. Not yet, anyway - it might be good to backpatch
later, after this mechanism has had some more testing in master branch.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/1f67078ea324d492a366a24abb2ac313c629314f

Modified Files
--------------
src/backend/access/transam/slru.c     |   24 +++---
src/backend/access/transam/timeline.c |   10 ++--
src/backend/access/transam/twophase.c |   53 ++++++--------
src/backend/access/transam/xlog.c     |   31 ++++++---
src/backend/libpq/be-fsstubs.c        |   18 +++---
src/backend/storage/file/copydir.c    |   26 ++++----
src/backend/storage/file/fd.c         |  125 ++++++++++++++++++++++++++++-----
src/backend/storage/smgr/md.c         |    4 +-
src/backend/utils/cache/relmapper.c   |   19 ++---
src/include/storage/fd.h              |   11 ++-
10 files changed, 209 insertions(+), 112 deletions(-)


pgsql-committers by date

Next:From: Tom LaneDate: 2012-11-27 15:58:45
Subject: pgsql: Add explicit casts in ilist.h's inline functions.
Previous:From: Bruce MomjianDate: 2012-11-26 22:36:32
Subject: pgsql: Remove inaccurate "Incrementally Updated Backups"documentation

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group