Use appendBinaryStringInfo in more places where the length is known
authorDavid Rowley <drowley@postgresql.org>
Mon, 22 Jul 2019 12:14:11 +0000 (00:14 +1200)
committerDavid Rowley <drowley@postgresql.org>
Mon, 22 Jul 2019 12:14:11 +0000 (00:14 +1200)
When we already know the length that we're going to append, then it
makes sense to use appendBinaryStringInfo instead of
appendStringInfoString so that the append can be performed with a simple
memcpy() using a known length rather than having to first perform a
strlen() call to obtain the length.

Discussion: https://postgr.es/m/CAKJS1f8+FRAM1s5+mAa3isajeEoAaicJ=4e0WzrH3tAusbbiMQ@mail.gmail.com

contrib/postgres_fdw/deparse.c
src/backend/executor/execMain.c
src/backend/executor/execPartition.c
src/backend/storage/lmgr/deadlock.c
src/backend/utils/adt/ri_triggers.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/adt/xml.c

index 548ae66119b0aeb3f440bcd7965c68b908f4600d..19f928ec598d55ee46975558844c6fcee9bfb8a3 100644 (file)
@@ -1531,7 +1531,7 @@ deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel,
            {
                Assert(fpinfo->jointype == JOIN_INNER);
                Assert(fpinfo->joinclauses == NIL);
-               appendStringInfoString(buf, join_sql_o.data);
+               appendBinaryStringInfo(buf, join_sql_o.data, join_sql_o.len);
                return;
            }
        }
@@ -1552,7 +1552,7 @@ deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel,
            {
                Assert(fpinfo->jointype == JOIN_INNER);
                Assert(fpinfo->joinclauses == NIL);
-               appendStringInfoString(buf, join_sql_i.data);
+               appendBinaryStringInfo(buf, join_sql_i.data, join_sql_i.len);
                return;
            }
        }
index 29e2681484c2bf1dbc75cb90cc21de12868bc4f8..dbd7dd9bcd4ece8513024b14201c5963dd8006be 100644 (file)
@@ -2290,7 +2290,7 @@ ExecBuildSlotValueDescription(Oid reloid,
            /* truncate if needed */
            vallen = strlen(val);
            if (vallen <= maxfieldlen)
-               appendStringInfoString(&buf, val);
+               appendBinaryStringInfo(&buf, val, vallen);
            else
            {
                vallen = pg_mbcliplen(val, vallen, maxfieldlen);
@@ -2309,7 +2309,7 @@ ExecBuildSlotValueDescription(Oid reloid,
    if (!table_perm)
    {
        appendStringInfoString(&collist, ") = ");
-       appendStringInfoString(&collist, buf.data);
+       appendBinaryStringInfo(&collist, buf.data, buf.len);
 
        return collist.data;
    }
index f49a48adb68ecb022f7e839faddbd2207e4ec045..729dc396a94c103659feb7d83ddf2621a2016b54 100644 (file)
@@ -1406,7 +1406,7 @@ ExecBuildSlotPartitionKeyDescription(Relation rel,
        /* truncate if needed */
        vallen = strlen(val);
        if (vallen <= maxfieldlen)
-           appendStringInfoString(&buf, val);
+           appendBinaryStringInfo(&buf, val, vallen);
        else
        {
            vallen = pg_mbcliplen(val, vallen, maxfieldlen);
index 9abc9d778f6a3365b6f5cf1e932c9ee28cf58aae..990d48377d8368ed0b064fc2b859edf55b2ca704 100644 (file)
@@ -1115,7 +1115,7 @@ DeadLockReport(void)
    }
 
    /* Duplicate all the above for the server ... */
-   appendStringInfoString(&logbuf, clientbuf.data);
+   appendBinaryStringInfo(&logbuf, clientbuf.data, clientbuf.len);
 
    /* ... and add info about query strings */
    for (i = 0; i < nDeadlockDetails; i++)
index 44a6eef5bbfd2c5a16eb6b9b4c82e466b68e010b..8c895459c3750d15b2bd9d00b49aaedf09f2a57d 100644 (file)
@@ -927,7 +927,7 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
            queryoids[i] = pk_type;
            queryoids[j] = pk_type;
        }
-       appendStringInfoString(&querybuf, qualbuf.data);
+       appendBinaryStringInfo(&querybuf, qualbuf.data, qualbuf.len);
 
        /* Prepare and save the plan */
        qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys * 2, queryoids,
@@ -1106,7 +1106,7 @@ ri_set(TriggerData *trigdata, bool is_set_null)
            qualsep = "AND";
            queryoids[i] = pk_type;
        }
-       appendStringInfoString(&querybuf, qualbuf.data);
+       appendBinaryStringInfo(&querybuf, qualbuf.data, qualbuf.len);
 
        /* Prepare and save the plan */
        qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
index 4ca0ed2bbbd22e202698c699200540cbd2fff2f6..0c58f1f1096c04d4353897598df8fa28a3aff097 100644 (file)
@@ -2804,9 +2804,9 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
        appendStringInfoChar(&dq, 'x');
    appendStringInfoChar(&dq, '$');
 
-   appendStringInfoString(&buf, dq.data);
+   appendBinaryStringInfo(&buf, dq.data, dq.len);
    appendStringInfoString(&buf, prosrc);
-   appendStringInfoString(&buf, dq.data);
+   appendBinaryStringInfo(&buf, dq.data, dq.len);
 
    appendStringInfoChar(&buf, '\n');
 
@@ -2930,7 +2930,7 @@ print_function_rettype(StringInfo buf, HeapTuple proctup)
        appendStringInfoString(&rbuf, format_type_be(proc->prorettype));
    }
 
-   appendStringInfoString(buf, rbuf.data);
+   appendBinaryStringInfo(buf, rbuf.data, rbuf.len);
 }
 
 /*
@@ -5682,7 +5682,7 @@ get_target_list(List *targetList, deparse_context *context,
        }
 
        /* Add the new field */
-       appendStringInfoString(buf, targetbuf.data);
+       appendBinaryStringInfo(buf, targetbuf.data, targetbuf.len);
    }
 
    /* clean up */
@@ -9987,7 +9987,7 @@ get_from_clause(Query *query, const char *prefix, deparse_context *context)
            }
 
            /* Add the new item */
-           appendStringInfoString(buf, itembuf.data);
+           appendBinaryStringInfo(buf, itembuf.data, itembuf.len);
 
            /* clean up */
            pfree(itembuf.data);
index d43c3055f3a5a6223d3b3df620342a1ddddaa316..5e629d29ea8a4f4046f8519650d7920538400e70 100644 (file)
@@ -559,7 +559,7 @@ xmlconcat(List *args)
                       0,
                       global_standalone);
 
-       appendStringInfoString(&buf2, buf.data);
+       appendBinaryStringInfo(&buf2, buf.data, buf.len);
        buf = buf2;
    }
 
@@ -1879,7 +1879,8 @@ xml_errorHandler(void *data, xmlErrorPtr error)
    if (xmlerrcxt->strictness == PG_XML_STRICTNESS_LEGACY)
    {
        appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
-       appendStringInfoString(&xmlerrcxt->err_buf, errorBuf->data);
+       appendBinaryStringInfo(&xmlerrcxt->err_buf, errorBuf->data,
+                              errorBuf->len);
 
        pfree(errorBuf->data);
        pfree(errorBuf);
@@ -1897,7 +1898,8 @@ xml_errorHandler(void *data, xmlErrorPtr error)
    if (level >= XML_ERR_ERROR)
    {
        appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
-       appendStringInfoString(&xmlerrcxt->err_buf, errorBuf->data);
+       appendBinaryStringInfo(&xmlerrcxt->err_buf, errorBuf->data,
+                              errorBuf->len);
 
        xmlerrcxt->err_occurred = true;
    }
@@ -2874,7 +2876,7 @@ schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
        subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
                                       targetns, false);
 
-       appendStringInfoString(result, subres->data);
+       appendBinaryStringInfo(result, subres->data, subres->len);
        appendStringInfoChar(result, '\n');
    }
 
@@ -3049,7 +3051,7 @@ database_to_xml_internal(const char *xmlschema, bool nulls,
        subres = schema_to_xml_internal(nspid, NULL, nulls,
                                        tableforest, targetns, false);
 
-       appendStringInfoString(result, subres->data);
+       appendBinaryStringInfo(result, subres->data, subres->len);
        appendStringInfoChar(result, '\n');
    }