LCOV - differential code coverage report
Current view: top level - src/backend/storage/file - sharedfileset.c (source / functions) Coverage Total Hit UBC GBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 93.8 % 32 30 2 30
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 4 4 4
Baseline: lcov-20250906-005545-baseline Branches: 66.7 % 18 12 6 1 11
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 93.8 % 32 30 2 30
Function coverage date bins:
(360..) days: 100.0 % 4 4 4
Branch coverage date bins:
(360..) days: 66.7 % 18 12 6 1 11

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * sharedfileset.c
                                  4                 :                :  *    Shared temporary file management.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, 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 "storage/dsm.h"
                                 24                 :                : #include "storage/sharedfileset.h"
                                 25                 :                : 
                                 26                 :                : static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum);
                                 27                 :                : 
                                 28                 :                : /*
                                 29                 :                :  * Initialize a space for temporary files that can be opened by other backends.
                                 30                 :                :  * Other backends must attach to it before accessing it.  Associate this
                                 31                 :                :  * SharedFileSet with 'seg'.  Any contained files will be deleted when the
                                 32                 :                :  * last backend detaches.
                                 33                 :                :  *
                                 34                 :                :  * Under the covers the set is one or more directories which will eventually
                                 35                 :                :  * be deleted.
                                 36                 :                :  */
                                 37                 :                : void
 2836 andres@anarazel.de         38                 :CBC         171 : SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg)
                                 39                 :                : {
                                 40                 :                :     /* Initialize the shared fileset specific members. */
                                 41                 :            171 :     SpinLockInit(&fileset->mutex);
                                 42                 :            171 :     fileset->refcnt = 1;
                                 43                 :                : 
                                 44                 :                :     /* Initialize the fileset. */
 1468 akapila@postgresql.o       45                 :            171 :     FileSetInit(&fileset->fs);
                                 46                 :                : 
                                 47                 :                :     /* Register our cleanup callback. */
 1837                            48         [ +  - ]:            171 :     if (seg)
                                 49                 :            171 :         on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));
 2836 andres@anarazel.de         50                 :            171 : }
                                 51                 :                : 
                                 52                 :                : /*
                                 53                 :                :  * Attach to a set of directories that was created with SharedFileSetInit.
                                 54                 :                :  */
                                 55                 :                : void
                                 56                 :            265 : SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg)
                                 57                 :                : {
                                 58                 :                :     bool        success;
                                 59                 :                : 
                                 60         [ +  + ]:            265 :     SpinLockAcquire(&fileset->mutex);
                                 61         [ -  + ]:            265 :     if (fileset->refcnt == 0)
 2836 andres@anarazel.de         62                 :UBC           0 :         success = false;
                                 63                 :                :     else
                                 64                 :                :     {
 2836 andres@anarazel.de         65                 :CBC         265 :         ++fileset->refcnt;
                                 66                 :            265 :         success = true;
                                 67                 :                :     }
                                 68                 :            265 :     SpinLockRelease(&fileset->mutex);
                                 69                 :                : 
                                 70         [ -  + ]:            265 :     if (!success)
 2836 andres@anarazel.de         71         [ #  # ]:UBC           0 :         ereport(ERROR,
                                 72                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                 73                 :                :                  errmsg("could not attach to a SharedFileSet that is already destroyed")));
                                 74                 :                : 
                                 75                 :                :     /* Register our cleanup callback. */
 2836 andres@anarazel.de         76                 :CBC         265 :     on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));
                                 77                 :            265 : }
                                 78                 :                : 
                                 79                 :                : /*
                                 80                 :                :  * Delete all files in the set.
                                 81                 :                :  */
                                 82                 :                : void
                                 83                 :             24 : SharedFileSetDeleteAll(SharedFileSet *fileset)
                                 84                 :                : {
 1468 akapila@postgresql.o       85                 :             24 :     FileSetDeleteAll(&fileset->fs);
 2836 andres@anarazel.de         86                 :             24 : }
                                 87                 :                : 
                                 88                 :                : /*
                                 89                 :                :  * Callback function that will be invoked when this backend detaches from a
                                 90                 :                :  * DSM segment holding a SharedFileSet that it has created or attached to.  If
                                 91                 :                :  * we are the last to detach, then try to remove the directories and
                                 92                 :                :  * everything in them.  We can't raise an error on failures, because this runs
                                 93                 :                :  * in error cleanup paths.
                                 94                 :                :  */
                                 95                 :                : static void
                                 96                 :            436 : SharedFileSetOnDetach(dsm_segment *segment, Datum datum)
                                 97                 :                : {
                                 98                 :            436 :     bool        unlink_all = false;
                                 99                 :            436 :     SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum);
                                100                 :                : 
                                101         [ +  + ]:            436 :     SpinLockAcquire(&fileset->mutex);
                                102         [ -  + ]:            436 :     Assert(fileset->refcnt > 0);
                                103         [ +  + ]:            436 :     if (--fileset->refcnt == 0)
                                104                 :            171 :         unlink_all = true;
                                105                 :            436 :     SpinLockRelease(&fileset->mutex);
                                106                 :                : 
                                107                 :                :     /*
                                108                 :                :      * If we are the last to detach, we delete the directory in all
                                109                 :                :      * tablespaces.  Note that we are still actually attached for the rest of
                                110                 :                :      * this function so we can safely access its data.
                                111                 :                :      */
                                112         [ +  + ]:            436 :     if (unlink_all)
 1468 akapila@postgresql.o      113                 :            171 :         FileSetDeleteAll(&fileset->fs);
 2836 andres@anarazel.de        114                 :            436 : }
        

Generated by: LCOV version 2.4-beta