1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-25 20:23:07 +03:00

Revert refactoring of hex code to src/common/

This is a combined revert of the following commits:
- c3826f8, a refactoring piece that moved the hex decoding code to
src/common/.  This code was cleaned up by aef8948, as it originally
included no overflow checks in the same way as the base64 routines in
src/common/ used by SCRAM, making it unsafe for its purpose.
- aef8948, a more advanced refactoring of the hex encoding/decoding code
to src/common/ that added sanity checks on the result buffer for hex
decoding and encoding.  As reported by Hans Buschmann, those overflow
checks are expensive, and it is possible to see a performance drop in
the decoding/encoding of bytea or LOs the longer they are.  Simple SQLs
working on large bytea values show a clear difference in perf profile.
- ccf4e27, a cleanup made possible by aef8948.

The reverts of all those commits bring back the performance of hex
decoding and encoding back to what it was in ~13.  Fow now and
post-beta3, this is the simplest option.

Reported-by: Hans Buschmann
Discussion: https://postgr.es/m/1629039545467.80333@nidsa.net
Backpatch-through: 14
This commit is contained in:
Michael Paquier
2021-08-19 09:20:13 +09:00
parent 2313dda9d4
commit 2576dcfb76
9 changed files with 127 additions and 304 deletions

View File

@@ -13,11 +13,11 @@
#include "postgres.h"
#include "access/timeline.h"
#include "common/hex.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "replication/backup_manifest.h"
#include "utils/builtins.h"
#include "utils/json.h"
static void AppendStringToManifest(backup_manifest_info *manifest, char *s);
@@ -150,12 +150,10 @@ AddFileToBackupManifest(backup_manifest_info *manifest, const char *spcoid,
}
else
{
uint64 dstlen = pg_hex_enc_len(pathlen);
appendStringInfoString(&buf, "{ \"Encoded-Path\": \"");
enlargeStringInfo(&buf, dstlen);
buf.len += pg_hex_encode(pathname, pathlen,
&buf.data[buf.len], dstlen);
enlargeStringInfo(&buf, 2 * pathlen);
buf.len += hex_encode(pathname, pathlen,
&buf.data[buf.len]);
appendStringInfoString(&buf, "\", ");
}
@@ -178,7 +176,6 @@ AddFileToBackupManifest(backup_manifest_info *manifest, const char *spcoid,
{
uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
int checksumlen;
uint64 dstlen;
checksumlen = pg_checksum_final(checksum_ctx, checksumbuf);
if (checksumlen < 0)
@@ -188,10 +185,9 @@ AddFileToBackupManifest(backup_manifest_info *manifest, const char *spcoid,
appendStringInfo(&buf,
", \"Checksum-Algorithm\": \"%s\", \"Checksum\": \"",
pg_checksum_type_name(checksum_ctx->type));
dstlen = pg_hex_enc_len(checksumlen);
enlargeStringInfo(&buf, dstlen);
buf.len += pg_hex_encode((char *) checksumbuf, checksumlen,
&buf.data[buf.len], dstlen);
enlargeStringInfo(&buf, 2 * checksumlen);
buf.len += hex_encode((char *) checksumbuf, checksumlen,
&buf.data[buf.len]);
appendStringInfoChar(&buf, '"');
}
@@ -311,9 +307,8 @@ SendBackupManifest(backup_manifest_info *manifest)
{
StringInfoData protobuf;
uint8 checksumbuf[PG_SHA256_DIGEST_LENGTH];
char *checksumstringbuf;
char checksumstringbuf[PG_SHA256_DIGEST_STRING_LENGTH];
size_t manifest_bytes_done = 0;
uint64 dstlen;
if (!IsManifestEnabled(manifest))
return;
@@ -334,11 +329,10 @@ SendBackupManifest(backup_manifest_info *manifest)
sizeof(checksumbuf)) < 0)
elog(ERROR, "failed to finalize checksum of backup manifest");
AppendStringToManifest(manifest, "\"Manifest-Checksum\": \"");
dstlen = pg_hex_enc_len(sizeof(checksumbuf));
checksumstringbuf = palloc0(dstlen + 1); /* includes \0 */
pg_hex_encode((char *) checksumbuf, sizeof(checksumbuf),
checksumstringbuf, dstlen);
checksumstringbuf[dstlen] = '\0';
hex_encode((char *) checksumbuf, sizeof checksumbuf, checksumstringbuf);
checksumstringbuf[PG_SHA256_DIGEST_STRING_LENGTH - 1] = '\0';
AppendStringToManifest(manifest, checksumstringbuf);
AppendStringToManifest(manifest, "\"}\n");