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.96) (envelope-from ) id 1vNdy8-009n90-12 for pgsql-docs@arkaria.postgresql.org; Mon, 24 Nov 2025 21:19:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vNdy6-004VHL-1q for pgsql-docs@arkaria.postgresql.org; Mon, 24 Nov 2025 21:19:30 +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.96) (envelope-from ) id 1vNdy6-004VHD-16 for pgsql-docs@lists.postgresql.org; Mon, 24 Nov 2025 21:19:30 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vNdy4-001Ifv-1t for pgsql-docs@lists.postgresql.org; Mon, 24 Nov 2025 21:19:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2025010100; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description; bh=I8aHpkwwe6uQDP6RxLQBbH7FZvDDUvgQoxh3ZFsb7n0=; b=QS438 UR7X9U01HF9oCjZp7fTv0lmRf60/pKzrRsNK7SHy7u8phopTOtBsWleT3PhN98NWZElOjnWK/fJYB FieYV4JSjiirFlyypn7hTShBlcoMhKrfbs5QpI41VebDp7XkaE6ckjGYfz2C9UfITSBTD210XCOzN fc+/xrYHZX1LPcYiawneUdwITssyac7bK3CuBpoB5i3JY90x3E4lxV9QNlCSRZ9hlZr6tO6yJ3Edt JlScEnjpIrJY42IPpiCDTsSKjIebkpY1CEA6n5dqsJU0PK6RgjZZApGvEIc/g6RHwYQh8DwXvvGO7 o+Oj5AHMbrxJOoLv94xUEs9Q1G2IA==; Received: from bruce by momjian.us with local (Exim 4.98.2) (envelope-from ) id 1vNdy2-00000000xUF-0nVU; Mon, 24 Nov 2025 16:19:26 -0500 Date: Mon, 24 Nov 2025 16:19:26 -0500 From: Bruce Momjian To: Bernice Southey Cc: pgsql-docs@lists.postgresql.org Subject: Re: More guidance on ctid Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Nov 23, 2025 at 10:24:42AM +0000, Bernice Southey wrote: > Hi, > I'm wondering if the explanation of ctid in System Columns should have > more guidance - especially as it's used in examples in the UPDATE and > DELETE docs. So far I've been caught out by partitions and concurrent > updates. This was after I changed primary keys to ctid, in self-joins > that get ORDER BY or LIMIT, for UPDATE and DELETE. > > I'm too unsure about this idea and my knowledge of ctid to propose a > patch, but FWIW, here's my attempt: > Ctid is useful for removing duplicate rows and efficient self-joins. > Be aware that when ctid is used to find a row, only the first > concurrent write will be applied. Also note that ctids are not unique > across table partitions. > > I'm learning my way through ctid, and the mailing lists, and am still > in the foothills of both. Sorry about the topic-adjacent duplicates in > other lists from my ramblings. I didn't think we had ctid referenced in example queries, but I now see it was added in PG 17: commit 2daeba6a4e4 Author: Tom Lane Date: Sun Apr 7 16:26:47 2024 -0400 Doc: show how to get the equivalent of LIMIT for UPDATE/DELETE. Add examples showing use of a CTE and a self-join to perform partial UPDATEs and DELETEs. Corey Huinker, reviewed by Laurenz Albe Discussion: https://postgr.es/m/CADkLM=caNEQsUwPWnfi2jR4ix99E0EJM_3jtcE-YjnEQC7Rssw@mail.gmail.com The examples use ctid to show how you can do incremental UPDATEs and DELETEs, to avoid excessive cleanup/vacuum requirements. While that certainly is a good idea, it only works because the common table expression examples, by definition, operate in the same snapshot. I think the reporter above extrapolated this example to use ctid in other, non-appropriate cases. Should we add a warning to the docs to explain that the general use of ctid is discouraged? -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Do not let urgent matters crowd out time for investment in the future.