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

Commit cdbb0ed

Browse files
author
Commitfest Bot
committed
[PATCH]: ./v1-handle-partitioned-tables-better.patch
1 parent 03c53a7 commit cdbb0ed

7 files changed

+223
-41
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,12 +1988,14 @@ buildTocEntryArrays(ArchiveHandle *AH)
19881988
AH->tocsByDumpId[te->dumpId] = te;
19891989

19901990
/*
1991-
* tableDataId provides the TABLE DATA item's dump ID for each TABLE
1992-
* TOC entry that has a DATA item. We compute this by reversing the
1993-
* TABLE DATA item's dependency, knowing that a TABLE DATA item has
1994-
* just one dependency and it is the TABLE item.
1991+
* tableDataId provides the DATA item's dump ID for each TABLE TOC
1992+
* entry that has a TABLE DATA or PARTITIONED DATA item. We compute
1993+
* this by reversing the DATA item's dependency, knowing that its
1994+
* first dependency is the TABLE item.
19951995
*/
1996-
if (strcmp(te->desc, "TABLE DATA") == 0 && te->nDeps > 0)
1996+
if (te->nDeps > 0 &&
1997+
(strcmp(te->desc, "TABLE DATA") == 0 ||
1998+
strcmp(te->desc, "PARTITIONED DATA") == 0))
19971999
{
19982000
DumpId tableId = te->dependencies[0];
19992001

@@ -2003,7 +2005,7 @@ buildTocEntryArrays(ArchiveHandle *AH)
20032005
* item's dump ID, so there should be a place for it in the array.
20042006
*/
20052007
if (tableId <= 0 || tableId > maxDumpId)
2006-
pg_fatal("bad table dumpId for TABLE DATA item");
2008+
pg_fatal("bad table dumpId for %s item", te->desc);
20072009

20082010
AH->tableDataId[tableId] = te->dumpId;
20092011
}
@@ -3140,6 +3142,7 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
31403142
{
31413143
if (strcmp(te->desc, "TABLE") == 0 ||
31423144
strcmp(te->desc, "TABLE DATA") == 0 ||
3145+
strcmp(te->desc, "PARTITIONED DATA") == 0 ||
31433146
strcmp(te->desc, "VIEW") == 0 ||
31443147
strcmp(te->desc, "FOREIGN TABLE") == 0 ||
31453148
strcmp(te->desc, "MATERIALIZED VIEW") == 0 ||
@@ -3194,13 +3197,14 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
31943197
if (!te->hadDumper)
31953198
{
31963199
/*
3197-
* Special Case: If 'SEQUENCE SET' or anything to do with LOs, then it
3198-
* is considered a data entry. We don't need to check for BLOBS or
3199-
* old-style BLOB COMMENTS entries, because they will have hadDumper =
3200-
* true ... but we do need to check new-style BLOB ACLs, comments,
3201-
* etc.
3200+
* Special Case: If 'PARTITIONED DATA', 'SEQUENCE SET' or anything to
3201+
* do with LOs, then it is considered a data entry. We don't need to
3202+
* check for BLOBS or old-style BLOB COMMENTS entries, because they
3203+
* will have hadDumper = true ... but we do need to check new-style
3204+
* BLOB ACLs, comments, etc.
32023205
*/
3203-
if (strcmp(te->desc, "SEQUENCE SET") == 0 ||
3206+
if (strcmp(te->desc, "PARTITIONED DATA") == 0 ||
3207+
strcmp(te->desc, "SEQUENCE SET") == 0 ||
32043208
strcmp(te->desc, "BLOB") == 0 ||
32053209
strcmp(te->desc, "BLOB METADATA") == 0 ||
32063210
(strcmp(te->desc, "ACL") == 0 &&
@@ -4996,14 +5000,14 @@ identify_locking_dependencies(ArchiveHandle *AH, TocEntry *te)
49965000
return;
49975001

49985002
/*
4999-
* We assume the entry requires exclusive lock on each TABLE or TABLE DATA
5000-
* item listed among its dependencies. Originally all of these would have
5001-
* been TABLE items, but repoint_table_dependencies would have repointed
5002-
* them to the TABLE DATA items if those are present (which they might not
5003-
* be, eg in a schema-only dump). Note that all of the entries we are
5004-
* processing here are POST_DATA; otherwise there might be a significant
5005-
* difference between a dependency on a table and a dependency on its
5006-
* data, so that closer analysis would be needed here.
5003+
* We assume the entry requires exclusive lock on each TABLE, TABLE DATA,
5004+
* or PARTITIONED DATA item listed among its dependencies. Originally all
5005+
* of these would have been TABLE items, but repoint_table_dependencies
5006+
* would have repointed them to the DATA items if those are present (which
5007+
* they might not be, eg in a schema-only dump). Note that all of the
5008+
* entries we are processing here are POST_DATA; otherwise there might be
5009+
* a significant difference between a dependency on a table and a
5010+
* dependency on its data, so that closer analysis would be needed here.
50075011
*/
50085012
lockids = (DumpId *) pg_malloc(te->nDeps * sizeof(DumpId));
50095013
nlockids = 0;
@@ -5012,8 +5016,9 @@ identify_locking_dependencies(ArchiveHandle *AH, TocEntry *te)
50125016
DumpId depid = te->dependencies[i];
50135017

50145018
if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL &&
5015-
((strcmp(AH->tocsByDumpId[depid]->desc, "TABLE DATA") == 0) ||
5016-
strcmp(AH->tocsByDumpId[depid]->desc, "TABLE") == 0))
5019+
(strcmp(AH->tocsByDumpId[depid]->desc, "TABLE") == 0 ||
5020+
strcmp(AH->tocsByDumpId[depid]->desc, "TABLE DATA") == 0 ||
5021+
strcmp(AH->tocsByDumpId[depid]->desc, "PARTITIONED DATA") == 0))
50175022
lockids[nlockids++] = depid;
50185023
}
50195024

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ struct _archiveHandle
310310

311311
/* arrays created after the TOC list is complete: */
312312
struct _tocEntry **tocsByDumpId; /* TOCs indexed by dumpId */
313-
DumpId *tableDataId; /* TABLE DATA ids, indexed by table dumpId */
313+
DumpId *tableDataId; /* TABLE DATA and PARTITIONED DATA dumpIds,
314+
* indexed by table dumpId */
314315

315316
struct _tocEntry *currToc; /* Used when dumping data */
316317
pg_compress_specification compression_spec; /* Requested specification for

src/bin/pg_dump/pg_backup_custom.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
#include "postgres_fe.h"
2727

28+
#include <limits.h>
29+
2830
#include "common/file_utils.h"
2931
#include "compress_io.h"
3032
#include "pg_backup_utils.h"
@@ -819,17 +821,18 @@ _ReopenArchive(ArchiveHandle *AH)
819821
/*
820822
* Prepare for parallel restore.
821823
*
822-
* The main thing that needs to happen here is to fill in TABLE DATA and BLOBS
823-
* TOC entries' dataLength fields with appropriate values to guide the
824-
* ordering of restore jobs. The source of said data is format-dependent,
825-
* as is the exact meaning of the values.
824+
* The main thing that needs to happen here is to fill in TABLE DATA,
825+
* PARTITIONED_DATA, and BLOBS TOC entries' dataLength fields with appropriate
826+
* values to guide the ordering of restore jobs. The source of said data is
827+
* format-dependent, as is the exact meaning of the values.
826828
*
827829
* A format module might also choose to do other setup here.
828830
*/
829831
static void
830832
_PrepParallelRestore(ArchiveHandle *AH)
831833
{
832834
lclContext *ctx = (lclContext *) AH->formatData;
835+
bool have_partitioned_data = false;
833836
TocEntry *prev_te = NULL;
834837
lclTocEntry *prev_tctx = NULL;
835838
TocEntry *te;
@@ -843,6 +846,10 @@ _PrepParallelRestore(ArchiveHandle *AH)
843846
{
844847
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
845848

849+
/* Track whether there are any PARTITIONED_DATA items */
850+
if (!have_partitioned_data && strcmp(te->desc, "PARTITIONED DATA") == 0)
851+
have_partitioned_data = true;
852+
846853
/*
847854
* Ignore entries without a known data offset; if we were unable to
848855
* seek to rewrite the TOC when creating the archive, this'll be all
@@ -873,6 +880,38 @@ _PrepParallelRestore(ArchiveHandle *AH)
873880
if (endpos > prev_tctx->dataPos)
874881
prev_te->dataLength = endpos - prev_tctx->dataPos;
875882
}
883+
884+
/*
885+
* For PARTITIONED DATA items, add up the sizes of their child objects.
886+
* (We couldn't do this earlier, since when we encounter a PARTITIONED
887+
* DATA item in the first loop we typically don't know the dataLength of
888+
* its last child yet.)
889+
*/
890+
if (have_partitioned_data)
891+
{
892+
for (te = AH->toc->next; te != AH->toc; te = te->next)
893+
{
894+
if (strcmp(te->desc, "PARTITIONED DATA") != 0)
895+
continue;
896+
for (int i = 0; i < te->nDeps; i++)
897+
{
898+
DumpId depid = te->dependencies[i];
899+
900+
if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL)
901+
{
902+
pgoff_t childLength = AH->tocsByDumpId[depid]->dataLength;
903+
904+
te->dataLength += childLength;
905+
/* handle overflow -- unlikely except with 32-bit pgoff_t */
906+
if (unlikely(te->dataLength < 0))
907+
{
908+
te->dataLength = INT_MAX;
909+
break;
910+
}
911+
}
912+
}
913+
}
914+
}
876915
}
877916

878917
/*

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "postgres_fe.h"
3838

3939
#include <dirent.h>
40+
#include <limits.h>
4041
#include <sys/stat.h>
4142

4243
#include "common/file_utils.h"
@@ -722,16 +723,17 @@ setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
722723
/*
723724
* Prepare for parallel restore.
724725
*
725-
* The main thing that needs to happen here is to fill in TABLE DATA and BLOBS
726-
* TOC entries' dataLength fields with appropriate values to guide the
727-
* ordering of restore jobs. The source of said data is format-dependent,
728-
* as is the exact meaning of the values.
726+
* The main thing that needs to happen here is to fill in TABLE DATA,
727+
* PARTITIONED_DATA, and BLOBS TOC entries' dataLength fields with appropriate
728+
* values to guide the ordering of restore jobs. The source of said data is
729+
* format-dependent, as is the exact meaning of the values.
729730
*
730731
* A format module might also choose to do other setup here.
731732
*/
732733
static void
733734
_PrepParallelRestore(ArchiveHandle *AH)
734735
{
736+
bool have_partitioned_data = false;
735737
TocEntry *te;
736738

737739
for (te = AH->toc->next; te != AH->toc; te = te->next)
@@ -740,6 +742,10 @@ _PrepParallelRestore(ArchiveHandle *AH)
740742
char fname[MAXPGPATH];
741743
struct stat st;
742744

745+
/* Track whether there are any PARTITIONED_DATA items */
746+
if (!have_partitioned_data && strcmp(te->desc, "PARTITIONED DATA") == 0)
747+
have_partitioned_data = true;
748+
743749
/*
744750
* A dumpable object has set tctx->filename, any other object has not.
745751
* (see _ArchiveEntry).
@@ -784,6 +790,38 @@ _PrepParallelRestore(ArchiveHandle *AH)
784790
if (strcmp(te->desc, "BLOBS") == 0)
785791
te->dataLength *= 1024;
786792
}
793+
794+
/*
795+
* For PARTITIONED DATA items, add up the sizes of their child objects.
796+
* (Unlike pg_backup_custom.c, we could theoretically do this within the
797+
* previous loop, but it seems best to keep the logic looking the same in
798+
* both functions.)
799+
*/
800+
if (have_partitioned_data)
801+
{
802+
for (te = AH->toc->next; te != AH->toc; te = te->next)
803+
{
804+
if (strcmp(te->desc, "PARTITIONED DATA") != 0)
805+
continue;
806+
for (int i = 0; i < te->nDeps; i++)
807+
{
808+
DumpId depid = te->dependencies[i];
809+
810+
if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL)
811+
{
812+
pgoff_t childLength = AH->tocsByDumpId[depid]->dataLength;
813+
814+
te->dataLength += childLength;
815+
/* handle overflow -- unlikely except with 32-bit pgoff_t */
816+
if (unlikely(te->dataLength < 0))
817+
{
818+
te->dataLength = INT_MAX;
819+
break;
820+
}
821+
}
822+
}
823+
}
824+
}
787825
}
788826

789827
/*

0 commit comments

Comments
 (0)