Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iW7wD-0004fW-TU for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Nov 2019 23:57:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iW7wB-0000yh-2D for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Nov 2019 23:57:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iW7wA-0000ya-MW for pgsql-bugs@lists.postgresql.org; Sat, 16 Nov 2019 23:57:06 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iW7w7-0003I5-6n for pgsql-bugs@lists.postgresql.org; Sat, 16 Nov 2019 23:57:06 +0000 Received: by mail-wr1-x444.google.com with SMTP id i12so15269027wro.5 for ; Sat, 16 Nov 2019 15:57:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=O1Ov+p5GqiqtK4O6t6ygKIYrdrIT1/J+ZPzVJyr7Xog=; b=u+fdRa6pE5icrlhaOlo3IO1DjPYsk2F2k0hv1+jJacQPQ4Qs8BJJ0xcC6sEfUmu87T 7xBgf0wJ9dDTw1eSA2Wrw6QvLvBdsWim9XIudRpC7fp2PgbhVgEpDSyzQU+wO7qbMD3M fOf1+cjKschQmyMBYHtFJ9TJXa9iRngoVrLbNGGTkJIUFjpj6zo7U/cxjwuCFoVv98EV 2/BJuH1b19sLMoLUokoykYg94iXPhxoY/7nk0p9N1WiNgGWkXfA351LoMEIJj8X4f+I+ X6oo3kysnlqo3W4MHqkT1g01cV4egwl7GMAmwew8/dFZtpr2pCteHXHh5Br/Sktb8Kdo E8cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=O1Ov+p5GqiqtK4O6t6ygKIYrdrIT1/J+ZPzVJyr7Xog=; b=HtfQ7V/o253yRMKg2FngSRcLitWZP9zDwO3gF6eA16X15SIr3x1eDOgl+GDuod1Xlb M3VCam/SgzrwnOFgLQM8efEvca0gUXG72LEw7JQJPKUGM67pn/w+/oHmnpNLcoQXBjNh J3Tbb2penyzwTyw8Qf48gPbrXu/cc9Oh4COuoEYu2KsomFxg3s2ee7XQu3RhuYHvCcA7 C4tw7EbtWtsk13cJIouE+ZZn/LABrhviRIE30Jexon0KhQGuSRfe8Mh1YUXx/SHHOTeZ KlOt3fjteEXg65DGiBh+jsD72x1byQqaXqf2bCGedVutWgAD0ljbGzKVwKVNO1m2cTj8 R3PA== X-Gm-Message-State: APjAAAXSKjZBNXjTGof4MetODjKre+zj4iw06ZBbihtypWvhxmv2HWfM Rm/pODSH3dsuOG8w0qaSNQEMeg== X-Google-Smtp-Source: APXvYqzu0RKOoEp70TYQLkxJBtMS+J2pKZanDB6TpbsmLKDvJX7l1of5sg0xeBuWKj9L3DhnD45MwQ== X-Received: by 2002:adf:e942:: with SMTP id m2mr24449541wrn.26.1573948620007; Sat, 16 Nov 2019 15:57:00 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id 11sm15894017wmb.34.2019.11.16.15.56.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Nov 2019 15:56:58 -0800 (PST) Date: Sun, 17 Nov 2019 00:56:57 +0100 From: Tomas Vondra To: Manuel Rigger Cc: PostgreSQL mailing lists Subject: Re: ALTER TABLE results in "could not find cast from 3904 to 3831" Message-ID: <20191116235657.4ratlshjd7dt3svw@development> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sat, Nov 16, 2019 at 11:46:02PM +0100, Manuel Rigger wrote: >Hi everyone, > >Consider the following test case: > >CREATE TABLE t0(c0 int4range UNIQUE, FOREIGN KEY (c0) REFERENCES t0(c0)); >ALTER TABLE t0 ALTER c0 SET DATA TYPE int4range; -- ERROR: could not >find cast from 3904 to 3831 > >Unexpectedly, the ALTER TABLE results in an error. If, for example, >the references to the type "int4range" are replaced by "int", the two >statements execute without error. > Thanks for the report. You haven't mentioned which PostgreSQL version you've tried this on, but I can reproduce it on 12 and I support it affects older versions too (haven't checked though). This seems to be essentially an omission in how we handle polymorphic types in ATAddForeignKeyConstraint(). That is, the same issue aplies to int4[] for example: CREATE TABLE t1(c0 int4[] UNIQUE, FOREIGN KEY (c0) REFERENCES t1(c0)); ALTER TABLE t1 ALTER c0 SET DATA TYPE int4[]; ERROR: could not find cast from 1007 to 2277 There's actually this comment /* * Otherwise, look for an implicit cast from the FK type to the * opcintype, and if found, use the primary equality operator. * This is a bit tricky because opcintype might be a polymorphic * type such as ANYARRAY or ANYENUM; so what we have to test is * whether the two actual column types can be concurrently cast to * that type. (Otherwise, we'd fail to reject combinations such * as int[] and point[].) */ Oid input_typeids[2]; Oid target_typeids[2]; input_typeids[0] = pktype; input_typeids[1] = fktype; target_typeids[0] = opcintype; target_typeids[1] = opcintype; if (can_coerce_type(2, input_typeids, target_typeids, COERCION_IMPLICIT)) { pfeqop = ffeqop = ppeqop; pfeqop_right = opcintype; } And opcintype = 3831 (which is anyrange). We do get it here because the opclass is defined like this: { oid => '3882', descr => 'equal', oprname => '=', oprcanmerge => 't', oprcanhash => 't', oprleft => 'anyrange', oprright => 'anyrange', oprresult => 'bool', oprcom => '=(anyrange,anyrange)', oprnegate => '<>(anyrange,anyrange)', oprcode => 'range_eq', oprrest => 'eqsel', oprjoin => 'eqjoinsel' }, i.e. it's a generic opclass using 'anyrange'. And can_coerce_type() does return true, which seems fine according to the comment. But then a couple lines down we do this: old_pathtype = findFkeyCast(pfeqop_right, old_fktype, &old_castfunc); which internally calls find_coercion_pathway() and that has a comment saying this: /* * find_coercion_pathway * Look for a coercion pathway between two types. * * Currently, this deals only with scalar-type cases; it does not consider * polymorphic types nor casts between composite types. (Perhaps fold * those in someday?) ... */ So this code does not really work for polymorphic types, I'm afraid. I'm not sure how much new code would be needed. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services