include $(top_builddir)/src/Makefile.global
OBJS = \
+ attmap.o \
bufmask.o \
detoast.o \
heaptuple.o \
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * attmap.c
+ * Attribute mapping support.
+ *
+ * This file provides utility routines to build and manage attribute
+ * mappings by comparing input and output TupleDescs. Such mappings
+ * are typically used by DDL operating on inheritance and partition trees
+ * to do a conversion between rowtypes logically equivalent but with
+ * columns in a different order, taking into account dropped columns.
+ * They are also used by the tuple conversion routines in tupconvert.c.
+ *
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/access/common/attmap.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "access/attmap.h"
+#include "access/htup_details.h"
+#include "utils/builtins.h"
+
+
+static bool check_attrmap_match(TupleDesc indesc,
+ TupleDesc outdesc,
+ AttrMap *attrMap);
+
+/*
+ * make_attrmap
+ *
+ * Utility routine to allocate an attribute map in the current memory
+ * context.
+ */
+AttrMap *
+make_attrmap(int maplen)
+{
+ AttrMap *res;
+
+ res = (AttrMap *) palloc0(sizeof(AttrMap));
+ res->maplen = maplen;
+ res->attnums = (AttrNumber *) palloc0(sizeof(AttrNumber) * maplen);
+ return res;
+}
+
+/*
+ * free_attrmap
+ *
+ * Utility routine to release an attribute map.
+ */
+void
+free_attrmap(AttrMap *map)
+{
+ pfree(map->attnums);
+ pfree(map);
+}
+
+/*
+ * build_attrmap_by_position
+ *
+ * Return a palloc'd bare attribute map for tuple conversion, matching input
+ * and output columns by position. Dropped columns are ignored in both input
+ * and output, marked as 0. This is normally a subroutine for
+ * convert_tuples_by_position in tupconvert.c, but it can be used standalone.
+ *
+ * Note: the errdetail messages speak of indesc as the "returned" rowtype,
+ * outdesc as the "expected" rowtype. This is okay for current uses but
+ * might need generalization in future.
+ */
+AttrMap *
+build_attrmap_by_position(TupleDesc indesc,
+ TupleDesc outdesc,
+ const char *msg)
+{
+ AttrMap *attrMap;
+ int nincols;
+ int noutcols;
+ int n;
+ int i;
+ int j;
+ bool same;
+
+ /*
+ * The length is computed as the number of attributes of the expected
+ * rowtype as it includes dropped attributes in its count.
+ */
+ n = outdesc->natts;
+ attrMap = make_attrmap(n);
+
+ j = 0; /* j is next physical input attribute */
+ nincols = noutcols = 0; /* these count non-dropped attributes */
+ same = true;
+ for (i = 0; i < n; i++)
+ {
+ Form_pg_attribute att = TupleDescAttr(outdesc, i);
+ Oid atttypid;
+ int32 atttypmod;
+
+ if (att->attisdropped)
+ continue; /* attrMap->attnums[i] is already 0 */
+ noutcols++;
+ atttypid = att->atttypid;
+ atttypmod = att->atttypmod;
+ for (; j < indesc->natts; j++)
+ {
+ att = TupleDescAttr(indesc, j);
+ if (att->attisdropped)
+ continue;
+ nincols++;
+
+ /* Found matching column, now check type */
+ if (atttypid != att->atttypid ||
+ (atttypmod != att->atttypmod && atttypmod >= 0))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg_internal("%s", _(msg)),
+ errdetail("Returned type %s does not match expected type %s in column %d.",
+ format_type_with_typemod(att->atttypid,
+ att->atttypmod),
+ format_type_with_typemod(atttypid,
+ atttypmod),
+ noutcols)));
+ attrMap->attnums[i] = (AttrNumber) (j + 1);
+ j++;
+ break;
+ }
+ if (attrMap->attnums[i] == 0)
+ same = false; /* we'll complain below */
+ }
+
+ /* Check for unused input columns */
+ for (; j < indesc->natts; j++)
+ {
+ if (TupleDescAttr(indesc, j)->attisdropped)
+ continue;
+ nincols++;
+ same = false; /* we'll complain below */
+ }
+
+ /* Report column count mismatch using the non-dropped-column counts */
+ if (!same)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg_internal("%s", _(msg)),
+ errdetail("Number of returned columns (%d) does not match "
+ "expected column count (%d).",
+ nincols, noutcols)));
+
+ /* Check if the map has a one-to-one match */
+ if (check_attrmap_match(indesc, outdesc, attrMap))
+ {
+ /* Runtime conversion is not needed */
+ free_attrmap(attrMap);
+ return NULL;
+ }
+
+ return attrMap;
+}
+
+/*
+ * build_attrmap_by_name
+ *
+ * Return a palloc'd bare attribute map for tuple conversion, matching input
+ * and output columns by name. (Dropped columns are ignored in both input and
+ * output.) This is normally a subroutine for convert_tuples_by_name in
+ * tupconvert.c, but can be used standalone.
+ */
+AttrMap *
+build_attrmap_by_name(TupleDesc indesc,
+ TupleDesc outdesc)
+{
+ AttrMap *attrMap;
+ int outnatts;
+ int innatts;
+ int i;
+ int nextindesc = -1;
+
+ outnatts = outdesc->natts;
+ innatts = indesc->natts;
+
+ attrMap = make_attrmap(outnatts);
+ for (i = 0; i < outnatts; i++)
+ {
+ Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
+ char *attname;
+ Oid atttypid;
+ int32 atttypmod;
+ int j;
+
+ if (outatt->attisdropped)
+ continue; /* attrMap->attnums[i] is already 0 */
+ attname = NameStr(outatt->attname);
+ atttypid = outatt->atttypid;
+ atttypmod = outatt->atttypmod;
+
+ /*
+ * Now search for an attribute with the same name in the indesc. It
+ * seems likely that a partitioned table will have the attributes in
+ * the same order as the partition, so the search below is optimized
+ * for that case. It is possible that columns are dropped in one of
+ * the relations, but not the other, so we use the 'nextindesc'
+ * counter to track the starting point of the search. If the inner
+ * loop encounters dropped columns then it will have to skip over
+ * them, but it should leave 'nextindesc' at the correct position for
+ * the next outer loop.
+ */
+ for (j = 0; j < innatts; j++)
+ {
+ Form_pg_attribute inatt;
+
+ nextindesc++;
+ if (nextindesc >= innatts)
+ nextindesc = 0;
+
+ inatt = TupleDescAttr(indesc, nextindesc);
+ if (inatt->attisdropped)
+ continue;
+ if (strcmp(attname, NameStr(inatt->attname)) == 0)
+ {
+ /* Found it, check type */
+ if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("could not convert row type"),
+ errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
+ attname,
+ format_type_be(outdesc->tdtypeid),
+ format_type_be(indesc->tdtypeid))));
+ attrMap->attnums[i] = inatt->attnum;
+ break;
+ }
+ }
+ if (attrMap->attnums[i] == 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("could not convert row type"),
+ errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
+ attname,
+ format_type_be(outdesc->tdtypeid),
+ format_type_be(indesc->tdtypeid))));
+ }
+ return attrMap;
+}
+
+/*
+ * build_attrmap_by_name_if_req
+ *
+ * Returns mapping created by build_attrmap_by_name, or NULL if no
+ * conversion is required. This is a convenience routine for
+ * convert_tuples_by_name() in tupconvert.c and other functions, but it
+ * can be used standalone.
+ */
+AttrMap *
+build_attrmap_by_name_if_req(TupleDesc indesc,
+ TupleDesc outdesc)
+{
+ AttrMap *attrMap;
+
+ /* Verify compatibility and prepare attribute-number map */
+ attrMap = build_attrmap_by_name(indesc, outdesc);
+
+ /* Check if the map has a one-to-one match */
+ if (check_attrmap_match(indesc, outdesc, attrMap))
+ {
+ /* Runtime conversion is not needed */
+ free_attrmap(attrMap);
+ return NULL;
+ }
+
+ return attrMap;
+}
+
+/*
+ * check_attrmap_match
+ *
+ * Check to see if the map is a one-to-one match, in which case we need
+ * not to do a tuple conversion, and the attribute map is not necessary.
+ */
+static bool
+check_attrmap_match(TupleDesc indesc,
+ TupleDesc outdesc,
+ AttrMap *attrMap)
+{
+ int i;
+
+ /* no match if attribute numbers are not the same */
+ if (indesc->natts != outdesc->natts)
+ return false;
+
+ for (i = 0; i < attrMap->maplen; i++)
+ {
+ Form_pg_attribute inatt;
+ Form_pg_attribute outatt;
+
+ if (attrMap->attnums[i] == (i + 1))
+ continue;
+
+ /*
+ * If it's a dropped column and the corresponding input column is also
+ * dropped, we don't need a conversion. However, attlen and attalign
+ * must agree.
+ */
+ inatt = TupleDescAttr(indesc, i);
+ outatt = TupleDescAttr(outdesc, i);
+ if (attrMap->attnums[i] == 0 &&
+ inatt->attisdropped &&
+ inatt->attlen == outatt->attlen &&
+ inatt->attalign == outatt->attalign)
+ continue;
+
+ return false;
+ }
+
+ return true;
+}
*/
#include "postgres.h"
-#include "access/htup_details.h"
#include "access/tupconvert.h"
#include "executor/tuptable.h"
-#include "utils/builtins.h"
/*
* The conversion setup routines have the following common API:
*
- * The setup routine checks whether the given source and destination tuple
- * descriptors are logically compatible. If not, it throws an error.
- * If so, it returns NULL if they are physically compatible (ie, no conversion
- * is needed), else a TupleConversionMap that can be used by execute_attr_map_tuple
- * to perform the conversion.
+ * The setup routine checks using attmap.c whether the given source and
+ * destination tuple descriptors are logically compatible. If not, it throws
+ * an error. If so, it returns NULL if they are physically compatible (ie, no
+ * conversion is needed), else a TupleConversionMap that can be used by
+ * execute_attr_map_tuple or execute_attr_map_slot to perform the conversion.
*
* The TupleConversionMap, if needed, is palloc'd in the caller's memory
* context. Also, the given tuple descriptors are referenced by the map,
/*
* Set up for tuple conversion, matching input and output columns by
* position. (Dropped columns are ignored in both input and output.)
- *
- * Note: the errdetail messages speak of indesc as the "returned" rowtype,
- * outdesc as the "expected" rowtype. This is okay for current uses but
- * might need generalization in future.
*/
TupleConversionMap *
convert_tuples_by_position(TupleDesc indesc,
const char *msg)
{
TupleConversionMap *map;
- AttrNumber *attrMap;
- int nincols;
- int noutcols;
int n;
- int i;
- int j;
- bool same;
+ AttrMap *attrMap;
/* Verify compatibility and prepare attribute-number map */
- n = outdesc->natts;
- attrMap = (AttrNumber *) palloc0(n * sizeof(AttrNumber));
- j = 0; /* j is next physical input attribute */
- nincols = noutcols = 0; /* these count non-dropped attributes */
- same = true;
- for (i = 0; i < n; i++)
- {
- Form_pg_attribute att = TupleDescAttr(outdesc, i);
- Oid atttypid;
- int32 atttypmod;
-
- if (att->attisdropped)
- continue; /* attrMap[i] is already 0 */
- noutcols++;
- atttypid = att->atttypid;
- atttypmod = att->atttypmod;
- for (; j < indesc->natts; j++)
- {
- att = TupleDescAttr(indesc, j);
- if (att->attisdropped)
- continue;
- nincols++;
- /* Found matching column, check type */
- if (atttypid != att->atttypid ||
- (atttypmod != att->atttypmod && atttypmod >= 0))
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg_internal("%s", _(msg)),
- errdetail("Returned type %s does not match expected type %s in column %d.",
- format_type_with_typemod(att->atttypid,
- att->atttypmod),
- format_type_with_typemod(atttypid,
- atttypmod),
- noutcols)));
- attrMap[i] = (AttrNumber) (j + 1);
- j++;
- break;
- }
- if (attrMap[i] == 0)
- same = false; /* we'll complain below */
- }
-
- /* Check for unused input columns */
- for (; j < indesc->natts; j++)
- {
- if (TupleDescAttr(indesc, j)->attisdropped)
- continue;
- nincols++;
- same = false; /* we'll complain below */
- }
-
- /* Report column count mismatch using the non-dropped-column counts */
- if (!same)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg_internal("%s", _(msg)),
- errdetail("Number of returned columns (%d) does not match "
- "expected column count (%d).",
- nincols, noutcols)));
-
- /*
- * Check to see if the map is one-to-one, in which case we need not do a
- * tuple conversion.
- */
- if (indesc->natts == outdesc->natts)
- {
- for (i = 0; i < n; i++)
- {
- Form_pg_attribute inatt;
- Form_pg_attribute outatt;
-
- if (attrMap[i] == (i + 1))
- continue;
+ attrMap = build_attrmap_by_position(indesc, outdesc, msg);
- /*
- * If it's a dropped column and the corresponding input column is
- * also dropped, we needn't convert. However, attlen and attalign
- * must agree.
- */
- inatt = TupleDescAttr(indesc, i);
- outatt = TupleDescAttr(outdesc, i);
- if (attrMap[i] == 0 &&
- inatt->attisdropped &&
- inatt->attlen == outatt->attlen &&
- inatt->attalign == outatt->attalign)
- continue;
-
- same = false;
- break;
- }
- }
- else
- same = false;
-
- if (same)
+ if (attrMap == NULL)
{
- /* Runtime conversion is not needed */
- pfree(attrMap);
+ /* runtime conversion is not needed */
return NULL;
}
map->outdesc = outdesc;
map->attrMap = attrMap;
/* preallocate workspace for Datum arrays */
+ n = outdesc->natts + 1; /* +1 for NULL */
map->outvalues = (Datum *) palloc(n * sizeof(Datum));
map->outisnull = (bool *) palloc(n * sizeof(bool));
n = indesc->natts + 1; /* +1 for NULL */
TupleDesc outdesc)
{
TupleConversionMap *map;
- AttrNumber *attrMap;
+ AttrMap *attrMap;
int n = outdesc->natts;
/* Verify compatibility and prepare attribute-number map */
- attrMap = convert_tuples_by_name_map_if_req(indesc, outdesc);
+ attrMap = build_attrmap_by_name_if_req(indesc, outdesc);
if (attrMap == NULL)
{
return map;
}
-/*
- * Return a palloc'd bare attribute map for tuple conversion, matching input
- * and output columns by name. (Dropped columns are ignored in both input and
- * output.) This is normally a subroutine for convert_tuples_by_name, but can
- * be used standalone.
- */
-AttrNumber *
-convert_tuples_by_name_map(TupleDesc indesc,
- TupleDesc outdesc)
-{
- AttrNumber *attrMap;
- int outnatts;
- int innatts;
- int i;
- int nextindesc = -1;
-
- outnatts = outdesc->natts;
- innatts = indesc->natts;
-
- attrMap = (AttrNumber *) palloc0(outnatts * sizeof(AttrNumber));
- for (i = 0; i < outnatts; i++)
- {
- Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
- char *attname;
- Oid atttypid;
- int32 atttypmod;
- int j;
-
- if (outatt->attisdropped)
- continue; /* attrMap[i] is already 0 */
- attname = NameStr(outatt->attname);
- atttypid = outatt->atttypid;
- atttypmod = outatt->atttypmod;
-
- /*
- * Now search for an attribute with the same name in the indesc. It
- * seems likely that a partitioned table will have the attributes in
- * the same order as the partition, so the search below is optimized
- * for that case. It is possible that columns are dropped in one of
- * the relations, but not the other, so we use the 'nextindesc'
- * counter to track the starting point of the search. If the inner
- * loop encounters dropped columns then it will have to skip over
- * them, but it should leave 'nextindesc' at the correct position for
- * the next outer loop.
- */
- for (j = 0; j < innatts; j++)
- {
- Form_pg_attribute inatt;
-
- nextindesc++;
- if (nextindesc >= innatts)
- nextindesc = 0;
-
- inatt = TupleDescAttr(indesc, nextindesc);
- if (inatt->attisdropped)
- continue;
- if (strcmp(attname, NameStr(inatt->attname)) == 0)
- {
- /* Found it, check type */
- if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("could not convert row type"),
- errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
- attname,
- format_type_be(outdesc->tdtypeid),
- format_type_be(indesc->tdtypeid))));
- attrMap[i] = inatt->attnum;
- break;
- }
- }
- if (attrMap[i] == 0)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("could not convert row type"),
- errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
- attname,
- format_type_be(outdesc->tdtypeid),
- format_type_be(indesc->tdtypeid))));
- }
- return attrMap;
-}
-
-/*
- * Returns mapping created by convert_tuples_by_name_map, or NULL if no
- * conversion not required. This is a convenience routine for
- * convert_tuples_by_name() and other functions.
- */
-AttrNumber *
-convert_tuples_by_name_map_if_req(TupleDesc indesc,
- TupleDesc outdesc)
-{
- AttrNumber *attrMap;
- int n = outdesc->natts;
- int i;
- bool same;
-
- /* Verify compatibility and prepare attribute-number map */
- attrMap = convert_tuples_by_name_map(indesc, outdesc);
-
- /*
- * Check to see if the map is one-to-one, in which case we need not do a
- * tuple conversion.
- */
- if (indesc->natts == outdesc->natts)
- {
- same = true;
- for (i = 0; i < n; i++)
- {
- Form_pg_attribute inatt;
- Form_pg_attribute outatt;
-
- if (attrMap[i] == (i + 1))
- continue;
-
- /*
- * If it's a dropped column and the corresponding input column is
- * also dropped, we needn't convert. However, attlen and attalign
- * must agree.
- */
- inatt = TupleDescAttr(indesc, i);
- outatt = TupleDescAttr(outdesc, i);
- if (attrMap[i] == 0 &&
- inatt->attisdropped &&
- inatt->attlen == outatt->attlen &&
- inatt->attalign == outatt->attalign)
- continue;
-
- same = false;
- break;
- }
- }
- else
- same = false;
-
- if (same)
- {
- /* Runtime conversion is not needed */
- pfree(attrMap);
- return NULL;
- }
- else
- return attrMap;
-}
-
/*
* Perform conversion of a tuple according to the map.
*/
HeapTuple
execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
{
- AttrNumber *attrMap = map->attrMap;
+ AttrMap *attrMap = map->attrMap;
Datum *invalues = map->invalues;
bool *inisnull = map->inisnull;
Datum *outvalues = map->outvalues;
/*
* Transpose into proper fields of the new tuple.
*/
- for (i = 0; i < outnatts; i++)
+ Assert(attrMap->maplen == outnatts);
+ for (i = 0; i < attrMap->maplen; i++)
{
- int j = attrMap[i];
+ int j = attrMap->attnums[i];
outvalues[i] = invalues[j];
outisnull[i] = inisnull[j];
* Perform conversion of a tuple slot according to the map.
*/
TupleTableSlot *
-execute_attr_map_slot(AttrNumber *attrMap,
+execute_attr_map_slot(AttrMap *attrMap,
TupleTableSlot *in_slot,
TupleTableSlot *out_slot)
{
/* Transpose into proper fields of the out slot. */
for (i = 0; i < outnatts; i++)
{
- int j = attrMap[i] - 1;
+ int j = attrMap->attnums[i] - 1;
- /* attrMap[i] == 0 means it's a NULL datum. */
+ /* attrMap->attnums[i] == 0 means it's a NULL datum. */
if (j == -1)
{
outvalues[i] = (Datum) 0;
free_conversion_map(TupleConversionMap *map)
{
/* indesc and outdesc are not ours to free */
- pfree(map->attrMap);
+ free_attrmap(map->attrMap);
pfree(map->invalues);
pfree(map->inisnull);
pfree(map->outvalues);
* Note: passing collations and opfamilies separately is a kludge. Adding
* them to IndexInfo may result in better coding here and elsewhere.
*
- * Use convert_tuples_by_name_map(index2, index1) to build the attmap.
+ * Use build_attrmap_by_name(index2, index1) to build the attmap.
*/
bool
CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
Oid *collations1, Oid *collations2,
Oid *opfamilies1, Oid *opfamilies2,
- AttrNumber *attmap, int maplen)
+ AttrMap *attmap)
{
int i;
*/
for (i = 0; i < info1->ii_NumIndexAttrs; i++)
{
- if (maplen < info2->ii_IndexAttrNumbers[i])
+ if (attmap->maplen < info2->ii_IndexAttrNumbers[i])
elog(ERROR, "incorrect attribute map");
/* ignore expressions at this stage */
if ((info1->ii_IndexAttrNumbers[i] != InvalidAttrNumber) &&
- (attmap[info2->ii_IndexAttrNumbers[i] - 1] !=
+ (attmap->attnums[info2->ii_IndexAttrNumbers[i] - 1] !=
info1->ii_IndexAttrNumbers[i]))
return false;
Node *mapped;
mapped = map_variable_attnos((Node *) info2->ii_Expressions,
- 1, 0, attmap, maplen,
+ 1, 0, attmap,
InvalidOid, &found_whole_row);
if (found_whole_row)
{
Node *mapped;
mapped = map_variable_attnos((Node *) info2->ii_Predicate,
- 1, 0, attmap, maplen,
+ 1, 0, attmap,
InvalidOid, &found_whole_row);
if (found_whole_row)
{
*/
#include "postgres.h"
+#include "access/attmap.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
-#include "access/tupconvert.h"
#include "catalog/indexing.h"
#include "catalog/partition.h"
#include "catalog/pg_inherits.h"
if (expr != NIL)
{
- AttrNumber *part_attnos;
+ AttrMap *part_attmap;
- part_attnos = convert_tuples_by_name_map(RelationGetDescr(to_rel),
- RelationGetDescr(from_rel));
+ part_attmap = build_attrmap_by_name(RelationGetDescr(to_rel),
+ RelationGetDescr(from_rel));
expr = (List *) map_variable_attnos((Node *) expr,
fromrel_varno, 0,
- part_attnos,
- RelationGetDescr(from_rel)->natts,
+ part_attmap,
RelationGetForm(to_rel)->reltype,
&my_found_whole_row);
}
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
-#include "access/tupconvert.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
Relation childrel;
List *childidxs;
ListCell *cell;
- AttrNumber *attmap;
+ AttrMap *attmap;
bool found = false;
- int maplen;
childrel = table_open(childRelid, lockmode);
childidxs = RelationGetIndexList(childrel);
attmap =
- convert_tuples_by_name_map(RelationGetDescr(childrel),
- parentDesc);
- maplen = parentDesc->natts;
+ build_attrmap_by_name(RelationGetDescr(childrel),
+ parentDesc);
foreach(cell, childidxs)
{
collationObjectId,
cldidx->rd_opfamily,
opfamOids,
- attmap, maplen))
+ attmap))
{
Oid cldConstrOid = InvalidOid;
{
ielem->expr =
map_variable_attnos((Node *) ielem->expr,
- 1, 0, attmap, maplen,
+ 1, 0, attmap,
InvalidOid,
&found_whole_row);
if (found_whole_row)
}
childStmt->whereClause =
map_variable_attnos(stmt->whereClause, 1, 0,
- attmap, maplen,
+ attmap,
InvalidOid, &found_whole_row);
if (found_whole_row)
elog(ERROR, "cannot convert whole-row table reference");
pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_DONE,
i + 1);
- pfree(attmap);
+ free_attrmap(attmap);
}
/*
*/
#include "postgres.h"
+#include "access/attmap.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/heapam_xlog.h"
#include "access/relscan.h"
#include "access/sysattr.h"
#include "access/tableam.h"
-#include "access/tupconvert.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/catalog.h"
foreach(cell, idxlist)
{
Relation idxRel = index_open(lfirst_oid(cell), AccessShareLock);
- AttrNumber *attmap;
+ AttrMap *attmap;
IndexStmt *idxstmt;
Oid constraintOid;
}
}
- attmap = convert_tuples_by_name_map(RelationGetDescr(rel),
- RelationGetDescr(parent));
+ attmap = build_attrmap_by_name(RelationGetDescr(rel),
+ RelationGetDescr(parent));
idxstmt =
generateClonedIndexStmt(NULL, idxRel,
- attmap, RelationGetDescr(parent)->natts,
- &constraintOid);
+ attmap, &constraintOid);
DefineIndex(RelationGetRelid(rel),
idxstmt,
InvalidOid,
Relation relation;
TupleDesc tupleDesc;
TupleConstr *constr;
- AttrNumber *newattno;
+ AttrMap *newattmap;
AttrNumber parent_attno;
/* caller already got lock */
constr = tupleDesc->constr;
/*
- * newattno[] will contain the child-table attribute numbers for the
- * attributes of this parent table. (They are not the same for
- * parents after the first one, nor if we have dropped columns.)
+ * newattmap->attnums[] will contain the child-table attribute numbers
+ * for the attributes of this parent table. (They are not the same
+ * for parents after the first one, nor if we have dropped columns.)
*/
- newattno = (AttrNumber *)
- palloc0(tupleDesc->natts * sizeof(AttrNumber));
+ newattmap = make_attrmap(tupleDesc->natts);
for (parent_attno = 1; parent_attno <= tupleDesc->natts;
parent_attno++)
* Ignore dropped columns in the parent.
*/
if (attribute->attisdropped)
- continue; /* leave newattno entry as zero */
+ continue; /* leave newattmap->attnums entry as zero */
/*
* Does it conflict with some previously inherited column?
/* Merge of NOT NULL constraints = OR 'em together */
def->is_not_null |= attribute->attnotnull;
/* Default and other constraints are handled below */
- newattno[parent_attno - 1] = exist_attno;
+ newattmap->attnums[parent_attno - 1] = exist_attno;
/* Check for GENERATED conflicts */
if (def->generated != attribute->attgenerated)
def->constraints = NIL;
def->location = -1;
inhSchema = lappend(inhSchema, def);
- newattno[parent_attno - 1] = ++child_attno;
+ newattmap->attnums[parent_attno - 1] = ++child_attno;
}
/*
/*
* Now copy the CHECK constraints of this parent, adjusting attnos
- * using the completed newattno[] map. Identically named constraints
+ * using the completed newattmap map. Identically named constraints
* are merged if possible, else we throw error.
*/
if (constr && constr->num_check > 0)
/* Adjust Vars to match new table's column numbering */
expr = map_variable_attnos(stringToNode(check[i].ccbin),
1, 0,
- newattno, tupleDesc->natts,
+ newattmap,
InvalidOid, &found_whole_row);
/*
}
}
- pfree(newattno);
+ free_attrmap(newattmap);
/*
* Close the parent rel, but keep our lock on it until xact commit.
for (int i = 0; i < pd->nparts; i++)
{
Relation partRel;
- AttrNumber *map;
+ AttrMap *map;
AttrNumber *mapped_pkattnum;
Oid partIndexId;
* Map the attribute numbers in the referenced side of the FK
* definition to match the partition's column layout.
*/
- map = convert_tuples_by_name_map_if_req(RelationGetDescr(partRel),
- RelationGetDescr(pkrel));
+ map = build_attrmap_by_name_if_req(RelationGetDescr(partRel),
+ RelationGetDescr(pkrel));
if (map)
{
mapped_pkattnum = palloc(sizeof(AttrNumber) * numfks);
for (int j = 0; j < numfks; j++)
- mapped_pkattnum[j] = map[pkattnum[j] - 1];
+ mapped_pkattnum[j] = map->attnums[pkattnum[j] - 1];
}
else
mapped_pkattnum = pkattnum;
if (map)
{
pfree(mapped_pkattnum);
- pfree(map);
+ free_attrmap(map);
}
}
}
Oid partitionId = pd->oids[i];
Relation partition = table_open(partitionId, lockmode);
List *partFKs;
- AttrNumber *attmap;
+ AttrMap *attmap;
AttrNumber mapped_fkattnum[INDEX_MAX_KEYS];
bool attached;
char *conname;
CheckTableNotInUse(partition, "ALTER TABLE");
- attmap = convert_tuples_by_name_map(RelationGetDescr(partition),
- RelationGetDescr(rel));
+ attmap = build_attrmap_by_name(RelationGetDescr(partition),
+ RelationGetDescr(rel));
for (int j = 0; j < numfks; j++)
- mapped_fkattnum[j] = attmap[fkattnum[j] - 1];
+ mapped_fkattnum[j] = attmap->attnums[fkattnum[j] - 1];
/* Check whether an existing constraint can be repurposed */
partFKs = copyObject(RelationGetFKeyList(partition));
CloneFkReferenced(Relation parentRel, Relation partitionRel)
{
Relation pg_constraint;
- AttrNumber *attmap;
+ AttrMap *attmap;
ListCell *cell;
SysScanDesc scan;
ScanKeyData key[2];
systable_endscan(scan);
table_close(pg_constraint, RowShareLock);
- attmap = convert_tuples_by_name_map(RelationGetDescr(partitionRel),
- RelationGetDescr(parentRel));
+ attmap = build_attrmap_by_name(RelationGetDescr(partitionRel),
+ RelationGetDescr(parentRel));
foreach(cell, clone)
{
Oid constrOid = lfirst_oid(cell);
conpfeqop,
conppeqop,
conffeqop);
+ Assert(numfks == attmap->maplen);
for (int i = 0; i < numfks; i++)
- mapped_confkey[i] = attmap[confkey[i] - 1];
+ mapped_confkey[i] = attmap->attnums[confkey[i] - 1];
fkconstraint = makeNode(Constraint);
/* for now this is all we need */
static void
CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
{
- AttrNumber *attmap;
+ AttrMap *attmap;
List *partFKs;
List *clone = NIL;
ListCell *cell;
* The constraint key may differ, if the columns in the partition are
* different. This map is used to convert them.
*/
- attmap = convert_tuples_by_name_map(RelationGetDescr(partRel),
- RelationGetDescr(parentRel));
+ attmap = build_attrmap_by_name(RelationGetDescr(partRel),
+ RelationGetDescr(parentRel));
partFKs = copyObject(RelationGetFKeyList(partRel));
DeconstructFkConstraintRow(tuple, &numfks, conkey, confkey,
conpfeqop, conppeqop, conffeqop);
for (int i = 0; i < numfks; i++)
- mapped_conkey[i] = attmap[conkey[i] - 1];
+ mapped_conkey[i] = attmap->attnums[conkey[i] - 1];
/*
* Before creating a new constraint, see whether any existing FKs are
*/
if (def->cooked_default)
{
- AttrNumber *attmap;
+ AttrMap *attmap;
bool found_whole_row;
/* create a copy to scribble on */
cmd = copyObject(cmd);
- attmap = convert_tuples_by_name_map(RelationGetDescr(childrel),
- RelationGetDescr(rel));
+ attmap = build_attrmap_by_name(RelationGetDescr(childrel),
+ RelationGetDescr(rel));
((ColumnDef *) cmd->def)->cooked_default =
map_variable_attnos(def->cooked_default,
1, 0,
- attmap, RelationGetDescr(rel)->natts,
+ attmap,
InvalidOid, &found_whole_row);
if (found_whole_row)
ereport(ERROR,
Oid idx = lfirst_oid(cell);
Relation idxRel = index_open(idx, AccessShareLock);
IndexInfo *info;
- AttrNumber *attmap;
+ AttrMap *attmap;
bool found = false;
Oid constraintOid;
/* construct an indexinfo to compare existing indexes against */
info = BuildIndexInfo(idxRel);
- attmap = convert_tuples_by_name_map(RelationGetDescr(attachrel),
- RelationGetDescr(rel));
+ attmap = build_attrmap_by_name(RelationGetDescr(attachrel),
+ RelationGetDescr(rel));
constraintOid = get_relation_idx_constraint_oid(RelationGetRelid(rel), idx);
/*
idxRel->rd_indcollation,
attachrelIdxRels[i]->rd_opfamily,
idxRel->rd_opfamily,
- attmap,
- RelationGetDescr(rel)->natts))
+ attmap))
{
/*
* If this index is being created in the parent because of a
stmt = generateClonedIndexStmt(NULL,
idxRel, attmap,
- RelationGetDescr(rel)->natts,
&constraintOid);
DefineIndex(RelationGetRelid(attachrel), stmt, InvalidOid,
RelationGetRelid(idxRel),
{
IndexInfo *childInfo;
IndexInfo *parentInfo;
- AttrNumber *attmap;
+ AttrMap *attmap;
bool found;
int i;
PartitionDesc partDesc;
/* Ensure the indexes are compatible */
childInfo = BuildIndexInfo(partIdx);
parentInfo = BuildIndexInfo(parentIdx);
- attmap = convert_tuples_by_name_map(RelationGetDescr(partTbl),
- RelationGetDescr(parentTbl));
+ attmap = build_attrmap_by_name(RelationGetDescr(partTbl),
+ RelationGetDescr(parentTbl));
if (!CompareIndexInfo(childInfo, parentInfo,
partIdx->rd_indcollation,
parentIdx->rd_indcollation,
partIdx->rd_opfamily,
parentIdx->rd_opfamily,
- attmap,
- RelationGetDescr(parentTbl)->natts))
+ attmap))
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("cannot attach index \"%s\" as a partition of index \"%s\"",
ConstraintSetParentConstraint(cldConstrId, constraintOid,
RelationGetRelid(partTbl));
- pfree(attmap);
+ free_attrmap(attmap);
validatePartitionedIndex(parentIdx, parentTbl);
}
if (resultRelInfo->ri_PartitionRoot)
{
TupleDesc old_tupdesc;
- AttrNumber *map;
+ AttrMap *map;
root_relid = RelationGetRelid(resultRelInfo->ri_PartitionRoot);
tupdesc = RelationGetDescr(resultRelInfo->ri_PartitionRoot);
old_tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
/* a reverse map */
- map = convert_tuples_by_name_map_if_req(old_tupdesc, tupdesc);
+ map = build_attrmap_by_name_if_req(old_tupdesc, tupdesc);
/*
* Partition-specific slot's tupdesc can't be changed, so allocate a
*/
if (resultRelInfo->ri_PartitionRoot)
{
- AttrNumber *map;
+ AttrMap *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
- map = convert_tuples_by_name_map_if_req(orig_tupdesc,
- tupdesc);
+ map = build_attrmap_by_name_if_req(orig_tupdesc,
+ tupdesc);
/*
* Partition-specific slot's tupdesc can't be changed, so
if (resultRelInfo->ri_PartitionRoot)
{
TupleDesc old_tupdesc = RelationGetDescr(rel);
- AttrNumber *map;
+ AttrMap *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
- map = convert_tuples_by_name_map_if_req(old_tupdesc,
- tupdesc);
+ map = build_attrmap_by_name_if_req(old_tupdesc,
+ tupdesc);
/*
* Partition-specific slot's tupdesc can't be changed, so
if (resultRelInfo->ri_PartitionRoot)
{
TupleDesc old_tupdesc = RelationGetDescr(rel);
- AttrNumber *map;
+ AttrMap *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
- map = convert_tuples_by_name_map_if_req(old_tupdesc,
- tupdesc);
+ map = build_attrmap_by_name_if_req(old_tupdesc,
+ tupdesc);
/*
* Partition-specific slot's tupdesc can't be changed,
List *keystate; /* list of ExprState */
PartitionDesc partdesc;
TupleTableSlot *tupslot;
- AttrNumber *tupmap;
+ AttrMap *tupmap;
int indexes[FLEXIBLE_ARRAY_MEMBER];
} PartitionDispatchData;
dispatch = pd[0];
while (true)
{
- AttrNumber *map = dispatch->tupmap;
+ AttrMap *map = dispatch->tupmap;
int partidx = -1;
CHECK_FOR_INTERRUPTS();
Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
ResultRelInfo *leaf_part_rri;
MemoryContext oldcxt;
- AttrNumber *part_attnos = NULL;
+ AttrMap *part_attmap = NULL;
bool found_whole_row;
oldcxt = MemoryContextSwitchTo(proute->memcxt);
/*
* Convert Vars in it to contain this partition's attribute numbers.
*/
- part_attnos =
- convert_tuples_by_name_map(RelationGetDescr(partrel),
- RelationGetDescr(firstResultRel));
+ part_attmap =
+ build_attrmap_by_name(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel));
wcoList = (List *)
map_variable_attnos((Node *) wcoList,
firstVarno, 0,
- part_attnos,
- RelationGetDescr(firstResultRel)->natts,
+ part_attmap,
RelationGetForm(partrel)->reltype,
&found_whole_row);
/* We ignore the value of found_whole_row. */
/*
* Convert Vars in it to contain this partition's attribute numbers.
*/
- if (part_attnos == NULL)
- part_attnos =
- convert_tuples_by_name_map(RelationGetDescr(partrel),
- RelationGetDescr(firstResultRel));
+ if (part_attmap == NULL)
+ part_attmap =
+ build_attrmap_by_name(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel));
returningList = (List *)
map_variable_attnos((Node *) returningList,
firstVarno, 0,
- part_attnos,
- RelationGetDescr(firstResultRel)->natts,
+ part_attmap,
RelationGetForm(partrel)->reltype,
&found_whole_row);
/* We ignore the value of found_whole_row. */
* target relation (firstVarno).
*/
onconflset = (List *) copyObject((Node *) node->onConflictSet);
- if (part_attnos == NULL)
- part_attnos =
- convert_tuples_by_name_map(RelationGetDescr(partrel),
- RelationGetDescr(firstResultRel));
+ if (part_attmap == NULL)
+ part_attmap =
+ build_attrmap_by_name(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel));
onconflset = (List *)
map_variable_attnos((Node *) onconflset,
INNER_VAR, 0,
- part_attnos,
- RelationGetDescr(firstResultRel)->natts,
+ part_attmap,
RelationGetForm(partrel)->reltype,
&found_whole_row);
/* We ignore the value of found_whole_row. */
onconflset = (List *)
map_variable_attnos((Node *) onconflset,
firstVarno, 0,
- part_attnos,
- RelationGetDescr(firstResultRel)->natts,
+ part_attmap,
RelationGetForm(partrel)->reltype,
&found_whole_row);
/* We ignore the value of found_whole_row. */
clause = (List *)
map_variable_attnos((Node *) clause,
INNER_VAR, 0,
- part_attnos,
- RelationGetDescr(firstResultRel)->natts,
+ part_attmap,
RelationGetForm(partrel)->reltype,
&found_whole_row);
/* We ignore the value of found_whole_row. */
clause = (List *)
map_variable_attnos((Node *) clause,
firstVarno, 0,
- part_attnos,
- RelationGetDescr(firstResultRel)->natts,
+ part_attmap,
RelationGetForm(partrel)->reltype,
&found_whole_row);
/* We ignore the value of found_whole_row. */
* tuple descriptor when computing its partition key for tuple
* routing.
*/
- pd->tupmap = convert_tuples_by_name_map_if_req(RelationGetDescr(parent_pd->reldesc),
- tupdesc);
+ pd->tupmap = build_attrmap_by_name_if_req(RelationGetDescr(parent_pd->reldesc),
+ tupdesc);
pd->tupslot = pd->tupmap ?
MakeSingleTupleTableSlot(tupdesc, &TTSOpsVirtual) : NULL;
}
{
List *new_tlist = NIL;
TupleDesc tupdesc = map->outdesc;
- AttrNumber *attrMap = map->attrMap;
+ AttrMap *attrMap = map->attrMap;
AttrNumber attrno;
+ Assert(tupdesc->natts == attrMap->maplen);
for (attrno = 1; attrno <= tupdesc->natts; attrno++)
{
Form_pg_attribute att_tup = TupleDescAttr(tupdesc, attrno - 1);
TargetEntry *tle;
- if (attrMap[attrno - 1] != InvalidAttrNumber)
+ if (attrMap->attnums[attrno - 1] != InvalidAttrNumber)
{
Assert(!att_tup->attisdropped);
* Use the corresponding entry from the parent's tlist, adjusting
* the resno the match the partition's attno.
*/
- tle = (TargetEntry *) list_nth(tlist, attrMap[attrno - 1] - 1);
+ tle = (TargetEntry *) list_nth(tlist, attrMap->attnums[attrno - 1] - 1);
tle->resno = attrno;
}
else
#include "access/htup_details.h"
#include "access/nbtree.h"
-#include "access/tupconvert.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_type.h"
#include "executor/execExpr.h"
Relation relation;
TupleDesc tupleDesc;
TupleConstr *constr;
- AttrNumber *attmap;
+ AttrMap *attmap;
AclResult aclresult;
char *comment;
ParseCallbackState pcbstate;
* since dropped columns in the source table aren't copied, so the new
* table can have different column numbers.
*/
- attmap = (AttrNumber *) palloc0(sizeof(AttrNumber) * tupleDesc->natts);
+ attmap = make_attrmap(tupleDesc->natts);
/*
* Insert the copied attributes into the cxt for the new table definition.
*/
cxt->columns = lappend(cxt->columns, def);
- attmap[parent_attno - 1] = list_length(cxt->columns);
+ attmap->attnums[parent_attno - 1] = list_length(cxt->columns);
/*
* Copy default, if present and it should be copied. We have separate
def->cooked_default = map_variable_attnos(this_default,
1, 0,
- attmap, tupleDesc->natts,
+ attmap,
InvalidOid, &found_whole_row);
/*
ccbin_node = map_variable_attnos(stringToNode(ccbin),
1, 0,
- attmap, tupleDesc->natts,
+ attmap,
InvalidOid, &found_whole_row);
/*
/* Build CREATE INDEX statement to recreate the parent_index */
index_stmt = generateClonedIndexStmt(cxt->relation,
parent_index,
- attmap, tupleDesc->natts,
+ attmap,
NULL);
/* Copy comment on index, if requested */
*/
IndexStmt *
generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
- const AttrNumber *attmap, int attmap_length,
+ const AttrMap *attmap,
Oid *constraintOid)
{
Oid source_relid = RelationGetRelid(source_idx);
/* Adjust Vars to match new table's column numbering */
indexkey = map_variable_attnos(indexkey,
1, 0,
- attmap, attmap_length,
+ attmap,
InvalidOid, &found_whole_row);
/* As in transformTableLikeClause, reject whole-row variables */
/* Adjust Vars to match new table's column numbering */
pred_tree = map_variable_attnos(pred_tree,
1, 0,
- attmap, attmap_length,
+ attmap,
InvalidOid, &found_whole_row);
/* As in transformTableLikeClause, reject whole-row variables */
*/
desc = RelationGetDescr(entry->localrel);
oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
- entry->attrmap = palloc(desc->natts * sizeof(AttrNumber));
+ entry->attrmap = make_attrmap(desc->natts);
MemoryContextSwitchTo(oldctx);
found = 0;
if (attr->attisdropped || attr->attgenerated)
{
- entry->attrmap[i] = -1;
+ entry->attrmap->attnums[i] = -1;
continue;
}
attnum = logicalrep_rel_att_by_name(remoterel,
NameStr(attr->attname));
- entry->attrmap[i] = attnum;
+ entry->attrmap->attnums[i] = attnum;
if (attnum >= 0)
found++;
}
attnum = AttrNumberGetAttrOffset(attnum);
- if (entry->attrmap[attnum] < 0 ||
- !bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
+ if (entry->attrmap->attnums[attnum] < 0 ||
+ !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
{
entry->updatable = false;
break;
defmap = (int *) palloc(num_phys_attrs * sizeof(int));
defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
+ Assert(rel->attrmap->maplen == num_phys_attrs);
for (attnum = 0; attnum < num_phys_attrs; attnum++)
{
Expr *defexpr;
if (TupleDescAttr(desc, attnum)->attisdropped || TupleDescAttr(desc, attnum)->attgenerated)
continue;
- if (rel->attrmap[attnum] >= 0)
+ if (rel->attrmap->attnums[attnum] >= 0)
continue;
defexpr = (Expr *) build_column_default(rel->localrel, attnum + 1);
error_context_stack = &errcallback;
/* Call the "in" function for each non-dropped attribute */
+ Assert(natts == rel->attrmap->maplen);
for (i = 0; i < natts; i++)
{
Form_pg_attribute att = TupleDescAttr(slot->tts_tupleDescriptor, i);
- int remoteattnum = rel->attrmap[i];
+ int remoteattnum = rel->attrmap->attnums[i];
if (!att->attisdropped && remoteattnum >= 0 &&
values[remoteattnum] != NULL)
error_context_stack = &errcallback;
/* Call the "in" function for each replaced attribute */
+ Assert(natts == rel->attrmap->maplen);
for (i = 0; i < natts; i++)
{
Form_pg_attribute att = TupleDescAttr(slot->tts_tupleDescriptor, i);
- int remoteattnum = rel->attrmap[i];
+ int remoteattnum = rel->attrmap->attnums[i];
if (remoteattnum < 0)
continue;
{
int target_varno; /* RTE index to search for */
int sublevels_up; /* (current) nesting depth */
- const AttrNumber *attno_map; /* map array for user attnos */
- int map_length; /* number of entries in attno_map[] */
+ const AttrMap *attno_map; /* map array for user attnos */
Oid to_rowtype; /* change whole-row Vars to this type */
bool *found_whole_row; /* output flag */
} map_variable_attnos_context;
if (attno > 0)
{
/* user-defined column, replace attno */
- if (attno > context->map_length ||
- context->attno_map[attno - 1] == 0)
+ if (attno > context->attno_map->maplen ||
+ context->attno_map->attnums[attno - 1] == 0)
elog(ERROR, "unexpected varattno %d in expression to be mapped",
attno);
- newvar->varattno = newvar->varoattno = context->attno_map[attno - 1];
+ newvar->varattno = newvar->varoattno = context->attno_map->attnums[attno - 1];
}
else if (attno == 0)
{
Node *
map_variable_attnos(Node *node,
int target_varno, int sublevels_up,
- const AttrNumber *attno_map, int map_length,
+ const AttrMap *attno_map,
Oid to_rowtype, bool *found_whole_row)
{
map_variable_attnos_context context;
context.target_varno = target_varno;
context.sublevels_up = sublevels_up;
context.attno_map = attno_map;
- context.map_length = map_length;
context.to_rowtype = to_rowtype;
context.found_whole_row = found_whole_row;
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * attmap.h
+ * Definitions for PostgreSQL attribute mappings
+ *
+ *
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/access/attmap.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef ATTMAP_H
+#define ATTMAP_H
+
+#include "access/attnum.h"
+#include "access/tupdesc.h"
+
+/*
+ * Attribute mapping structure
+ *
+ * This maps attribute numbers between a pair of relations, designated
+ * 'input' and 'output' (most typically inheritance parent and child
+ * relations), whose common columns may have different attribute numbers.
+ * Such difference may arise due to the columns being ordered differently
+ * in the two relations or the two relations having dropped columns at
+ * different positions.
+ *
+ * 'maplen' is set to the number of attributes of the 'output' relation,
+ * taking into account any of its dropped attributes, with the corresponding
+ * elements of the 'attnums' array set to 0.
+ */
+typedef struct AttrMap
+{
+ AttrNumber *attnums;
+ int maplen;
+} AttrMap;
+
+extern AttrMap *make_attrmap(int maplen);
+extern void free_attrmap(AttrMap *map);
+
+/* Convertion routines to build mappings */
+extern AttrMap *build_attrmap_by_name(TupleDesc indesc,
+ TupleDesc outdesc);
+extern AttrMap *build_attrmap_by_name_if_req(TupleDesc indesc,
+ TupleDesc outdesc);
+extern AttrMap *build_attrmap_by_position(TupleDesc indesc,
+ TupleDesc outdesc,
+ const char *msg);
+
+#endif /* ATTMAP_H */
#ifndef TUPCONVERT_H
#define TUPCONVERT_H
+#include "access/attmap.h"
#include "access/htup.h"
#include "access/tupdesc.h"
#include "executor/tuptable.h"
{
TupleDesc indesc; /* tupdesc for source rowtype */
TupleDesc outdesc; /* tupdesc for result rowtype */
- AttrNumber *attrMap; /* indexes of input fields, or 0 for null */
+ AttrMap *attrMap; /* indexes of input fields, or 0 for null */
Datum *invalues; /* workspace for deconstructing source */
bool *inisnull;
Datum *outvalues; /* workspace for constructing result */
extern TupleConversionMap *convert_tuples_by_name(TupleDesc indesc,
TupleDesc outdesc);
-extern AttrNumber *convert_tuples_by_name_map(TupleDesc indesc,
- TupleDesc outdesc);
-extern AttrNumber *convert_tuples_by_name_map_if_req(TupleDesc indesc,
- TupleDesc outdesc);
-
extern HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map);
-extern TupleTableSlot *execute_attr_map_slot(AttrNumber *attrMap,
- TupleTableSlot *in_slot, TupleTableSlot *out_slot);
+extern TupleTableSlot *execute_attr_map_slot(AttrMap *attrMap,
+ TupleTableSlot *in_slot,
+ TupleTableSlot *out_slot);
extern void free_conversion_map(TupleConversionMap *map);
extern bool CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
Oid *collations1, Oid *collations2,
Oid *opfamilies1, Oid *opfamilies2,
- AttrNumber *attmap, int maplen);
+ AttrMap *attmap);
extern void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii);
#include "parser/parse_node.h"
+typedef struct AttrMap AttrMap;
extern List *transformCreateStmt(CreateStmt *stmt, const char *queryString);
extern List *transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
PartitionBoundSpec *spec);
extern IndexStmt *generateClonedIndexStmt(RangeVar *heapRel,
Relation source_idx,
- const AttrNumber *attmap, int attmap_length,
+ const AttrMap *attmap,
Oid *constraintOid);
#endif /* PARSE_UTILCMD_H */
#ifndef LOGICALRELATION_H
#define LOGICALRELATION_H
+#include "access/attmap.h"
#include "replication/logicalproto.h"
typedef struct LogicalRepRelMapEntry
/* Mapping to local relation, filled as needed. */
Oid localreloid; /* local relation id */
Relation localrel; /* relcache entry */
- AttrNumber *attrmap; /* map of local attributes to remote ones */
+ AttrMap *attrmap; /* map of local attributes to remote ones */
bool updatable; /* Can apply updates/deletes? */
/* Sync state. */
#include "nodes/parsenodes.h"
+typedef struct AttrMap AttrMap;
typedef struct replace_rte_variables_context replace_rte_variables_context;
typedef Node *(*replace_rte_variables_callback) (Var *var,
extern Node *map_variable_attnos(Node *node,
int target_varno, int sublevels_up,
- const AttrNumber *attno_map, int map_length,
+ const AttrMap *attno_map,
Oid to_rowtype, bool *found_whole_row);
extern Node *ReplaceVarsFromTargetList(Node *node,
AttoptCacheKey
AttrDefInfo
AttrDefault
+AttrMap
AttrMissing
AttrNumber
AttributeOpts