Move pg_lzcompress.c to src/common.
authorFujii Masao <[email protected]>
Thu, 25 Dec 2014 11:46:14 +0000 (20:46 +0900)
committerFujii Masao <[email protected]>
Thu, 25 Dec 2014 11:46:14 +0000 (20:46 +0900)
Exposing compression and decompression APIs of pglz makes possible its
use by extensions and contrib modules. pglz_decompress contained a call
to elog to emit an error message in case of corrupted data. This function
is changed to return a status code to let its callers return an error instead.

This commit is required for upcoming WAL compression feature so that
the WAL reader facility can decompress the WAL data by using pglz_decompress.

Michael Paquier

src/backend/access/heap/tuptoaster.c
src/backend/utils/adt/Makefile
src/common/Makefile
src/common/pg_lzcompress.c [moved from src/backend/utils/adt/pg_lzcompress.c with 97% similarity]
src/include/common/pg_lzcompress.h [moved from src/include/utils/pg_lzcompress.h with 96% similarity]
src/tools/msvc/Mkvcbuild.pm

index d230387c85920748a99e9a79fe918acbef238689..c91bdc0d3f53fdf9d38e1e2ea689563d7e594795 100644 (file)
@@ -37,7 +37,7 @@
 #include "catalog/catalog.h"
 #include "miscadmin.h"
 #include "utils/fmgroids.h"
-#include "utils/pg_lzcompress.h"
+#include "common/pg_lzcompress.h"
 #include "utils/rel.h"
 #include "utils/typcache.h"
 #include "utils/tqual.h"
@@ -142,7 +142,8 @@ heap_tuple_untoast_attr(struct varlena * attr)
 
            attr = (struct varlena *) palloc(PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
            SET_VARSIZE(attr, PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
-           pglz_decompress(tmp, VARDATA(attr));
+           if (!pglz_decompress(tmp, VARDATA(attr)))
+               elog(ERROR, "compressed data is corrupted");
            pfree(tmp);
        }
    }
@@ -167,7 +168,8 @@ heap_tuple_untoast_attr(struct varlena * attr)
 
        attr = (struct varlena *) palloc(PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
        SET_VARSIZE(attr, PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
-       pglz_decompress(tmp, VARDATA(attr));
+       if (!pglz_decompress(tmp, VARDATA(attr)))
+           elog(ERROR, "compressed data is corrupted");
    }
    else if (VARATT_IS_SHORT(attr))
    {
@@ -239,7 +241,8 @@ heap_tuple_untoast_attr_slice(struct varlena * attr,
 
        preslice = (struct varlena *) palloc(size);
        SET_VARSIZE(preslice, size);
-       pglz_decompress(tmp, VARDATA(preslice));
+       if (!pglz_decompress(tmp, VARDATA(preslice)))
+           elog(ERROR, "compressed data is corrupted");
 
        if (tmp != (PGLZ_Header *) attr)
            pfree(tmp);
index 3ea9bf435a31c7d68ec6f142044fd36540866854..20e5ff10c7f18c13ecf9fa9d68fa02f94d55cf53 100644 (file)
@@ -25,8 +25,8 @@ OBJS = acl.o arrayfuncs.o array_selfuncs.o array_typanalyze.o \
    jsonfuncs.o like.o lockfuncs.o mac.o misc.o nabstime.o name.o \
    network.o network_gist.o network_selfuncs.o \
    numeric.o numutils.o oid.o oracle_compat.o \
-   orderedsetaggs.o pg_lzcompress.o pg_locale.o pg_lsn.o \
-   pgstatfuncs.o pseudotypes.o quote.o rangetypes.o rangetypes_gist.o \
+   orderedsetaggs.o pg_locale.o pg_lsn.o pgstatfuncs.o \
+   pseudotypes.o quote.o rangetypes.o rangetypes_gist.o \
    rangetypes_selfuncs.o rangetypes_spgist.o rangetypes_typanalyze.o \
    regexp.o regproc.o ri_triggers.o rowtypes.o ruleutils.o \
    selfuncs.o tid.o timestamp.o trigfuncs.o \
index 7edbaaa2c1a707f59cbf6b9c137838c201f28cb8..bd77c1da8c3584101668ef6ce92d7615de99f770 100644 (file)
@@ -23,7 +23,8 @@ include $(top_builddir)/src/Makefile.global
 override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
 LIBS += $(PTHREAD_LIBS)
 
-OBJS_COMMON = exec.o pgfnames.o psprintf.o relpath.o rmtree.o username.o wait_error.o
+OBJS_COMMON = exec.o pg_lzcompress.o pgfnames.o psprintf.o relpath.o \
+   rmtree.o username.o wait_error.o
 
 OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o
 
similarity index 97%
rename from src/backend/utils/adt/pg_lzcompress.c
rename to src/common/pg_lzcompress.c
index fe088901f03d132479b9007cf92df5cd323c6cca..9f97c362d189468c4c296328a2305fb0f0893580 100644 (file)
@@ -27,7 +27,7 @@
  *             FALSE if not; in the latter case the contents of dest
  *             are undefined.
  *
- *         void
+ *         bool
  *         pglz_decompress(const PGLZ_Header *source, char *dest)
  *
  *             source is the compressed input.
  *                 The data is written to buff exactly as it was handed
  *                 to pglz_compress(). No terminating zero byte is added.
  *
+ *             The return value is TRUE if decompression succeeded,
+ *             FALSE if not; in the latter case the contents of dest
+ *             are undefined.
+ *
  *     The decompression algorithm and internal data format:
  *
  *         PGLZ_Header is defined as
  *
  * Copyright (c) 1999-2014, PostgreSQL Global Development Group
  *
- * src/backend/utils/adt/pg_lzcompress.c
+ * src/common/pg_lzcompress.c
  * ----------
  */
 #include "postgres.h"
 
 #include <limits.h>
 
-#include "utils/pg_lzcompress.h"
+#include "common/pg_lzcompress.h"
 
 
 /* ----------
@@ -492,7 +496,8 @@ pglz_find_match(int16 *hstart, const char *input, const char *end,
 /* ----------
  * pglz_compress -
  *
- *     Compresses source into dest using strategy.
+ *     Compresses source into dest using strategy. Returns false if a failure
+ *     occurred, true in case of success.
  * ----------
  */
 bool
@@ -678,10 +683,11 @@ pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
 /* ----------
  * pglz_decompress -
  *
- *     Decompresses source into dest.
+ *     Decompresses source into dest. Returns false if a failure
+ *     occurred, true in case of success.
  * ----------
  */
-void
+bool
 pglz_decompress(const PGLZ_Header *source, char *dest)
 {
    const unsigned char *sp;
@@ -771,9 +777,10 @@ pglz_decompress(const PGLZ_Header *source, char *dest)
     * Check we decompressed the right amount.
     */
    if (dp != destend || sp != srcend)
-       elog(ERROR, "compressed data is corrupt");
+       return false;
 
    /*
     * That's it.
     */
+   return true;
 }
similarity index 96%
rename from src/include/utils/pg_lzcompress.h
rename to src/include/common/pg_lzcompress.h
index 4af24a32a4935e8ee6da21936580545dd33a1292..f36d2da8eeaed76c3d406c90a20228d4ea6ac488 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Definitions for the builtin LZ compressor
  *
- * src/include/utils/pg_lzcompress.h
+ * src/include/common/pg_lzcompress.h
  * ----------
  */
 
@@ -107,6 +107,6 @@ extern const PGLZ_Strategy *const PGLZ_strategy_always;
  */
 extern bool pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
              const PGLZ_Strategy *strategy);
-extern void pglz_decompress(const PGLZ_Header *source, char *dest);
+extern bool pglz_decompress(const PGLZ_Header *source, char *dest);
 
 #endif   /* _PG_LZCOMPRESS_H_ */
index 004942ca07af83dc18e857ff0492618109997f1d..6779b18300a1cb259c69daf9a582ab7f76960bd0 100644 (file)
@@ -76,7 +76,8 @@ sub mkvcbuild
    push(@pgportfiles, 'rint.c') if ($vsVersion < '12.00');
 
    our @pgcommonallfiles = qw(
-     exec.c pgfnames.c psprintf.c relpath.c rmtree.c username.c wait_error.c);
+     exec.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
+     username.c wait_error.c);
 
    our @pgcommonfrontendfiles = (@pgcommonallfiles, qw(fe_memutils.c));