Thread: in C trigger function find out if column is part of primary key

in C trigger function find out if column is part of primary key

From
"Enke, Dr., Michael"
Date:

Hello C-(trigger)-experts,

I have to find out if a column ( i = 0, …, tupdesc->natts-1 ) is part of a primary key
but for performance reasons I do not want to execute another query via SPI.
Is this possible?

 

Currently I did not find a way, instead I check for “not null” attribute tupdesc->attrs[i]->attnotnull
and in addition if the column is used in an index via relation->rd_indexattr
but this Bitmapset has 8 leading zeros.
Is there a define which I can use instead of hardcode the 8? Currently I use bms_is_member(i + 8, rd_indexattr)

 

Thank you,

Michael

WINCOR NIXDORF International GmbH
Sitz der Gesellschaft: Paderborn
Registergericht Paderborn HRB 3507
Geschäftsführer: Eckard Heidloff (Vorsitzender), Dr. Jürgen Wunram (stellv. Vors.), Jens Bohlen
Vorsitzender des Aufsichtsrats: Dr. Alexander Dibelius
Steuernummer: 339/5884/0020 - Ust-ID Nr.: DE812927716 - WEEE-Reg.-Nr. DE44477193

Diese E-Mail enthält vertrauliche Informationen.
Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben,
informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail.
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

This e-mail may contain confidential information.
If you are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail.
Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Re: in C trigger function find out if column is part of primary key

From
Marko Kreen
Date:
On Mon, Apr 01, 2013 at 12:06:25AM +0200, Enke, Dr., Michael wrote:
> I have to find out if a column ( i = 0, ..., tupdesc->natts-1 ) is part of a primary key
> but for performance reasons I do not want to execute another query via SPI.
> Is this possible?

Simpler and probably even faster approach would be to do
catalog lookup via SPI and then cache the result.

The trick here is to get cache invalidation right,
so on any table change cache is refreshed.

You can use PgQ triggers as example:

  https://github.com/markokr/skytools/blob/master/sql/pgq/triggers/common.c

It implements various unrelated features, but basics are in
find_table_info() which loads cached data and relcache_reset_cb()
which invalidates.

--
marko



Re: in C trigger function find out if column is part of primary key

From
Kevin Grittner
Date:
"Enke, Dr., Michael" <michael.enke@wincor-nixdorf.com> wrote:
> I have to find out if a column ( i = 0, …, tupdesc->natts-1 ) is
> part of a primary key but for performance reasons I do not want
> to execute another query via SPI.  Is this possible?

You might get some inspiration from looking at this bit of code:


http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=contrib/tcn/tcn.c;h=eb7e1a61a6a1d5c5ed2e840af41410ac4a52418f;hb=master#l127

--
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company