pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: holly.roberts@starlingbank.com
Subject: BUG #17409: Unable to alter data type of clustered column which is referenced by foreign key
Date: Wed, 16 Feb 2022 14:38:55 +0000
Message-ID: <17409-52871dda8b5741cb@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      17409
Logged by:          Holly Roberts
Email address:      holly.roberts@starlingbank.com
PostgreSQL version: 14.2
Operating system:   Debian 10.2.1-6
Description:        

When attempting to change the data type of a column that has previously been
clustered on, which is also referenced by a foreign key, then an exception
is thrown.

Reproduction steps using a fresh database:
	CREATE TABLE parent (
		parent_field INTEGER CONSTRAINT pk_parent PRIMARY KEY
	);
	CREATE TABLE child (
		child_field INTEGER,
		CONSTRAINT fk_child FOREIGN KEY (child_field) REFERENCES parent
(parent_field)
	);
	CLUSTER parent USING pk_parent;
	ALTER TABLE parent ALTER COLUMN parent_field SET DATA TYPE BIGINT;

This throws the following error:
	ERROR:  relation 16458 has multiple clustered indexes
	'SELECT 16458::regclass' returns 'parent';
This has previously worked on various versions of postgres 12 and 13 for me
(latest tried 13.6)

I have reproduced the following on both 14.2 and 14.0, my postgres version
is as follows:
	SELECT version();
	PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled
by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

Some minor observations:
- Removing the FK allows the data type to be changed
- Occurs for both primary key/unique index
- Occurs on other data types (eg. going from TEXT to BIGINT with 'USING')

Looking through pg_catalog I can also see that only one index is clustered
as would be expected:
	SELECT
	  table_cls.relnamespace::regnamespace::text AS schema,
	  table_cls.relname AS table,
	  index_cls.relname AS index,
	  indisclustered
	FROM pg_index pi
	INNER JOIN pg_class index_cls ON (pi.indexrelid = index_cls.oid)
	INNER JOIN pg_class table_cls ON (pi.indrelid = table_cls.oid)
	WHERE (table_cls.relnamespace::regnamespace::text, table_cls.relname) =
('public', 'parent');

	 schema | table  |   index   | indisclustered
	--------+--------+-----------+----------------
	 public | parent | pk_parent | t
	(1 row)

Many Thanks,
Holly Roberts



view thread (16+ messages)  latest in thread

Message-ID: <17409-52871dda8b5741cb@postgresql.org>
Permalink:  ../17409-52871dda8b5741cb@postgresql.org/
Also on:    postgresql.org/message-id/17409-52871dda8b5741cb@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, holly.roberts@starlingbank.com
  Subject: Re: BUG #17409: Unable to alter data type of clustered column which is referenced by foreign key
  In-Reply-To: <17409-52871dda8b5741cb@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox