Skip to content

Commit 8b83e01

Browse files
committed
Fix temporary tablespaces for shared filesets some more.
Commit ecd9e9f fixed the problem in the wrong place, causing unwanted side-effects on the behavior of GetNextTempTableSpace(). Instead, let's make SharedFileSetInit() responsible for subbing in the value of MyDatabaseTableSpace when the default tablespace is called for. The convention about what is in the tempTableSpaces[] array is evidently insufficiently documented, so try to improve that. It also looks like SharedFileSetInit() is doing the wrong thing in the case where temp_tablespaces is empty. It was hard-wiring use of the pg_default tablespace, but it seems like using MyDatabaseTableSpace is more consistent with what happens for other temp files. Back-patch the reversion of PrepareTempTablespaces()'s behavior to 9.5, as ecd9e9f was. The changes in SharedFileSetInit() go back to v11 where that was introduced. (Note there is net zero code change before v11 from these two patch sets, so nothing to release-note.) Magnus Hagander and Tom Lane Discussion: https://postgr.es/m/CABUevExg5YEsOvqMxrjoNvb3ApVyH+9jggWGKwTDFyFCVWczGQ@mail.gmail.com
1 parent 28a862e commit 8b83e01

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/backend/commands/tablespace.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ GetDefaultTablespace(char relpersistence, bool partitioned)
11841184

11851185
typedef struct
11861186
{
1187+
/* Array of OIDs to be passed to SetTempTablespaces() */
11871188
int numSpcs;
11881189
Oid tblSpcs[FLEXIBLE_ARRAY_MEMBER];
11891190
} temp_tablespaces_extra;
@@ -1233,6 +1234,7 @@ check_temp_tablespaces(char **newval, void **extra, GucSource source)
12331234
/* Allow an empty string (signifying database default) */
12341235
if (curname[0] == '\0')
12351236
{
1237+
/* InvalidOid signifies database's default tablespace */
12361238
tblSpcs[numSpcs++] = InvalidOid;
12371239
continue;
12381240
}
@@ -1259,6 +1261,7 @@ check_temp_tablespaces(char **newval, void **extra, GucSource source)
12591261
*/
12601262
if (curoid == MyDatabaseTableSpace)
12611263
{
1264+
/* InvalidOid signifies database's default tablespace */
12621265
tblSpcs[numSpcs++] = InvalidOid;
12631266
continue;
12641267
}
@@ -1369,6 +1372,7 @@ PrepareTempTablespaces(void)
13691372
/* Allow an empty string (signifying database default) */
13701373
if (curname[0] == '\0')
13711374
{
1375+
/* InvalidOid signifies database's default tablespace */
13721376
tblSpcs[numSpcs++] = InvalidOid;
13731377
continue;
13741378
}
@@ -1387,7 +1391,8 @@ PrepareTempTablespaces(void)
13871391
*/
13881392
if (curoid == MyDatabaseTableSpace)
13891393
{
1390-
tblSpcs[numSpcs++] = curoid;
1394+
/* InvalidOid signifies database's default tablespace */
1395+
tblSpcs[numSpcs++] = InvalidOid;
13911396
continue;
13921397
}
13931398

src/backend/storage/file/fd.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,10 @@ static AllocateDesc *allocatedDescs = NULL;
251251
static long tempFileCounter = 0;
252252

253253
/*
254-
* Array of OIDs of temp tablespaces. When numTempTableSpaces is -1,
255-
* this has not been set in the current transaction.
254+
* Array of OIDs of temp tablespaces. (Some entries may be InvalidOid,
255+
* indicating that the current database's default tablespace should be used.)
256+
* When numTempTableSpaces is -1, this has not been set in the current
257+
* transaction.
256258
*/
257259
static Oid *tempTableSpaces = NULL;
258260
static int numTempTableSpaces = -1;
@@ -2670,6 +2672,9 @@ closeAllVfds(void)
26702672
* unless this function is called again before then. It is caller's
26712673
* responsibility that the passed-in array has adequate lifespan (typically
26722674
* it'd be allocated in TopTransactionContext).
2675+
*
2676+
* Some entries of the array may be InvalidOid, indicating that the current
2677+
* database's default tablespace should be used.
26732678
*/
26742679
void
26752680
SetTempTablespaces(Oid *tableSpaces, int numSpaces)
@@ -2709,7 +2714,10 @@ TempTablespacesAreSet(void)
27092714
* GetTempTablespaces
27102715
*
27112716
* Populate an array with the OIDs of the tablespaces that should be used for
2712-
* temporary files. Return the number that were copied into the output array.
2717+
* temporary files. (Some entries may be InvalidOid, indicating that the
2718+
* current database's default tablespace should be used.) At most numSpaces
2719+
* entries will be filled.
2720+
* Returns the number of OIDs that were copied into the output array.
27132721
*/
27142722
int
27152723
GetTempTablespaces(Oid *tableSpaces, int numSpaces)

src/backend/storage/file/sharedfileset.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,25 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg)
6363
lengthof(fileset->tablespaces));
6464
if (fileset->ntablespaces == 0)
6565
{
66-
fileset->tablespaces[0] = DEFAULTTABLESPACE_OID;
66+
/* If the GUC is empty, use current database's default tablespace */
67+
fileset->tablespaces[0] = MyDatabaseTableSpace;
6768
fileset->ntablespaces = 1;
6869
}
70+
else
71+
{
72+
int i;
73+
74+
/*
75+
* An entry of InvalidOid means use the default tablespace for the
76+
* current database. Replace that now, to be sure that all users of
77+
* the SharedFileSet agree on what to do.
78+
*/
79+
for (i = 0; i < fileset->ntablespaces; i++)
80+
{
81+
if (fileset->tablespaces[i] == InvalidOid)
82+
fileset->tablespaces[i] = MyDatabaseTableSpace;
83+
}
84+
}
6985

7086
/* Register our cleanup callback. */
7187
on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));

0 commit comments

Comments
 (0)