blob: ce00ac34cc405ea1886cf81390456582610a9d99 [file] [log] [blame]
From 5c17cb0aa7f06bb3ac15d4ca72a37f59a10fa59e Mon Sep 17 00:00:00 2001
From: Sven Klemm <sven@timescale.com>
Date: Thu, 24 Sep 2020 15:21:04 +0200
Subject: [PATCH] Adjust code to PG13 tuple conversion changes
PG13 changes the way attribute mappings are done and uses AttrMap
instead of AttrNumber[] in the new function signatures. This patch
changes ChunkInsertState to use TupleConversionMap which abstracts
this change.
https://github.com/postgres/postgres/commit/e1551f96e6
Signed-off-by: Maxim Kochetkov <fido_max@inbox.ru>
Fetch from: https://github.com/timescale/timescaledb/commit/2cb7c41276c8f8112bd225a020fef709a8e776ff.patch
---
src/chunk_insert_state.c | 92 +++++++++++++++++++++------------------
src/compat.h | 15 +++++++
tsl/src/fdw/modify_exec.c | 4 ++
3 files changed, 69 insertions(+), 42 deletions(-)
diff --git a/src/chunk_insert_state.c b/src/chunk_insert_state.c
index 5dc294df..f8200dd8 100644
--- a/src/chunk_insert_state.c
+++ b/src/chunk_insert_state.c
@@ -120,7 +120,7 @@ create_chunk_result_relation_info(ChunkDispatch *dispatch, Relation rel)
static ProjectionInfo *
get_adjusted_projection_info_returning(ProjectionInfo *orig, List *returning_clauses,
- AttrNumber *map, int map_size, Index varno, Oid rowtype,
+ TupleConversionMap *map, Index varno, Oid rowtype,
TupleDesc chunk_desc)
{
bool found_whole_row;
@@ -128,14 +128,15 @@ get_adjusted_projection_info_returning(ProjectionInfo *orig, List *returning_cla
Assert(returning_clauses != NIL);
/* map hypertable attnos -> chunk attnos */
- returning_clauses = castNode(List,
- map_variable_attnos((Node *) returning_clauses,
- varno,
- 0,
- map,
- map_size,
- rowtype,
- &found_whole_row));
+ if (map != NULL)
+ returning_clauses = castNode(List,
+ map_variable_attnos_compat((Node *) returning_clauses,
+ varno,
+ 0,
+ map->attrMap,
+ map->outdesc->natts,
+ rowtype,
+ &found_whole_row));
return ExecBuildProjectionInfo(returning_clauses,
orig->pi_exprContext,
@@ -145,7 +146,7 @@ get_adjusted_projection_info_returning(ProjectionInfo *orig, List *returning_cla
}
static List *
-translate_clause(List *inclause, AttrNumber *chunk_attnos, Index varno, Relation hyper_rel,
+translate_clause(List *inclause, TupleConversionMap *chunk_map, Index varno, Relation hyper_rel,
Relation chunk_rel)
{
List *clause = copyObject(inclause);
@@ -153,23 +154,23 @@ translate_clause(List *inclause, AttrNumber *chunk_attnos, Index varno, Relation
/* map hypertable attnos -> chunk attnos for the "excluded" table */
clause = castNode(List,
- map_variable_attnos((Node *) clause,
- INNER_VAR,
- 0,
- chunk_attnos,
- RelationGetDescr(hyper_rel)->natts,
- RelationGetForm(chunk_rel)->reltype,
- &found_whole_row));
+ map_variable_attnos_compat((Node *) clause,
+ INNER_VAR,
+ 0,
+ chunk_map->attrMap,
+ RelationGetDescr(hyper_rel)->natts,
+ RelationGetForm(chunk_rel)->reltype,
+ &found_whole_row));
/* map hypertable attnos -> chunk attnos for the hypertable */
clause = castNode(List,
- map_variable_attnos((Node *) clause,
- varno,
- 0,
- chunk_attnos,
- RelationGetDescr(hyper_rel)->natts,
- RelationGetForm(chunk_rel)->reltype,
- &found_whole_row));
+ map_variable_attnos_compat((Node *) clause,
+ varno,
+ 0,
+ chunk_map->attrMap,
+ RelationGetDescr(hyper_rel)->natts,
+ RelationGetForm(chunk_rel)->reltype,
+ &found_whole_row));
return clause;
}
@@ -193,7 +194,11 @@ adjust_hypertable_tlist(List *tlist, TupleConversionMap *map)
{
List *new_tlist = NIL;
TupleDesc chunk_tupdesc = map->outdesc;
+#if PG13_GE
+ AttrNumber *attrMap = map->attrMap->attnums;
+#else
AttrNumber *attrMap = map->attrMap;
+#endif
AttrNumber chunk_attrno;
for (chunk_attrno = 1; chunk_attrno <= chunk_tupdesc->natts; chunk_attrno++)
@@ -351,7 +356,8 @@ get_default_existing_slot(ChunkInsertState *state, ChunkDispatch *dispatch)
* columns, etc.
*/
static void
-setup_on_conflict_state(ChunkInsertState *state, ChunkDispatch *dispatch, AttrNumber *chunk_attnos)
+setup_on_conflict_state(ChunkInsertState *state, ChunkDispatch *dispatch,
+ TupleConversionMap *chunk_map)
{
TupleConversionMap *map = state->hyper_to_chunk_map;
ResultRelInfo *chunk_rri = get_chunk_rri(state);
@@ -377,16 +383,17 @@ setup_on_conflict_state(ChunkInsertState *state, ChunkDispatch *dispatch, AttrNu
Assert(map->outdesc == RelationGetDescr(chunk_rel));
- if (NULL == chunk_attnos)
- chunk_attnos = convert_tuples_by_name_map(RelationGetDescr(chunk_rel),
- RelationGetDescr(first_rel)
+ if (NULL == chunk_map)
+ chunk_map = convert_tuples_by_name(RelationGetDescr(chunk_rel),
+ RelationGetDescr(first_rel)
#if PG13_LT
- , gettext_noop("could not convert row type")
+ ,
+ gettext_noop("could not convert row type")
#endif
- );
+ );
onconflset = translate_clause(ts_chunk_dispatch_get_on_conflict_set(dispatch),
- chunk_attnos,
+ chunk_map,
hyper_rri->ri_RangeTableIndex,
hyper_rel,
chunk_rel);
@@ -412,7 +419,7 @@ setup_on_conflict_state(ChunkInsertState *state, ChunkDispatch *dispatch, AttrNu
if (NULL != onconflict_where)
{
List *clause = translate_clause(castNode(List, onconflict_where),
- chunk_attnos,
+ chunk_map,
hyper_rri->ri_RangeTableIndex,
hyper_rel,
chunk_rel);
@@ -476,7 +483,7 @@ adjust_projections(ChunkInsertState *cis, ChunkDispatch *dispatch, Oid rowtype)
ResultRelInfo *chunk_rri = cis->result_relation_info;
Relation hyper_rel = dispatch->hypertable_result_rel_info->ri_RelationDesc;
Relation chunk_rel = cis->rel;
- AttrNumber *chunk_attnos = NULL;
+ TupleConversionMap *chunk_map = NULL;
OnConflictAction onconflict_action = ts_chunk_dispatch_get_on_conflict_action(dispatch);
if (ts_chunk_dispatch_has_returning(dispatch))
@@ -486,19 +493,19 @@ adjust_projections(ChunkInsertState *cis, ChunkDispatch *dispatch, Oid rowtype)
* to have the hypertable_desc in the out spot for map_variable_attnos
* to work correctly in mapping hypertable attnos->chunk attnos.
*/
- chunk_attnos = convert_tuples_by_name_map(RelationGetDescr(chunk_rel),
- RelationGetDescr(hyper_rel)
+ chunk_map = convert_tuples_by_name(RelationGetDescr(chunk_rel),
+ RelationGetDescr(hyper_rel)
#if PG13_LT
- ,gettext_noop("could not convert row type")
+ ,
+ gettext_noop("could not convert row type")
#endif
- );
+ );
chunk_rri->ri_projectReturning =
get_adjusted_projection_info_returning(chunk_rri->ri_projectReturning,
ts_chunk_dispatch_get_returning_clauses(
dispatch),
- chunk_attnos,
- RelationGetDescr(hyper_rel)->natts,
+ chunk_map,
dispatch->hypertable_result_rel_info
->ri_RangeTableIndex,
rowtype,
@@ -511,7 +518,7 @@ adjust_projections(ChunkInsertState *cis, ChunkDispatch *dispatch, Oid rowtype)
set_arbiter_indexes(cis, dispatch);
if (onconflict_action == ONCONFLICT_UPDATE)
- setup_on_conflict_state(cis, dispatch, chunk_attnos);
+ setup_on_conflict_state(cis, dispatch, chunk_map);
}
}
@@ -598,9 +605,10 @@ ts_chunk_insert_state_create(Chunk *chunk, ChunkDispatch *dispatch)
convert_tuples_by_name(RelationGetDescr(parent_rel),
RelationGetDescr(rel)
#if PG13_LT
- ,gettext_noop("could not convert row type")
+ ,
+ gettext_noop("could not convert row type")
#endif
- );
+ );
adjust_projections(state, dispatch, RelationGetForm(rel)->reltype);
diff --git a/src/compat.h b/src/compat.h
index 51c1c181..1b2ed8e5 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -374,4 +374,19 @@ get_vacuum_options(const VacuumStmt *stmt)
#define for_each_cell_compat(cell, list, initcell) for_each_cell ((cell), (list), (initcell))
#endif
+/* PG13 removes the natts parameter from map_variable_attnos */
+#if PG13_LT
+#define map_variable_attnos_compat(node, varno, sublevels_up, map, natts, rowtype, found_wholerow) \
+ map_variable_attnos((node), \
+ (varno), \
+ (sublevels_up), \
+ (map), \
+ (natts), \
+ (rowtype), \
+ (found_wholerow))
+#else
+#define map_variable_attnos_compat(node, varno, sublevels_up, map, natts, rowtype, found_wholerow) \
+ map_variable_attnos((node), (varno), (sublevels_up), (map), (rowtype), (found_wholerow))
+#endif
+
#endif /* TIMESCALEDB_COMPAT_H */
diff --git a/tsl/src/fdw/modify_exec.c b/tsl/src/fdw/modify_exec.c
index 38d0e0c6..5307079d 100644
--- a/tsl/src/fdw/modify_exec.c
+++ b/tsl/src/fdw/modify_exec.c
@@ -202,7 +202,11 @@ convert_attrs(TupleConversionMap *map, List *attrs)
for (i = 0; i < map->outdesc->natts; i++)
{
+#if PG13_GE
+ if (map->attrMap->attnums[i] == attnum)
+#else
if (map->attrMap[i] == attnum)
+#endif
{
new_attrs = lappend_int(new_attrs, AttrOffsetGetAttrNumber(i));
break;
--
2.29.2