Skip to content

Commit 5fa3418

Browse files
committed
Disallow VACUUM, ANALYZE, TRUNCATE on temp tables belonging to other
backends. Given that temp tables now store data locally in the local buffer manager, these things are not going to work safely.
1 parent c99f820 commit 5fa3418

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

src/backend/catalog/namespace.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.35 2002/09/04 20:31:14 momjian Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.36 2002/09/23 20:43:40 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -1217,6 +1217,28 @@ isTempNamespace(Oid namespaceId)
12171217
return false;
12181218
}
12191219

1220+
/*
1221+
* isOtherTempNamespace - is the given namespace some other backend's
1222+
* temporary-table namespace?
1223+
*/
1224+
bool
1225+
isOtherTempNamespace(Oid namespaceId)
1226+
{
1227+
bool result;
1228+
char *nspname;
1229+
1230+
/* If it's my own temp namespace, say "false" */
1231+
if (isTempNamespace(namespaceId))
1232+
return false;
1233+
/* Else, if the namespace name starts with "pg_temp_", say "true" */
1234+
nspname = get_namespace_name(namespaceId);
1235+
if (!nspname)
1236+
return false; /* no such namespace? */
1237+
result = (strncmp(nspname, "pg_temp_", 8) == 0);
1238+
pfree(nspname);
1239+
return result;
1240+
}
1241+
12201242
/*
12211243
* PushSpecialNamespace - push a "special" namespace onto the front of the
12221244
* search path.

src/backend/commands/analyze.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.46 2002/09/04 20:31:14 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.47 2002/09/23 20:43:40 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -21,6 +21,7 @@
2121
#include "catalog/catalog.h"
2222
#include "catalog/catname.h"
2323
#include "catalog/indexing.h"
24+
#include "catalog/namespace.h"
2425
#include "catalog/pg_operator.h"
2526
#include "catalog/pg_statistic.h"
2627
#include "catalog/pg_type.h"
@@ -215,6 +216,19 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
215216
return;
216217
}
217218

219+
/*
220+
* Silently ignore tables that are temp tables of other backends ---
221+
* trying to analyze these is rather pointless, since their
222+
* contents are probably not up-to-date on disk. (We don't throw a
223+
* warning here; it would just lead to chatter during a database-wide
224+
* ANALYZE.)
225+
*/
226+
if (isOtherTempNamespace(RelationGetNamespace(onerel)))
227+
{
228+
relation_close(onerel, AccessShareLock);
229+
return;
230+
}
231+
218232
/*
219233
* We can ANALYZE any table except pg_statistic. See update_attstats
220234
*/

src/backend/commands/tablecmds.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.43 2002/09/22 19:42:50 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.44 2002/09/23 20:43:40 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -367,13 +367,21 @@ TruncateRelation(const RangeVar *relation)
367367
RelationGetRelationName(rel));
368368
}
369369

370+
/* Permissions checks */
370371
if (!allowSystemTableMods && IsSystemRelation(rel))
371372
elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
372373
RelationGetRelationName(rel));
373374

374375
if (!pg_class_ownercheck(relid, GetUserId()))
375376
aclcheck_error(ACLCHECK_NOT_OWNER, RelationGetRelationName(rel));
376377

378+
/*
379+
* Don't allow truncate on temp tables of other backends ... their
380+
* local buffer manager is not going to cope.
381+
*/
382+
if (isOtherTempNamespace(RelationGetNamespace(rel)))
383+
elog(ERROR, "TRUNCATE cannot be used on temp tables of other processes");
384+
377385
/*
378386
* Don't allow truncate on tables which are referenced by foreign keys
379387
*/

src/backend/commands/vacuum.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.239 2002/09/23 00:42:48 tgl Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.240 2002/09/23 20:43:41 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -775,6 +775,20 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, char expected_relkind)
775775
return;
776776
}
777777

778+
/*
779+
* Silently ignore tables that are temp tables of other backends ---
780+
* trying to vacuum these will lead to great unhappiness, since their
781+
* contents are probably not up-to-date on disk. (We don't throw a
782+
* warning here; it would just lead to chatter during a database-wide
783+
* VACUUM.)
784+
*/
785+
if (isOtherTempNamespace(RelationGetNamespace(onerel)))
786+
{
787+
relation_close(onerel, lmode);
788+
CommitTransactionCommand(true);
789+
return;
790+
}
791+
778792
/*
779793
* Get a session-level lock too. This will protect our access to the
780794
* relation across multiple transactions, so that we can vacuum the

src/include/catalog/namespace.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: namespace.h,v 1.20 2002/09/04 20:31:37 momjian Exp $
10+
* $Id: namespace.h,v 1.21 2002/09/23 20:43:41 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -76,6 +76,7 @@ extern RangeVar *makeRangeVarFromNameList(List *names);
7676
extern char *NameListToString(List *names);
7777

7878
extern bool isTempNamespace(Oid namespaceId);
79+
extern bool isOtherTempNamespace(Oid namespaceId);
7980

8081
extern void PushSpecialNamespace(Oid namespaceId);
8182
extern void PopSpecialNamespace(Oid namespaceId);

0 commit comments

Comments
 (0)