Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so2uG-007sqp-Vg for pgsql-general@arkaria.postgresql.org; Tue, 10 Sep 2024 15:35:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1so2uG-00F9Ov-LL for pgsql-general@arkaria.postgresql.org; Tue, 10 Sep 2024 15:35:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so2uG-00F9On-AD for pgsql-general@lists.postgresql.org; Tue, 10 Sep 2024 15:35:52 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so2uC-000UVb-Qa for pgsql-general@postgresql.org; Tue, 10 Sep 2024 15:35:51 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 4390D2A14D6 for ; Tue, 10 Sep 2024 08:35:48 -0700 (PDT) Date: Tue, 10 Sep 2024 08:35:48 -0700 (PDT) From: Rich Shepard To: pgsql-general@postgresql.org Subject: Re: Removing duplicate rows in table In-Reply-To: <5872cd30-9a2b-4b4c-82c4-48228197e872@a-kretschmer.de> Message-ID: <265771f-f2c8-3c98-aebf-6239da97f3b2@appl-ecosys.com> References: <5872cd30-9a2b-4b4c-82c4-48228197e872@a-kretschmer.de> MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=ISO-8859-15 Content-Transfer-Encoding: 8BIT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 10 Sep 2024, Andreas Kretschmer wrote: > you can use the hidden ctid-column: > > postgres=# create table demo (id int, val text); > CREATE TABLE > postgres=# insert into demo values (1, 'test1'); > INSERT 0 1 > postgres=# insert into demo values (1, 'test1'); > INSERT 0 1 > postgres=# insert into demo values (1, 'test1'); > INSERT 0 1 > postgres=# insert into demo values (1, 'test1'); > INSERT 0 1 > postgres=# select ctid, * from demo; >  ctid  | id |  val > -------+----+------- >  (0,1) |  1 | test1 >  (0,2) |  1 | test1 >  (0,3) |  1 | test1 >  (0,4) |  1 | test1 > (4 rows) > > postgres=# with my_ctid as (select min(ctid) from demo where id = 1 and val = > 'test1') delete from demo using my_ctid where id=1 and val='test1' and ctid > != my_ctid.min; > DELETE 3 > postgres=# select ctid, * from demo; >  ctid  | id |  val > -------+----+------- >  (0,1) |  1 | test1 > (1 row) Thanks, Andreas. Rich