New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft compressed datum slicing #2

Open
wants to merge 2 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+42 −7
Diff settings

Always

Just for now

@@ -76,6 +76,7 @@ static struct varlena *toast_fetch_datum(struct varlena *attr);
static struct varlena *toast_fetch_datum_slice(struct varlena *attr,
int32 sliceoffset, int32 length);
static struct varlena *toast_decompress_datum(struct varlena *attr);
static struct varlena *toast_decompress_datum_slice(struct varlena *attr, int32 slicelength);
static int toast_open_indexes(Relation toastrel,
LOCKMODE lock,
Relation **toastidxs,
@@ -302,7 +303,10 @@ heap_tuple_untoast_attr_slice(struct varlena *attr,
{
struct varlena *tmp = preslice;
preslice = toast_decompress_datum(tmp);
if (sliceoffset == 0 && slicelength > 0)
preslice = toast_decompress_datum_slice(tmp, slicelength);
else
preslice = toast_decompress_datum(tmp);
if (tmp != attr)
pfree(tmp);
@@ -2287,9 +2291,40 @@ toast_decompress_datum(struct varlena *attr)
if (pglz_decompress(TOAST_COMPRESS_RAWDATA(attr),
VARSIZE(attr) - TOAST_COMPRESS_HDRSZ,
VARDATA(result),
TOAST_COMPRESS_RAWSIZE(attr)) < 0)
TOAST_COMPRESS_RAWSIZE(attr), false) < 0)
elog(ERROR, "compressed data is corrupted");
return result;
}
/* ----------
* toast_decompress_datum_slice -
*
* Decompress the front of a compressed version of a varlena datum
* We do not try to slice with an offset, as that means decompressing
* and then throwing away the data at the front
*/
static struct varlena *
toast_decompress_datum_slice(struct varlena *attr, int32 slicelength)
{
struct varlena *result;
int32 rawsize;
Assert(VARATT_IS_COMPRESSED(attr));
result = (struct varlena *)
palloc(TOAST_COMPRESS_RAWSIZE(attr) + VARHDRSZ);
SET_VARSIZE(result, TOAST_COMPRESS_RAWSIZE(attr) + VARHDRSZ);
rawsize = pglz_decompress(TOAST_COMPRESS_RAWDATA(attr),
VARSIZE(attr) - TOAST_COMPRESS_HDRSZ,
VARDATA(result),
slicelength, true);
if (rawsize < 0)
elog(ERROR, "compressed data is corrupted");
SET_VARSIZE(result, rawsize + VARHDRSZ);
return result;
}
@@ -1426,7 +1426,7 @@ RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
{
/* If a backup block image is compressed, decompress it */
if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
BLCKSZ - bkpb->hole_length) < 0)
BLCKSZ - bkpb->hole_length, false) < 0)
{
report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
(uint32) (record->ReadRecPtr >> 32),
@@ -680,7 +680,7 @@ pglz_compress(const char *source, int32 slen, char *dest,
*/
int32
pglz_decompress(const char *source, int32 slen, char *dest,
int32 rawsize)
int32 rawsize, bool is_slice)
{
const unsigned char *sp;
const unsigned char *srcend;
@@ -768,11 +768,11 @@ pglz_decompress(const char *source, int32 slen, char *dest,
/*
* Check we decompressed the right amount.
*/
if (dp != destend || sp != srcend)
if (!is_slice && (dp != destend || sp != srcend))
return -1;
/*
* That's it.
*/
return rawsize;
return (char*)dp - dest;
}
@@ -86,6 +86,6 @@ extern const PGLZ_Strategy *const PGLZ_strategy_always;
extern int32 pglz_compress(const char *source, int32 slen, char *dest,
const PGLZ_Strategy *strategy);
extern int32 pglz_decompress(const char *source, int32 slen, char *dest,
int32 rawsize);
int32 rawsize, bool is_slice);
#endif /* _PG_LZCOMPRESS_H_ */
ProTip! Use n and p to navigate between commits in a pull request.