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 1pff15-0003Ko-1y for pgsql-general@arkaria.postgresql.org; Fri, 24 Mar 2023 10:51:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pff13-0006Bg-VR for pgsql-general@arkaria.postgresql.org; Fri, 24 Mar 2023 10:51:25 +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 1pff13-0006BX-KP for pgsql-general@lists.postgresql.org; Fri, 24 Mar 2023 10:51:25 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pff11-0002OO-0l for pgsql-general@postgresql.org; Fri, 24 Mar 2023 10:51:24 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id BD4A332007BE; Fri, 24 Mar 2023 06:51:21 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Fri, 24 Mar 2023 06:51:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1679655081; x= 1679741481; bh=yZOh3oHT3OIjpnYVUK3s48mqY2o75S81hY4+KIE4iJM=; b=h ohIZW6B0FBBMQ0IzkSnA5vEt1mXXAQaUbs7MFUpZFWqZ3WSVkeiFVagMltHBU8Vg n5rZjSZ6ruUes57f6XvNS/9nsDEOzrpF+zVWZ3XMWb3o9wkvz4ro+lDFmc10hGxO av6Us/Wvgkdz+IVEGEm8BRIH7qRIvgGG7geIHimJRYDKQ3ul3blbFwXHgVZzVn20 s8oLYTqI7b009eDz1zl8+b4HBIOctzJ5quFXAPeIOQs4Nv/yAkrnCh62oG3QySS4 iIG5mhZl//yhj16ggD6SEQXdHkBmylteExzak/dkum21HaGTlrbhIT4g4XoknJYe w5v0yZh3lDIDCe2+XaBsQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdegiedgvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 24 Mar 2023 06:51:21 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id EE759386; Fri, 24 Mar 2023 11:51:19 +0100 (CET) Date: Fri, 24 Mar 2023 11:51:19 +0100 From: Alvaro Herrera To: Dominique Devienne Cc: pgsql-general@postgresql.org Subject: Re: Convert pg_constraint.conkey array to same-order array of column names Message-ID: <20230324105119.43vqslrpj5klofs2@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2023-Mar-23, Dominique Devienne wrote: > We have a query returning 1 row per constraint column, > which until recently we didn't realize wasn't preserving order of the > columns. > > A colleague fixed that, with something like below: > > SELECT ... > FROM pg_catalog.pg_constraint cnstr > ... > CROSS JOIN LATERAL UNNEST(cnstr.conkey) WITH ORDINALITY AS cols(value, rank) > ORDER BY cols.rank > > But I'm wondering about getting 1 row per constraint instead, > and fetching an array of column names. IIRC you can use array_agg(unnest ORDER BY ordinality), grouping by constraint OID or name. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ "La espina, desde que nace, ya pincha" (Proverbio africano)