Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hUFt0-0004tv-43 for pgsql-general@arkaria.postgresql.org; Fri, 24 May 2019 19:29:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hUFsy-0005da-Ji for pgsql-general@arkaria.postgresql.org; Fri, 24 May 2019 19:29:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hUFsw-0005dT-Qn for pgsql-general@lists.postgresql.org; Fri, 24 May 2019 19:29:48 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hUFsr-0001W7-T3 for pgsql-general@lists.postgresql.org; Fri, 24 May 2019 19:29:46 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 465FC694; Fri, 24 May 2019 15:29:38 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 24 May 2019 15:29:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=S bhNp/PWSUW7nozvo6F9tRnWjCKtR9b/k94yvFyPvLg=; b=ALjdsAUc9GGaIeA/K UY7/GVVJWKeA5eIYRR0ax+C1B8FiYdHk+cW6LE8ATrtUE3GmKAePsXSFOUrJh2Ux WDKknxSe7kwaT0QA6MEqBymnBTOCNY1XSq6R+Y5OVdmpJxiBEMJYPJ39jqWx8+f7 JD6TAwE/yrfIDJCnvoyVsjx/vNX4Z9YCVwCaVLLU2q7MFtQ7R+pycU+y8s+KUm4R 7R9mFA9qSsQFf2VEj3Oxkyh5UQ4pBUpJ72W/8BRuL8HaSptCFH2W2vIAp9QIbsoe ZsZ3Kzf341CCuKJEreR8oHEMdx8TKtW1uDIV8pJe+3BD8B5VWeFWyAYka0z5EtPH N0XOg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=SbhNp/PWSUW7nozvo6F9tRnWjCKtR9b/k94yvFyPv Lg=; b=eBg+RGYthPwc5MDvhkwU89MAP8e8K+1hWnwWibw/1qSUBgb7ck7Bqzz1L 3d08OUeDexLixW+5u6iMKrUnZmV/1oJS31CTENdR0G+rGKPd8abtr9rsF6F17ZBA zD562iPbFCOOpYy965GRO2iCYfGa+/jeOf3jxoSV4//cD9vBaXLslW6Gsus6Hk7r 9x7OvrRbEg3A191r8WFvexggaf+2lQ3n7IBadhPdtxhf5VsnliXms68FKdhVqqrZ /edHkIBBt5WmQ6r7p7Rvx+fnAuqdMcthKL8bfVyO5LqOlg4RfmTB+fapMWUPjOZt P9Fg84sbbD+LHYd5RGe2v0KZ39GCQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrudduiedgudegtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhkfihilhhlih hsrdgtohhmpdhsqhhithgthhdrohhrghdpphhoshhtghhrvghsqhhlrdhorhhgnecukfhp peejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurh hirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigv pedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 140D9380087; Fri, 24 May 2019 15:29:36 -0400 (EDT) Subject: Re: Snippets? To: Ron , pgsql-general@lists.postgresql.org References: <665912f2-754c-f95c-62b2-37d5dc3a1b8b@gmail.com> From: Adrian Klaver Message-ID: <074b89cd-8458-ef88-397a-f088659c2ba8@aklaver.com> Date: Fri, 24 May 2019 12:29:36 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <665912f2-754c-f95c-62b2-37d5dc3a1b8b@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/24/19 12:24 PM, Ron wrote: > On 5/24/19 1:27 PM, Adrian Klaver wrote: >> On 5/24/19 11:15 AM, Ken Lacrosse wrote: >>> Is there any way in postgresql to have a "snippet" of SQL code which >>> you could apply to all tables. Something you could add which would >>> ensure that every table always has a Created, Changed and Deleted >>> column for example. Sort of like a C include I suppose.  Of course if >>> I’m building a DB schema by hand I could just copy and paste those >>> columns but it still seems a bit too, darn humans!, error prone. >> >> Not that I know of. >> >> Things I have done: >> 1) Template table that I includes those fields that I then add to. >> >> FYI the Sqitch schema management system allows you to do the above: >> https://sqitch.org/docs/manual/sqitch-add/#templates >> >> 2) A script that I run over table definition to add the fields. > > What about INHERITS? It is an option, though it does have baggage: https://www.postgresql.org/docs/11/sql-createtable.html "INHERITS ( parent_table [, ... ] ) The optional INHERITS clause specifies a list of tables from which the new table automatically inherits all columns. Parent tables can be plain tables or foreign tables. Use of INHERITS creates a persistent relationship between the new child table and its parent table(s). Schema modifications to the parent(s) normally propagate to children as well, and by default the data of the child table is included in scans of the parent(s). ...""" There is more. If I was to go that route I would use LIKE: "LIKE source_table [ like_option ... ] The LIKE clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints. Unlike INHERITS, the new table and original table are completely decoupled after creation is complete. Changes to the original table will not be applied to the new table, and it is not possible to include data of the new table in scans of the original table. ..." > > test=# create table CCD_template ( > test(#  Created timestamptz, > test(#  Changed timestamptz, > test(#  Deleted timestamptz ); > CREATE TABLE > > test=# create table foobar ( >   field1 integer, >   field2 bytea > ) inherits (CCD_template); > CREATE TABLE > test=# > test=# \d foobar >              Table "public.foobar" >  Column  |           Type           | Modifiers > ---------+--------------------------+----------- >  created | timestamp with time zone | >  changed | timestamp with time zone | >  deleted | timestamp with time zone | >  field1  | integer                  | >  field2  | bytea                    | > Inherits: ccd_template > > >> >>> >>> Ken LaCrosse >>> >>> Senior IT InfoSec and Infrastructure Monitoring Specialist >>> >>> I.T. | American Recovery Service & Skipbusters >>> >>> Phone: (800)398-6480 x3758 >>> >>> Email: KLacrosse@pkwillis.com >>> >>> www.pkwillis.com >>> >>> * Visibility is key. * >>> >>> Without visibility you can't see. >>> >>> What you can't see you can't measure. >>> >>> What you can't measure you can’t planfully change. >>> >>> What you can’t planfully change you can't manage. >>> >>> And if we can’t manage then why are we here? >>> >>> NOTICE: The information contained in this transmission, including >>> attachments, may contain confidential information that is privileged, >>> confidential and/or exempt from disclosure by applicable law. It is >>> intended only for the use of the person(s) or entity to which it is >>> addressed. If the reader of this transmission is not the intended >>> recipient, the reader is hereby notified that any review, use, >>> dissemination, distribution or duplication of this communication >>> (including any reliance thereon) is strictly prohibited. If you have >>> received this transmission in error, please contact the sender by >>> reply email, then delete and destroy the material in its entirety, >>> whether in electronic or hard copy format. WE SPECIFICALLY DISCLAIM >>> RESPONSIBILITY FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY >>> ATTACHMENTS TO IT. Thank you. >> >> > -- Adrian Klaver adrian.klaver@aklaver.com