@@ -829,16 +829,18 @@ dblink_replace_text(PG_FUNCTION_ARGS)
829829left_text = DatumGetTextP (DirectFunctionCall3 (text_substr ,PointerGetDatum (buf_text ),1 ,DatumGetInt32 (DirectFunctionCall2 (textpos ,PointerGetDatum (buf_text ),PointerGetDatum (from_sub_text )))- 1 ));
830830right_text = DatumGetTextP (DirectFunctionCall3 (text_substr ,PointerGetDatum (buf_text ),DatumGetInt32 (DirectFunctionCall2 (textpos ,PointerGetDatum (buf_text ),PointerGetDatum (from_sub_text )))+ from_sub_text_len ,-1 ));
831831
832- appendStringInfo (str ,DatumGetCString (DirectFunctionCall1 (textout ,PointerGetDatum (left_text ))));
833- appendStringInfo (str ,to_sub_str );
832+ appendStringInfo (str ,"%s" ,
833+ DatumGetCString (DirectFunctionCall1 (textout ,PointerGetDatum (left_text ))));
834+ appendStringInfo (str ,"%s" ,to_sub_str );
834835
835836pfree (buf_text );
836837pfree (left_text );
837838buf_text = right_text ;
838839curr_posn = DatumGetInt32 (DirectFunctionCall2 (textpos ,PointerGetDatum (buf_text ),PointerGetDatum (from_sub_text )));
839840}
840841
841- appendStringInfo (str ,DatumGetCString (DirectFunctionCall1 (textout ,PointerGetDatum (buf_text ))));
842+ appendStringInfo (str ,"%s" ,
843+ DatumGetCString (DirectFunctionCall1 (textout ,PointerGetDatum (buf_text ))));
842844pfree (buf_text );
843845
844846ret_str = pstrdup (str -> data );
@@ -1013,10 +1015,11 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
10131015TupleDesc tupdesc ;
10141016int natts ;
10151017StringInfo str = makeStringInfo ();
1016- char * sql = NULL ;
1017- char * val = NULL ;
1018+ char * sql ;
1019+ char * val ;
10181020int16 key ;
1019- unsignedint i ;
1021+ int i ;
1022+ bool needComma ;
10201023
10211024/*
10221025 * Open relation using relid
@@ -1029,22 +1032,33 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
10291032tuple = get_tuple_of_interest (relid ,pkattnums ,pknumatts ,src_pkattvals );
10301033
10311034appendStringInfo (str ,"INSERT INTO %s(" ,quote_ident_cstr (relname ));
1035+
1036+ needComma = false;
10321037for (i = 0 ;i < natts ;i ++ )
10331038{
1034- if (i > 0 )
1039+ if (tupdesc -> attrs [i ]-> attisdropped )
1040+ continue ;
1041+
1042+ if (needComma )
10351043appendStringInfo (str ,"," );
10361044
1037- appendStringInfo (str ,NameStr (tupdesc -> attrs [i ]-> attname ));
1045+ appendStringInfo (str ,"%s" ,
1046+ quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1047+ needComma = true;
10381048}
10391049
10401050appendStringInfo (str ,") VALUES(" );
10411051
10421052/*
10431053 * remember attvals are 1 based
10441054 */
1055+ needComma = false;
10451056for (i = 0 ;i < natts ;i ++ )
10461057{
1047- if (i > 0 )
1058+ if (tupdesc -> attrs [i ]-> attisdropped )
1059+ continue ;
1060+
1061+ if (needComma )
10481062appendStringInfo (str ,"," );
10491063
10501064if (tgt_pkattvals != NULL )
@@ -1059,11 +1073,12 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
10591073
10601074if (val != NULL )
10611075{
1062- appendStringInfo (str ,quote_literal_cstr (val ));
1076+ appendStringInfo (str ,"%s" , quote_literal_cstr (val ));
10631077pfree (val );
10641078}
10651079else
10661080appendStringInfo (str ,"NULL" );
1081+ needComma = true;
10671082}
10681083appendStringInfo (str ,")" );
10691084
@@ -1083,9 +1098,9 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
10831098TupleDesc tupdesc ;
10841099int natts ;
10851100StringInfo str = makeStringInfo ();
1086- char * sql = NULL ;
1087- char * val = NULL ;
1088- unsigned int i ;
1101+ char * sql ;
1102+ char * val ;
1103+ int i ;
10891104
10901105/*
10911106 * Open relation using relid
@@ -1103,21 +1118,24 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
11031118if (i > 0 )
11041119appendStringInfo (str ," AND " );
11051120
1106- appendStringInfo (str ,NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1121+ appendStringInfo (str ,"%s" ,
1122+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
11071123
11081124if (tgt_pkattvals != NULL )
11091125val = pstrdup (tgt_pkattvals [i ]);
11101126else
1127+ {
11111128elog (ERROR ,"Target key array must not be NULL" );
1129+ val = NULL ;/* keep compiler quiet */
1130+ }
11121131
11131132if (val != NULL )
11141133{
1115- appendStringInfo (str ,"=" );
1116- appendStringInfo (str ,quote_literal_cstr (val ));
1134+ appendStringInfo (str ," = %s" ,quote_literal_cstr (val ));
11171135pfree (val );
11181136}
11191137else
1120- appendStringInfo (str ,"IS NULL" );
1138+ appendStringInfo (str ," IS NULL" );
11211139}
11221140
11231141sql = pstrdup (str -> data );
@@ -1137,10 +1155,11 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11371155TupleDesc tupdesc ;
11381156int natts ;
11391157StringInfo str = makeStringInfo ();
1140- char * sql = NULL ;
1141- char * val = NULL ;
1158+ char * sql ;
1159+ char * val ;
11421160int16 key ;
11431161int i ;
1162+ bool needComma ;
11441163
11451164/*
11461165 * Open relation using relid
@@ -1154,13 +1173,17 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11541173
11551174appendStringInfo (str ,"UPDATE %s SET " ,quote_ident_cstr (relname ));
11561175
1176+ needComma = false;
11571177for (i = 0 ;i < natts ;i ++ )
11581178{
1159- if (i > 0 )
1160- appendStringInfo (str ,"," );
1179+ if (tupdesc -> attrs [i ]-> attisdropped )
1180+ continue ;
1181+
1182+ if (needComma )
1183+ appendStringInfo (str ,", " );
11611184
1162- appendStringInfo (str ,NameStr ( tupdesc -> attrs [ i ] -> attname ));
1163- appendStringInfo ( str , "=" );
1185+ appendStringInfo (str ,"%s = " ,
1186+ quote_ident_cstr ( NameStr ( tupdesc -> attrs [ i ] -> attname )) );
11641187
11651188if (tgt_pkattvals != NULL )
11661189key = get_attnum_pk_pos (pkattnums ,pknumatts ,i + 1 );
@@ -1174,11 +1197,12 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11741197
11751198if (val != NULL )
11761199{
1177- appendStringInfo (str ,quote_literal_cstr (val ));
1200+ appendStringInfo (str ,"%s" , quote_literal_cstr (val ));
11781201pfree (val );
11791202}
11801203else
11811204appendStringInfo (str ,"NULL" );
1205+ needComma = true;
11821206}
11831207
11841208appendStringInfo (str ," WHERE " );
@@ -1190,7 +1214,8 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11901214if (i > 0 )
11911215appendStringInfo (str ," AND " );
11921216
1193- appendStringInfo (str ,NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1217+ appendStringInfo (str ,"%s" ,
1218+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
11941219
11951220if (tgt_pkattvals != NULL )
11961221val = pstrdup (tgt_pkattvals [i ]);
@@ -1199,12 +1224,11 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11991224
12001225if (val != NULL )
12011226{
1202- appendStringInfo (str ,"=" );
1203- appendStringInfo (str ,quote_literal_cstr (val ));
1227+ appendStringInfo (str ," = %s" ,quote_literal_cstr (val ));
12041228pfree (val );
12051229}
12061230else
1207- appendStringInfo (str ,"IS NULL" );
1231+ appendStringInfo (str ," IS NULL" );
12081232}
12091233
12101234sql = pstrdup (str -> data );
@@ -1297,7 +1321,7 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
12971321 * Build sql statement to look up tuple of interest
12981322 * Use src_pkattvals as the criteria.
12991323 */
1300- appendStringInfo (str ,"SELECT *from %s WHERE " ,relname );
1324+ appendStringInfo (str ,"SELECT *FROM %s WHERE " ,quote_ident_cstr ( relname ) );
13011325
13021326for (i = 0 ;i < pknumatts ;i ++ )
13031327{
@@ -1306,17 +1330,17 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
13061330if (i > 0 )
13071331appendStringInfo (str ," AND " );
13081332
1309- appendStringInfo (str ,NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1333+ appendStringInfo (str ,"%s" ,
1334+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
13101335
13111336val = pstrdup (src_pkattvals [i ]);
13121337if (val != NULL )
13131338{
1314- appendStringInfo (str ,"=" );
1315- appendStringInfo (str ,quote_literal_cstr (val ));
1339+ appendStringInfo (str ," = %s" ,quote_literal_cstr (val ));
13161340pfree (val );
13171341}
13181342else
1319- appendStringInfo (str ,"IS NULL" );
1343+ appendStringInfo (str ," IS NULL" );
13201344}
13211345
13221346sql = pstrdup (str -> data );