LCOV - code coverage report
Current view: top level - contrib/lo - lo.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 89.2 % 37 33
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 50.0 % 38 19

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  *  PostgreSQL definitions for managed Large Objects.
       3                 :             :  *
       4                 :             :  *  contrib/lo/lo.c
       5                 :             :  *
       6                 :             :  */
       7                 :             : 
       8                 :             : #include "postgres.h"
       9                 :             : 
      10                 :             : #include "commands/trigger.h"
      11                 :             : #include "executor/spi.h"
      12                 :             : #include "utils/fmgrprotos.h"
      13                 :             : #include "utils/rel.h"
      14                 :             : 
      15                 :           1 : PG_MODULE_MAGIC_EXT(
      16                 :             :                     .name = "lo",
      17                 :             :                     .version = PG_VERSION
      18                 :             : );
      19                 :             : 
      20                 :             : 
      21                 :             : /*
      22                 :             :  * This is the trigger that protects us from orphaned large objects
      23                 :             :  */
      24                 :           2 : PG_FUNCTION_INFO_V1(lo_manage);
      25                 :             : 
      26                 :             : Datum
      27                 :           8 : lo_manage(PG_FUNCTION_ARGS)
      28                 :             : {
      29                 :           8 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
      30                 :             :     int         attnum;         /* attribute number to monitor  */
      31                 :             :     char      **args;           /* Args containing attr name    */
      32                 :             :     TupleDesc   tupdesc;        /* Tuple Descriptor             */
      33                 :             :     HeapTuple   rettuple;       /* Tuple to be returned         */
      34                 :             :     bool        isdelete;       /* are we deleting?             */
      35                 :             :     HeapTuple   newtuple;       /* The new value for tuple      */
      36                 :             :     HeapTuple   trigtuple;      /* The original value of tuple  */
      37                 :             : 
      38   [ +  -  -  + ]:           8 :     if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
      39         [ #  # ]:           0 :         elog(ERROR, "lo_manage: not fired by trigger manager");
      40                 :             : 
      41         [ -  + ]:           8 :     if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) /* internal error */
      42         [ #  # ]:           0 :         elog(ERROR, "%s: must be fired for row",
      43                 :             :              trigdata->tg_trigger->tgname);
      44                 :             : 
      45                 :             :     /*
      46                 :             :      * Fetch some values from trigdata
      47                 :             :      */
      48                 :           8 :     newtuple = trigdata->tg_newtuple;
      49                 :           8 :     trigtuple = trigdata->tg_trigtuple;
      50                 :           8 :     tupdesc = trigdata->tg_relation->rd_att;
      51                 :           8 :     args = trigdata->tg_trigger->tgargs;
      52                 :             : 
      53         [ -  + ]:           8 :     if (args == NULL)           /* internal error */
      54         [ #  # ]:           0 :         elog(ERROR, "%s: no column name provided in the trigger definition",
      55                 :             :              trigdata->tg_trigger->tgname);
      56                 :             : 
      57                 :             :     /* tuple to return to Executor */
      58         [ +  + ]:           8 :     if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
      59                 :           6 :         rettuple = newtuple;
      60                 :             :     else
      61                 :           2 :         rettuple = trigtuple;
      62                 :             : 
      63                 :             :     /* Are we deleting the row? */
      64                 :           8 :     isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
      65                 :             : 
      66                 :             :     /* Get the column we're interested in */
      67                 :           8 :     attnum = SPI_fnumber(tupdesc, args[0]);
      68                 :             : 
      69         [ -  + ]:           8 :     if (attnum <= 0)
      70         [ #  # ]:           0 :         elog(ERROR, "%s: column \"%s\" does not exist",
      71                 :             :              trigdata->tg_trigger->tgname, args[0]);
      72                 :             : 
      73                 :             :     /*
      74                 :             :      * Handle updates
      75                 :             :      *
      76                 :             :      * Here, if the value of the monitored attribute changes, then the large
      77                 :             :      * object associated with the original value is unlinked.
      78                 :             :      */
      79   [ +  +  +  + ]:          14 :     if (newtuple != NULL &&
      80                 :           6 :         bms_is_member(attnum - FirstLowInvalidHeapAttributeNumber, trigdata->tg_updatedcols))
      81                 :             :     {
      82                 :           3 :         char       *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
      83                 :           3 :         char       *newv = SPI_getvalue(newtuple, tupdesc, attnum);
      84                 :             : 
      85   [ +  -  +  -  :           3 :         if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
                   +  - ]
      86                 :           3 :             DirectFunctionCall1(be_lo_unlink,
      87                 :             :                                 ObjectIdGetDatum(atooid(orig)));
      88                 :             : 
      89         [ +  - ]:           3 :         if (newv)
      90                 :           3 :             pfree(newv);
      91         [ +  - ]:           3 :         if (orig)
      92                 :           3 :             pfree(orig);
      93                 :             :     }
      94                 :             : 
      95                 :             :     /*
      96                 :             :      * Handle deleting of rows
      97                 :             :      *
      98                 :             :      * Here, we unlink the large object associated with the managed attribute
      99                 :             :      */
     100         [ +  + ]:           8 :     if (isdelete)
     101                 :             :     {
     102                 :           2 :         char       *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
     103                 :             : 
     104         [ +  - ]:           2 :         if (orig != NULL)
     105                 :             :         {
     106                 :           2 :             DirectFunctionCall1(be_lo_unlink,
     107                 :             :                                 ObjectIdGetDatum(atooid(orig)));
     108                 :             : 
     109                 :           2 :             pfree(orig);
     110                 :             :         }
     111                 :             :     }
     112                 :             : 
     113                 :           8 :     return PointerGetDatum(rettuple);
     114                 :             : }
        

Generated by: LCOV version 2.0-1