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 1k0SDi-0005Nb-ME for pgsql-general@arkaria.postgresql.org; Tue, 28 Jul 2020 16:12:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k0SDg-00039E-KA for pgsql-general@arkaria.postgresql.org; Tue, 28 Jul 2020 16:12:48 +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 1k0Rnp-0000oJ-Ix for pgsql-general@lists.postgresql.org; Tue, 28 Jul 2020 15:46:05 +0000 Received: from mail.pussywipe.pw ([172.105.238.192]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k0Rnm-0002dT-6S for pgsql-general@lists.postgresql.org; Tue, 28 Jul 2020 15:46:05 +0000 Received: from insignia.local (unknown [IPv6:2409:4042:e97:1a91:fd16:46dc:5171:6ca0]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.pussywipe.pw (Postfix) with ESMTPSA id 58C792064F; Tue, 28 Jul 2020 15:45:53 +0000 (UTC) Subject: Re: determine what column(s) form the primary key, in C extention To: David Rowley Cc: pgsql-general@lists.postgresql.org References: <2f671cae-a3fe-7064-37d9-68847194ce8d@serendipia.email> From: alex maslakov Message-ID: <4e3b5a5e-00eb-56dd-aa17-f6f340a91c8d@serendipia.email> Date: Tue, 28 Jul 2020 21:15:49 +0530 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I'm emailing it to the 'general' list. (1) This:     int i = -1;     while ((i = bms_next_member(pkattnos , i)) >= 0) {         /* do stuff with i */         /* you'll need to use i - FirstLowInvalidHeapAttributeNumber to get the pg_attribute.attnum */         elog(INFO, "bms_next_member i: %d", i);     } prints 10 and then 9 Then:   10 - FirstLowInvalidHeapAttributeNumber (-8) ==> 2   9 - FirstLowInvalidHeapAttributeNumber (-8) ==> 1 These are indexes of the columns, right? Do they start from 1, not from 0? (2) I'll use this C code as an example to build an extention in Rust. The Postgresql bindings for Rust I have don't contain a definition of `FirstLowInvalidHeapAttributeNumber` for some reason. I can define it since it's simply single digit constant. However what does in some source files it's defined as (-7) and in some as (-8)? Which should I use? El 28/07/2020 a las 03:20, David Rowley escribió: > Hi Alex, > > On Tue, 28 Jul 2020 at 05:47, alex maslakov wrote: >> I was suggested to use `get_primary_key_attnos` from >> `src/include/catalog/pg_constraint.h` >> >> extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk) >> >> >> It returns *Bitstamp. And it's got "nwords" and "words[]". But those >> return just big numbers, not something that look similar to an index of >> the primary key column. >> >> >> And therefore I haven't had any luck thus far. >> >> How to do it? > You'll need to loop over the return value of that function with > bms_next_member() > > e.g. > pkattnos = get_primary_key_attnos(oid, false); > i = -1; > while ((i = bms_next_member(pkattnos , i)) >= 0) > { > /* do stuff with i */ > /* you'll need to use i - FirstLowInvalidHeapAttributeNumber to get > the pg_attribute.attnum */ > } > > For the future, for questions, you should use the general list. If > the question is very source code related then you might have more luck > in pgsql-hackers. This is not the right list. Please post any > followup questions on one of those lists. > > Note the description for this list, per > https://www.postgresql.org/list/ is "Notification of git commits are > sent to this list. Do not post here!" > > David