From 747814af420e69647c6cbc0447d1c37ec53db80b Mon Sep 17 00:00:00 2001
From: Nikhil Kumar Veldanda <veldanda.nikhilkumar17@gmail.com>
Date: Tue, 3 Jun 2025 11:35:24 +0000
Subject: [PATCH v25 1/3] Add pg_compression_available and split sql
 compression tests - Introduce new SQL function
 `pg_compression_available(text)`: - Returns true/false depending on whether
 the given TOAST compression method (e.g. 'pglz', 'lz4', 'zstd') is supported
 in the current build.

---
 doc/src/sgml/func.sgml                        |  20 ++
 src/backend/access/common/toast_compression.c |  32 +++
 src/include/catalog/pg_proc.dat               |   5 +
 src/test/regress/expected/compression.out     |  93 ++++----
 ...{compression_1.out => compression_lz4.out} | 216 +++++++++---------
 .../regress/expected/compression_lz4_1.out    |   5 +
 src/test/regress/parallel_schedule            |   2 +-
 src/test/regress/sql/compression.sql          |  20 +-
 src/test/regress/sql/compression_lz4.sql      | 161 +++++++++++++
 9 files changed, 384 insertions(+), 170 deletions(-)
 rename src/test/regress/expected/{compression_1.out => compression_lz4.out} (66%)
 create mode 100644 src/test/regress/expected/compression_lz4_1.out
 create mode 100644 src/test/regress/sql/compression_lz4.sql

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index c67688cbf5f..56284237df7 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -25288,6 +25288,26 @@ SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
         This is equivalent to <function>current_user</function>.
        </para></entry>
       </row>
+      <row>
+        <entry role="func_table_entry">
+          <para role="func_signature">
+            <indexterm>
+              <primary>pg_compression_available</primary>
+            </indexterm>
+            <function>pg_compression_available</function> ( <type>text</type> )
+            <returnvalue>boolean</returnvalue>
+          </para>
+          <para>
+            Returns <literal>true</literal> if the given compression method name is supported in this PostgreSQL build.
+            The built-in compression method <literal>pglz</literal> is always available because it is included
+            in the core server code and does not require any additional compile-time option or external library.
+            In contrast, <literal>lz4</literal> will return <literal>true</literal> only if PostgreSQL was
+            compiled with the <literal>--with-lz4</literal> flag, and <literal>zstd</literal> will return
+            <literal>true</literal> only if compiled with the <literal>--with-zstd</literal> flag.
+            If the input name does not match any supported method, the function returns <literal>false</literal>.
+          </para>
+        </entry>
+      </row>
      </tbody>
     </tgroup>
    </table>
diff --git a/src/backend/access/common/toast_compression.c b/src/backend/access/common/toast_compression.c
index 21f2f4af97e..fb93555bdb0 100644
--- a/src/backend/access/common/toast_compression.c
+++ b/src/backend/access/common/toast_compression.c
@@ -21,6 +21,9 @@
 #include "access/toast_compression.h"
 #include "common/pg_lzcompress.h"
 #include "varatt.h"
+#include "fmgr.h"
+#include "parser/scansup.h"
+#include "utils/builtins.h"
 
 /* GUC */
 int			default_toast_compression = TOAST_PGLZ_COMPRESSION;
@@ -314,3 +317,32 @@ GetCompressionMethodName(char method)
 			return NULL;		/* keep compiler quiet */
 	}
 }
+
+/*
+ * pg_compression_available(text) → bool
+ *
+ * True if the named TOAST compressor method was compiled into this server.
+ */
+Datum
+pg_compression_available(PG_FUNCTION_ARGS)
+{
+	text	   *name = PG_GETARG_TEXT_PP(0);
+	char	   *cname = downcase_truncate_identifier(text_to_cstring(name),
+													 NAMEDATALEN, false);
+
+	/* pglz is always there */
+	if (strcmp(cname, "pglz") == 0)
+		PG_RETURN_BOOL(true);
+
+#ifdef USE_LZ4
+	if (strcmp(cname, "lz4") == 0)
+		PG_RETURN_BOOL(true);
+#endif
+
+#ifdef USE_ZSTD
+	if (strcmp(cname, "zstd") == 0)
+		PG_RETURN_BOOL(true);
+#endif
+
+	PG_RETURN_BOOL(false);
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index d3d28a263fa..0a1ef15b481 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -12556,4 +12556,9 @@
   proargnames => '{pid,io_id,io_generation,state,operation,off,length,target,handle_data_len,raw_result,result,target_desc,f_sync,f_localmem,f_buffered}',
   prosrc => 'pg_get_aios' },
 
+{ oid =>  9474, descr => 'is toast compression method available?',
+  proname => 'pg_compression_available', prokind => 'f',
+  provolatile => 'i', prorettype => 'bool', proargtypes => 'text',
+  prosrc => 'pg_compression_available' },
+
 ]
diff --git a/src/test/regress/expected/compression.out b/src/test/regress/expected/compression.out
index 4dd9ee7200d..84b4d0b39d4 100644
--- a/src/test/regress/expected/compression.out
+++ b/src/test/regress/expected/compression.out
@@ -1,3 +1,5 @@
+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';
@@ -6,20 +8,20 @@ CREATE TABLE cmdata(f1 text COMPRESSION pglz);
 CREATE INDEX idx ON cmdata(f1);
 INSERT INTO cmdata VALUES(repeat('1234567890', 1000));
 \d+ cmdata
-                                        Table "public.cmdata"
+                                         Table "pglz.cmdata"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | text |           |          |         | extended | pglz        |              | 
 Indexes:
     "idx" btree (f1)
 
-CREATE TABLE cmdata1(f1 TEXT COMPRESSION lz4);
+CREATE TABLE cmdata1(f1 TEXT COMPRESSION pglz);
 INSERT INTO cmdata1 VALUES(repeat('1234567890', 1004));
 \d+ cmdata1
-                                        Table "public.cmdata1"
+                                         Table "pglz.cmdata1"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
- f1     | text |           |          |         | extended | lz4         |              | 
+ f1     | text |           |          |         | extended | pglz        |              | 
 
 -- verify stored compression method in the data
 SELECT pg_column_compression(f1) FROM cmdata;
@@ -31,7 +33,7 @@ SELECT pg_column_compression(f1) FROM cmdata;
 SELECT pg_column_compression(f1) FROM cmdata1;
  pg_column_compression 
 -----------------------
- lz4
+ pglz
 (1 row)
 
 -- decompress data slice
@@ -50,7 +52,7 @@ SELECT SUBSTR(f1, 2000, 50) FROM cmdata1;
 -- copy with table creation
 SELECT * INTO cmmove1 FROM cmdata;
 \d+ cmmove1
-                                        Table "public.cmmove1"
+                                         Table "pglz.cmmove1"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | text |           |          |         | extended |             |              | 
@@ -69,16 +71,16 @@ SELECT pg_column_compression(f1) FROM cmmove3;
  pg_column_compression 
 -----------------------
  pglz
- lz4
+ pglz
 (2 rows)
 
 -- test LIKE INCLUDING COMPRESSION
 CREATE TABLE cmdata2 (LIKE cmdata1 INCLUDING COMPRESSION);
 \d+ cmdata2
-                                        Table "public.cmdata2"
+                                         Table "pglz.cmdata2"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
- f1     | text |           |          |         | extended | lz4         |              | 
+ f1     | text |           |          |         | extended | pglz        |              | 
 
 DROP TABLE cmdata2;
 -- try setting compression for incompressible data type
@@ -97,7 +99,7 @@ UPDATE cmmove2 SET f1 = cmdata1.f1 FROM cmdata1;
 SELECT pg_column_compression(f1) FROM cmmove2;
  pg_column_compression 
 -----------------------
- lz4
+ pglz
 (1 row)
 
 -- test externally stored compressed data
@@ -115,8 +117,8 @@ INSERT INTO cmdata1 SELECT large_val() || repeat('a', 4000);
 SELECT pg_column_compression(f1) FROM cmdata1;
  pg_column_compression 
 -----------------------
- lz4
- lz4
+ pglz
+ pglz
 (2 rows)
 
 SELECT SUBSTR(f1, 200, 5) FROM cmdata1;
@@ -136,21 +138,21 @@ DROP TABLE cmdata2;
 --test column type update varlena/non-varlena
 CREATE TABLE cmdata2 (f1 int);
 \d+ cmdata2
-                                         Table "public.cmdata2"
+                                          Table "pglz.cmdata2"
  Column |  Type   | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+---------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | integer |           |          |         | plain   |             |              | 
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE varchar;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                               Table "pglz.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | character varying |           |          |         | extended |             |              | 
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE int USING f1::integer;
 \d+ cmdata2
-                                         Table "public.cmdata2"
+                                          Table "pglz.cmdata2"
  Column |  Type   | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+---------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | integer |           |          |         | plain   |             |              | 
@@ -160,14 +162,14 @@ ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE int USING f1::integer;
 ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE varchar;
 ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION pglz;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                               Table "pglz.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | character varying |           |          |         | extended | pglz        |              | 
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 SET STORAGE plain;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                               Table "pglz.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | character varying |           |          |         | plain   | pglz        |              | 
@@ -182,7 +184,7 @@ SELECT pg_column_compression(f1) FROM cmdata2;
 -- test compression with materialized view
 CREATE MATERIALIZED VIEW compressmv(x) AS SELECT * FROM cmdata1;
 \d+ compressmv
-                                Materialized view "public.compressmv"
+                                 Materialized view "pglz.compressmv"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
  x      | text |           |          |         | extended |             |              | 
@@ -193,19 +195,19 @@ View definition:
 SELECT pg_column_compression(f1) FROM cmdata1;
  pg_column_compression 
 -----------------------
- lz4
- lz4
+ pglz
+ pglz
 (2 rows)
 
 SELECT pg_column_compression(x) FROM compressmv;
  pg_column_compression 
 -----------------------
- lz4
- lz4
+ pglz
+ pglz
 (2 rows)
 
 -- test compression with partition
-CREATE TABLE cmpart(f1 text COMPRESSION lz4) PARTITION BY HASH(f1);
+CREATE TABLE cmpart(f1 text COMPRESSION pglz) PARTITION BY HASH(f1);
 CREATE TABLE cmpart1 PARTITION OF cmpart FOR VALUES WITH (MODULUS 2, REMAINDER 0);
 CREATE TABLE cmpart2(f1 text COMPRESSION pglz);
 ALTER TABLE cmpart ATTACH PARTITION cmpart2 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
@@ -214,7 +216,7 @@ INSERT INTO cmpart VALUES (repeat('123456789', 4004));
 SELECT pg_column_compression(f1) FROM cmpart1;
  pg_column_compression 
 -----------------------
- lz4
+ pglz
 (1 row)
 
 SELECT pg_column_compression(f1) FROM cmpart2;
@@ -224,34 +226,19 @@ SELECT pg_column_compression(f1) FROM cmpart2;
 (1 row)
 
 -- test compression with inheritance
-CREATE TABLE cminh() INHERITS(cmdata, cmdata1); -- error
-NOTICE:  merging multiple inherited definitions of column "f1"
-ERROR:  column "f1" has a compression method conflict
-DETAIL:  pglz versus lz4
-CREATE TABLE cminh(f1 TEXT COMPRESSION lz4) INHERITS(cmdata); -- error
-NOTICE:  merging column "f1" with inherited definition
-ERROR:  column "f1" has a compression method conflict
-DETAIL:  pglz versus lz4
 CREATE TABLE cmdata3(f1 text);
 CREATE TABLE cminh() INHERITS (cmdata, cmdata3);
 NOTICE:  merging multiple inherited definitions of column "f1"
 -- test default_toast_compression GUC
-SET default_toast_compression = '';
-ERROR:  invalid value for parameter "default_toast_compression": ""
-HINT:  Available values: pglz, lz4.
-SET default_toast_compression = 'I do not exist compression';
-ERROR:  invalid value for parameter "default_toast_compression": "I do not exist compression"
-HINT:  Available values: pglz, lz4.
-SET default_toast_compression = 'lz4';
 SET default_toast_compression = 'pglz';
 -- test alter compression method
-ALTER TABLE cmdata ALTER COLUMN f1 SET COMPRESSION lz4;
+ALTER TABLE cmdata ALTER COLUMN f1 SET COMPRESSION pglz;
 INSERT INTO cmdata VALUES (repeat('123456789', 4004));
 \d+ cmdata
-                                        Table "public.cmdata"
+                                         Table "pglz.cmdata"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
- f1     | text |           |          |         | extended | lz4         |              | 
+ f1     | text |           |          |         | extended | pglz        |              | 
 Indexes:
     "idx" btree (f1)
 Child tables: cminh
@@ -260,37 +247,37 @@ SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
 -----------------------
  pglz
- lz4
+ pglz
 (2 rows)
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION default;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                               Table "pglz.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | character varying |           |          |         | plain   |             |              | 
 
 -- test alter compression method for materialized views
-ALTER MATERIALIZED VIEW compressmv ALTER COLUMN x SET COMPRESSION lz4;
+ALTER MATERIALIZED VIEW compressmv ALTER COLUMN x SET COMPRESSION pglz;
 \d+ compressmv
-                                Materialized view "public.compressmv"
+                                 Materialized view "pglz.compressmv"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
- x      | text |           |          |         | extended | lz4         |              | 
+ x      | text |           |          |         | extended | pglz        |              | 
 View definition:
  SELECT f1 AS x
    FROM cmdata1;
 
 -- test alter compression method for partitioned tables
 ALTER TABLE cmpart1 ALTER COLUMN f1 SET COMPRESSION pglz;
-ALTER TABLE cmpart2 ALTER COLUMN f1 SET COMPRESSION lz4;
+ALTER TABLE cmpart2 ALTER COLUMN f1 SET COMPRESSION pglz;
 -- new data should be compressed with the current compression method
 INSERT INTO cmpart VALUES (repeat('123456789', 1004));
 INSERT INTO cmpart VALUES (repeat('123456789', 4004));
 SELECT pg_column_compression(f1) FROM cmpart1;
  pg_column_compression 
 -----------------------
- lz4
+ pglz
  pglz
 (2 rows)
 
@@ -298,7 +285,7 @@ SELECT pg_column_compression(f1) FROM cmpart2;
  pg_column_compression 
 -----------------------
  pglz
- lz4
+ pglz
 (2 rows)
 
 -- VACUUM FULL does not recompress
@@ -306,7 +293,7 @@ SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
 -----------------------
  pglz
- lz4
+ pglz
 (2 rows)
 
 VACUUM FULL cmdata;
@@ -314,12 +301,12 @@ SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
 -----------------------
  pglz
- lz4
+ pglz
 (2 rows)
 
 -- test expression index
 DROP TABLE cmdata2;
-CREATE TABLE cmdata2 (f1 TEXT COMPRESSION pglz, f2 TEXT COMPRESSION lz4);
+CREATE TABLE cmdata2 (f1 TEXT COMPRESSION pglz, f2 TEXT COMPRESSION pglz);
 CREATE UNIQUE INDEX idx1 ON cmdata2 ((f1 || f2));
 INSERT INTO cmdata2 VALUES((SELECT array_agg(fipshash(g::TEXT))::TEXT FROM
 generate_series(1, 50) g), VERSION());
diff --git a/src/test/regress/expected/compression_1.out b/src/test/regress/expected/compression_lz4.out
similarity index 66%
rename from src/test/regress/expected/compression_1.out
rename to src/test/regress/expected/compression_lz4.out
index 7bd7642b4b9..d4970ffe5d4 100644
--- a/src/test/regress/expected/compression_1.out
+++ b/src/test/regress/expected/compression_lz4.out
@@ -1,12 +1,19 @@
+SELECT NOT(pg_compression_available('lz4')) AS skip_test \gset
+\if :skip_test
+   \echo '*** skipping lz4 tests (lz4 not available) ***'
+   \quit
+\endif
+CREATE SCHEMA lz4;
+SET search_path TO lz4, public;
 \set HIDE_TOAST_COMPRESSION false
 -- ensure we get stable results regardless of installation's default
-SET default_toast_compression = 'pglz';
+SET default_toast_compression = 'lz4';
 -- 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
-                                        Table "public.cmdata"
+                                          Table "lz4.cmdata"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | text |           |          |         | extended | pglz        |              | 
@@ -14,13 +21,13 @@ Indexes:
     "idx" btree (f1)
 
 CREATE TABLE cmdata1(f1 TEXT COMPRESSION lz4);
-ERROR:  compression method lz4 not supported
-DETAIL:  This functionality requires the server to be built with lz4 support.
 INSERT INTO cmdata1 VALUES(repeat('1234567890', 1004));
-ERROR:  relation "cmdata1" does not exist
-LINE 1: INSERT INTO cmdata1 VALUES(repeat('1234567890', 1004));
-                    ^
 \d+ cmdata1
+                                         Table "lz4.cmdata1"
+ Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
+--------+------+-----------+----------+---------+----------+-------------+--------------+-------------
+ f1     | text |           |          |         | extended | lz4         |              | 
+
 -- verify stored compression method in the data
 SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
@@ -29,9 +36,11 @@ SELECT pg_column_compression(f1) FROM cmdata;
 (1 row)
 
 SELECT pg_column_compression(f1) FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: SELECT pg_column_compression(f1) FROM cmdata1;
-                                              ^
+ pg_column_compression 
+-----------------------
+ lz4
+(1 row)
+
 -- decompress data slice
 SELECT SUBSTR(f1, 200, 5) FROM cmdata;
  substr 
@@ -40,13 +49,15 @@ SELECT SUBSTR(f1, 200, 5) FROM cmdata;
 (1 row)
 
 SELECT SUBSTR(f1, 2000, 50) FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: SELECT SUBSTR(f1, 2000, 50) FROM cmdata1;
-                                         ^
+                       substr                       
+----------------------------------------------------
+ 01234567890123456789012345678901234567890123456789
+(1 row)
+
 -- copy with table creation
 SELECT * INTO cmmove1 FROM cmdata;
 \d+ cmmove1
-                                        Table "public.cmmove1"
+                                         Table "lz4.cmmove1"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | text |           |          |         | extended |             |              | 
@@ -61,23 +72,22 @@ SELECT pg_column_compression(f1) FROM cmmove1;
 CREATE TABLE cmmove3(f1 text COMPRESSION pglz);
 INSERT INTO cmmove3 SELECT * FROM cmdata;
 INSERT INTO cmmove3 SELECT * FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: INSERT INTO cmmove3 SELECT * FROM cmdata1;
-                                          ^
 SELECT pg_column_compression(f1) FROM cmmove3;
  pg_column_compression 
 -----------------------
  pglz
-(1 row)
+ lz4
+(2 rows)
 
 -- test LIKE INCLUDING COMPRESSION
 CREATE TABLE cmdata2 (LIKE cmdata1 INCLUDING COMPRESSION);
-ERROR:  relation "cmdata1" does not exist
-LINE 1: CREATE TABLE cmdata2 (LIKE cmdata1 INCLUDING COMPRESSION);
-                                   ^
 \d+ cmdata2
+                                         Table "lz4.cmdata2"
+ Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
+--------+------+-----------+----------+---------+----------+-------------+--------------+-------------
+ f1     | text |           |          |         | extended | lz4         |              | 
+
 DROP TABLE cmdata2;
-ERROR:  table "cmdata2" does not exist
 -- try setting compression for incompressible data type
 CREATE TABLE cmdata2 (f1 int COMPRESSION pglz);
 ERROR:  column data type integer does not support compression
@@ -91,13 +101,10 @@ SELECT pg_column_compression(f1) FROM cmmove2;
 (1 row)
 
 UPDATE cmmove2 SET f1 = cmdata1.f1 FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: UPDATE cmmove2 SET f1 = cmdata1.f1 FROM cmdata1;
-                                                ^
 SELECT pg_column_compression(f1) FROM cmmove2;
  pg_column_compression 
 -----------------------
- pglz
+ lz4
 (1 row)
 
 -- test externally stored compressed data
@@ -112,17 +119,20 @@ SELECT pg_column_compression(f1) FROM cmdata2;
 (1 row)
 
 INSERT INTO cmdata1 SELECT large_val() || repeat('a', 4000);
-ERROR:  relation "cmdata1" does not exist
-LINE 1: INSERT INTO cmdata1 SELECT large_val() || repeat('a', 4000);
-                    ^
 SELECT pg_column_compression(f1) FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: SELECT pg_column_compression(f1) FROM cmdata1;
-                                              ^
+ pg_column_compression 
+-----------------------
+ lz4
+ lz4
+(2 rows)
+
 SELECT SUBSTR(f1, 200, 5) FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: SELECT SUBSTR(f1, 200, 5) FROM cmdata1;
-                                       ^
+ substr 
+--------
+ 01234
+ 79026
+(2 rows)
+
 SELECT SUBSTR(f1, 200, 5) FROM cmdata2;
  substr 
 --------
@@ -133,21 +143,21 @@ DROP TABLE cmdata2;
 --test column type update varlena/non-varlena
 CREATE TABLE cmdata2 (f1 int);
 \d+ cmdata2
-                                         Table "public.cmdata2"
+                                          Table "lz4.cmdata2"
  Column |  Type   | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+---------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | integer |           |          |         | plain   |             |              | 
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE varchar;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                                Table "lz4.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | character varying |           |          |         | extended |             |              | 
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE int USING f1::integer;
 \d+ cmdata2
-                                         Table "public.cmdata2"
+                                          Table "lz4.cmdata2"
  Column |  Type   | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+---------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | integer |           |          |         | plain   |             |              | 
@@ -157,14 +167,14 @@ ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE int USING f1::integer;
 ALTER TABLE cmdata2 ALTER COLUMN f1 TYPE varchar;
 ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION pglz;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                                Table "lz4.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+----------+-------------+--------------+-------------
  f1     | character varying |           |          |         | extended | pglz        |              | 
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 SET STORAGE plain;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                               Table "lz4.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | character varying |           |          |         | plain   | pglz        |              | 
@@ -178,47 +188,53 @@ SELECT pg_column_compression(f1) FROM cmdata2;
 
 -- test compression with materialized view
 CREATE MATERIALIZED VIEW compressmv(x) AS SELECT * FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: ...TE MATERIALIZED VIEW compressmv(x) AS SELECT * FROM cmdata1;
-                                                               ^
 \d+ compressmv
+                                  Materialized view "lz4.compressmv"
+ Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
+--------+------+-----------+----------+---------+----------+-------------+--------------+-------------
+ x      | text |           |          |         | extended |             |              | 
+View definition:
+ SELECT f1 AS x
+   FROM cmdata1;
+
 SELECT pg_column_compression(f1) FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: SELECT pg_column_compression(f1) FROM cmdata1;
-                                              ^
+ pg_column_compression 
+-----------------------
+ lz4
+ lz4
+(2 rows)
+
 SELECT pg_column_compression(x) FROM compressmv;
-ERROR:  relation "compressmv" does not exist
-LINE 1: SELECT pg_column_compression(x) FROM compressmv;
-                                             ^
+ pg_column_compression 
+-----------------------
+ lz4
+ lz4
+(2 rows)
+
 -- test compression with partition
 CREATE TABLE cmpart(f1 text COMPRESSION lz4) PARTITION BY HASH(f1);
-ERROR:  compression method lz4 not supported
-DETAIL:  This functionality requires the server to be built with lz4 support.
 CREATE TABLE cmpart1 PARTITION OF cmpart FOR VALUES WITH (MODULUS 2, REMAINDER 0);
-ERROR:  relation "cmpart" does not exist
 CREATE TABLE cmpart2(f1 text COMPRESSION pglz);
 ALTER TABLE cmpart ATTACH PARTITION cmpart2 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
-ERROR:  relation "cmpart" does not exist
 INSERT INTO cmpart VALUES (repeat('123456789', 1004));
-ERROR:  relation "cmpart" does not exist
-LINE 1: INSERT INTO cmpart VALUES (repeat('123456789', 1004));
-                    ^
 INSERT INTO cmpart VALUES (repeat('123456789', 4004));
-ERROR:  relation "cmpart" does not exist
-LINE 1: INSERT INTO cmpart VALUES (repeat('123456789', 4004));
-                    ^
 SELECT pg_column_compression(f1) FROM cmpart1;
-ERROR:  relation "cmpart1" does not exist
-LINE 1: SELECT pg_column_compression(f1) FROM cmpart1;
-                                              ^
+ pg_column_compression 
+-----------------------
+ lz4
+(1 row)
+
 SELECT pg_column_compression(f1) FROM cmpart2;
  pg_column_compression 
 -----------------------
-(0 rows)
+ pglz
+(1 row)
 
 -- test compression with inheritance
 CREATE TABLE cminh() INHERITS(cmdata, cmdata1); -- error
-ERROR:  relation "cmdata1" does not exist
+NOTICE:  merging multiple inherited definitions of column "f1"
+ERROR:  column "f1" has a compression method conflict
+DETAIL:  pglz versus lz4
 CREATE TABLE cminh(f1 TEXT COMPRESSION lz4) INHERITS(cmdata); -- error
 NOTICE:  merging column "f1" with inherited definition
 ERROR:  column "f1" has a compression method conflict
@@ -227,26 +243,15 @@ CREATE TABLE cmdata3(f1 text);
 CREATE TABLE cminh() INHERITS (cmdata, cmdata3);
 NOTICE:  merging multiple inherited definitions of column "f1"
 -- test default_toast_compression GUC
-SET default_toast_compression = '';
-ERROR:  invalid value for parameter "default_toast_compression": ""
-HINT:  Available values: pglz.
-SET default_toast_compression = 'I do not exist compression';
-ERROR:  invalid value for parameter "default_toast_compression": "I do not exist compression"
-HINT:  Available values: pglz.
 SET default_toast_compression = 'lz4';
-ERROR:  invalid value for parameter "default_toast_compression": "lz4"
-HINT:  Available values: pglz.
-SET default_toast_compression = 'pglz';
 -- test alter compression method
 ALTER TABLE cmdata ALTER COLUMN f1 SET COMPRESSION lz4;
-ERROR:  compression method lz4 not supported
-DETAIL:  This functionality requires the server to be built with lz4 support.
 INSERT INTO cmdata VALUES (repeat('123456789', 4004));
 \d+ cmdata
-                                        Table "public.cmdata"
+                                          Table "lz4.cmdata"
  Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
 --------+------+-----------+----------+---------+----------+-------------+--------------+-------------
- f1     | text |           |          |         | extended | pglz        |              | 
+ f1     | text |           |          |         | extended | lz4         |              | 
 Indexes:
     "idx" btree (f1)
 Child tables: cminh
@@ -255,50 +260,53 @@ SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
 -----------------------
  pglz
- pglz
+ lz4
 (2 rows)
 
 ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION default;
 \d+ cmdata2
-                                              Table "public.cmdata2"
+                                               Table "lz4.cmdata2"
  Column |       Type        | Collation | Nullable | Default | Storage | Compression | Stats target | Description 
 --------+-------------------+-----------+----------+---------+---------+-------------+--------------+-------------
  f1     | character varying |           |          |         | plain   |             |              | 
 
 -- test alter compression method for materialized views
 ALTER MATERIALIZED VIEW compressmv ALTER COLUMN x SET COMPRESSION lz4;
-ERROR:  relation "compressmv" does not exist
 \d+ compressmv
+                                  Materialized view "lz4.compressmv"
+ Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
+--------+------+-----------+----------+---------+----------+-------------+--------------+-------------
+ x      | text |           |          |         | extended | lz4         |              | 
+View definition:
+ SELECT f1 AS x
+   FROM cmdata1;
+
 -- test alter compression method for partitioned tables
 ALTER TABLE cmpart1 ALTER COLUMN f1 SET COMPRESSION pglz;
-ERROR:  relation "cmpart1" does not exist
 ALTER TABLE cmpart2 ALTER COLUMN f1 SET COMPRESSION lz4;
-ERROR:  compression method lz4 not supported
-DETAIL:  This functionality requires the server to be built with lz4 support.
 -- new data should be compressed with the current compression method
 INSERT INTO cmpart VALUES (repeat('123456789', 1004));
-ERROR:  relation "cmpart" does not exist
-LINE 1: INSERT INTO cmpart VALUES (repeat('123456789', 1004));
-                    ^
 INSERT INTO cmpart VALUES (repeat('123456789', 4004));
-ERROR:  relation "cmpart" does not exist
-LINE 1: INSERT INTO cmpart VALUES (repeat('123456789', 4004));
-                    ^
 SELECT pg_column_compression(f1) FROM cmpart1;
-ERROR:  relation "cmpart1" does not exist
-LINE 1: SELECT pg_column_compression(f1) FROM cmpart1;
-                                              ^
+ pg_column_compression 
+-----------------------
+ lz4
+ pglz
+(2 rows)
+
 SELECT pg_column_compression(f1) FROM cmpart2;
  pg_column_compression 
 -----------------------
-(0 rows)
+ pglz
+ lz4
+(2 rows)
 
 -- VACUUM FULL does not recompress
 SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
 -----------------------
  pglz
- pglz
+ lz4
 (2 rows)
 
 VACUUM FULL cmdata;
@@ -306,21 +314,15 @@ SELECT pg_column_compression(f1) FROM cmdata;
  pg_column_compression 
 -----------------------
  pglz
- pglz
+ lz4
 (2 rows)
 
 -- test expression index
 DROP TABLE cmdata2;
 CREATE TABLE cmdata2 (f1 TEXT COMPRESSION pglz, f2 TEXT COMPRESSION lz4);
-ERROR:  compression method lz4 not supported
-DETAIL:  This functionality requires the server to be built with lz4 support.
 CREATE UNIQUE INDEX idx1 ON cmdata2 ((f1 || f2));
-ERROR:  relation "cmdata2" does not exist
 INSERT INTO cmdata2 VALUES((SELECT array_agg(fipshash(g::TEXT))::TEXT FROM
 generate_series(1, 50) g), VERSION());
-ERROR:  relation "cmdata2" does not exist
-LINE 1: INSERT INTO cmdata2 VALUES((SELECT array_agg(fipshash(g::TEX...
-                    ^
 -- check data is ok
 SELECT length(f1) FROM cmdata;
  length 
@@ -330,9 +332,12 @@ SELECT length(f1) FROM cmdata;
 (2 rows)
 
 SELECT length(f1) FROM cmdata1;
-ERROR:  relation "cmdata1" does not exist
-LINE 1: SELECT length(f1) FROM cmdata1;
-                               ^
+ length 
+--------
+  10040
+  12449
+(2 rows)
+
 SELECT length(f1) FROM cmmove1;
  length 
 --------
@@ -349,7 +354,8 @@ SELECT length(f1) FROM cmmove3;
  length 
 --------
   10000
-(1 row)
+  10040
+(2 rows)
 
 CREATE TABLE badcompresstbl (a text COMPRESSION I_Do_Not_Exist_Compression); -- fails
 ERROR:  invalid compression method "i_do_not_exist_compression"
diff --git a/src/test/regress/expected/compression_lz4_1.out b/src/test/regress/expected/compression_lz4_1.out
new file mode 100644
index 00000000000..199b4c4abd0
--- /dev/null
+++ b/src/test/regress/expected/compression_lz4_1.out
@@ -0,0 +1,5 @@
+SELECT NOT(pg_compression_available('lz4')) AS skip_test \gset
+\if :skip_test
+   \echo '*** skipping lz4 tests (lz4 not available) ***'
+*** skipping lz4 tests (lz4 not available) ***
+   \quit
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index a424be2a6bf..fbffc67ae60 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -123,7 +123,7 @@ test: plancache limit plpgsql copy2 temp domain rangefuncs prepare conversion tr
 # The stats test resets stats, so nothing else needing stats access can be in
 # this group.
 # ----------
-test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain compression memoize stats predicate numa
+test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain compression compression_lz4 memoize stats predicate numa
 
 # event_trigger depends on create_am and cannot run concurrently with
 # any test that runs DDL
diff --git a/src/test/regress/sql/compression.sql b/src/test/regress/sql/compression.sql
index 490595fcfb2..a21491456a7 100644
--- a/src/test/regress/sql/compression.sql
+++ b/src/test/regress/sql/compression.sql
@@ -1,3 +1,6 @@
+CREATE SCHEMA pglz;
+SET search_path TO pglz, public;
+
 \set HIDE_TOAST_COMPRESSION false
 
 -- ensure we get stable results regardless of installation's default
@@ -8,7 +11,7 @@ CREATE TABLE cmdata(f1 text COMPRESSION pglz);
 CREATE INDEX idx ON cmdata(f1);
 INSERT INTO cmdata VALUES(repeat('1234567890', 1000));
 \d+ cmdata
-CREATE TABLE cmdata1(f1 TEXT COMPRESSION lz4);
+CREATE TABLE cmdata1(f1 TEXT COMPRESSION pglz);
 INSERT INTO cmdata1 VALUES(repeat('1234567890', 1004));
 \d+ cmdata1
 
@@ -83,7 +86,7 @@ SELECT pg_column_compression(f1) FROM cmdata1;
 SELECT pg_column_compression(x) FROM compressmv;
 
 -- test compression with partition
-CREATE TABLE cmpart(f1 text COMPRESSION lz4) PARTITION BY HASH(f1);
+CREATE TABLE cmpart(f1 text COMPRESSION pglz) PARTITION BY HASH(f1);
 CREATE TABLE cmpart1 PARTITION OF cmpart FOR VALUES WITH (MODULUS 2, REMAINDER 0);
 CREATE TABLE cmpart2(f1 text COMPRESSION pglz);
 
@@ -94,19 +97,14 @@ SELECT pg_column_compression(f1) FROM cmpart1;
 SELECT pg_column_compression(f1) FROM cmpart2;
 
 -- test compression with inheritance
-CREATE TABLE cminh() INHERITS(cmdata, cmdata1); -- error
-CREATE TABLE cminh(f1 TEXT COMPRESSION lz4) INHERITS(cmdata); -- error
 CREATE TABLE cmdata3(f1 text);
 CREATE TABLE cminh() INHERITS (cmdata, cmdata3);
 
 -- test default_toast_compression GUC
-SET default_toast_compression = '';
-SET default_toast_compression = 'I do not exist compression';
-SET default_toast_compression = 'lz4';
 SET default_toast_compression = 'pglz';
 
 -- test alter compression method
-ALTER TABLE cmdata ALTER COLUMN f1 SET COMPRESSION lz4;
+ALTER TABLE cmdata ALTER COLUMN f1 SET COMPRESSION pglz;
 INSERT INTO cmdata VALUES (repeat('123456789', 4004));
 \d+ cmdata
 SELECT pg_column_compression(f1) FROM cmdata;
@@ -115,12 +113,12 @@ ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION default;
 \d+ cmdata2
 
 -- test alter compression method for materialized views
-ALTER MATERIALIZED VIEW compressmv ALTER COLUMN x SET COMPRESSION lz4;
+ALTER MATERIALIZED VIEW compressmv ALTER COLUMN x SET COMPRESSION pglz;
 \d+ compressmv
 
 -- test alter compression method for partitioned tables
 ALTER TABLE cmpart1 ALTER COLUMN f1 SET COMPRESSION pglz;
-ALTER TABLE cmpart2 ALTER COLUMN f1 SET COMPRESSION lz4;
+ALTER TABLE cmpart2 ALTER COLUMN f1 SET COMPRESSION pglz;
 
 -- new data should be compressed with the current compression method
 INSERT INTO cmpart VALUES (repeat('123456789', 1004));
@@ -135,7 +133,7 @@ SELECT pg_column_compression(f1) FROM cmdata;
 
 -- test expression index
 DROP TABLE cmdata2;
-CREATE TABLE cmdata2 (f1 TEXT COMPRESSION pglz, f2 TEXT COMPRESSION lz4);
+CREATE TABLE cmdata2 (f1 TEXT COMPRESSION pglz, f2 TEXT COMPRESSION pglz);
 CREATE UNIQUE INDEX idx1 ON cmdata2 ((f1 || f2));
 INSERT INTO cmdata2 VALUES((SELECT array_agg(fipshash(g::TEXT))::TEXT FROM
 generate_series(1, 50) g), VERSION());
diff --git a/src/test/regress/sql/compression_lz4.sql b/src/test/regress/sql/compression_lz4.sql
new file mode 100644
index 00000000000..c801adfa557
--- /dev/null
+++ b/src/test/regress/sql/compression_lz4.sql
@@ -0,0 +1,161 @@
+SELECT NOT(pg_compression_available('lz4')) AS skip_test \gset
+\if :skip_test
+   \echo '*** skipping lz4 tests (lz4 not available) ***'
+   \quit
+\endif
+
+CREATE SCHEMA lz4;
+SET search_path TO lz4, public;
+
+\set HIDE_TOAST_COMPRESSION false
+
+-- ensure we get stable results regardless of installation's default
+SET default_toast_compression = 'lz4';
+
+-- 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
+CREATE TABLE cmdata1(f1 TEXT COMPRESSION lz4);
+INSERT INTO cmdata1 VALUES(repeat('1234567890', 1004));
+\d+ cmdata1
+
+-- verify stored compression method in the data
+SELECT pg_column_compression(f1) FROM cmdata;
+SELECT pg_column_compression(f1) FROM cmdata1;
+
+-- decompress data slice
+SELECT SUBSTR(f1, 200, 5) FROM cmdata;
+SELECT SUBSTR(f1, 2000, 50) FROM cmdata1;
+
+-- copy with table creation
+SELECT * INTO cmmove1 FROM cmdata;
+\d+ cmmove1
+SELECT pg_column_compression(f1) FROM cmmove1;
+
+-- copy to existing table
+CREATE TABLE cmmove3(f1 text COMPRESSION pglz);
+INSERT INTO cmmove3 SELECT * FROM cmdata;
+INSERT INTO cmmove3 SELECT * FROM cmdata1;
+SELECT pg_column_compression(f1) FROM cmmove3;
+
+-- test LIKE INCLUDING COMPRESSION
+CREATE TABLE cmdata2 (LIKE cmdata1 INCLUDING COMPRESSION);
+\d+ cmdata2
+DROP TABLE cmdata2;
+
+-- try setting compression for incompressible data type
+CREATE TABLE cmdata2 (f1 int COMPRESSION pglz);
+
+-- update using datum from different table
+CREATE TABLE cmmove2(f1 text COMPRESSION pglz);
+INSERT INTO cmmove2 VALUES (repeat('1234567890', 1004));
+SELECT pg_column_compression(f1) FROM cmmove2;
+UPDATE cmmove2 SET f1 = cmdata1.f1 FROM cmdata1;
+SELECT pg_column_compression(f1) FROM cmmove2;
+
+-- 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;
+INSERT INTO cmdata1 SELECT large_val() || repeat('a', 4000);
+SELECT pg_column_compression(f1) FROM cmdata1;
+SELECT SUBSTR(f1, 200, 5) FROM cmdata1;
+SELECT SUBSTR(f1, 200, 5) FROM cmdata2;
+DROP TABLE cmdata2;
+
+--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 materialized view
+CREATE MATERIALIZED VIEW compressmv(x) AS SELECT * FROM cmdata1;
+\d+ compressmv
+SELECT pg_column_compression(f1) FROM cmdata1;
+SELECT pg_column_compression(x) FROM compressmv;
+
+-- test compression with partition
+CREATE TABLE cmpart(f1 text COMPRESSION lz4) PARTITION BY HASH(f1);
+CREATE TABLE cmpart1 PARTITION OF cmpart FOR VALUES WITH (MODULUS 2, REMAINDER 0);
+CREATE TABLE cmpart2(f1 text COMPRESSION pglz);
+
+ALTER TABLE cmpart ATTACH PARTITION cmpart2 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
+INSERT INTO cmpart VALUES (repeat('123456789', 1004));
+INSERT INTO cmpart VALUES (repeat('123456789', 4004));
+SELECT pg_column_compression(f1) FROM cmpart1;
+SELECT pg_column_compression(f1) FROM cmpart2;
+
+-- test compression with inheritance
+CREATE TABLE cminh() INHERITS(cmdata, cmdata1); -- error
+CREATE TABLE cminh(f1 TEXT COMPRESSION lz4) INHERITS(cmdata); -- error
+CREATE TABLE cmdata3(f1 text);
+CREATE TABLE cminh() INHERITS (cmdata, cmdata3);
+
+-- test default_toast_compression GUC
+SET default_toast_compression = 'lz4';
+
+-- test alter compression method
+ALTER TABLE cmdata ALTER COLUMN f1 SET COMPRESSION lz4;
+INSERT INTO cmdata VALUES (repeat('123456789', 4004));
+\d+ cmdata
+SELECT pg_column_compression(f1) FROM cmdata;
+
+ALTER TABLE cmdata2 ALTER COLUMN f1 SET COMPRESSION default;
+\d+ cmdata2
+
+-- test alter compression method for materialized views
+ALTER MATERIALIZED VIEW compressmv ALTER COLUMN x SET COMPRESSION lz4;
+\d+ compressmv
+
+-- test alter compression method for partitioned tables
+ALTER TABLE cmpart1 ALTER COLUMN f1 SET COMPRESSION pglz;
+ALTER TABLE cmpart2 ALTER COLUMN f1 SET COMPRESSION lz4;
+
+-- new data should be compressed with the current compression method
+INSERT INTO cmpart VALUES (repeat('123456789', 1004));
+INSERT INTO cmpart VALUES (repeat('123456789', 4004));
+SELECT pg_column_compression(f1) FROM cmpart1;
+SELECT pg_column_compression(f1) FROM cmpart2;
+
+-- VACUUM FULL does not recompress
+SELECT pg_column_compression(f1) FROM cmdata;
+VACUUM FULL cmdata;
+SELECT pg_column_compression(f1) FROM cmdata;
+
+-- test expression index
+DROP TABLE cmdata2;
+CREATE TABLE cmdata2 (f1 TEXT COMPRESSION pglz, f2 TEXT COMPRESSION lz4);
+CREATE UNIQUE INDEX idx1 ON cmdata2 ((f1 || f2));
+INSERT INTO cmdata2 VALUES((SELECT array_agg(fipshash(g::TEXT))::TEXT FROM
+generate_series(1, 50) g), VERSION());
+
+-- check data is ok
+SELECT length(f1) FROM cmdata;
+SELECT length(f1) FROM cmdata1;
+SELECT length(f1) FROM cmmove1;
+SELECT length(f1) FROM cmmove2;
+SELECT length(f1) FROM cmmove3;
+
+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

base-commit: b87163e5f3847730ee5f59718d215c6e63e13bff
-- 
2.47.1

