LCOV - code coverage report
Current view: top level - src/backend/utils/adt - partitionfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15beta1 Lines: 78 78 100.0 %
Date: 2022-05-18 04:09:35 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14