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 1hGstq-0007ca-Um for pgsql-general@arkaria.postgresql.org; Wed, 17 Apr 2019 22:19:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGstp-0006X4-LR for pgsql-general@arkaria.postgresql.org; Wed, 17 Apr 2019 22:19:25 +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 1hGstn-0006Wx-Of for pgsql-general@lists.postgresql.org; Wed, 17 Apr 2019 22:19:25 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGstj-0000XM-I4 for pgsql-general@lists.postgresql.org; Wed, 17 Apr 2019 22:19:23 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id BEFF421385; Wed, 17 Apr 2019 18:19:17 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 17 Apr 2019 18:19:17 -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=fm2; bh=m gxYItOAZinE/3bbGn15FxFMeV9GvLCQHrNxiAfjinY=; b=N6tRFW7CiWS+fyn/t G23Ae06MNHZruZHXoAU8qZxuv6Zg5ByUUV8IUg6jX2es2j7O2hc0l24Je6bUVP/j tFVQpGqY2NqjmYcZEkQiM+2ba9FlR4XRtmOlg7smtThLauZa42/4Zipu0tdmey3z OiYSpy2kywKui0FkebxUd9cEtprpOVKAWCuN8Lj8gHDfZbrUHETpIeoxm7KtCZKG Y0DG6NOZBlIGDuJjPEX/DVlfN5sL1FzfIy7tUv52arI8m2dsTeAe21Oyb51L2W2L P9p8PydTmbLAQO/XCxHO8E/C4NwBJvEa84PcAgJyO8ty3NeyLKyffsycbvvlmxPN ziHTQ== 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=mgxYItOAZinE/3bbGn15FxFMeV9GvLCQHrNxiAfji nY=; b=77YacJ/vp4VtSTi7Fc9+lvja1nfH36+uR44X3mvLt7Tu6N4+J6vZ7Lq8y AihaKk89EseMv+nIpA7gy9mKEtx/uxiZrbRxohhSScyjjkRpXpJtQost/ZW+FBhm yAZfaYbQ4BUQ06gksIwKttvIKpG/ikb1RAyQ6m+3hndsZuaIsm7nh9F7x7jt3YvC c/KdTIhuT3swibugRFgPGd5UbMVn1WYaMSc/4JuIhnQsSBkXmfWhKm0+Vi+/6pST B2GPPQhHpO6pbE98IreAdWzIKDxz+tWS/RhvzRaV4ZtQ4DlMZen429h6IGSPK6x/ Fr/3gVkpa1XXC+RKUp9L76TMMlzhA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrfeeggddthecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphephedtrdduleejrdektddrvdenuc frrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgv rhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id EE79B103C9; Wed, 17 Apr 2019 18:19:16 -0400 (EDT) Subject: Re: is it possible to create partitioned tables using tables from different schemas To: pabloa98 , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Wed, 17 Apr 2019 15:19:15 -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: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/17/19 2:21 PM, pabloa98 wrote: > I have a schema with a generated table with information coming from > batch processes. > > I would like to store in that table manually generated information. > Since those rows are inserted by hand, they will be lost when the table > will be reimported. > > So I was thinking of creating a partitioned table with a column "origin" > to select if the data is batch inserted or inserted by hand. Current > generated by batch sub-table will live in its schema. > > I want the other sub-table containing manually inserted information > living in another schema. > > Is this possible? Do I have to do something else (create some trigger, > or something)? > > Something like: > > CREATE TABLE*automatic.*measurement ( > city_id int not null, > logdate date not null, > peaktemp int, > unitsales int > origin int > ) PARTITION BY RANGE (origin); > > > CREATE TABLE*automatic.*measurement_automatic PARTITION OF*automatic.*measurement > FOR VALUES FROM (1) TO (1) > PARTITION BY RANGE (origin); > > CREATE TABLE*manual.*measurement_manual PARTITION OF*automatic.*measurement > FOR VALUES FROM (2) TO (2) > PARTITION BY RANGE (origin); > > It would seem so(with a caveat): CREATE TABLE automatic.measurement ( city_id int not null, logdate date not null, peaktemp int, unitsales int, origin int ) PARTITION BY RANGE (origin); CREATE TABLE CREATE TABLE automatic.measurement_automatic PARTITION OF automatic.measurement test-# FOR VALUES FROM (1) TO (1) test-# PARTITION BY RANGE (origin); ERROR: empty range bound specified for partition "measurement_automatic" DETAIL: Specified lower bound (1) is greater than or equal to upper bound (1). OOPS, so lets cheat: Removed PARTITION BY RANGE (origin) as it is redundant and causes problems later. CREATE TABLE automatic.measurement_automatic PARTITION OF automatic.measurement FOR VALUES FROM (1) TO (2) CREATE TABLE CREATE TABLE manual.measurement_manual PARTITION OF automatic.measurement FOR VALUES FROM (3) TO (4) CREATE TABLE The above assumes that manual origin will be in (1,2) and automatic in (3,4) insert into automatic.measurement values(1, '04/16/19', 25, 100, 1); INSERT 0 1 insert into automatic.measurement values(1, '04/16/19', 25, 100, 3); INSERT 0 1 select * from automatic.measurement_automatic ; city_id | logdate | peaktemp | unitsales | origin ---------+------------+----------+-----------+-------- 1 | 2019-04-16 | 25 | 100 | 1 (1 row) select * from manual.measurement_manual; city_id | logdate | peaktemp | unitsales | origin ---------+------------+----------+-----------+-------- 1 | 2019-04-16 | 25 | 100 | 3 (1 row) -- Adrian Klaver adrian.klaver@aklaver.com