Skip to content

Commit 58752c5

Browse files
author
oleg gurev
committed
[PBCKP-198] Added tablespaces into show command output
- Solve Issue #431 - Output all tablespaces in backup in JSON output - Output all tablespaces in backup in PLAIN output
1 parent 9a91ea7 commit 58752c5

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Diff for: src/show.c

+54
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static void show_archive_plain(const char *instance_name, uint32 xlog_seg_size,
6767
parray *timelines_list, bool show_name);
6868
static void show_archive_json(const char *instance_name, uint32 xlog_seg_size,
6969
parray *tli_list);
70+
static bool backup_has_tablespace_map(pgBackup *backup);
7071

7172
static PQExpBufferData show_buf;
7273
static bool first_instance = true;
@@ -479,6 +480,32 @@ print_backup_json_object(PQExpBuffer buf, pgBackup *backup)
479480
appendPQExpBuffer(buf, "%u", backup->content_crc);
480481
}
481482

483+
/* print tablespaces list */
484+
if (backup_has_tablespace_map(backup))
485+
{
486+
parray *links = parray_new();
487+
488+
json_add_key(buf, "tablespace_map", json_level);
489+
json_add(buf, JT_BEGIN_ARRAY, &json_level);
490+
491+
read_tablespace_map(links, backup->root_dir);
492+
parray_qsort(links, pgFileCompareLinked);
493+
494+
for (size_t i = 0; i < parray_num(links); i++){
495+
pgFile *link = (pgFile *) parray_get(links, i);
496+
if (i)
497+
appendPQExpBufferChar(buf, ',');
498+
json_add(buf, JT_BEGIN_OBJECT, &json_level);
499+
json_add_value(buf, "oid", link->name, json_level, true);
500+
json_add_value(buf, "path", link->linked, json_level, true);
501+
json_add(buf, JT_END_OBJECT, &json_level);
502+
}
503+
/* End of tablespaces */
504+
json_add(buf, JT_END_ARRAY, &json_level);
505+
parray_walk(links, pgFileFree);
506+
parray_free(links);
507+
}
508+
482509
json_add(buf, JT_END_OBJECT, &json_level);
483510
}
484511

@@ -521,7 +548,27 @@ show_backup(InstanceState *instanceState, time_t requested_backup_id)
521548
}
522549

523550
if (show_format == SHOW_PLAIN)
551+
{
524552
pgBackupWriteControl(stdout, backup, false);
553+
554+
/* print tablespaces list */
555+
if (backup_has_tablespace_map(backup))
556+
{
557+
parray *links = parray_new();
558+
559+
fio_fprintf(stdout, "\ntablespace_map = '");
560+
561+
read_tablespace_map(links, backup->root_dir);
562+
parray_qsort(links, pgFileCompareLinked);
563+
564+
for (size_t i = 0; i < parray_num(links); i++){
565+
pgFile *link = (pgFile *) parray_get(links, i);
566+
fio_fprintf(stdout, "%s %s%s", link->name, link->linked, (i < parray_num(links) - 1) ? "; " : "'\n");
567+
}
568+
parray_walk(links, pgFileFree);
569+
parray_free(links);
570+
}
571+
}
525572
else
526573
elog(ERROR, "Invalid show format %d", (int) show_format);
527574

@@ -1174,3 +1221,10 @@ show_archive_json(const char *instance_name, uint32 xlog_seg_size,
11741221

11751222
first_instance = false;
11761223
}
1224+
1225+
static bool backup_has_tablespace_map(pgBackup *backup)
1226+
{
1227+
char map_path[MAXPGPATH];
1228+
join_path_components(map_path, backup->database_dir, PG_TABLESPACE_MAP_FILE);
1229+
return fileExists(map_path, FIO_BACKUP_HOST);
1230+
}

Diff for: tests/show_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,39 @@ def test_color_with_no_terminal(self):
507507
'[0m', e.message,
508508
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(
509509
repr(e.message), self.cmd))
510+
511+
# @unittest.skip("skip")
512+
def test_tablespace_print_issue_431(self):
513+
backup_dir = os.path.join(self.tmp_path, self.module_name, self.fname, 'backup')
514+
node = self.make_simple_node(
515+
base_dir=os.path.join(self.module_name, self.fname, 'node'),
516+
initdb_params=['--data-checksums'])
517+
518+
self.init_pb(backup_dir)
519+
self.add_instance(backup_dir, 'node', node)
520+
self.set_archiving(backup_dir, 'node', node)
521+
node.slow_start()
522+
523+
# Create tablespace
524+
tblspc_path = os.path.join(node.base_dir, "tblspc")
525+
os.makedirs(tblspc_path)
526+
with node.connect("postgres") as con:
527+
con.connection.autocommit = True
528+
con.execute("CREATE TABLESPACE tblspc LOCATION '%s'" % tblspc_path)
529+
con.connection.autocommit = False
530+
con.execute("CREATE TABLE test (id int) TABLESPACE tblspc")
531+
con.execute("INSERT INTO test VALUES (1)")
532+
con.commit()
533+
534+
full_backup_id = self.backup_node(backup_dir, 'node', node)
535+
self.assertIn("OK", self.show_pb(backup_dir,'node', as_text=True))
536+
# Check that tablespace info exists. JSON
537+
self.assertIn("tablespace_map", self.show_pb(backup_dir, 'node', as_text=True))
538+
self.assertIn("oid", self.show_pb(backup_dir, 'node', as_text=True))
539+
self.assertIn("path", self.show_pb(backup_dir, 'node', as_text=True))
540+
self.assertIn(tblspc_path, self.show_pb(backup_dir, 'node', as_text=True))
541+
# Check that tablespace info exists. PLAIN
542+
self.assertIn("tablespace_map", self.show_pb(backup_dir, 'node', backup_id=full_backup_id, as_text=True, as_json=False))
543+
self.assertIn(tblspc_path, self.show_pb(backup_dir, 'node', backup_id=full_backup_id, as_text=True, as_json=False))
544+
# Check that tablespace info NOT exists if backup id not provided. PLAIN
545+
self.assertNotIn("tablespace_map", self.show_pb(backup_dir, 'node', as_text=True, as_json=False))

0 commit comments

Comments
 (0)