Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f6f0db4

Browse files
committed
Fix pg_tablespace_location() with in-place tablespaces
Using this system function with an in-place tablespace (created when allow_in_place_tablespaces is enabled by specifying an empty string as location) caused a failure when using readlink(), as the tablespace is, in this case, not a symbolic link in pg_tblspc/ but a directory. Rather than getting a failure, the commit changes pg_tablespace_location() so as a relative path to the data directory is returned for in-place tablespaces, to make a difference between tablespaces created when allow_in_place_tablespaces is enabled or not. Getting a path rather than an empty string that would match the CREATE TABLESPACE command in this case is more useful for tests that would like to rely on this function. While on it, a regression test is added for this case. This is simple to add in the main regression test suite thanks to regexp_replace() to mask the part of the tablespace location dependent on its OID. Author: Michael Paquier Reviewed-by: Kyotaro Horiguchi, Thomas Munro Discussion: https://postgr.es/m/YiG1RleON1WBcLnX@paquier.xyz
1 parent c91f71b commit f6f0db4

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

doc/src/sgml/func.sgml

+7-1
Original file line numberDiff line numberDiff line change
@@ -23924,7 +23924,13 @@ SELECT currval(pg_get_serial_sequence('sometable', 'id'));
2392423924
</para>
2392523925
<para>
2392623926
Returns the file system path that this tablespace is located in.
23927-
</para></entry>
23927+
</para>
23928+
<para>
23929+
A relative path to the data directory is returned for tablespaces
23930+
created when <xref linkend="guc-allow-in-place-tablespaces"/> is
23931+
enabled.
23932+
</para>
23933+
</entry>
2392823934
</row>
2392923935

2393023936
<row>

src/backend/utils/adt/misc.c

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "postgres.h"
1616

1717
#include <sys/file.h>
18+
#include <sys/stat.h>
1819
#include <dirent.h>
1920
#include <fcntl.h>
2021
#include <math.h>
@@ -282,6 +283,9 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
282283
char sourcepath[MAXPGPATH];
283284
char targetpath[MAXPGPATH];
284285
int rllen;
286+
#ifndef WIN32
287+
struct stat st;
288+
#endif
285289

286290
/*
287291
* It's useful to apply this function to pg_class.reltablespace, wherein
@@ -306,6 +310,31 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
306310
*/
307311
snprintf(sourcepath, sizeof(sourcepath), "pg_tblspc/%u", tablespaceOid);
308312

313+
/*
314+
* Before reading the link, check if the source path is a link or a
315+
* junction point. Note that a directory is possible for a tablespace
316+
* created with allow_in_place_tablespaces enabled. If a directory is
317+
* found, a relative path to the data directory is returned.
318+
*/
319+
#ifdef WIN32
320+
if (!pgwin32_is_junction(sourcepath))
321+
PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
322+
#else
323+
if (lstat(sourcepath, &st) < 0)
324+
{
325+
ereport(ERROR,
326+
(errcode_for_file_access(),
327+
errmsg("could not stat file \"%s\": %m",
328+
sourcepath)));
329+
}
330+
331+
if (!S_ISLNK(st.st_mode))
332+
PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
333+
#endif
334+
335+
/*
336+
* In presence of a link or a junction point, return the path pointing to.
337+
*/
309338
rllen = readlink(sourcepath, targetpath, sizeof(targetpath));
310339
if (rllen < 0)
311340
ereport(ERROR,

src/test/regress/expected/tablespace.out

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ SELECT spcoptions FROM pg_tablespace WHERE spcname = 'regress_tblspacewith';
2424
DROP TABLESPACE regress_tblspacewith;
2525
-- create a tablespace we can use
2626
CREATE TABLESPACE regress_tblspace LOCATION '';
27+
-- This returns a relative path as of an effect of allow_in_place_tablespaces,
28+
-- masking the tablespace OID used in the path name.
29+
SELECT regexp_replace(pg_tablespace_location(oid), '(pg_tblspc)/(\d+)', '\1/NNN')
30+
FROM pg_tablespace WHERE spcname = 'regress_tblspace';
31+
regexp_replace
32+
----------------
33+
pg_tblspc/NNN
34+
(1 row)
35+
2736
-- try setting and resetting some properties for the new tablespace
2837
ALTER TABLESPACE regress_tblspace SET (random_page_cost = 1.0, seq_page_cost = 1.1);
2938
ALTER TABLESPACE regress_tblspace SET (some_nonexistent_parameter = true); -- fail

src/test/regress/sql/tablespace.sql

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ DROP TABLESPACE regress_tblspacewith;
2222

2323
-- create a tablespace we can use
2424
CREATE TABLESPACE regress_tblspace LOCATION '';
25+
-- This returns a relative path as of an effect of allow_in_place_tablespaces,
26+
-- masking the tablespace OID used in the path name.
27+
SELECT regexp_replace(pg_tablespace_location(oid), '(pg_tblspc)/(\d+)', '\1/NNN')
28+
FROM pg_tablespace WHERE spcname = 'regress_tblspace';
2529

2630
-- try setting and resetting some properties for the new tablespace
2731
ALTER TABLESPACE regress_tblspace SET (random_page_cost = 1.0, seq_page_cost = 1.1);

0 commit comments

Comments
 (0)