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 1jiCbi-0007bT-0B for pgsql-general@arkaria.postgresql.org; Mon, 08 Jun 2020 07:54:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jiCbe-0008Ts-N1 for pgsql-general@arkaria.postgresql.org; Mon, 08 Jun 2020 07:54:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jiCbe-0008Pd-Dk for pgsql-general@lists.postgresql.org; Mon, 08 Jun 2020 07:54:06 +0000 Received: from smh-06.1blu.de ([178.254.0.206]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jiCbZ-00070q-T1 for pgsql-general@lists.postgresql.org; Mon, 08 Jun 2020 07:54:04 +0000 Received: from [172.16.29.5] (helo=sh4-5.1blu.de) by smh-06.1blu.de with esmtp (Exim 4.86_2) (envelope-from ) id 1jiCbW-0006wn-LY for pgsql-general@lists.postgresql.org; Mon, 08 Jun 2020 09:53:58 +0200 Received: from ftp51246-2575596 by sh4-5.1blu.de with local (Exim 4.90_1) (envelope-from ) id 1jiCbW-0006MN-Jc for pgsql-general@lists.postgresql.org; Mon, 08 Jun 2020 09:53:58 +0200 Date: Mon, 8 Jun 2020 09:53:58 +0200 From: Matthias Apitz To: pgsql-general@lists.postgresql.org Subject: checking existence of a table before updating its SERIAL Message-ID: <20200608075358.GA17471@sh4-5.1blu.de> Reply-To: Matthias Apitz MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Operating-System: FreeBSD 12.0-CURRENT r314251 (amd64) X-message-flag: Mails containing HTML will not be read! Please send only plain text. User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, We're updating the SERIAL of a bunch of tables with a SQL script which does for any table: /* table: idm_tasktab */ DO $$ DECLARE max_id int; BEGIN SELECT INTO max_id GREATEST(COALESCE(max(taskid), 0),0) + 1 FROM idm_tasktab; RAISE NOTICE '% % %', 'idm_tasktab', 'taskid', max_id ; EXECUTE 'ALTER SEQUENCE idm_tasktab_taskid_seq RESTART ' || max_id::text; END $$ LANGUAGE plpgsql; Can some kind soul help me with doing a test for the existence of the table to avoid the error message about non existing relation? Thanks in advance. matthias -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub