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 | |
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')
-rw-r--r-- | src/backend/commands/event_trigger.c | 8 | ||||
-rw-r--r-- | src/backend/commands/functioncmds.c | 4 | ||||
-rw-r--r-- | src/backend/commands/publicationcmds.c | 6 | ||||
-rw-r--r-- | src/backend/commands/statscmds.c | 4 | ||||
-rw-r--r-- | src/backend/commands/subscriptioncmds.c | 2 | ||||
-rw-r--r-- | src/backend/commands/tablecmds.c | 8 | ||||
-rw-r--r-- | src/backend/commands/typecmds.c | 2 |
7 files changed, 17 insertions, 17 deletions
diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index eef3e5d56e5..9f8fee7c168 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -1143,9 +1143,9 @@ trackDroppedObjectsNeeded(void) * true if any sql_drop, table_rewrite, ddl_command_end event trigger * exists */ - return list_length(EventCacheLookup(EVT_SQLDrop)) > 0 || - list_length(EventCacheLookup(EVT_TableRewrite)) > 0 || - list_length(EventCacheLookup(EVT_DDLCommandEnd)) > 0; + return (EventCacheLookup(EVT_SQLDrop) != NIL) || + (EventCacheLookup(EVT_TableRewrite) != NIL) || + (EventCacheLookup(EVT_DDLCommandEnd) != NIL); } /* @@ -1616,7 +1616,7 @@ EventTriggerAlterTableEnd(void) parent = currentEventTriggerState->currentCommand->parent; /* If no subcommands, don't collect */ - if (list_length(currentEventTriggerState->currentCommand->d.alterTable.subcmds) != 0) + if (currentEventTriggerState->currentCommand->d.alterTable.subcmds != NIL) { MemoryContext oldcxt; diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c index 59e3af626fa..e7e37146f69 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -419,7 +419,7 @@ interpret_function_parameter_list(ParseState *pstate, * Make sure no variables are referred to (this is probably dead * code now that add_missing_from is history). */ - if (list_length(pstate->p_rtable) != 0 || + if (pstate->p_rtable != NIL || contain_var_clause(def)) ereport(ERROR, (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), @@ -1209,7 +1209,7 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt) returnsSet = false; } - if (list_length(trftypes_list) > 0) + if (trftypes_list != NIL) { ListCell *lc; Datum *arr; diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 89a005540f6..8b574b86c47 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -848,12 +848,12 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt) &schemaidlist); /* FOR ALL TABLES IN SCHEMA requires superuser */ - if (list_length(schemaidlist) > 0 && !superuser()) + if (schemaidlist != NIL && !superuser()) ereport(ERROR, errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), errmsg("must be superuser to create FOR ALL TABLES IN SCHEMA publication")); - if (list_length(relations) > 0) + if (relations != NIL) { List *rels; @@ -871,7 +871,7 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt) CloseTableList(rels); } - if (list_length(schemaidlist) > 0) + if (schemaidlist != NIL) { /* * Schema lock is held until the publication is created to prevent diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 7c62bebfd2d..55216d28916 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -339,7 +339,7 @@ CreateStatistics(CreateStatsStmt *stmt) if ((list_length(stmt->exprs) == 1) && (list_length(stxexprs) == 1)) { /* statistics kinds not specified */ - if (list_length(stmt->stat_types) > 0) + if (stmt->stat_types != NIL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("when building statistics on a single expression, statistics kinds may not be specified"))); @@ -391,7 +391,7 @@ CreateStatistics(CreateStatsStmt *stmt) * automatically. This allows calculating good estimates for stats that * consider per-clause estimates (e.g. functional dependencies). */ - build_expressions = (list_length(stxexprs) > 0); + build_expressions = (stxexprs != NIL); /* * Check that at least two columns were specified in the statement, or diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index f73dfb6067f..670b219c8d4 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -410,7 +410,7 @@ get_publications_str(List *publications, StringInfo dest, bool quote_literal) ListCell *lc; bool first = true; - Assert(list_length(publications) > 0); + Assert(publications != NIL); foreach(lc, publications) { 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", diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index d0d87a1184a..33b64fd2793 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -3503,7 +3503,7 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid, * Domains don't allow variables (this is probably dead code now that * add_missing_from is history, but let's be sure). */ - if (list_length(pstate->p_rtable) != 0 || + if (pstate->p_rtable != NIL || contain_var_clause(expr)) ereport(ERROR, (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |