]> granicus.if.org Git - postgresql/commitdiff
Make BufFileCreateTemp() ensure that temp tablespaces are set up.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 18 May 2019 17:51:16 +0000 (13:51 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 18 May 2019 17:51:16 +0000 (13:51 -0400)
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

index c2c445dbf4098aa8f113b779337140f2bb8086b1..b773a7604993558e5cd08a49a5d45019a6abf26f 100644 (file)
@@ -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);