Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VhPf0-0004GY-Mz for pgsql-admin@arkaria.postgresql.org; Fri, 15 Nov 2013 20:06:34 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VhPf0-00029V-2q for pgsql-admin@arkaria.postgresql.org; Fri, 15 Nov 2013 20:06:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VhPey-00027I-6b for pgsql-admin@postgresql.org; Fri, 15 Nov 2013 20:06:32 +0000 Received: from nm41-vm3.bullet.mail.bf1.yahoo.com ([216.109.114.158]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VhPeu-000091-CI for pgsql-admin@postgresql.org; Fri, 15 Nov 2013 20:06:31 +0000 Received: from [66.196.81.172] by nm41.bullet.mail.bf1.yahoo.com with NNFMP; 15 Nov 2013 20:06:25 -0000 Received: from [98.139.212.234] by tm18.bullet.mail.bf1.yahoo.com with NNFMP; 15 Nov 2013 20:06:25 -0000 Received: from [127.0.0.1] by omp1043.mail.bf1.yahoo.com with NNFMP; 15 Nov 2013 20:06:25 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 870039.24513.bm@omp1043.mail.bf1.yahoo.com Received: (qmail 3379 invoked by uid 60001); 15 Nov 2013 20:06:25 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s1024; t=1384545985; bh=J8pRnXkZZNRitO8wqyU+BAzZjl22A6p06Gyndks8TUk=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=vq9jmR/ZaUFBpglJKJITDb9GdI//Ts0l2o4jvtsO7K3na0BzAUCC0J0zFIqFzuti19UKoW+4J+wkLeZTAZ/jnKhiIdCCH842mMtB+iMefTPNX8Qi2q7m3XqYrRdYdKpRaNs+wHQCnV0OJCNCB/7n745vKQChRnV/NzO7XHUcDnE= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=ymail.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=rkLaCRTN8z+KbbdC16U7k8GjOrupyuNGHSYDNDzPgNFF7l3P98Hxf5PwEc19FQQZEfvna17pSXUkY4aELgPI3viYWmcy45wkbl/NqDEUq/uAX7dBABULkKrKFtowyh0LwM+4zD1nH4TsEnVOV74XwVzP9xoUQGdvWbrSdGu7u3U=; X-YMail-OSG: yIdJqx0VM1lv3kxjN44szYgh9HxA9vMJDc5pjS9yxUJeLl. hmRZY52j1ATXo4gAL2KswurH0CgeST4R2qVtTnRJu9NG4TEF5ceTVQ1BxXhB c1A2.MS9jxO_f0lqw4iZzxOuOfNYoHlsmbRkLgR5wmSr3DprHndgnoCfT.G. d8y9ZF8c5etlVDRLjSA9daAeWnE5OjMX0ue98wk4xorrpQDIUjWn27xWIJnQ 0ucRcPcFurZEcjYVciVi91YivgnpNg6e73n5OTzCUai7tYSTcL1rbEDoBjw1 _aYHuNn5fvp0g5I1SEM9aMw5230tJxpLuD_xBjuSTm1KnlmxpJcw50ex_vsY _YFSBvbQU1ushoOu.8jGiKWNNwuhGDsXY2ZooXcl23ulLsmVgd5kxzTyDIS8 UeKP7Jm0BXjNWMVUx62Zvjtet8UBNPatzliSBjYpoHPUJzuh1j11jjG7h9aP 8dM_ssaslQfswyzaSsgedXgI1_gQ9pIo5ZKcvt0VpxDrzA10ruwTGVCi1krn .2fgOpE91d50KwAkxZkNw5o4toTXckwjl.tX7aiWyzs4OycADaRfFtk55rlD _9YcM7u9G9Zufhl1ZfCz3D.o9wkRL5vF_KqB_N1s4v.jnlueR3ACdQ0oIWRs LNQ-- Received: from [76.255.18.237] by web162906.mail.bf1.yahoo.com via HTTP; Fri, 15 Nov 2013 12:06:25 PST X-Rocket-MIMEInfo: 002.001, VGhhcmEgVmFkYWtrZXZlZWR1IDx0aGFyYWd2QHlhaG9vLmNvbT4gd3JvdGU6Cgo.IEkgdHJpZWQgdGhpcyBmcm9tIHBnX2FkbWluLCBidXQgSSBnZXQgYSBzeW50YXggZXJyb3IgKHVuZXhwZWN0ZWQKPiBjaGFyYWN0ZXIpCgo.IAo.IElGIEVYSVNUUyAoU0VMRUNUIDAgRlJPTSBwZ19jbGFzcwo.wqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgV0hFUkUgcmVsa2luZCA9ICdTJwo.wqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgIEFORCBvaWQ6OnJlZ2NsYXNzOjp0ZXh0ID0gJ3B1YmxpYy4nIHx8IHF1b3QBMAEBAQE- X-Mailer: YahooMailWebService/0.8.163.597 References: <1384543840.72953.YahooMailNeo@web125002.mail.ne1.yahoo.com> <1384544163.79352.YahooMailNeo@web125002.mail.ne1.yahoo.com> Message-ID: <1384545985.96990.YahooMailNeo@web162906.mail.bf1.yahoo.com> Date: Fri, 15 Nov 2013 12:06:25 -0800 (PST) From: Kevin Grittner Reply-To: Kevin Grittner Subject: Re: checking if sequence exists To: Thara Vadakkeveedu , "pgsql-admin@postgresql.org" In-Reply-To: <1384544163.79352.YahooMailNeo@web125002.mail.ne1.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.0 (--) 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 Thara Vadakkeveedu wrote: > I tried this from pg_admin, but I get a syntax error (unexpected > character) >=20 > IF EXISTS (SELECT 0 FROM pg_class >=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 WHERE relkind =3D 'S' >=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 AND oid::regclass::text =3D = 'public.' || quote_ident('hibernate_sequence')) >=A0=A0 THEN >=A0=A0=A0=A0 RAISE EXCEPTION 'sequence public.% already exists!', 'hiberna= te_sequence > ENF IF; First, is this code in a plpgsql context (like a function definition or a DO command)?=A0 If not, that IF is not going to work. =A0If it *is*, you seem to have a typo where you meant END IF. Also note that if you force the oid to text for the comparison, it is both more fragile and slower than if you convert the text to regclass for the comparison. explain analyze SELECT 0 FROM pg_class =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 WHERE relkind =3D 'S' =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 AND oid::regclass::text =3D 'pub= lic.' || quote_ident('hibernate_sequence'); =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 QUERY PLAN=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =A0 ---------------------------------------------------------------------------= ---------------------------- =A0Seq Scan on pg_class=A0 (cost=3D0.00..13.86 rows=3D1 width=3D0) (actual = time=3D0.197..0.197 rows=3D0 loops=3D1) =A0=A0 Filter: ((relkind =3D 'S'::"char") AND (((oid)::regclass)::text =3D = 'public.hibernate_sequence'::text)) =A0=A0 Rows Removed by Filter: 295 =A0Total runtime: 0.221 ms (4 rows) explain analyze SELECT 0 FROM pg_class =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 WHERE relkind =3D 'S' =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 AND oid =3D ('public.' || quote_= ident('hibernate_sequence'))::regclass; =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 QUERY PLAN=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =A0 ---------------------------------------------------------------------------= -------------------------------------------------- =A0Index Scan using pg_class_oid_index on pg_class=A0 (cost=3D0.28..8.29 ro= ws=3D1 width=3D0) (actual time=3D0.031..0.032 rows=3D1 loops=3D1) =A0=A0 Index Cond: (oid =3D (('public.hibernate_sequence'::text)::regclass)= ::oid) =A0=A0 Filter: (relkind =3D 'S'::"char") =A0Total runtime: 0.062 ms (4 rows) Notice that there was a table scan, which was slower, and the record was not found because the cast of the oid to regclass and then text did not include the 'public." part.=A0 The other way used an index and matched as you probably intended. -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --=20 Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin