Avoid PG_TRY/PG_CATCH overhead if stringinfo is already large enough. copy_hack
authorRobert Haas <rhaas@postgresql.org>
Thu, 11 Apr 2013 19:20:24 +0000 (19:20 +0000)
committerRobert Haas <rhaas@postgresql.org>
Thu, 11 Apr 2013 19:20:24 +0000 (19:20 +0000)
src/backend/libpq/pqcomm.c
src/include/lib/stringinfo.h

index d9dda21d93c3c1128d744d6486d914cf5ef2af21..b0406bba46e590b7c0d78a14b048cf30d7e7603a 100644 (file)
@@ -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)
index ecb693fcc215beed68ef50a543334330084f7203..f795680777c84bbe425ca3e68d3a8ee27828a082 100644 (file)
@@ -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 */