Re: [HACKERS] Patch to log usage of temporary files - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [HACKERS] Patch to log usage of temporary files
Date
Msg-id 200701092132.l09LWTM27477@momjian.us
Whole thread Raw
In response to Re: [HACKERS] Patch to log usage of temporary files  (Bill Moran <wmoran@collaborativefusion.com>)
Responses Re: [HACKERS] Patch to log usage of temporary files  (Bill Moran <wmoran@collaborativefusion.com>)
Re: [HACKERS] Patch to log usage of temporary files  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
Bill Moran wrote:
> In response to Tom Lane <tgl@sss.pgh.pa.us>:
>
> > Bill Moran <wmoran@collaborativefusion.com> writes:
> > > Andrew Dunstan <andrew@dunslane.net> wrote:
> > >>> Might be more robust to say
> > >>> if (trace_temp_files >= 0)
> >
> > > I specified in the GUC config that minimum allowable value is -1.
> >
> > I'd still tend to go with Andrew's suggestion because it makes this
> > particular bit of code self-defending against bad values.  Yes, it's
> > reasonably safe given that bit of coding way over yonder in guc.c,
> > but there's no particularly good reason why this code has to depend
> > on that to avoid doing something stupid.  And it's easier to understand
> > too --- you don't have to go looking in guc.c to convince yourself it's
> > safe.
>
> Ahh ... well, I've probably already argued about it more than it's worth.
> The patch is easy enough to adjust, find attached.

I have applied a modified version of your patch.  I renamed the
parameter to 'log_temp_files', for consistency, added documentation, and
improved the wording, particularly mentioning that the logging happens
at file deletion time.

Thanks.

--
  Bruce Momjian   bruce@momjian.us
  EnterpriseDB    http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
Index: doc/src/sgml/config.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/config.sgml,v
retrieving revision 1.99
diff -c -c -r1.99 config.sgml
*** doc/src/sgml/config.sgml    12 Dec 2006 21:30:33 -0000    1.99
--- doc/src/sgml/config.sgml    9 Jan 2007 21:03:51 -0000
***************
*** 2920,2925 ****
--- 2920,2942 ----
        </listitem>
       </varlistentry>

+      <varlistentry id="guc-log-temp-files" xreflabel="log_temp_files">
+       <term><varname>log_temp_files</varname> (<type>integer</type>)</term>
+       <indexterm>
+        <primary><varname>log_temp_files</> configuration parameter</primary>
+       </indexterm>
+       <listitem>
+        <para>
+         Controls whether temporary files are logged when deleted.
+         A value of zero logs all temporary files, and positive
+         values log only files whose size is equal or greater than
+         the specified number of bytes.  Temporary files can be
+         created for sorts, hashes, and temporary results.  The
+         default is <literal>-1</> (off).
+        </para>
+       </listitem>
+      </varlistentry>
+
       </variablelist>
      </sect2>
     </sect1>
Index: src/backend/storage/file/fd.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/storage/file/fd.c,v
retrieving revision 1.132
diff -c -c -r1.132 fd.c
*** src/backend/storage/file/fd.c    5 Jan 2007 22:19:37 -0000    1.132
--- src/backend/storage/file/fd.c    9 Jan 2007 21:03:54 -0000
***************
*** 50,55 ****
--- 50,56 ----
  #include "access/xact.h"
  #include "storage/fd.h"
  #include "storage/ipc.h"
+ #include "utils/guc.h"


  /*
***************
*** 938,944 ****
  void
  FileClose(File file)
  {
!     Vfd           *vfdP;

      Assert(FileIsValid(file));

--- 939,946 ----
  void
  FileClose(File file)
  {
!     Vfd            *vfdP;
!     struct stat    filestats;

      Assert(FileIsValid(file));

***************
*** 968,973 ****
--- 970,988 ----
      {
          /* reset flag so that die() interrupt won't cause problems */
          vfdP->fdstate &= ~FD_TEMPORARY;
+         PG_TRACE1(temp__file__cleanup, vfdP->fileName);
+         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);
+         }
          if (unlink(vfdP->fileName))
              elog(LOG, "failed to unlink \"%s\": %m",
                   vfdP->fileName);
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v
retrieving revision 1.365
diff -c -c -r1.365 guc.c
*** src/backend/utils/misc/guc.c    5 Jan 2007 22:19:46 -0000    1.365
--- src/backend/utils/misc/guc.c    9 Jan 2007 21:03:57 -0000
***************
*** 182,187 ****
--- 182,188 ----
  int            log_min_messages = NOTICE;
  int            client_min_messages = NOTICE;
  int            log_min_duration_statement = -1;
+ int            log_temp_files = -1;

  int            num_temp_buffers = 1000;

***************
*** 1660,1665 ****
--- 1661,1676 ----
          &server_version_num,
          PG_VERSION_NUM, PG_VERSION_NUM, PG_VERSION_NUM, NULL, NULL
      },
+
+     {
+         {"log_temp_files", PGC_USERSET, LOGGING_WHAT,
+             gettext_noop("Log the use of temporary files larger than this size."),
+             gettext_noop("Zero logs all files. The default is -1 (turning this feature off)."),
+             NULL
+         },
+         &log_temp_files,
+         -1, -1, INT_MAX, NULL, NULL
+     },

      /* End-of-list marker */
      {
Index: src/backend/utils/misc/postgresql.conf.sample
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/misc/postgresql.conf.sample,v
retrieving revision 1.199
diff -c -c -r1.199 postgresql.conf.sample
*** src/backend/utils/misc/postgresql.conf.sample    21 Nov 2006 01:23:37 -0000    1.199
--- src/backend/utils/misc/postgresql.conf.sample    9 Jan 2007 21:03:57 -0000
***************
*** 333,338 ****
--- 333,341 ----
  #log_statement = 'none'            # none, ddl, mod, all
  #log_hostname = off

+ #log_temp_files = -1            # Log temporary files equal or larger
+                     # than the specified number of bytes.
+                     # -1 disables;  0 logs all temp files

  #---------------------------------------------------------------------------
  # RUNTIME STATISTICS
Index: src/include/utils/guc.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/utils/guc.h,v
retrieving revision 1.77
diff -c -c -r1.77 guc.h
*** src/include/utils/guc.h    5 Jan 2007 22:19:59 -0000    1.77
--- src/include/utils/guc.h    9 Jan 2007 21:03:58 -0000
***************
*** 123,128 ****
--- 123,129 ----
  extern int    log_min_messages;
  extern int    client_min_messages;
  extern int    log_min_duration_statement;
+ extern int    log_temp_files;

  extern int    num_temp_buffers;


pgsql-patches by date:

Previous
From: "Simon Riggs"
Date:
Subject: COPY with no WAL, v2
Next
From: Bill Moran
Date:
Subject: Re: [HACKERS] Patch to log usage of temporary files