Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dFPo5-00086L-Cr for pgsql-bugs@arkaria.postgresql.org; Mon, 29 May 2017 18:54:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dFPo4-00008D-Vj for pgsql-bugs@arkaria.postgresql.org; Mon, 29 May 2017 18:54:21 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dFPnM-00065a-5B for pgsql-bugs@postgreSQL.org; Mon, 29 May 2017 18:53:36 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dFPnI-0004Ou-7B for pgsql-bugs@postgreSQL.org; Mon, 29 May 2017 18:53:35 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v4TIrTQ0027472; Mon, 29 May 2017 14:53:30 -0400 From: Tom Lane To: Flo cc: pgsql-bugs@postgreSQL.org Subject: Re: Can't restore view with pg_restore In-reply-to: References: <2329.1495640378@sss.pgh.pa.us> Comments: In-reply-to Flo message dated "Mon, 29 May 2017 12:13:22 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27470.1496084009.1@sss.pgh.pa.us> Date: Mon, 29 May 2017 14:53:29 -0400 Message-ID: <27471.1496084009@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org [ please keep the list cc'd for the archives' sake ] Flo writes: > Very interesting point. I have done a pg_dump -s and I've found in the text > file that all the views are dumped with a CREATE VIEW command, but not that > one. This doesn't seem like it could be all of the commands affecting bmv_visits_list, because nothing you've shown here could result in creation of a trigger on bmv_visits_list before the rule is created. > CREATE RULE "_RETURN" AS > ON SELECT TO bmv_visits_list DO INSTEAD SELECT patient.firstname, > patient.lastname, > patient.birthdate, > company.code, > jsonb_agg(jsonb_build_object('nb', v.nb, 'admissiondt', v.admitdt, > 'patientroom', v.patientroom)) AS visits > ... > GROUP BY patient.id, company.code; > Does it have anything to do with the jsonb data type, because it is the > only view that uses that ? No. I think the reason the view is getting dumped like this is the incomplete GROUP BY spec. To make that SELECT legal, patient.id must be a primary key (or at least unique), which means that the pkey or unique index must be created before we can issue the command. pg_dump doesn't want to build indexes until after the data is loaded, so it has to split creation of this view into creation of a shell view (before data load) and replacement of the shell with the real view definition (after data load and index creation). It might help to think of the CREATE TABLE as being CREATE VIEW with a dummy view definition and then the CREATE RULE as being like CREATE OR REPLACE VIEW with the real definition. The fact that these commands are spelled the way they are is a historical accident, which we might or might not get around to fixing someday. This gets us no closer to understanding your failure though. Please take a closer look for other commands mentioning bmv_visits_list before the CREATE RULE. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs