@@ -301,11 +301,12 @@ dblink_open(PG_FUNCTION_ARGS)
301301 char * curname = NULL ;
302302 char * sql = NULL ;
303303 char * conname = NULL ;
304- StringInfo str = makeStringInfo () ;
304+ StringInfoData buf ;
305305 remoteConn * rconn = NULL ;
306306 bool fail = true; /* default to backward compatible behavior */
307307
308308 DBLINK_INIT ;
309+ initStringInfo (& buf );
309310
310311 if (PG_NARGS () == 2 )
311312 {
@@ -361,8 +362,8 @@ dblink_open(PG_FUNCTION_ARGS)
361362 if (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 );
366367 if (!res || PQresultStatus (res ) != PGRES_COMMAND_OK )
367368 {
368369 if (fail )
@@ -389,12 +390,13 @@ dblink_close(PG_FUNCTION_ARGS)
389390 PGresult * res = NULL ;
390391 char * curname = NULL ;
391392 char * conname = NULL ;
392- StringInfo str = makeStringInfo () ;
393+ StringInfoData buf ;
393394 char * msg ;
394395 remoteConn * rconn = NULL ;
395396 bool fail = true; /* default to backward compatible behavior */
396397
397398 DBLINK_INIT ;
399+ initStringInfo (& buf );
398400
399401 if (PG_NARGS () == 1 )
400402 {
@@ -432,10 +434,10 @@ dblink_close(PG_FUNCTION_ARGS)
432434 else
433435 conn = 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 );
439441 if (!res || PQresultStatus (res ) != PGRES_COMMAND_OK )
440442 {
441443 if (fail )
@@ -493,7 +495,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
493495 if (SRF_IS_FIRSTCALL ())
494496 {
495497 PGconn * conn = NULL ;
496- StringInfo str = makeStringInfo () ;
498+ StringInfoData buf ;
497499 char * curname = NULL ;
498500 int howmany = 0 ;
499501 bool fail = true; /* default to backward compatible */
@@ -542,6 +544,9 @@ dblink_fetch(PG_FUNCTION_ARGS)
542544 if (!conn )
543545 DBLINK_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 */
546551 funcctx = SRF_FIRSTCALL_INIT ();
547552
@@ -550,9 +555,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
550555 */
551556 oldcontext = 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 );
556559 if (!res ||
557560 (PQresultStatus (res ) != PGRES_COMMAND_OK &&
558561 PQresultStatus (res ) != PGRES_TUPLES_OK ))
@@ -1547,13 +1550,14 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
15471550 HeapTuple tuple ;
15481551 TupleDesc tupdesc ;
15491552 int natts ;
1550- StringInfo str = makeStringInfo ();
1551- char * sql ;
1553+ StringInfoData buf ;
15521554 char * val ;
15531555 int16 key ;
15541556 int i ;
15551557 bool needComma ;
15561558
1559+ initStringInfo (& buf );
1560+
15571561 /* get relation name including any needed schema prefix and quoting */
15581562 relname = 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 ),
15711575 errmsg ("source row not found" )));
15721576
1573- appendStringInfo (str , "INSERT INTO %s(" , relname );
1577+ appendStringInfo (& buf , "INSERT INTO %s(" , relname );
15741578
15751579 needComma = false;
15761580 for (i = 0 ; i < natts ; i ++ )
@@ -1579,14 +1583,14 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
15791583 continue ;
15801584
15811585 if (needComma )
1582- appendStringInfo (str , "," );
1586+ appendStringInfo (& buf , "," );
15831587
1584- appendStringInfo ( str , "%s" ,
1588+ appendStringInfoString ( & buf ,
15851589 quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
15861590 needComma = 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
15981602 continue ;
15991603
16001604 if (needComma )
1601- appendStringInfo (str , "," );
1605+ appendStringInfo (& buf , "," );
16021606
16031607 if (tgt_pkattvals != NULL )
16041608 key = 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
16131617 if (val != NULL )
16141618 {
1615- appendStringInfo ( str , "%s" , quote_literal_cstr (val ));
1619+ appendStringInfoString ( & buf , quote_literal_cstr (val ));
16161620 pfree (val );
16171621 }
16181622 else
1619- appendStringInfo (str , "NULL" );
1623+ appendStringInfo (& buf , "NULL" );
16201624 needComma = true;
16211625 }
1622- appendStringInfo (str , ")" );
1626+ appendStringInfo (& buf , ")" );
16231627
1624- sql = pstrdup (str -> data );
1625- pfree (str -> data );
1626- pfree (str );
16271628 relation_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
16361636 char * relname ;
16371637 TupleDesc tupdesc ;
16381638 int natts ;
1639- StringInfo str = makeStringInfo ();
1640- char * sql ;
1639+ StringInfoData buf ;
16411640 int i ;
16421641
1642+ initStringInfo (& buf );
1643+
16431644 /* get relation name including any needed schema prefix and quoting */
16441645 relname = generate_relation_name (relid );
16451646
@@ -1650,34 +1651,30 @@ get_sql_delete(Oid relid, int2vector *pkattnums, int16 pknumatts, char **tgt_pka
16501651 tupdesc = rel -> rd_att ;
16511652 natts = tupdesc -> natts ;
16521653
1653- appendStringInfo (str , "DELETE FROM %s WHERE " , relname );
1654+ appendStringInfo (& buf , "DELETE FROM %s WHERE " , relname );
16541655 for (i = 0 ; i < pknumatts ; i ++ )
16551656 {
16561657 int16 pkattnum = pkattnums -> values [i ];
16571658
16581659 if (i > 0 )
1659- appendStringInfo (str , " AND " );
1660+ appendStringInfo (& buf , " AND " );
16601661
1661- appendStringInfo ( str , "%s" ,
1662+ appendStringInfoString ( & buf ,
16621663 quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
16631664
16641665 if (tgt_pkattvals == NULL )
16651666 /* internal error */
16661667 elog (ERROR , "target key array must not be NULL" );
16671668
16681669 if (tgt_pkattvals [i ] != NULL )
1669- appendStringInfo (str , " = %s" ,
1670+ appendStringInfo (& buf , " = %s" ,
16701671 quote_literal_cstr (tgt_pkattvals [i ]));
16711672 else
1672- appendStringInfo (str , " IS NULL" );
1673+ appendStringInfo (& buf , " IS NULL" );
16731674 }
16741675
1675- sql = pstrdup (str -> data );
1676- pfree (str -> data );
1677- pfree (str );
16781676 relation_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
16881685 HeapTuple tuple ;
16891686 TupleDesc tupdesc ;
16901687 int natts ;
1691- StringInfo str = makeStringInfo ();
1692- char * sql ;
1688+ StringInfoData buf ;
16931689 char * val ;
16941690 int16 key ;
16951691 int i ;
16961692 bool needComma ;
16971693
1694+ initStringInfo (& buf );
1695+
16981696 /* get relation name including any needed schema prefix and quoting */
16991697 relname = 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 ),
17121710 errmsg ("source row not found" )));
17131711
1714- appendStringInfo (str , "UPDATE %s SET " , relname );
1712+ appendStringInfo (& buf , "UPDATE %s SET " , relname );
17151713
17161714 needComma = false;
17171715 for (i = 0 ; i < natts ; i ++ )
@@ -1720,9 +1718,9 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17201718 continue ;
17211719
17221720 if (needComma )
1723- appendStringInfo (str , ", " );
1721+ appendStringInfo (& buf , ", " );
17241722
1725- appendStringInfo (str , "%s = " ,
1723+ appendStringInfo (& buf , "%s = " ,
17261724 quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
17271725
17281726 if (tgt_pkattvals != NULL )
@@ -1737,24 +1735,24 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17371735
17381736 if (val != NULL )
17391737 {
1740- appendStringInfo ( str , "%s" , quote_literal_cstr (val ));
1738+ appendStringInfoString ( & buf , quote_literal_cstr (val ));
17411739 pfree (val );
17421740 }
17431741 else
1744- appendStringInfo ( str , "NULL" );
1742+ appendStringInfoString ( & buf , "NULL" );
17451743 needComma = true;
17461744 }
17471745
1748- appendStringInfo (str , " WHERE " );
1746+ appendStringInfo (& buf , " WHERE " );
17491747
17501748 for (i = 0 ; i < pknumatts ; i ++ )
17511749 {
17521750 int16 pkattnum = pkattnums -> values [i ];
17531751
17541752 if (i > 0 )
1755- appendStringInfo (str , " AND " );
1753+ appendStringInfo (& buf , " AND " );
17561754
1757- appendStringInfo (str , "%s" ,
1755+ appendStringInfo (& buf , "%s" ,
17581756 quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
17591757
17601758 if (tgt_pkattvals != NULL )
@@ -1764,19 +1762,15 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
17641762
17651763 if (val != NULL )
17661764 {
1767- appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1765+ appendStringInfo (& buf , " = %s" , quote_literal_cstr (val ));
17681766 pfree (val );
17691767 }
17701768 else
1771- appendStringInfo (str , " IS NULL" );
1769+ appendStringInfo (& buf , " IS NULL" );
17721770 }
17731771
1774- sql = pstrdup (str -> data );
1775- pfree (str -> data );
1776- pfree (str );
17771772 relation_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 **
18361830 Relation rel ;
18371831 char * relname ;
18381832 TupleDesc tupdesc ;
1839- StringInfo str = makeStringInfo ();
1840- char * sql = NULL ;
1833+ StringInfoData buf ;
18411834 int ret ;
18421835 HeapTuple tuple ;
18431836 int i ;
18441837
1838+ initStringInfo (& buf );
1839+
18451840 /* get relation name including any needed schema prefix and quoting */
18461841 relname = 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
18681863 for (i = 0 ; i < pknumatts ; i ++ )
18691864 {
18701865 int16 pkattnum = pkattnums -> values [i ];
18711866
18721867 if (i > 0 )
1873- appendStringInfo (str , " AND " );
1868+ appendStringInfo (& buf , " AND " );
18741869
1875- appendStringInfo ( str , "%s" ,
1870+ appendStringInfoString ( & buf ,
18761871 quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
18771872
18781873 if (src_pkattvals [i ] != NULL )
1879- appendStringInfo (str , " = %s" ,
1874+ appendStringInfo (& buf , " = %s" ,
18801875 quote_literal_cstr (src_pkattvals [i ]));
18811876 else
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
0 commit comments