postgresql/src/include/catalog
Alvaro Herrera 5b01a6f13f
Prevent drop of tablespaces used by partitioned relations
When a tablespace is used in a partitioned relation (per commits
ca4103025d in pg12 for tables and 33e6c34c32 in pg11 for indexes),
it is possible to drop the tablespace, potentially causing various
problems.  One such was reported in bug #16577, where a rewriting ALTER
TABLE causes a server crash.

Protect against this by using pg_shdepend to keep track of tablespaces
when used for relations that don't keep physical files; we now abort a
tablespace if we see that the tablespace is referenced from any
partitioned relations.

Backpatch this to 11, where this problem has been latent all along.  We
don't try to create pg_shdepend entries for existing partitioned
indexes/tables, but any ones that are modified going forward will be
protected.

Note slight behavior change: when trying to drop a tablespace that
contains both regular tables as well as partitioned ones, you'd
previously get ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE and now you'll
get ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST.  Arguably, the latter is more
correct.

It is possible to add protecting pg_shdepend entries for existing
tables/indexes, by doing
  ALTER TABLE ONLY some_partitioned_table SET TABLESPACE pg_default;
  ALTER TABLE ONLY some_partitioned_table SET TABLESPACE original_tablespace;
for each partitioned table/index that is not in the database default
tablespace.  Because these partitioned objects do not have storage, no
file needs to be actually moved, so it shouldn't take more time than
what's required to acquire locks.

This query can be used to search for such relations:
SELECT ... FROM pg_class WHERE relkind IN ('p', 'I') AND reltablespace <> 0

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://postgr.es/m/16577-881633a9f9894fd5@postgresql.org
Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
2021-01-14 15:32:14 -03:00
..
.gitignore Replace our traditional initial-catalog-data format with a better design. 2018-04-08 13:17:27 -04:00
binary_upgrade.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
catalog.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
catversion.h Correctly mark pg_subscription_rel.srsublsn as nullable. 2020-07-20 14:55:56 -04:00
dependency.h Prevent drop of tablespaces used by partitioned relations 2021-01-14 15:32:14 -03:00
duplicate_oids Update copyrights for 2020 2020-01-01 12:21:45 -05:00
genbki.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
heap.h Preserve index data in pg_statistic across REINDEX CONCURRENTLY 2020-11-01 21:24:10 +09:00
index.h Fix possible crash during FATAL exit from reindexing. 2020-04-21 15:58:42 -04:00
indexing.h Invent "trusted" extensions, and remove the pg_pltemplate catalog. 2020-01-29 18:42:43 -05:00
Makefile Update copyrights for 2020 2020-01-01 12:21:45 -05:00
namespace.h Improve performance of "simple expressions" in PL/pgSQL. 2020-03-26 18:58:57 -04:00
objectaccess.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
objectaddress.h Remove utils/acl.h from catalog/objectaddress.h 2020-03-10 10:27:00 +01:00
opfam_internal.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
partition.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_aggregate.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_aggregate.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_am.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_am.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_amop.dat Do pre-release housekeeping on catalog data. 2020-05-12 13:03:43 -04:00
pg_amop.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_amproc.dat Add SQL type xid8 to expose FullTransactionId to users. 2020-04-07 12:03:59 +12:00
pg_amproc.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_attrdef.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_attribute.h Introduce macros for typalign and typstorage constants. 2020-03-04 10:34:25 -05:00
pg_auth_members.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_authid.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_authid.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_cast.dat Add SQL type xid8 to expose FullTransactionId to users. 2020-04-07 12:03:59 +12:00
pg_cast.h Split out CreateCast into src/backend/catalog/pg_cast.c 2020-03-10 11:28:23 -03:00
pg_class.dat Don't require pg_class.dat to contain correct relnatts values. 2020-02-15 14:57:27 -05:00
pg_class.h Don't require pg_class.dat to contain correct relnatts values. 2020-02-15 14:57:27 -05:00
pg_collation.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_collation.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_constraint.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_control.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_conversion.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_conversion.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_database.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_database.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_db_role_setting.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_default_acl.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_depend.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_description.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_enum.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_event_trigger.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_extension.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_foreign_data_wrapper.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_foreign_server.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_foreign_table.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_index.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_inherits.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_init_privs.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_language.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_language.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_largeobject.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_largeobject_metadata.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_namespace.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_namespace.h Remove utils/acl.h from catalog/objectaddress.h 2020-03-10 10:27:00 +01:00
pg_opclass.dat Add SQL type xid8 to expose FullTransactionId to users. 2020-04-07 12:03:59 +12:00
pg_opclass.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_operator.dat Mark factorial operator, and postfix operators in general, as deprecated. 2020-08-30 14:37:24 -04:00
pg_operator.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_opfamily.dat Do pre-release housekeeping on catalog data. 2020-05-12 13:03:43 -04:00
pg_opfamily.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_partitioned_table.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_policy.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_proc.dat Mark factorial operator, and postfix operators in general, as deprecated. 2020-08-30 14:37:24 -04:00
pg_proc.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_publication.h Initial pgindent and pgperltidy run for v13. 2020-05-14 13:06:50 -04:00
pg_publication_rel.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_range.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_range.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_replication_origin.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_rewrite.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_seclabel.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_sequence.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_shdepend.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_shdescription.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_shseclabel.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_statistic.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_statistic_ext.h Initial pgindent and pgperltidy run for v13. 2020-05-14 13:06:50 -04:00
pg_statistic_ext_data.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_subscription.h Correctly mark pg_subscription.subslotname as nullable. 2020-07-19 12:37:23 -04:00
pg_subscription_rel.h Correctly mark pg_subscription_rel.srsublsn as nullable. 2020-07-20 14:55:56 -04:00
pg_tablespace.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_tablespace.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_transform.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_trigger.h Record parents of triggers 2020-02-27 13:23:33 -03:00
pg_ts_config.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_config.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_config_map.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_config_map.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_dict.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_dict.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_parser.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_parser.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_template.dat Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_ts_template.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
pg_type.dat Do pre-release housekeeping on catalog data. 2020-05-12 13:03:43 -04:00
pg_type.h Introduce "anycompatible" family of polymorphic types. 2020-03-19 11:43:11 -04:00
pg_user_mapping.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
reformat_dat_file.pl Update copyrights for 2020 2020-01-01 12:21:45 -05:00
renumber_oids.pl Update copyrights for 2020 2020-01-01 12:21:45 -05:00
storage.h Skip WAL for new relfilenodes, under wal_level=minimal. 2020-04-04 12:25:34 -07:00
storage_xlog.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
toasting.h Invent "trusted" extensions, and remove the pg_pltemplate catalog. 2020-01-29 18:42:43 -05:00
unused_oids Update copyrights for 2020 2020-01-01 12:21:45 -05:00