postgresql/src/backend/commands
Alvaro Herrera d0054432d4
Fix ALTER TABLE DETACH for inconsistent indexes
When a partitioned table has an index that doesn't support a constraint,
but a partition has an equivalent index that does, then a DETACH
operation would misbehave: a crash in assertion-enabled systems (because
we fail to find the constraint in the parent that we expect to), or a
broken coninhcount value (-1) in production systems (because we blindly
believe that we've successfully detached the parent).

While we should reject an ATTACH of a partition with such an index, we
have failed to do so in existing releases, so adding an error in stable
releases might break the (unlikely) existing applications that rely on
this behavior.  At this point I don't even want to reject them in
master, because it'd break pg_upgrade if such databases exist, and there
would be no easy way to fix existing databases without expensive index
rebuilds.

(Later on we could add ALTER TABLE ... ADD CONSTRAINT USING INDEX to
partitioned tables, which would allow the user to fix such patterns.  At
that point we could add more restrictions to prevent the problem from
its root.)

Also, add a test case that leaves one table in this condition, so that
we can verify that pg_upgrade continues to work if we later decide to
change the policy on the master branch.

Backpatch to all supported branches.

Co-authored-by: Tender Wang <tndrwang@gmail.com>
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/18500-62948b6fe5522f56@postgresql.org
2024-07-12 12:54:01 +02:00
..
aggregatecmds.c Implement OR REPLACE option for CREATE AGGREGATE. 2019-03-19 01:16:50 +00:00
alter.c Fix bugs in manipulation of large objects. 2023-12-15 13:55:05 -05:00
amcmds.c Initial pgindent run for v12. 2019-05-22 12:55:34 -04:00
analyze.c Ensure acquire_inherited_sample_rows sets its output parameters. 2023-03-31 10:08:40 -04:00
async.c Preserve CurrentMemoryContext across notify and sinval interrupts. 2024-07-01 12:21:07 -04:00
cluster.c Make relation-enumerating operations be security-restricted operations. 2022-05-09 08:35:12 -07:00
collationcmds.c Disallow creating an ICU collation if the DB encoding won't support it. 2021-09-03 16:38:55 -04:00
comment.c Remove superfluous tqual.h includes. 2019-01-21 12:15:02 -08:00
constraint.c Fix bogus CALLED_AS_TRIGGER() defenses. 2020-04-03 11:24:56 -04:00
conversioncmds.c Update copyright for 2019 2019-01-02 12:44:25 -05:00
copy.c Ensure we preprocess expressions before checking their volatility. 2023-11-16 10:05:14 -05:00
createas.c In extensions, don't replace objects not belonging to the extension. 2022-08-08 11:12:31 -04:00
dbcommands.c Handle DROP DATABASE getting interrupted 2023-07-13 13:03:36 -07:00
define.c Update copyright for 2019 2019-01-02 12:44:25 -05:00
discard.c Update copyright for 2019 2019-01-02 12:44:25 -05:00
dropcmds.c Revert "Forbid DROP SCHEMA on temporary namespaces" 2020-01-08 10:36:22 +09:00
event_trigger.c Fix use-after-free bug with event triggers and ALTER TABLE. 2020-10-27 15:37:13 -04:00
explain.c Fix index-only scan plans, take 2. 2022-01-03 15:42:27 -05:00
extension.c Reject substituting extension schemas or owners matching ["$'\]. 2023-08-07 06:06:00 -07:00
foreigncmds.c Silence compiler warning in release 11 and 12 branches 2023-08-27 07:04:23 -04:00
functioncmds.c Fix handling of polymorphic output arguments for procedures. 2024-05-14 20:19:20 -04:00
indexcmds.c Lock before setting relhassubclass on RELKIND_PARTITIONED_INDEX. 2024-06-27 19:21:13 -07:00
lockcmds.c Revert "Accept relations of any kind in LOCK TABLE". 2020-11-06 16:17:57 -05:00
Makefile Move vacuumlazy.c into access/heap. 2019-01-15 12:06:19 -08:00
matview.c Fix assertion if index is dropped during REFRESH CONCURRENTLY 2024-02-05 11:04:23 +02:00
opclasscmds.c Fix DDL deparse of CREATE OPERATOR CLASS 2022-05-20 18:52:55 +02:00
operatorcmds.c Make contrib modules' installation scripts more secure. 2020-08-10 10:44:42 -04:00
policy.c Remove unnecessary failure cases in RemoveRoleFromObjectPolicy(). 2021-06-25 13:59:38 -04:00
portalcmds.c Fix some anomalies with NO SCROLL cursors. 2021-09-10 13:18:32 -04:00
prepare.c Fix plancache refcount leak after error in ExecuteQuery. 2021-06-16 19:30:17 -04:00
proclang.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
publicationcmds.c Invalidate relcache for publications defined for all tables. 2021-09-08 12:16:15 +05:30
schemacmds.c Replace last PushOverrideSearchPath() call with set_config_option(). 2023-05-08 06:14:12 -07:00
seclabel.c Check criticalSharedRelcachesBuilt in GetSharedSecurityLabel(). 2021-10-14 12:25:07 -07:00
sequence.c Fix pg_sequence_last_value() for unlogged sequences on standbys. 2024-05-13 15:54:23 -05:00
statscmds.c Lock table in DROP STATISTICS 2023-11-19 21:04:47 +01:00
subscriptioncmds.c Fix the errhint message and docs for drop subscription failure. 2023-06-21 10:20:35 +05:30
tablecmds.c Fix ALTER TABLE DETACH for inconsistent indexes 2024-07-12 12:54:01 +02:00
tablespace.c Fix replay of create database records on standby 2022-07-28 08:26:05 +02:00
trigger.c Don't throw an error if a queued AFTER trigger no longer exists. 2024-06-20 14:21:36 -04:00
tsearchcmds.c Fix various issues with ALTER TEXT SEARCH CONFIGURATION 2024-01-31 13:16:50 +09:00
typecmds.c Avoid scribbling on input node tree in CREATE/ALTER DOMAIN. 2021-06-18 12:09:22 -04:00
user.c Change some errdetail() to errdetail_internal() 2022-09-28 17:14:53 +02:00
vacuum.c Close race condition between datfrozen and relfrozen updates. 2024-04-29 10:25:00 -07:00
variable.c Fix some inappropriately-disallowed uses of ALTER ROLE/DATABASE SET. 2021-04-13 15:10:18 -04:00
view.c In extensions, don't replace objects not belonging to the extension. 2022-08-08 11:12:31 -04:00