Skip to content

Commit

Permalink
PG16: Fix multinode deparsing issues
Browse files Browse the repository at this point in the history
With the changes introduced in PG16 by moving the permission checking
information out of the range table entries to a new data struct named
`RTEPermissionInfo` we can't use the rte->updatedcols anymore for the
target_attrs when deparsing, so we need to build new target_attrs based
on the `get_rel_all_updated_cols` and for our multinode implementation
we need to get rid the generated always attributes to don't risk to
build the parameters in `stmt_params_create` using this column. Postgres
FDW also have it own logic to skip generated columns as well.

postgres/postgres@a61b1f74
  • Loading branch information
fabriziomello committed Oct 10, 2023
1 parent 6af0cb0 commit 34a483d
Show file tree
Hide file tree
Showing 18 changed files with 4,952 additions and 244 deletions.
11 changes: 9 additions & 2 deletions tsl/src/fdw/deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,7 @@ void
deparseUpdateSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs,
List *returningList, List **retrieved_attrs)
{
TupleDesc tupdesc = RelationGetDescr(rel);
AttrNumber pindex;
bool first;
ListCell *lc;
Expand All @@ -1969,14 +1970,20 @@ deparseUpdateSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel
foreach (lc, targetAttrs)
{
int attnum = lfirst_int(lc);
Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);

if (!first)
appendStringInfoString(buf, ", ");
first = false;

deparseColumnRef(buf, rtindex, attnum, rte, false);
appendStringInfo(buf, " = $%d", pindex);
pindex++;
if (attr->attgenerated)
appendStringInfoString(buf, " = DEFAULT");
else
{
appendStringInfo(buf, " = $%d", pindex);
pindex++;
}
}
appendStringInfoString(buf, " WHERE ctid = $1");

Expand Down
114 changes: 58 additions & 56 deletions tsl/src/fdw/modify_plan.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,15 @@
#include <parser/parsetree.h>
#include <access/sysattr.h>
#include <utils/rel.h>
#include <optimizer/inherit.h>
#include <optimizer/pathnode.h>

#include <chunk.h>
#include "deparse.h"
#include "errors.h"
#include "modify_plan.h"
#include "ts_catalog/chunk_data_node.h"

static List *
get_insert_attrs(Relation rel)
{
TupleDesc tupdesc = RelationGetDescr(rel);
List *attrs = NIL;
int i;

for (i = 0; i < tupdesc->natts; i++)
{
Form_pg_attribute attr = TupleDescAttr(tupdesc, i);

if (!attr->attisdropped)
attrs = lappend_int(attrs, AttrOffsetGetAttrNumber(i));
}

return attrs;
}

static List *
get_update_attrs(Bitmapset *updatedCols)
{
List *attrs = NIL;
int col = -1;

while ((col = bms_next_member(updatedCols, col)) >= 0)
{
/* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
AttrNumber attno = col + FirstLowInvalidHeapAttributeNumber;

if (attno <= InvalidAttrNumber) /* shouldn't happen */
elog(ERROR, "system-column update is not supported");

attrs = lappend_int(attrs, attno);
}

return attrs;
}

/* get a list of "live" DNs associated with this chunk */
List *
get_chunk_data_nodes(Oid relid)
Expand Down Expand Up @@ -144,6 +108,62 @@ fdw_plan_foreign_modify(PlannerInfo *root, ModifyTable *plan, Index result_relat
if (plan->returningLists)
returning_list = (List *) list_nth(plan->returningLists, subplan_index);

/*
* Core code already has some lock on each rel being planned, so we can
* use NoLock here.
*/
rel = table_open(rte->relid, NoLock);
TupleDesc tupdesc = RelationGetDescr(rel);

/*
* In an INSERT, we transmit all columns that are defined in the foreign
* table. In an UPDATE, if there are BEFORE ROW UPDATE triggers on the
* foreign table, we transmit all columns like INSERT; else we transmit
* only columns that were explicitly targets of the UPDATE, so as to avoid
* unnecessary data transmission. (We can't do that for INSERT since we
* would miss sending default values for columns not listed in the source
* statement, and for UPDATE if there are BEFORE ROW UPDATE triggers since
* those triggers might change values for non-target columns, in which
* case we would miss sending changed values for those columns.)
*/
if (operation == CMD_INSERT ||
(operation == CMD_UPDATE && rel->trigdesc && rel->trigdesc->trig_update_before_row))
{
int attnum;

for (attnum = 1; attnum <= tupdesc->natts; attnum++)
{
Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);

if (!attr->attisdropped && attr->attgenerated != '\0')
target_attrs = lappend_int(target_attrs, attnum);
}
}
else if (operation == CMD_UPDATE)
{
RelOptInfo *rel = find_base_rel(root, result_relation);
Bitmapset *allUpdatedCols = NULL;
int col = -1;

allUpdatedCols = get_rel_all_updated_cols(root, rel);

while ((col = bms_next_member(allUpdatedCols, col)) >= 0)
{
/* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
AttrNumber attno = col + FirstLowInvalidHeapAttributeNumber;
Form_pg_attribute attr = TupleDescAttr(tupdesc, attno - 1);

if (attno <= InvalidAttrNumber) /* shouldn't happen */
elog(ERROR, "system-column update is not supported");

/* Ignore generated columns */
if (attr->attgenerated)
continue;

target_attrs = lappend_int(target_attrs, attno);
}
}

/*
* ON CONFLICT DO UPDATE and DO NOTHING case with inference specification
* should have already been rejected in the optimizer, as presently there
Expand All @@ -158,12 +178,6 @@ fdw_plan_foreign_modify(PlannerInfo *root, ModifyTable *plan, Index result_relat
errmsg("ON CONFLICT DO UPDATE not supported"
" on distributed hypertables")));

/*
* Core code already has some lock on each rel being planned, so we can
* use NoLock here.
*/
rel = table_open(rte->relid, NoLock);

/*
* Construct the SQL command string
*
Expand All @@ -176,7 +190,6 @@ fdw_plan_foreign_modify(PlannerInfo *root, ModifyTable *plan, Index result_relat
switch (operation)
{
case CMD_INSERT:
target_attrs = get_insert_attrs(rel);
deparseInsertSql(&sql,
rte,
result_relation,
Expand All @@ -189,17 +202,6 @@ fdw_plan_foreign_modify(PlannerInfo *root, ModifyTable *plan, Index result_relat
break;
case CMD_UPDATE:
{
#if PG16_LT
Bitmapset *updatedCols = rte->updatedCols;
#else
Bitmapset *updatedCols = NULL;
if (rte->perminfoindex > 0)
{
RTEPermissionInfo *perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
updatedCols = perminfo->updatedCols;
}
#endif
target_attrs = get_update_attrs(updatedCols);
deparseUpdateSql(&sql,
rte,
result_relation,
Expand Down
File renamed without changes.
Loading

0 comments on commit 34a483d

Please sign in to comment.