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 1nXPrp-0004nf-Dd for pgsql-general@arkaria.postgresql.org; Thu, 24 Mar 2022 15:59:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nXPrn-0003IA-FF for pgsql-general@arkaria.postgresql.org; Thu, 24 Mar 2022 15:59:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXPrn-0003I1-4d for pgsql-general@lists.postgresql.org; Thu, 24 Mar 2022 15:59:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXPrg-00009S-Rb for pgsql-general@lists.postgresql.org; Thu, 24 Mar 2022 15:59:14 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 22OFx7JK3016113; Thu, 24 Mar 2022 11:59:07 -0400 From: Tom Lane To: Oskar Stenberg cc: "pgsql-general@lists.postgresql.org" Subject: Re: Get primary key of a table through SPI In-reply-to: References: Comments: In-reply-to Oskar Stenberg message dated "Thu, 24 Mar 2022 15:49:39 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3016111.1648137547.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 24 Mar 2022 11:59:07 -0400 Message-ID: <3016112.1648137547@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Oskar Stenberg writes: > I need to get the primary keys of a few tables for some code that I'm wr= iting in C. I've found out how to do this through regular SQL code and I k= now that I can use that SQL code through the SPI. But I was just wondering= if there is a better/faster way to get information like this directly thr= ough the SPI? I'm imagining something similar to how I can get for example= the name of the column with the SPI_fname function? Couldn't find anythin= g when I was looking around, but I might have looked in the wrong place? SPI doesn't concern itself with such things. If you're writing C code, there are a lot of server internal APIs you can use for catalog inquiries --- in this case, RelationGetPrimaryKeyIndex() might be just what you want. However, you may or may not find those convenient to use, and in any case they're less stable across major versions than the SPI APIs. So it's a tradeoff. regards, tom lane