Skip to content

Commit 50c19fc

Browse files
committed
Fix contrib/postgres_fdw's handling of column defaults.
Adopt the position that only locally-defined defaults matter. Any defaults defined in the remote database do not affect insertions performed through a foreign table (unless they are for columns not known to the foreign table). While it'd arguably be more useful to permit remote defaults to be used, making that work in a consistent fashion requires far more work than seems possible for 9.3.
1 parent a0c6dfe commit 50c19fc

File tree

5 files changed

+205
-202
lines changed

5 files changed

+205
-202
lines changed

contrib/postgres_fdw/deparse.c

+17-38
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void deparseReturningList(StringInfo buf, PlannerInfo *root,
7777
List *returningList);
7878
static void deparseColumnRef(StringInfo buf, int varno, int varattno,
7979
PlannerInfo *root);
80-
static void deparseRelation(StringInfo buf, Oid relid);
80+
static void deparseRelation(StringInfo buf, Relation rel);
8181
static void deparseStringLiteral(StringInfo buf, const char *val);
8282
static void deparseExpr(StringInfo buf, Expr *expr, PlannerInfo *root);
8383
static void deparseVar(StringInfo buf, Var *node, PlannerInfo *root);
@@ -387,7 +387,7 @@ deparseSelectSql(StringInfo buf,
387387
* Construct FROM clause
388388
*/
389389
appendStringInfoString(buf, " FROM ");
390-
deparseRelation(buf, RelationGetRelid(rel));
390+
deparseRelation(buf, rel);
391391

392392
heap_close(rel, NoLock);
393393
}
@@ -499,18 +499,16 @@ appendWhereClause(StringInfo buf,
499499
* deparse remote INSERT statement
500500
*/
501501
void
502-
deparseInsertSql(StringInfo buf, PlannerInfo *root, Index rtindex,
502+
deparseInsertSql(StringInfo buf, PlannerInfo *root,
503+
Index rtindex, Relation rel,
503504
List *targetAttrs, List *returningList)
504505
{
505-
RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
506-
Relation rel = heap_open(rte->relid, NoLock);
507-
TupleDesc tupdesc = RelationGetDescr(rel);
508506
AttrNumber pindex;
509507
bool first;
510508
ListCell *lc;
511509

512510
appendStringInfoString(buf, "INSERT INTO ");
513-
deparseRelation(buf, rte->relid);
511+
deparseRelation(buf, rel);
514512

515513
if (targetAttrs)
516514
{
@@ -520,9 +518,6 @@ deparseInsertSql(StringInfo buf, PlannerInfo *root, Index rtindex,
520518
foreach(lc, targetAttrs)
521519
{
522520
int attnum = lfirst_int(lc);
523-
Form_pg_attribute attr = tupdesc->attrs[attnum - 1];
524-
525-
Assert(!attr->attisdropped);
526521

527522
if (!first)
528523
appendStringInfoString(buf, ", ");
@@ -552,36 +547,29 @@ deparseInsertSql(StringInfo buf, PlannerInfo *root, Index rtindex,
552547

553548
if (returningList)
554549
deparseReturningList(buf, root, rtindex, rel, returningList);
555-
556-
heap_close(rel, NoLock);
557550
}
558551

559552
/*
560553
* deparse remote UPDATE statement
561554
*/
562555
void
563-
deparseUpdateSql(StringInfo buf, PlannerInfo *root, Index rtindex,
556+
deparseUpdateSql(StringInfo buf, PlannerInfo *root,
557+
Index rtindex, Relation rel,
564558
List *targetAttrs, List *returningList)
565559
{
566-
RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
567-
Relation rel = heap_open(rte->relid, NoLock);
568-
TupleDesc tupdesc = RelationGetDescr(rel);
569560
AttrNumber pindex;
570561
bool first;
571562
ListCell *lc;
572563

573564
appendStringInfoString(buf, "UPDATE ");
574-
deparseRelation(buf, rte->relid);
565+
deparseRelation(buf, rel);
575566
appendStringInfoString(buf, " SET ");
576567

577568
pindex = 2; /* ctid is always the first param */
578569
first = true;
579570
foreach(lc, targetAttrs)
580571
{
581572
int attnum = lfirst_int(lc);
582-
Form_pg_attribute attr = tupdesc->attrs[attnum - 1];
583-
584-
Assert(!attr->attisdropped);
585573

586574
if (!first)
587575
appendStringInfoString(buf, ", ");
@@ -595,30 +583,22 @@ deparseUpdateSql(StringInfo buf, PlannerInfo *root, Index rtindex,
595583

596584
if (returningList)
597585
deparseReturningList(buf, root, rtindex, rel, returningList);
598-
599-
heap_close(rel, NoLock);
600586
}
601587

602588
/*
603589
* deparse remote DELETE statement
604590
*/
605591
void
606-
deparseDeleteSql(StringInfo buf, PlannerInfo *root, Index rtindex,
592+
deparseDeleteSql(StringInfo buf, PlannerInfo *root,
593+
Index rtindex, Relation rel,
607594
List *returningList)
608595
{
609-
RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
610-
611596
appendStringInfoString(buf, "DELETE FROM ");
612-
deparseRelation(buf, rte->relid);
597+
deparseRelation(buf, rel);
613598
appendStringInfoString(buf, " WHERE ctid = $1");
614599

615600
if (returningList)
616-
{
617-
Relation rel = heap_open(rte->relid, NoLock);
618-
619601
deparseReturningList(buf, root, rtindex, rel, returningList);
620-
heap_close(rel, NoLock);
621-
}
622602
}
623603

624604
/*
@@ -653,12 +633,11 @@ deparseReturningList(StringInfo buf, PlannerInfo *root,
653633
void
654634
deparseAnalyzeSizeSql(StringInfo buf, Relation rel)
655635
{
656-
Oid relid = RelationGetRelid(rel);
657636
StringInfoData relname;
658637

659638
/* We'll need the remote relation name as a literal. */
660639
initStringInfo(&relname);
661-
deparseRelation(&relname, relid);
640+
deparseRelation(&relname, rel);
662641

663642
appendStringInfo(buf, "SELECT pg_catalog.pg_relation_size(");
664643
deparseStringLiteral(buf, relname.data);
@@ -718,7 +697,7 @@ deparseAnalyzeSql(StringInfo buf, Relation rel)
718697
* Construct FROM clause
719698
*/
720699
appendStringInfoString(buf, " FROM ");
721-
deparseRelation(buf, relid);
700+
deparseRelation(buf, rel);
722701
}
723702

724703
/*
@@ -771,15 +750,15 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root)
771750
* Similarly, schema_name FDW option overrides schema name.
772751
*/
773752
static void
774-
deparseRelation(StringInfo buf, Oid relid)
753+
deparseRelation(StringInfo buf, Relation rel)
775754
{
776755
ForeignTable *table;
777756
const char *nspname = NULL;
778757
const char *relname = NULL;
779758
ListCell *lc;
780759

781760
/* obtain additional catalog information. */
782-
table = GetForeignTable(relid);
761+
table = GetForeignTable(RelationGetRelid(rel));
783762

784763
/*
785764
* Use value of FDW options if any, instead of the name of object itself.
@@ -799,9 +778,9 @@ deparseRelation(StringInfo buf, Oid relid)
799778
* that doesn't seem worth the trouble.
800779
*/
801780
if (nspname == NULL)
802-
nspname = get_namespace_name(get_rel_namespace(relid));
781+
nspname = get_namespace_name(RelationGetNamespace(rel));
803782
if (relname == NULL)
804-
relname = get_rel_name(relid);
783+
relname = RelationGetRelationName(rel);
805784

806785
appendStringInfo(buf, "%s.%s",
807786
quote_identifier(nspname), quote_identifier(relname));

0 commit comments

Comments
 (0)