aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/tablecmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-07-15 13:41:58 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2019-07-15 13:41:58 -0400
commit1cff1b95ab6ddae32faa3efe0d95a820dbfdc164 (patch)
tree8790ee74932f76826180419725e498ba7a3c7441 /src/backend/commands/tablecmds.c
parent67b9b3ca328392f9afc4e66fe03564f5fc87feff (diff)
downloadpostgresql-1cff1b95ab6ddae32faa3efe0d95a820dbfdc164.tar.gz
postgresql-1cff1b95ab6ddae32faa3efe0d95a820dbfdc164.zip
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of Lisp lists, which consist of chains of separately-allocated cons cells, each having a value and a next-cell link. We'd hacked that once before (commit d0b4399d8) to add a separate List header, but the data was still in cons cells. That makes some operations -- notably list_nth() -- O(N), and it's bulky because of the next-cell pointers and per-cell palloc overhead, and it's very cache-unfriendly if the cons cells end up scattered around rather than being adjacent. In this rewrite, we still have List headers, but the data is in a resizable array of values, with no next-cell links. Now we need at most two palloc's per List, and often only one, since we can allocate some values in the same palloc call as the List header. (Of course, extending an existing List may require repalloc's to enlarge the array. But this involves just O(log N) allocations not O(N).) Of course this is not without downsides. The key difficulty is that addition or deletion of a list entry may now cause other entries to move, which it did not before. For example, that breaks foreach() and sister macros, which historically used a pointer to the current cons-cell as loop state. We can repair those macros transparently by making their actual loop state be an integer list index; the exposed "ListCell *" pointer is no longer state carried across loop iterations, but is just a derived value. (In practice, modern compilers can optimize things back to having just one loop state value, at least for simple cases with inline loop bodies.) In principle, this is a semantics change for cases where the loop body inserts or deletes list entries ahead of the current loop index; but I found no such cases in the Postgres code. The change is not at all transparent for code that doesn't use foreach() but chases lists "by hand" using lnext(). The largest share of such code in the backend is in loops that were maintaining "prev" and "next" variables in addition to the current-cell pointer, in order to delete list cells efficiently using list_delete_cell(). However, we no longer need a previous-cell pointer to delete a list cell efficiently. Keeping a next-cell pointer doesn't work, as explained above, but we can improve matters by changing such code to use a regular foreach() loop and then using the new macro foreach_delete_current() to delete the current cell. (This macro knows how to update the associated foreach loop's state so that no cells will be missed in the traversal.) There remains a nontrivial risk of code assuming that a ListCell * pointer will remain good over an operation that could now move the list contents. To help catch such errors, list.c can be compiled with a new define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents whenever that could possibly happen. This makes list operations significantly more expensive so it's not normally turned on (though it is on by default if USE_VALGRIND is on). There are two notable API differences from the previous code: * lnext() now requires the List's header pointer in addition to the current cell's address. * list_delete_cell() no longer requires a previous-cell argument. These changes are somewhat unfortunate, but on the other hand code using either function needs inspection to see if it is assuming anything it shouldn't, so it's not all bad. Programmers should be aware of these significant performance changes: * list_nth() and related functions are now O(1); so there's no major access-speed difference between a list and an array. * Inserting or deleting a list element now takes time proportional to the distance to the end of the list, due to moving the array elements. (However, it typically *doesn't* require palloc or pfree, so except in long lists it's probably still faster than before.) Notably, lcons() used to be about the same cost as lappend(), but that's no longer true if the list is long. Code that uses lcons() and list_delete_first() to maintain a stack might usefully be rewritten to push and pop at the end of the list rather than the beginning. * There are now list_insert_nth...() and list_delete_nth...() functions that add or remove a list cell identified by index. These have the data-movement penalty explained above, but there's no search penalty. * list_concat() and variants now copy the second list's data into storage belonging to the first list, so there is no longer any sharing of cells between the input lists. The second argument is now declared "const List *" to reflect that it isn't changed. This patch just does the minimum needed to get the new implementation in place and fix bugs exposed by the regression tests. As suggested by the foregoing, there's a fair amount of followup work remaining to do. Also, the ENABLE_LIST_COMPAT macros are finally removed in this commit. Code using those should have been gone a dozen years ago. Patch by me; thanks to David Rowley, Jesper Pedersen, and others for review. Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r--src/backend/commands/tablecmds.c65
1 files changed, 22 insertions, 43 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 0f1a9f0e548..2e792edbcfa 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -2040,13 +2040,13 @@ static List *
MergeAttributes(List *schema, List *supers, char relpersistence,
bool is_partition, List **supconstr)
{
- ListCell *entry;
List *inhSchema = NIL;
List *constraints = NIL;
bool have_bogus_defaults = false;
int child_attno;
static Node bogus_marker = {0}; /* marks conflicting defaults */
List *saved_schema = NIL;
+ ListCell *entry;
/*
* Check for and reject tables with too many columns. We perform this
@@ -2071,12 +2071,17 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
* Although we might consider merging such entries in the same way that we
* handle name conflicts for inherited attributes, it seems to make more
* sense to assume such conflicts are errors.
+ *
+ * We don't use foreach() here because we have two nested loops over the
+ * schema list, with possible element deletions in the inner one. If we
+ * used foreach_delete_current() it could only fix up the state of one of
+ * the loops, so it seems cleaner to use looping over list indexes for
+ * both loops. Note that any deletion will happen beyond where the outer
+ * loop is, so its index never needs adjustment.
*/
- foreach(entry, schema)
+ for (int coldefpos = 0; coldefpos < list_length(schema); coldefpos++)
{
- ColumnDef *coldef = lfirst(entry);
- ListCell *rest = lnext(entry);
- ListCell *prev = entry;
+ ColumnDef *coldef = list_nth_node(ColumnDef, schema, coldefpos);
if (!is_partition && coldef->typeName == NULL)
{
@@ -2092,11 +2097,10 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
coldef->colname)));
}
- while (rest != NULL)
+ /* restpos scans all entries beyond coldef; incr is in loop body */
+ for (int restpos = coldefpos + 1; restpos < list_length(schema);)
{
- ColumnDef *restdef = lfirst(rest);
- ListCell *next = lnext(rest); /* need to save it in case we
- * delete it */
+ ColumnDef *restdef = list_nth_node(ColumnDef, schema, restpos);
if (strcmp(coldef->colname, restdef->colname) == 0)
{
@@ -2110,13 +2114,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
coldef->cooked_default = restdef->cooked_default;
coldef->constraints = restdef->constraints;
coldef->is_from_type = false;
- schema = list_delete_cell(schema, rest, prev);
-
- /*
- * As two elements are merged and one is removed, we
- * should never finish with an empty list.
- */
- Assert(schema != NIL);
+ schema = list_delete_nth_cell(schema, restpos);
}
else
ereport(ERROR,
@@ -2124,8 +2122,8 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
errmsg("column \"%s\" specified more than once",
coldef->colname)));
}
- prev = rest;
- rest = next;
+ else
+ restpos++;
}
}
@@ -7866,7 +7864,8 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
* assess ppeqop or ffeqop, which RI_Initial_Check() does not use.
*/
old_check_ok = (pfeqop == lfirst_oid(old_pfeqop_item));
- old_pfeqop_item = lnext(old_pfeqop_item);
+ old_pfeqop_item = lnext(fkconstraint->old_conpfeqop,
+ old_pfeqop_item);
}
if (old_check_ok)
{
@@ -14619,12 +14618,8 @@ void
AtEOXact_on_commit_actions(bool isCommit)
{
ListCell *cur_item;
- ListCell *prev_item;
-
- prev_item = NULL;
- cur_item = list_head(on_commits);
- while (cur_item != NULL)
+ foreach(cur_item, on_commits)
{
OnCommitItem *oc = (OnCommitItem *) lfirst(cur_item);
@@ -14632,20 +14627,14 @@ AtEOXact_on_commit_actions(bool isCommit)
oc->creating_subid != InvalidSubTransactionId)
{
/* cur_item must be removed */
- on_commits = list_delete_cell(on_commits, cur_item, prev_item);
+ on_commits = foreach_delete_current(on_commits, cur_item);
pfree(oc);
- if (prev_item)
- cur_item = lnext(prev_item);
- else
- cur_item = list_head(on_commits);
}
else
{
/* cur_item must be preserved */
oc->creating_subid = InvalidSubTransactionId;
oc->deleting_subid = InvalidSubTransactionId;
- prev_item = cur_item;
- cur_item = lnext(prev_item);
}
}
}
@@ -14662,24 +14651,16 @@ AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid,
SubTransactionId parentSubid)
{
ListCell *cur_item;
- ListCell *prev_item;
- prev_item = NULL;
- cur_item = list_head(on_commits);
-
- while (cur_item != NULL)
+ foreach(cur_item, on_commits)
{
OnCommitItem *oc = (OnCommitItem *) lfirst(cur_item);
if (!isCommit && oc->creating_subid == mySubid)
{
/* cur_item must be removed */
- on_commits = list_delete_cell(on_commits, cur_item, prev_item);
+ on_commits = foreach_delete_current(on_commits, cur_item);
pfree(oc);
- if (prev_item)
- cur_item = lnext(prev_item);
- else
- cur_item = list_head(on_commits);
}
else
{
@@ -14688,8 +14669,6 @@ AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid,
oc->creating_subid = parentSubid;
if (oc->deleting_subid == mySubid)
oc->deleting_subid = isCommit ? parentSubid : InvalidSubTransactionId;
- prev_item = cur_item;
- cur_item = lnext(prev_item);
}
}
}