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
This commit is contained in:
Tom Lane 2019-05-18 13:51:16 -04:00
parent b12db9ff5f
commit 93f03dad82
1 changed files with 12 additions and 0 deletions

View 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);