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

Commit 89ace65

Browse files
committed
CFS branch for PGPROEE9_6
1 parent 7a7527c commit 89ace65

File tree

21 files changed

+857
-61
lines changed

21 files changed

+857
-61
lines changed

doc/src/sgml/filelist.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<!ENTITY config SYSTEM "config.sgml">
5050
<!ENTITY user-manag SYSTEM "user-manag.sgml">
5151
<!ENTITY wal SYSTEM "wal.sgml">
52+
<!ENTITY cfs SYSTEM "cfs.sgml">
5253

5354
<!-- programmer's guide -->
5455
<!ENTITY bgworker SYSTEM "bgworker.sgml">

doc/src/sgml/postgres.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
&diskusage;
161161
&wal;
162162
&regress;
163+
&cfs;
163164

164165
</part>
165166

doc/src/sgml/ref/create_tablespace.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ CREATE TABLESPACE <replaceable class="parameter">tablespace_name</replaceable>
105105
<para>
106106
A tablespace parameter to be set or reset. Currently, the only
107107
available parameters are <varname>seq_page_cost</>,
108-
<varname>random_page_cost</> and <varname>effective_io_concurrency</>.
109-
Setting either value for a particular tablespace will override the
108+
<varname>random_page_cost</>, <varname>effective_io_concurrency</> and <varname>compression</>.
109+
Setting cost value for a particular tablespace will override the
110110
planner's usual estimate of the cost of reading pages from tables in
111111
that tablespace, as established by the configuration parameters of the
112112
same name (see <xref linkend="guc-seq-page-cost">,
113113
<xref linkend="guc-random-page-cost">,
114114
<xref linkend="guc-effective-io-concurrency">). This may be useful if
115115
one tablespace is located on a disk which is faster or slower than the
116116
remainder of the I/O subsystem.
117+
Compression is discussed in section <xref linkend="cfs">.
117118
</para>
118119
</listitem>
119120
</varlistentry>

src/backend/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ OBJS = $(SUBDIROBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a \
4343
LIBS := $(filter-out -lpgport -lpgcommon, $(LIBS)) $(LDAP_LIBS_BE)
4444

4545
# The backend doesn't need everything that's in LIBS, however
46-
LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
46+
LIBS := $(filter-out -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
4747

4848
ifeq ($(with_systemd),yes)
4949
LIBS += -lsystemd
5050
endif
5151

52+
LIBS += -lzstd
53+
54+
55+
# Extra compression libraries
56+
#LIBS += -llz4 -lsnappy -llzfse
57+
5258
##########################################################################
5359

5460
all: submake-libpgport submake-schemapg postgres $(POSTGRES_IMP)

src/backend/access/common/reloptions.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@
5454

5555
static relopt_bool boolRelOpts[] =
5656
{
57+
{
58+
{
59+
"compression",
60+
"Enables compression for this table space",
61+
RELOPT_KIND_TABLESPACE,
62+
AccessExclusiveLock
63+
},
64+
false
65+
},
5766
{
5867
{
5968
"autovacuum_enabled",
@@ -1451,7 +1460,8 @@ tablespace_reloptions(Datum reloptions, bool validate)
14511460
static const relopt_parse_elt tab[] = {
14521461
{"random_page_cost", RELOPT_TYPE_REAL, offsetof(TableSpaceOpts, random_page_cost)},
14531462
{"seq_page_cost", RELOPT_TYPE_REAL, offsetof(TableSpaceOpts, seq_page_cost)},
1454-
{"effective_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, effective_io_concurrency)}
1463+
{"effective_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, effective_io_concurrency)},
1464+
{"compression", RELOPT_TYPE_BOOL, offsetof(TableSpaceOpts, compression)}
14551465
};
14561466

14571467
options = parseRelOptions(reloptions, validate, RELOPT_KIND_TABLESPACE,

src/backend/access/rmgrdesc/tblspcdesc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tblspc_desc(StringInfo buf, XLogReaderState *record)
2727
{
2828
xl_tblspc_create_rec *xlrec = (xl_tblspc_create_rec *) rec;
2929

30-
appendStringInfo(buf, "%u \"%s\"", xlrec->ts_id, xlrec->ts_path);
30+
appendStringInfo(buf, "%u %s\"%s\"", xlrec->ts_id, xlrec->ts_compressed ? "compressed " : "", xlrec->ts_path);
3131
}
3232
else if (info == XLOG_TBLSPC_DROP)
3333
{

src/backend/commands/dbcommands.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "utils/pg_locale.h"
6262
#include "utils/snapmgr.h"
6363
#include "utils/syscache.h"
64+
#include "utils/spccache.h"
6465
#include "utils/tqual.h"
6566

6667

@@ -1049,6 +1050,8 @@ movedb(const char *dbname, const char *tblspcname)
10491050
char *src_dbpath;
10501051
char *dst_dbpath;
10511052
DIR *dstdir;
1053+
bool src_compressed;
1054+
bool dst_compressed;
10521055
struct dirent *xlde;
10531056
movedb_failure_params fparms;
10541057

@@ -1122,6 +1125,9 @@ movedb(const char *dbname, const char *tblspcname)
11221125
AccessExclusiveLock);
11231126
return;
11241127
}
1128+
1129+
src_compressed = is_tablespace_compressed(src_tblspcoid);
1130+
dst_compressed = is_tablespace_compressed(dst_tblspcoid);
11251131

11261132
/*
11271133
* Check for other backends in the target database. (Because we hold the
@@ -1221,7 +1227,11 @@ movedb(const char *dbname, const char *tblspcname)
12211227
/*
12221228
* Copy files from the old tablespace to the new one
12231229
*/
1224-
copydir(src_dbpath, dst_dbpath, false);
1230+
if (src_compressed ^ dst_compressed) {
1231+
copyzipdir(src_dbpath, src_compressed, dst_dbpath, dst_compressed);
1232+
} else {
1233+
copydir(src_dbpath, dst_dbpath, false);
1234+
}
12251235

12261236
/*
12271237
* Record the filesystem change in XLOG

src/backend/commands/tablespace.c

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#include <unistd.h>
5050
#include <dirent.h>
51+
#include <stdio.h>
5152
#include <sys/types.h>
5253
#include <sys/stat.h>
5354

@@ -65,20 +66,23 @@
6566
#include "catalog/objectaccess.h"
6667
#include "catalog/pg_namespace.h"
6768
#include "catalog/pg_tablespace.h"
69+
#include "commands/defrem.h"
6870
#include "commands/comment.h"
6971
#include "commands/seclabel.h"
7072
#include "commands/tablecmds.h"
7173
#include "commands/tablespace.h"
7274
#include "miscadmin.h"
7375
#include "postmaster/bgwriter.h"
7476
#include "storage/fd.h"
77+
#include "storage/cfs.h"
7578
#include "storage/lmgr.h"
7679
#include "storage/standby.h"
7780
#include "utils/acl.h"
7881
#include "utils/builtins.h"
7982
#include "utils/fmgroids.h"
8083
#include "utils/guc.h"
8184
#include "utils/lsyscache.h"
85+
#include "utils/spccache.h"
8286
#include "utils/memutils.h"
8387
#include "utils/rel.h"
8488
#include "utils/tqual.h"
@@ -90,7 +94,7 @@ char *temp_tablespaces = NULL;
9094

9195

9296
static void create_tablespace_directories(const char *location,
93-
const Oid tablespaceoid);
97+
const Oid tablespaceoid, bool compressed);
9498
static bool destroy_tablespace_directories(Oid tablespaceoid, bool redo);
9599

96100

@@ -222,6 +226,23 @@ TablespaceCreateDbspace(Oid spcNode, Oid dbNode, bool isRedo)
222226
pfree(dir);
223227
}
224228

229+
static bool
230+
getBoolOption(List* options, char const* name, bool defaultValue)
231+
{
232+
ListCell *cell;
233+
foreach(cell, options)
234+
{
235+
DefElem *def = (DefElem *) lfirst(cell);
236+
if (strcmp(def->defname, name) == 0)
237+
{
238+
return defGetBoolean(def);
239+
}
240+
}
241+
return defaultValue;
242+
}
243+
244+
245+
225246
/*
226247
* Create a table space
227248
*
@@ -241,6 +262,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
241262
char *location;
242263
Oid ownerId;
243264
Datum newOptions;
265+
bool compressed;
244266

245267
/* Must be super user */
246268
if (!superuser())
@@ -256,6 +278,9 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
256278
else
257279
ownerId = GetUserId();
258280

281+
/* If tablespace should be compressed */
282+
compressed = getBoolOption(stmt->options, "compression", false);
283+
259284
/* Unix-ify the offered path, and strip any trailing slashes */
260285
location = pstrdup(stmt->location);
261286
canonicalize_path(location);
@@ -355,14 +380,14 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
355380
/* Post creation hook for new tablespace */
356381
InvokeObjectPostCreateHook(TableSpaceRelationId, tablespaceoid, 0);
357382

358-
create_tablespace_directories(location, tablespaceoid);
383+
create_tablespace_directories(location, tablespaceoid, getBoolOption(stmt->options, "compression", false));
359384

360385
/* Record the filesystem change in XLOG */
361386
{
362387
xl_tblspc_create_rec xlrec;
363388

364389
xlrec.ts_id = tablespaceoid;
365-
390+
xlrec.ts_compressed = compressed;
366391
XLogBeginInsert();
367392
XLogRegisterData((char *) &xlrec,
368393
offsetof(xl_tblspc_create_rec, ts_path));
@@ -562,7 +587,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
562587
* to the specified directory
563588
*/
564589
static void
565-
create_tablespace_directories(const char *location, const Oid tablespaceoid)
590+
create_tablespace_directories(const char *location, const Oid tablespaceoid, bool compressed)
566591
{
567592
char *linkloc;
568593
char *location_with_version_dir;
@@ -626,6 +651,16 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
626651
location_with_version_dir)));
627652
}
628653

654+
if (compressed)
655+
{
656+
char* compressionFilePath = psprintf("%s/pg_compression", location_with_version_dir);
657+
FILE* comp = fopen(compressionFilePath, "w");
658+
elog(LOG, "Create compressed tablespace at %s", location);
659+
fputs(cfs_algorithm(), comp);
660+
fclose(comp);
661+
pfree(compressionFilePath);
662+
}
663+
629664
/*
630665
* In recovery, remove old symlink, in case it points to the wrong place.
631666
*/
@@ -664,6 +699,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
664699
{
665700
char *linkloc;
666701
char *linkloc_with_version_dir;
702+
char *compression_file;
667703
DIR *dirdesc;
668704
struct dirent *de;
669705
char *subfile;
@@ -672,6 +708,9 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
672708
linkloc_with_version_dir = psprintf("pg_tblspc/%u/%s", tablespaceoid,
673709
TABLESPACE_VERSION_DIRECTORY);
674710

711+
compression_file = psprintf("%s/pg_compression", linkloc_with_version_dir);
712+
unlink(compression_file);
713+
675714
/*
676715
* Check if the tablespace still contains any files. We try to rmdir each
677716
* per-database directory we find in it. rmdir failure implies there are
@@ -840,7 +879,8 @@ directory_is_empty(const char *path)
840879
while ((de = ReadDir(dirdesc, path)) != NULL)
841880
{
842881
if (strcmp(de->d_name, ".") == 0 ||
843-
strcmp(de->d_name, "..") == 0)
882+
strcmp(de->d_name, "..") == 0 ||
883+
strcmp(de->d_name, "pg_compression") == 0)
844884
continue;
845885
FreeDir(dirdesc);
846886
return false;
@@ -1000,6 +1040,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt)
10001040
bool repl_null[Natts_pg_tablespace];
10011041
bool repl_repl[Natts_pg_tablespace];
10021042
HeapTuple newtuple;
1043+
bool compressed;
10031044

10041045
/* Search pg_tablespace */
10051046
rel = heap_open(TableSpaceRelationId, RowExclusiveLock);
@@ -1018,6 +1059,27 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt)
10181059

10191060
tablespaceoid = HeapTupleGetOid(tup);
10201061

1062+
/* If tablespace should be compressed */
1063+
compressed = getBoolOption(stmt->options, "compression", false);
1064+
1065+
if (compressed ^ is_tablespace_compressed(tablespaceoid)) {
1066+
char* tbsdir = psprintf("pg_tblspc/%u/%s", tablespaceoid, TABLESPACE_VERSION_DIRECTORY);
1067+
if (!directory_is_empty(tbsdir)) {
1068+
ereport(ERROR,
1069+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1070+
errmsg("It is not possible to toggle compression option for tablespace")));
1071+
} else {
1072+
char* compressionFilePath = psprintf("%s/pg_compression", tbsdir);
1073+
if (compressed) {
1074+
FILE* comp = fopen(compressionFilePath, "w");
1075+
fputs(cfs_algorithm(), comp);
1076+
fclose(comp);
1077+
} else {
1078+
unlink(compressionFilePath);
1079+
}
1080+
}
1081+
}
1082+
10211083
/* Must be owner of the existing object */
10221084
if (!pg_tablespace_ownercheck(HeapTupleGetOid(tup), GetUserId()))
10231085
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TABLESPACE,
@@ -1478,7 +1540,7 @@ tblspc_redo(XLogReaderState *record)
14781540
xl_tblspc_create_rec *xlrec = (xl_tblspc_create_rec *) XLogRecGetData(record);
14791541
char *location = xlrec->ts_path;
14801542

1481-
create_tablespace_directories(location, xlrec->ts_id);
1543+
create_tablespace_directories(location, xlrec->ts_id, xlrec->ts_compressed);
14821544
}
14831545
else if (info == XLOG_TBLSPC_DROP)
14841546
{

src/backend/postmaster/postmaster.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
#include "postmaster/syslogger.h"
116116
#include "replication/walsender.h"
117117
#include "storage/fd.h"
118+
#include "storage/cfs.h"
118119
#include "storage/ipc.h"
119120
#include "storage/pg_shmem.h"
120121
#include "storage/pmsignal.h"
@@ -502,6 +503,7 @@ typedef struct
502503
bool IsBinaryUpgrade;
503504
int max_safe_fds;
504505
int MaxBackends;
506+
CfsState *cfs_state;
505507
#ifdef WIN32
506508
HANDLE PostmasterHandle;
507509
HANDLE initial_signal_pipe;
@@ -1238,6 +1240,11 @@ PostmasterMain(int argc, char *argv[])
12381240
*/
12391241
autovac_init();
12401242

1243+
/*
1244+
* Initialize compressed file sysystem support
1245+
*/
1246+
cfs_initialize();
1247+
12411248
/*
12421249
* Load configuration files for client authentication.
12431250
*/
@@ -5836,6 +5843,8 @@ save_backend_variables(BackendParameters *param, Port *port,
58365843

58375844
param->MaxBackends = MaxBackends;
58385845

5846+
param->cfs_state = cfs_state;
5847+
58395848
#ifdef WIN32
58405849
param->PostmasterHandle = PostmasterHandle;
58415850
if (!write_duplicated_handle(&param->initial_signal_pipe,
@@ -6068,6 +6077,8 @@ restore_backend_variables(BackendParameters *param, Port *port)
60686077

60696078
MaxBackends = param->MaxBackends;
60706079

6080+
cfs_state = param->cfs_state;
6081+
60716082
#ifdef WIN32
60726083
PostmasterHandle = param->PostmasterHandle;
60736084
pgwin32_initial_signal_pipe = param->initial_signal_pipe;

src/backend/storage/file/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ subdir = src/backend/storage/file
1212
top_builddir = ../../../..
1313
include $(top_builddir)/src/Makefile.global
1414

15-
OBJS = fd.o buffile.o copydir.o reinit.o
15+
OBJS = fd.o cfs.o buffile.o copydir.o reinit.o
1616

1717
include $(top_srcdir)/src/backend/common.mk

0 commit comments

Comments
 (0)