Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite690b95

Browse files
committed
Avoid retrieving dummy NULL columns in postgres_fdw.
This should provide some marginal overall savings, since it surely takesmany more cycles for the remote server to deal with the NULL columns thanit takes for postgres_fdw not to emit them. But really the reason is tokeep the emitted queries from looking quite so silly ...
1 parent9cbc4b8 commite690b95

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
Indexrtindex,
108108
Relationrel,
109-
Bitmapset*attrs_used);
109+
Bitmapset*attrs_used,
110+
List**retrieved_attrs);
110111
staticvoiddeparseReturningList(StringInfobuf,PlannerInfo*root,
111112
Indexrtindex,Relationrel,
112-
List*returningList);
113+
List*returningList,
114+
List**retrieved_attrs);
113115
staticvoiddeparseColumnRef(StringInfobuf,intvarno,intvarattno,
114116
PlannerInfo*root);
115117
staticvoiddeparseRelation(StringInfobuf,Relationrel);
@@ -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(StringInfobuf,
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
Relationrel;
@@ -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
staticvoid
697701
deparseTargetList(StringInfobuf,
698702
PlannerInfo*root,
699703
Indexrtindex,
700704
Relationrel,
701-
Bitmapset*attrs_used)
705+
Bitmapset*attrs_used,
706+
List**retrieved_attrs)
702707
{
703708
TupleDesctupdesc=RelationGetDescr(rel);
704709
boolhave_wholerow;
705710
boolfirst;
706711
inti;
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(StringInfobuf,PlannerInfo*root,
815831
Indexrtindex,Relationrel,
816-
List*targetAttrs,List*returningList)
832+
List*targetAttrs,List*returningList,
833+
List**retrieved_attrs)
817834
{
818835
AttrNumberpindex;
819836
boolfirst;
@@ -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(StringInfobuf,PlannerInfo*root,
869893
Indexrtindex,Relationrel,
870-
List*targetAttrs,List*returningList)
894+
List*targetAttrs,List*returningList,
895+
List**retrieved_attrs)
871896
{
872897
AttrNumberpindex;
873898
boolfirst;
@@ -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(StringInfobuf,PlannerInfo*root,
905937
Indexrtindex,Relationrel,
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
staticvoid
920956
deparseReturningList(StringInfobuf,PlannerInfo*root,
921957
Indexrtindex,Relationrel,
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(StringInfobuf,Relationrel)
1004+
deparseAnalyzeSql(StringInfobuf,Relationrel,List**retrieved_attrs)
9661005
{
9671006
Oidrelid=RelationGetRelid(rel);
9681007
TupleDesctupdesc=RelationGetDescr(rel);
@@ -972,6 +1011,8 @@ deparseAnalyzeSql(StringInfo buf, Relation rel)
9721011
ListCell*lc;
9731012
boolfirst= 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)

[8]ページ先頭

©2009-2025 Movatter.jp