Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBrMm-0001Nd-Bk for pgsql-admin@arkaria.postgresql.org; Sat, 02 Mar 2013 18:41:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UBrMl-00067m-O2 for pgsql-admin@arkaria.postgresql.org; Sat, 02 Mar 2013 18:41:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBrMk-00067g-QG for pgsql-admin@postgresql.org; Sat, 02 Mar 2013 18:41:02 +0000 Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBrMg-0000Kd-RD for pgsql-admin@postgresql.org; Sat, 02 Mar 2013 18:41:02 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UBrMz-0004lL-GN for pgsql-admin@postgresql.org; Sat, 02 Mar 2013 19:41:17 +0100 Received: from 217.31.70.82 ([217.31.70.82]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 02 Mar 2013 19:41:17 +0100 Received: from chris by 217.31.70.82 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 02 Mar 2013 19:41:17 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-admin@postgresql.org From: Christian Ullrich Subject: Re: CREATE TABLE LIKE and tablespaces Date: Sat, 02 Mar 2013 19:40:42 +0100 Lines: 56 Message-ID: References: <20130227132935.44fc25f1@hydrogen.roaringpenguin.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 217.31.70.82 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <20130227132935.44fc25f1@hydrogen.roaringpenguin.com> X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org * David F. Skoll wrote: > I have an application that creates a daily table from a "prototype" > table, so something like: > > CREATE TABLE data_20130226 LIKE data_prototype INCLUDING DEFAULTS INCLUDING CONSTRATINTS INCLUDING INDEXES; > > It would be really nice to be able to add: > > INCLUDING TABLESPACE Workaround: CREATE OR REPLACE FUNCTION table_tablespace(p_relname regclass) RETURNS name LANGUAGE sql AS $$ SELECT t.spcname FROM pg_class c JOIN pg_tablespace t ON (c.oid = p_relname AND COALESCE( NULLIF(c.reltablespace, 0), (SELECT dattablespace FROM pg_database WHERE datname = current_database())) = t.oid); $$; -- It is surprisingly difficult to get the tablespace of a table. -- (Note to self: So *that* is what NULLIF() is for!) CREATE OR REPLACE FUNCTION create_table_like(p_old regclass, p_new name) RETURNS regclass LANGUAGE plpgsql AS $$ BEGIN EXECUTE 'CREATE TABLE ' || quote_ident(p_new) || ' (LIKE ' || quote_ident(p_old::name) || ' INCLUDING DEFAULTS ' || ' INCLUDING CONSTRAINTS ' || ' INCLUDING INDEXES) ' || ' TABLESPACE '|| quote_ident(table_tablespace(p_old)) || ';'; RETURN p_new::regclass; END; $$; test=> SELECT create_table_like('data_prototype', 'data_20130226'); create_table_like ------------------- data_20130226 -- Christian -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin