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

Commit13e8643

Browse files
author
Amit Kapila
committed
During pg_upgrade, conditionally skip transfer of FSMs.
If a heap on the old cluster has 4 pages or fewer, and the old clusterwas PG v11 or earlier, don't copy or link the FSM. This will shrinkspace usage for installations with large numbers of small tables.This will allow pg_upgrade to take advantage of commitb0eaa4c wherewe have avoided creation of the free space map for small heap relations.Author: John NaylorReviewed-by: Amit KapilaDiscussion:https://postgr.es/m/CACPNZCu4cOdm3uGnNEGXivy7Gz8UWyQjynDpdkPGabQ18_zK6g%40mail.gmail.com
1 parent2fadf24 commit13e8643

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

‎doc/src/sgml/ref/pgupgrade.sgml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,13 @@ psql --username=postgres --file=script.sql postgres
792792
is down.
793793
</para>
794794

795+
<para>
796+
In <productname>PostgreSQL</productname> 12 and later small tables by
797+
default don't have a free space map, as a space optimization. If you are
798+
upgrading a pre-12 cluster, the free space maps of small tables will
799+
likewise not be transferred to the new cluster.
800+
</para>
801+
795802
</refsect1>
796803

797804
<refsect1>

‎src/bin/pg_upgrade/info.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
200200

201201
map->old_db_oid=old_db->db_oid;
202202
map->new_db_oid=new_db->db_oid;
203+
map->relpages=old_rel->relpages;
204+
map->relkind=old_rel->relkind;
203205

204206
/*
205207
* old_relfilenode might differ from pg_class.oid (and hence
@@ -418,14 +420,17 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
418420
char*nspname=NULL;
419421
char*relname=NULL;
420422
char*tablespace=NULL;
423+
char*relkind=NULL;
421424
inti_spclocation,
422425
i_nspname,
423426
i_relname,
424427
i_reloid,
425428
i_indtable,
426429
i_toastheap,
427430
i_relfilenode,
428-
i_reltablespace;
431+
i_reltablespace,
432+
i_relpages,
433+
i_relkind;
429434
charquery[QUERY_ALLOC];
430435
char*last_namespace=NULL,
431436
*last_tablespace=NULL;
@@ -494,7 +499,7 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
494499
*/
495500
snprintf(query+strlen(query),sizeof(query)-strlen(query),
496501
"SELECT all_rels.*, n.nspname, c.relname, "
497-
" c.relfilenode, c.reltablespace, %s "
502+
" c.relfilenode, c.reltablespace,c.relpages, c.relkind,%s "
498503
"FROM (SELECT * FROM regular_heap "
499504
" UNION ALL "
500505
" SELECT * FROM toast_heap "
@@ -525,6 +530,8 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
525530
i_relname=PQfnumber(res,"relname");
526531
i_relfilenode=PQfnumber(res,"relfilenode");
527532
i_reltablespace=PQfnumber(res,"reltablespace");
533+
i_relpages=PQfnumber(res,"relpages");
534+
i_relkind=PQfnumber(res,"relkind");
528535
i_spclocation=PQfnumber(res,"spclocation");
529536

530537
for (relnum=0;relnum<ntups;relnum++)
@@ -556,6 +563,11 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
556563
curr->relname=pg_strdup(relname);
557564

558565
curr->relfilenode=atooid(PQgetvalue(res,relnum,i_relfilenode));
566+
curr->relpages=atoi(PQgetvalue(res,relnum,i_relpages));
567+
568+
relkind=PQgetvalue(res,relnum,i_relkind);
569+
curr->relkind=relkind[0];
570+
559571
curr->tblsp_alloc= false;
560572

561573
/* Is the tablespace oid non-default? */

‎src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ typedef struct
147147
char*tablespace;/* tablespace path; "" for cluster default */
148148
boolnsp_alloc;/* should nspname be freed? */
149149
booltblsp_alloc;/* should tablespace be freed? */
150+
int32relpages;/* # of pages -- see pg_class.h */
151+
charrelkind;/* relation kind -- see pg_class.h */
150152
}RelInfo;
151153

152154
typedefstruct
@@ -173,6 +175,10 @@ typedef struct
173175
*/
174176
Oidold_relfilenode;
175177
Oidnew_relfilenode;
178+
179+
int32relpages;/* # of pages -- see pg_class.h */
180+
charrelkind;/* relation kind -- see pg_class.h */
181+
176182
/* the rest are used only for logging and error reporting */
177183
char*nspname;/* namespaces */
178184
char*relname;

‎src/bin/pg_upgrade/relfilenode.c

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#include<sys/stat.h>
1515
#include"catalog/pg_class_d.h"
1616
#include"access/transam.h"
17+
#include"storage/freespace.h"
1718

1819

1920
staticvoidtransfer_single_new_db(FileNameMap*maps,intsize,char*old_tablespace);
2021
staticvoidtransfer_relfile(FileNameMap*map,constchar*suffix,boolvm_must_add_frozenbit);
22+
staticboolnew_cluster_needs_fsm(FileNameMap*map);
2123

2224

2325
/*
@@ -174,7 +176,8 @@ transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
174176
/*
175177
* Copy/link any fsm and vm files, if they exist
176178
*/
177-
transfer_relfile(&maps[mapnum],"_fsm",vm_must_add_frozenbit);
179+
if (new_cluster_needs_fsm(&maps[mapnum]))
180+
transfer_relfile(&maps[mapnum],"_fsm",vm_must_add_frozenbit);
178181
if (vm_crashsafe_match)
179182
transfer_relfile(&maps[mapnum],"_vm",vm_must_add_frozenbit);
180183
}
@@ -278,3 +281,61 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
278281
}
279282
}
280283
}
284+
285+
/*
286+
* new_cluster_needs_fsm()
287+
*
288+
* Return false for small heaps if we're upgrading across PG 12, the first
289+
* version where small heap relations don't have FSMs by default.
290+
*/
291+
staticbool
292+
new_cluster_needs_fsm(FileNameMap*map)
293+
{
294+
charold_primary_file[MAXPGPATH];
295+
structstatstatbuf;
296+
297+
/* fsm/vm files added in PG 8.4 */
298+
Assert(GET_MAJOR_VERSION(old_cluster.major_version) >=804);
299+
300+
if (!(GET_MAJOR_VERSION(old_cluster.major_version) <=1100&&
301+
GET_MAJOR_VERSION(new_cluster.major_version) >=1200))
302+
return true;
303+
304+
/* Always transfer FSMs of non-heap relations. */
305+
if (map->relkind!=RELKIND_RELATION&&
306+
map->relkind!=RELKIND_TOASTVALUE)
307+
return true;
308+
309+
/*
310+
* If pg_class.relpages falsely reports that the heap is above the
311+
* threshold, we will transfer a FSM when we don't need to, but this is
312+
* harmless.
313+
*/
314+
if (map->relpages>HEAP_FSM_CREATION_THRESHOLD)
315+
return true;
316+
317+
/* Determine path of the primary file. */
318+
snprintf(old_primary_file,sizeof(old_primary_file),"%s%s/%u/%u",
319+
map->old_tablespace,
320+
map->old_tablespace_suffix,
321+
map->old_db_oid,
322+
map->old_relfilenode);
323+
324+
/*
325+
* If pg_class.relpages falsely reports that the heap is below the
326+
* threshold, a FSM would be skipped when we actually need it. To guard
327+
* against this, we verify the size of the primary file.
328+
*/
329+
if (stat(old_primary_file,&statbuf)!=0)
330+
{
331+
pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\"): %s\n",
332+
map->nspname,map->relname,old_primary_file,strerror(errno));
333+
334+
/* Keep compiler quiet. */
335+
return false;
336+
}
337+
elseif (statbuf.st_size>HEAP_FSM_CREATION_THRESHOLD*BLCKSZ)
338+
return true;
339+
else
340+
return false;
341+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp