postgres/contrib
Thomas Munro 38c579b089 Fix corruption when relation truncation fails.
RelationTruncate() does three things, while holding an
AccessExclusiveLock and preventing checkpoints:

1. Logs the truncation.
2. Drops buffers, even if they're dirty.
3. Truncates some number of files.

Step 2 could previously be canceled if it had to wait for I/O, and step
3 could and still can fail in file APIs.  All orderings of these
operations have data corruption hazards if interrupted, so we can't give
up until the whole operation is done.  When dirty pages were discarded
but the corresponding blocks were left on disk due to ERROR, old page
versions could come back from disk, reviving deleted data (see
pgsql-bugs #18146 and several like it).  When primary and standby were
allowed to disagree on relation size, standbys could panic (see
pgsql-bugs #18426) or revive data unknown to visibility management on
the primary (theorized).

Changes:

 * WAL is now unconditionally flushed first
 * smgrtruncate() is now called in a critical section, preventing
   interrupts and causing PANIC on file API failure
 * smgrtruncate() has a new parameter for existing fork sizes,
   because it can't call smgrnblocks() itself inside a critical section

The changes apply to RelationTruncate(), smgr_redo() and
pg_truncate_visibility_map().  That last is also brought up to date with
other evolutions of the truncation protocol.

The VACUUM FileTruncate() failure mode had been discussed in older
reports than the ones referenced below, with independent analysis from
many people, but earlier theories on how to fix it were too complicated
to back-patch.  The more recently invented cancellation bug was
diagnosed by Alexander Lakhin.  Other corruption scenarios were spotted
by me while iterating on this patch and earlier commit 75818b3a.

Back-patch to all supported releases.

Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Reported-by: rootcause000@gmail.com
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://postgr.es/m/18146-04e908c662113ad5%40postgresql.org
Discussion: https://postgr.es/m/18426-2d18da6586f152d6%40postgresql.org
2024-12-20 23:57:02 +13:00
..
amcheck Introduce CompactAttribute array in TupleDesc, take 2 2024-12-20 22:31:26 +13:00
auth_delay Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
auto_explain Simplify executor's determination of whether to use parallelism. 2024-12-09 14:38:19 -05:00
basebackup_to_shell Update copyright for 2024 2024-01-03 20:49:05 -05:00
basic_archive Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
bloom Remove useless casts to (void *) 2024-11-28 08:27:20 +01:00
bool_plperl Update copyright for 2024 2024-01-03 20:49:05 -05:00
btree_gin Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
btree_gist Remove useless casts to (void *) 2024-11-28 08:27:20 +01:00
citext Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
cube Prevent redeclaration of typedef yyscan_t 2024-12-19 11:24:43 +01:00
dblink Don't bother checking the result of SPI_connect[_ext] anymore. 2024-09-09 12:18:34 -04:00
dict_int Update copyright for 2024 2024-01-03 20:49:05 -05:00
dict_xsyn Remove ts_locale.c's lowerstr() 2024-12-17 14:04:55 +01:00
earthdistance contrib/earthdistance: Use SQL-standard function bodies. 2024-12-14 16:07:18 -05:00
file_fdw Remove useless casts to (void *) 2024-11-28 08:27:20 +01:00
fuzzystrmatch Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
hstore Remove useless casts to (void *) 2024-11-28 08:27:20 +01:00
hstore_plperl Update copyright for 2024 2024-01-03 20:49:05 -05:00
hstore_plpython Update copyright for 2024 2024-01-03 20:49:05 -05:00
intagg Update copyright for 2024 2024-01-03 20:49:05 -05:00
intarray Remove useless casts to (void *) 2024-11-28 08:27:20 +01:00
isn Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
jsonb_plperl Update copyright for 2024 2024-01-03 20:49:05 -05:00
jsonb_plpython Update copyright for 2024 2024-01-03 20:49:05 -05:00
lo contrib/lo: Use SQL-standard function bodies 2024-11-14 13:23:11 +09:00
ltree Remove ts_locale.c's t_isdigit(), t_isspace(), t_isprint() 2024-12-17 12:52:29 +01:00
ltree_plpython Update copyright for 2024 2024-01-03 20:49:05 -05:00
oid2name Apply more quoting to GUC names in messages 2024-09-04 13:50:44 +09:00
pageinspect Introduce CompactAttribute array in TupleDesc, take 2 2024-12-20 22:31:26 +13:00
passwordcheck Deprecate MD5 passwords. 2024-12-02 13:30:07 -06:00
pg_buffercache Add pg_buffercache_evict() function for testing. 2024-04-08 16:23:40 +12:00
pg_freespacemap pg_freespacemap: Use SQL-standard function bodies 2024-11-12 17:28:03 +09:00
pg_logicalinspect Add contrib/pg_logicalinspect. 2024-10-14 17:22:02 -07:00
pg_prewarm Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
pg_stat_statements Fix further fallout from EXPLAIN ANALYZE BUFFERS change 2024-12-12 09:50:00 +13:00
pg_surgery Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
pg_trgm Remove ts_locale.c's lowerstr() 2024-12-17 14:04:55 +01:00
pg_visibility Fix corruption when relation truncation fails. 2024-12-20 23:57:02 +13:00
pg_walinspect Assign error codes where missing for user-facing failures 2024-07-04 09:48:40 +09:00
pgcrypto Remove useless casts to (void *) 2024-11-28 08:27:20 +01:00
pgrowlocks Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
pgstattuple Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
postgres_fdw Introduce CompactAttribute array in TupleDesc, take 2 2024-12-20 22:31:26 +13:00
seg Prevent redeclaration of typedef yyscan_t 2024-12-19 11:24:43 +01:00
sepgsql Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
spi Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
sslinfo Revert "Add notBefore and notAfter to SSL cert info display" 2024-03-22 22:58:41 +01:00
start-scripts Remove gratuitous references to postmaster program 2023-01-26 10:48:32 +01:00
tablefunc Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
tcn Update copyright for 2024 2024-01-03 20:49:05 -05:00
test_decoding Fix invalidation of local pgstats references for entry reinitialization 2024-12-09 10:45:28 +09:00
tsm_system_rows Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
tsm_system_time Remove unused #include's from contrib, pl, test .c files 2024-10-28 08:02:17 +01:00
unaccent Remove ts_locale.c's t_isdigit(), t_isspace(), t_isprint() 2024-12-17 12:52:29 +01:00
uuid-ossp Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
vacuumlo Apply more quoting to GUC names in messages 2024-09-04 13:50:44 +09:00
xml2 xml2: Add tests for functions xpath_nodeset() and xpath_list() 2024-11-14 13:10:36 +09:00
contrib-global.mk
Makefile Add contrib/pg_logicalinspect. 2024-10-14 17:22:02 -07:00
meson.build Add contrib/pg_logicalinspect. 2024-10-14 17:22:02 -07:00
README

The PostgreSQL contrib tree
---------------------------

This subtree contains porting tools, analysis utilities, and plug-in
features that are not part of the core PostgreSQL system, mainly
because they address a limited audience or are too experimental to be
part of the main source tree.  This does not preclude their
usefulness.

User documentation for each module appears in the main SGML
documentation.

When building from the source distribution, these modules are not
built automatically, unless you build the "world" target.  You can
also build and install them all by running "make all" and "make
install" in this directory; or to build and install just one selected
module, do the same in that module's subdirectory.

Some directories supply new user-defined functions, operators, or
types.  To make use of one of these modules, after you have installed
the code you need to register the new SQL objects in the database
system by executing a CREATE EXTENSION command.  In a fresh database,
you can simply do

    CREATE EXTENSION module_name;

See the PostgreSQL documentation for more information about this
procedure.