diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-08-17 11:12:35 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-08-17 11:12:35 -0400 |
commit | efd0c16becbf45e3b0215e124fde75fee8fcbce4 (patch) | |
tree | 4583c8e845ce18ae4a6c6b4c008a4989c514b3cb /src/backend/commands/tablecmds.c | |
parent | 4a319fce7671ffbe2a730f79529b7a2ef3794d41 (diff) | |
download | postgresql-efd0c16becbf45e3b0215e124fde75fee8fcbce4.tar.gz postgresql-efd0c16becbf45e3b0215e124fde75fee8fcbce4.zip |
Avoid using list_length() to test for empty list.
The standard way to check for list emptiness is to compare the
List pointer to NIL; our list code goes out of its way to ensure
that that is the only representation of an empty list. (An
acceptable alternative is a plain boolean test for non-null
pointer, but explicit mention of NIL is usually preferable.)
Various places didn't get that memo and expressed the condition
with list_length(), which might not be so bad except that there
were such a variety of ways to check it exactly: equal to zero,
less than or equal to zero, less than one, yadda yadda. In the
name of code readability, let's standardize all those spellings
as "list == NIL" or "list != NIL". (There's probably some
microscopic efficiency gain too, though few of these look to be
at all performance-critical.)
A very small number of cases were left as-is because they seemed
more consistent with other adjacent list_length tests that way.
Peter Smith, with bikeshedding from a number of us
Discussion: https://postgr.es/m/CAHut+PtQYe+ENX5KrONMfugf0q6NHg4hR5dAhqEXEc2eefFeig@mail.gmail.com
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 70b94bbb397..8d7c68b8b3c 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2097,7 +2097,7 @@ ExecuteTruncateGuts(List *explicit_rels, * Assemble an array of relids so we can write a single WAL record for the * whole action. */ - if (list_length(relids_logged) > 0) + if (relids_logged != NIL) { xl_heap_truncate xlrec; int i = 0; @@ -16264,11 +16264,11 @@ ATPrepChangePersistence(Relation rel, bool toLogged) } /* - * Check that the table is not part any publication when changing to - * UNLOGGED as UNLOGGED tables can't be published. + * Check that the table is not part of any publication when changing to + * UNLOGGED, as UNLOGGED tables can't be published. */ if (!toLogged && - list_length(GetRelationPublications(RelationGetRelid(rel))) > 0) + GetRelationPublications(RelationGetRelid(rel)) != NIL) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("cannot change table \"%s\" to unlogged because it is part of a publication", |