Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c98157d

Browse files
committed
CLUSTER specified the wrong namespace when renaming toast tables of temporary
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.
1 parent 0a27347 commit c98157d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/backend/commands/cluster.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.195 2010/01/28 23:21:11 petere Exp $
14+
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.196 2010/02/02 19:12:29 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -657,20 +657,25 @@ rebuild_relation(Relation OldHeap, Oid indexOid,
657657
newrel = heap_open(tableOid, NoLock);
658658
if (OidIsValid(newrel->rd_rel->reltoastrelid))
659659
{
660-
char NewToastName[NAMEDATALEN];
661660
Relation toastrel;
661+
Oid toastidx;
662+
Oid toastnamespace;
663+
char NewToastName[NAMEDATALEN];
664+
665+
toastrel = relation_open(newrel->rd_rel->reltoastrelid, AccessShareLock);
666+
toastidx = toastrel->rd_rel->reltoastidxid;
667+
toastnamespace = toastrel->rd_rel->relnamespace;
668+
relation_close(toastrel, AccessShareLock);
662669

663670
/* rename the toast table ... */
664671
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u", tableOid);
665672
RenameRelationInternal(newrel->rd_rel->reltoastrelid, NewToastName,
666-
PG_TOAST_NAMESPACE);
673+
toastnamespace);
667674

668675
/* ... and its index too */
669-
toastrel = relation_open(newrel->rd_rel->reltoastrelid, AccessShareLock);
670676
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index", tableOid);
671-
RenameRelationInternal(toastrel->rd_rel->reltoastidxid, NewToastName,
672-
PG_TOAST_NAMESPACE);
673-
relation_close(toastrel, AccessShareLock);
677+
RenameRelationInternal(toastidx, NewToastName,
678+
toastnamespace);
674679
}
675680
relation_close(newrel, NoLock);
676681
}

src/test/regress/expected/cluster.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,18 @@ SELECT * FROM clustertest;
434434
100
435435
(5 rows)
436436

437+
-- check that temp tables can be clustered
438+
create temp table clstr_temp (col1 int primary key, col2 text);
439+
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "clstr_temp_pkey" for table "clstr_temp"
440+
insert into clstr_temp values (2, 'two'), (1, 'one');
441+
cluster clstr_temp using clstr_temp_pkey;
442+
select * from clstr_temp;
443+
col1 | col2
444+
------+------
445+
1 | one
446+
2 | two
447+
(2 rows)
448+
437449
-- clean up
438450
\c -
439451
DROP TABLE clustertest;

src/test/regress/sql/cluster.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ COMMIT;
187187

188188
SELECT * FROM clustertest;
189189

190+
-- check that temp tables can be clustered
191+
create temp table clstr_temp (col1 int primary key, col2 text);
192+
insert into clstr_temp values (2, 'two'), (1, 'one');
193+
cluster clstr_temp using clstr_temp_pkey;
194+
select * from clstr_temp;
195+
190196
-- clean up
191197
\c -
192198
DROP TABLE clustertest;

0 commit comments

Comments
 (0)