question about is_valid parameter in set_attnotnull
Hi,
(CC'ing Álvaro as the author of a379061a22a8)
I was looking at code in tablecmds.c for some other patch and I
spotted that the is_valid parameter in set_attnotnull(), added in
a379061a22a8, is never used in the function body. It's not clear
why it was added, except that it's passed by the caller. The
CompactAttribute is unconditionally set to ATTNULLABLE_VALID:
```
thisatt = TupleDescCompactAttr(RelationGetDescr(rel), attnum - 1);
thisatt->attnullability = ATTNULLABLE_VALID;
```
The only caller that passes is_valid=false is ATAddCheckNNConstraint
(for NOT VALID constraints).
My only concern is that the assignment is wrong when is_valid is
false, but nothing currently reads attnullability from the same
relation handle after this point, so it's harmless for now.
Currently, the correct attnullability comes from pg_constraint on the
next relcache rebuild.
Should this be using is_valid to set the attnullability
conditionally, or should the parameter just be removed?
--
Sami Imseih
Amazon Web Services (AWS)
Hello Sami,
On 2026-Jul-29, Sami Imseih wrote:
I was looking at code in tablecmds.c for some other patch and I
spotted that the is_valid parameter in set_attnotnull(), added in
a379061a22a8, is never used in the function body. It's not clear
why it was added, except that it's passed by the caller. The
CompactAttribute is unconditionally set to ATTNULLABLE_VALID:```
thisatt = TupleDescCompactAttr(RelationGetDescr(rel), attnum - 1);
thisatt->attnullability = ATTNULLABLE_VALID;
```The only caller that passes is_valid=false is ATAddCheckNNConstraint
(for NOT VALID constraints).
Hmm, yeah -- as I recall, the intention there is that is_valid would be
false when you want to create an invalid constraint. I suppose it was
used that way during development but we later changed our minds about
the design, made the parameter obsolete, and forgot to set it correctly
or remove it. I would have to study the code to remember in more
detail, but I suspect the right action now is to remove it. Maybe we
don't need thisatt at all and it should also be removed?
But it's time to log off for today ...
--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Hay quien adquiere la mala costumbre de ser infeliz" (M. A. Evans)
Thanks for looking!
I was looking at code in tablecmds.c for some other patch and I
spotted that the is_valid parameter in set_attnotnull(), added in
a379061a22a8, is never used in the function body. It's not clear
why it was added, except that it's passed by the caller. The
CompactAttribute is unconditionally set to ATTNULLABLE_VALID:```
thisatt = TupleDescCompactAttr(RelationGetDescr(rel), attnum - 1);
thisatt->attnullability = ATTNULLABLE_VALID;
```The only caller that passes is_valid=false is ATAddCheckNNConstraint
(for NOT VALID constraints).Hmm, yeah -- as I recall, the intention there is that is_valid would be
false when you want to create an invalid constraint. I suppose it was
used that way during development but we later changed our minds about
the design, made the parameter obsolete, and forgot to set it correctly
or remove it. I would have to study the code to remember in more
detail, but I suspect the right action now is to remove it.
At least I could not find any reason for it, but I may have missed something.
The function header comment and callers never mention is_valid either;
only attnotnull and queue_validation.
Maybe we
don't need thisatt at all and it should also be removed?
Right, both the is_valid and thisatt are dead code AFAICT.
Here is a patch that cleans it up.
--
Sami
On Jul 30, 2026, at 02:55, Sami Imseih <samimseih@gmail.com> wrote:
Thanks for looking!
I was looking at code in tablecmds.c for some other patch and I
spotted that the is_valid parameter in set_attnotnull(), added in
a379061a22a8, is never used in the function body. It's not clear
why it was added, except that it's passed by the caller. The
CompactAttribute is unconditionally set to ATTNULLABLE_VALID:```
thisatt = TupleDescCompactAttr(RelationGetDescr(rel), attnum - 1);
thisatt->attnullability = ATTNULLABLE_VALID;
```The only caller that passes is_valid=false is ATAddCheckNNConstraint
(for NOT VALID constraints).Hmm, yeah -- as I recall, the intention there is that is_valid would be
false when you want to create an invalid constraint. I suppose it was
used that way during development but we later changed our minds about
the design, made the parameter obsolete, and forgot to set it correctly
or remove it. I would have to study the code to remember in more
detail, but I suspect the right action now is to remove it.At least I could not find any reason for it, but I may have missed something.
The function header comment and callers never mention is_valid either;
only attnotnull and queue_validation.Maybe we
don't need thisatt at all and it should also be removed?Right, both the is_valid and thisatt are dead code AFAICT.
Here is a patch that cleans it up.
--
Sami
<v1-0001-Remove-dead-code-in-set_attnotnull.patch>
Even from a purely C programming perspective, leaving the unused argument and local variable hurts readability. So +1 for removing them.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
On 2026-Jul-30, Chao Li wrote:
On Jul 30, 2026, at 02:55, Sami Imseih <samimseih@gmail.com> wrote:
Right, both the is_valid and thisatt are dead code AFAICT.
Even from a purely C programming perspective, leaving the unused
argument and local variable hurts readability. So +1 for removing
them.
Okay, removed, thanks.
--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/