PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
partition.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "access/sysattr.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaddress.h"
#include "catalog/partition.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_inherits_fn.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/parsenodes.h"
#include "optimizer/clauses.h"
#include "optimizer/planmain.h"
#include "optimizer/var.h"
#include "rewrite/rewriteManip.h"
#include "storage/lmgr.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/memutils.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/ruleutils.h"
#include "utils/syscache.h"
Include dependency graph for partition.c:

Go to the source code of this file.

Data Structures

struct  PartitionBoundInfoData
 
struct  PartitionListValue
 
struct  PartitionRangeBound
 

Macros

#define APPEND_REL_PARTITION_OIDS(rel, partoids, parents)
 

Typedefs

typedef enum RangeDatumContent RangeDatumContent
 
typedef struct
PartitionBoundInfoData 
PartitionBoundInfoData
 
typedef struct PartitionListValue PartitionListValue
 
typedef struct PartitionRangeBound PartitionRangeBound
 

Enumerations

enum  RangeDatumContent { RANGE_DATUM_FINITE = 0, RANGE_DATUM_NEG_INF, RANGE_DATUM_POS_INF }
 

Functions

static int32 qsort_partition_list_value_cmp (const void *a, const void *b, void *arg)
 
static int32 qsort_partition_rbound_cmp (const void *a, const void *b, void *arg)
 
static Listget_qual_for_list (PartitionKey key, PartitionBoundSpec *spec)
 
static Listget_qual_for_range (PartitionKey key, PartitionBoundSpec *spec)
 
static Oid get_partition_operator (PartitionKey key, int col, StrategyNumber strategy, bool *need_relabel)
 
static Listgenerate_partition_qual (Relation rel)
 
static PartitionRangeBoundmake_one_range_bound (PartitionKey key, int index, List *datums, bool lower)
 
static int32 partition_rbound_cmp (PartitionKey key, Datum *datums1, RangeDatumContent *content1, bool lower1, PartitionRangeBound *b2)
 
static int32 partition_rbound_datum_cmp (PartitionKey key, Datum *rb_datums, RangeDatumContent *rb_content, Datum *tuple_datums)
 
static int32 partition_bound_cmp (PartitionKey key, PartitionBoundInfo boundinfo, int offset, void *probe, bool probe_is_bound)
 
static int partition_bound_bsearch (PartitionKey key, PartitionBoundInfo boundinfo, void *probe, bool probe_is_bound, bool *is_equal)
 
void RelationBuildPartitionDesc (Relation rel)
 
bool partition_bounds_equal (PartitionKey key, PartitionBoundInfo b1, PartitionBoundInfo b2)
 
void check_new_partition_bound (char *relname, Relation parent, Node *bound)
 
Oid get_partition_parent (Oid relid)
 
Listget_qual_from_partbound (Relation rel, Relation parent, Node *bound)
 
Listmap_partition_varattnos (List *expr, int target_varno, Relation partrel, Relation parent)
 
ListRelationGetPartitionQual (Relation rel)
 
PartitionDispatchRelationGetPartitionDispatchInfo (Relation rel, int lockmode, int *num_parted, List **leaf_part_oids)
 
void FormPartitionKeyDatum (PartitionDispatch pd, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
 
int get_partition_for_tuple (PartitionDispatch *pd, TupleTableSlot *slot, EState *estate, PartitionDispatchData **failed_at, TupleTableSlot **failed_slot)
 

Macro Definition Documentation

#define APPEND_REL_PARTITION_OIDS (   rel,
  partoids,
  parents 
)
Value:
do\
{\
int i;\
for (i = 0; i < (rel)->rd_partdesc->nparts; i++)\
{\
(partoids) = lappend_oid((partoids), (rel)->rd_partdesc->oids[i]);\
(parents) = lappend((parents), (rel));\
}\
} while(0)
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
List * lappend(List *list, void *datum)
Definition: list.c:128
int i

Definition at line 971 of file partition.c.

Referenced by RelationGetPartitionDispatchInfo().

Typedef Documentation

Enumeration Type Documentation

Enumerator
RANGE_DATUM_FINITE 
RANGE_DATUM_NEG_INF 
RANGE_DATUM_POS_INF 

Definition at line 70 of file partition.c.

71 {
72  RANGE_DATUM_FINITE = 0, /* actual datum stored elsewhere */
73  RANGE_DATUM_NEG_INF, /* negative infinity */
74  RANGE_DATUM_POS_INF /* positive infinity */
RangeDatumContent
Definition: partition.c:70

Function Documentation

void check_new_partition_bound ( char *  relname,
Relation  parent,
Node bound 
)

Definition at line 669 of file partition.c.

References Assert, PartitionDescData::boundinfo, Const::constisnull, Const::constvalue, PartitionRangeBound::content, PartitionRangeBound::datums, elog, equal(), ereport, errcode(), errmsg(), ERROR, get_rel_name(), PartitionBoundInfoData::has_null, PartitionBoundInfoData::indexes, lfirst, PartitionBoundSpec::listdatums, PartitionBoundSpec::location, lower(), PartitionBoundSpec::lowerdatums, make_one_range_bound(), make_parsestate(), PartitionBoundInfoData::ndatums, PartitionDescData::nparts, NULL, PartitionBoundInfoData::null_index, PartitionDescData::oids, parser_errposition(), partition_bound_bsearch(), partition_rbound_cmp(), PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, RelationGetPartitionDesc, RelationGetPartitionKey, PartitionKeyData::strategy, PartitionBoundInfoData::strategy, PartitionBoundSpec::strategy, upper(), PartitionBoundSpec::upperdatums, and val.

Referenced by ATExecAttachPartition(), and DefineRelation().

670 {
671  PartitionBoundSpec *spec = (PartitionBoundSpec *) bound;
673  PartitionDesc partdesc = RelationGetPartitionDesc(parent);
674  ParseState *pstate = make_parsestate(NULL);
675  int with = -1;
676  bool overlap = false;
677 
678  switch (key->strategy)
679  {
681  {
683 
684  if (partdesc->nparts > 0)
685  {
686  PartitionBoundInfo boundinfo = partdesc->boundinfo;
687  ListCell *cell;
688 
689  Assert(boundinfo &&
690  boundinfo->strategy == PARTITION_STRATEGY_LIST &&
691  (boundinfo->ndatums > 0 || boundinfo->has_null));
692 
693  foreach(cell, spec->listdatums)
694  {
695  Const *val = lfirst(cell);
696 
697  if (!val->constisnull)
698  {
699  int offset;
700  bool equal;
701 
702  offset = partition_bound_bsearch(key, boundinfo,
703  &val->constvalue,
704  true, &equal);
705  if (offset >= 0 && equal)
706  {
707  overlap = true;
708  with = boundinfo->indexes[offset];
709  break;
710  }
711  }
712  else if (boundinfo->has_null)
713  {
714  overlap = true;
715  with = boundinfo->null_index;
716  break;
717  }
718  }
719  }
720 
721  break;
722  }
723 
725  {
727  *upper;
728 
730  lower = make_one_range_bound(key, -1, spec->lowerdatums, true);
731  upper = make_one_range_bound(key, -1, spec->upperdatums, false);
732 
733  /*
734  * First check if the resulting range would be empty with
735  * specified lower and upper bounds
736  */
737  if (partition_rbound_cmp(key, lower->datums, lower->content, true,
738  upper) >= 0)
739  ereport(ERROR,
740  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
741  errmsg("cannot create range partition with empty range"),
742  parser_errposition(pstate, spec->location)));
743 
744  if (partdesc->nparts > 0)
745  {
746  PartitionBoundInfo boundinfo = partdesc->boundinfo;
747  int off1,
748  off2;
749  bool equal = false;
750 
751  Assert(boundinfo && boundinfo->ndatums > 0 &&
752  boundinfo->strategy == PARTITION_STRATEGY_RANGE);
753 
754  /*
755  * Firstly, find the greatest range bound that is less
756  * than or equal to the new lower bound.
757  */
758  off1 = partition_bound_bsearch(key, boundinfo, lower, true,
759  &equal);
760 
761  /*
762  * off1 == -1 means that all existing bounds are greater
763  * than the new lower bound. In that case and the case
764  * where no partition is defined between the bounds at
765  * off1 and off1 + 1, we have a "gap" in the range that
766  * could be occupied by the new partition. We confirm if
767  * so by checking whether the new upper bound is confined
768  * within the gap.
769  */
770  if (!equal && boundinfo->indexes[off1 + 1] < 0)
771  {
772  off2 = partition_bound_bsearch(key, boundinfo, upper,
773  true, &equal);
774 
775  /*
776  * If the new upper bound is returned to be equal to
777  * the bound at off2, the latter must be the upper
778  * bound of some partition with which the new
779  * partition clearly overlaps.
780  *
781  * Also, if bound at off2 is not same as the one
782  * returned for the new lower bound (IOW, off1 !=
783  * off2), then the new partition overlaps at least one
784  * partition.
785  */
786  if (equal || off1 != off2)
787  {
788  overlap = true;
789 
790  /*
791  * The bound at off2 could be the lower bound of
792  * the partition with which the new partition
793  * overlaps. In that case, use the upper bound
794  * (that is, the bound at off2 + 1) to get the
795  * index of that partition.
796  */
797  if (boundinfo->indexes[off2] < 0)
798  with = boundinfo->indexes[off2 + 1];
799  else
800  with = boundinfo->indexes[off2];
801  }
802  }
803  else
804  {
805  /*
806  * Equal has been set to true and there is no "gap"
807  * between the bound at off1 and that at off1 + 1, so
808  * the new partition will overlap some partition. In
809  * the former case, the new lower bound is found to be
810  * equal to the bound at off1, which could only ever
811  * be true if the latter is the lower bound of some
812  * partition. It's clear in such a case that the new
813  * partition overlaps that partition, whose index we
814  * get using its upper bound (that is, using the bound
815  * at off1 + 1).
816  */
817  overlap = true;
818  with = boundinfo->indexes[off1 + 1];
819  }
820  }
821 
822  break;
823  }
824 
825  default:
826  elog(ERROR, "unexpected partition strategy: %d",
827  (int) key->strategy);
828  }
829 
830  if (overlap)
831  {
832  Assert(with >= 0);
833  ereport(ERROR,
834  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
835  errmsg("partition \"%s\" would overlap partition \"%s\"",
836  relname, get_rel_name(partdesc->oids[with])),
837  parser_errposition(pstate, spec->location)));
838  }
839 }
Datum constvalue
Definition: primnodes.h:196
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:74
char strategy
Definition: rel.h:54
PartitionBoundInfo boundinfo
Definition: partition.h:37
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
#define ERROR
Definition: elog.h:43
static int32 partition_rbound_cmp(PartitionKey key, Datum *datums1, RangeDatumContent *content1, bool lower1, PartitionRangeBound *b2)
Definition: partition.c:1871
static int partition_bound_bsearch(PartitionKey key, PartitionBoundInfo boundinfo, void *probe, bool probe_is_bound, bool *is_equal)
Definition: partition.c:2019
#define ereport(elevel, rest)
Definition: elog.h:122
static PartitionRangeBound * make_one_range_bound(PartitionKey key, int index, List *datums, bool lower)
Definition: partition.c:1814
RangeDatumContent * content
Definition: partition.c:112
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:769
#define RelationGetPartitionKey(relation)
Definition: rel.h:585
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:770
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
long val
Definition: informix.c:689
bool constisnull
Definition: primnodes.h:197
#define RelationGetPartitionDesc(relation)
Definition: rel.h:633
void FormPartitionKeyDatum ( PartitionDispatch  pd,
TupleTableSlot slot,
EState estate,
Datum values,
bool isnull 
)

Definition at line 1605 of file partition.c.

References Assert, elog, ERROR, ExecEvalExprSwitchContext(), ExecPrepareExpr(), GetPerTupleExprContext, i, PartitionDispatchData::key, PartitionDispatchData::keystate, lfirst, list_head(), lnext, NIL, NULL, PartitionKeyData::partattrs, PartitionKeyData::partexprs, PartitionKeyData::partnatts, and slot_getattr().

Referenced by ExecFindPartition(), and get_partition_for_tuple().

1610 {
1611  ListCell *partexpr_item;
1612  int i;
1613 
1614  if (pd->key->partexprs != NIL && pd->keystate == NIL)
1615  {
1616  /* Check caller has set up context correctly */
1617  Assert(estate != NULL &&
1618  GetPerTupleExprContext(estate)->ecxt_scantuple == slot);
1619 
1620  /* First time through, set up expression evaluation state */
1621  pd->keystate = (List *) ExecPrepareExpr((Expr *) pd->key->partexprs,
1622  estate);
1623  }
1624 
1625  partexpr_item = list_head(pd->keystate);
1626  for (i = 0; i < pd->key->partnatts; i++)
1627  {
1628  AttrNumber keycol = pd->key->partattrs[i];
1629  Datum datum;
1630  bool isNull;
1631 
1632  if (keycol != 0)
1633  {
1634  /* Plain column; get the value directly from the heap tuple */
1635  datum = slot_getattr(slot, keycol, &isNull);
1636  }
1637  else
1638  {
1639  /* Expression; need to evaluate it */
1640  if (partexpr_item == NULL)
1641  elog(ERROR, "wrong number of partition key expressions");
1642  datum = ExecEvalExprSwitchContext((ExprState *) lfirst(partexpr_item),
1643  GetPerTupleExprContext(estate),
1644  &isNull);
1645  partexpr_item = lnext(partexpr_item);
1646  }
1647  values[i] = datum;
1648  isnull[i] = isNull;
1649  }
1650 
1651  if (partexpr_item != NULL)
1652  elog(ERROR, "wrong number of partition key expressions");
1653 }
#define NIL
Definition: pg_list.h:69
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execQual.c:5005
List * partexprs
Definition: rel.h:58
Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext, bool *isNull)
Definition: execQual.c:4220
#define GetPerTupleExprContext(estate)
Definition: executor.h:339
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
AttrNumber * partattrs
Definition: rel.h:56
uintptr_t Datum
Definition: postgres.h:372
int16 partnatts
Definition: rel.h:55
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int i
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1143
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
PartitionKey key
Definition: partition.h:63
static List * generate_partition_qual ( Relation  rel)
static

Definition at line 1524 of file partition.c.

References AccessShareLock, Anum_pg_class_relpartbound, CacheMemoryContext, check_stack_depth(), copyObject(), elog, ERROR, get_partition_parent(), get_qual_from_partbound(), heap_close, heap_open(), HeapTupleIsValid, list_concat(), map_partition_varattnos(), MemoryContextSwitchTo(), NIL, NoLock, RelationData::rd_partcheck, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), RELOID, result, SearchSysCache1, stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by RelationGetPartitionQual().

1525 {
1526  HeapTuple tuple;
1527  MemoryContext oldcxt;
1528  Datum boundDatum;
1529  bool isnull;
1530  Node *bound;
1531  List *my_qual = NIL,
1532  *result = NIL;
1533  Relation parent;
1534 
1535  /* Guard against stack overflow due to overly deep partition tree */
1537 
1538  /* Quick copy */
1539  if (rel->rd_partcheck != NIL)
1540  return copyObject(rel->rd_partcheck);
1541 
1542  /* Grab at least an AccessShareLock on the parent table */
1544  AccessShareLock);
1545 
1546  /* Get pg_class.relpartbound */
1547  tuple = SearchSysCache1(RELOID, RelationGetRelid(rel));
1548  if (!HeapTupleIsValid(tuple))
1549  elog(ERROR, "cache lookup failed for relation %u",
1550  RelationGetRelid(rel));
1551 
1552  boundDatum = SysCacheGetAttr(RELOID, tuple,
1554  &isnull);
1555  if (isnull) /* should not happen */
1556  elog(ERROR, "relation \"%s\" has relpartbound = null",
1558  bound = stringToNode(TextDatumGetCString(boundDatum));
1559  ReleaseSysCache(tuple);
1560 
1561  my_qual = get_qual_from_partbound(rel, parent, bound);
1562 
1563  /* Add the parent's quals to the list (if any) */
1564  if (parent->rd_rel->relispartition)
1565  result = list_concat(generate_partition_qual(parent), my_qual);
1566  else
1567  result = my_qual;
1568 
1569  /*
1570  * Change Vars to have partition's attnos instead of the parent's. We do
1571  * this after we concatenate the parent's quals, because we want every Var
1572  * in it to bear this relation's attnos. It's safe to assume varno = 1
1573  * here.
1574  */
1575  result = map_partition_varattnos(result, 1, rel, parent);
1576 
1577  /* Save a copy in the relcache */
1579  rel->rd_partcheck = copyObject(result);
1580  MemoryContextSwitchTo(oldcxt);
1581 
1582  /* Keep the parent locked until commit */
1583  heap_close(parent, NoLock);
1584 
1585  return result;
1586 }
#define NIL
Definition: pg_list.h:69
void * stringToNode(char *str)
Definition: read.c:38
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:522
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1618
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:114
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
#define ERROR
Definition: elog.h:43
Oid get_partition_parent(Oid relid)
Definition: partition.c:851
#define NoLock
Definition: lockdefs.h:34
void check_stack_depth(void)
Definition: postgres.c:3098
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
List * get_qual_from_partbound(Relation rel, Relation parent, Node *bound)
Definition: partition.c:892
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1278
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define Anum_pg_class_relpartbound
Definition: pg_class.h:135
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
static List * generate_partition_qual(Relation rel)
Definition: partition.c:1524
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:417
List * rd_partcheck
Definition: rel.h:132
List * map_partition_varattnos(List *expr, int target_varno, Relation partrel, Relation parent)
Definition: partition.c:928
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
int get_partition_for_tuple ( PartitionDispatch pd,
TupleTableSlot slot,
EState estate,
PartitionDispatchData **  failed_at,
TupleTableSlot **  failed_slot 
)

Definition at line 1665 of file partition.c.

References PartitionDescData::boundinfo, do_convert_tuple(), ExprContext::ecxt_scantuple, elog, equal(), ereport, errcode(), errmsg(), ERROR, ExecClearTuple(), ExecFetchSlotTuple(), ExecStoreTuple(), FormPartitionKeyDatum(), GetPerTupleExprContext, PartitionBoundInfoData::has_null, i, PartitionDispatchData::indexes, PartitionBoundInfoData::indexes, InvalidBuffer, PartitionDispatchData::key, PartitionDescData::nparts, NULL, PartitionBoundInfoData::null_index, PartitionDispatchData::partdesc, partition_bound_bsearch(), PARTITION_MAX_KEYS, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, PartitionKeyData::partnatts, result, PartitionKeyData::strategy, PartitionDispatchData::tupmap, PartitionDispatchData::tupslot, and values.

Referenced by ExecFindPartition().

1670 {
1671  PartitionDispatch parent;
1673  bool isnull[PARTITION_MAX_KEYS];
1674  int cur_offset,
1675  cur_index;
1676  int i,
1677  result;
1678  ExprContext *ecxt = GetPerTupleExprContext(estate);
1679  TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
1680 
1681  /* start with the root partitioned table */
1682  parent = pd[0];
1683  while (true)
1684  {
1685  PartitionKey key = parent->key;
1686  PartitionDesc partdesc = parent->partdesc;
1687  TupleTableSlot *myslot = parent->tupslot;
1688  TupleConversionMap *map = parent->tupmap;
1689 
1690  if (myslot != NULL && map != NULL)
1691  {
1692  HeapTuple tuple = ExecFetchSlotTuple(slot);
1693 
1694  ExecClearTuple(myslot);
1695  tuple = do_convert_tuple(tuple, map);
1696  ExecStoreTuple(tuple, myslot, InvalidBuffer, true);
1697  slot = myslot;
1698  }
1699 
1700  /* Quick exit */
1701  if (partdesc->nparts == 0)
1702  {
1703  *failed_at = parent;
1704  *failed_slot = slot;
1705  return -1;
1706  }
1707 
1708  /*
1709  * Extract partition key from tuple. Expression evaluation machinery
1710  * that FormPartitionKeyDatum() invokes expects ecxt_scantuple to
1711  * point to the correct tuple slot. The slot might have changed from
1712  * what was used for the parent table if the table of the current
1713  * partitioning level has different tuple descriptor from the parent.
1714  * So update ecxt_scantuple accordingly.
1715  */
1716  ecxt->ecxt_scantuple = slot;
1717  FormPartitionKeyDatum(parent, slot, estate, values, isnull);
1718 
1719  if (key->strategy == PARTITION_STRATEGY_RANGE)
1720  {
1721  /* Disallow nulls in the range partition key of the tuple */
1722  for (i = 0; i < key->partnatts; i++)
1723  if (isnull[i])
1724  ereport(ERROR,
1725  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1726  errmsg("range partition key of row contains null")));
1727  }
1728 
1729  if (partdesc->boundinfo->has_null && isnull[0])
1730  /* Tuple maps to the null-accepting list partition */
1731  cur_index = partdesc->boundinfo->null_index;
1732  else
1733  {
1734  /* Else bsearch in partdesc->boundinfo */
1735  bool equal = false;
1736 
1737  cur_offset = partition_bound_bsearch(key, partdesc->boundinfo,
1738  values, false, &equal);
1739  switch (key->strategy)
1740  {
1742  if (cur_offset >= 0 && equal)
1743  cur_index = partdesc->boundinfo->indexes[cur_offset];
1744  else
1745  cur_index = -1;
1746  break;
1747 
1749 
1750  /*
1751  * Offset returned is such that the bound at offset is
1752  * found to be less or equal with the tuple. So, the bound
1753  * at offset+1 would be the upper bound.
1754  */
1755  cur_index = partdesc->boundinfo->indexes[cur_offset + 1];
1756  break;
1757 
1758  default:
1759  elog(ERROR, "unexpected partition strategy: %d",
1760  (int) key->strategy);
1761  }
1762  }
1763 
1764  /*
1765  * cur_index < 0 means we failed to find a partition of this parent.
1766  * cur_index >= 0 means we either found the leaf partition, or the
1767  * next parent to find a partition of.
1768  */
1769  if (cur_index < 0)
1770  {
1771  result = -1;
1772  *failed_at = parent;
1773  *failed_slot = slot;
1774  break;
1775  }
1776  else if (parent->indexes[cur_index] >= 0)
1777  {
1778  result = parent->indexes[cur_index];
1779  break;
1780  }
1781  else
1782  parent = pd[-parent->indexes[cur_index]];
1783  }
1784 
1785  ecxt->ecxt_scantuple = ecxt_scantuple_old;
1786  return result;
1787 }
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:320
PartitionDesc partdesc
Definition: partition.h:65
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleConversionMap * tupmap
Definition: partition.h:67
#define InvalidBuffer
Definition: buf.h:25
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PARTITION_MAX_KEYS
return result
Definition: formatting.c:1618
char strategy
Definition: rel.h:54
PartitionBoundInfo boundinfo
Definition: partition.h:37
#define GetPerTupleExprContext(estate)
Definition: executor.h:339
#define ERROR
Definition: elog.h:43
static int partition_bound_bsearch(PartitionKey key, PartitionBoundInfo boundinfo, void *probe, bool probe_is_bound, bool *is_equal)
Definition: partition.c:2019
void FormPartitionKeyDatum(PartitionDispatch pd, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: partition.c:1605
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
int16 partnatts
Definition: rel.h:55
TupleTableSlot * tupslot
Definition: partition.h:66
#define NULL
Definition: c.h:229
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:130
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:769
HeapTuple do_convert_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:341
HeapTuple ExecFetchSlotTuple(TupleTableSlot *slot)
Definition: execTuples.c:618
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:770
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define elog
Definition: elog.h:219
PartitionKey key
Definition: partition.h:63
static Oid get_partition_operator ( PartitionKey  key,
int  col,
StrategyNumber  strategy,
bool need_relabel 
)
static

Definition at line 1469 of file partition.c.

References elog, ERROR, get_opfamily_member(), OidIsValid, PartitionKeyData::partopcintype, PartitionKeyData::partopfamily, and PartitionKeyData::parttypid.

Referenced by get_qual_for_list(), and get_qual_for_range().

1471 {
1472  Oid operoid;
1473 
1474  /*
1475  * First check if there exists an operator of the given strategy, with
1476  * this column's type as both its lefttype and righttype, in the
1477  * partitioning operator family specified for the column.
1478  */
1479  operoid = get_opfamily_member(key->partopfamily[col],
1480  key->parttypid[col],
1481  key->parttypid[col],
1482  strategy);
1483 
1484  /*
1485  * If one doesn't exist, we must resort to using an operator in the same
1486  * opreator family but with the operator class declared input type. It is
1487  * OK to do so, because the column's type is known to be binary-coercible
1488  * with the operator class input type (otherwise, the operator class in
1489  * question would not have been accepted as the partitioning operator
1490  * class). We must however inform the caller to wrap the non-Const
1491  * expression with a RelabelType node to denote the implicit coercion. It
1492  * ensures that the resulting expression structurally matches similarly
1493  * processed expressions within the optimizer.
1494  */
1495  if (!OidIsValid(operoid))
1496  {
1497  operoid = get_opfamily_member(key->partopfamily[col],
1498  key->partopcintype[col],
1499  key->partopcintype[col],
1500  strategy);
1501  *need_relabel = true;
1502  }
1503  else
1504  *need_relabel = false;
1505 
1506  if (!OidIsValid(operoid))
1507  elog(ERROR, "could not find operator for partitioning");
1508 
1509  return operoid;
1510 }
Oid * partopfamily
Definition: rel.h:61
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define ERROR
Definition: elog.h:43
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
Oid * parttypid
Definition: rel.h:69
Oid * partopcintype
Definition: rel.h:62
#define elog
Definition: elog.h:219
Oid get_partition_parent ( Oid  relid)

Definition at line 851 of file partition.c.

References AccessShareLock, Anum_pg_inherits_inhrelid, Anum_pg_inherits_inhseqno, Assert, BTEqualStrategyNumber, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, InheritsRelationId, InheritsRelidSeqnoIndexId, Int32GetDatum, NULL, ObjectIdGetDatum, result, ScanKeyInit(), systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by ATExecDropNotNull(), generate_partition_qual(), and heap_drop_with_catalog().

852 {
853  Form_pg_inherits form;
854  Relation catalogRelation;
855  SysScanDesc scan;
856  ScanKeyData key[2];
857  HeapTuple tuple;
858  Oid result;
859 
860  catalogRelation = heap_open(InheritsRelationId, AccessShareLock);
861 
862  ScanKeyInit(&key[0],
864  BTEqualStrategyNumber, F_OIDEQ,
865  ObjectIdGetDatum(relid));
866  ScanKeyInit(&key[1],
868  BTEqualStrategyNumber, F_INT4EQ,
869  Int32GetDatum(1));
870 
871  scan = systable_beginscan(catalogRelation, InheritsRelidSeqnoIndexId, true,
872  NULL, 2, key);
873 
874  tuple = systable_getnext(scan);
875  Assert(HeapTupleIsValid(tuple));
876 
877  form = (Form_pg_inherits) GETSTRUCT(tuple);
878  result = form->inhparent;
879 
880  systable_endscan(scan);
881  heap_close(catalogRelation, AccessShareLock);
882 
883  return result;
884 }
#define Anum_pg_inherits_inhrelid
Definition: pg_inherits.h:50
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define AccessShareLock
Definition: lockdefs.h:36
return result
Definition: formatting.c:1618
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define Anum_pg_inherits_inhseqno
Definition: pg_inherits.h:52
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define InheritsRelidSeqnoIndexId
Definition: indexing.h:167
FormData_pg_inherits * Form_pg_inherits
Definition: pg_inherits.h:43
#define InheritsRelationId
Definition: pg_inherits.h:29
#define Int32GetDatum(X)
Definition: postgres.h:485
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static List * get_qual_for_list ( PartitionKey  key,
PartitionBoundSpec spec 
)
static

Definition at line 1150 of file partition.c.

References NullTest::arg, NullTest::argisrow, ScalarArrayOpExpr::args, ArrayExpr::array_collid, ArrayExpr::array_typeid, BTEqualStrategyNumber, COERCE_EXPLICIT_CAST, Const::constisnull, copyObject(), ArrayExpr::element_typeid, ArrayExpr::elements, get_array_type(), get_opcode(), get_partition_operator(), ScalarArrayOpExpr::inputcollid, IS_NOT_NULL, IS_NULL, lfirst, linitial, list_delete_cell(), list_head(), list_make1, list_make2, PartitionBoundSpec::listdatums, lnext, ScalarArrayOpExpr::location, ArrayExpr::location, NullTest::location, makeBoolExpr(), makeNode, makeRelabelType(), makeVar(), ArrayExpr::multidims, next, NULL, NullTest::nulltesttype, ScalarArrayOpExpr::opfuncid, ScalarArrayOpExpr::opno, OR_EXPR, PartitionKeyData::partattrs, PartitionKeyData::partcollation, PartitionKeyData::partexprs, PartitionKeyData::partopcintype, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::parttypmod, result, type_is_array, ScalarArrayOpExpr::useOr, and val.

Referenced by get_qual_from_partbound().

1151 {
1152  List *result;
1153  ArrayExpr *arr;
1154  ScalarArrayOpExpr *opexpr;
1155  ListCell *cell,
1156  *prev,
1157  *next;
1158  Node *keyCol;
1159  Oid operoid;
1160  bool need_relabel,
1161  list_has_null = false;
1162  NullTest *nulltest1 = NULL,
1163  *nulltest2 = NULL;
1164 
1165  /* Left operand is either a simple Var or arbitrary expression */
1166  if (key->partattrs[0] != 0)
1167  keyCol = (Node *) makeVar(1,
1168  key->partattrs[0],
1169  key->parttypid[0],
1170  key->parttypmod[0],
1171  key->parttypcoll[0],
1172  0);
1173  else
1174  keyCol = (Node *) copyObject(linitial(key->partexprs));
1175 
1176  /*
1177  * We must remove any NULL value in the list; we handle it separately
1178  * below.
1179  */
1180  prev = NULL;
1181  for (cell = list_head(spec->listdatums); cell; cell = next)
1182  {
1183  Const *val = (Const *) lfirst(cell);
1184 
1185  next = lnext(cell);
1186 
1187  if (val->constisnull)
1188  {
1189  list_has_null = true;
1190  spec->listdatums = list_delete_cell(spec->listdatums,
1191  cell, prev);
1192  }
1193  else
1194  prev = cell;
1195  }
1196 
1197  if (!list_has_null)
1198  {
1199  /*
1200  * Gin up a col IS NOT NULL test that will be AND'd with other
1201  * expressions
1202  */
1203  nulltest1 = makeNode(NullTest);
1204  nulltest1->arg = (Expr *) keyCol;
1205  nulltest1->nulltesttype = IS_NOT_NULL;
1206  nulltest1->argisrow = false;
1207  nulltest1->location = -1;
1208  }
1209  else
1210  {
1211  /*
1212  * Gin up a col IS NULL test that will be OR'd with other expressions
1213  */
1214  nulltest2 = makeNode(NullTest);
1215  nulltest2->arg = (Expr *) keyCol;
1216  nulltest2->nulltesttype = IS_NULL;
1217  nulltest2->argisrow = false;
1218  nulltest2->location = -1;
1219  }
1220 
1221  /* Right operand is an ArrayExpr containing this partition's values */
1222  arr = makeNode(ArrayExpr);
1223  arr->array_typeid = !type_is_array(key->parttypid[0])
1224  ? get_array_type(key->parttypid[0])
1225  : key->parttypid[0];
1226  arr->array_collid = key->parttypcoll[0];
1227  arr->element_typeid = key->parttypid[0];
1228  arr->elements = spec->listdatums;
1229  arr->multidims = false;
1230  arr->location = -1;
1231 
1232  /* Get the correct btree equality operator */
1234  &need_relabel);
1235  if (need_relabel || key->partcollation[0] != key->parttypcoll[0])
1236  keyCol = (Node *) makeRelabelType((Expr *) keyCol,
1237  key->partopcintype[0],
1238  -1,
1239  key->partcollation[0],
1241 
1242  /* Build leftop = ANY (rightop) */
1243  opexpr = makeNode(ScalarArrayOpExpr);
1244  opexpr->opno = operoid;
1245  opexpr->opfuncid = get_opcode(operoid);
1246  opexpr->useOr = true;
1247  opexpr->inputcollid = key->partcollation[0];
1248  opexpr->args = list_make2(keyCol, arr);
1249  opexpr->location = -1;
1250 
1251  if (nulltest1)
1252  result = list_make2(nulltest1, opexpr);
1253  else if (nulltest2)
1254  {
1255  Expr *or;
1256 
1257  or = makeBoolExpr(OR_EXPR, list_make2(nulltest2, opexpr), -1);
1258  result = list_make1(or);
1259  }
1260  else
1261  result = list_make1(opexpr);
1262 
1263  return result;
1264 }
#define list_make2(x1, x2)
Definition: pg_list.h:134
bool multidims
Definition: primnodes.h:954
static int32 next
Definition: blutils.c:210
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2480
static Oid get_partition_operator(PartitionKey key, int col, StrategyNumber strategy, bool *need_relabel)
Definition: partition.c:1469
Definition: nodes.h:522
Oid array_typeid
Definition: primnodes.h:950
return result
Definition: formatting.c:1618
List * partexprs
Definition: rel.h:58
unsigned int Oid
Definition: postgres_ext.h:31
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:366
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
#define list_make1(x1)
Definition: pg_list.h:133
Oid * parttypcoll
Definition: rel.h:74
#define linitial(l)
Definition: pg_list.h:110
Expr * arg
Definition: primnodes.h:1178
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
List * elements
Definition: primnodes.h:953
#define lnext(lc)
Definition: pg_list.h:105
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
Oid * parttypid
Definition: rel.h:69
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Oid * partcollation
Definition: rel.h:66
int location
Definition: primnodes.h:955
AttrNumber * partattrs
Definition: rel.h:56
NullTestType nulltesttype
Definition: primnodes.h:1179
int32 * parttypmod
Definition: rel.h:70
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
#define makeNode(_type_)
Definition: nodes.h:570
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Oid array_collid
Definition: primnodes.h:951
int location
Definition: primnodes.h:1181
#define type_is_array(typid)
Definition: lsyscache.h:163
Oid element_typeid
Definition: primnodes.h:952
Oid * partopcintype
Definition: rel.h:62
bool argisrow
Definition: primnodes.h:1180
Definition: pg_list.h:45
long val
Definition: informix.c:689
bool constisnull
Definition: primnodes.h:197
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static List * get_qual_for_range ( PartitionKey  key,
PartitionBoundSpec spec 
)
static

Definition at line 1272 of file partition.c.

References NullTest::arg, NullTest::argisrow, BOOLOID, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTLessStrategyNumber, COERCE_EXPLICIT_CAST, copyObject(), CreateExecutorState(), DatumGetBool, elog, ERROR, EState::es_query_cxt, ExecEvalExprSwitchContext(), ExecInitExpr(), fix_opfuncids(), forboth, FreeExecutorState(), get_partition_operator(), GetPerTupleExprContext, i, PartitionRangeDatum::infinite, InvalidOid, IS_NOT_NULL, IsA, lappend(), lfirst, list_head(), lnext, NullTest::location, PartitionBoundSpec::lowerdatums, make_opclause(), makeNode, makeRelabelType(), makeVar(), MemoryContextSwitchTo(), NIL, NULL, NullTest::nulltesttype, PartitionKeyData::partattrs, PartitionKeyData::partcollation, PartitionKeyData::partexprs, PartitionKeyData::partnatts, PartitionKeyData::partopcintype, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::parttypmod, result, PartitionBoundSpec::upperdatums, and PartitionRangeDatum::value.

Referenced by get_qual_from_partbound().

1273 {
1274  List *result = NIL;
1275  ListCell *cell1,
1276  *cell2,
1277  *partexprs_item;
1278  int i;
1279 
1280  /*
1281  * Iterate over columns of the key, emitting an OpExpr for each using the
1282  * corresponding lower and upper datums as constant operands.
1283  */
1284  i = 0;
1285  partexprs_item = list_head(key->partexprs);
1286  forboth(cell1, spec->lowerdatums, cell2, spec->upperdatums)
1287  {
1288  PartitionRangeDatum *ldatum = lfirst(cell1),
1289  *udatum = lfirst(cell2);
1290  Node *keyCol;
1291  Const *lower_val = NULL,
1292  *upper_val = NULL;
1293  EState *estate;
1294  MemoryContext oldcxt;
1295  Expr *test_expr;
1296  ExprState *test_exprstate;
1297  Datum test_result;
1298  bool isNull;
1299  bool need_relabel = false;
1300  Oid operoid;
1301  NullTest *nulltest;
1302 
1303  /* Left operand */
1304  if (key->partattrs[i] != 0)
1305  {
1306  keyCol = (Node *) makeVar(1,
1307  key->partattrs[i],
1308  key->parttypid[i],
1309  key->parttypmod[i],
1310  key->parttypcoll[i],
1311  0);
1312  }
1313  else
1314  {
1315  keyCol = (Node *) copyObject(lfirst(partexprs_item));
1316  partexprs_item = lnext(partexprs_item);
1317  }
1318 
1319  /*
1320  * Emit a IS NOT NULL expression for non-Var keys, because whereas
1321  * simple attributes are covered by NOT NULL constraints, expression
1322  * keys are still nullable which is not acceptable in case of range
1323  * partitioning.
1324  */
1325  if (!IsA(keyCol, Var))
1326  {
1327  nulltest = makeNode(NullTest);
1328  nulltest->arg = (Expr *) keyCol;
1329  nulltest->nulltesttype = IS_NOT_NULL;
1330  nulltest->argisrow = false;
1331  nulltest->location = -1;
1332  result = lappend(result, nulltest);
1333  }
1334 
1335  /*
1336  * Stop at this column if either of lower or upper datum is infinite,
1337  * but do emit an OpExpr for the non-infinite datum.
1338  */
1339  if (!ldatum->infinite)
1340  lower_val = (Const *) ldatum->value;
1341  if (!udatum->infinite)
1342  upper_val = (Const *) udatum->value;
1343 
1344  /*
1345  * If lower_val and upper_val are both finite and happen to be equal,
1346  * emit only (keyCol = lower_val) for this column, because all rows in
1347  * this partition could only ever contain this value (ie, lower_val)
1348  * in the current partitioning column. We must consider further
1349  * columns because the above condition does not fully constrain the
1350  * rows of this partition.
1351  */
1352  if (lower_val && upper_val)
1353  {
1354  /* Get the correct btree equality operator for the test */
1356  &need_relabel);
1357 
1358  /* Create the test expression */
1359  estate = CreateExecutorState();
1360  oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
1361  test_expr = make_opclause(operoid,
1362  BOOLOID,
1363  false,
1364  (Expr *) lower_val,
1365  (Expr *) upper_val,
1366  InvalidOid,
1367  key->partcollation[i]);
1368  fix_opfuncids((Node *) test_expr);
1369  test_exprstate = ExecInitExpr(test_expr, NULL);
1370  test_result = ExecEvalExprSwitchContext(test_exprstate,
1371  GetPerTupleExprContext(estate),
1372  &isNull);
1373  MemoryContextSwitchTo(oldcxt);
1374  FreeExecutorState(estate);
1375 
1376  if (DatumGetBool(test_result))
1377  {
1378  /* This can never be, but it's better to make sure */
1379  if (i == key->partnatts - 1)
1380  elog(ERROR, "invalid range bound specification");
1381 
1382  if (need_relabel || key->partcollation[i] != key->parttypcoll[i])
1383  keyCol = (Node *) makeRelabelType((Expr *) keyCol,
1384  key->partopcintype[i],
1385  -1,
1386  key->partcollation[i],
1388  result = lappend(result,
1389  make_opclause(operoid,
1390  BOOLOID,
1391  false,
1392  (Expr *) keyCol,
1393  (Expr *) lower_val,
1394  InvalidOid,
1395  key->partcollation[i]));
1396 
1397  /* Go over to consider the next column. */
1398  i++;
1399  continue;
1400  }
1401  }
1402 
1403  /*
1404  * We can say here that lower_val != upper_val. Emit expressions
1405  * (keyCol >= lower_val) and (keyCol < upper_val), then stop.
1406  */
1407  if (lower_val)
1408  {
1409  operoid = get_partition_operator(key, i,
1411  &need_relabel);
1412 
1413  if (need_relabel || key->partcollation[i] != key->parttypcoll[i])
1414  keyCol = (Node *) makeRelabelType((Expr *) keyCol,
1415  key->partopcintype[i],
1416  -1,
1417  key->partcollation[i],
1419  result = lappend(result,
1420  make_opclause(operoid,
1421  BOOLOID,
1422  false,
1423  (Expr *) keyCol,
1424  (Expr *) lower_val,
1425  InvalidOid,
1426  key->partcollation[i]));
1427  }
1428 
1429  if (upper_val)
1430  {
1431  operoid = get_partition_operator(key, i,
1433  &need_relabel);
1434 
1435  if (need_relabel || key->partcollation[i] != key->parttypcoll[i])
1436  keyCol = (Node *) makeRelabelType((Expr *) keyCol,
1437  key->partopcintype[i],
1438  -1,
1439  key->partcollation[i],
1441 
1442  result = lappend(result,
1443  make_opclause(operoid,
1444  BOOLOID,
1445  false,
1446  (Expr *) keyCol,
1447  (Expr *) upper_val,
1448  InvalidOid,
1449  key->partcollation[i]));
1450  }
1451 
1452  /*
1453  * We can stop at this column, because we would not have checked the
1454  * next column when routing a given row into this partition.
1455  */
1456  break;
1457  }
1458 
1459  return result;
1460 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1594
static Oid get_partition_operator(PartitionKey key, int col, StrategyNumber strategy, bool *need_relabel)
Definition: partition.c:1469
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:522
return result
Definition: formatting.c:1618
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:171
List * partexprs
Definition: rel.h:58
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext, bool *isNull)
Definition: execQual.c:4220
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
void FreeExecutorState(EState *estate)
Definition: execUtils.c:169
#define GetPerTupleExprContext(estate)
Definition: executor.h:339
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4267
MemoryContext es_query_cxt
Definition: execnodes.h:399
Oid * parttypcoll
Definition: rel.h:74
#define ERROR
Definition: elog.h:43
Expr * arg
Definition: primnodes.h:1178
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
#define DatumGetBool(X)
Definition: postgres.h:399
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
Oid * parttypid
Definition: rel.h:69
EState * CreateExecutorState(void)
Definition: execUtils.c:73
List * lappend(List *list, void *datum)
Definition: list.c:128
Oid * partcollation
Definition: rel.h:66
AttrNumber * partattrs
Definition: rel.h:56
uintptr_t Datum
Definition: postgres.h:372
int16 partnatts
Definition: rel.h:55
NullTestType nulltesttype
Definition: primnodes.h:1179
int32 * parttypmod
Definition: rel.h:70
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:570
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int location
Definition: primnodes.h:1181
#define BOOLOID
Definition: pg_type.h:288
Oid * partopcintype
Definition: rel.h:62
int i
bool argisrow
Definition: primnodes.h:1180
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:45
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
List* get_qual_from_partbound ( Relation  rel,
Relation  parent,
Node bound 
)

Definition at line 892 of file partition.c.

References Assert, elog, ERROR, get_qual_for_list(), get_qual_for_range(), NIL, NULL, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, RelationGetPartitionKey, PartitionKeyData::strategy, and PartitionBoundSpec::strategy.

Referenced by ATExecAttachPartition(), and generate_partition_qual().

893 {
894  PartitionBoundSpec *spec = (PartitionBoundSpec *) bound;
896  List *my_qual = NIL;
897 
898  Assert(key != NULL);
899 
900  switch (key->strategy)
901  {
904  my_qual = get_qual_for_list(key, spec);
905  break;
906 
909  my_qual = get_qual_for_range(key, spec);
910  break;
911 
912  default:
913  elog(ERROR, "unexpected partition strategy: %d",
914  (int) key->strategy);
915  }
916 
917  return my_qual;
918 }
#define NIL
Definition: pg_list.h:69
char strategy
Definition: rel.h:54
static List * get_qual_for_range(PartitionKey key, PartitionBoundSpec *spec)
Definition: partition.c:1272
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static List * get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
Definition: partition.c:1150
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:769
#define RelationGetPartitionKey(relation)
Definition: rel.h:585
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:770
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static PartitionRangeBound * make_one_range_bound ( PartitionKey  key,
int  index,
List datums,
bool  lower 
)
static

Definition at line 1814 of file partition.c.

References Const::constisnull, Const::constvalue, PartitionRangeBound::content, PartitionRangeBound::datums, elog, ERROR, i, PartitionRangeBound::index, PartitionRangeDatum::infinite, lfirst, lower(), PartitionRangeBound::lower, palloc0(), PartitionKeyData::partnatts, RANGE_DATUM_FINITE, RANGE_DATUM_NEG_INF, RANGE_DATUM_POS_INF, val, and PartitionRangeDatum::value.

Referenced by check_new_partition_bound(), and RelationBuildPartitionDesc().

1815 {
1816  PartitionRangeBound *bound;
1817  ListCell *cell;
1818  int i;
1819 
1820  bound = (PartitionRangeBound *) palloc0(sizeof(PartitionRangeBound));
1821  bound->index = index;
1822  bound->datums = (Datum *) palloc0(key->partnatts * sizeof(Datum));
1823  bound->content = (RangeDatumContent *) palloc0(key->partnatts *
1824  sizeof(RangeDatumContent));
1825  bound->lower = lower;
1826 
1827  i = 0;
1828  foreach(cell, datums)
1829  {
1830  PartitionRangeDatum *datum = lfirst(cell);
1831 
1832  /* What's contained in this range datum? */
1833  bound->content[i] = !datum->infinite
1837 
1838  if (bound->content[i] == RANGE_DATUM_FINITE)
1839  {
1840  Const *val = (Const *) datum->value;
1841 
1842  if (val->constisnull)
1843  elog(ERROR, "invalid range bound datum");
1844  bound->datums[i] = val->constvalue;
1845  }
1846 
1847  i++;
1848  }
1849 
1850  return bound;
1851 }
Datum constvalue
Definition: primnodes.h:196
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
Definition: type.h:90
#define ERROR
Definition: elog.h:43
RangeDatumContent
Definition: partition.c:70
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
int16 partnatts
Definition: rel.h:55
RangeDatumContent * content
Definition: partition.c:112
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define elog
Definition: elog.h:219
long val
Definition: informix.c:689
bool constisnull
Definition: primnodes.h:197
List* map_partition_varattnos ( List expr,
int  target_varno,
Relation  partrel,
Relation  parent 
)

Definition at line 928 of file partition.c.

References convert_tuples_by_name_map(), elog, ERROR, gettext_noop, map_variable_attnos(), NIL, and RelationGetDescr.

Referenced by ATExecAttachPartition(), ExecInitModifyTable(), generate_partition_qual(), and InitResultRelInfo().

930 {
931  AttrNumber *part_attnos;
932  bool found_whole_row;
933 
934  if (expr == NIL)
935  return NIL;
936 
937  part_attnos = convert_tuples_by_name_map(RelationGetDescr(partrel),
938  RelationGetDescr(parent),
939  gettext_noop("could not convert row type"));
940  expr = (List *) map_variable_attnos((Node *) expr,
941  target_varno, 0,
942  part_attnos,
943  RelationGetDescr(parent)->natts,
944  &found_whole_row);
945  /* There can never be a whole-row reference here */
946  if (found_whole_row)
947  elog(ERROR, "unexpected whole-row reference found in partition key");
948 
949  return expr;
950 }
Node * map_variable_attnos(Node *node, int target_varno, int sublevels_up, const AttrNumber *attno_map, int map_length, bool *found_whole_row)
#define NIL
Definition: pg_list.h:69
#define RelationGetDescr(relation)
Definition: rel.h:429
#define gettext_noop(x)
Definition: c.h:139
Definition: nodes.h:522
#define ERROR
Definition: elog.h:43
AttrNumber * convert_tuples_by_name_map(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: tupconvert.c:281
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
static int partition_bound_bsearch ( PartitionKey  key,
PartitionBoundInfo  boundinfo,
void *  probe,
bool  probe_is_bound,
bool is_equal 
)
static

Definition at line 2019 of file partition.c.

References PartitionBoundInfoData::ndatums, and partition_bound_cmp().

Referenced by check_new_partition_bound(), and get_partition_for_tuple().

2021 {
2022  int lo,
2023  hi,
2024  mid;
2025 
2026  lo = -1;
2027  hi = boundinfo->ndatums - 1;
2028  while (lo < hi)
2029  {
2030  int32 cmpval;
2031 
2032  mid = (lo + hi + 1) / 2;
2033  cmpval = partition_bound_cmp(key, boundinfo, mid, probe,
2034  probe_is_bound);
2035  if (cmpval <= 0)
2036  {
2037  lo = mid;
2038  *is_equal = (cmpval == 0);
2039 
2040  if (*is_equal)
2041  break;
2042  }
2043  else
2044  hi = mid - 1;
2045  }
2046 
2047  return lo;
2048 }
signed int int32
Definition: c.h:256
static int32 partition_bound_cmp(PartitionKey key, PartitionBoundInfo boundinfo, int offset, void *probe, bool probe_is_bound)
Definition: partition.c:1958
static int32 partition_bound_cmp ( PartitionKey  key,
PartitionBoundInfo  boundinfo,
int  offset,
void *  probe,
bool  probe_is_bound 
)
static

Definition at line 1958 of file partition.c.

References PartitionBoundInfoData::content, DatumGetInt32, PartitionBoundInfoData::datums, elog, ERROR, FunctionCall2Coll(), PartitionBoundInfoData::indexes, lower(), PartitionKeyData::partcollation, partition_rbound_cmp(), partition_rbound_datum_cmp(), PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, PartitionKeyData::partsupfunc, and PartitionKeyData::strategy.

Referenced by partition_bound_bsearch().

1960 {
1961  Datum *bound_datums = boundinfo->datums[offset];
1962  int32 cmpval = -1;
1963 
1964  switch (key->strategy)
1965  {
1967  cmpval = DatumGetInt32(FunctionCall2Coll(&key->partsupfunc[0],
1968  key->partcollation[0],
1969  bound_datums[0],
1970  *(Datum *) probe));
1971  break;
1972 
1974  {
1975  RangeDatumContent *content = boundinfo->content[offset];
1976 
1977  if (probe_is_bound)
1978  {
1979  /*
1980  * We need to pass whether the existing bound is a lower
1981  * bound, so that two equal-valued lower and upper bounds
1982  * are not regarded equal.
1983  */
1984  bool lower = boundinfo->indexes[offset] < 0;
1985 
1986  cmpval = partition_rbound_cmp(key,
1987  bound_datums, content, lower,
1988  (PartitionRangeBound *) probe);
1989  }
1990  else
1991  cmpval = partition_rbound_datum_cmp(key,
1992  bound_datums, content,
1993  (Datum *) probe);
1994  break;
1995  }
1996 
1997  default:
1998  elog(ERROR, "unexpected partition strategy: %d",
1999  (int) key->strategy);
2000  }
2001 
2002  return cmpval;
2003 }
static int32 partition_rbound_datum_cmp(PartitionKey key, Datum *rb_datums, RangeDatumContent *rb_content, Datum *tuple_datums)
Definition: partition.c:1928
#define DatumGetInt32(X)
Definition: postgres.h:478
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
FmgrInfo * partsupfunc
Definition: rel.h:63
char strategy
Definition: rel.h:54
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
static int32 partition_rbound_cmp(PartitionKey key, Datum *datums1, RangeDatumContent *content1, bool lower1, PartitionRangeBound *b2)
Definition: partition.c:1871
RangeDatumContent
Definition: partition.c:70
Oid * partcollation
Definition: rel.h:66
uintptr_t Datum
Definition: postgres.h:372
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:769
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:770
#define elog
Definition: elog.h:219
RangeDatumContent ** content
Definition: partition.c:83
bool partition_bounds_equal ( PartitionKey  key,
PartitionBoundInfo  b1,
PartitionBoundInfo  b2 
)

Definition at line 595 of file partition.c.

References PartitionBoundInfoData::content, datumIsEqual(), PartitionBoundInfoData::datums, PartitionBoundInfoData::has_null, i, PartitionBoundInfoData::indexes, PartitionBoundInfoData::ndatums, NULL, PartitionBoundInfoData::null_index, PARTITION_STRATEGY_RANGE, PartitionKeyData::partnatts, PartitionKeyData::parttypbyval, PartitionKeyData::parttyplen, RANGE_DATUM_FINITE, PartitionKeyData::strategy, and PartitionBoundInfoData::strategy.

Referenced by equalPartitionDescs().

597 {
598  int i;
599 
600  if (b1->strategy != b2->strategy)
601  return false;
602 
603  if (b1->ndatums != b2->ndatums)
604  return false;
605 
606  if (b1->has_null != b2->has_null)
607  return false;
608 
609  if (b1->null_index != b2->null_index)
610  return false;
611 
612  for (i = 0; i < b1->ndatums; i++)
613  {
614  int j;
615 
616  for (j = 0; j < key->partnatts; j++)
617  {
618  /* For range partitions, the bounds might not be finite. */
619  if (b1->content != NULL)
620  {
621  /*
622  * A finite bound always differs from an infinite bound, and
623  * different kinds of infinities differ from each other.
624  */
625  if (b1->content[i][j] != b2->content[i][j])
626  return false;
627 
628  /* Non-finite bounds are equal without further examination. */
629  if (b1->content[i][j] != RANGE_DATUM_FINITE)
630  continue;
631  }
632 
633  /*
634  * Compare the actual values. Note that it would be both incorrect
635  * and unsafe to invoke the comparison operator derived from the
636  * partitioning specification here. It would be incorrect because
637  * we want the relcache entry to be updated for ANY change to the
638  * partition bounds, not just those that the partitioning operator
639  * thinks are significant. It would be unsafe because we might
640  * reach this code in the context of an aborted transaction, and
641  * an arbitrary partitioning operator might not be safe in that
642  * context. datumIsEqual() should be simple enough to be safe.
643  */
644  if (!datumIsEqual(b1->datums[i][j], b2->datums[i][j],
645  key->parttypbyval[j],
646  key->parttyplen[j]))
647  return false;
648  }
649 
650  if (b1->indexes[i] != b2->indexes[i])
651  return false;
652  }
653 
654  /* There are ndatums+1 indexes in case of range partitions */
655  if (key->strategy == PARTITION_STRATEGY_RANGE &&
656  b1->indexes[i] != b2->indexes[i])
657  return false;
658 
659  return true;
660 }
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:219
char strategy
Definition: rel.h:54
int16 partnatts
Definition: rel.h:55
bool * parttypbyval
Definition: rel.h:72
#define NULL
Definition: c.h:229
int16 * parttyplen
Definition: rel.h:71
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:770
int i
RangeDatumContent ** content
Definition: partition.c:83
static int32 partition_rbound_cmp ( PartitionKey  key,
Datum datums1,
RangeDatumContent content1,
bool  lower1,
PartitionRangeBound b2 
)
static

Definition at line 1871 of file partition.c.

References PartitionRangeBound::content, DatumGetInt32, PartitionRangeBound::datums, FunctionCall2Coll(), i, PartitionRangeBound::lower, PartitionKeyData::partcollation, PartitionKeyData::partnatts, PartitionKeyData::partsupfunc, RANGE_DATUM_FINITE, and RANGE_DATUM_NEG_INF.

Referenced by check_new_partition_bound(), partition_bound_cmp(), and qsort_partition_rbound_cmp().

1874 {
1875  int32 cmpval = 0; /* placate compiler */
1876  int i;
1877  Datum *datums2 = b2->datums;
1878  RangeDatumContent *content2 = b2->content;
1879  bool lower2 = b2->lower;
1880 
1881  for (i = 0; i < key->partnatts; i++)
1882  {
1883  /*
1884  * First, handle cases involving infinity, which don't require
1885  * invoking the comparison proc.
1886  */
1887  if (content1[i] != RANGE_DATUM_FINITE &&
1888  content2[i] != RANGE_DATUM_FINITE)
1889 
1890  /*
1891  * Both are infinity, so they are equal unless one is negative
1892  * infinity and other positive (or vice versa)
1893  */
1894  return content1[i] == content2[i] ? 0
1895  : (content1[i] < content2[i] ? -1 : 1);
1896  else if (content1[i] != RANGE_DATUM_FINITE)
1897  return content1[i] == RANGE_DATUM_NEG_INF ? -1 : 1;
1898  else if (content2[i] != RANGE_DATUM_FINITE)
1899  return content2[i] == RANGE_DATUM_NEG_INF ? 1 : -1;
1900 
1901  cmpval = DatumGetInt32(FunctionCall2Coll(&key->partsupfunc[i],
1902  key->partcollation[i],
1903  datums1[i],
1904  datums2[i]));
1905  if (cmpval != 0)
1906  break;
1907  }
1908 
1909  /*
1910  * If the comparison is anything other than equal, we're done. If they
1911  * compare equal though, we still have to consider whether the boundaries
1912  * are inclusive or exclusive. Exclusive one is considered smaller of the
1913  * two.
1914  */
1915  if (cmpval == 0 && lower1 != lower2)
1916  cmpval = lower1 ? 1 : -1;
1917 
1918  return cmpval;
1919 }
#define DatumGetInt32(X)
Definition: postgres.h:478
FmgrInfo * partsupfunc
Definition: rel.h:63
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
signed int int32
Definition: c.h:256
RangeDatumContent
Definition: partition.c:70
Oid * partcollation
Definition: rel.h:66
uintptr_t Datum
Definition: postgres.h:372
int16 partnatts
Definition: rel.h:55
RangeDatumContent * content
Definition: partition.c:112
int i
static int32 partition_rbound_datum_cmp ( PartitionKey  key,
Datum rb_datums,
RangeDatumContent rb_content,
Datum tuple_datums 
)
static

Definition at line 1928 of file partition.c.

References DatumGetInt32, FunctionCall2Coll(), i, PartitionKeyData::partcollation, PartitionKeyData::partnatts, PartitionKeyData::partsupfunc, RANGE_DATUM_FINITE, and RANGE_DATUM_NEG_INF.

Referenced by partition_bound_cmp().

1931 {
1932  int i;
1933  int32 cmpval = -1;
1934 
1935  for (i = 0; i < key->partnatts; i++)
1936  {
1937  if (rb_content[i] != RANGE_DATUM_FINITE)
1938  return rb_content[i] == RANGE_DATUM_NEG_INF ? -1 : 1;
1939 
1940  cmpval = DatumGetInt32(FunctionCall2Coll(&key->partsupfunc[i],
1941  key->partcollation[i],
1942  rb_datums[i],
1943  tuple_datums[i]));
1944  if (cmpval != 0)
1945  break;
1946  }
1947 
1948  return cmpval;
1949 }
#define DatumGetInt32(X)
Definition: postgres.h:478
FmgrInfo * partsupfunc
Definition: rel.h:63
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
signed int int32
Definition: c.h:256
Oid * partcollation
Definition: rel.h:66
int16 partnatts
Definition: rel.h:55
int i
static int32 qsort_partition_list_value_cmp ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 1795 of file partition.c.

References DatumGetInt32, FunctionCall2Coll(), PartitionKeyData::partcollation, and PartitionKeyData::partsupfunc.

Referenced by RelationBuildPartitionDesc().

1796 {
1797  Datum val1 = (*(const PartitionListValue **) a)->value,
1798  val2 = (*(const PartitionListValue **) b)->value;
1799  PartitionKey key = (PartitionKey) arg;
1800 
1802  key->partcollation[0],
1803  val1, val2));
1804 }
#define DatumGetInt32(X)
Definition: postgres.h:478
FmgrInfo * partsupfunc
Definition: rel.h:63
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
Oid * partcollation
Definition: rel.h:66
uintptr_t Datum
Definition: postgres.h:372
struct PartitionKeyData * PartitionKey
Definition: rel.h:77
void * arg
static int32 qsort_partition_rbound_cmp ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 1855 of file partition.c.

References PartitionRangeBound::content, PartitionRangeBound::datums, PartitionRangeBound::lower, and partition_rbound_cmp().

Referenced by RelationBuildPartitionDesc().

1856 {
1857  PartitionRangeBound *b1 = (*(PartitionRangeBound *const *) a);
1858  PartitionRangeBound *b2 = (*(PartitionRangeBound *const *) b);
1859  PartitionKey key = (PartitionKey) arg;
1860 
1861  return partition_rbound_cmp(key, b1->datums, b1->content, b1->lower, b2);
1862 }
static int32 partition_rbound_cmp(PartitionKey key, Datum *datums1, RangeDatumContent *content1, bool lower1, PartitionRangeBound *b2)
Definition: partition.c:1871
RangeDatumContent * content
Definition: partition.c:112
struct PartitionKeyData * PartitionKey
Definition: rel.h:77
void * arg
void RelationBuildPartitionDesc ( Relation  rel)

Definition at line 151 of file partition.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Anum_pg_class_relpartbound, Assert, PartitionDescData::boundinfo, CacheMemoryContext, Const::constisnull, Const::constvalue, PartitionBoundInfoData::content, PartitionRangeBound::content, cur, datumCopy(), DatumGetInt32, PartitionBoundInfoData::datums, PartitionRangeBound::datums, elog, ERROR, find_inheritance_children(), FunctionCall2Coll(), GETSTRUCT, PartitionBoundInfoData::has_null, HeapTupleIsValid, i, PartitionListValue::index, PartitionRangeBound::index, PartitionBoundInfoData::indexes, lappend(), lappend_oid(), lfirst, lfirst_oid, list_length(), PartitionBoundSpec::listdatums, lower(), PartitionBoundSpec::lowerdatums, make_one_range_bound(), MemoryContextSwitchTo(), PartitionBoundInfoData::ndatums, NIL, NoLock, PartitionDescData::nparts, NULL, PartitionBoundInfoData::null_index, PartitionDescData::oids, palloc(), palloc0(), PartitionKeyData::partcollation, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, PartitionKeyData::partnatts, PartitionKeyData::partsupfunc, PartitionKeyData::parttypbyval, PartitionKeyData::parttyplen, pfree(), qsort_arg(), qsort_partition_list_value_cmp(), qsort_partition_rbound_cmp(), RANGE_DATUM_FINITE, RelationData::rd_partdesc, RelationData::rd_pdcxt, RelationGetPartitionKey, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), RELOID, result, SearchSysCache1, PartitionKeyData::strategy, PartitionBoundInfoData::strategy, PartitionBoundSpec::strategy, stringToNode(), SysCacheGetAttr(), TextDatumGetCString, upper(), PartitionBoundSpec::upperdatums, val, PartitionListValue::value, and value.

Referenced by RelationBuildDesc(), and RelationCacheInitializePhase3().

152 {
153  List *inhoids,
154  *partoids;
155  Oid *oids = NULL;
156  List *boundspecs = NIL;
157  ListCell *cell;
158  int i,
159  nparts;
162  MemoryContext oldcxt;
163 
164  int ndatums = 0;
165 
166  /* List partitioning specific */
167  PartitionListValue **all_values = NULL;
168  bool found_null = false;
169  int null_index = -1;
170 
171  /* Range partitioning specific */
172  PartitionRangeBound **rbounds = NULL;
173 
174  /*
175  * The following could happen in situations where rel has a pg_class entry
176  * but not the pg_partitioned_table entry yet.
177  */
178  if (key == NULL)
179  return;
180 
181  /* Get partition oids from pg_inherits */
183 
184  /* Collect bound spec nodes in a list */
185  i = 0;
186  partoids = NIL;
187  foreach(cell, inhoids)
188  {
189  Oid inhrelid = lfirst_oid(cell);
190  HeapTuple tuple;
191  Datum datum;
192  bool isnull;
193  Node *boundspec;
194 
195  tuple = SearchSysCache1(RELOID, inhrelid);
196  if (!HeapTupleIsValid(tuple))
197  elog(ERROR, "cache lookup failed for relation %u", inhrelid);
198 
199  /*
200  * It is possible that the pg_class tuple of a partition has not been
201  * updated yet to set its relpartbound field. The only case where
202  * this happens is when we open the parent relation to check using its
203  * partition descriptor that a new partition's bound does not overlap
204  * some existing partition.
205  */
206  if (!((Form_pg_class) GETSTRUCT(tuple))->relispartition)
207  {
208  ReleaseSysCache(tuple);
209  continue;
210  }
211 
212  datum = SysCacheGetAttr(RELOID, tuple,
214  &isnull);
215  Assert(!isnull);
216  boundspec = (Node *) stringToNode(TextDatumGetCString(datum));
217  boundspecs = lappend(boundspecs, boundspec);
218  partoids = lappend_oid(partoids, inhrelid);
219  ReleaseSysCache(tuple);
220  }
221 
222  nparts = list_length(partoids);
223 
224  if (nparts > 0)
225  {
226  oids = (Oid *) palloc(nparts * sizeof(Oid));
227  i = 0;
228  foreach(cell, partoids)
229  oids[i++] = lfirst_oid(cell);
230 
231  /* Convert from node to the internal representation */
232  if (key->strategy == PARTITION_STRATEGY_LIST)
233  {
234  List *non_null_values = NIL;
235 
236  /*
237  * Create a unified list of non-null values across all partitions.
238  */
239  i = 0;
240  found_null = false;
241  null_index = -1;
242  foreach(cell, boundspecs)
243  {
244  ListCell *c;
245  PartitionBoundSpec *spec = lfirst(cell);
246 
247  if (spec->strategy != PARTITION_STRATEGY_LIST)
248  elog(ERROR, "invalid strategy in partition bound spec");
249 
250  foreach(c, spec->listdatums)
251  {
252  Const *val = lfirst(c);
253  PartitionListValue *list_value = NULL;
254 
255  if (!val->constisnull)
256  {
257  list_value = (PartitionListValue *)
258  palloc0(sizeof(PartitionListValue));
259  list_value->index = i;
260  list_value->value = val->constvalue;
261  }
262  else
263  {
264  /*
265  * Never put a null into the values array, flag
266  * instead for the code further down below where we
267  * construct the actual relcache struct.
268  */
269  if (found_null)
270  elog(ERROR, "found null more than once");
271  found_null = true;
272  null_index = i;
273  }
274 
275  if (list_value)
276  non_null_values = lappend(non_null_values,
277  list_value);
278  }
279 
280  i++;
281  }
282 
283  ndatums = list_length(non_null_values);
284 
285  /*
286  * Collect all list values in one array. Alongside the value, we
287  * also save the index of partition the value comes from.
288  */
289  all_values = (PartitionListValue **) palloc(ndatums *
290  sizeof(PartitionListValue *));
291  i = 0;
292  foreach(cell, non_null_values)
293  {
294  PartitionListValue *src = lfirst(cell);
295 
296  all_values[i] = (PartitionListValue *)
297  palloc(sizeof(PartitionListValue));
298  all_values[i]->value = src->value;
299  all_values[i]->index = src->index;
300  i++;
301  }
302 
303  qsort_arg(all_values, ndatums, sizeof(PartitionListValue *),
304  qsort_partition_list_value_cmp, (void *) key);
305  }
306  else if (key->strategy == PARTITION_STRATEGY_RANGE)
307  {
308  int j,
309  k;
310  PartitionRangeBound **all_bounds,
311  *prev;
312  bool *distinct_indexes;
313 
314  all_bounds = (PartitionRangeBound **) palloc0(2 * nparts *
315  sizeof(PartitionRangeBound *));
316  distinct_indexes = (bool *) palloc(2 * nparts * sizeof(bool));
317 
318  /*
319  * Create a unified list of range bounds across all the
320  * partitions.
321  */
322  i = j = 0;
323  foreach(cell, boundspecs)
324  {
325  PartitionBoundSpec *spec = lfirst(cell);
327  *upper;
328 
329  if (spec->strategy != PARTITION_STRATEGY_RANGE)
330  elog(ERROR, "invalid strategy in partition bound spec");
331 
332  lower = make_one_range_bound(key, i, spec->lowerdatums,
333  true);
334  upper = make_one_range_bound(key, i, spec->upperdatums,
335  false);
336  all_bounds[j] = lower;
337  all_bounds[j + 1] = upper;
338  j += 2;
339  i++;
340  }
341  Assert(j == 2 * nparts);
342 
343  /* Sort all the bounds in ascending order */
344  qsort_arg(all_bounds, 2 * nparts,
345  sizeof(PartitionRangeBound *),
347  (void *) key);
348 
349  /*
350  * Count the number of distinct bounds to allocate an array of
351  * that size.
352  */
353  ndatums = 0;
354  prev = NULL;
355  for (i = 0; i < 2 * nparts; i++)
356  {
357  PartitionRangeBound *cur = all_bounds[i];
358  bool is_distinct = false;
359  int j;
360 
361  /* Is current bound is distinct from the previous? */
362  for (j = 0; j < key->partnatts; j++)
363  {
364  Datum cmpval;
365 
366  if (prev == NULL)
367  {
368  is_distinct = true;
369  break;
370  }
371 
372  /*
373  * If either of them has infinite element, we can't equate
374  * them. Even when both are infinite, they'd have
375  * opposite signs, because only one of cur and prev is a
376  * lower bound).
377  */
378  if (cur->content[j] != RANGE_DATUM_FINITE ||
379  prev->content[j] != RANGE_DATUM_FINITE)
380  {
381  is_distinct = true;
382  break;
383  }
384  cmpval = FunctionCall2Coll(&key->partsupfunc[j],
385  key->partcollation[j],
386  cur->datums[j],
387  prev->datums[j]);
388  if (DatumGetInt32(cmpval) != 0)
389  {
390  is_distinct = true;
391  break;
392  }
393  }
394 
395  /*
396  * Count the current bound if it is distinct from the previous
397  * one. Also, store if the index i contains a distinct bound
398  * that we'd like put in the relcache array.
399  */
400  if (is_distinct)
401  {
402  distinct_indexes[i] = true;
403  ndatums++;
404  }
405  else
406  distinct_indexes[i] = false;
407 
408  prev = cur;
409  }
410 
411  /*
412  * Finally save them in an array from where they will be copied
413  * into the relcache.
414  */
415  rbounds = (PartitionRangeBound **) palloc(ndatums *
416  sizeof(PartitionRangeBound *));
417  k = 0;
418  for (i = 0; i < 2 * nparts; i++)
419  {
420  if (distinct_indexes[i])
421  rbounds[k++] = all_bounds[i];
422  }
423  Assert(k == ndatums);
424  }
425  else
426  elog(ERROR, "unexpected partition strategy: %d",
427  (int) key->strategy);
428  }
429 
430  /* Now build the actual relcache partition descriptor */
434  oldcxt = MemoryContextSwitchTo(rel->rd_pdcxt);
435 
436  result = (PartitionDescData *) palloc0(sizeof(PartitionDescData));
437  result->nparts = nparts;
438  if (nparts > 0)
439  {
440  PartitionBoundInfo boundinfo;
441  int *mapping;
442  int next_index = 0;
443 
444  result->oids = (Oid *) palloc0(nparts * sizeof(Oid));
445 
446  boundinfo = (PartitionBoundInfoData *)
448  boundinfo->strategy = key->strategy;
449  boundinfo->ndatums = ndatums;
450  boundinfo->datums = (Datum **) palloc0(ndatums * sizeof(Datum *));
451 
452  /* Initialize mapping array with invalid values */
453  mapping = (int *) palloc(sizeof(int) * nparts);
454  for (i = 0; i < nparts; i++)
455  mapping[i] = -1;
456 
457  switch (key->strategy)
458  {
460  {
461  boundinfo->has_null = found_null;
462  boundinfo->indexes = (int *) palloc(ndatums * sizeof(int));
463 
464  /*
465  * Copy values. Indexes of individual values are mapped
466  * to canonical values so that they match for any two list
467  * partitioned tables with same number of partitions and
468  * same lists per partition. One way to canonicalize is
469  * to assign the index in all_values[] of the smallest
470  * value of each partition, as the index of all of the
471  * partition's values.
472  */
473  for (i = 0; i < ndatums; i++)
474  {
475  boundinfo->datums[i] = (Datum *) palloc(sizeof(Datum));
476  boundinfo->datums[i][0] = datumCopy(all_values[i]->value,
477  key->parttypbyval[0],
478  key->parttyplen[0]);
479 
480  /* If the old index has no mapping, assign one */
481  if (mapping[all_values[i]->index] == -1)
482  mapping[all_values[i]->index] = next_index++;
483 
484  boundinfo->indexes[i] = mapping[all_values[i]->index];
485  }
486 
487  /*
488  * If null-accepting partition has no mapped index yet,
489  * assign one. This could happen if such partition
490  * accepts only null and hence not covered in the above
491  * loop which only handled non-null values.
492  */
493  if (found_null)
494  {
495  Assert(null_index >= 0);
496  if (mapping[null_index] == -1)
497  mapping[null_index] = next_index++;
498  }
499 
500  /* All partition must now have a valid mapping */
501  Assert(next_index == nparts);
502 
503  if (found_null)
504  boundinfo->null_index = mapping[null_index];
505  else
506  boundinfo->null_index = -1;
507  break;
508  }
509 
511  {
512  boundinfo->content = (RangeDatumContent **) palloc(ndatums *
513  sizeof(RangeDatumContent *));
514  boundinfo->indexes = (int *) palloc((ndatums + 1) *
515  sizeof(int));
516 
517  for (i = 0; i < ndatums; i++)
518  {
519  int j;
520 
521  boundinfo->datums[i] = (Datum *) palloc(key->partnatts *
522  sizeof(Datum));
523  boundinfo->content[i] = (RangeDatumContent *)
524  palloc(key->partnatts *
525  sizeof(RangeDatumContent));
526  for (j = 0; j < key->partnatts; j++)
527  {
528  if (rbounds[i]->content[j] == RANGE_DATUM_FINITE)
529  boundinfo->datums[i][j] =
530  datumCopy(rbounds[i]->datums[j],
531  key->parttypbyval[j],
532  key->parttyplen[j]);
533  /* Remember, we are storing the tri-state value. */
534  boundinfo->content[i][j] = rbounds[i]->content[j];
535  }
536 
537  /*
538  * There is no mapping for invalid indexes.
539  *
540  * Any lower bounds in the rbounds array have invalid
541  * indexes assigned, because the values between the
542  * previous bound (if there is one) and this (lower)
543  * bound are not part of the range of any existing
544  * partition.
545  */
546  if (rbounds[i]->lower)
547  boundinfo->indexes[i] = -1;
548  else
549  {
550  int orig_index = rbounds[i]->index;
551 
552  /* If the old index is has no mapping, assign one */
553  if (mapping[orig_index] == -1)
554  mapping[orig_index] = next_index++;
555 
556  boundinfo->indexes[i] = mapping[orig_index];
557  }
558  }
559  boundinfo->indexes[i] = -1;
560  break;
561  }
562 
563  default:
564  elog(ERROR, "unexpected partition strategy: %d",
565  (int) key->strategy);
566  }
567 
568  result->boundinfo = boundinfo;
569 
570  /*
571  * Now assign OIDs from the original array into mapped indexes of the
572  * result array. Order of OIDs in the former is defined by the
573  * catalog scan that retrived them, whereas that in the latter is
574  * defined by canonicalized representation of the list values or the
575  * range bounds.
576  */
577  for (i = 0; i < nparts; i++)
578  result->oids[mapping[i]] = oids[i];
579  pfree(mapping);
580  }
581 
582  MemoryContextSwitchTo(oldcxt);
583  rel->rd_partdesc = result;
584 }
Datum constvalue
Definition: primnodes.h:196
#define NIL
Definition: pg_list.h:69
struct PartitionDescData * rd_partdesc
Definition: rel.h:131
void * stringToNode(char *str)
Definition: read.c:38
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static struct @78 value
#define DatumGetInt32(X)
Definition: postgres.h:478
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
FmgrInfo * partsupfunc
Definition: rel.h:63
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static int32 qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
Definition: partition.c:1855
Definition: nodes.h:522
struct cursor * cur
Definition: ecpg.c:28
return result
Definition: formatting.c:1618
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:74
char strategy
Definition: rel.h:54
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
PartitionBoundInfo boundinfo
Definition: partition.h:37
Definition: type.h:90
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
static int32 qsort_partition_list_value_cmp(const void *a, const void *b, void *arg)
Definition: partition.c:1795
char * c
#define NoLock
Definition: lockdefs.h:34
#define RelationGetRelationName(relation)
Definition: rel.h:437
RangeDatumContent
Definition: partition.c:70
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
List * lappend(List *list, void *datum)
Definition: list.c:128
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
Oid * partcollation
Definition: rel.h:66
#define TextDatumGetCString(d)
Definition: builtins.h:92
static PartitionRangeBound * make_one_range_bound(PartitionKey key, int index, List *datums, bool lower)
Definition: partition.c:1814
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
int16 partnatts
Definition: rel.h:55
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1278
List * find_inheritance_children(Oid parentrelId, LOCKMODE lockmode)
Definition: pg_inherits.c:48
RangeDatumContent * content
Definition: partition.c:112
#define Anum_pg_class_relpartbound
Definition: pg_class.h:135
bool * parttypbyval
Definition: rel.h:72
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
MemoryContext rd_pdcxt
Definition: rel.h:130
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
int16 * parttyplen
Definition: rel.h:71
static int list_length(const List *l)
Definition: pg_list.h:89
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:769
#define RelationGetPartitionKey(relation)
Definition: rel.h:585
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:770
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:417
long val
Definition: informix.c:689
bool constisnull
Definition: primnodes.h:197
#define lfirst_oid(lc)
Definition: pg_list.h:108
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
RangeDatumContent ** content
Definition: partition.c:83
PartitionDispatch* RelationGetPartitionDispatchInfo ( Relation  rel,
int  lockmode,
int *  num_parted,
List **  leaf_part_oids 
)

Definition at line 991 of file partition.c.

References APPEND_REL_PARTITION_OIDS, convert_tuples_by_name(), forboth, get_rel_relkind(), gettext_noop, heap_close, heap_open(), i, PartitionDispatchData::indexes, PartitionDispatchData::key, PartitionDispatchData::keystate, lappend(), lappend_oid(), lfirst, lfirst_oid, list_make1, MakeSingleTupleTableSlot(), NIL, NoLock, PartitionDescData::nparts, NULL, PartitionDescData::oids, palloc(), PartitionDispatchData::partdesc, RelationGetDescr, RelationGetPartitionDesc, RelationGetPartitionKey, PartitionDispatchData::reldesc, RELKIND_PARTITIONED_TABLE, PartitionDispatchData::tupmap, and PartitionDispatchData::tupslot.

Referenced by ExecSetupPartitionTupleRouting().

993 {
995  List *all_parts = NIL,
996  *all_parents = NIL,
997  *parted_rels,
998  *parted_rel_parents;
999  ListCell *lc1,
1000  *lc2;
1001  int i,
1002  k,
1003  offset;
1004 
1005  /*
1006  * Lock partitions and make a list of the partitioned ones to prepare
1007  * their PartitionDispatch objects below.
1008  *
1009  * Cannot use find_all_inheritors() here, because then the order of OIDs
1010  * in parted_rels list would be unknown, which does not help, because we
1011  * we assign indexes within individual PartitionDispatch in an order that
1012  * is predetermined (determined by the order of OIDs in individual
1013  * partition descriptors).
1014  */
1015  *num_parted = 1;
1016  parted_rels = list_make1(rel);
1017  /* Root partitioned table has no parent, so NULL for parent */
1018  parted_rel_parents = list_make1(NULL);
1019  APPEND_REL_PARTITION_OIDS(rel, all_parts, all_parents);
1020  forboth(lc1, all_parts, lc2, all_parents)
1021  {
1022  Relation partrel = heap_open(lfirst_oid(lc1), lockmode);
1023  Relation parent = lfirst(lc2);
1024  PartitionDesc partdesc = RelationGetPartitionDesc(partrel);
1025 
1026  /*
1027  * If this partition is a partitioned table, add its children to the
1028  * end of the list, so that they are processed as well.
1029  */
1030  if (partdesc)
1031  {
1032  (*num_parted)++;
1033  parted_rels = lappend(parted_rels, partrel);
1034  parted_rel_parents = lappend(parted_rel_parents, parent);
1035  APPEND_REL_PARTITION_OIDS(partrel, all_parts, all_parents);
1036  }
1037  else
1038  heap_close(partrel, NoLock);
1039 
1040  /*
1041  * We keep the partitioned ones open until we're done using the
1042  * information being collected here (for example, see
1043  * ExecEndModifyTable).
1044  */
1045  }
1046 
1047  /*
1048  * We want to create two arrays - one for leaf partitions and another for
1049  * partitioned tables (including the root table and internal partitions).
1050  * While we only create the latter here, leaf partition array of suitable
1051  * objects (such as, ResultRelInfo) is created by the caller using the
1052  * list of OIDs we return. Indexes into these arrays get assigned in a
1053  * breadth-first manner, whereby partitions of any given level are placed
1054  * consecutively in the respective arrays.
1055  */
1056  pd = (PartitionDispatchData **) palloc(*num_parted *
1057  sizeof(PartitionDispatchData *));
1058  *leaf_part_oids = NIL;
1059  i = k = offset = 0;
1060  forboth(lc1, parted_rels, lc2, parted_rel_parents)
1061  {
1062  Relation partrel = lfirst(lc1);
1063  Relation parent = lfirst(lc2);
1064  PartitionKey partkey = RelationGetPartitionKey(partrel);
1065  TupleDesc tupdesc = RelationGetDescr(partrel);
1066  PartitionDesc partdesc = RelationGetPartitionDesc(partrel);
1067  int j,
1068  m;
1069 
1071  pd[i]->reldesc = partrel;
1072  pd[i]->key = partkey;
1073  pd[i]->keystate = NIL;
1074  pd[i]->partdesc = partdesc;
1075  if (parent != NULL)
1076  {
1077  /*
1078  * For every partitioned table other than root, we must store a
1079  * tuple table slot initialized with its tuple descriptor and a
1080  * tuple conversion map to convert a tuple from its parent's
1081  * rowtype to its own. That is to make sure that we are looking at
1082  * the correct row using the correct tuple descriptor when
1083  * computing its partition key for tuple routing.
1084  */
1085  pd[i]->tupslot = MakeSingleTupleTableSlot(tupdesc);
1087  tupdesc,
1088  gettext_noop("could not convert row type"));
1089  }
1090  else
1091  {
1092  /* Not required for the root partitioned table */
1093  pd[i]->tupslot = NULL;
1094  pd[i]->tupmap = NULL;
1095  }
1096  pd[i]->indexes = (int *) palloc(partdesc->nparts * sizeof(int));
1097 
1098  /*
1099  * Indexes corresponding to the internal partitions are multiplied by
1100  * -1 to distinguish them from those of leaf partitions. Encountering
1101  * an index >= 0 means we found a leaf partition, which is immediately
1102  * returned as the partition we are looking for. A negative index
1103  * means we found a partitioned table, whose PartitionDispatch object
1104  * is located at the above index multiplied back by -1. Using the
1105  * PartitionDispatch object, search is continued further down the
1106  * partition tree.
1107  */
1108  m = 0;
1109  for (j = 0; j < partdesc->nparts; j++)
1110  {
1111  Oid partrelid = partdesc->oids[j];
1112 
1113  if (get_rel_relkind(partrelid) != RELKIND_PARTITIONED_TABLE)
1114  {
1115  *leaf_part_oids = lappend_oid(*leaf_part_oids, partrelid);
1116  pd[i]->indexes[j] = k++;
1117  }
1118  else
1119  {
1120  /*
1121  * offset denotes the number of partitioned tables of upper
1122  * levels including those of the current level. Any partition
1123  * of this table must belong to the next level and hence will
1124  * be placed after the last partitioned table of this level.
1125  */
1126  pd[i]->indexes[j] = -(1 + offset + m);
1127  m++;
1128  }
1129  }
1130  i++;
1131 
1132  /*
1133  * This counts the number of partitioned tables at upper levels
1134  * including those of the current level.
1135  */
1136  offset += m;
1137  }
1138 
1139  return pd;
1140 }
#define NIL
Definition: pg_list.h:69
struct PartitionDispatchData * PartitionDispatch
Definition: partition.h:71
PartitionDesc partdesc
Definition: partition.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
#define RelationGetDescr(relation)
Definition: rel.h:429
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1769
TupleConversionMap * tupmap
Definition: partition.h:67
#define gettext_noop(x)
Definition: c.h:139
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define list_make1(x1)
Definition: pg_list.h:133
#define NoLock
Definition: lockdefs.h:34
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:199
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: tupconvert.c:204
List * lappend(List *list, void *datum)
Definition: list.c:128
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
TupleTableSlot * tupslot
Definition: partition.h:66
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
#define RelationGetPartitionKey(relation)
Definition: rel.h:585
void * palloc(Size size)
Definition: mcxt.c:849
#define APPEND_REL_PARTITION_OIDS(rel, partoids, parents)
Definition: partition.c:971
int i
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
#define RelationGetPartitionDesc(relation)
Definition: rel.h:633
PartitionKey key
Definition: partition.h:63
List* RelationGetPartitionQual ( Relation  rel)

Definition at line 958 of file partition.c.

References generate_partition_qual(), NIL, and RelationData::rd_rel.

Referenced by ATExecAttachPartition(), get_relation_constraints(), and InitResultRelInfo().

959 {
960  /* Quick exit */
961  if (!rel->rd_rel->relispartition)
962  return NIL;
963 
964  return generate_partition_qual(rel);
965 }
#define NIL
Definition: pg_list.h:69
Form_pg_class rd_rel
Definition: rel.h:114
static List * generate_partition_qual(Relation rel)
Definition: partition.c:1524