LCOV - code coverage report
Current view: top level - src/backend/storage/file - sharedfileset.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 29 31 93.5 %
Date: 2021-09-17 16:07:28 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * sharedfileset.c
       4             :  *    Shared temporary file management.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/storage/file/sharedfileset.c
      11             :  *
      12             :  * SharedFileSets provide a temporary namespace (think directory) so that
      13             :  * files can be discovered by name, and a shared ownership semantics so that
      14             :  * shared files survive until the last user detaches.
      15             :  *
      16             :  *-------------------------------------------------------------------------
      17             :  */
      18             : 
      19             : #include "postgres.h"
      20             : 
      21             : #include <limits.h>
      22             : 
      23             : #include "catalog/pg_tablespace.h"
      24             : #include "commands/tablespace.h"
      25             : #include "common/hashfn.h"
      26             : #include "miscadmin.h"
      27             : #include "storage/dsm.h"
      28             : #include "storage/ipc.h"
      29             : #include "storage/sharedfileset.h"
      30             : #include "utils/builtins.h"
      31             : 
      32             : static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum);
      33             : 
      34             : /*
      35             :  * Initialize a space for temporary files that can be opened by other backends.
      36             :  * Other backends must attach to it before accessing it.  Associate this
      37             :  * SharedFileSet with 'seg'.  Any contained files will be deleted when the
      38             :  * last backend detaches.
      39             :  *
      40             :  * Under the covers the set is one or more directories which will eventually
      41             :  * be deleted.
      42             :  */
      43             : void
      44         204 : SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg)
      45             : {
      46             :     /* Initialize the shared fileset specific members. */
      47         204 :     SpinLockInit(&fileset->mutex);
      48         204 :     fileset->refcnt = 1;
      49             : 
      50             :     /* Initialize the fileset. */
      51         204 :     FileSetInit(&fileset->fs);
      52             : 
      53             :     /* Register our cleanup callback. */
      54         204 :     if (seg)
      55         204 :         on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));
      56         204 : }
      57             : 
      58             : /*
      59             :  * Attach to a set of directories that was created with SharedFileSetInit.
      60             :  */
      61             : void
      62         312 : SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg)
      63             : {
      64             :     bool        success;
      65             : 
      66         312 :     SpinLockAcquire(&fileset->mutex);
      67         312 :     if (fileset->refcnt == 0)
      68           0 :         success = false;
      69             :     else
      70             :     {
      71         312 :         ++fileset->refcnt;
      72         312 :         success = true;
      73             :     }
      74         312 :     SpinLockRelease(&fileset->mutex);
      75             : 
      76         312 :     if (!success)
      77           0 :         ereport(ERROR,
      78             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
      79             :                  errmsg("could not attach to a SharedFileSet that is already destroyed")));
      80             : 
      81             :     /* Register our cleanup callback. */
      82         312 :     on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));
      83         312 : }
      84             : 
      85             : /*
      86             :  * Delete all files in the set.
      87             :  */
      88             : void
      89          32 : SharedFileSetDeleteAll(SharedFileSet *fileset)
      90             : {
      91          32 :     FileSetDeleteAll(&fileset->fs);
      92          32 : }
      93             : 
      94             : /*
      95             :  * Callback function that will be invoked when this backend detaches from a
      96             :  * DSM segment holding a SharedFileSet that it has created or attached to.  If
      97             :  * we are the last to detach, then try to remove the directories and
      98             :  * everything in them.  We can't raise an error on failures, because this runs
      99             :  * in error cleanup paths.
     100             :  */
     101             : static void
     102         516 : SharedFileSetOnDetach(dsm_segment *segment, Datum datum)
     103             : {
     104         516 :     bool        unlink_all = false;
     105         516 :     SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum);
     106             : 
     107         516 :     SpinLockAcquire(&fileset->mutex);
     108             :     Assert(fileset->refcnt > 0);
     109         516 :     if (--fileset->refcnt == 0)
     110         204 :         unlink_all = true;
     111         516 :     SpinLockRelease(&fileset->mutex);
     112             : 
     113             :     /*
     114             :      * If we are the last to detach, we delete the directory in all
     115             :      * tablespaces.  Note that we are still actually attached for the rest of
     116             :      * this function so we can safely access its data.
     117             :      */
     118         516 :     if (unlink_all)
     119         204 :         FileSetDeleteAll(&fileset->fs);
     120         516 : }

Generated by: LCOV version 1.13