Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG16: Fix multinode deparsing issues #6183

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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");

Check warning on line 1981 in tsl/src/fdw/deparse.c

View check run for this annotation

Codecov / codecov/patch

tsl/src/fdw/deparse.c#L1981

Added line #L1981 was not covered by tests
else
{
appendStringInfo(buf, " = $%d", pindex);
pindex++;
}
}
appendStringInfoString(buf, " WHERE ctid = $1");

Expand Down
112 changes: 56 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,60 @@
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 ||
fabriziomello marked this conversation as resolved.
Show resolved Hide resolved
(operation == CMD_UPDATE && rel->trigdesc && rel->trigdesc->trig_update_before_row))
{
AttrNumber attnum;

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

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

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

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

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

Check warning on line 159 in tsl/src/fdw/modify_plan.c

View check run for this annotation

Codecov / codecov/patch

tsl/src/fdw/modify_plan.c#L159

Added line #L159 was not covered by tests

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 +176,6 @@
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 +188,6 @@
switch (operation)
{
case CMD_INSERT:
target_attrs = get_insert_attrs(rel);
deparseInsertSql(&sql,
rte,
result_relation,
Expand All @@ -189,17 +200,6 @@
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
Loading
Loading