Skip to content

Commit 3350b37

Browse files
author
Neil Conway
committed
This patch optimizes the md5_text() function (which is used to
implement the md5() SQL-level function). The old code did the following: 1. de-toast the datum 2. convert it to a cstring via textout() 3. get the length of the cstring via strlen() Since we are treating the datum context as a blob of binary data, the latter two steps are unnecessary. Once the data has been detoasted, we can just use it as-is, and derive its length from the varlena metadata. This patch improves some run-of-the-mill md5() computations by just under 10% in my limited tests, and passes the regression tests. I also noticed that md5_text() wasn't checking the return value of md5_hash(); encountering OOM at precisely the right moment could result in returning a random md5 hash. This patch corrects that. A better fix would be to make md5_hash() only return on success (and/or allocate via palloc()), but since it's used in the frontend as well I don't see an easy way to do that.
1 parent b9a87e5 commit 3350b37

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/backend/libpq/md5.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/libpq/md5.c,v 1.27 2004/12/31 21:59:50 pgsql Exp $
17+
* $PostgreSQL: pgsql/src/backend/libpq/md5.c,v 1.28 2005/02/23 22:46:17 neilc Exp $
1818
*/
1919

2020

@@ -289,8 +289,8 @@ bytesToHex(uint8 b[16], char *s)
289289
* characters. you thus need to provide an array
290290
* of 33 characters, including the trailing '\0'.
291291
*
292-
* RETURNS 0 on failure (out of memory for internal buffers) or
293-
* non-zero on success.
292+
* RETURNS false on failure (out of memory for internal buffers) or
293+
* true on success.
294294
*
295295
* STANDARDS MD5 is described in RFC 1321.
296296
*

src/backend/utils/adt/varlena.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.118 2004/12/31 22:01:22 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.119 2005/02/23 22:46:17 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2310,16 +2310,22 @@ to_hex64(PG_FUNCTION_ARGS)
23102310
Datum
23112311
md5_text(PG_FUNCTION_ARGS)
23122312
{
2313-
char *buff = PG_TEXT_GET_STR(PG_GETARG_TEXT_P(0));
2314-
size_t len = strlen(buff);
2313+
text *in_text = PG_GETARG_TEXT_P(0);
2314+
size_t len;
23152315
char *hexsum;
23162316
text *result_text;
23172317

2318+
/* Calculate the length of the buffer using varlena metadata */
2319+
len = VARSIZE(in_text) - VARHDRSZ;
2320+
23182321
/* leave room for the terminating '\0' */
23192322
hexsum = (char *) palloc(MD5_HASH_LEN + 1);
23202323

23212324
/* get the hash result */
2322-
md5_hash((void *) buff, len, hexsum);
2325+
if (md5_hash(VARDATA(in_text), len, hexsum) == false)
2326+
ereport(ERROR,
2327+
(errcode(ERRCODE_OUT_OF_MEMORY),
2328+
errmsg("out of memory")));
23232329

23242330
/* convert to text and return it */
23252331
result_text = PG_STR_GET_TEXT(hexsum);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy