8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.497 2006/08/10 00:44:01 momjian Exp $
11
+ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.498 2006/08/13 22:18:08 tgl Exp $
12
12
*
13
13
* NOTES
14
14
* this is the "main" module of the postgres backend and
@@ -1368,7 +1368,7 @@ exec_bind_message(StringInfo input_message)
1368
1368
PreparedStatement * pstmt ;
1369
1369
Portal portal ;
1370
1370
ParamListInfo params ;
1371
- StringInfoData str ;
1371
+ StringInfoData bind_values_str ;
1372
1372
1373
1373
pgstat_report_activity ("<BIND>" );
1374
1374
@@ -1385,7 +1385,7 @@ exec_bind_message(StringInfo input_message)
1385
1385
MemoryContextSwitchTo (MessageContext );
1386
1386
1387
1387
if (log_statement == LOGSTMT_ALL )
1388
- initStringInfo (& str );
1388
+ initStringInfo (& bind_values_str );
1389
1389
1390
1390
/* Get the fixed part of the message */
1391
1391
portal_name = pq_getmsgstring (input_message );
@@ -1534,14 +1534,18 @@ exec_bind_message(StringInfo input_message)
1534
1534
else
1535
1535
pstring = pg_client_to_server (pbuf .data , plength );
1536
1536
1537
- params -> params [paramno ].value = OidInputFunctionCall (typinput ,
1538
- pstring ,
1539
- typioparam ,
1540
- -1 );
1537
+ params -> params [paramno ].value =
1538
+ OidInputFunctionCall (typinput ,
1539
+ pstring ,
1540
+ typioparam ,
1541
+ -1 );
1541
1542
1543
+ /* Log the parameter value if needed */
1542
1544
if (log_statement == LOGSTMT_ALL )
1543
- appendStringInfo (& str , "%s$%d = \"%s\"" ,
1544
- * str .data ? ", " : "" , paramno + 1 , pstring );
1545
+ appendStringInfo (& bind_values_str , "%s$%d = \"%s\"" ,
1546
+ bind_values_str .len ? ", " : "" ,
1547
+ paramno + 1 ,
1548
+ pstring );
1545
1549
1546
1550
/* Free result of encoding conversion, if any */
1547
1551
if (pstring && pstring != pbuf .data )
@@ -1574,6 +1578,8 @@ exec_bind_message(StringInfo input_message)
1574
1578
(errcode (ERRCODE_INVALID_BINARY_REPRESENTATION ),
1575
1579
errmsg ("incorrect binary data format in bind parameter %d" ,
1576
1580
paramno + 1 )));
1581
+
1582
+ /* XXX TODO: convert value to text and log it */
1577
1583
}
1578
1584
else
1579
1585
{
@@ -1600,22 +1606,14 @@ exec_bind_message(StringInfo input_message)
1600
1606
1601
1607
if (log_statement == LOGSTMT_ALL )
1602
1608
{
1603
- if (* str .data )
1604
- ereport (LOG ,
1605
- (errmsg ("bind %s%s%s: %s" ,
1606
- * stmt_name ? stmt_name : "<unnamed>" ,
1607
- * portal -> name ? "/" : "" ,
1608
- * portal -> name ? portal -> name : "" ,
1609
- pstmt -> query_string ? pstmt -> query_string : "" ),
1610
- errdetail (str .data )));
1611
- else
1612
- ereport (LOG ,
1613
- (errmsg ("bind %s%s%s: %s" ,
1614
- * stmt_name ? stmt_name : "<unnamed>" ,
1615
- * portal -> name ? "/" : "" ,
1616
- * portal -> name ? portal -> name : "" ,
1617
- pstmt -> query_string ? pstmt -> query_string : "" )));
1618
- pfree (str .data );
1609
+ ereport (LOG ,
1610
+ (errmsg ("bind %s%s%s: %s" ,
1611
+ * stmt_name ? stmt_name : "<unnamed>" ,
1612
+ * portal -> name ? "/" : "" ,
1613
+ * portal -> name ? portal -> name : "" ,
1614
+ pstmt -> query_string ? pstmt -> query_string : "" ),
1615
+ bind_values_str .len ? errdetail (bind_values_str .data ) : 0 ));
1616
+ pfree (bind_values_str .data );
1619
1617
}
1620
1618
1621
1619
/* Get the result format codes */
@@ -1685,8 +1683,11 @@ exec_execute_message(const char *portal_name, long max_rows)
1685
1683
Portal portal ;
1686
1684
bool completed ;
1687
1685
char completionTag [COMPLETION_TAG_BUFSIZE ];
1686
+ const char * sourceText = NULL ;
1687
+ const char * prepStmtName ;
1688
1688
bool save_log_statement_stats = log_statement_stats ;
1689
- bool execute_is_fetch = false;
1689
+ bool is_xact_command ;
1690
+ bool execute_is_fetch ;
1690
1691
1691
1692
/* Adjust destination to tell printtup.c what to do */
1692
1693
dest = whereToSendOutput ;
@@ -1699,15 +1700,6 @@ exec_execute_message(const char *portal_name, long max_rows)
1699
1700
(errcode (ERRCODE_UNDEFINED_CURSOR ),
1700
1701
errmsg ("portal \"%s\" does not exist" , portal_name )));
1701
1702
1702
- /*
1703
- * If we re-issue an Execute protocol request against an existing portal,
1704
- * then we are only fetching more rows rather than completely re-executing
1705
- * the query from the start. atStart is never reset for a v3 portal, so we
1706
- * are safe to use this check.
1707
- */
1708
- if (!portal -> atStart )
1709
- execute_is_fetch = true;
1710
-
1711
1703
/*
1712
1704
* If the original query was a null string, just return
1713
1705
* EmptyQueryResponse.
@@ -1719,6 +1711,17 @@ exec_execute_message(const char *portal_name, long max_rows)
1719
1711
return ;
1720
1712
}
1721
1713
1714
+ /* Does the portal contain a transaction command? */
1715
+ is_xact_command = IsTransactionStmtList (portal -> parseTrees );
1716
+
1717
+ /*
1718
+ * If we re-issue an Execute protocol request against an existing portal,
1719
+ * then we are only fetching more rows rather than completely re-executing
1720
+ * the query from the start. atStart is never reset for a v3 portal, so we
1721
+ * are safe to use this check.
1722
+ */
1723
+ execute_is_fetch = !portal -> atStart ;
1724
+
1722
1725
/* Should we display the portal names here? */
1723
1726
if (execute_is_fetch )
1724
1727
{
@@ -1727,8 +1730,18 @@ exec_execute_message(const char *portal_name, long max_rows)
1727
1730
}
1728
1731
else if (portal -> sourceText )
1729
1732
{
1730
- debug_query_string = portal -> sourceText ;
1731
- pgstat_report_activity (portal -> sourceText );
1733
+ /*
1734
+ * We must copy the sourceText into MessageContext in case the
1735
+ * portal is destroyed during finish_xact_command. Can avoid
1736
+ * the copy if it's not an xact command, though.
1737
+ */
1738
+ if (is_xact_command )
1739
+ sourceText = pstrdup (portal -> sourceText );
1740
+ else
1741
+ sourceText = portal -> sourceText ;
1742
+
1743
+ debug_query_string = sourceText ;
1744
+ pgstat_report_activity (sourceText );
1732
1745
}
1733
1746
else
1734
1747
{
@@ -1738,6 +1751,12 @@ exec_execute_message(const char *portal_name, long max_rows)
1738
1751
1739
1752
set_ps_display (portal -> commandTag , false);
1740
1753
1754
+ /* copy prepStmtName now too, in case portal is destroyed */
1755
+ if (portal -> prepStmtName )
1756
+ prepStmtName = pstrdup (portal -> prepStmtName );
1757
+ else
1758
+ prepStmtName = "<unnamed>" ;
1759
+
1741
1760
/*
1742
1761
* We use save_log_statement_stats so ShowUsage doesn't report incorrect
1743
1762
* results because ResetUsage wasn't called.
@@ -1746,14 +1765,13 @@ exec_execute_message(const char *portal_name, long max_rows)
1746
1765
ResetUsage ();
1747
1766
1748
1767
if (log_statement == LOGSTMT_ALL )
1749
- /* We have the portal, so output the source query. */
1750
1768
ereport (LOG ,
1751
1769
(errmsg ("execute %s%s%s%s: %s" ,
1752
1770
execute_is_fetch ? "fetch from " : "" ,
1753
- portal -> prepStmtName ? portal -> prepStmtName : "<unnamed>" ,
1754
- * portal -> name ? "/" : "" ,
1755
- * portal -> name ? portal -> name : "" ,
1756
- portal -> sourceText ? portal -> sourceText : "" )));
1771
+ prepStmtName ,
1772
+ * portal_name ? "/" : "" ,
1773
+ * portal_name ? portal_name : "" ,
1774
+ sourceText ? sourceText : "" )));
1757
1775
1758
1776
BeginCommand (portal -> commandTag , dest );
1759
1777
@@ -1799,7 +1817,7 @@ exec_execute_message(const char *portal_name, long max_rows)
1799
1817
1800
1818
if (completed )
1801
1819
{
1802
- if (IsTransactionStmtList ( portal -> parseTrees ) )
1820
+ if (is_xact_command )
1803
1821
{
1804
1822
/*
1805
1823
* If this was a transaction control statement, commit it. We
@@ -1861,10 +1879,10 @@ exec_execute_message(const char *portal_name, long max_rows)
1861
1879
(errmsg ("duration: %ld.%03d ms execute %s%s%s%s: %s" ,
1862
1880
secs * 1000 + msecs , usecs % 1000 ,
1863
1881
execute_is_fetch ? "fetch from " : "" ,
1864
- portal -> prepStmtName ? portal -> prepStmtName : "<unnamed>" ,
1865
- * portal -> name ? "/" : "" ,
1866
- * portal -> name ? portal -> name : "" ,
1867
- portal -> sourceText ? portal -> sourceText : "" )));
1882
+ prepStmtName ,
1883
+ * portal_name ? "/" : "" ,
1884
+ * portal_name ? portal_name : "" ,
1885
+ sourceText ? sourceText : "" )));
1868
1886
}
1869
1887
}
1870
1888
0 commit comments