Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOV54-0004zJ-IC for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Mar 2021 00:39:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lOV53-0008Ub-Gc for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Mar 2021 00:39:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOV53-0008SW-4O for pgsql-hackers@lists.postgresql.org; Tue, 23 Mar 2021 00:39:33 +0000 Received: from mail-io1-xd36.google.com ([2607:f8b0:4864:20::d36]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lOV50-0006s2-AB for pgsql-hackers@postgresql.org; Tue, 23 Mar 2021 00:39:32 +0000 Received: by mail-io1-xd36.google.com with SMTP id r193so15912541ior.9 for ; Mon, 22 Mar 2021 17:39:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=nYWAlhf7ry/DM+k0imS8sNod8U24vifXlo8w03fXKXY=; b=aJq3F8td6cLOc9ICMOEtQ7Kpwz5jlNtMiGFtfC92c8b0r4oP1waQOHSVRmtlfdaUos AFt0pbsf/BXHdGr5hlB4jV+PhfTjVXtuCwg5ZCIGYklJBMtabOuW7pvvBVcW3IxGCtu8 vTS+t476crGx6uAVWvsgL1zherQCzjXSyig/D6lpkHvksRK8TtHREAjDwsWI9vKuwUuY gZU4A4pq++f4Iweq0+la7f/P/MANUZvSj3OoMMzH0hu2En7XBI9xfAB87oBeFZ2kUpFo oSuq8Ibe2+rZXNLNmlxZJb4Txl0LDxdtDTW6PAl7Zas5rN6Oe7/JL2BXX6e0ajvGPVUM ZKFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=nYWAlhf7ry/DM+k0imS8sNod8U24vifXlo8w03fXKXY=; b=GkHHejFDIqpufPX3KIQTySyWUK0D8GCE8GRyk5UN37K2LsKoeR6VvjNPlccoxtAssU 6Aqv89TTkaSJkazFm2bX+m6IYRWaGIvEwyBJ+TVWgtw/3HgTMVY6p58cTHu167qqmGWD TU0/QmNngxfdxJaCaZs+ZeFmn3q75uDv4TuL7CyY5DpGsUg+ze9aMX/pUnlSNcXHf2/b LMBY+OJhSDX+UxSNO7t7Mw9sX0D/FhRLcUR/689RMOBp5os142EKXhTisjQeRADbt9B8 CTIJu1k5LCZ67uLEzKyOPkD/CmvYf2kw2MOen8oKPOYkn6TAvgfRhHUc6oVge8RsvvjC MfHQ== X-Gm-Message-State: AOAM531Ps7qm4XBDEMtGOy8/Qljw2+jfjPxykcD6k41Mu5dCCSH1BFZI ASX1YZQEHk63dVSGK1LK0SfB+w== X-Google-Smtp-Source: ABdhPJwJ2tiiPd0dQ4i5htPXQ5a9dVVzFJOlMPeq+EL/Kd8Umh+xlIhC34FFg7ObGYI4sxntvf02YA== X-Received: by 2002:a6b:7010:: with SMTP id l16mr2009220ioc.96.1616459968328; Mon, 22 Mar 2021 17:39:28 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id x17sm8803236ilm.40.2021.03.22.17.39.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Mar 2021 17:39:27 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 4FB15800941; Mon, 22 Mar 2021 19:39:26 -0500 (CDT) Date: Mon, 22 Mar 2021 19:39:26 -0500 From: Justin Pryzby To: Simon Riggs Cc: Michael Paquier , pgsql-hackers@postgresql.org, Jacob Champion , Heikki Linnakangas , Ashwin Agrawal , Ashutosh Sharma , Andres Freund , Robert Haas , Amit Kapila , Alvaro Herrera Subject: Re: create table like: ACCESS METHOD Message-ID: <20210323003926.GV4203@telsasoft.com> References: <20201209201328.GT24052@telsasoft.com> <20201229230805.GE25152@telsasoft.com> <20210119210331.GN8560@telsasoft.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="UnaWdueM1EBWVRzC" Content-Disposition: inline In-Reply-To: <20210119210331.GN8560@telsasoft.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --UnaWdueM1EBWVRzC Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Jan 19, 2021 at 03:03:31PM -0600, Justin Pryzby wrote: > On Wed, Dec 30, 2020 at 12:33:56PM +0000, Simon Riggs wrote: > > There are no tests for the new functionality, please could you add some? > > Did you look at the most recent patch? > > +CREATE ACCESS METHOD heapdup TYPE TABLE HANDLER heap_tableam_handler; > +CREATE TABLE likeam() USING heapdup; > +CREATE TABLE likeamlike(LIKE likeam INCLUDING ALL); > > Also, I just realized that Dilip's toast compression patch adds "INCLUDING > COMPRESSION", which is stored in pg_am. That's an implementation detail of > that patch, but it's not intuitive that "including access method" wouldn't > include the compression stored there. So I think this should use "INCLUDING > TABLE ACCESS METHOD" not just ACCESS METHOD. Since the TOAST patch ended up not using access methods after all, I renamed this back to "like ACCESS METHOD" (without table). For now, I left TableLikeOption un-alphabetized. -- Justin --UnaWdueM1EBWVRzC Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-create-table-like-.-including-ACCESS-METHOD.patch" From 2da8104a39f65f576d0f221c288502d27211a209 Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Sun, 15 Nov 2020 16:54:53 -0600 Subject: [PATCH v4] create table (like .. including ACCESS METHOD) --- doc/src/sgml/ref/create_table.sgml | 12 +++++++++++- src/backend/parser/gram.y | 1 + src/backend/parser/parse_utilcmd.c | 10 ++++++++++ src/include/nodes/parsenodes.h | 1 + src/test/regress/expected/create_table_like.out | 12 ++++++++++++ src/test/regress/sql/create_table_like.sql | 8 ++++++++ 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index c6c248f1e9..dd92fd0e9a 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -87,7 +87,7 @@ class="parameter">referential_action ] [ ON UPDATE and like_option is: -{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL } +{ INCLUDING | EXCLUDING } { ACCESS METHOD | COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL } and partition_bound_spec is: @@ -613,6 +613,16 @@ WITH ( MODULUS numeric_literal, REM available options are: + + INCLUDING ACCESS METHOD + + + The table's access method will be copied. By default, the + default_table_access_method is used. + + + + INCLUDING COMMENTS diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index bc43641ffe..383e0671af 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -3741,6 +3741,7 @@ TableLikeOption: | STATISTICS { $$ = CREATE_TABLE_LIKE_STATISTICS; } | STORAGE { $$ = CREATE_TABLE_LIKE_STORAGE; } | COMPRESSION { $$ = CREATE_TABLE_LIKE_COMPRESSION; } + | ACCESS METHOD { $$ = CREATE_TABLE_LIKE_ACCESS_METHOD; } | ALL { $$ = CREATE_TABLE_LIKE_ALL; } ; diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index aa6c19adad..07e18fa62f 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -97,6 +97,7 @@ typedef struct bool ispartitioned; /* true if table is partitioned */ PartitionBoundSpec *partbound; /* transformed FOR VALUES */ bool ofType; /* true if statement contains OF typename */ + char *accessMethod; /* table access method */ } CreateStmtContext; /* State shared by transformCreateSchemaStmt and its subroutines */ @@ -253,6 +254,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) cxt.ispartitioned = stmt->partspec != NULL; cxt.partbound = stmt->partbound; cxt.ofType = (stmt->ofTypename != NULL); + cxt.accessMethod = NULL; Assert(!stmt->ofTypename || !stmt->inhRelations); /* grammar enforces */ @@ -347,6 +349,9 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) stmt->tableElts = cxt.columns; stmt->constraints = cxt.ckconstraints; + if (cxt.accessMethod != NULL) + stmt->accessMethod = cxt.accessMethod; + result = lappend(cxt.blist, stmt); result = list_concat(result, cxt.alist); result = list_concat(result, save_alist); @@ -1137,6 +1142,11 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla cxt->likeclauses = lappend(cxt->likeclauses, table_like_clause); } + /* ACCESS METHOD doesn't apply and isn't copied for partitioned tables */ + if ((table_like_clause->options & CREATE_TABLE_LIKE_ACCESS_METHOD) != 0 && + !cxt->ispartitioned) + cxt->accessMethod = get_am_name(relation->rd_rel->relam); + /* * We may copy extended statistics if requested, since the representation * of CreateStatsStmt doesn't depend on column numbers. diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 68425eb2c0..5e2fc9baca 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -696,6 +696,7 @@ typedef enum TableLikeOption CREATE_TABLE_LIKE_STATISTICS = 1 << 6, CREATE_TABLE_LIKE_STORAGE = 1 << 7, CREATE_TABLE_LIKE_COMPRESSION = 1 << 8, + CREATE_TABLE_LIKE_ACCESS_METHOD = 1 << 9, CREATE_TABLE_LIKE_ALL = PG_INT32_MAX } TableLikeOption; diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out index 10d17be23c..a09cd48ed0 100644 --- a/src/test/regress/expected/create_table_like.out +++ b/src/test/regress/expected/create_table_like.out @@ -429,6 +429,18 @@ SELECT s.stxname, objsubid, description FROM pg_description, pg_statistic_ext s ctlt_all_a_b_stat | 0 | ab stats (1 row) +CREATE ACCESS METHOD heapdup TYPE TABLE HANDLER heap_tableam_handler; +CREATE TABLE likeam() USING heapdup; +CREATE TABLE likeamlike(LIKE likeam INCLUDING ALL); +-- pg_regress helpfully hides the Access Method output, which we need: +SELECT a.amname FROM pg_class c JOIN pg_am a ON c.relam=a.oid WHERE c.oid='likeamlike'::regclass; + amname +--------- + heapdup +(1 row) + +DROP TABLE likeam, likeamlike; +DROP ACCESS METHOD heapdup; CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4); NOTICE: merging multiple inherited definitions of column "a" ERROR: inherited column "a" has a storage parameter conflict diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql index 06b76f949d..ee6c464851 100644 --- a/src/test/regress/sql/create_table_like.sql +++ b/src/test/regress/sql/create_table_like.sql @@ -165,6 +165,14 @@ CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL); SELECT c.relname, objsubid, description FROM pg_description, pg_index i, pg_class c WHERE classoid = 'pg_class'::regclass AND objoid = i.indexrelid AND c.oid = i.indexrelid AND i.indrelid = 'ctlt_all'::regclass ORDER BY c.relname, objsubid; SELECT s.stxname, objsubid, description FROM pg_description, pg_statistic_ext s WHERE classoid = 'pg_statistic_ext'::regclass AND objoid = s.oid AND s.stxrelid = 'ctlt_all'::regclass ORDER BY s.stxname, objsubid; +CREATE ACCESS METHOD heapdup TYPE TABLE HANDLER heap_tableam_handler; +CREATE TABLE likeam() USING heapdup; +CREATE TABLE likeamlike(LIKE likeam INCLUDING ALL); +-- pg_regress helpfully hides the Access Method output, which we need: +SELECT a.amname FROM pg_class c JOIN pg_am a ON c.relam=a.oid WHERE c.oid='likeamlike'::regclass; +DROP TABLE likeam, likeamlike; +DROP ACCESS METHOD heapdup; + CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4); CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1); -- 2.17.0 --UnaWdueM1EBWVRzC--