From 93f03dad824f14f40519597e5e4a8fe7b6df858e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 18 May 2019 13:51:16 -0400 Subject: [PATCH] Make BufFileCreateTemp() ensure that temp tablespaces are set up. If PrepareTempTablespaces() has never been called in the current transaction, OpenTemporaryFile() will fall back to using the default tablespace, which is a bug if the user wanted temp files placed elsewhere. gistInitBuildBuffers() appears to have this disease already, and it seems like an easy trap for future coders to fall into. We discussed other ways to close this gap, but none of them are prettier or more reliable than just having BufFileCreateTemp do it. In particular, having fd.c do this creates layering issues that we could do without. Per suggestion from Melanie Plageman. Arguably this is a bug fix, but nobody seems very excited about back-patching, so change in HEAD only. Discussion: https://postgr.es/m/CAAKRu_YwzjuGAmmaw4-8XO=OVFGR1QhY_Pq-t3wjb9ribBJb_Q@mail.gmail.com --- src/backend/storage/file/buffile.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index c2c445dbf4..b773a76049 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -41,6 +41,7 @@ #include "postgres.h" +#include "commands/tablespace.h" #include "executor/instrument.h" #include "miscadmin.h" #include "pgstat.h" @@ -185,6 +186,17 @@ BufFileCreateTemp(bool interXact) BufFile *file; File pfile; + /* + * Ensure that temp tablespaces are set up for OpenTemporaryFile to use. + * Possibly the caller will have done this already, but it seems useful to + * double-check here. Failure to do this at all would result in the temp + * files always getting placed in the default tablespace, which is a + * pretty hard-to-detect bug. Callers may prefer to do it earlier if they + * want to be sure that any required catalog access is done in some other + * resource context. + */ + PrepareTempTablespaces(); + pfile = OpenTemporaryFile(interXact); Assert(pfile >= 0); -- 2.40.0