1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00
Files
postgres/src/test/regress/sql/compression.sql
Michael Paquier c68cba09dd Add battery of tests related TOAST tables with oid8
The tests added by this commit cover a large ground in terms of the
introduction of oid8 in TOAST tables, with the following areas covered,
with all the gaps found during the development of the feature:
- Tables with oid8 TOAST tables in general, under strings.sql.
- ALTER TABLE with attribute manipulations in transactions and different
toast_table_value settings.
- Relation rewrites, not affecting the TOAST relation after initial
creation with VACUUM FULL, CLUSTER, REPACK.
- pg_column_compression().
- test_decoding, for external and extended TOAST data.
- amcheck, external and extended storage with heap checked.
- UPDATE with out-of-line datums that belongs to another TOAST table.
This offers coverage for toast_tuple_init(), where external vartags
could be mixed once multiple on-disk external TOAST pointers can
co-exist.  This pattern can be triggered with asan/ubsan, with an
out-of-bound memcpy when unpatched.

These tests are added before the new vartag_external, mostly as a matter
of avoiding dead code in the tree.

Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Greg Burd <greg@burd.me>
Reviewed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Reviewed-by: Yugo Nagata <nagata@sraoss.co.jp>
Discussion: https://postgr.es/m/af19kUjwjhaoUTLn@paquier.xy
2026-09-15 11:30:45 +09:00

112 lines
4.0 KiB
SQL

-- Default set of tests for TOAST compression, independent on compression
-- methods supported by the build.
CREATE SCHEMA pglz;
SET search_path TO pglz, public;
\set HIDE_TOAST_COMPRESSION false
-- ensure we get stable results regardless of installation's default
SET default_toast_compression = 'pglz';
-- test creating table with compression method
CREATE TABLE cmdata(f1 text COMPRESSION pglz);
CREATE INDEX idx ON cmdata(f1);
INSERT INTO cmdata VALUES(repeat('1234567890', 1000));
\d+ cmdata
-- verify stored compression method in the data
SELECT pg_column_compression(f1) FROM cmdata;
-- decompress data slice
SELECT SUBSTR(f1, 200, 5) FROM cmdata;
-- copy with table creation
SELECT * INTO cmmove1 FROM cmdata;
\d+ cmmove1
SELECT pg_column_compression(f1) FROM cmmove1;
-- try setting compression for incompressible data type
CREATE TABLE cmdata2 (f1 int COMPRESSION pglz);
-- test externally stored compressed data
CREATE OR REPLACE FUNCTION large_val() RETURNS TEXT LANGUAGE SQL AS
'select array_agg(fipshash(g::text))::text from generate_series(1, 256) g';
CREATE TABLE cmdata2 (f1 text COMPRESSION pglz);
INSERT INTO cmdata2 SELECT large_val() || repeat('a', 4000);
SELECT pg_column_compression(f1) FROM cmdata2;
SELECT SUBSTR(f1, 200, 5) FROM cmdata2;
DROP TABLE cmdata2;
-- pg_column_compression() with oid and oid8
CREATE TABLE toastcomp_oid(f1 text) WITH (toast_value_type = 'oid');
CREATE TABLE toastcomp_oid8(f1 text) WITH (toast_value_type = 'oid8');
ALTER TABLE toastcomp_oid ALTER COLUMN f1 SET STORAGE EXTERNAL;
ALTER TABLE toastcomp_oid8 ALTER COLUMN f1 SET STORAGE EXTERNAL;
INSERT INTO toastcomp_oid VALUES (repeat('1234567890', 10000));
INSERT INTO toastcomp_oid8 VALUES (repeat('1234567890', 10000));
SELECT pg_column_compression(f1) IS NULL AS uncompressed FROM toastcomp_oid;
SELECT pg_column_compression(f1) IS NULL AS uncompressed FROM toastcomp_oid8;
-- out-of-line and compressed.
TRUNCATE toastcomp_oid;
TRUNCATE toastcomp_oid8;
ALTER TABLE toastcomp_oid ALTER COLUMN f1 SET STORAGE EXTENDED;
ALTER TABLE toastcomp_oid8 ALTER COLUMN f1 SET STORAGE EXTENDED;
ALTER TABLE toastcomp_oid SET (toast_tuple_target = 128);
ALTER TABLE toastcomp_oid8 SET (toast_tuple_target = 128);
INSERT INTO toastcomp_oid VALUES (repeat('1234567890', 10000));
INSERT INTO toastcomp_oid8 VALUES (repeat('1234567890', 10000));
SELECT pg_column_compression(f1) FROM toastcomp_oid;
SELECT pg_column_compression(f1) FROM toastcomp_oid8;
DROP TABLE toastcomp_oid, toastcomp_oid8;
--test column type update varlena/non-varlena
CREATE TABLE cmdata2 (f1 int);
\d+ cmdata2
ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE varchar;
\d+ cmdata2
ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE int USING f1::integer;
\d+ cmdata2
--changing column storage should not impact the compression method
--but the data should not be compressed
ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE varchar;
ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION pglz;
\d+ cmdata2
ALTER TABLE cmdata2 ALTER COLUMN f1 SET STORAGE plain;
\d+ cmdata2
INSERT INTO cmdata2 VALUES (repeat('123456789', 800));
SELECT pg_column_compression(f1) FROM cmdata2;
-- test compression with inheritance
CREATE TABLE cmdata3(f1 text);
CREATE TABLE cminh() INHERITS (cmdata, cmdata3);
-- test default_toast_compression GUC
-- suppress machine-dependent details
\set VERBOSITY terse
SET default_toast_compression = '';
SET default_toast_compression = 'I do not exist compression';
SET default_toast_compression = 'pglz';
\set VERBOSITY default
ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION default;
\d+ cmdata2
DROP TABLE cmdata2;
-- VACUUM FULL does not recompress
SELECT pg_column_compression(f1) FROM cmdata;
VACUUM FULL cmdata;
SELECT pg_column_compression(f1) FROM cmdata;
-- check data is ok
SELECT length(f1) FROM cmdata;
SELECT length(f1) FROM cmmove1;
CREATE TABLE badcompresstbl (a text COMPRESSION I_Do_Not_Exist_Compression); -- fails
CREATE TABLE badcompresstbl (a text);
ALTER TABLE badcompresstbl ALTER a SET COMPRESSION I_Do_Not_Exist_Compression; -- fails
DROP TABLE badcompresstbl;
\set HIDE_TOAST_COMPRESSION true