Skip to content

Commit

Permalink
Detoast columns that are needed (#581)
Browse files Browse the repository at this point in the history
If columns contains value that could not fit into heap page we need to
detoast it before passing to duckdb execution.
  • Loading branch information
mkaruza authored Feb 7, 2025
1 parent de3735a commit a9faa3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/pgduckdb_detoast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ ToastFetchDatum(struct varlena *attr) {
return result;
}

std::lock_guard<std::mutex> lock(GlobalProcessLock::GetLock());

if (!PostgresFunctionGuard(table_relation_fetch_toast_slice, toast_pointer, attrsize, result)) {
duckdb_free(result);
throw duckdb::InternalException("(PGDuckDB/ToastFetchDatum) Error toast relation is NULL");
Expand Down
15 changes: 12 additions & 3 deletions src/pgduckdb_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,10 +1468,19 @@ InsertTupleIntoChunk(duckdb::DataChunk &output, PostgresScanLocalState &scan_loc
auto &array_mask = duckdb::FlatVector::Validity(result);
array_mask.SetInvalid(scan_local_state.output_vector_size);
} else {
/* Use returned tuple slot attr information. */
auto attr = slot->tts_tupleDescriptor->attrs[duckdb_output_index];
ConvertPostgresToDuckValue(attr.atttypid, slot->tts_values[duckdb_output_index], result,
scan_local_state.output_vector_size);
if (attr.attlen == -1) {
bool should_free = false;
Datum detoasted_value = DetoastPostgresDatum(
reinterpret_cast<varlena *>(slot->tts_values[duckdb_output_index]), &should_free);
ConvertPostgresToDuckValue(attr.atttypid, detoasted_value, result, scan_local_state.output_vector_size);
if (should_free) {
duckdb_free(reinterpret_cast<void *>(detoasted_value));
}
} else {
ConvertPostgresToDuckValue(attr.atttypid, slot->tts_values[duckdb_output_index], result,
scan_local_state.output_vector_size);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/regression/expected/basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ SELECT * FROM t JOIN s ON a = b;

DROP TABLE t;
DROP TABLE s;
-- Check that we are counting detoasted value
CREATE TABLE t(a INT, b VARCHAR);
INSERT INTO t SELECT g, repeat('ABCDE', 10000) FROM generate_series(1, 10) g;
SELECT LENGTH(b) FROM t WHERE a = 5;
length
--------
50000
(1 row)

DROP TABLE t;
6 changes: 6 additions & 0 deletions test/regression/sql/basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ CREATE TABLE s (b INT);
SELECT * FROM t JOIN s ON a = b;
DROP TABLE t;
DROP TABLE s;

-- Check that we are counting detoasted value
CREATE TABLE t(a INT, b VARCHAR);
INSERT INTO t SELECT g, repeat('ABCDE', 10000) FROM generate_series(1, 10) g;
SELECT LENGTH(b) FROM t WHERE a = 5;
DROP TABLE t;

0 comments on commit a9faa3c

Please sign in to comment.