Skip to content

Commit e690b95

Browse files
committed
Avoid retrieving dummy NULL columns in postgres_fdw.
This should provide some marginal overall savings, since it surely takes many more cycles for the remote server to deal with the NULL columns than it takes for postgres_fdw not to emit them. But really the reason is to keep the emitted queries from looking quite so silly ...
1 parent 9cbc4b8 commit e690b95

File tree

5 files changed

+301
-218
lines changed

5 files changed

+301
-218
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ static void deparseTargetList(StringInfo buf,
106106
PlannerInfo *root,
107107
Index rtindex,
108108
Relation rel,
109-
Bitmapset *attrs_used);
109+
Bitmapset *attrs_used,
110+
List **retrieved_attrs);
110111
static void deparseReturningList(StringInfo buf, PlannerInfo *root,
111112
Index rtindex, Relation rel,
112-
List *returningList);
113+
List *returningList,
114+
List **retrieved_attrs);
113115
static void deparseColumnRef(StringInfo buf, int varno, int varattno,
114116
PlannerInfo *root);
115117
static void deparseRelation(StringInfo buf, Relation rel);
@@ -652,12 +654,16 @@ is_builtin(Oid oid)
652654
* Construct a simple SELECT statement that retrieves desired columns
653655
* of the specified foreign table, and append it to "buf". The output
654656
* contains just "SELECT ... FROM tablename".
657+
*
658+
* We also create an integer List of the columns being retrieved, which is
659+
* returned to *retrieved_attrs.
655660
*/
656661
void
657662
deparseSelectSql(StringInfo buf,
658663
PlannerInfo *root,
659664
RelOptInfo *baserel,
660-
Bitmapset *attrs_used)
665+
Bitmapset *attrs_used,
666+
List **retrieved_attrs)
661667
{
662668
RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
663669
Relation rel;
@@ -672,7 +678,8 @@ deparseSelectSql(StringInfo buf,
672678
* Construct SELECT list
673679
*/
674680
appendStringInfoString(buf, "SELECT ");
675-
deparseTargetList(buf, root, baserel->relid, rel, attrs_used);
681+
deparseTargetList(buf, root, baserel->relid, rel, attrs_used,
682+
retrieved_attrs);
676683

677684
/*
678685
* Construct FROM clause
@@ -687,24 +694,24 @@ deparseSelectSql(StringInfo buf,
687694
* Emit a target list that retrieves the columns specified in attrs_used.
688695
* This is used for both SELECT and RETURNING targetlists.
689696
*
690-
* We list attributes in order of the foreign table's columns, but replace
691-
* any attributes that need not be fetched with NULL constants. (We can't
692-
* just omit such attributes, or we'll lose track of which columns are
693-
* which at runtime.) Note however that any dropped columns are ignored.
694-
* Also, if ctid needs to be retrieved, it's added at the end.
697+
* The tlist text is appended to buf, and we also create an integer List
698+
* of the columns being retrieved, which is returned to *retrieved_attrs.
695699
*/
696700
static void
697701
deparseTargetList(StringInfo buf,
698702
PlannerInfo *root,
699703
Index rtindex,
700704
Relation rel,
701-
Bitmapset *attrs_used)
705+
Bitmapset *attrs_used,
706+
List **retrieved_attrs)
702707
{
703708
TupleDesc tupdesc = RelationGetDescr(rel);
704709
bool have_wholerow;
705710
bool first;
706711
int i;
707712

713+
*retrieved_attrs = NIL;
714+
708715
/* If there's a whole-row reference, we'll need all the columns. */
709716
have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber,
710717
attrs_used);
@@ -718,16 +725,18 @@ deparseTargetList(StringInfo buf,
718725
if (attr->attisdropped)
719726
continue;
720727

721-
if (!first)
722-
appendStringInfoString(buf, ", ");
723-
first = false;
724-
725728
if (have_wholerow ||
726729
bms_is_member(i - FirstLowInvalidHeapAttributeNumber,
727730
attrs_used))
731+
{
732+
if (!first)
733+
appendStringInfoString(buf, ", ");
734+
first = false;
735+
728736
deparseColumnRef(buf, rtindex, i, root);
729-
else
730-
appendStringInfoString(buf, "NULL");
737+
738+
*retrieved_attrs = lappend_int(*retrieved_attrs, i);
739+
}
731740
}
732741

733742
/*
@@ -742,6 +751,9 @@ deparseTargetList(StringInfo buf,
742751
first = false;
743752

744753
appendStringInfoString(buf, "ctid");
754+
755+
*retrieved_attrs = lappend_int(*retrieved_attrs,
756+
SelfItemPointerAttributeNumber);
745757
}
746758

747759
/* Don't generate bad syntax if no undropped columns */
@@ -809,11 +821,16 @@ appendWhereClause(StringInfo buf,
809821

810822
/*
811823
* deparse remote INSERT statement
824+
*
825+
* The statement text is appended to buf, and we also create an integer List
826+
* of the columns being retrieved by RETURNING (if any), which is returned
827+
* to *retrieved_attrs.
812828
*/
813829
void
814830
deparseInsertSql(StringInfo buf, PlannerInfo *root,
815831
Index rtindex, Relation rel,
816-
List *targetAttrs, List *returningList)
832+
List *targetAttrs, List *returningList,
833+
List **retrieved_attrs)
817834
{
818835
AttrNumber pindex;
819836
bool first;
@@ -858,16 +875,24 @@ deparseInsertSql(StringInfo buf, PlannerInfo *root,
858875
appendStringInfoString(buf, " DEFAULT VALUES");
859876

860877
if (returningList)
861-
deparseReturningList(buf, root, rtindex, rel, returningList);
878+
deparseReturningList(buf, root, rtindex, rel, returningList,
879+
retrieved_attrs);
880+
else
881+
*retrieved_attrs = NIL;
862882
}
863883

864884
/*
865885
* deparse remote UPDATE statement
886+
*
887+
* The statement text is appended to buf, and we also create an integer List
888+
* of the columns being retrieved by RETURNING (if any), which is returned
889+
* to *retrieved_attrs.
866890
*/
867891
void
868892
deparseUpdateSql(StringInfo buf, PlannerInfo *root,
869893
Index rtindex, Relation rel,
870-
List *targetAttrs, List *returningList)
894+
List *targetAttrs, List *returningList,
895+
List **retrieved_attrs)
871896
{
872897
AttrNumber pindex;
873898
bool first;
@@ -894,23 +919,34 @@ deparseUpdateSql(StringInfo buf, PlannerInfo *root,
894919
appendStringInfoString(buf, " WHERE ctid = $1");
895920

896921
if (returningList)
897-
deparseReturningList(buf, root, rtindex, rel, returningList);
922+
deparseReturningList(buf, root, rtindex, rel, returningList,
923+
retrieved_attrs);
924+
else
925+
*retrieved_attrs = NIL;
898926
}
899927

900928
/*
901929
* deparse remote DELETE statement
930+
*
931+
* The statement text is appended to buf, and we also create an integer List
932+
* of the columns being retrieved by RETURNING (if any), which is returned
933+
* to *retrieved_attrs.
902934
*/
903935
void
904936
deparseDeleteSql(StringInfo buf, PlannerInfo *root,
905937
Index rtindex, Relation rel,
906-
List *returningList)
938+
List *returningList,
939+
List **retrieved_attrs)
907940
{
908941
appendStringInfoString(buf, "DELETE FROM ");
909942
deparseRelation(buf, rel);
910943
appendStringInfoString(buf, " WHERE ctid = $1");
911944

912945
if (returningList)
913-
deparseReturningList(buf, root, rtindex, rel, returningList);
946+
deparseReturningList(buf, root, rtindex, rel, returningList,
947+
retrieved_attrs);
948+
else
949+
*retrieved_attrs = NIL;
914950
}
915951

916952
/*
@@ -919,7 +955,8 @@ deparseDeleteSql(StringInfo buf, PlannerInfo *root,
919955
static void
920956
deparseReturningList(StringInfo buf, PlannerInfo *root,
921957
Index rtindex, Relation rel,
922-
List *returningList)
958+
List *returningList,
959+
List **retrieved_attrs)
923960
{
924961
Bitmapset *attrs_used;
925962

@@ -931,7 +968,8 @@ deparseReturningList(StringInfo buf, PlannerInfo *root,
931968
&attrs_used);
932969

933970
appendStringInfoString(buf, " RETURNING ");
934-
deparseTargetList(buf, root, rtindex, rel, attrs_used);
971+
deparseTargetList(buf, root, rtindex, rel, attrs_used,
972+
retrieved_attrs);
935973
}
936974

937975
/*
@@ -959,10 +997,11 @@ deparseAnalyzeSizeSql(StringInfo buf, Relation rel)
959997
/*
960998
* Construct SELECT statement to acquire sample rows of given relation.
961999
*
962-
* Note: command is appended to whatever might be in buf already.
1000+
* SELECT command is appended to buf, and list of columns retrieved
1001+
* is returned to *retrieved_attrs.
9631002
*/
9641003
void
965-
deparseAnalyzeSql(StringInfo buf, Relation rel)
1004+
deparseAnalyzeSql(StringInfo buf, Relation rel, List **retrieved_attrs)
9661005
{
9671006
Oid relid = RelationGetRelid(rel);
9681007
TupleDesc tupdesc = RelationGetDescr(rel);
@@ -972,6 +1011,8 @@ deparseAnalyzeSql(StringInfo buf, Relation rel)
9721011
ListCell *lc;
9731012
bool first = true;
9741013

1014+
*retrieved_attrs = NIL;
1015+
9751016
appendStringInfoString(buf, "SELECT ");
9761017
for (i = 0; i < tupdesc->natts; i++)
9771018
{
@@ -999,6 +1040,8 @@ deparseAnalyzeSql(StringInfo buf, Relation rel)
9991040
}
10001041

10011042
appendStringInfoString(buf, quote_identifier(colname));
1043+
1044+
*retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
10021045
}
10031046

10041047
/* Don't generate bad syntax for zero-column relation. */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy