Re: determine what column(s) form the primary key, in C extention

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: alex maslakov <alex(at)serendipia(dot)email>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: determine what column(s) form the primary key, in C extention
Date: 2020-07-29 01:01:45
Message-ID: CAApHDvp-z6rOnEsCMUYQO0aTPOPT1xJ8ZwiBdRw6e11p2qqusw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-general

On Wed, 29 Jul 2020 at 03:45, alex maslakov <alex(at)serendipia(dot)email> wrote:
> 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?

User attributes start at 1. Have a look at the pg_attribute system
catalogue table. The number you get will be the attnum column from
that table.

> (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?

It did recently change from -8 to -7 when we removed Oid as a system
column in pg12. The number will never change on a major version, so
you'll always know what it is for versions that have already been
released. There's always a chance it'll change from -7 in some future
PostgreSQL version though.

David

In response to

Browse pgsql-committers by date

  From Date Subject
Next Message Thomas Munro 2020-07-29 05:01:28 pgsql: Move syncscan.c to src/backend/access/common.
Previous Message Peter Geoghegan 2020-07-29 00:59:29 pgsql: Rename another "hash_mem" local variable.

Browse pgsql-general by date

  From Date Subject
Next Message David Rowley 2020-07-29 01:19:55 Re: is JIT available
Previous Message Shaozhong SHI 2020-07-28 23:00:18 Re: Issues of slow running queries when dealing with Big Data