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 1hjWba-0004ht-3C for pgsql-bugs@arkaria.postgresql.org; Fri, 05 Jul 2019 22:22:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hjWbY-0004wl-LN for pgsql-bugs@arkaria.postgresql.org; Fri, 05 Jul 2019 22:22:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjWbY-0004FZ-AP for pgsql-bugs@lists.postgresql.org; Fri, 05 Jul 2019 22:22:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjWbA-0001oS-RZ for pgsql-bugs@lists.postgresql.org; Fri, 05 Jul 2019 22:22:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x65MMQKU004710; Fri, 5 Jul 2019 18:22:26 -0400 From: Tom Lane To: Manuel Rigger cc: PostgreSQL mailing lists Subject: Re: DISCARD TEMP results in "ERROR: cache lookup failed for type 0" In-reply-to: References: Comments: In-reply-to Manuel Rigger message dated "Thu, 04 Jul 2019 14:59:37 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4708.1562365346.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 05 Jul 2019 18:22:26 -0400 Message-ID: <4709.1562365346@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Manuel Rigger writes: > CREATE TEMP TABLE t0(c0 INT GENERATED ALWAYS AS IDENTITY) PARTITION BY > HASH((t0.c0)); > VACUUM FULL; > DISCARD TEMP; -- unexpected: ERROR: cache lookup failed for type 0 Blech. The proximate cause is fairly obvious from poking around with a debugger: #0 errfinish (dummy=3D0) at elog.c:414 #1 0x00000000008a2694 in elog_finish (elevel=3D, = fmt=3D) at elog.c:1376 #2 0x00000000008898ae in get_typlenbyvalalign (typid=3D0, typlen=3D0x2d42= 118, = typbyval=3D0x2d42138, typalign=3D0x2d42158 "") at lsyscache.c:2057 #3 0x000000000088c2e0 in RelationBuildPartitionKey (relation=3D0x7f727c58= 3a80) at partcache.c:233 get_typlenbyvalalign(key->parttypid[i], ...); #4 0x00000000008957a7 in RelationBuildDesc ( targetRelId=3D, insertIt=3Dtrue) at relcache.c:11= 89 #5 0x0000000000897ce6 in RelationIdGetRelation (relationId=3D53072) at relcache.c:1956 #6 0x00000000004ca6b0 in relation_open (relationId=3D53072, = lockmode=3D) at heapam.c:1135 #7 0x0000000000535c89 in heap_drop_with_catalog (relid=3D53072) at heap.c= :1828 #8 0x0000000000533074 in doDeletion (object=3D0x2d4dfe4, depRel=3D0x7ffd1= d23b3d8, = flags=3D) at dependency.c:1134 #9 deleteOneObject (object=3D0x2d4dfe4, depRel=3D0x7ffd1d23b3d8, = flags=3D) at dependency.c:1036 #10 0x000000000053310f in deleteObjectsInList (targetObjects=3D0x2c367b8, = depRel=3D0x7ffd1d23b3d8, flags=3D29) at dependency.c:260 #11 0x0000000000533328 in performDeletion (object=3D0x7ffd1d23b410, = behavior=3DDROP_CASCADE, flags=3D29) at dependency.c:341 #12 0x000000000053d283 in RemoveTempRelations () at namespace.c:4140 ... (gdb) f 3 (gdb) p *relation->rd_att $2 =3D {natts =3D 1, tdtypeid =3D 53074, tdtypmod =3D -1, tdhasoid =3D fal= se, = tdrefcount =3D 1, constr =3D 0x0, attrs =3D 0x7f727c6f68e0} (gdb) p *relation->rd_att->attrs $3 =3D {attrelid =3D 53072, attname =3D { data =3D "........pg.dropped.1........", '\000' }, = atttypid =3D 0, attstattarget =3D 0, attlen =3D 4, attnum =3D 1, attndim= s =3D 0, = attcacheoff =3D 0, atttypmod =3D -1, attbyval =3D true, attstorage =3D 1= 12 'p', = attalign =3D 105 'i', attnotnull =3D false, atthasdef =3D false, = atthasmissing =3D false, attidentity =3D 97 'a', attisdropped =3D true, = attislocal =3D true, attinhcount =3D 0, attcollation =3D 0} So we're getting the failure because, when we come to delete the whole temp table, its partitioning column has already been dropped, causing RelationBuildPartitionKey (and hence any relation-opening operation) to fail. That means that order-of-operations in dependency.c is at the root of the issue. The relevant pg_depend entries are (your OIDs will vary): # select objid, pg_describe_object(classid,objid,objsubid) as obj, pg_describe_object(refclassid,refobjid,refobjsubid) as ref, deptype from pg_depend where ...; objid | obj | ref | deptype = -------+--------------------+-----------------------+--------- 53071 | type t0_c0_seq | sequence t0_c0_seq | i 53070 | sequence t0_c0_seq | schema pg_temp_3 | n 53074 | type t0 | table t0 | i 53073 | type t0[] | type t0 | i 53072 | table t0 | schema pg_temp_3 | n 53070 | sequence t0_c0_seq | column c0 of table t0 | i (6 rows) deleteObjectsInList's target list is (gdb) p *targetObjects $1 =3D {refs =3D 0x2d87450, extras =3D 0x2d2c8f8, numrefs =3D 7, maxrefs =3D= 32} (gdb) p targetObjects->refs[0] $2 =3D {classId =3D 1247, objectId =3D 53071, objectSubId =3D 0} (gdb) p targetObjects->refs[1] $3 =3D {classId =3D 1259, objectId =3D 53070, objectSubId =3D 0} (gdb) p targetObjects->refs[2] $4 =3D {classId =3D 1259, objectId =3D 53072, objectSubId =3D 1} (gdb) p targetObjects->refs[3] $5 =3D {classId =3D 1247, objectId =3D 53073, objectSubId =3D 0} (gdb) p targetObjects->refs[4] $6 =3D {classId =3D 1247, objectId =3D 53074, objectSubId =3D 0} (gdb) p targetObjects->refs[5] $7 =3D {classId =3D 1259, objectId =3D 53072, objectSubId =3D 0} (gdb) p targetObjects->refs[6] $8 =3D {classId =3D 2615, objectId =3D 16982, objectSubId =3D 0} So what's evidently happening is that it lands on the sequence first and follows that to column c0, making a deletion target entry (refs[2]) for that column, and only afterwards finds the whole table and makes a deletion target entry for the table (refs[5]). The behavior is dependent on the order of entries in pg_depend, so you might or might not see it (and that's why the VACUUM FULL contributes; it's changing the entry order). The example doesn't reproduce in v12/HEAD, but I fear that that is only a cosmetic side-effect of commit f1ad067fc (Sort the dependent objects before recursing in findDependentObjects()). [ pokes at it... ] Yup, you can break it in HEAD too, if you arrange for the sequence and the table to have different relative OIDs: regression=3D# create temp table t0(c0 int not null) partition by range(c0= ); CREATE TABLE regression=3D# alter table t0 alter column c0 add generated always as iden= tity; ALTER TABLE regression=3D# select objid, pg_describe_object(classid,objid,objsubid) as= obj, pg_describe_object(refclassid,refobjid,refobjsubid) as ref, deptype = from pg_depend where ... objid | obj | ref | deptype = -------+--------------------+-----------------------+--------- 43530 | type t0 | table t0 | i 43529 | type t0[] | type t0 | i 43528 | table t0 | schema pg_temp_3 | n 43534 | type t0_c0_seq | sequence t0_c0_seq | i 43533 | sequence t0_c0_seq | schema pg_temp_3 | n 43533 | sequence t0_c0_seq | column c0 of table t0 | i (6 rows) regression=3D# discard temp; psql: ERROR: cache lookup failed for type 0 ISTM the real fix here probably involves having some explicit dependencies between the table and its partitioning columns, rather than relying on this check: regression=3D# alter table t0 drop column c0; psql: ERROR: cannot drop column named in partition key regression=3D# \errverbose = psql: error: ERROR: 42P16: cannot drop column named in partition key LOCATION: ATExecDropColumn, tablecmds.c:7041 which is evidently not even in the right place to guard this with any security. With an explicit dependency, we could force a drop of a partitioning column to be turned into a drop of the whole table. Without one, well, you can break it really easily: regression=3D# create domain d1 as int; CREATE DOMAIN regression=3D# create table t1 (f1 d1) partition by range(f1); CREATE TABLE regression=3D# drop domain d1 cascade; psql: NOTICE: drop cascades to column f1 of table t1 DROP DOMAIN regression=3D# \d t1 psql: ERROR: cache lookup failed for type 0 However, we can only apply a fix like that in HEAD and maybe v12; it seems too late to be adding such dependencies in v10/v11. Not sure what to do about those branches. regards, tom lane