LCOV - code coverage report
Current view: top level - src/backend/catalog - partition.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 102 105 97.1 %
Date: 2019-11-22 07:06:56 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * partition.c
       4             :  *        Partitioning related data structures and functions.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *        src/backend/catalog/partition.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             : */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/genam.h"
      18             : #include "access/htup_details.h"
      19             : #include "access/sysattr.h"
      20             : #include "access/table.h"
      21             : #include "access/tupconvert.h"
      22             : #include "catalog/indexing.h"
      23             : #include "catalog/partition.h"
      24             : #include "catalog/pg_inherits.h"
      25             : #include "catalog/pg_partitioned_table.h"
      26             : #include "nodes/makefuncs.h"
      27             : #include "optimizer/optimizer.h"
      28             : #include "partitioning/partbounds.h"
      29             : #include "rewrite/rewriteManip.h"
      30             : #include "utils/fmgroids.h"
      31             : #include "utils/partcache.h"
      32             : #include "utils/rel.h"
      33             : #include "utils/syscache.h"
      34             : 
      35             : static Oid  get_partition_parent_worker(Relation inhRel, Oid relid);
      36             : static void get_partition_ancestors_worker(Relation inhRel, Oid relid,
      37             :                                            List **ancestors);
      38             : 
      39             : /*
      40             :  * get_partition_parent
      41             :  *      Obtain direct parent of given relation
      42             :  *
      43             :  * Returns inheritance parent of a partition by scanning pg_inherits
      44             :  *
      45             :  * Note: Because this function assumes that the relation whose OID is passed
      46             :  * as an argument will have precisely one parent, it should only be called
      47             :  * when it is known that the relation is a partition.
      48             :  */
      49             : Oid
      50        7254 : get_partition_parent(Oid relid)
      51             : {
      52             :     Relation    catalogRelation;
      53             :     Oid         result;
      54             : 
      55        7254 :     catalogRelation = table_open(InheritsRelationId, AccessShareLock);
      56             : 
      57        7254 :     result = get_partition_parent_worker(catalogRelation, relid);
      58             : 
      59        7254 :     if (!OidIsValid(result))
      60           0 :         elog(ERROR, "could not find tuple for parent of relation %u", relid);
      61             : 
      62        7254 :     table_close(catalogRelation, AccessShareLock);
      63             : 
      64        7254 :     return result;
      65             : }
      66             : 
      67             : /*
      68             :  * get_partition_parent_worker
      69             :  *      Scan the pg_inherits relation to return the OID of the parent of the
      70             :  *      given relation
      71             :  */
      72             : static Oid
      73       11632 : get_partition_parent_worker(Relation inhRel, Oid relid)
      74             : {
      75             :     SysScanDesc scan;
      76             :     ScanKeyData key[2];
      77       11632 :     Oid         result = InvalidOid;
      78             :     HeapTuple   tuple;
      79             : 
      80       11632 :     ScanKeyInit(&key[0],
      81             :                 Anum_pg_inherits_inhrelid,
      82             :                 BTEqualStrategyNumber, F_OIDEQ,
      83             :                 ObjectIdGetDatum(relid));
      84       11632 :     ScanKeyInit(&key[1],
      85             :                 Anum_pg_inherits_inhseqno,
      86             :                 BTEqualStrategyNumber, F_INT4EQ,
      87             :                 Int32GetDatum(1));
      88             : 
      89       11632 :     scan = systable_beginscan(inhRel, InheritsRelidSeqnoIndexId, true,
      90             :                               NULL, 2, key);
      91       11632 :     tuple = systable_getnext(scan);
      92       11632 :     if (HeapTupleIsValid(tuple))
      93             :     {
      94        9158 :         Form_pg_inherits form = (Form_pg_inherits) GETSTRUCT(tuple);
      95             : 
      96        9158 :         result = form->inhparent;
      97             :     }
      98             : 
      99       11632 :     systable_endscan(scan);
     100             : 
     101       11632 :     return result;
     102             : }
     103             : 
     104             : /*
     105             :  * get_partition_ancestors
     106             :  *      Obtain ancestors of given relation
     107             :  *
     108             :  * Returns a list of ancestors of the given relation.
     109             :  *
     110             :  * Note: Because this function assumes that the relation whose OID is passed
     111             :  * as an argument and each ancestor will have precisely one parent, it should
     112             :  * only be called when it is known that the relation is a partition.
     113             :  */
     114             : List *
     115        2474 : get_partition_ancestors(Oid relid)
     116             : {
     117        2474 :     List       *result = NIL;
     118             :     Relation    inhRel;
     119             : 
     120        2474 :     inhRel = table_open(InheritsRelationId, AccessShareLock);
     121             : 
     122        2474 :     get_partition_ancestors_worker(inhRel, relid, &result);
     123             : 
     124        2474 :     table_close(inhRel, AccessShareLock);
     125             : 
     126        2474 :     return result;
     127             : }
     128             : 
     129             : /*
     130             :  * get_partition_ancestors_worker
     131             :  *      recursive worker for get_partition_ancestors
     132             :  */
     133             : static void
     134        4378 : get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
     135             : {
     136             :     Oid         parentOid;
     137             : 
     138             :     /* Recursion ends at the topmost level, ie., when there's no parent */
     139        4378 :     parentOid = get_partition_parent_worker(inhRel, relid);
     140        4378 :     if (parentOid == InvalidOid)
     141        2474 :         return;
     142             : 
     143        1904 :     *ancestors = lappend_oid(*ancestors, parentOid);
     144        1904 :     get_partition_ancestors_worker(inhRel, parentOid, ancestors);
     145             : }
     146             : 
     147             : /*
     148             :  * index_get_partition
     149             :  *      Return the OID of index of the given partition that is a child
     150             :  *      of the given index, or InvalidOid if there isn't one.
     151             :  */
     152             : Oid
     153         416 : index_get_partition(Relation partition, Oid indexId)
     154             : {
     155         416 :     List       *idxlist = RelationGetIndexList(partition);
     156             :     ListCell   *l;
     157             : 
     158         738 :     foreach(l, idxlist)
     159             :     {
     160         546 :         Oid         partIdx = lfirst_oid(l);
     161             :         HeapTuple   tup;
     162             :         Form_pg_class classForm;
     163             :         bool        ispartition;
     164             : 
     165         546 :         tup = SearchSysCache1(RELOID, ObjectIdGetDatum(partIdx));
     166         546 :         if (!HeapTupleIsValid(tup))
     167           0 :             elog(ERROR, "cache lookup failed for relation %u", partIdx);
     168         546 :         classForm = (Form_pg_class) GETSTRUCT(tup);
     169         546 :         ispartition = classForm->relispartition;
     170         546 :         ReleaseSysCache(tup);
     171         546 :         if (!ispartition)
     172         310 :             continue;
     173         236 :         if (get_partition_parent(lfirst_oid(l)) == indexId)
     174             :         {
     175         224 :             list_free(idxlist);
     176         224 :             return partIdx;
     177             :         }
     178             :     }
     179             : 
     180         192 :     return InvalidOid;
     181             : }
     182             : 
     183             : /*
     184             :  * map_partition_varattnos - maps varattno of any Vars in expr from the
     185             :  * attno's of 'from_rel' to the attno's of 'to_rel' partition, each of which
     186             :  * may be either a leaf partition or a partitioned table, but both of which
     187             :  * must be from the same partitioning hierarchy.
     188             :  *
     189             :  * Even though all of the same column names must be present in all relations
     190             :  * in the hierarchy, and they must also have the same types, the attnos may
     191             :  * be different.
     192             :  *
     193             :  * If found_whole_row is not NULL, *found_whole_row returns whether a
     194             :  * whole-row variable was found in the input expression.
     195             :  *
     196             :  * Note: this will work on any node tree, so really the argument and result
     197             :  * should be declared "Node *".  But a substantial majority of the callers
     198             :  * are working on Lists, so it's less messy to do the casts internally.
     199             :  */
     200             : List *
     201        5442 : map_partition_varattnos(List *expr, int fromrel_varno,
     202             :                         Relation to_rel, Relation from_rel,
     203             :                         bool *found_whole_row)
     204             : {
     205        5442 :     bool        my_found_whole_row = false;
     206             : 
     207        5442 :     if (expr != NIL)
     208             :     {
     209             :         AttrNumber *part_attnos;
     210             : 
     211        5210 :         part_attnos = convert_tuples_by_name_map(RelationGetDescr(to_rel),
     212             :                                                  RelationGetDescr(from_rel));
     213       10420 :         expr = (List *) map_variable_attnos((Node *) expr,
     214             :                                             fromrel_varno, 0,
     215             :                                             part_attnos,
     216        5210 :                                             RelationGetDescr(from_rel)->natts,
     217        5210 :                                             RelationGetForm(to_rel)->reltype,
     218             :                                             &my_found_whole_row);
     219             :     }
     220             : 
     221        5442 :     if (found_whole_row)
     222        5048 :         *found_whole_row = my_found_whole_row;
     223             : 
     224        5442 :     return expr;
     225             : }
     226             : 
     227             : /*
     228             :  * Checks if any of the 'attnums' is a partition key attribute for rel
     229             :  *
     230             :  * Sets *used_in_expr if any of the 'attnums' is found to be referenced in some
     231             :  * partition key expression.  It's possible for a column to be both used
     232             :  * directly and as part of an expression; if that happens, *used_in_expr may
     233             :  * end up as either true or false.  That's OK for current uses of this
     234             :  * function, because *used_in_expr is only used to tailor the error message
     235             :  * text.
     236             :  */
     237             : bool
     238        9952 : has_partition_attrs(Relation rel, Bitmapset *attnums, bool *used_in_expr)
     239             : {
     240             :     PartitionKey key;
     241             :     int         partnatts;
     242             :     List       *partexprs;
     243             :     ListCell   *partexprs_item;
     244             :     int         i;
     245             : 
     246        9952 :     if (attnums == NULL || rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
     247        8842 :         return false;
     248             : 
     249        1110 :     key = RelationGetPartitionKey(rel);
     250        1110 :     partnatts = get_partition_natts(key);
     251        1110 :     partexprs = get_partition_exprs(key);
     252             : 
     253        1110 :     partexprs_item = list_head(partexprs);
     254        1900 :     for (i = 0; i < partnatts; i++)
     255             :     {
     256        1206 :         AttrNumber  partattno = get_partition_col_attnum(key, i);
     257             : 
     258        1206 :         if (partattno != 0)
     259             :         {
     260        1130 :             if (bms_is_member(partattno - FirstLowInvalidHeapAttributeNumber,
     261             :                               attnums))
     262             :             {
     263         392 :                 if (used_in_expr)
     264          20 :                     *used_in_expr = false;
     265         392 :                 return true;
     266             :             }
     267             :         }
     268             :         else
     269             :         {
     270             :             /* Arbitrary expression */
     271          76 :             Node       *expr = (Node *) lfirst(partexprs_item);
     272          76 :             Bitmapset  *expr_attrs = NULL;
     273             : 
     274             :             /* Find all attributes referenced */
     275          76 :             pull_varattnos(expr, 1, &expr_attrs);
     276          76 :             partexprs_item = lnext(partexprs, partexprs_item);
     277             : 
     278          76 :             if (bms_overlap(attnums, expr_attrs))
     279             :             {
     280          24 :                 if (used_in_expr)
     281          12 :                     *used_in_expr = true;
     282          24 :                 return true;
     283             :             }
     284             :         }
     285             :     }
     286             : 
     287         694 :     return false;
     288             : }
     289             : 
     290             : /*
     291             :  * get_default_partition_oid
     292             :  *
     293             :  * Given a relation OID, return the OID of the default partition, if one
     294             :  * exists.  Use get_default_oid_from_partdesc where possible, for
     295             :  * efficiency.
     296             :  */
     297             : Oid
     298        4382 : get_default_partition_oid(Oid parentId)
     299             : {
     300             :     HeapTuple   tuple;
     301        4382 :     Oid         defaultPartId = InvalidOid;
     302             : 
     303        4382 :     tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(parentId));
     304             : 
     305        4382 :     if (HeapTupleIsValid(tuple))
     306             :     {
     307             :         Form_pg_partitioned_table part_table_form;
     308             : 
     309        4382 :         part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
     310        4382 :         defaultPartId = part_table_form->partdefid;
     311        4382 :         ReleaseSysCache(tuple);
     312             :     }
     313             : 
     314        4382 :     return defaultPartId;
     315             : }
     316             : 
     317             : /*
     318             :  * update_default_partition_oid
     319             :  *
     320             :  * Update pg_partitioned_table.partdefid with a new default partition OID.
     321             :  */
     322             : void
     323         400 : update_default_partition_oid(Oid parentId, Oid defaultPartId)
     324             : {
     325             :     HeapTuple   tuple;
     326             :     Relation    pg_partitioned_table;
     327             :     Form_pg_partitioned_table part_table_form;
     328             : 
     329         400 :     pg_partitioned_table = table_open(PartitionedRelationId, RowExclusiveLock);
     330             : 
     331         400 :     tuple = SearchSysCacheCopy1(PARTRELID, ObjectIdGetDatum(parentId));
     332             : 
     333         400 :     if (!HeapTupleIsValid(tuple))
     334           0 :         elog(ERROR, "cache lookup failed for partition key of relation %u",
     335             :              parentId);
     336             : 
     337         400 :     part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
     338         400 :     part_table_form->partdefid = defaultPartId;
     339         400 :     CatalogTupleUpdate(pg_partitioned_table, &tuple->t_self, tuple);
     340             : 
     341         400 :     heap_freetuple(tuple);
     342         400 :     table_close(pg_partitioned_table, RowExclusiveLock);
     343         400 : }
     344             : 
     345             : /*
     346             :  * get_proposed_default_constraint
     347             :  *
     348             :  * This function returns the negation of new_part_constraints, which
     349             :  * would be an integral part of the default partition constraints after
     350             :  * addition of the partition to which the new_part_constraints belongs.
     351             :  */
     352             : List *
     353         298 : get_proposed_default_constraint(List *new_part_constraints)
     354             : {
     355             :     Expr       *defPartConstraint;
     356             : 
     357         298 :     defPartConstraint = make_ands_explicit(new_part_constraints);
     358             : 
     359             :     /*
     360             :      * Derive the partition constraints of default partition by negating the
     361             :      * given partition constraints. The partition constraint never evaluates
     362             :      * to NULL, so negating it like this is safe.
     363             :      */
     364         298 :     defPartConstraint = makeBoolExpr(NOT_EXPR,
     365         298 :                                      list_make1(defPartConstraint),
     366             :                                      -1);
     367             : 
     368             :     /* Simplify, to put the negated expression into canonical form */
     369         298 :     defPartConstraint =
     370             :         (Expr *) eval_const_expressions(NULL,
     371             :                                         (Node *) defPartConstraint);
     372         298 :     defPartConstraint = canonicalize_qual(defPartConstraint, true);
     373             : 
     374         298 :     return make_ands_implicit(defPartConstraint);
     375             : }

Generated by: LCOV version 1.13