LCOV - code coverage report
Current view: top level - src/backend/utils/cache - partcache.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 123 128 96.1 %
Date: 2020-06-03 11:07:14 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * partcache.c
       4             :  *      Support routines for manipulating partition information cached in
       5             :  *      relcache
       6             :  *
       7             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *        src/backend/utils/cache/partcache.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             : */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/hash.h"
      18             : #include "access/htup_details.h"
      19             : #include "access/nbtree.h"
      20             : #include "access/relation.h"
      21             : #include "catalog/partition.h"
      22             : #include "catalog/pg_inherits.h"
      23             : #include "catalog/pg_opclass.h"
      24             : #include "catalog/pg_partitioned_table.h"
      25             : #include "miscadmin.h"
      26             : #include "nodes/makefuncs.h"
      27             : #include "nodes/nodeFuncs.h"
      28             : #include "optimizer/optimizer.h"
      29             : #include "partitioning/partbounds.h"
      30             : #include "rewrite/rewriteHandler.h"
      31             : #include "utils/builtins.h"
      32             : #include "utils/datum.h"
      33             : #include "utils/lsyscache.h"
      34             : #include "utils/memutils.h"
      35             : #include "utils/partcache.h"
      36             : #include "utils/rel.h"
      37             : #include "utils/syscache.h"
      38             : 
      39             : 
      40             : static void RelationBuildPartitionKey(Relation relation);
      41             : static List *generate_partition_qual(Relation rel);
      42             : 
      43             : /*
      44             :  * RelationGetPartitionKey -- get partition key, if relation is partitioned
      45             :  *
      46             :  * Note: partition keys are not allowed to change after the partitioned rel
      47             :  * is created.  RelationClearRelation knows this and preserves rd_partkey
      48             :  * across relcache rebuilds, as long as the relation is open.  Therefore,
      49             :  * even though we hand back a direct pointer into the relcache entry, it's
      50             :  * safe for callers to continue to use that pointer as long as they hold
      51             :  * the relation open.
      52             :  */
      53             : PartitionKey
      54       60992 : RelationGetPartitionKey(Relation rel)
      55             : {
      56       60992 :     if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
      57           8 :         return NULL;
      58             : 
      59       60984 :     if (unlikely(rel->rd_partkey == NULL))
      60        8528 :         RelationBuildPartitionKey(rel);
      61             : 
      62       60984 :     return rel->rd_partkey;
      63             : }
      64             : 
      65             : /*
      66             :  * RelationBuildPartitionKey
      67             :  *      Build partition key data of relation, and attach to relcache
      68             :  *
      69             :  * Partitioning key data is a complex structure; to avoid complicated logic to
      70             :  * free individual elements whenever the relcache entry is flushed, we give it
      71             :  * its own memory context, a child of CacheMemoryContext, which can easily be
      72             :  * deleted on its own.  To avoid leaking memory in that context in case of an
      73             :  * error partway through this function, the context is initially created as a
      74             :  * child of CurTransactionContext and only re-parented to CacheMemoryContext
      75             :  * at the end, when no further errors are possible.  Also, we don't make this
      76             :  * context the current context except in very brief code sections, out of fear
      77             :  * that some of our callees allocate memory on their own which would be leaked
      78             :  * permanently.
      79             :  */
      80             : static void
      81        8528 : RelationBuildPartitionKey(Relation relation)
      82             : {
      83             :     Form_pg_partitioned_table form;
      84             :     HeapTuple   tuple;
      85             :     bool        isnull;
      86             :     int         i;
      87             :     PartitionKey key;
      88             :     AttrNumber *attrs;
      89             :     oidvector  *opclass;
      90             :     oidvector  *collation;
      91             :     ListCell   *partexprs_item;
      92             :     Datum       datum;
      93             :     MemoryContext partkeycxt,
      94             :                 oldcxt;
      95             :     int16       procnum;
      96             : 
      97        8528 :     tuple = SearchSysCache1(PARTRELID,
      98        8528 :                             ObjectIdGetDatum(RelationGetRelid(relation)));
      99             : 
     100        8528 :     if (!HeapTupleIsValid(tuple))
     101           0 :         elog(ERROR, "cache lookup failed for partition key of relation %u",
     102             :              RelationGetRelid(relation));
     103             : 
     104        8528 :     partkeycxt = AllocSetContextCreate(CurTransactionContext,
     105             :                                        "partition key",
     106             :                                        ALLOCSET_SMALL_SIZES);
     107        8528 :     MemoryContextCopyAndSetIdentifier(partkeycxt,
     108             :                                       RelationGetRelationName(relation));
     109             : 
     110        8528 :     key = (PartitionKey) MemoryContextAllocZero(partkeycxt,
     111             :                                                 sizeof(PartitionKeyData));
     112             : 
     113             :     /* Fixed-length attributes */
     114        8528 :     form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
     115        8528 :     key->strategy = form->partstrat;
     116        8528 :     key->partnatts = form->partnatts;
     117             : 
     118             :     /*
     119             :      * We can rely on the first variable-length attribute being mapped to the
     120             :      * relevant field of the catalog's C struct, because all previous
     121             :      * attributes are non-nullable and fixed-length.
     122             :      */
     123        8528 :     attrs = form->partattrs.values;
     124             : 
     125             :     /* But use the hard way to retrieve further variable-length attributes */
     126             :     /* Operator class */
     127        8528 :     datum = SysCacheGetAttr(PARTRELID, tuple,
     128             :                             Anum_pg_partitioned_table_partclass, &isnull);
     129             :     Assert(!isnull);
     130        8528 :     opclass = (oidvector *) DatumGetPointer(datum);
     131             : 
     132             :     /* Collation */
     133        8528 :     datum = SysCacheGetAttr(PARTRELID, tuple,
     134             :                             Anum_pg_partitioned_table_partcollation, &isnull);
     135             :     Assert(!isnull);
     136        8528 :     collation = (oidvector *) DatumGetPointer(datum);
     137             : 
     138             :     /* Expressions */
     139        8528 :     datum = SysCacheGetAttr(PARTRELID, tuple,
     140             :                             Anum_pg_partitioned_table_partexprs, &isnull);
     141        8528 :     if (!isnull)
     142             :     {
     143             :         char       *exprString;
     144             :         Node       *expr;
     145             : 
     146         608 :         exprString = TextDatumGetCString(datum);
     147         608 :         expr = stringToNode(exprString);
     148         608 :         pfree(exprString);
     149             : 
     150             :         /*
     151             :          * Run the expressions through const-simplification since the planner
     152             :          * will be comparing them to similarly-processed qual clause operands,
     153             :          * and may fail to detect valid matches without this step; fix
     154             :          * opfuncids while at it.  We don't need to bother with
     155             :          * canonicalize_qual() though, because partition expressions should be
     156             :          * in canonical form already (ie, no need for OR-merging or constant
     157             :          * elimination).
     158             :          */
     159         608 :         expr = eval_const_expressions(NULL, expr);
     160         608 :         fix_opfuncids(expr);
     161             : 
     162         608 :         oldcxt = MemoryContextSwitchTo(partkeycxt);
     163         608 :         key->partexprs = (List *) copyObject(expr);
     164         608 :         MemoryContextSwitchTo(oldcxt);
     165             :     }
     166             : 
     167             :     /* Allocate assorted arrays in the partkeycxt, which we'll fill below */
     168        8528 :     oldcxt = MemoryContextSwitchTo(partkeycxt);
     169        8528 :     key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
     170        8528 :     key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
     171        8528 :     key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
     172        8528 :     key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
     173             : 
     174        8528 :     key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
     175        8528 :     key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
     176        8528 :     key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
     177        8528 :     key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
     178        8528 :     key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
     179        8528 :     key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
     180        8528 :     key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
     181        8528 :     MemoryContextSwitchTo(oldcxt);
     182             : 
     183             :     /* determine support function number to search for */
     184        8528 :     procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
     185             :         HASHEXTENDED_PROC : BTORDER_PROC;
     186             : 
     187             :     /* Copy partattrs and fill other per-attribute info */
     188        8528 :     memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
     189        8528 :     partexprs_item = list_head(key->partexprs);
     190       18044 :     for (i = 0; i < key->partnatts; i++)
     191             :     {
     192        9516 :         AttrNumber  attno = key->partattrs[i];
     193             :         HeapTuple   opclasstup;
     194             :         Form_pg_opclass opclassform;
     195             :         Oid         funcid;
     196             : 
     197             :         /* Collect opfamily information */
     198        9516 :         opclasstup = SearchSysCache1(CLAOID,
     199        9516 :                                      ObjectIdGetDatum(opclass->values[i]));
     200        9516 :         if (!HeapTupleIsValid(opclasstup))
     201           0 :             elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
     202             : 
     203        9516 :         opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
     204        9516 :         key->partopfamily[i] = opclassform->opcfamily;
     205        9516 :         key->partopcintype[i] = opclassform->opcintype;
     206             : 
     207             :         /* Get a support function for the specified opfamily and datatypes */
     208        9516 :         funcid = get_opfamily_proc(opclassform->opcfamily,
     209             :                                    opclassform->opcintype,
     210             :                                    opclassform->opcintype,
     211             :                                    procnum);
     212        9516 :         if (!OidIsValid(funcid))
     213           0 :             ereport(ERROR,
     214             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     215             :                      errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
     216             :                             NameStr(opclassform->opcname),
     217             :                             (key->strategy == PARTITION_STRATEGY_HASH) ?
     218             :                             "hash" : "btree",
     219             :                             procnum,
     220             :                             format_type_be(opclassform->opcintype))));
     221             : 
     222        9516 :         fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
     223             : 
     224             :         /* Collation */
     225        9516 :         key->partcollation[i] = collation->values[i];
     226             : 
     227             :         /* Collect type information */
     228        9516 :         if (attno != 0)
     229             :         {
     230        8860 :             Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
     231             : 
     232        8860 :             key->parttypid[i] = att->atttypid;
     233        8860 :             key->parttypmod[i] = att->atttypmod;
     234        8860 :             key->parttypcoll[i] = att->attcollation;
     235             :         }
     236             :         else
     237             :         {
     238         656 :             if (partexprs_item == NULL)
     239           0 :                 elog(ERROR, "wrong number of partition key expressions");
     240             : 
     241         656 :             key->parttypid[i] = exprType(lfirst(partexprs_item));
     242         656 :             key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
     243         656 :             key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
     244             : 
     245         656 :             partexprs_item = lnext(key->partexprs, partexprs_item);
     246             :         }
     247       28548 :         get_typlenbyvalalign(key->parttypid[i],
     248        9516 :                              &key->parttyplen[i],
     249        9516 :                              &key->parttypbyval[i],
     250        9516 :                              &key->parttypalign[i]);
     251             : 
     252        9516 :         ReleaseSysCache(opclasstup);
     253             :     }
     254             : 
     255        8528 :     ReleaseSysCache(tuple);
     256             : 
     257             :     /* Assert that we're not leaking any old data during assignments below */
     258             :     Assert(relation->rd_partkeycxt == NULL);
     259             :     Assert(relation->rd_partkey == NULL);
     260             : 
     261             :     /*
     262             :      * Success --- reparent our context and make the relcache point to the
     263             :      * newly constructed key
     264             :      */
     265        8528 :     MemoryContextSetParent(partkeycxt, CacheMemoryContext);
     266        8528 :     relation->rd_partkeycxt = partkeycxt;
     267        8528 :     relation->rd_partkey = key;
     268        8528 : }
     269             : 
     270             : /*
     271             :  * RelationGetPartitionQual
     272             :  *
     273             :  * Returns a list of partition quals
     274             :  */
     275             : List *
     276      110284 : RelationGetPartitionQual(Relation rel)
     277             : {
     278             :     /* Quick exit */
     279      110284 :     if (!rel->rd_rel->relispartition)
     280      101848 :         return NIL;
     281             : 
     282        8436 :     return generate_partition_qual(rel);
     283             : }
     284             : 
     285             : /*
     286             :  * get_partition_qual_relid
     287             :  *
     288             :  * Returns an expression tree describing the passed-in relation's partition
     289             :  * constraint.
     290             :  *
     291             :  * If the relation is not found, or is not a partition, or there is no
     292             :  * partition constraint, return NULL.  We must guard against the first two
     293             :  * cases because this supports a SQL function that could be passed any OID.
     294             :  * The last case can happen even if relispartition is true, when a default
     295             :  * partition is the only partition.
     296             :  */
     297             : Expr *
     298         128 : get_partition_qual_relid(Oid relid)
     299             : {
     300         128 :     Expr       *result = NULL;
     301             : 
     302             :     /* Do the work only if this relation exists and is a partition. */
     303         128 :     if (get_rel_relispartition(relid))
     304             :     {
     305         128 :         Relation    rel = relation_open(relid, AccessShareLock);
     306             :         List       *and_args;
     307             : 
     308         128 :         and_args = generate_partition_qual(rel);
     309             : 
     310             :         /* Convert implicit-AND list format to boolean expression */
     311         128 :         if (and_args == NIL)
     312          12 :             result = NULL;
     313         116 :         else if (list_length(and_args) > 1)
     314         108 :             result = makeBoolExpr(AND_EXPR, and_args, -1);
     315             :         else
     316           8 :             result = linitial(and_args);
     317             : 
     318             :         /* Keep the lock, to allow safe deparsing against the rel by caller. */
     319         128 :         relation_close(rel, NoLock);
     320             :     }
     321             : 
     322         128 :     return result;
     323             : }
     324             : 
     325             : /*
     326             :  * generate_partition_qual
     327             :  *
     328             :  * Generate partition predicate from rel's partition bound expression. The
     329             :  * function returns a NIL list if there is no predicate.
     330             :  *
     331             :  * We cache a copy of the result in the relcache entry, after constructing
     332             :  * it using the caller's context.  This approach avoids leaking any data
     333             :  * into long-lived cache contexts, especially if we fail partway through.
     334             :  */
     335             : static List *
     336        9514 : generate_partition_qual(Relation rel)
     337             : {
     338             :     HeapTuple   tuple;
     339             :     MemoryContext oldcxt;
     340             :     Datum       boundDatum;
     341             :     bool        isnull;
     342        9514 :     List       *my_qual = NIL,
     343        9514 :                *result = NIL;
     344             :     Relation    parent;
     345             : 
     346             :     /* Guard against stack overflow due to overly deep partition tree */
     347        9514 :     check_stack_depth();
     348             : 
     349             :     /* If we already cached the result, just return a copy */
     350        9514 :     if (rel->rd_partcheckvalid)
     351        5348 :         return copyObject(rel->rd_partcheck);
     352             : 
     353             :     /* Grab at least an AccessShareLock on the parent table */
     354        4166 :     parent = relation_open(get_partition_parent(RelationGetRelid(rel)),
     355             :                            AccessShareLock);
     356             : 
     357             :     /* Get pg_class.relpartbound */
     358        4166 :     tuple = SearchSysCache1(RELOID, RelationGetRelid(rel));
     359        4166 :     if (!HeapTupleIsValid(tuple))
     360           0 :         elog(ERROR, "cache lookup failed for relation %u",
     361             :              RelationGetRelid(rel));
     362             : 
     363        4166 :     boundDatum = SysCacheGetAttr(RELOID, tuple,
     364             :                                  Anum_pg_class_relpartbound,
     365             :                                  &isnull);
     366        4166 :     if (!isnull)
     367             :     {
     368             :         PartitionBoundSpec *bound;
     369             : 
     370        4158 :         bound = castNode(PartitionBoundSpec,
     371             :                          stringToNode(TextDatumGetCString(boundDatum)));
     372             : 
     373        4158 :         my_qual = get_qual_from_partbound(rel, parent, bound);
     374             :     }
     375             : 
     376        4166 :     ReleaseSysCache(tuple);
     377             : 
     378             :     /* Add the parent's quals to the list (if any) */
     379        4166 :     if (parent->rd_rel->relispartition)
     380         950 :         result = list_concat(generate_partition_qual(parent), my_qual);
     381             :     else
     382        3216 :         result = my_qual;
     383             : 
     384             :     /*
     385             :      * Change Vars to have partition's attnos instead of the parent's. We do
     386             :      * this after we concatenate the parent's quals, because we want every Var
     387             :      * in it to bear this relation's attnos. It's safe to assume varno = 1
     388             :      * here.
     389             :      */
     390        4166 :     result = map_partition_varattnos(result, 1, rel, parent);
     391             : 
     392             :     /* Assert that we're not leaking any old data during assignments below */
     393             :     Assert(rel->rd_partcheckcxt == NULL);
     394             :     Assert(rel->rd_partcheck == NIL);
     395             : 
     396             :     /*
     397             :      * Save a copy in the relcache.  The order of these operations is fairly
     398             :      * critical to avoid memory leaks and ensure that we don't leave a corrupt
     399             :      * relcache entry if we fail partway through copyObject.
     400             :      *
     401             :      * If, as is definitely possible, the partcheck list is NIL, then we do
     402             :      * not need to make a context to hold it.
     403             :      */
     404        4166 :     if (result != NIL)
     405             :     {
     406        4138 :         rel->rd_partcheckcxt = AllocSetContextCreate(CacheMemoryContext,
     407             :                                                      "partition constraint",
     408             :                                                      ALLOCSET_SMALL_SIZES);
     409        4138 :         MemoryContextCopyAndSetIdentifier(rel->rd_partcheckcxt,
     410             :                                           RelationGetRelationName(rel));
     411        4138 :         oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
     412        4138 :         rel->rd_partcheck = copyObject(result);
     413        4138 :         MemoryContextSwitchTo(oldcxt);
     414             :     }
     415             :     else
     416          28 :         rel->rd_partcheck = NIL;
     417        4166 :     rel->rd_partcheckvalid = true;
     418             : 
     419             :     /* Keep the parent locked until commit */
     420        4166 :     relation_close(parent, NoLock);
     421             : 
     422             :     /* Return the working copy to the caller */
     423        4166 :     return result;
     424             : }

Generated by: LCOV version 1.13