Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6f0db4

Browse files
committed
Fix pg_tablespace_location() with in-place tablespaces
Using this system function with an in-place tablespace (created whenallow_in_place_tablespaces is enabled by specifying an empty string aslocation) 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 changespg_tablespace_location() so as a relative path to the data directory isreturned for in-place tablespaces, to make a difference betweentablespaces created when allow_in_place_tablespaces is enabled or not.Getting a path rather than an empty string that would match the CREATETABLESPACE command in this case is more useful for tests that would liketo rely on this function.While on it, a regression test is added for this case. This is simpleto add in the main regression test suite thanks to regexp_replace() tomask the part of the tablespace location dependent on its OID.Author: Michael PaquierReviewed-by: Kyotaro Horiguchi, Thomas MunroDiscussion:https://postgr.es/m/YiG1RleON1WBcLnX@paquier.xyz
1 parentc91f71b commitf6f0db4

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 7 additions & 1 deletion
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

Lines changed: 29 additions & 0 deletions
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
charsourcepath[MAXPGPATH];
283284
chartargetpath[MAXPGPATH];
284285
intrllen;
286+
#ifndefWIN32
287+
structstatst;
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+
#ifdefWIN32
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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
CREATETABLESPACEregress_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_tablespaceWHERE spcname='regress_tblspace';
2529

2630
-- try setting and resetting some properties for the new tablespace
2731
ALTERTABLESPACE regress_tblspaceSET (random_page_cost=1.0, seq_page_cost=1.1);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp