Skip to content

Commit e3b0f44

Browse files
committed
Add support get ptrack from SQL for atomic.
1 parent 091fe0a commit e3b0f44

File tree

3 files changed

+99
-26
lines changed

3 files changed

+99
-26
lines changed

backup.c

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ static bool pg_is_standby(void);
5959
static void get_lsn(PGresult *res, XLogRecPtr *lsn);
6060
static void get_xid(PGresult *res, uint32 *xid);
6161
static void pg_ptrack_clear(void);
62-
62+
static char *pg_ptrack_get_and_clear(Oid tablespace_oid,
63+
Oid db_oid,
64+
Oid rel_oid,
65+
size_t *result_size);
6366
static void add_files(parray *files, const char *root, bool add_root, bool is_pgdata);
6467
static void create_file_list(parray *files,
6568
const char *root,
@@ -315,9 +318,6 @@ do_backup_database(parray *backup_list, pgBackupOption bkupopt)
315318
pg_free(backup_threads_args[i]);
316319
}
317320

318-
/* Clear ptrack files after backup */
319-
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
320-
pg_ptrack_clear();
321321
/* Notify end of backup */
322322
pg_stop_backup(&current);
323323

@@ -568,6 +568,38 @@ pg_ptrack_clear(void)
568568
dbname = old_dbname;
569569
}
570570

571+
static char *
572+
pg_ptrack_get_and_clear(Oid tablespace_oid, Oid db_oid, Oid rel_oid, size_t *result_size)
573+
{
574+
PGresult *res_db, *res;
575+
const char *old_dbname = dbname;
576+
char *params[2];
577+
char *result;
578+
579+
reconnect();
580+
params[0] = palloc(64);
581+
params[1] = palloc(64);
582+
sprintf(params[0], "%i", db_oid);
583+
sprintf(params[1], "%i", rel_oid);
584+
res_db = execute("SELECT datname FROM pg_database WHERE oid=$1", 1, (const char **)params);
585+
disconnect();
586+
dbname = pstrdup(PQgetvalue(res_db, 0, 0));
587+
PQclear(res_db);
588+
589+
reconnect();
590+
sprintf(params[0], "%i", tablespace_oid);
591+
res = execute("SELECT pg_ptrack_get_and_clear($1, $2)", 2, (const char **)params);
592+
result = (char *)PQunescapeBytea((unsigned char *)PQgetvalue(res, 0, 0), result_size);
593+
PQclear(res);
594+
pfree(params[0]);
595+
pfree(params[1]);
596+
597+
pfree((char *)dbname);
598+
dbname = old_dbname;
599+
600+
return result;
601+
}
602+
571603
static void
572604
wait_for_archive(pgBackup *backup, const char *sql)
573605
{
@@ -907,7 +939,7 @@ add_files(parray *files, const char *root, bool add_root, bool is_pgdata)
907939
relative = file->path + strlen(root) + 1;
908940
if (is_pgdata &&
909941
!path_is_prefix_of_path("base", relative) &&
910-
!path_is_prefix_of_path("global", relative) &&
942+
/*!path_is_prefix_of_path("global", relative) &&*/
911943
!path_is_prefix_of_path("pg_tblspc", relative))
912944
continue;
913945

@@ -1062,35 +1094,45 @@ void make_pagemap_from_ptrack(parray *files)
10621094
pgFile *p = (pgFile *) parray_get(files, i);
10631095
if (p->ptrack_path != NULL)
10641096
{
1065-
DataPage page;
1066-
char *flat_memory, *flat_mamory_cur;
1097+
char *flat_memory;
1098+
char *tmp_path = p->ptrack_path;
1099+
char *tablespace;
1100+
size_t path_length = strlen(p->ptrack_path);
10671101
size_t flat_size = 0;
10681102
size_t start_addr;
1069-
struct stat st;
1103+
Oid db_oid, rel_oid, tablespace_oid = 0;
1104+
int sep_iter, sep_count = 0;
1105+
tablespace = palloc0(64);
10701106

1071-
FILE *ptrack_file = fopen(p->ptrack_path, "r");
1072-
if (ptrack_file == NULL)
1107+
/* Find target path*/
1108+
for(sep_iter = (int)path_length; sep_iter >= 0; sep_iter--)
10731109
{
1074-
elog(ERROR, "cannot open ptrack file \"%s\": %s", p->ptrack_path,
1075-
strerror(errno));
1110+
if (IS_DIR_SEP(tmp_path[sep_iter]))
1111+
{
1112+
sep_count++;
1113+
}
1114+
if (sep_count == 3)
1115+
{
1116+
tmp_path += sep_iter + 1;
1117+
break;
1118+
}
10761119
}
1120+
/* For unix only now */
1121+
sscanf(tmp_path, "%[^/]/%u/%u_ptrack", tablespace, &db_oid, &rel_oid);
1122+
if (strcmp(tablespace, "base") != 0 && strcmp(tablespace, "global") != 0)
1123+
sscanf(tablespace, "%i", &tablespace_oid);
10771124

1078-
fstat(fileno(ptrack_file), &st);
1079-
flat_size = st.st_size-(st.st_size/BLCKSZ)*MAXALIGN(SizeOfPageHeaderData);
1080-
flat_mamory_cur = flat_memory = pg_malloc(flat_size);
1125+
flat_memory = pg_ptrack_get_and_clear(tablespace_oid,
1126+
db_oid,
1127+
rel_oid,
1128+
&flat_size);
10811129

1082-
while(fread(page.data, BLCKSZ, 1, ptrack_file) == 1)
1083-
{
1084-
char *map = PageGetContents(page.data);
1085-
memcpy(flat_memory, map, MAPSIZE);
1086-
flat_mamory_cur += MAPSIZE;
1087-
}
1088-
fclose(ptrack_file);
10891130
start_addr = (RELSEG_SIZE/8)*p->segno;
10901131
p->pagemap.bitmapsize = start_addr+RELSEG_SIZE/8 > flat_size ? flat_size - start_addr : RELSEG_SIZE/8;
10911132
p->pagemap.bitmap = pg_malloc(p->pagemap.bitmapsize);
10921133
memcpy(p->pagemap.bitmap, flat_memory+start_addr, p->pagemap.bitmapsize);
10931134
pg_free(flat_memory);
1135+
pg_free(tablespace);
10941136
}
10951137
}
10961138
}

restore.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ create_recovery_conf(const char *target_time,
476476
fprintf(fp, "recovery_target_xid = '%s'\n", target_xid);
477477
if (target_inclusive)
478478
fprintf(fp, "recovery_target_inclusive = '%s'\n", target_inclusive);
479+
/*fprintf(fp, "recovery_target = 'immediate'\n");*/
479480
fprintf(fp, "recovery_target_timeline = '%u'\n", target_tli);
480481

481482
fclose(fp);

sql/restore.sh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,32 @@ psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT * FROM pgbench_branches
170170
diff ${TEST_BASE}/TEST-0007-before.out ${TEST_BASE}/TEST-0007-after.out
171171
echo ''
172172

173+
174+
echo '###### RESTORE COMMAND TEST-0010 ######'
175+
echo '###### recovery to latest from full + page backups with loads when full backup do ######'
176+
init_backup
177+
pgbench_objs 0009
178+
pgbench -p ${TEST_PGPORT} -d pgbench -c 4 -T 8 > /dev/null 2>&1 &
179+
PGBENCH_PID=$!
180+
pg_arman backup -B ${BACKUP_PATH} -b full -j 4 -p ${TEST_PGPORT} -d postgres --verbose > ${TEST_BASE}/TEST-0010-run.out 2>&1;echo $?
181+
pg_arman validate -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0010-run.out 2>&1
182+
#kill $PGBENCH_PID 2> /dev/null
183+
sleep 12
184+
#psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT count(*) FROM pgbench_history;" > ${TEST_BASE}/TEST-0010-count1.out
185+
pg_arman backup -B ${BACKUP_PATH} -b page -j 4 -p ${TEST_PGPORT} -d postgres --verbose >> ${TEST_BASE}/TEST-0010-run.out 2>&1;echo $?
186+
pg_arman validate -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0010-run.out 2>&1
187+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(bbalance) FROM pgbench_branches;" > ${TEST_BASE}/TEST-0010-count1.out
188+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(delta) FROM pgbench_history;" > ${TEST_BASE}/TEST-0010-count2.out
189+
diff ${TEST_BASE}/TEST-0010-count1.out ${TEST_BASE}/TEST-0010-count2.out
190+
pg_ctl stop -m immediate > /dev/null 2>&1
191+
pg_arman restore -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0010-run.out 2>&1;echo $?
192+
pg_ctl start -w -t 600 > /dev/null 2>&1
193+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(bbalance) FROM pgbench_branches;" > ${TEST_BASE}/TEST-0010-count1.out
194+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(delta) FROM pgbench_history;" > ${TEST_BASE}/TEST-0010-count2.out
195+
diff ${TEST_BASE}/TEST-0010-count1.out ${TEST_BASE}/TEST-0010-count2.out
196+
echo ''
197+
198+
173199
echo '###### RESTORE COMMAND TEST-0009 ######'
174200
echo '###### recovery to latest from full + ptrack backups with loads when full backup do ######'
175201
init_backup
@@ -180,13 +206,17 @@ pg_arman backup -B ${BACKUP_PATH} -b full -j 4 -p ${TEST_PGPORT} -d postgres --v
180206
pg_arman validate -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0009-run.out 2>&1
181207
#kill $PGBENCH_PID 2> /dev/null
182208
sleep 12
183-
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT count(*) FROM pgbench_history;" > ${TEST_BASE}/TEST-0009-count1.out
209+
#psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT count(*) FROM pgbench_history;" > ${TEST_BASE}/TEST-0009-count1.out
184210
pg_arman backup -B ${BACKUP_PATH} -b ptrack -j 4 -p ${TEST_PGPORT} -d postgres --verbose >> ${TEST_BASE}/TEST-0009-run.out 2>&1;echo $?
185-
pg_arman validate -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0006-run.out 2>&1
211+
pg_arman validate -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0009-run.out 2>&1
212+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(bbalance) FROM pgbench_branches;" > ${TEST_BASE}/TEST-0009-count1.out
213+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(delta) FROM pgbench_history;" > ${TEST_BASE}/TEST-0009-count2.out
214+
diff ${TEST_BASE}/TEST-0009-count1.out ${TEST_BASE}/TEST-0009-count2.out
186215
pg_ctl stop -m immediate > /dev/null 2>&1
187-
pg_arman restore -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0006-run.out 2>&1;echo $?
216+
pg_arman restore -B ${BACKUP_PATH} --verbose >> ${TEST_BASE}/TEST-0009-run.out 2>&1;echo $?
188217
pg_ctl start -w -t 600 > /dev/null 2>&1
189-
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT count(*) FROM pgbench_history;" > ${TEST_BASE}/TEST-0009-count2.out
218+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(bbalance) FROM pgbench_branches;" > ${TEST_BASE}/TEST-0009-count1.out
219+
psql --no-psqlrc -p ${TEST_PGPORT} -d pgbench -c "SELECT sum(delta) FROM pgbench_history;" > ${TEST_BASE}/TEST-0009-count2.out
190220
diff ${TEST_BASE}/TEST-0009-count1.out ${TEST_BASE}/TEST-0009-count2.out
191221
echo ''
192222

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy