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

Commit961cab0

Browse files
committed
Allow "in place" tablespaces.
This is a backpatch to branches 10-14 of the following commits:7170f21 Allow "in place" tablespaces.c6f2f01 Fix pg_basebackup with in-place tablespaces.f6f0db4 Fix pg_tablespace_location() with in-place tablespaces7a7cd84 doc: Remove mention to in-place tablespaces for pg_tablespace_location()5344723 Remove unnecessary Windows-specific basebackup code.In-place tablespaces were introduced as a testing helper mechanism, butthey are going to be used for a bugfix in WAL replay to be backpatchedto all stable branches.I (Álvaro) had to adjust some code to account for lack ofget_dirent_type() in branches prior to 14.Author: Thomas Munro <thomas.munro@gmail.com>Author: Michaël Paquier <michael@paquier.xyz>Author: Álvaro Herrera <alvherre@alvh.no-ip.org>Discussion:https://postgr.es/m/20220722081858.omhn2in5zt3g4nek@alvherre.pgsql
1 parent3e1297a commit961cab0

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10369,6 +10369,25 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
1036910369
</para>
1037010370

1037110371
<variablelist>
10372+
<varlistentry id="guc-allow-in-place-tablespaces" xreflabel="allow_in_place_tablespaces">
10373+
<term><varname>allow_in_place_tablespaces</varname> (<type>boolean</type>)
10374+
<indexterm>
10375+
<primary><varname>allow_in_place_tablespaces</varname> configuration parameter</primary>
10376+
</indexterm>
10377+
</term>
10378+
<listitem>
10379+
<para>
10380+
Allows tablespaces to be created as directories inside
10381+
<filename>pg_tblspc</filename>, when an empty location string
10382+
is provided to the <command>CREATE TABLESPACE</command> command. This
10383+
is intended to allow testing replication scenarios where primary and
10384+
standby servers are running on the same machine. Such directories
10385+
are likely to confuse backup tools that expect to find only symbolic
10386+
links in that location. Only superusers can change this setting.
10387+
</para>
10388+
</listitem>
10389+
</varlistentry>
10390+
1037210391
<varlistentry id="guc-allow-system-table-mods" xreflabel="allow_system_table_mods">
1037310392
<term><varname>allow_system_table_mods</varname> (<type>boolean</type>)
1037410393
<indexterm>

‎src/backend/access/transam/xlog.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include"commands/progress.h"
4444
#include"commands/tablespace.h"
4545
#include"common/controldata_utils.h"
46+
#include"common/file_utils.h"
4647
#include"executor/instrument.h"
4748
#include"miscadmin.h"
4849
#include"pg_trace.h"
@@ -11168,6 +11169,14 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1116811169

1116911170
snprintf(fullpath,sizeof(fullpath),"pg_tblspc/%s",de->d_name);
1117011171

11172+
/*
11173+
* Skip anything that isn't a symlink/junction. For testing only,
11174+
* we sometimes use allow_in_place_tablespaces to create
11175+
* directories directly under pg_tblspc, which would fail below.
11176+
*/
11177+
if (get_dirent_type(fullpath,de, false,ERROR)!=PGFILETYPE_LNK)
11178+
continue;
11179+
1117111180
#if defined(HAVE_READLINK)|| defined(WIN32)
1117211181
rllen=readlink(fullpath,linkpath,sizeof(linkpath));
1117311182
if (rllen<0)

‎src/backend/commands/tablespace.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
/* GUC variables */
8888
char*default_tablespace=NULL;
8989
char*temp_tablespaces=NULL;
90+
boolallow_in_place_tablespaces= false;
9091

9192

9293
staticvoidcreate_tablespace_directories(constchar*location,
@@ -241,6 +242,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
241242
char*location;
242243
OidownerId;
243244
DatumnewOptions;
245+
boolin_place;
244246

245247
/* Must be super user */
246248
if (!superuser())
@@ -266,12 +268,15 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
266268
(errcode(ERRCODE_INVALID_NAME),
267269
errmsg("tablespace location cannot contain single quotes")));
268270

271+
in_place=allow_in_place_tablespaces&&strlen(location)==0;
272+
269273
/*
270274
* Allowing relative paths seems risky
271275
*
272-
* this also helps us ensure that location is not empty or whitespace
276+
* This also helps us ensure that location is not empty or whitespace,
277+
* unless specifying a developer-only in-place tablespace.
273278
*/
274-
if (!is_absolute_path(location))
279+
if (!in_place&& !is_absolute_path(location))
275280
ereport(ERROR,
276281
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
277282
errmsg("tablespace location must be an absolute path")));
@@ -592,16 +597,36 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
592597
char*linkloc;
593598
char*location_with_version_dir;
594599
structstatst;
600+
boolin_place;
595601

596602
linkloc=psprintf("pg_tblspc/%u",tablespaceoid);
597-
location_with_version_dir=psprintf("%s/%s",location,
603+
604+
/*
605+
* If we're asked to make an 'in place' tablespace, create the directory
606+
* directly where the symlink would normally go. This is a developer-only
607+
* option for now, to facilitate regression testing.
608+
*/
609+
in_place=strlen(location)==0;
610+
611+
if (in_place)
612+
{
613+
if (MakePGDirectory(linkloc)<0&&errno!=EEXIST)
614+
ereport(ERROR,
615+
(errcode_for_file_access(),
616+
errmsg("could not create directory \"%s\": %m",
617+
linkloc)));
618+
}
619+
620+
location_with_version_dir=psprintf("%s/%s",in_place ?linkloc :location,
598621
TABLESPACE_VERSION_DIRECTORY);
599622

600623
/*
601624
* Attempt to coerce target directory to safe permissions. If this fails,
602-
* it doesn't exist or has the wrong owner.
625+
* it doesn't exist or has the wrong owner. Not needed for in-place mode,
626+
* because in that case we created the directory with the desired
627+
* permissions.
603628
*/
604-
if (chmod(location,pg_dir_create_mode)!=0)
629+
if (!in_place&&chmod(location,pg_dir_create_mode)!=0)
605630
{
606631
if (errno==ENOENT)
607632
ereport(ERROR,
@@ -650,13 +675,13 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
650675
/*
651676
* In recovery, remove old symlink, in case it points to the wrong place.
652677
*/
653-
if (InRecovery)
678+
if (!in_place&&InRecovery)
654679
remove_tablespace_symlink(linkloc);
655680

656681
/*
657682
* Create the symlink under PGDATA
658683
*/
659-
if (symlink(location,linkloc)<0)
684+
if (!in_place&&symlink(location,linkloc)<0)
660685
ereport(ERROR,
661686
(errcode_for_file_access(),
662687
errmsg("could not create symbolic link \"%s\": %m",

‎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>
@@ -309,6 +310,9 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
309310
charsourcepath[MAXPGPATH];
310311
chartargetpath[MAXPGPATH];
311312
intrllen;
313+
#ifndefWIN32
314+
structstatst;
315+
#endif
312316

313317
/*
314318
* It's useful to apply this function to pg_class.reltablespace, wherein
@@ -333,6 +337,31 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
333337
*/
334338
snprintf(sourcepath,sizeof(sourcepath),"pg_tblspc/%u",tablespaceOid);
335339

340+
/*
341+
* Before reading the link, check if the source path is a link or a
342+
* junction point. Note that a directory is possible for a tablespace
343+
* created with allow_in_place_tablespaces enabled. If a directory is
344+
* found, a relative path to the data directory is returned.
345+
*/
346+
#ifdefWIN32
347+
if (!pgwin32_is_junction(sourcepath))
348+
PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
349+
#else
350+
if (lstat(sourcepath,&st)<0)
351+
{
352+
ereport(ERROR,
353+
(errcode_for_file_access(),
354+
errmsg("could not stat file \"%s\": %m",
355+
sourcepath)));
356+
}
357+
358+
if (!S_ISLNK(st.st_mode))
359+
PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
360+
#endif
361+
362+
/*
363+
* In presence of a link or a junction point, return the path pointing to.
364+
*/
336365
rllen=readlink(sourcepath,targetpath,sizeof(targetpath));
337366
if (rllen<0)
338367
ereport(ERROR,

‎src/backend/utils/misc/guc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include"catalog/storage.h"
4747
#include"commands/async.h"
4848
#include"commands/prepare.h"
49+
#include"commands/tablespace.h"
4950
#include"commands/trigger.h"
5051
#include"commands/user.h"
5152
#include"commands/vacuum.h"
@@ -1952,6 +1953,17 @@ static struct config_bool ConfigureNamesBool[] =
19521953
NULL,NULL,NULL
19531954
},
19541955

1956+
{
1957+
{"allow_in_place_tablespaces",PGC_SUSET,DEVELOPER_OPTIONS,
1958+
gettext_noop("Allows tablespaces directly inside pg_tblspc, for testing."),
1959+
NULL,
1960+
GUC_NOT_IN_SAMPLE
1961+
},
1962+
&allow_in_place_tablespaces,
1963+
false,
1964+
NULL,NULL,NULL
1965+
},
1966+
19551967
{
19561968
{"lo_compat_privileges",PGC_SUSET,COMPAT_OPTIONS_PREVIOUS,
19571969
gettext_noop("Enables backward compatibility mode for privilege checks on large objects."),

‎src/include/commands/tablespace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include"lib/stringinfo.h"
2020
#include"nodes/parsenodes.h"
2121

22+
externboolallow_in_place_tablespaces;
23+
2224
/* XLOG stuff */
2325
#defineXLOG_TBLSPC_CREATE0x00
2426
#defineXLOG_TBLSPC_DROP0x10

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp