Hi,

While working on another patch, I find that tablecmds.c now has three
ways to check the validity of catalog tuples.
i.
if (tuple != NULL)

ii.
if (!tuple)

iii.
if (HeapTupleIsValid(tuple)

In tablecmds.c, most checks use macro HeapTupleIsValid. For
code readability,
I changed the first and the second formats to the third one, e.g., using
HeapTupleIsValid.

BTW,  I searched the other files, some files also have different ways to
check the validity of tuples.
But the attached patch only focuses on tablecmds.c

Any thoughts?

-- 
Thanks, Tender Wang
From 13bfbba547df1db9a441eafd9d1aef194ad38a9e Mon Sep 17 00:00:00 2001
From: Tender Wang <tndrw...@gmail.com>
Date: Wed, 9 Apr 2025 19:25:59 +0800
Subject: [PATCH] Use macro HeapTupleIsValid to check tuple valid or not.

Now, in tablecmds.c, we have three different format to check catalog
tuple be valid or not.
  if (tuple != NULL)
     ...

  if (!tuple)
     ...

  if (HeapTupleIsValid(tuple))
    ...

For more codes readability, using macro HeapTupleIsValid to check the
validity of tuples.
---
 src/backend/commands/tablecmds.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 686f1850cab..cd0eaa2efa6 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -7097,7 +7097,7 @@ find_typed_table_dependencies(Oid typeOid, const char 
*typeName, DropBehavior be
 
        scan = table_beginscan_catalog(classRel, 1, key);
 
-       while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
+       while (HeapTupleIsValid(tuple = heap_getnext(scan, 
ForwardScanDirection)))
        {
                Form_pg_class classform = (Form_pg_class) GETSTRUCT(tuple);
 
@@ -7798,7 +7798,7 @@ ATExecDropNotNull(Relation rel, const char *colName, bool 
recurse,
         * dropconstraint_internal() resets attnotnull.
         */
        conTup = findNotNullConstraintAttnum(RelationGetRelid(rel), attnum);
-       if (conTup == NULL)
+       if (!HeapTupleIsValid(conTup))
                elog(ERROR, "cache lookup failed for not-null constraint on 
column \"%s\" of relation \"%s\"",
                         colName, RelationGetRelationName(rel));
 
@@ -9470,7 +9470,7 @@ ATPrepAddPrimaryKey(List **wqueue, Relation rel, 
AlterTableCmd *cmd,
                                Form_pg_attribute attrForm;
 
                                tup = SearchSysCacheAttName(childrelid, 
strVal(attname));
-                               if (!tup)
+                               if (!HeapTupleIsValid(tup))
                                        elog(ERROR, "cache lookup failed for 
attribute %s of relation %u",
                                                 strVal(attname), childrelid);
                                attrForm = (Form_pg_attribute) GETSTRUCT(tup);
@@ -9496,7 +9496,7 @@ ATPrepAddPrimaryKey(List **wqueue, Relation rel, 
AlterTableCmd *cmd,
                 * valid, though.
                 */
                tuple = findNotNullConstraint(RelationGetRelid(rel), 
strVal(column));
-               if (tuple != NULL)
+               if (HeapTupleIsValid(tuple))
                {
                        Form_pg_constraint conForm = (Form_pg_constraint) 
GETSTRUCT(tuple);
 
@@ -11207,7 +11207,7 @@ CloneFkReferenced(Relation parentRel, Relation 
partitionRel)
        /* This is a seqscan, as we don't have a usable index ... */
        scan = systable_beginscan(pg_constraint, InvalidOid, true,
                                                          NULL, 2, key);
-       while ((tuple = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(tuple = systable_getnext(scan)))
        {
                Form_pg_constraint constrForm = (Form_pg_constraint) 
GETSTRUCT(tuple);
 
@@ -11878,7 +11878,7 @@ RemoveInheritedConstraint(Relation conrel, Relation 
trigrel, Oid conoid,
                                                          
ConstraintRelidTypidNameIndexId,
                                                          true, NULL, 1, &key);
        objs = new_object_addresses();
-       while ((consttup = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(consttup = systable_getnext(scan)))
        {
                Form_pg_constraint conform = (Form_pg_constraint) 
GETSTRUCT(consttup);
 
@@ -11915,7 +11915,7 @@ RemoveInheritedConstraint(Relation conrel, Relation 
trigrel, Oid conoid,
                                                ObjectIdGetDatum(conform->oid));
                        scan2 = systable_beginscan(trigrel, 
TriggerConstraintIndexId,
                                                                           
true, NULL, 1, &key2);
-                       while ((trigtup = systable_getnext(scan2)) != NULL)
+                       while (HeapTupleIsValid(trigtup = 
systable_getnext(scan2)))
                        {
                                ObjectAddressSet(addr, TriggerRelationId,
                                                                 
((Form_pg_trigger) GETSTRUCT(trigtup))->oid);
@@ -11955,7 +11955,7 @@ DropForeignKeyConstraintTriggers(Relation trigrel, Oid 
conoid, Oid confrelid,
                                ObjectIdGetDatum(conoid));
        scan = systable_beginscan(trigrel, TriggerConstraintIndexId, true,
                                                          NULL, 1, &key);
-       while ((trigtup = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(trigtup = systable_getnext(scan)))
        {
                Form_pg_trigger trgform = (Form_pg_trigger) GETSTRUCT(trigtup);
                ObjectAddress trigger;
@@ -12027,7 +12027,7 @@ GetForeignKeyActionTriggers(Relation trigrel,
 
        scan = systable_beginscan(trigrel, TriggerConstraintIndexId, true,
                                                          NULL, 1, &key);
-       while ((trigtup = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(trigtup = systable_getnext(scan)))
        {
                Form_pg_trigger trgform = (Form_pg_trigger) GETSTRUCT(trigtup);
 
@@ -12088,7 +12088,7 @@ GetForeignKeyCheckTriggers(Relation trigrel,
 
        scan = systable_beginscan(trigrel, TriggerConstraintIndexId, true,
                                                          NULL, 1, &key);
-       while ((trigtup = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(trigtup = systable_getnext(scan)))
        {
                Form_pg_trigger trgform = (Form_pg_trigger) GETSTRUCT(trigtup);
 
@@ -12597,7 +12597,7 @@ ATExecAlterConstrInheritability(List **wqueue, 
ATAlterConstraint *cmdcon,
                        Form_pg_constraint childcon;
 
                        childtup = findNotNullConstraint(childoid, colName);
-                       if (!childtup)
+                       if (!HeapTupleIsValid(childtup))
                                elog(ERROR, "cache lookup failed for not-null 
constraint on column \"%s\" of relation %u",
                                         colName, childoid);
                        childcon = (Form_pg_constraint) GETSTRUCT(childtup);
@@ -13189,7 +13189,7 @@ QueueNNConstraintValidation(List **wqueue, Relation 
conrel, Relation rel,
                 * column name.
                 */
                contup = findNotNullConstraint(childoid, colname);
-               if (!contup)
+               if (!HeapTupleIsValid(contup))
                        elog(ERROR, "cache lookup failed for not-null 
constraint on column \"%s\" of relation \"%s\"",
                                 colname, get_rel_name(childoid));
                childcon = (Form_pg_constraint) GETSTRUCT(contup);
@@ -21707,7 +21707,7 @@ validatePartitionedIndex(Relation partedIdx, Relation 
partedTbl)
                                ObjectIdGetDatum(RelationGetRelid(partedIdx)));
        scan = systable_beginscan(inheritsRel, InheritsParentIndexId, true,
                                                          NULL, 1, &key);
-       while ((inhTup = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(inhTup = systable_getnext(scan)))
        {
                Form_pg_inherits inhForm = (Form_pg_inherits) GETSTRUCT(inhTup);
                HeapTuple       indTup;
@@ -21837,7 +21837,7 @@ GetParentedForeignKeyRefs(Relation partition)
 
        /* XXX This is a seqscan, as we don't have a usable index */
        scan = systable_beginscan(pg_constraint, InvalidOid, true, NULL, 2, 
key);
-       while ((tuple = systable_getnext(scan)) != NULL)
+       while (HeapTupleIsValid(tuple = systable_getnext(scan)))
        {
                Form_pg_constraint constrForm = (Form_pg_constraint) 
GETSTRUCT(tuple);
 
-- 
2.34.1

Reply via email to