Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit948d6ec

Browse files
committed
Modify the relcache to record the temp status of both local and nonlocal
temp relations; this is no more expensive than before, now that we havepg_class.relistemp. Insert tests into bufmgr.c to prevent attemptingto fetch pages from nonlocal temp relations. This provides a low-leveldefense against bugs-of-omission allowing temp pages to be loaded into sharedbuffers, as in the contrib/pgstattuple problem reported by Stuart Bishop.While at it, tweak a bunch of places to use new relcache tests (instead ofexpensive probes into pg_namespace) to detect local or nonlocal temp tables.
1 parent84a059a commit948d6ec

File tree

14 files changed

+92
-56
lines changed

14 files changed

+92
-56
lines changed

‎src/backend/catalog/index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.314 2009/03/27 15:57:11 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.315 2009/03/31 22:12:46 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -2264,7 +2264,7 @@ reindex_index(Oid indexId)
22642264
* Don't allow reindex on temp tables of other backends ... their local
22652265
* buffer manager is not going to cope.
22662266
*/
2267-
if (isOtherTempNamespace(RelationGetNamespace(iRel)))
2267+
if (RELATION_IS_OTHER_TEMP(iRel))
22682268
ereport(ERROR,
22692269
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
22702270
errmsg("cannot reindex temporary tables of other sessions")));

‎src/backend/catalog/namespace.c

Lines changed: 4 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-
* $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.116 2009/01/01 17:23:37 momjian Exp $
16+
* $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.117 2009/03/31 22:12:46 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -2389,6 +2389,9 @@ isAnyTempNamespace(Oid namespaceId)
23892389
/*
23902390
* isOtherTempNamespace - is the given namespace some other backend's
23912391
* temporary-table namespace (including temporary-toast-table namespaces)?
2392+
*
2393+
* Note: for most purposes in the C code, this function is obsolete. Use
2394+
* RELATION_IS_OTHER_TEMP() instead to detect non-local temp relations.
23922395
*/
23932396
bool
23942397
isOtherTempNamespace(OidnamespaceId)

‎src/backend/catalog/toasting.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/toasting.c,v 1.13 2009/02/02 19:31:38 alvherre Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/toasting.c,v 1.14 2009/03/31 22:12:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -179,7 +179,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptio
179179
* Toast tables for regular relations go in pg_toast; those for temp
180180
* relations go into the per-backend temp-toast-table namespace.
181181
*/
182-
if (rel->rd_istemp)
182+
if (rel->rd_islocaltemp)
183183
namespaceid=GetTempToastNamespace();
184184
else
185185
namespaceid=PG_TOAST_NAMESPACE;

‎src/backend/commands/analyze.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.134 2009/03/24 20:17:13 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.135 2009/03/31 22:12:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -213,7 +213,7 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt,
213213
* probably not up-to-date on disk. (We don't throw a warning here; it
214214
* would just lead to chatter during a database-wide ANALYZE.)
215215
*/
216-
if (isOtherTempNamespace(RelationGetNamespace(onerel)))
216+
if (RELATION_IS_OTHER_TEMP(onerel))
217217
{
218218
relation_close(onerel,ShareUpdateExclusiveLock);
219219
return;

‎src/backend/commands/cluster.c

Lines changed: 4 additions & 4 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.182 2009/02/02 19:31:38 alvherre Exp $
14+
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.183 2009/03/31 22:12:46 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -117,7 +117,7 @@ cluster(ClusterStmt *stmt, bool isTopLevel)
117117
* Reject clustering a remote temp table ... their local buffer
118118
* manager is not going to cope.
119119
*/
120-
if (isOtherTempNamespace(RelationGetNamespace(rel)))
120+
if (RELATION_IS_OTHER_TEMP(rel))
121121
ereport(ERROR,
122122
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
123123
errmsg("cannot cluster temporary tables of other sessions")));
@@ -302,7 +302,7 @@ cluster_rel(RelToCluster *rvtc, bool recheck, bool verbose)
302302
* check_index_is_clusterable which is redundant, but we leave it for
303303
* extra safety.
304304
*/
305-
if (isOtherTempNamespace(RelationGetNamespace(OldHeap)))
305+
if (RELATION_IS_OTHER_TEMP(OldHeap))
306306
{
307307
relation_close(OldHeap,AccessExclusiveLock);
308308
return;
@@ -465,7 +465,7 @@ check_index_is_clusterable(Relation OldHeap, Oid indexOid, bool recheck)
465465
* Don't allow cluster on temp tables of other backends ... their local
466466
* buffer manager is not going to cope.
467467
*/
468-
if (isOtherTempNamespace(RelationGetNamespace(OldHeap)))
468+
if (RELATION_IS_OTHER_TEMP(OldHeap))
469469
ereport(ERROR,
470470
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
471471
errmsg("cannot cluster temporary tables of other sessions")));

‎src/backend/commands/copy.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.306 2009/03/26 19:24:54 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.307 2009/03/31 22:12:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1001,8 +1001,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
10011001
}
10021002

10031003
/* check read-only transaction */
1004-
if (XactReadOnly&&is_from&&
1005-
!isTempNamespace(RelationGetNamespace(cstate->rel)))
1004+
if (XactReadOnly&&is_from&& !cstate->rel->rd_islocaltemp)
10061005
ereport(ERROR,
10071006
(errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
10081007
errmsg("transaction is read-only")));

‎src/backend/commands/indexcmds.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.182 2009/02/02 19:31:38 alvherre Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.183 2009/03/31 22:12:47 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -175,7 +175,7 @@ DefineIndex(RangeVar *heapRelation,
175175
/*
176176
* Don't try to CREATE INDEX on temp tables of other backends.
177177
*/
178-
if (isOtherTempNamespace(namespaceId))
178+
if (RELATION_IS_OTHER_TEMP(rel))
179179
ereport(ERROR,
180180
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
181181
errmsg("cannot create indexes on temporary tables of other sessions")));
@@ -1404,7 +1404,8 @@ ReindexDatabase(const char *databaseName, bool do_system, bool do_user)
14041404
continue;
14051405

14061406
/* Skip temp tables of other backends; we can't reindex them at all */
1407-
if (isOtherTempNamespace(classtuple->relnamespace))
1407+
if (classtuple->relistemp&&
1408+
!isTempNamespace(classtuple->relnamespace))
14081409
continue;
14091410

14101411
/* Check user/system classification, and optionally skip */

‎src/backend/commands/tablecmds.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.280 2009/02/11 21:11:16 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.281 2009/03/31 22:12:47 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1056,7 +1056,7 @@ truncate_check_rel(Relation rel)
10561056
* Don't allow truncate on temp tables of other backends ... their local
10571057
* buffer manager is not going to cope.
10581058
*/
1059-
if (isOtherTempNamespace(RelationGetNamespace(rel)))
1059+
if (RELATION_IS_OTHER_TEMP(rel))
10601060
ereport(ERROR,
10611061
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10621062
errmsg("cannot truncate temporary tables of other sessions")));
@@ -1203,7 +1203,7 @@ MergeAttributes(List *schema, List *supers, bool istemp,
12031203
errmsg("inherited relation \"%s\" is not a table",
12041204
parent->relname)));
12051205
/* Permanent rels cannot inherit from temporary ones */
1206-
if (!istemp&&isTempNamespace(RelationGetNamespace(relation)))
1206+
if (!istemp&&relation->rd_istemp)
12071207
ereport(ERROR,
12081208
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
12091209
errmsg("cannot inherit from temporary relation \"%s\"",
@@ -2793,7 +2793,7 @@ ATRewriteTables(List **wqueue)
27932793
* Don't allow rewrite on temp tables of other backends ... their
27942794
* local buffer manager is not going to cope.
27952795
*/
2796-
if (isOtherTempNamespace(RelationGetNamespace(OldHeap)))
2796+
if (RELATION_IS_OTHER_TEMP(OldHeap))
27972797
ereport(ERROR,
27982798
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
27992799
errmsg("cannot rewrite temporary tables of other sessions")));
@@ -4603,16 +4603,16 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
46034603
* backend has created in the temp table, because non-shared buffers are
46044604
* used for temp tables.)
46054605
*/
4606-
if (isTempNamespace(RelationGetNamespace(pkrel)))
4606+
if (pkrel->rd_istemp)
46074607
{
4608-
if (!isTempNamespace(RelationGetNamespace(rel)))
4608+
if (!rel->rd_istemp)
46094609
ereport(ERROR,
46104610
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
46114611
errmsg("cannot reference temporary table from permanent table constraint")));
46124612
}
46134613
else
46144614
{
4615-
if (isTempNamespace(RelationGetNamespace(rel)))
4615+
if (rel->rd_istemp)
46164616
ereport(ERROR,
46174617
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
46184618
errmsg("cannot reference permanent table from temporary table constraint")));
@@ -6690,7 +6690,7 @@ ATExecSetTableSpace(Oid tableOid, Oid newTableSpace)
66906690
* Don't allow moving temp tables of other backends ... their local buffer
66916691
* manager is not going to cope.
66926692
*/
6693-
if (isOtherTempNamespace(RelationGetNamespace(rel)))
6693+
if (RELATION_IS_OTHER_TEMP(rel))
66946694
ereport(ERROR,
66956695
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
66966696
errmsg("cannot move temporary tables of other sessions")));
@@ -6901,8 +6901,7 @@ ATExecAddInherit(Relation child_rel, RangeVar *parent)
69016901
ATSimplePermissions(parent_rel, false);
69026902

69036903
/* Permanent rels cannot inherit from temporary ones */
6904-
if (!isTempNamespace(RelationGetNamespace(child_rel))&&
6905-
isTempNamespace(RelationGetNamespace(parent_rel)))
6904+
if (parent_rel->rd_istemp&& !child_rel->rd_istemp)
69066905
ereport(ERROR,
69076906
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
69086907
errmsg("cannot inherit from temporary relation \"%s\"",

‎src/backend/commands/vacuum.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.386 2009/03/24 20:17:13 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.387 2009/03/31 22:12:48 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -1147,7 +1147,7 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
11471147
* warning here; it would just lead to chatter during a database-wide
11481148
* VACUUM.)
11491149
*/
1150-
if (isOtherTempNamespace(RelationGetNamespace(onerel)))
1150+
if (RELATION_IS_OTHER_TEMP(onerel))
11511151
{
11521152
relation_close(onerel,lmode);
11531153
PopActiveSnapshot();

‎src/backend/optimizer/prep/prepunion.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.167 2009/03/05 17:30:29 tgl Exp $
25+
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.168 2009/03/31 22:12:48 tgl Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -1258,21 +1258,23 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
12581258
IndexchildRTindex;
12591259
AppendRelInfo*appinfo;
12601260

1261+
/* Open rel, acquire the appropriate lock type */
1262+
if (childOID!=parentOID)
1263+
newrelation=heap_open(childOID,lockmode);
1264+
else
1265+
newrelation=oldrelation;
1266+
12611267
/*
12621268
* It is possible that the parent table has children that are temp
12631269
* tables of other backends. We cannot safely access such tables
12641270
* (because of buffering issues), and the best thing to do seems to be
12651271
* to silently ignore them.
12661272
*/
1267-
if (childOID!=parentOID&&
1268-
isOtherTempNamespace(get_rel_namespace(childOID)))
1273+
if (childOID!=parentOID&&RELATION_IS_OTHER_TEMP(newrelation))
1274+
{
1275+
heap_close(newrelation,lockmode);
12691276
continue;
1270-
1271-
/* Open rel, acquire the appropriate lock type */
1272-
if (childOID!=parentOID)
1273-
newrelation=heap_open(childOID,lockmode);
1274-
else
1275-
newrelation=oldrelation;
1277+
}
12761278

12771279
/*
12781280
* Build an RTE for the child, and attach to query's rangetable list.

‎src/backend/postmaster/autovacuum.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
*
5656
*
5757
* IDENTIFICATION
58-
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.93 2009/02/09 20:57:59 alvherre Exp $
58+
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.94 2009/03/31 22:12:48 tgl Exp $
5959
*
6060
*-------------------------------------------------------------------------
6161
*/
@@ -1942,7 +1942,6 @@ do_autovacuum(void)
19421942
booldovacuum;
19431943
booldoanalyze;
19441944
boolwraparound;
1945-
intbackendID;
19461945

19471946
relid=HeapTupleGetOid(tuple);
19481947

@@ -1959,10 +1958,12 @@ do_autovacuum(void)
19591958
* Check if it is a temp table (presumably, of some other backend's).
19601959
* We cannot safely process other backends' temp tables.
19611960
*/
1962-
backendID=GetTempNamespaceBackendId(classForm->relnamespace);
1963-
1964-
if (backendID>0)
1961+
if (classForm->relistemp)
19651962
{
1963+
intbackendID;
1964+
1965+
backendID=GetTempNamespaceBackendId(classForm->relnamespace);
1966+
19661967
/* We just ignore it if the owning backend is still active */
19671968
if (backendID==MyBackendId|| !BackendIdIsActive(backendID))
19681969
{
@@ -2052,10 +2053,9 @@ do_autovacuum(void)
20522053
boolwraparound;
20532054

20542055
/*
2055-
* Skip temp tables (i.e. those in temp namespaces). We cannot safely
2056-
* process other backends' temp tables.
2056+
* We cannot safely process other backends' temp tables, so skip 'em.
20572057
*/
2058-
if (isAnyTempNamespace(classForm->relnamespace))
2058+
if (classForm->relistemp)
20592059
continue;
20602060

20612061
relid=HeapTupleGetOid(tuple);

‎src/backend/storage/buffer/bufmgr.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.249 2009/03/23 01:52:38 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.250 2009/03/31 22:12:48 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -122,6 +122,12 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
122122

123123
if (reln->rd_istemp)
124124
{
125+
/* see comments in ReadBufferExtended */
126+
if (RELATION_IS_OTHER_TEMP(reln))
127+
ereport(ERROR,
128+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
129+
errmsg("cannot access temporary tables of other sessions")));
130+
125131
/* pass it off to localbuf.c */
126132
LocalPrefetchBuffer(reln->rd_smgr,forkNum,blockNum);
127133
}
@@ -204,6 +210,16 @@ ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
204210
/* Open it at the smgr level if not already done */
205211
RelationOpenSmgr(reln);
206212

213+
/*
214+
* Reject attempts to read non-local temporary relations; we would
215+
* be likely to get wrong data since we have no visibility into the
216+
* owning session's local buffers.
217+
*/
218+
if (RELATION_IS_OTHER_TEMP(reln))
219+
ereport(ERROR,
220+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
221+
errmsg("cannot access temporary tables of other sessions")));
222+
207223
/*
208224
* Read the buffer, and update pgstat counters to reflect a cache
209225
* hit or miss.
@@ -220,6 +236,8 @@ ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
220236
/*
221237
* ReadBufferWithoutRelcache -- like ReadBufferExtended, but doesn't require
222238
*a relcache entry for the relation.
239+
*
240+
* NB: caller is assumed to know what it's doing if isTemp is true.
223241
*/
224242
Buffer
225243
ReadBufferWithoutRelcache(RelFileNodernode,boolisTemp,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp