LCOV - code coverage report
Current view: top level - src/backend/utils/adt - partitionfuncs.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 99.0 % 100 99
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 75.0 % 68 51

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * partitionfuncs.c
       4                 :             :  *        Functions for accessing partition-related metadata
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *        src/backend/utils/adt/partitionfuncs.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : 
      16                 :             : #include "postgres.h"
      17                 :             : 
      18                 :             : #include "access/htup_details.h"
      19                 :             : #include "catalog/partition.h"
      20                 :             : #include "catalog/pg_class.h"
      21                 :             : #include "catalog/pg_inherits.h"
      22                 :             : #include "funcapi.h"
      23                 :             : #include "utils/fmgrprotos.h"
      24                 :             : #include "utils/lsyscache.h"
      25                 :             : #include "utils/syscache.h"
      26                 :             : 
      27                 :             : /*
      28                 :             :  * Checks if a given relation can be part of a partition tree.  Returns
      29                 :             :  * false if the relation cannot be processed, in which case it is up to
      30                 :             :  * the caller to decide what to do, by either raising an error or doing
      31                 :             :  * something else.
      32                 :             :  */
      33                 :             : static bool
      34                 :        4227 : check_rel_can_be_partition(Oid relid)
      35                 :             : {
      36                 :        4227 :         char            relkind;
      37                 :        4227 :         bool            relispartition;
      38                 :             : 
      39                 :             :         /* Check if relation exists */
      40         [ +  + ]:        4227 :         if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relid)))
      41                 :           3 :                 return false;
      42                 :             : 
      43                 :        4224 :         relkind = get_rel_relkind(relid);
      44                 :        4224 :         relispartition = get_rel_relispartition(relid);
      45                 :             : 
      46                 :             :         /* Only allow relation types that can appear in partition trees. */
      47   [ +  +  +  +  :        4224 :         if (!relispartition && !RELKIND_HAS_PARTITIONS(relkind))
                   +  + ]
      48                 :        2502 :                 return false;
      49                 :             : 
      50                 :        1722 :         return true;
      51                 :        4227 : }
      52                 :             : 
      53                 :             : /*
      54                 :             :  * pg_partition_tree
      55                 :             :  *
      56                 :             :  * Produce a view with one row per member of a partition tree, beginning
      57                 :             :  * from the top-most parent given by the caller.  This gives information
      58                 :             :  * about each partition, its immediate partitioned parent, if it is
      59                 :             :  * a leaf partition and its level in the hierarchy.
      60                 :             :  */
      61                 :             : Datum
      62                 :         177 : pg_partition_tree(PG_FUNCTION_ARGS)
      63                 :             : {
      64                 :             : #define PG_PARTITION_TREE_COLS  4
      65                 :         177 :         Oid                     rootrelid = PG_GETARG_OID(0);
      66                 :         177 :         FuncCallContext *funcctx;
      67                 :         177 :         List       *partitions;
      68                 :             : 
      69                 :             :         /* stuff done only on the first call of the function */
      70         [ +  + ]:         177 :         if (SRF_IS_FIRSTCALL())
      71                 :             :         {
      72                 :          36 :                 MemoryContext oldcxt;
      73                 :          36 :                 TupleDesc       tupdesc;
      74                 :             : 
      75                 :             :                 /* create a function context for cross-call persistence */
      76                 :          36 :                 funcctx = SRF_FIRSTCALL_INIT();
      77                 :             : 
      78         [ +  + ]:          36 :                 if (!check_rel_can_be_partition(rootrelid))
      79         [ +  - ]:           6 :                         SRF_RETURN_DONE(funcctx);
      80                 :             : 
      81                 :             :                 /* switch to memory context appropriate for multiple function calls */
      82                 :          30 :                 oldcxt = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
      83                 :             : 
      84                 :             :                 /*
      85                 :             :                  * Find all members of inheritance set.  We only need AccessShareLock
      86                 :             :                  * on the children for the partition information lookup.
      87                 :             :                  */
      88                 :          30 :                 partitions = find_all_inheritors(rootrelid, AccessShareLock, NULL);
      89                 :             : 
      90         [ +  - ]:          30 :                 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
      91   [ #  #  #  # ]:           0 :                         elog(ERROR, "return type must be a row type");
      92                 :          30 :                 funcctx->tuple_desc = tupdesc;
      93                 :             : 
      94                 :             :                 /* The only state we need is the partition list */
      95                 :          30 :                 funcctx->user_fctx = partitions;
      96                 :             : 
      97                 :          30 :                 MemoryContextSwitchTo(oldcxt);
      98         [ +  + ]:          36 :         }
      99                 :             : 
     100                 :             :         /* stuff done on every call of the function */
     101                 :         171 :         funcctx = SRF_PERCALL_SETUP();
     102                 :         171 :         partitions = (List *) funcctx->user_fctx;
     103                 :             : 
     104         [ +  + ]:         171 :         if (funcctx->call_cntr < list_length(partitions))
     105                 :             :         {
     106                 :         141 :                 Datum           result;
     107                 :         141 :                 Datum           values[PG_PARTITION_TREE_COLS] = {0};
     108                 :         141 :                 bool            nulls[PG_PARTITION_TREE_COLS] = {0};
     109                 :         141 :                 HeapTuple       tuple;
     110                 :         141 :                 Oid                     parentid = InvalidOid;
     111                 :         141 :                 Oid                     relid = list_nth_oid(partitions, funcctx->call_cntr);
     112                 :         141 :                 char            relkind = get_rel_relkind(relid);
     113                 :         141 :                 int                     level = 0;
     114                 :         141 :                 List       *ancestors = get_partition_ancestors(relid);
     115                 :         141 :                 ListCell   *lc;
     116                 :             : 
     117                 :             :                 /*
     118                 :             :                  * Form tuple with appropriate data.
     119                 :             :                  */
     120                 :             : 
     121                 :             :                 /* relid */
     122                 :         141 :                 values[0] = ObjectIdGetDatum(relid);
     123                 :             : 
     124                 :             :                 /* parentid */
     125         [ +  + ]:         141 :                 if (ancestors != NIL)
     126                 :         117 :                         parentid = linitial_oid(ancestors);
     127         [ +  + ]:         141 :                 if (OidIsValid(parentid))
     128                 :         117 :                         values[1] = ObjectIdGetDatum(parentid);
     129                 :             :                 else
     130                 :          24 :                         nulls[1] = true;
     131                 :             : 
     132                 :             :                 /* isleaf */
     133         [ +  + ]:         141 :                 values[2] = BoolGetDatum(!RELKIND_HAS_PARTITIONS(relkind));
     134                 :             : 
     135                 :             :                 /* level */
     136         [ +  + ]:         141 :                 if (relid != rootrelid)
     137                 :             :                 {
     138   [ +  -  -  +  :         269 :                         foreach(lc, ancestors)
                   +  - ]
     139                 :             :                         {
     140                 :         158 :                                 level++;
     141         [ +  + ]:         158 :                                 if (lfirst_oid(lc) == rootrelid)
     142                 :         111 :                                         break;
     143                 :          47 :                         }
     144                 :         111 :                 }
     145                 :         141 :                 values[3] = Int32GetDatum(level);
     146                 :             : 
     147                 :         141 :                 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
     148                 :         141 :                 result = HeapTupleGetDatum(tuple);
     149                 :         141 :                 SRF_RETURN_NEXT(funcctx, result);
     150         [ +  - ]:         141 :         }
     151                 :             : 
     152                 :             :         /* done when there are no more elements left */
     153         [ +  - ]:          30 :         SRF_RETURN_DONE(funcctx);
     154         [ -  + ]:         177 : }
     155                 :             : 
     156                 :             : /*
     157                 :             :  * pg_partition_root
     158                 :             :  *
     159                 :             :  * Returns the top-most parent of the partition tree to which a given
     160                 :             :  * relation belongs, or NULL if it's not (or cannot be) part of any
     161                 :             :  * partition tree.
     162                 :             :  */
     163                 :             : Datum
     164                 :          16 : pg_partition_root(PG_FUNCTION_ARGS)
     165                 :             : {
     166                 :          16 :         Oid                     relid = PG_GETARG_OID(0);
     167                 :          16 :         Oid                     rootrelid;
     168                 :          16 :         List       *ancestors;
     169                 :             : 
     170         [ +  + ]:          16 :         if (!check_rel_can_be_partition(relid))
     171                 :           6 :                 PG_RETURN_NULL();
     172                 :             : 
     173                 :             :         /* fetch the list of ancestors */
     174                 :          10 :         ancestors = get_partition_ancestors(relid);
     175                 :             : 
     176                 :             :         /*
     177                 :             :          * If the input relation is already the top-most parent, just return
     178                 :             :          * itself.
     179                 :             :          */
     180         [ +  + ]:          10 :         if (ancestors == NIL)
     181                 :           2 :                 PG_RETURN_OID(relid);
     182                 :             : 
     183                 :           8 :         rootrelid = llast_oid(ancestors);
     184                 :           8 :         list_free(ancestors);
     185                 :             : 
     186                 :             :         /*
     187                 :             :          * "rootrelid" must contain a valid OID, given that the input relation is
     188                 :             :          * a valid partition tree member as checked above.
     189                 :             :          */
     190         [ +  - ]:           8 :         Assert(OidIsValid(rootrelid));
     191                 :           8 :         PG_RETURN_OID(rootrelid);
     192                 :          16 : }
     193                 :             : 
     194                 :             : /*
     195                 :             :  * pg_partition_ancestors
     196                 :             :  *
     197                 :             :  * Produces a view with one row per ancestor of the given partition,
     198                 :             :  * including the input relation itself.
     199                 :             :  */
     200                 :             : Datum
     201                 :        7204 : pg_partition_ancestors(PG_FUNCTION_ARGS)
     202                 :             : {
     203                 :        7204 :         Oid                     relid = PG_GETARG_OID(0);
     204                 :        7204 :         FuncCallContext *funcctx;
     205                 :        7204 :         List       *ancestors;
     206                 :             : 
     207         [ +  + ]:        7204 :         if (SRF_IS_FIRSTCALL())
     208                 :             :         {
     209                 :        4175 :                 MemoryContext oldcxt;
     210                 :             : 
     211                 :        4175 :                 funcctx = SRF_FIRSTCALL_INIT();
     212                 :             : 
     213         [ +  + ]:        4175 :                 if (!check_rel_can_be_partition(relid))
     214         [ +  - ]:        2493 :                         SRF_RETURN_DONE(funcctx);
     215                 :             : 
     216                 :        1682 :                 oldcxt = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
     217                 :             : 
     218                 :        1682 :                 ancestors = get_partition_ancestors(relid);
     219                 :        1682 :                 ancestors = lcons_oid(relid, ancestors);
     220                 :             : 
     221                 :             :                 /* The only state we need is the ancestors list */
     222                 :        1682 :                 funcctx->user_fctx = ancestors;
     223                 :             : 
     224                 :        1682 :                 MemoryContextSwitchTo(oldcxt);
     225         [ +  + ]:        4175 :         }
     226                 :             : 
     227                 :        4711 :         funcctx = SRF_PERCALL_SETUP();
     228                 :        4711 :         ancestors = (List *) funcctx->user_fctx;
     229                 :             : 
     230         [ +  + ]:        4711 :         if (funcctx->call_cntr < list_length(ancestors))
     231                 :             :         {
     232                 :        3031 :                 Oid                     resultrel = list_nth_oid(ancestors, funcctx->call_cntr);
     233                 :             : 
     234                 :        3031 :                 SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(resultrel));
     235         [ +  - ]:        3031 :         }
     236                 :             : 
     237         [ +  - ]:        1680 :         SRF_RETURN_DONE(funcctx);
     238         [ -  + ]:        7204 : }
        

Generated by: LCOV version 2.3.2-1