Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efd0c16

Browse files
committed
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
1 parent 4a319fc commit efd0c16

File tree

28 files changed

+47
-48
lines changed

28 files changed

+47
-48
lines changed

src/backend/catalog/objectaddress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
21862186
else
21872187
{
21882188
name = textarray_to_strvaluelist(namearr);
2189-
if (list_length(name) < 1)
2189+
if (name == NIL)
21902190
ereport(ERROR,
21912191
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
21922192
errmsg("name list length must be at least %d", 1)));

src/backend/catalog/pg_depend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ getIdentitySequence(Oid relid, AttrNumber attnum, bool missing_ok)
947947

948948
if (list_length(seqlist) > 1)
949949
elog(ERROR, "more than one owned sequence found");
950-
else if (list_length(seqlist) < 1)
950+
else if (seqlist == NIL)
951951
{
952952
if (missing_ok)
953953
return InvalidOid;

src/backend/commands/event_trigger.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,9 @@ trackDroppedObjectsNeeded(void)
11431143
* true if any sql_drop, table_rewrite, ddl_command_end event trigger
11441144
* exists
11451145
*/
1146-
return list_length(EventCacheLookup(EVT_SQLDrop)) > 0 ||
1147-
list_length(EventCacheLookup(EVT_TableRewrite)) > 0 ||
1148-
list_length(EventCacheLookup(EVT_DDLCommandEnd)) > 0;
1146+
return (EventCacheLookup(EVT_SQLDrop) != NIL) ||
1147+
(EventCacheLookup(EVT_TableRewrite) != NIL) ||
1148+
(EventCacheLookup(EVT_DDLCommandEnd) != NIL);
11491149
}
11501150

11511151
/*
@@ -1616,7 +1616,7 @@ EventTriggerAlterTableEnd(void)
16161616
parent = currentEventTriggerState->currentCommand->parent;
16171617

16181618
/* If no subcommands, don't collect */
1619-
if (list_length(currentEventTriggerState->currentCommand->d.alterTable.subcmds) != 0)
1619+
if (currentEventTriggerState->currentCommand->d.alterTable.subcmds != NIL)
16201620
{
16211621
MemoryContext oldcxt;
16221622

src/backend/commands/functioncmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ interpret_function_parameter_list(ParseState *pstate,
419419
* Make sure no variables are referred to (this is probably dead
420420
* code now that add_missing_from is history).
421421
*/
422-
if (list_length(pstate->p_rtable) != 0 ||
422+
if (pstate->p_rtable != NIL ||
423423
contain_var_clause(def))
424424
ereport(ERROR,
425425
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
@@ -1209,7 +1209,7 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
12091209
returnsSet = false;
12101210
}
12111211

1212-
if (list_length(trftypes_list) > 0)
1212+
if (trftypes_list != NIL)
12131213
{
12141214
ListCell *lc;
12151215
Datum *arr;

src/backend/commands/publicationcmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,12 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
848848
&schemaidlist);
849849

850850
/* FOR ALL TABLES IN SCHEMA requires superuser */
851-
if (list_length(schemaidlist) > 0 && !superuser())
851+
if (schemaidlist != NIL && !superuser())
852852
ereport(ERROR,
853853
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
854854
errmsg("must be superuser to create FOR ALL TABLES IN SCHEMA publication"));
855855

856-
if (list_length(relations) > 0)
856+
if (relations != NIL)
857857
{
858858
List *rels;
859859

@@ -871,7 +871,7 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
871871
CloseTableList(rels);
872872
}
873873

874-
if (list_length(schemaidlist) > 0)
874+
if (schemaidlist != NIL)
875875
{
876876
/*
877877
* Schema lock is held until the publication is created to prevent

src/backend/commands/statscmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ CreateStatistics(CreateStatsStmt *stmt)
339339
if ((list_length(stmt->exprs) == 1) && (list_length(stxexprs) == 1))
340340
{
341341
/* statistics kinds not specified */
342-
if (list_length(stmt->stat_types) > 0)
342+
if (stmt->stat_types != NIL)
343343
ereport(ERROR,
344344
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
345345
errmsg("when building statistics on a single expression, statistics kinds may not be specified")));
@@ -391,7 +391,7 @@ CreateStatistics(CreateStatsStmt *stmt)
391391
* automatically. This allows calculating good estimates for stats that
392392
* consider per-clause estimates (e.g. functional dependencies).
393393
*/
394-
build_expressions = (list_length(stxexprs) > 0);
394+
build_expressions = (stxexprs != NIL);
395395

396396
/*
397397
* Check that at least two columns were specified in the statement, or

src/backend/commands/subscriptioncmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ get_publications_str(List *publications, StringInfo dest, bool quote_literal)
410410
ListCell *lc;
411411
bool first = true;
412412

413-
Assert(list_length(publications) > 0);
413+
Assert(publications != NIL);
414414

415415
foreach(lc, publications)
416416
{

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ ExecuteTruncateGuts(List *explicit_rels,
20972097
* Assemble an array of relids so we can write a single WAL record for the
20982098
* whole action.
20992099
*/
2100-
if (list_length(relids_logged) > 0)
2100+
if (relids_logged != NIL)
21012101
{
21022102
xl_heap_truncate xlrec;
21032103
int i = 0;
@@ -16264,11 +16264,11 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
1626416264
}
1626516265

1626616266
/*
16267-
* Check that the table is not part any publication when changing to
16268-
* UNLOGGED as UNLOGGED tables can't be published.
16267+
* Check that the table is not part of any publication when changing to
16268+
* UNLOGGED, as UNLOGGED tables can't be published.
1626916269
*/
1627016270
if (!toLogged &&
16271-
list_length(GetRelationPublications(RelationGetRelid(rel))) > 0)
16271+
GetRelationPublications(RelationGetRelid(rel)) != NIL)
1627216272
ereport(ERROR,
1627316273
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1627416274
errmsg("cannot change table \"%s\" to unlogged because it is part of a publication",

src/backend/commands/typecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3503,7 +3503,7 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
35033503
* Domains don't allow variables (this is probably dead code now that
35043504
* add_missing_from is history, but let's be sure).
35053505
*/
3506-
if (list_length(pstate->p_rtable) != 0 ||
3506+
if (pstate->p_rtable != NIL ||
35073507
contain_var_clause(expr))
35083508
ereport(ERROR,
35093509
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),

src/backend/executor/execPartition.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
685685
* list and searching for ancestry relationships to each index in the
686686
* ancestor table.
687687
*/
688-
if (list_length(rootResultRelInfo->ri_onConflictArbiterIndexes) > 0)
688+
if (rootResultRelInfo->ri_onConflictArbiterIndexes != NIL)
689689
{
690690
List *childIdxs;
691691

0 commit comments

Comments
 (0)