Re: Getting NOT NULL constraint from pg_attribute

From: Wu Ivy <ivywuyzl(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Getting NOT NULL constraint from pg_attribute
Date: 2018-08-20 17:27:36
Message-ID: 4C9A329E-A6D5-4240-8919-BB6397BF8739@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> Wu Ivy <ivywuyzl(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David G. Johnston 2018-08-20 17:40:13 Re: Getting NOT NULL constraint from pg_attribute
Previous Message Chris Travers 2018-08-20 17:14:10 Re: Two proposed modifications to the PostgreSQL FDW