@@ -301,11 +301,12 @@ dblink_open(PG_FUNCTION_ARGS)
301301char * curname = NULL ;
302302char * sql = NULL ;
303303char * conname = NULL ;
304- StringInfo str = makeStringInfo () ;
304+ StringInfoData buf ;
305305remoteConn * rconn = NULL ;
306306bool fail = true;/* default to backward compatible behavior */
307307
308308DBLINK_INIT ;
309+ initStringInfo (& buf );
309310
310311if (PG_NARGS ()== 2 )
311312{
@@ -361,8 +362,8 @@ dblink_open(PG_FUNCTION_ARGS)
361362if (rconn -> newXactForCursor )
362363(rconn -> openCursorCount )++ ;
363364
364- appendStringInfo (str ,"DECLARE %s CURSOR FOR %s" ,curname ,sql );
365- res = PQexec (conn ,str -> data );
365+ appendStringInfo (& buf ,"DECLARE %s CURSOR FOR %s" ,curname ,sql );
366+ res = PQexec (conn ,buf . data );
366367if (!res || PQresultStatus (res )!= PGRES_COMMAND_OK )
367368{
368369if (fail )
@@ -389,12 +390,13 @@ dblink_close(PG_FUNCTION_ARGS)
389390PGresult * res = NULL ;
390391char * curname = NULL ;
391392char * conname = NULL ;
392- StringInfo str = makeStringInfo () ;
393+ StringInfoData buf ;
393394char * msg ;
394395remoteConn * rconn = NULL ;
395396bool fail = true;/* default to backward compatible behavior */
396397
397398DBLINK_INIT ;
399+ initStringInfo (& buf );
398400
399401if (PG_NARGS ()== 1 )
400402{
@@ -432,10 +434,10 @@ dblink_close(PG_FUNCTION_ARGS)
432434else
433435conn = rconn -> conn ;
434436
435- appendStringInfo (str ,"CLOSE %s" ,curname );
437+ appendStringInfo (& buf ,"CLOSE %s" ,curname );
436438
437439/* close the cursor */
438- res = PQexec (conn ,str -> data );
440+ res = PQexec (conn ,buf . data );
439441if (!res || PQresultStatus (res )!= PGRES_COMMAND_OK )
440442{
441443if (fail )
@@ -493,7 +495,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
493495if (SRF_IS_FIRSTCALL ())
494496{
495497PGconn * conn = NULL ;
496- StringInfo str = makeStringInfo () ;
498+ StringInfoData buf ;
497499char * curname = NULL ;
498500int howmany = 0 ;
499501bool fail = true;/* default to backward compatible */
@@ -542,6 +544,9 @@ dblink_fetch(PG_FUNCTION_ARGS)
542544if (!conn )
543545DBLINK_CONN_NOT_AVAIL ;
544546
547+ initStringInfo (& buf );
548+ appendStringInfo (& buf ,"FETCH %d FROM %s" ,howmany ,curname );
549+
545550/* create a function context for cross-call persistence */
546551funcctx = SRF_FIRSTCALL_INIT ();
547552
@@ -550,9 +555,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
550555 */
551556oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
552557
553- appendStringInfo (str ,"FETCH %d FROM %s" ,howmany ,curname );
554-
555- res = PQexec (conn ,str -> data );
558+ res = PQexec (conn ,buf .data );
556559if (!res ||
557560(PQresultStatus (res )!= PGRES_COMMAND_OK &&
558561PQresultStatus (res )!= PGRES_TUPLES_OK ))
@@ -1547,13 +1550,14 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
15471550HeapTuple tuple ;
15481551TupleDesc tupdesc ;
15491552int natts ;
1550- StringInfo str = makeStringInfo ();
1551- char * sql ;
1553+ StringInfoData buf ;
15521554char * val ;
15531555int16 key ;
15541556int i ;
15551557bool needComma ;
15561558
1559+ initStringInfo (& buf );
1560+
15571561/* get relation name including any needed schema prefix and quoting */
15581562relname = generate_relation_name (relid );
15591563
@@ -1570,7 +1574,7 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
15701574(errcode (ERRCODE_CARDINALITY_VIOLATION ),
15711575errmsg ("source row not found" )));
15721576
1573- appendStringInfo (str ,"INSERT INTO %s(" ,relname );
1577+ appendStringInfo (& buf ,"INSERT INTO %s(" ,relname );
15741578
15751579needComma = false;
15761580for (i = 0 ;i < natts ;i ++ )
@@ -1579,14 +1583,14 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
15791583continue ;
15801584
15811585if (needComma )
1582- appendStringInfo (str ,"," );
1586+ appendStringInfo (& buf ,"," );
15831587
1584- appendStringInfo ( str , "%s" ,
1588+ appendStringInfoString ( & buf ,
15851589quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
15861590needComma = true;
15871591}
15881592
1589- appendStringInfo (str ,") VALUES(" );
1593+ appendStringInfo (& buf ,") VALUES(" );
15901594
15911595/*
15921596 * remember attvals are 1 based
@@ -1598,7 +1602,7 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
15981602continue ;
15991603
16001604if (needComma )
1601- appendStringInfo (str ,"," );
1605+ appendStringInfo (& buf ,"," );
16021606
16031607if (tgt_pkattvals != NULL )
16041608key = get_attnum_pk_pos (pkattnums ,pknumatts ,i + 1 );
@@ -1612,21 +1616,17 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
16121616
16131617if (val != NULL )
16141618{
1615- appendStringInfo ( str , "%s" ,quote_literal_cstr (val ));
1619+ appendStringInfoString ( & buf ,quote_literal_cstr (val ));
16161620pfree (val );
16171621}
16181622else
1619- appendStringInfo (str ,"NULL" );
1623+ appendStringInfo (& buf ,"NULL" );
16201624needComma = true;
16211625}
1622- appendStringInfo (str ,")" );
1626+ appendStringInfo (& buf ,")" );
16231627
1624- sql = pstrdup (str -> data );
1625- pfree (str -> data );
1626- pfree (str );
16271628relation_close (rel ,AccessShareLock );
1628-
1629- return (sql );
1629+ return (buf .data );
16301630}
16311631
16321632static char *
@@ -1636,10 +1636,11 @@ get_sql_delete(Oid relid, int2vector *pkattnums, int16 pknumatts, char **tgt_pka
16361636char * relname ;
16371637TupleDesc tupdesc ;
16381638int natts ;
1639- StringInfo str = makeStringInfo ();
1640- char * sql ;
1639+ StringInfoData buf ;
16411640int i ;
16421641
1642+ initStringInfo (& buf );
1643+
16431644/* get relation name including any needed schema prefix and quoting */
16441645relname = generate_relation_name (relid );
16451646
@@ -1650,34 +1651,30 @@ get_sql_delete(Oid relid, int2vector *pkattnums, int16 pknumatts, char **tgt_pka
16501651tupdesc = rel -> rd_att ;
16511652natts = tupdesc -> natts ;
16521653
1653- appendStringInfo (str ,"DELETE FROM %s WHERE " ,relname );
1654+ appendStringInfo (& buf ,"DELETE FROM %s WHERE " ,relname );
16541655for (i = 0 ;i < pknumatts ;i ++ )
16551656{
16561657int16 pkattnum = pkattnums -> values [i ];
16571658
16581659if (i > 0 )
1659- appendStringInfo (str ," AND " );
1660+ appendStringInfo (& buf ," AND " );
16601661
1661- appendStringInfo ( str , "%s" ,
1662+ appendStringInfoString ( & buf ,
16621663quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
16631664
16641665if (tgt_pkattvals == NULL )
16651666/* internal error */
16661667elog (ERROR ,"target key array must not be NULL" );
16671668
16681669if (tgt_pkattvals [i ]!= NULL )
1669- appendStringInfo (str ," = %s" ,
1670+ appendStringInfo (& buf ," = %s" ,
16701671quote_literal_cstr (tgt_pkattvals [i ]));
16711672else
1672- appendStringInfo (str ," IS NULL" );
1673+ appendStringInfo (& buf ," IS NULL" );
16731674}
16741675
1675- sql = pstrdup (str -> data );
1676- pfree (str -> data );
1677- pfree (str );
16781676relation_close (rel ,AccessShareLock );
1679-
1680- return (sql );
1677+ return (buf .data );
16811678}
16821679
16831680static char *
@@ -1688,13 +1685,14 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
16881685HeapTuple tuple ;
16891686TupleDesc tupdesc ;
16901687int natts ;
1691- StringInfo str = makeStringInfo ();
1692- char * sql ;
1688+ StringInfoData buf ;
16931689char * val ;
16941690int16 key ;
16951691int i ;
16961692bool needComma ;
16971693
1694+ initStringInfo (& buf );
1695+
16981696/* get relation name including any needed schema prefix and quoting */
16991697relname = generate_relation_name (relid );
17001698
@@ -1711,7 +1709,7 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17111709(errcode (ERRCODE_CARDINALITY_VIOLATION ),
17121710errmsg ("source row not found" )));
17131711
1714- appendStringInfo (str ,"UPDATE %s SET " ,relname );
1712+ appendStringInfo (& buf ,"UPDATE %s SET " ,relname );
17151713
17161714needComma = false;
17171715for (i = 0 ;i < natts ;i ++ )
@@ -1720,9 +1718,9 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17201718continue ;
17211719
17221720if (needComma )
1723- appendStringInfo (str ,", " );
1721+ appendStringInfo (& buf ,", " );
17241722
1725- appendStringInfo (str ,"%s = " ,
1723+ appendStringInfo (& buf ,"%s = " ,
17261724quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
17271725
17281726if (tgt_pkattvals != NULL )
@@ -1737,24 +1735,24 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17371735
17381736if (val != NULL )
17391737{
1740- appendStringInfo ( str , "%s" ,quote_literal_cstr (val ));
1738+ appendStringInfoString ( & buf ,quote_literal_cstr (val ));
17411739pfree (val );
17421740}
17431741else
1744- appendStringInfo ( str ,"NULL" );
1742+ appendStringInfoString ( & buf ,"NULL" );
17451743needComma = true;
17461744}
17471745
1748- appendStringInfo (str ," WHERE " );
1746+ appendStringInfo (& buf ," WHERE " );
17491747
17501748for (i = 0 ;i < pknumatts ;i ++ )
17511749{
17521750int16 pkattnum = pkattnums -> values [i ];
17531751
17541752if (i > 0 )
1755- appendStringInfo (str ," AND " );
1753+ appendStringInfo (& buf ," AND " );
17561754
1757- appendStringInfo (str ,"%s" ,
1755+ appendStringInfo (& buf ,"%s" ,
17581756quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
17591757
17601758if (tgt_pkattvals != NULL )
@@ -1764,19 +1762,15 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17641762
17651763if (val != NULL )
17661764{
1767- appendStringInfo (str ," = %s" ,quote_literal_cstr (val ));
1765+ appendStringInfo (& buf ," = %s" ,quote_literal_cstr (val ));
17681766pfree (val );
17691767}
17701768else
1771- appendStringInfo (str ," IS NULL" );
1769+ appendStringInfo (& buf ," IS NULL" );
17721770}
17731771
1774- sql = pstrdup (str -> data );
1775- pfree (str -> data );
1776- pfree (str );
17771772relation_close (rel ,AccessShareLock );
1778-
1779- return (sql );
1773+ return (buf .data );
17801774}
17811775
17821776/*
@@ -1836,12 +1830,13 @@ get_tuple_of_interest(Oid relid, int2vector *pkattnums, int16 pknumatts, char **
18361830Relation rel ;
18371831char * relname ;
18381832TupleDesc tupdesc ;
1839- StringInfo str = makeStringInfo ();
1840- char * sql = NULL ;
1833+ StringInfoData buf ;
18411834int ret ;
18421835HeapTuple tuple ;
18431836int i ;
18441837
1838+ initStringInfo (& buf );
1839+
18451840/* get relation name including any needed schema prefix and quoting */
18461841relname = generate_relation_name (relid );
18471842
@@ -1863,34 +1858,30 @@ get_tuple_of_interest(Oid relid, int2vector *pkattnums, int16 pknumatts, char **
18631858 * Build sql statement to look up tuple of interest Use src_pkattvals as
18641859 * the criteria.
18651860 */
1866- appendStringInfo (str ,"SELECT * FROM %s WHERE " ,relname );
1861+ appendStringInfo (& buf ,"SELECT * FROM %s WHERE " ,relname );
18671862
18681863for (i = 0 ;i < pknumatts ;i ++ )
18691864{
18701865int16 pkattnum = pkattnums -> values [i ];
18711866
18721867if (i > 0 )
1873- appendStringInfo (str ," AND " );
1868+ appendStringInfo (& buf ," AND " );
18741869
1875- appendStringInfo ( str , "%s" ,
1870+ appendStringInfoString ( & buf ,
18761871quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
18771872
18781873if (src_pkattvals [i ]!= NULL )
1879- appendStringInfo (str ," = %s" ,
1874+ appendStringInfo (& buf ," = %s" ,
18801875quote_literal_cstr (src_pkattvals [i ]));
18811876else
1882- appendStringInfo (str ," IS NULL" );
1877+ appendStringInfo (& buf ," IS NULL" );
18831878}
18841879
1885- sql = pstrdup (str -> data );
1886- pfree (str -> data );
1887- pfree (str );
1888-
18891880/*
18901881 * Retrieve the desired tuple
18911882 */
1892- ret = SPI_exec (sql ,0 );
1893- pfree (sql );
1883+ ret = SPI_exec (buf . data ,0 );
1884+ pfree (buf . data );
18941885
18951886/*
18961887 * Only allow one qualifying tuple