postgresql/src/backend/catalog
Tom Lane 1cff1b95ab 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
2019-07-15 13:41:58 -04:00
..
.gitignore Replace our traditional initial-catalog-data format with a better design. 2018-04-08 13:17:27 -04:00
Catalog.pm Create a script that can renumber manually-assigned OIDs. 2019-03-12 10:50:48 -04:00
Makefile Rework the pg_statistic_ext catalog 2019-06-16 01:20:31 +02:00
aclchk.c Fix inconsistencies in the code 2019-07-08 13:15:09 +09:00
catalog.c Fix typos and inconsistencies in code comments 2019-06-14 09:34:34 +09:00
dependency.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
genbki.pl Initial pgperltidy run for v12. 2019-05-22 13:36:19 -04:00
heap.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
index.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
indexing.c Fix unused variable compiler warning in !debug builds. 2019-04-30 17:45:32 -07:00
information_schema.sql Update SQL features/conformance information to SQL:2016 2019-05-14 15:44:37 +02:00
namespace.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
objectaccess.c Update copyright for 2019 2019-01-02 12:44:25 -05:00
objectaddress.c Fix handling of COMMENT for domain constraints 2019-06-12 11:30:11 +09:00
partition.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
pg_aggregate.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
pg_collation.c Collations with nondeterministic comparison 2019-03-22 12:12:43 +01:00
pg_constraint.c Move code for managing PartitionDescs into a new file, partdesc.c 2019-02-21 11:45:02 -05:00
pg_conversion.c tableam: Add and use scan APIs. 2019-03-11 12:46:41 -07:00
pg_db_role_setting.c tableam: Add and use scan APIs. 2019-03-11 12:46:41 -07:00
pg_depend.c Fix ALTER COLUMN TYPE failure with a partial exclusion constraint. 2019-06-12 12:29:39 -04:00
pg_enum.c Remove superfluous tqual.h includes. 2019-01-21 12:15:02 -08:00
pg_inherits.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
pg_largeobject.c Remove superfluous tqual.h includes. 2019-01-21 12:15:02 -08:00
pg_namespace.c Replace uses of heap_open et al with the corresponding table_* function. 2019-01-21 10:51:37 -08:00
pg_operator.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
pg_proc.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
pg_publication.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
pg_range.c Remove superfluous tqual.h includes. 2019-01-21 12:15:02 -08:00
pg_shdepend.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
pg_subscription.c tableam: Add and use scan APIs. 2019-03-11 12:46:41 -07:00
pg_type.c Replace uses of heap_open et al with the corresponding table_* function. 2019-01-21 10:51:37 -08:00
sql_feature_packages.txt > I have installed your patch and adjusted the names of the standards 2004-12-02 22:51:28 +00:00
sql_features.txt Update SQL conformance information about JSON path 2019-06-03 21:36:04 +02:00
storage.c Initial pgindent run for v12. 2019-05-22 12:55:34 -04:00
system_views.sql Further fix privileges on pg_statistic_ext[_data]. 2019-06-16 11:00:23 -04:00
toasting.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00