Re: Getting NOT NULL constraint from pg_attribute - Mailing list pgsql-hackers

From Wu Ivy
Subject Re: Getting NOT NULL constraint from pg_attribute
Date
Msg-id 4C9A329E-A6D5-4240-8919-BB6397BF8739@gmail.com
Whole thread Raw
In response to Re: Getting NOT NULL constraint from pg_attribute  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Getting NOT NULL constraint from pg_attribute
List pgsql-hackers
Hi tom,

Thanks for the quick respond.
Why are SELECT query never marked nullable? For nullable columns, when I call SPI_getvalue(), the result (in char*) is NULL. I don’t think I’m too clear on the definition of attnotnull. Can you give me a example in which the tupleTable is can be marked nullable?
Also, is there any other ways to get nullability of each column while getting the data from SPI_cursor_fetch? The only way I can think is to call another separate command to query the table schema, but it will be in a separate transaction in that case.

Thank you again!
Best,
Ivy
On Aug 17, 2018, at 6:02 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Wu Ivy <ivywuyzl@gmail.com> writes:
I’m currently building a Postgres C extension that fetch data from a Postgres table.
Since the table can be large, in order to prevent memory overrun, I use SPI_cursor_fetch to fetch chunks of data. The result rows are saved in SPITupleTable* SPI_tuptable and attributes are saved in SPI_tuptable->tupdesc.
In order to process my data, I need to get information of column nullability (whether column has NOT NULL constrain). I can get this information by calling:

TupleDesc tupdesc = SPI_tuptable->tupdesc;
bool is_nullable = TupleDescAttr(tupdesc, column_num - 1) -> attnotnull;
However, the result (is_nullable) is always 0, meaning the column does not have NOT NULLl constraint, even for columns that do have the NOT NULL constraint.

The output columns of a SELECT query are never marked nullable, regardless
of what the source data was.

regards, tom lane

pgsql-hackers by date:

Previous
From: Chris Travers
Date:
Subject: Re: Two proposed modifications to the PostgreSQL FDW
Next
From: "David G. Johnston"
Date:
Subject: Re: Getting NOT NULL constraint from pg_attribute