1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* fd.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Virtual file descriptor code.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2007-01-05 23:20:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-01-09 23:03:51 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.134 2007/01/09 22:03:51 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
*
|
|
|
|
* This code manages a cache of 'virtual' file descriptors (VFDs).
|
|
|
|
* The server opens many file descriptors for a variety of reasons,
|
|
|
|
* including base tables, scratch files (e.g., sort and hash spool
|
|
|
|
* files), and random calls to C library routines like system(3); it
|
|
|
|
* is quite easy to exceed system limits on the number of open files a
|
|
|
|
* single process can have. (This is around 256 on many modern
|
|
|
|
* operating systems, but can be as low as 32 on others.)
|
|
|
|
*
|
|
|
|
* VFDs are managed as an LRU pool, with actual OS file descriptors
|
|
|
|
* being opened and closed as needed. Obviously, if a routine is
|
|
|
|
* opened using these interfaces, all subsequent operations must also
|
|
|
|
* be through these interfaces (the File type is not a real file
|
|
|
|
* descriptor).
|
|
|
|
*
|
|
|
|
* For this scheme to work, most (if not all) routines throughout the
|
|
|
|
* server should use these interfaces instead of calling the C library
|
|
|
|
* routines (e.g., open(2) and fopen(3)) themselves. Otherwise, we
|
|
|
|
* may find ourselves short of real file descriptors anyway.
|
|
|
|
*
|
|
|
|
* This file used to contain a bunch of stuff to support RAID levels 0
|
|
|
|
* (jbod), 1 (duplex) and 5 (xor parity). That stuff is all gone
|
|
|
|
* because the parallel query processing code that called it is all
|
|
|
|
* gone. If you really need it you could get it from the original
|
|
|
|
* POSTGRES source.
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2000-10-02 21:42:56 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
1996-11-06 07:52:23 +01:00
|
|
|
#include <fcntl.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "miscadmin.h"
|
2004-07-28 16:23:31 +02:00
|
|
|
#include "access/xact.h"
|
1996-11-08 07:02:30 +01:00
|
|
|
#include "storage/fd.h"
|
2002-05-05 02:03:29 +02:00
|
|
|
#include "storage/ipc.h"
|
2007-01-09 22:31:17 +01:00
|
|
|
#include "utils/guc.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-11 06:12:29 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2004-02-23 21:45:59 +01:00
|
|
|
* We must leave some file descriptors free for system(), the dynamic loader,
|
|
|
|
* and other code that tries to open files without consulting fd.c. This
|
|
|
|
* is the number left free. (While we can be pretty sure we won't get
|
|
|
|
* EMFILE, there's never any guarantee that we won't get ENFILE due to
|
2004-08-29 07:07:03 +02:00
|
|
|
* other processes chewing up FDs. So it's a bad idea to try to open files
|
2004-02-23 21:45:59 +01:00
|
|
|
* without consulting fd.c. Nonetheless we cannot control all code.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-23 21:45:59 +01:00
|
|
|
* Because this is just a fixed setting, we are effectively assuming that
|
|
|
|
* no such code will leave FDs open over the long term; otherwise the slop
|
|
|
|
* is likely to be insufficient. Note in particular that we expect that
|
|
|
|
* loading a shared library does not result in any permanent increase in
|
|
|
|
* the number of open files. (This appears to be true on most if not
|
|
|
|
* all platforms as of Feb 2004.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-23 21:45:59 +01:00
|
|
|
#define NUM_RESERVED_FDS 10
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
2004-02-23 21:45:59 +01:00
|
|
|
* If we have fewer than this many usable FDs after allowing for the reserved
|
|
|
|
* ones, choke.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2004-02-23 21:45:59 +01:00
|
|
|
#define FD_MINFREE 10
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-09-30 20:57:45 +02:00
|
|
|
/*
|
2004-02-23 21:45:59 +01:00
|
|
|
* A number of platforms allow individual processes to open many more files
|
|
|
|
* than they can really support when *many* processes do the same thing.
|
|
|
|
* This GUC parameter lets the DBA limit max_safe_fds to something less than
|
|
|
|
* what the postmaster's initial probe suggests will work.
|
2001-09-30 20:57:45 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
int max_files_per_process = 1000;
|
2001-09-30 20:57:45 +02:00
|
|
|
|
2004-02-23 21:45:59 +01:00
|
|
|
/*
|
|
|
|
* Maximum number of file descriptors to open for either VFD entries or
|
2004-02-24 00:03:10 +01:00
|
|
|
* AllocateFile/AllocateDir operations. This is initialized to a conservative
|
|
|
|
* value, and remains that way indefinitely in bootstrap or standalone-backend
|
|
|
|
* cases. In normal postmaster operation, the postmaster calls
|
|
|
|
* set_max_safe_fds() late in initialization to update the value, and that
|
|
|
|
* value is then inherited by forked subprocesses.
|
2004-02-23 21:45:59 +01:00
|
|
|
*
|
|
|
|
* Note: the value of max_files_per_process is taken into account while
|
|
|
|
* setting this variable, and so need not be tested separately.
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
static int max_safe_fds = 32; /* default if not changed */
|
2004-02-23 21:45:59 +01:00
|
|
|
|
2001-09-30 20:57:45 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* Debugging.... */
|
|
|
|
|
|
|
|
#ifdef FDDEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
#define DO_DB(A) A
|
1996-07-09 08:22:35 +02:00
|
|
|
#else
|
1997-09-07 07:04:48 +02:00
|
|
|
#define DO_DB(A) /* A */
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
#define VFD_CLOSED (-1)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
#define FileIsValid(file) \
|
2000-03-17 03:36:41 +01:00
|
|
|
((file) > 0 && (file) < (int) SizeVfdCache && VfdCache[file].fileName != NULL)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#define FileIsNotOpen(file) (VfdCache[file].fd == VFD_CLOSED)
|
|
|
|
|
2002-02-10 23:56:31 +01:00
|
|
|
#define FileUnknownPos (-1L)
|
2000-06-14 05:19:24 +02:00
|
|
|
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
/* these are the assigned bits in fdstate below: */
|
2003-08-04 02:43:34 +02:00
|
|
|
#define FD_TEMPORARY (1 << 0) /* T = delete when closed */
|
|
|
|
#define FD_XACT_TEMPORARY (1 << 1) /* T = delete at eoXact */
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct vfd
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
signed short fd; /* current FD, or VFD_CLOSED if none */
|
|
|
|
unsigned short fdstate; /* bitflags for VFD's state */
|
2005-10-15 04:49:52 +02:00
|
|
|
SubTransactionId create_subid; /* for TEMPORARY fds, creating subxact */
|
1999-05-09 02:52:08 +02:00
|
|
|
File nextFree; /* link to next free VFD, if in freelist */
|
2001-10-28 07:26:15 +01:00
|
|
|
File lruMoreRecently; /* doubly linked recency-of-use list */
|
1997-09-08 04:41:22 +02:00
|
|
|
File lruLessRecently;
|
1999-05-09 02:52:08 +02:00
|
|
|
long seekPos; /* current logical file position */
|
|
|
|
char *fileName; /* name of file, or NULL for unused VFD */
|
|
|
|
/* NB: fileName is malloc'd, and must be free'd when closing the VFD */
|
2001-04-03 04:31:52 +02:00
|
|
|
int fileFlags; /* open(2) flags for (re)opening the file */
|
1999-05-09 02:52:08 +02:00
|
|
|
int fileMode; /* mode to pass to open(2) */
|
1997-09-08 23:56:23 +02:00
|
|
|
} Vfd;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Virtual File Descriptor array pointer and size. This grows as
|
1999-05-25 18:15:34 +02:00
|
|
|
* needed. 'File' values are indexes into this array.
|
1999-05-09 02:52:08 +02:00
|
|
|
* Note that VfdCache[0] is not a usable VFD, just a list header.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Vfd *VfdCache;
|
|
|
|
static Size SizeVfdCache = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-09 02:52:08 +02:00
|
|
|
* Number of file descriptors known to be in use by VFD entries.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static int nfile = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
2004-07-28 16:23:31 +02:00
|
|
|
* List of stdio FILEs and <dirent.h> DIRs opened with AllocateFile
|
|
|
|
* and AllocateDir.
|
1999-05-09 02:52:08 +02:00
|
|
|
*
|
2004-07-28 16:23:31 +02:00
|
|
|
* Since we don't want to encourage heavy use of AllocateFile or AllocateDir,
|
|
|
|
* it seems OK to put a pretty small maximum limit on the number of
|
|
|
|
* simultaneously allocated descs.
|
1999-05-09 02:52:08 +02:00
|
|
|
*/
|
2004-07-28 16:23:31 +02:00
|
|
|
#define MAX_ALLOCATED_DESCS 32
|
1999-05-09 02:52:08 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
2004-07-28 16:23:31 +02:00
|
|
|
AllocateDescFile,
|
|
|
|
AllocateDescDir
|
|
|
|
} AllocateDescKind;
|
1999-05-09 02:52:08 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
AllocateDescKind kind;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
DIR *dir;
|
|
|
|
} desc;
|
2004-09-16 18:58:44 +02:00
|
|
|
SubTransactionId create_subid;
|
2004-07-28 16:23:31 +02:00
|
|
|
} AllocateDesc;
|
2004-02-24 00:03:10 +01:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
static int numAllocatedDescs = 0;
|
2004-07-28 16:23:31 +02:00
|
|
|
static AllocateDesc allocatedDescs[MAX_ALLOCATED_DESCS];
|
2004-02-24 00:03:10 +01:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/*
|
2003-04-29 05:21:30 +02:00
|
|
|
* Number of temporary files opened during the current session;
|
1999-05-09 02:52:08 +02:00
|
|
|
* this is used in generation of tempfile names.
|
|
|
|
*/
|
|
|
|
static long tempFileCounter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/*--------------------
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Private Routines
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Delete - delete a file from the Lru ring
|
1999-05-09 02:52:08 +02:00
|
|
|
* LruDelete - remove a file from the Lru ring and close its FD
|
1997-09-07 07:04:48 +02:00
|
|
|
* Insert - put a file at the front of the Lru ring
|
1999-05-09 02:52:08 +02:00
|
|
|
* LruInsert - put a file at the front of the Lru ring and open it
|
|
|
|
* ReleaseLruFile - Release an fd by closing the last entry in the Lru ring
|
|
|
|
* AllocateVfd - grab a free (or new) file record (from VfdArray)
|
|
|
|
* FreeVfd - free a file record
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-05-09 02:52:08 +02:00
|
|
|
* The Least Recently Used ring is a doubly linked list that begins and
|
1996-12-27 23:57:51 +01:00
|
|
|
* ends on element zero. Element zero is special -- it doesn't represent
|
1997-09-07 07:04:48 +02:00
|
|
|
* a file and its "fd" field always == VFD_CLOSED. Element zero is just an
|
1996-12-27 23:57:51 +01:00
|
|
|
* anchor that shows us the beginning/end of the ring.
|
1999-05-09 02:52:08 +02:00
|
|
|
* Only VFD elements that are currently really open (have an FD assigned) are
|
|
|
|
* in the Lru ring. Elements that are "virtually" open can be recognized
|
|
|
|
* by having a non-null fileName field.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* example:
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* /--less----\ /---------\
|
|
|
|
* v \ v \
|
|
|
|
* #0 --more---> LeastRecentlyUsed --more-\ \
|
|
|
|
* ^\ | |
|
|
|
|
* \\less--> MostRecentlyUsedFile <---/ |
|
|
|
|
* \more---/ \--less--/
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-05-09 02:52:08 +02:00
|
|
|
*--------------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static void Delete(File file);
|
|
|
|
static void LruDelete(File file);
|
|
|
|
static void Insert(File file);
|
|
|
|
static int LruInsert(File file);
|
2000-08-27 23:48:00 +02:00
|
|
|
static bool ReleaseLruFile(void);
|
1997-09-08 04:41:22 +02:00
|
|
|
static File AllocateVfd(void);
|
|
|
|
static void FreeVfd(File file);
|
|
|
|
|
|
|
|
static int FileAccess(File file);
|
2005-07-04 06:51:52 +02:00
|
|
|
static char *make_database_relative(const char *filename);
|
2003-12-12 19:45:10 +01:00
|
|
|
static void AtProcExit_Files(int code, Datum arg);
|
2003-04-29 05:21:30 +02:00
|
|
|
static void CleanupTempFiles(bool isProcExit);
|
2004-12-29 22:36:09 +01:00
|
|
|
static void RemovePgTempFilesInDir(const char *tmpdirname);
|
2003-04-29 05:21:30 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-12-08 23:21:33 +01:00
|
|
|
/*
|
2005-05-20 16:53:26 +02:00
|
|
|
* pg_fsync --- do fsync with or without writethrough
|
2000-12-08 23:21:33 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_fsync(int fd)
|
2005-05-20 16:53:26 +02:00
|
|
|
{
|
|
|
|
#ifndef HAVE_FSYNC_WRITETHROUGH_ONLY
|
|
|
|
if (sync_method != SYNC_METHOD_FSYNC_WRITETHROUGH)
|
|
|
|
return pg_fsync_no_writethrough(fd);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return pg_fsync_writethrough(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_fsync_no_writethrough --- same as fsync except does nothing if
|
|
|
|
* enableFsync is off
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_fsync_no_writethrough(int fd)
|
2000-12-08 23:21:33 +01:00
|
|
|
{
|
|
|
|
if (enableFsync)
|
|
|
|
return fsync(fd);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-20 16:53:26 +02:00
|
|
|
/*
|
|
|
|
* pg_fsync_writethrough
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_fsync_writethrough(int fd)
|
|
|
|
{
|
|
|
|
if (enableFsync)
|
2006-01-18 00:52:31 +01:00
|
|
|
{
|
2005-05-20 16:53:26 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
return _commit(fd);
|
2006-01-18 00:52:31 +01:00
|
|
|
#elif defined(F_FULLFSYNC)
|
|
|
|
return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
|
2005-05-20 16:53:26 +02:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2006-01-18 00:52:31 +01:00
|
|
|
}
|
2005-05-20 16:53:26 +02:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-18 05:39:42 +01:00
|
|
|
/*
|
|
|
|
* pg_fdatasync --- same as fdatasync except does nothing if enableFsync is off
|
|
|
|
*
|
|
|
|
* Not all platforms have fdatasync; treat as fsync if not available.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_fdatasync(int fd)
|
|
|
|
{
|
|
|
|
if (enableFsync)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FDATASYNC
|
|
|
|
return fdatasync(fd);
|
|
|
|
#else
|
|
|
|
return fsync(fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-08 05:12:16 +02:00
|
|
|
/*
|
|
|
|
* InitFileAccess --- initialize this module during backend startup
|
|
|
|
*
|
|
|
|
* This is called during either normal or standalone backend start.
|
|
|
|
* It is *not* called in the postmaster.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
InitFileAccess(void)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Assert(SizeVfdCache == 0); /* call me only once */
|
2005-08-08 05:12:16 +02:00
|
|
|
|
|
|
|
/* initialize cache header entry */
|
|
|
|
VfdCache = (Vfd *) malloc(sizeof(Vfd));
|
|
|
|
if (VfdCache == NULL)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
|
|
|
|
|
|
|
MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));
|
|
|
|
VfdCache->fd = VFD_CLOSED;
|
|
|
|
|
|
|
|
SizeVfdCache = 1;
|
|
|
|
|
|
|
|
/* register proc-exit hook to ensure temp files are dropped at exit */
|
|
|
|
on_proc_exit(AtProcExit_Files, 0);
|
|
|
|
}
|
|
|
|
|
2004-02-23 21:45:59 +01:00
|
|
|
/*
|
|
|
|
* count_usable_fds --- count how many FDs the system will let us open,
|
|
|
|
* and estimate how many are already open.
|
|
|
|
*
|
2005-08-07 20:47:19 +02:00
|
|
|
* We stop counting if usable_fds reaches max_to_probe. Note: a small
|
|
|
|
* value of max_to_probe might result in an underestimate of already_open;
|
|
|
|
* we must fill in any "gaps" in the set of used FDs before the calculation
|
2005-10-15 04:49:52 +02:00
|
|
|
* of already_open will give the right answer. In practice, max_to_probe
|
2005-08-07 20:47:19 +02:00
|
|
|
* of a couple of dozen should be enough to ensure good results.
|
|
|
|
*
|
2004-02-23 21:45:59 +01:00
|
|
|
* We assume stdin (FD 0) is available for dup'ing
|
|
|
|
*/
|
|
|
|
static void
|
2005-08-07 20:47:19 +02:00
|
|
|
count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
|
2004-02-23 21:45:59 +01:00
|
|
|
{
|
|
|
|
int *fd;
|
|
|
|
int size;
|
|
|
|
int used = 0;
|
|
|
|
int highestfd = 0;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
size = 1024;
|
|
|
|
fd = (int *) palloc(size * sizeof(int));
|
|
|
|
|
2005-08-07 20:47:19 +02:00
|
|
|
/* dup until failure or probe limit reached */
|
2004-02-23 21:45:59 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int thisfd;
|
2004-02-23 21:45:59 +01:00
|
|
|
|
|
|
|
thisfd = dup(0);
|
|
|
|
if (thisfd < 0)
|
|
|
|
{
|
|
|
|
/* Expect EMFILE or ENFILE, else it's fishy */
|
|
|
|
if (errno != EMFILE && errno != ENFILE)
|
|
|
|
elog(WARNING, "dup(0) failed after %d successes: %m", used);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (used >= size)
|
|
|
|
{
|
|
|
|
size *= 2;
|
|
|
|
fd = (int *) repalloc(fd, size * sizeof(int));
|
|
|
|
}
|
|
|
|
fd[used++] = thisfd;
|
|
|
|
|
|
|
|
if (highestfd < thisfd)
|
|
|
|
highestfd = thisfd;
|
2005-08-07 20:47:19 +02:00
|
|
|
|
|
|
|
if (used >= max_to_probe)
|
|
|
|
break;
|
2004-02-23 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* release the files we opened */
|
|
|
|
for (j = 0; j < used; j++)
|
|
|
|
close(fd[j]);
|
|
|
|
|
|
|
|
pfree(fd);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Return results. usable_fds is just the number of successful dups. We
|
|
|
|
* assume that the system limit is highestfd+1 (remember 0 is a legal FD
|
|
|
|
* number) and so already_open is highestfd+1 - usable_fds.
|
2004-02-23 21:45:59 +01:00
|
|
|
*/
|
|
|
|
*usable_fds = used;
|
2004-08-29 07:07:03 +02:00
|
|
|
*already_open = highestfd + 1 - used;
|
2004-02-23 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_max_safe_fds
|
|
|
|
* Determine number of filedescriptors that fd.c is allowed to use
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_max_safe_fds(void)
|
|
|
|
{
|
|
|
|
int usable_fds;
|
|
|
|
int already_open;
|
|
|
|
|
2005-08-07 20:47:19 +02:00
|
|
|
/*----------
|
|
|
|
* We want to set max_safe_fds to
|
|
|
|
* MIN(usable_fds, max_files_per_process - already_open)
|
|
|
|
* less the slop factor for files that are opened without consulting
|
|
|
|
* fd.c. This ensures that we won't exceed either max_files_per_process
|
|
|
|
* or the experimentally-determined EMFILE limit.
|
|
|
|
*----------
|
2004-02-23 21:45:59 +01:00
|
|
|
*/
|
2005-08-07 20:47:19 +02:00
|
|
|
count_usable_fds(max_files_per_process,
|
|
|
|
&usable_fds, &already_open);
|
2004-02-23 21:45:59 +01:00
|
|
|
|
|
|
|
max_safe_fds = Min(usable_fds, max_files_per_process - already_open);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take off the FDs reserved for system() etc.
|
|
|
|
*/
|
|
|
|
max_safe_fds -= NUM_RESERVED_FDS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we still have enough to get by.
|
|
|
|
*/
|
|
|
|
if (max_safe_fds < FD_MINFREE)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
|
|
|
|
errmsg("insufficient file descriptors available to start server process"),
|
|
|
|
errdetail("System allows %d, we need at least %d.",
|
|
|
|
max_safe_fds + NUM_RESERVED_FDS,
|
|
|
|
FD_MINFREE + NUM_RESERVED_FDS)));
|
|
|
|
|
|
|
|
elog(DEBUG2, "max_safe_fds = %d, usable_fds = %d, already_open = %d",
|
|
|
|
max_safe_fds, usable_fds, already_open);
|
|
|
|
}
|
|
|
|
|
2000-06-02 05:58:34 +02:00
|
|
|
/*
|
|
|
|
* BasicOpenFile --- same as open(2) except can free other FDs if needed
|
|
|
|
*
|
|
|
|
* This is exported for use by places that really want a plain kernel FD,
|
|
|
|
* but need to be proof against running out of FDs. Once an FD has been
|
|
|
|
* successfully returned, it is the caller's responsibility to ensure that
|
2003-08-04 02:43:34 +02:00
|
|
|
* it will not be leaked on ereport()! Most users should *not* call this
|
2000-06-02 05:58:34 +02:00
|
|
|
* routine directly, but instead use the VFD abstraction level, which
|
|
|
|
* provides protection against descriptor leaks as well as management of
|
|
|
|
* files that need to be open for more than a short period of time.
|
|
|
|
*
|
|
|
|
* Ideally this should be the *only* direct call of open() in the backend.
|
|
|
|
* In practice, the postmaster calls open() directly, and there are some
|
|
|
|
* direct open() calls done early in backend startup. Those are OK since
|
|
|
|
* this module wouldn't have any open files to close at that point anyway.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int fd;
|
2000-06-02 05:58:34 +02:00
|
|
|
|
|
|
|
tryAgain:
|
|
|
|
fd = open(fileName, fileFlags, fileMode);
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
return fd; /* success! */
|
|
|
|
|
2000-08-27 23:48:00 +02:00
|
|
|
if (errno == EMFILE || errno == ENFILE)
|
2000-06-02 05:58:34 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int save_errno = errno;
|
2000-08-27 23:48:00 +02:00
|
|
|
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("out of file descriptors: %m; release and retry")));
|
2000-06-02 05:58:34 +02:00
|
|
|
errno = 0;
|
2000-08-27 23:48:00 +02:00
|
|
|
if (ReleaseLruFile())
|
|
|
|
goto tryAgain;
|
|
|
|
errno = save_errno;
|
2000-06-02 05:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1; /* failure */
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#if defined(FDDEBUG)
|
1999-05-09 02:52:08 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
static void
|
2000-08-27 23:48:00 +02:00
|
|
|
_dump_lru(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int mru = VfdCache[0].lruLessRecently;
|
|
|
|
Vfd *vfdP = &VfdCache[mru];
|
|
|
|
char buf[2048];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-09-02 08:11:43 +02:00
|
|
|
snprintf(buf, sizeof(buf), "LRU: MOST %d ", mru);
|
1997-09-07 07:04:48 +02:00
|
|
|
while (mru != 0)
|
|
|
|
{
|
|
|
|
mru = vfdP->lruLessRecently;
|
|
|
|
vfdP = &VfdCache[mru];
|
2002-09-02 08:11:43 +02:00
|
|
|
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%d ", mru);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2002-09-02 08:11:43 +02:00
|
|
|
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "LEAST");
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(LOG, buf);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* FDDEBUG */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
Delete(File file)
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Vfd *vfdP;
|
|
|
|
|
|
|
|
Assert(file != 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "Delete %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
DO_DB(_dump_lru());
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
vfdP = &VfdCache[file];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
VfdCache[vfdP->lruLessRecently].lruMoreRecently = vfdP->lruMoreRecently;
|
|
|
|
VfdCache[vfdP->lruMoreRecently].lruLessRecently = vfdP->lruLessRecently;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
DO_DB(_dump_lru());
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
LruDelete(File file)
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Vfd *vfdP;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(file != 0);
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "LruDelete %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
vfdP = &VfdCache[file];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* delete the vfd record from the LRU ring */
|
|
|
|
Delete(file);
|
|
|
|
|
|
|
|
/* save the seek position */
|
1999-05-09 02:52:08 +02:00
|
|
|
vfdP->seekPos = (long) lseek(vfdP->fd, 0L, SEEK_CUR);
|
2002-02-10 23:56:31 +01:00
|
|
|
Assert(vfdP->seekPos != -1L);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* close the file */
|
2002-02-10 23:56:31 +01:00
|
|
|
if (close(vfdP->fd))
|
2004-01-26 23:35:32 +01:00
|
|
|
elog(ERROR, "failed to close \"%s\": %m",
|
2002-02-10 23:56:31 +01:00
|
|
|
vfdP->fileName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
--nfile;
|
1999-05-09 02:52:08 +02:00
|
|
|
vfdP->fd = VFD_CLOSED;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Insert(File file)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Vfd *vfdP;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(file != 0);
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "Insert %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
DO_DB(_dump_lru());
|
|
|
|
|
|
|
|
vfdP = &VfdCache[file];
|
|
|
|
|
|
|
|
vfdP->lruMoreRecently = 0;
|
|
|
|
vfdP->lruLessRecently = VfdCache[0].lruLessRecently;
|
|
|
|
VfdCache[0].lruLessRecently = file;
|
|
|
|
VfdCache[vfdP->lruLessRecently].lruMoreRecently = file;
|
|
|
|
|
|
|
|
DO_DB(_dump_lru());
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/* returns 0 on success, -1 on re-open failure (with errno set) */
|
1996-07-09 08:22:35 +02:00
|
|
|
static int
|
1997-09-07 07:04:48 +02:00
|
|
|
LruInsert(File file)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Vfd *vfdP;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(file != 0);
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "LruInsert %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
|
|
|
vfdP = &VfdCache[file];
|
|
|
|
|
|
|
|
if (FileIsNotOpen(file))
|
|
|
|
{
|
2004-07-28 16:23:31 +02:00
|
|
|
while (nfile + numAllocatedDescs >= max_safe_fds)
|
2000-08-27 23:48:00 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
if (!ReleaseLruFile())
|
2000-08-27 23:48:00 +02:00
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The open could still fail for lack of file descriptors, eg due to
|
|
|
|
* overall system file table being full. So, be prepared to release
|
|
|
|
* another FD if necessary...
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-06-02 05:58:34 +02:00
|
|
|
vfdP->fd = BasicOpenFile(vfdP->fileName, vfdP->fileFlags,
|
|
|
|
vfdP->fileMode);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (vfdP->fd < 0)
|
|
|
|
{
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "RE_OPEN FAILED: %d", errno));
|
1998-09-01 05:29:17 +02:00
|
|
|
return vfdP->fd;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "RE_OPEN SUCCESS"));
|
1997-09-07 07:04:48 +02:00
|
|
|
++nfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* seek to the right position */
|
|
|
|
if (vfdP->seekPos != 0L)
|
|
|
|
{
|
2002-02-10 23:56:31 +01:00
|
|
|
long returnValue;
|
|
|
|
|
|
|
|
returnValue = (long) lseek(vfdP->fd, vfdP->seekPos, SEEK_SET);
|
|
|
|
Assert(returnValue != -1L);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* put it at the head of the Lru ring
|
|
|
|
*/
|
|
|
|
|
|
|
|
Insert(file);
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-08-27 23:48:00 +02:00
|
|
|
static bool
|
|
|
|
ReleaseLruFile(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "ReleaseLruFile. Opened %d", nfile));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-08-27 23:48:00 +02:00
|
|
|
if (nfile > 0)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* There are opened files and so there should be at least one used vfd
|
|
|
|
* in the ring.
|
2000-08-27 23:48:00 +02:00
|
|
|
*/
|
|
|
|
Assert(VfdCache[0].lruMoreRecently != 0);
|
|
|
|
LruDelete(VfdCache[0].lruMoreRecently);
|
|
|
|
return true; /* freed a file */
|
|
|
|
}
|
|
|
|
return false; /* no files available to free */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static File
|
2000-08-27 23:48:00 +02:00
|
|
|
AllocateVfd(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Index i;
|
|
|
|
File file;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-05-30 15:04:59 +02:00
|
|
|
DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
Assert(SizeVfdCache > 0); /* InitFileAccess not called? */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (VfdCache[0].nextFree == 0)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The free list is empty so it is time to increase the size of the
|
|
|
|
* array. We choose to double it each time this happens. However,
|
|
|
|
* there's not much point in starting *real* small.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
Size newCacheSize = SizeVfdCache * 2;
|
2001-04-03 06:07:02 +02:00
|
|
|
Vfd *newVfdCache;
|
1999-05-09 02:52:08 +02:00
|
|
|
|
|
|
|
if (newCacheSize < 32)
|
|
|
|
newCacheSize = 32;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-04-03 06:07:02 +02:00
|
|
|
/*
|
2003-07-25 00:04:15 +02:00
|
|
|
* Be careful not to clobber VfdCache ptr if realloc fails.
|
2001-04-03 06:07:02 +02:00
|
|
|
*/
|
|
|
|
newVfdCache = (Vfd *) realloc(VfdCache, sizeof(Vfd) * newCacheSize);
|
|
|
|
if (newVfdCache == NULL)
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2001-04-03 06:07:02 +02:00
|
|
|
VfdCache = newVfdCache;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-09 02:52:08 +02:00
|
|
|
* Initialize the new entries and link them into the free list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-05-09 02:52:08 +02:00
|
|
|
for (i = SizeVfdCache; i < newCacheSize; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
MemSet((char *) &(VfdCache[i]), 0, sizeof(Vfd));
|
1997-09-07 07:04:48 +02:00
|
|
|
VfdCache[i].nextFree = i + 1;
|
|
|
|
VfdCache[i].fd = VFD_CLOSED;
|
|
|
|
}
|
1999-05-09 02:52:08 +02:00
|
|
|
VfdCache[newCacheSize - 1].nextFree = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
VfdCache[0].nextFree = SizeVfdCache;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record the new size
|
|
|
|
*/
|
1999-05-09 02:52:08 +02:00
|
|
|
SizeVfdCache = newCacheSize;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-05-09 02:52:08 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
file = VfdCache[0].nextFree;
|
|
|
|
|
|
|
|
VfdCache[0].nextFree = VfdCache[file].nextFree;
|
|
|
|
|
|
|
|
return file;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
FreeVfd(File file)
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Vfd *vfdP = &VfdCache[file];
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FreeVfd: %d (%s)",
|
1999-05-09 02:52:08 +02:00
|
|
|
file, vfdP->fileName ? vfdP->fileName : ""));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
if (vfdP->fileName != NULL)
|
|
|
|
{
|
|
|
|
free(vfdP->fileName);
|
|
|
|
vfdP->fileName = NULL;
|
|
|
|
}
|
2001-04-03 06:07:02 +02:00
|
|
|
vfdP->fdstate = 0x0;
|
1999-05-09 02:52:08 +02:00
|
|
|
|
|
|
|
vfdP->nextFree = VfdCache[0].nextFree;
|
1997-09-07 07:04:48 +02:00
|
|
|
VfdCache[0].nextFree = file;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
/*
|
|
|
|
* make_database_relative()
|
|
|
|
* Prepend DatabasePath to the given file name.
|
1999-10-13 17:02:32 +02:00
|
|
|
*
|
2001-06-11 06:12:29 +02:00
|
|
|
* Result is a palloc'd string.
|
1997-11-07 07:38:51 +01:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *
|
2005-07-04 06:51:52 +02:00
|
|
|
make_database_relative(const char *filename)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *buf;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
Assert(!is_absolute_path(filename));
|
|
|
|
buf = (char *) palloc(strlen(DatabasePath) + strlen(filename) + 2);
|
|
|
|
sprintf(buf, "%s/%s", DatabasePath, filename);
|
1998-09-01 05:29:17 +02:00
|
|
|
return buf;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/* returns 0 on success, -1 on re-open failure (with errno set) */
|
1996-07-09 08:22:35 +02:00
|
|
|
static int
|
|
|
|
FileAccess(File file)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int returnValue;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileAccess %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Is the file open? If not, open it and put it at the head of the LRU
|
|
|
|
* ring (possibly closing the least recently used file to get an FD).
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (FileIsNotOpen(file))
|
|
|
|
{
|
|
|
|
returnValue = LruInsert(file);
|
|
|
|
if (returnValue != 0)
|
|
|
|
return returnValue;
|
|
|
|
}
|
1999-05-09 02:52:08 +02:00
|
|
|
else if (VfdCache[0].lruLessRecently != file)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We now know that the file is open and that it is not the last one
|
|
|
|
* accessed, so we need to move it to the head of the Lru ring.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
Delete(file);
|
|
|
|
Insert(file);
|
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Called when we get a shared invalidation message on some relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
#ifdef NOT_USED
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
|
|
|
FileInvalidate(File file)
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!FileIsNotOpen(file))
|
|
|
|
LruDelete(file);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-08-19 23:40:56 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
/*
|
|
|
|
* open a file in an arbitrary directory
|
|
|
|
*
|
|
|
|
* NB: if the passed pathname is relative (which it usually is),
|
|
|
|
* it will be interpreted relative to the process' working directory
|
|
|
|
* (which should always be $PGDATA when this code is running).
|
|
|
|
*/
|
|
|
|
File
|
|
|
|
PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-07-25 00:04:15 +02:00
|
|
|
char *fnamecopy;
|
1997-09-08 04:41:22 +02:00
|
|
|
File file;
|
|
|
|
Vfd *vfdP;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
DO_DB(elog(LOG, "PathNameOpenFile: %s %x %o",
|
1997-09-07 07:04:48 +02:00
|
|
|
fileName, fileFlags, fileMode));
|
|
|
|
|
2003-07-25 00:04:15 +02:00
|
|
|
/*
|
|
|
|
* We need a malloc'd copy of the file name; fail cleanly if no room.
|
|
|
|
*/
|
|
|
|
fnamecopy = strdup(fileName);
|
|
|
|
if (fnamecopy == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
file = AllocateVfd();
|
|
|
|
vfdP = &VfdCache[file];
|
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
while (nfile + numAllocatedDescs >= max_safe_fds)
|
2000-08-27 23:48:00 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
if (!ReleaseLruFile())
|
2000-08-27 23:48:00 +02:00
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-02 05:58:34 +02:00
|
|
|
vfdP->fd = BasicOpenFile(fileName, fileFlags, fileMode);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (vfdP->fd < 0)
|
|
|
|
{
|
|
|
|
FreeVfd(file);
|
2003-07-25 00:04:15 +02:00
|
|
|
free(fnamecopy);
|
1997-09-07 07:04:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
++nfile;
|
2005-07-04 06:51:52 +02:00
|
|
|
DO_DB(elog(LOG, "PathNameOpenFile: success %d",
|
1997-09-07 07:04:48 +02:00
|
|
|
vfdP->fd));
|
|
|
|
|
|
|
|
Insert(file);
|
|
|
|
|
2003-07-25 00:04:15 +02:00
|
|
|
vfdP->fileName = fnamecopy;
|
2001-04-03 04:31:52 +02:00
|
|
|
/* Saved flags are adjusted to be OK for re-opening file */
|
|
|
|
vfdP->fileFlags = fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL);
|
1997-09-07 07:04:48 +02:00
|
|
|
vfdP->fileMode = fileMode;
|
|
|
|
vfdP->seekPos = 0;
|
2002-08-06 04:36:35 +02:00
|
|
|
vfdP->fdstate = 0x0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return file;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-07-04 06:51:52 +02:00
|
|
|
* open a file in the database directory ($PGDATA/base/DIROID/)
|
|
|
|
*
|
|
|
|
* The passed name MUST be a relative path. Effectively, this
|
|
|
|
* prepends DatabasePath to it and then acts like PathNameOpenFile.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
File
|
|
|
|
FileNameOpenFile(FileName fileName, int fileFlags, int fileMode)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
File fd;
|
|
|
|
char *fname;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
fname = make_database_relative(fileName);
|
|
|
|
fd = PathNameOpenFile(fname, fileFlags, fileMode);
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(fname);
|
1998-09-01 05:29:17 +02:00
|
|
|
return fd;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/*
|
|
|
|
* Open a temporary file that will disappear when we close it.
|
|
|
|
*
|
|
|
|
* This routine takes care of generating an appropriate tempfile name.
|
|
|
|
* There's no need to pass in fileFlags or fileMode either, since only
|
|
|
|
* one setting makes any sense for a temp file.
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
*
|
2003-04-29 05:21:30 +02:00
|
|
|
* interXact: if true, don't close the file at end-of-transaction. In
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
* most cases, you don't want temporary files to outlive the transaction
|
|
|
|
* that created them, so this should be false -- but if you need
|
|
|
|
* "somewhat" temporary storage, this might be useful. In either case,
|
2003-04-29 05:21:30 +02:00
|
|
|
* the file is removed when the File is explicitly closed.
|
1999-05-09 02:52:08 +02:00
|
|
|
*/
|
|
|
|
File
|
2003-04-29 05:21:30 +02:00
|
|
|
OpenTemporaryFile(bool interXact)
|
1999-05-09 02:52:08 +02:00
|
|
|
{
|
2003-04-29 05:21:30 +02:00
|
|
|
char tempfilepath[MAXPGPATH];
|
1999-05-25 18:15:34 +02:00
|
|
|
File file;
|
1999-05-09 02:52:08 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2003-04-29 05:21:30 +02:00
|
|
|
* Generate a tempfile name that should be unique within the current
|
|
|
|
* database instance.
|
1999-05-25 18:15:34 +02:00
|
|
|
*/
|
2001-06-11 06:12:29 +02:00
|
|
|
snprintf(tempfilepath, sizeof(tempfilepath),
|
|
|
|
"%s/%s%d.%ld", PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
|
|
|
|
MyProcPid, tempFileCounter++);
|
1999-05-09 02:52:08 +02:00
|
|
|
|
2001-06-11 06:12:29 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Open the file. Note: we don't use O_EXCL, in case there is an orphaned
|
|
|
|
* temp file that can be reused.
|
2001-06-11 06:12:29 +02:00
|
|
|
*/
|
|
|
|
file = FileNameOpenFile(tempfilepath,
|
|
|
|
O_RDWR | O_CREAT | O_TRUNC | PG_BINARY,
|
|
|
|
0600);
|
1999-05-09 02:52:08 +02:00
|
|
|
if (file <= 0)
|
2001-06-11 06:12:29 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
char *dirpath;
|
2001-06-11 06:12:29 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We might need to create the pg_tempfiles subdirectory, if no one
|
|
|
|
* has yet done so.
|
2001-06-11 06:12:29 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Don't check for error from mkdir; it could fail if someone else
|
|
|
|
* just did the same thing. If it doesn't work then we'll bomb out on
|
|
|
|
* the second create attempt, instead.
|
2001-06-11 06:12:29 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
dirpath = make_database_relative(PG_TEMP_FILES_DIR);
|
2001-06-11 06:12:29 +02:00
|
|
|
mkdir(dirpath, S_IRWXU);
|
|
|
|
pfree(dirpath);
|
|
|
|
|
|
|
|
file = FileNameOpenFile(tempfilepath,
|
|
|
|
O_RDWR | O_CREAT | O_TRUNC | PG_BINARY,
|
|
|
|
0600);
|
|
|
|
if (file <= 0)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "could not create temporary file \"%s\": %m",
|
2003-04-29 05:21:30 +02:00
|
|
|
tempfilepath);
|
2001-06-11 06:12:29 +02:00
|
|
|
}
|
1999-05-09 02:52:08 +02:00
|
|
|
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
/* Mark it for deletion at close */
|
1999-05-09 02:52:08 +02:00
|
|
|
VfdCache[file].fdstate |= FD_TEMPORARY;
|
|
|
|
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
/* Mark it for deletion at EOXact */
|
2003-04-29 05:21:30 +02:00
|
|
|
if (!interXact)
|
2004-07-28 16:23:31 +02:00
|
|
|
{
|
2003-04-29 05:21:30 +02:00
|
|
|
VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
|
2004-09-16 18:58:44 +02:00
|
|
|
VfdCache[file].create_subid = GetCurrentSubTransactionId();
|
2004-07-28 16:23:31 +02:00
|
|
|
}
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close a file when done with it
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
|
|
|
FileClose(File file)
|
|
|
|
{
|
2007-01-09 22:31:17 +01:00
|
|
|
Vfd *vfdP;
|
|
|
|
struct stat filestats;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileClose: %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
2002-02-10 23:56:31 +01:00
|
|
|
vfdP = &VfdCache[file];
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!FileIsNotOpen(file))
|
|
|
|
{
|
|
|
|
/* remove the file from the lru ring */
|
|
|
|
Delete(file);
|
|
|
|
|
|
|
|
/* close the file */
|
2002-02-10 23:56:31 +01:00
|
|
|
if (close(vfdP->fd))
|
2004-01-26 23:35:32 +01:00
|
|
|
elog(ERROR, "failed to close \"%s\": %m",
|
2002-02-10 23:56:31 +01:00
|
|
|
vfdP->fileName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
--nfile;
|
2002-02-10 23:56:31 +01:00
|
|
|
vfdP->fd = VFD_CLOSED;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-09 02:52:08 +02:00
|
|
|
* Delete the file if it was temporary
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-02-10 23:56:31 +01:00
|
|
|
if (vfdP->fdstate & FD_TEMPORARY)
|
2001-01-12 22:54:01 +01:00
|
|
|
{
|
|
|
|
/* reset flag so that die() interrupt won't cause problems */
|
2002-02-10 23:56:31 +01:00
|
|
|
vfdP->fdstate &= ~FD_TEMPORARY;
|
2007-01-09 22:31:17 +01:00
|
|
|
if (log_temp_files >= 0)
|
|
|
|
{
|
|
|
|
if (stat(vfdP->fileName, &filestats) == 0)
|
|
|
|
{
|
|
|
|
if (filestats.st_size >= log_temp_files)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("temp file: path \"%s\" size %lu",
|
|
|
|
vfdP->fileName, (unsigned long)filestats.st_size)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(LOG, "Could not stat \"%s\": %m", vfdP->fileName);
|
|
|
|
}
|
2002-02-10 23:56:31 +01:00
|
|
|
if (unlink(vfdP->fileName))
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(LOG, "failed to unlink \"%s\": %m",
|
2002-02-10 23:56:31 +01:00
|
|
|
vfdP->fileName);
|
2001-01-12 22:54:01 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-09 02:52:08 +02:00
|
|
|
* Return the Vfd slot to the free list
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-05-09 02:52:08 +02:00
|
|
|
FreeVfd(file);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/*
|
|
|
|
* close a file and forcibly delete the underlying Unix file
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
|
|
|
FileUnlink(File file)
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileUnlink: %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/* force FileClose to delete it */
|
|
|
|
VfdCache[file].fdstate |= FD_TEMPORARY;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
FileClose(file);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FileRead(File file, char *buffer, int amount)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int returnCode;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileRead: %d (%s) %ld %d %p",
|
2001-02-17 02:00:04 +01:00
|
|
|
file, VfdCache[file].fileName,
|
|
|
|
VfdCache[file].seekPos, amount, buffer));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
returnCode = FileAccess(file);
|
|
|
|
if (returnCode < 0)
|
|
|
|
return returnCode;
|
|
|
|
|
2005-12-01 21:24:18 +01:00
|
|
|
retry:
|
1997-09-07 07:04:48 +02:00
|
|
|
returnCode = read(VfdCache[file].fd, buffer, amount);
|
2005-12-01 21:24:18 +01:00
|
|
|
|
|
|
|
if (returnCode >= 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
VfdCache[file].seekPos += returnCode;
|
2000-06-14 05:19:24 +02:00
|
|
|
else
|
2005-12-01 21:24:18 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Windows may run out of kernel buffers and return "Insufficient
|
|
|
|
* system resources" error. Wait a bit and retry to solve it.
|
|
|
|
*
|
|
|
|
* It is rumored that EINTR is also possible on some Unix filesystems,
|
|
|
|
* in which case immediate retry is indicated.
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
2006-10-04 02:30:14 +02:00
|
|
|
DWORD error = GetLastError();
|
2005-12-01 21:24:18 +01:00
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case ERROR_NO_SYSTEM_RESOURCES:
|
|
|
|
pg_usleep(1000L);
|
|
|
|
errno = EINTR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_dosmaperr(error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* OK to retry if interrupted */
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
/* Trouble, so assume we don't know the file position anymore */
|
2000-06-14 05:19:24 +02:00
|
|
|
VfdCache[file].seekPos = FileUnknownPos;
|
2005-12-01 21:24:18 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return returnCode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FileWrite(File file, char *buffer, int amount)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int returnCode;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileWrite: %d (%s) %ld %d %p",
|
2001-02-17 02:00:04 +01:00
|
|
|
file, VfdCache[file].fileName,
|
|
|
|
VfdCache[file].seekPos, amount, buffer));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
returnCode = FileAccess(file);
|
|
|
|
if (returnCode < 0)
|
|
|
|
return returnCode;
|
2001-06-06 19:07:46 +02:00
|
|
|
|
2005-12-01 21:24:18 +01:00
|
|
|
retry:
|
2001-06-06 19:07:46 +02:00
|
|
|
errno = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
returnCode = write(VfdCache[file].fd, buffer, amount);
|
2001-06-06 19:07:46 +02:00
|
|
|
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
if (returnCode != amount && errno == 0)
|
|
|
|
errno = ENOSPC;
|
|
|
|
|
2005-12-01 21:24:18 +01:00
|
|
|
if (returnCode >= 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
VfdCache[file].seekPos += returnCode;
|
2000-07-05 23:10:05 +02:00
|
|
|
else
|
2005-12-01 21:24:18 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* See comments in FileRead()
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
2006-10-04 02:30:14 +02:00
|
|
|
DWORD error = GetLastError();
|
2005-12-01 21:24:18 +01:00
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case ERROR_NO_SYSTEM_RESOURCES:
|
|
|
|
pg_usleep(1000L);
|
|
|
|
errno = EINTR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_dosmaperr(error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* OK to retry if interrupted */
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
/* Trouble, so assume we don't know the file position anymore */
|
2000-06-14 05:19:24 +02:00
|
|
|
VfdCache[file].seekPos = FileUnknownPos;
|
2005-12-01 21:24:18 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return returnCode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
int
|
|
|
|
FileSync(File file)
|
|
|
|
{
|
|
|
|
int returnCode;
|
|
|
|
|
|
|
|
Assert(FileIsValid(file));
|
|
|
|
|
|
|
|
DO_DB(elog(LOG, "FileSync: %d (%s)",
|
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
|
|
|
returnCode = FileAccess(file);
|
|
|
|
if (returnCode < 0)
|
|
|
|
return returnCode;
|
|
|
|
|
|
|
|
return pg_fsync(VfdCache[file].fd);
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
long
|
|
|
|
FileSeek(File file, long offset, int whence)
|
|
|
|
{
|
2004-05-31 05:48:10 +02:00
|
|
|
int returnCode;
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileSeek: %d (%s) %ld %ld %d",
|
2001-02-17 02:00:04 +01:00
|
|
|
file, VfdCache[file].fileName,
|
|
|
|
VfdCache[file].seekPos, offset, whence));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (FileIsNotOpen(file))
|
|
|
|
{
|
|
|
|
switch (whence)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case SEEK_SET:
|
2000-07-05 23:10:05 +02:00
|
|
|
if (offset < 0)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid seek offset: %ld", offset);
|
1997-09-08 04:41:22 +02:00
|
|
|
VfdCache[file].seekPos = offset;
|
1999-05-09 02:52:08 +02:00
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
case SEEK_CUR:
|
1999-05-09 02:52:08 +02:00
|
|
|
VfdCache[file].seekPos += offset;
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
case SEEK_END:
|
2004-05-31 05:48:10 +02:00
|
|
|
returnCode = FileAccess(file);
|
|
|
|
if (returnCode < 0)
|
|
|
|
return returnCode;
|
|
|
|
VfdCache[file].seekPos = lseek(VfdCache[file].fd,
|
|
|
|
offset, whence);
|
1999-05-09 02:52:08 +02:00
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid whence: %d", whence);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
2000-07-05 23:10:05 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-07-05 23:10:05 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (whence)
|
|
|
|
{
|
2000-06-14 05:19:24 +02:00
|
|
|
case SEEK_SET:
|
|
|
|
if (offset < 0)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid seek offset: %ld", offset);
|
2000-06-14 05:19:24 +02:00
|
|
|
if (VfdCache[file].seekPos != offset)
|
2004-05-31 05:48:10 +02:00
|
|
|
VfdCache[file].seekPos = lseek(VfdCache[file].fd,
|
|
|
|
offset, whence);
|
2000-06-14 05:19:24 +02:00
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2000-07-05 23:10:05 +02:00
|
|
|
if (offset != 0 || VfdCache[file].seekPos == FileUnknownPos)
|
2004-05-31 05:48:10 +02:00
|
|
|
VfdCache[file].seekPos = lseek(VfdCache[file].fd,
|
|
|
|
offset, whence);
|
2000-06-14 05:19:24 +02:00
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2004-05-31 05:48:10 +02:00
|
|
|
VfdCache[file].seekPos = lseek(VfdCache[file].fd,
|
|
|
|
offset, whence);
|
2000-06-14 05:19:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid whence: %d", whence);
|
2000-06-14 05:19:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2000-07-05 23:10:05 +02:00
|
|
|
}
|
1999-05-09 02:52:08 +02:00
|
|
|
return VfdCache[file].seekPos;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX not actually used but here for completeness
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
#ifdef NOT_USED
|
1996-07-09 08:22:35 +02:00
|
|
|
long
|
|
|
|
FileTell(File file)
|
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileTell %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
return VfdCache[file].seekPos;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-08-19 23:40:56 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
int
|
1999-10-13 17:02:32 +02:00
|
|
|
FileTruncate(File file, long offset)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int returnCode;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
Assert(FileIsValid(file));
|
|
|
|
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
DO_DB(elog(LOG, "FileTruncate %d (%s)",
|
1997-09-07 07:04:48 +02:00
|
|
|
file, VfdCache[file].fileName));
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
returnCode = FileAccess(file);
|
|
|
|
if (returnCode < 0)
|
|
|
|
return returnCode;
|
|
|
|
|
1999-10-13 17:02:32 +02:00
|
|
|
returnCode = ftruncate(VfdCache[file].fd, (size_t) offset);
|
1998-09-01 05:29:17 +02:00
|
|
|
return returnCode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-04-09 06:43:20 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1999-05-09 02:52:08 +02:00
|
|
|
* Routines that want to use stdio (ie, FILE*) should use AllocateFile
|
|
|
|
* rather than plain fopen(). This lets fd.c deal with freeing FDs if
|
1999-05-25 18:15:34 +02:00
|
|
|
* necessary to open the file. When done, call FreeFile rather than fclose.
|
1999-05-09 02:52:08 +02:00
|
|
|
*
|
|
|
|
* Note that files that will be open for any significant length of time
|
|
|
|
* should NOT be handled this way, since they cannot share kernel file
|
|
|
|
* descriptors with other files; there is grave risk of running out of FDs
|
|
|
|
* if anyone locks down too many FDs. Most callers of this routine are
|
|
|
|
* simply reading a config file that they will read and close immediately.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-05-09 02:52:08 +02:00
|
|
|
* fd.c will automatically close all files opened with AllocateFile at
|
|
|
|
* transaction commit or abort; this prevents FD leakage if a routine
|
2003-07-25 00:04:15 +02:00
|
|
|
* that calls AllocateFile is terminated prematurely by ereport(ERROR).
|
2000-06-02 05:58:34 +02:00
|
|
|
*
|
|
|
|
* Ideally this should be the *only* direct call of fopen() in the backend.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
FILE *
|
2006-03-04 22:32:47 +01:00
|
|
|
AllocateFile(const char *name, const char *mode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
FILE *file;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
DO_DB(elog(LOG, "AllocateFile: Allocated %d (%s)",
|
|
|
|
numAllocatedDescs, name));
|
1999-05-09 02:52:08 +02:00
|
|
|
|
2004-02-23 21:45:59 +01:00
|
|
|
/*
|
2004-07-28 16:23:31 +02:00
|
|
|
* The test against MAX_ALLOCATED_DESCS prevents us from overflowing
|
2005-10-15 04:49:52 +02:00
|
|
|
* allocatedFiles[]; the test against max_safe_fds prevents AllocateFile
|
|
|
|
* from hogging every one of the available FDs, which'd lead to infinite
|
|
|
|
* looping.
|
2004-02-23 21:45:59 +01:00
|
|
|
*/
|
2004-07-28 16:23:31 +02:00
|
|
|
if (numAllocatedDescs >= MAX_ALLOCATED_DESCS ||
|
|
|
|
numAllocatedDescs >= max_safe_fds - 1)
|
2004-02-24 00:03:10 +01:00
|
|
|
elog(ERROR, "too many private files demanded");
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-08-18 04:15:04 +02:00
|
|
|
TryAgain:
|
2000-08-27 23:48:00 +02:00
|
|
|
if ((file = fopen(name, mode)) != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-07-28 16:23:31 +02:00
|
|
|
AllocateDesc *desc = &allocatedDescs[numAllocatedDescs];
|
|
|
|
|
|
|
|
desc->kind = AllocateDescFile;
|
|
|
|
desc->desc.file = file;
|
2004-09-16 18:58:44 +02:00
|
|
|
desc->create_subid = GetCurrentSubTransactionId();
|
2004-07-28 16:23:31 +02:00
|
|
|
numAllocatedDescs++;
|
|
|
|
return desc->desc.file;
|
2000-08-27 23:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EMFILE || errno == ENFILE)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int save_errno = errno;
|
2000-08-27 23:48:00 +02:00
|
|
|
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("out of file descriptors: %m; release and retry")));
|
2000-08-27 23:48:00 +02:00
|
|
|
errno = 0;
|
|
|
|
if (ReleaseLruFile())
|
1997-09-07 07:04:48 +02:00
|
|
|
goto TryAgain;
|
2000-08-27 23:48:00 +02:00
|
|
|
errno = save_errno;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-08-27 23:48:00 +02:00
|
|
|
|
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
/*
|
|
|
|
* Free an AllocateDesc of either type.
|
|
|
|
*
|
|
|
|
* The argument *must* point into the allocatedDescs[] array.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
FreeDesc(AllocateDesc *desc)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int result;
|
2004-07-28 16:23:31 +02:00
|
|
|
|
|
|
|
/* Close the underlying object */
|
|
|
|
switch (desc->kind)
|
|
|
|
{
|
|
|
|
case AllocateDescFile:
|
|
|
|
result = fclose(desc->desc.file);
|
|
|
|
break;
|
|
|
|
case AllocateDescDir:
|
|
|
|
result = closedir(desc->desc.dir);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "AllocateDesc kind not recognized");
|
|
|
|
result = 0; /* keep compiler quiet */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compact storage in the allocatedDescs array */
|
|
|
|
numAllocatedDescs--;
|
|
|
|
*desc = allocatedDescs[numAllocatedDescs];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-01-26 23:35:32 +01:00
|
|
|
/*
|
|
|
|
* Close a file returned by AllocateFile.
|
|
|
|
*
|
|
|
|
* Note we do not check fclose's return value --- it is up to the caller
|
|
|
|
* to handle close errors.
|
|
|
|
*/
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
FreeFile(FILE *file)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int i;
|
1999-05-09 02:52:08 +02:00
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
DO_DB(elog(LOG, "FreeFile: Allocated %d", numAllocatedDescs));
|
1999-05-09 02:52:08 +02:00
|
|
|
|
|
|
|
/* Remove file from list of allocated files, if it's present */
|
2004-07-28 16:23:31 +02:00
|
|
|
for (i = numAllocatedDescs; --i >= 0;)
|
1999-05-09 02:52:08 +02:00
|
|
|
{
|
2004-07-28 16:23:31 +02:00
|
|
|
AllocateDesc *desc = &allocatedDescs[i];
|
|
|
|
|
|
|
|
if (desc->kind == AllocateDescFile && desc->desc.file == file)
|
|
|
|
return FreeDesc(desc);
|
1999-05-09 02:52:08 +02:00
|
|
|
}
|
2004-07-28 16:23:31 +02:00
|
|
|
|
|
|
|
/* Only get here if someone passes us a file not in allocatedDescs */
|
|
|
|
elog(WARNING, "file passed to FreeFile was not obtained from AllocateFile");
|
1997-05-22 18:51:19 +02:00
|
|
|
|
2004-01-26 23:35:32 +01:00
|
|
|
return fclose(file);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:03:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines that want to use <dirent.h> (ie, DIR*) should use AllocateDir
|
|
|
|
* rather than plain opendir(). This lets fd.c deal with freeing FDs if
|
|
|
|
* necessary to open the directory, and with closing it after an elog.
|
|
|
|
* When done, call FreeDir rather than closedir.
|
|
|
|
*
|
|
|
|
* Ideally this should be the *only* direct call of opendir() in the backend.
|
|
|
|
*/
|
|
|
|
DIR *
|
|
|
|
AllocateDir(const char *dirname)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
DIR *dir;
|
2004-02-24 00:03:10 +01:00
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
DO_DB(elog(LOG, "AllocateDir: Allocated %d (%s)",
|
|
|
|
numAllocatedDescs, dirname));
|
2004-02-24 00:03:10 +01:00
|
|
|
|
|
|
|
/*
|
2004-07-28 16:23:31 +02:00
|
|
|
* The test against MAX_ALLOCATED_DESCS prevents us from overflowing
|
2005-10-15 04:49:52 +02:00
|
|
|
* allocatedDescs[]; the test against max_safe_fds prevents AllocateDir
|
|
|
|
* from hogging every one of the available FDs, which'd lead to infinite
|
|
|
|
* looping.
|
2004-02-24 00:03:10 +01:00
|
|
|
*/
|
2004-07-28 16:23:31 +02:00
|
|
|
if (numAllocatedDescs >= MAX_ALLOCATED_DESCS ||
|
|
|
|
numAllocatedDescs >= max_safe_fds - 1)
|
2004-02-24 00:03:10 +01:00
|
|
|
elog(ERROR, "too many private dirs demanded");
|
|
|
|
|
|
|
|
TryAgain:
|
|
|
|
if ((dir = opendir(dirname)) != NULL)
|
|
|
|
{
|
2004-07-28 16:23:31 +02:00
|
|
|
AllocateDesc *desc = &allocatedDescs[numAllocatedDescs];
|
|
|
|
|
|
|
|
desc->kind = AllocateDescDir;
|
|
|
|
desc->desc.dir = dir;
|
2004-09-16 18:58:44 +02:00
|
|
|
desc->create_subid = GetCurrentSubTransactionId();
|
2004-07-28 16:23:31 +02:00
|
|
|
numAllocatedDescs++;
|
|
|
|
return desc->desc.dir;
|
2004-02-24 00:03:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EMFILE || errno == ENFILE)
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
ereport(LOG,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("out of file descriptors: %m; release and retry")));
|
2004-02-24 00:03:10 +01:00
|
|
|
errno = 0;
|
|
|
|
if (ReleaseLruFile())
|
|
|
|
goto TryAgain;
|
|
|
|
errno = save_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-19 23:34:03 +02:00
|
|
|
/*
|
|
|
|
* Read a directory opened with AllocateDir, ereport'ing any error.
|
|
|
|
*
|
|
|
|
* This is easier to use than raw readdir() since it takes care of some
|
2005-10-15 04:49:52 +02:00
|
|
|
* otherwise rather tedious and error-prone manipulation of errno. Also,
|
2005-06-19 23:34:03 +02:00
|
|
|
* if you are happy with a generic error message for AllocateDir failure,
|
|
|
|
* you can just do
|
|
|
|
*
|
|
|
|
* dir = AllocateDir(path);
|
|
|
|
* while ((dirent = ReadDir(dir, path)) != NULL)
|
|
|
|
* process dirent;
|
2005-12-08 16:38:29 +01:00
|
|
|
* FreeDir(dir);
|
2005-06-19 23:34:03 +02:00
|
|
|
*
|
|
|
|
* since a NULL dir parameter is taken as indicating AllocateDir failed.
|
|
|
|
* (Make sure errno hasn't been changed since AllocateDir if you use this
|
|
|
|
* shortcut.)
|
|
|
|
*
|
|
|
|
* The pathname passed to AllocateDir must be passed to this routine too,
|
|
|
|
* but it is only used for error reporting.
|
|
|
|
*/
|
|
|
|
struct dirent *
|
|
|
|
ReadDir(DIR *dir, const char *dirname)
|
|
|
|
{
|
|
|
|
struct dirent *dent;
|
|
|
|
|
|
|
|
/* Give a generic message for AllocateDir failure, if caller didn't */
|
|
|
|
if (dir == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open directory \"%s\": %m",
|
|
|
|
dirname)));
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if ((dent = readdir(dir)) != NULL)
|
|
|
|
return dent;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-06-19 23:34:03 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
|
|
|
* released version
|
2005-06-19 23:34:03 +02:00
|
|
|
*/
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
|
|
|
errno = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (errno)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read directory \"%s\": %m",
|
|
|
|
dirname)));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-02-24 00:03:10 +01:00
|
|
|
/*
|
|
|
|
* Close a directory opened with AllocateDir.
|
|
|
|
*
|
|
|
|
* Note we do not check closedir's return value --- it is up to the caller
|
|
|
|
* to handle close errors.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
FreeDir(DIR *dir)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
DO_DB(elog(LOG, "FreeDir: Allocated %d", numAllocatedDescs));
|
2004-02-24 00:03:10 +01:00
|
|
|
|
|
|
|
/* Remove dir from list of allocated dirs, if it's present */
|
2004-07-28 16:23:31 +02:00
|
|
|
for (i = numAllocatedDescs; --i >= 0;)
|
2004-02-24 00:03:10 +01:00
|
|
|
{
|
2004-07-28 16:23:31 +02:00
|
|
|
AllocateDesc *desc = &allocatedDescs[i];
|
|
|
|
|
|
|
|
if (desc->kind == AllocateDescDir && desc->desc.dir == dir)
|
|
|
|
return FreeDesc(desc);
|
2004-02-24 00:03:10 +01:00
|
|
|
}
|
2004-07-28 16:23:31 +02:00
|
|
|
|
|
|
|
/* Only get here if someone passes us a dir not in allocatedDescs */
|
|
|
|
elog(WARNING, "dir passed to FreeDir was not obtained from AllocateDir");
|
2004-02-24 00:03:10 +01:00
|
|
|
|
|
|
|
return closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/*
|
|
|
|
* closeAllVfds
|
|
|
|
*
|
|
|
|
* Force all VFDs into the physically-closed state, so that the fewest
|
|
|
|
* possible number of kernel file descriptors are in use. There is no
|
|
|
|
* change in the logical state of the VFDs.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
2000-08-27 23:48:00 +02:00
|
|
|
closeAllVfds(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-09 02:52:08 +02:00
|
|
|
Index i;
|
|
|
|
|
|
|
|
if (SizeVfdCache > 0)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
|
1999-05-09 02:52:08 +02:00
|
|
|
for (i = 1; i < SizeVfdCache; i++)
|
|
|
|
{
|
|
|
|
if (!FileIsNotOpen(i))
|
|
|
|
LruDelete(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
/*
|
|
|
|
* AtEOSubXact_Files
|
|
|
|
*
|
|
|
|
* Take care of subtransaction commit/abort. At abort, we close temp files
|
|
|
|
* that the subtransaction may have opened. At commit, we reassign the
|
2004-09-16 18:58:44 +02:00
|
|
|
* files that were opened to the parent subtransaction.
|
2004-07-28 16:23:31 +02:00
|
|
|
*/
|
|
|
|
void
|
2004-09-16 18:58:44 +02:00
|
|
|
AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
|
|
|
|
SubTransactionId parentSubid)
|
2004-07-28 16:23:31 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
Index i;
|
2004-07-28 16:23:31 +02:00
|
|
|
|
|
|
|
if (SizeVfdCache > 0)
|
|
|
|
{
|
|
|
|
Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
|
|
|
|
for (i = 1; i < SizeVfdCache; i++)
|
|
|
|
{
|
|
|
|
unsigned short fdstate = VfdCache[i].fdstate;
|
|
|
|
|
|
|
|
if ((fdstate & FD_XACT_TEMPORARY) &&
|
2004-09-16 18:58:44 +02:00
|
|
|
VfdCache[i].create_subid == mySubid)
|
2004-07-28 16:23:31 +02:00
|
|
|
{
|
|
|
|
if (isCommit)
|
2004-09-16 18:58:44 +02:00
|
|
|
VfdCache[i].create_subid = parentSubid;
|
2004-07-28 16:23:31 +02:00
|
|
|
else if (VfdCache[i].fileName != NULL)
|
|
|
|
FileClose(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < numAllocatedDescs; i++)
|
|
|
|
{
|
2004-09-16 18:58:44 +02:00
|
|
|
if (allocatedDescs[i].create_subid == mySubid)
|
2004-07-28 16:23:31 +02:00
|
|
|
{
|
|
|
|
if (isCommit)
|
2004-09-16 18:58:44 +02:00
|
|
|
allocatedDescs[i].create_subid = parentSubid;
|
2004-07-28 16:23:31 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* have to recheck the item after FreeDesc (ugly) */
|
|
|
|
FreeDesc(&allocatedDescs[i--]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/*
|
|
|
|
* AtEOXact_Files
|
|
|
|
*
|
2003-04-29 05:21:30 +02:00
|
|
|
* This routine is called during transaction commit or abort (it doesn't
|
|
|
|
* particularly care which). All still-open per-transaction temporary file
|
|
|
|
* VFDs are closed, which also causes the underlying files to be
|
|
|
|
* deleted. Furthermore, all "allocated" stdio files are closed.
|
1999-05-09 02:52:08 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AtEOXact_Files(void)
|
|
|
|
{
|
2003-04-29 05:21:30 +02:00
|
|
|
CleanupTempFiles(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AtProcExit_Files
|
|
|
|
*
|
|
|
|
* on_proc_exit hook to clean up temp files during backend shutdown.
|
|
|
|
* Here, we want to clean up *all* temp files including interXact ones.
|
|
|
|
*/
|
|
|
|
static void
|
2003-12-12 19:45:10 +01:00
|
|
|
AtProcExit_Files(int code, Datum arg)
|
2003-04-29 05:21:30 +02:00
|
|
|
{
|
|
|
|
CleanupTempFiles(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close temporary files and delete their underlying files.
|
|
|
|
*
|
|
|
|
* isProcExit: if true, this is being called as the backend process is
|
|
|
|
* exiting. If that's the case, we should remove all temporary files; if
|
|
|
|
* that's not the case, we are being called for transaction commit/abort
|
|
|
|
* and should only remove transaction-local temp files. In either case,
|
2004-02-24 00:03:10 +01:00
|
|
|
* also clean up "allocated" stdio files and dirs.
|
2003-04-29 05:21:30 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CleanupTempFiles(bool isProcExit)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Index i;
|
1999-05-09 02:52:08 +02:00
|
|
|
|
|
|
|
if (SizeVfdCache > 0)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
|
1999-05-09 02:52:08 +02:00
|
|
|
for (i = 1; i < SizeVfdCache; i++)
|
|
|
|
{
|
2003-04-29 05:21:30 +02:00
|
|
|
unsigned short fdstate = VfdCache[i].fdstate;
|
|
|
|
|
|
|
|
if ((fdstate & FD_TEMPORARY) && VfdCache[i].fileName != NULL)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we're in the process of exiting a backend process, close
|
|
|
|
* all temporary files. Otherwise, only close temporary files
|
|
|
|
* local to the current transaction.
|
2003-04-29 05:21:30 +02:00
|
|
|
*/
|
|
|
|
if (isProcExit || (fdstate & FD_XACT_TEMPORARY))
|
|
|
|
FileClose(i);
|
|
|
|
}
|
1999-05-09 02:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
while (numAllocatedDescs > 0)
|
|
|
|
FreeDesc(&allocatedDescs[0]);
|
1999-05-09 02:52:08 +02:00
|
|
|
}
|
2001-06-11 06:12:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-04-29 05:21:30 +02:00
|
|
|
* Remove temporary files left over from a prior postmaster session
|
2001-06-11 06:12:29 +02:00
|
|
|
*
|
|
|
|
* This should be called during postmaster startup. It will forcibly
|
|
|
|
* remove any leftover files created by OpenTemporaryFile.
|
2003-04-29 05:21:30 +02:00
|
|
|
*
|
|
|
|
* NOTE: we could, but don't, call this during a post-backend-crash restart
|
|
|
|
* cycle. The argument for not doing it is that someone might want to examine
|
|
|
|
* the temp files for debugging purposes. This does however mean that
|
|
|
|
* OpenTemporaryFile had better allow for collision with an existing temp
|
|
|
|
* file name.
|
2001-06-11 06:12:29 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
RemovePgTempFiles(void)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
char temp_path[MAXPGPATH];
|
2001-06-11 06:12:29 +02:00
|
|
|
DIR *db_dir;
|
2001-10-25 07:50:21 +02:00
|
|
|
struct dirent *db_de;
|
2001-06-11 06:12:29 +02:00
|
|
|
|
|
|
|
/*
|
2004-12-29 22:36:09 +01:00
|
|
|
* Cycle through pgsql_tmp directories for all databases and remove old
|
|
|
|
* temp files.
|
2001-06-11 06:12:29 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
db_dir = AllocateDir("base");
|
2004-12-29 22:36:09 +01:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
while ((db_de = ReadDir(db_dir, "base")) != NULL)
|
2004-12-29 22:36:09 +01:00
|
|
|
{
|
|
|
|
if (strcmp(db_de->d_name, ".") == 0 ||
|
|
|
|
strcmp(db_de->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(temp_path, sizeof(temp_path), "base/%s/%s",
|
|
|
|
db_de->d_name, PG_TEMP_FILES_DIR);
|
2004-12-29 22:36:09 +01:00
|
|
|
RemovePgTempFilesInDir(temp_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeDir(db_dir);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* In EXEC_BACKEND case there is a pgsql_tmp directory at the top level of
|
|
|
|
* DataDir as well.
|
2004-12-29 22:36:09 +01:00
|
|
|
*/
|
|
|
|
#ifdef EXEC_BACKEND
|
2005-07-04 06:51:52 +02:00
|
|
|
RemovePgTempFilesInDir(PG_TEMP_FILES_DIR);
|
2003-12-20 18:31:21 +01:00
|
|
|
#endif
|
2004-12-29 22:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process one pgsql_tmp directory for RemovePgTempFiles */
|
|
|
|
static void
|
|
|
|
RemovePgTempFilesInDir(const char *tmpdirname)
|
|
|
|
{
|
|
|
|
DIR *temp_dir;
|
|
|
|
struct dirent *temp_de;
|
|
|
|
char rm_path[MAXPGPATH];
|
|
|
|
|
|
|
|
temp_dir = AllocateDir(tmpdirname);
|
|
|
|
if (temp_dir == NULL)
|
|
|
|
{
|
|
|
|
/* anything except ENOENT is fishy */
|
|
|
|
if (errno != ENOENT)
|
|
|
|
elog(LOG,
|
|
|
|
"could not open temporary-files directory \"%s\": %m",
|
|
|
|
tmpdirname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-19 23:34:03 +02:00
|
|
|
while ((temp_de = ReadDir(temp_dir, tmpdirname)) != NULL)
|
2004-12-29 22:36:09 +01:00
|
|
|
{
|
|
|
|
if (strcmp(temp_de->d_name, ".") == 0 ||
|
|
|
|
strcmp(temp_de->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(rm_path, sizeof(rm_path), "%s/%s",
|
|
|
|
tmpdirname, temp_de->d_name);
|
|
|
|
|
|
|
|
if (strncmp(temp_de->d_name,
|
|
|
|
PG_TEMP_FILE_PREFIX,
|
|
|
|
strlen(PG_TEMP_FILE_PREFIX)) == 0)
|
|
|
|
unlink(rm_path); /* note we ignore any error */
|
|
|
|
else
|
|
|
|
elog(LOG,
|
|
|
|
"unexpected file found in temporary-files directory: \"%s\"",
|
|
|
|
rm_path);
|
2001-06-11 06:12:29 +02:00
|
|
|
}
|
2004-12-29 22:36:09 +01:00
|
|
|
|
|
|
|
FreeDir(temp_dir);
|
2001-06-11 06:12:29 +02:00
|
|
|
}
|