CLUSTER specified the wrong namespace when renaming toast tables of temporary
authorTom Lane <[email protected]>
Tue, 2 Feb 2010 19:12:29 +0000 (19:12 +0000)
committerTom Lane <[email protected]>
Tue, 2 Feb 2010 19:12:29 +0000 (19:12 +0000)
relations (they don't live in pg_toast).  This caused an Assert failure in
assert-enabled builds.  So far as I can see, in a non-assert build it would
only have messed up the checks for conflicting names, so a failure would be
quite improbable but perhaps not impossible.

src/backend/commands/cluster.c
src/test/regress/expected/cluster.out
src/test/regress/sql/cluster.sql

index b3a1af7cb94df7f726068f7bc669467c51262081..6216896d4a269474223a08187498c83f40e4c34c 100644 (file)
@@ -657,20 +657,25 @@ rebuild_relation(Relation OldHeap, Oid indexOid,
    newrel = heap_open(tableOid, NoLock);
    if (OidIsValid(newrel->rd_rel->reltoastrelid))
    {
-       char        NewToastName[NAMEDATALEN];
        Relation    toastrel;
+       Oid         toastidx;
+       Oid         toastnamespace;
+       char        NewToastName[NAMEDATALEN];
+
+       toastrel = relation_open(newrel->rd_rel->reltoastrelid, AccessShareLock);
+       toastidx = toastrel->rd_rel->reltoastidxid;
+       toastnamespace = toastrel->rd_rel->relnamespace;
+       relation_close(toastrel, AccessShareLock);
 
        /* rename the toast table ... */
        snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u", tableOid);
        RenameRelationInternal(newrel->rd_rel->reltoastrelid, NewToastName,
-                              PG_TOAST_NAMESPACE);
+                              toastnamespace);
 
        /* ... and its index too */
-       toastrel = relation_open(newrel->rd_rel->reltoastrelid, AccessShareLock);
        snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index", tableOid);
-       RenameRelationInternal(toastrel->rd_rel->reltoastidxid, NewToastName,
-                              PG_TOAST_NAMESPACE);
-       relation_close(toastrel, AccessShareLock);
+       RenameRelationInternal(toastidx, NewToastName,
+                              toastnamespace);
    }
    relation_close(newrel, NoLock);
 }
index 9bf4764b689f8c2b3d4da03e09d02bb4f8327a53..23d0a3cd509ccc5ba0f54740cdb0732076d13a66 100644 (file)
@@ -434,6 +434,18 @@ SELECT * FROM clustertest;
  100
 (5 rows)
 
+-- check that temp tables can be clustered
+create temp table clstr_temp (col1 int primary key, col2 text);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "clstr_temp_pkey" for table "clstr_temp"
+insert into clstr_temp values (2, 'two'), (1, 'one');
+cluster clstr_temp using clstr_temp_pkey;
+select * from clstr_temp;
+ col1 | col2 
+------+------
+    1 | one
+    2 | two
+(2 rows)
+
 -- clean up
 \c -
 DROP TABLE clustertest;
index a54d6e07f50cde7c562e0f4ea1ca76b5818aaf1f..f3f7a248100975bbcc14f91d515e4c3dc89a1810 100644 (file)
@@ -187,6 +187,12 @@ COMMIT;
 
 SELECT * FROM clustertest;
 
+-- check that temp tables can be clustered
+create temp table clstr_temp (col1 int primary key, col2 text);
+insert into clstr_temp values (2, 'two'), (1, 'one');
+cluster clstr_temp using clstr_temp_pkey;
+select * from clstr_temp;
+
 -- clean up
 \c -
 DROP TABLE clustertest;