summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Haas2013-04-11 19:20:24 +0000
committerRobert Haas2013-04-11 19:20:24 +0000
commitef5409534ea854748a04388905334ae958ae0e66 (patch)
tree2804afa2b40c79900c61b18aacb1df13e3aa8178
parent8cbbdddac18d5aaf9546342291fa9a20de4deee8 (diff)
Avoid PG_TRY/PG_CATCH overhead if stringinfo is already large enough.copy_hack
-rw-r--r--src/backend/libpq/pqcomm.c25
-rw-r--r--src/include/lib/stringinfo.h7
2 files changed, 21 insertions, 11 deletions
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index d9dda21d93..b0406bba46 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -1131,19 +1131,22 @@ pq_getmessage(StringInfo s, int maxlen)
* large message), we will elog(ERROR), but we want to discard the
* message body so as not to lose communication sync.
*/
- PG_TRY();
+ if (!isStringInfoLargeEnough(s, len))
{
- enlargeStringInfo(s, len);
- }
- PG_CATCH();
- {
- if (pq_discardbytes(len) == EOF)
- ereport(COMMERROR,
- (errcode(ERRCODE_PROTOCOL_VIOLATION),
- errmsg("incomplete message from client")));
- PG_RE_THROW();
+ PG_TRY();
+ {
+ enlargeStringInfo(s, len);
+ }
+ PG_CATCH();
+ {
+ if (pq_discardbytes(len) == EOF)
+ ereport(COMMERROR,
+ (errcode(ERRCODE_PROTOCOL_VIOLATION),
+ errmsg("incomplete message from client")));
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
}
- PG_END_TRY();
/* And grab the message */
if (pq_getbytes(s->data, len) == EOF)
diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h
index ecb693fcc2..f795680777 100644
--- a/src/include/lib/stringinfo.h
+++ b/src/include/lib/stringinfo.h
@@ -153,4 +153,11 @@ extern void appendBinaryStringInfo(StringInfo str,
*/
extern void enlargeStringInfo(StringInfo str, int needed);
+/*------------------------
+ * isStringInfoLargeEnough
+ * Test whether a StringInfo's buffer can hold at least 'needed' more bytes.
+ */
+#define isStringInfoLargeEnough(str,needed) \
+ ((needed) < (str)->maxlen - ((str)->len + 1))
+
#endif /* STRINGINFO_H */