@@ -783,13 +783,16 @@ do_backup(void)
783783
784784 is_ptrack_support = pg_ptrack_support ();
785785
786- if (!is_ptrack_support )
787- elog (ERROR , "This PostgreSQL instance does not support ptrack" );
788- else
786+ if (current .backup_mode == BACKUP_MODE_DIFF_PTRACK )
789787 {
790- is_ptrack_enable = pg_ptrack_enable ();
791- if (!is_ptrack_enable )
792- elog (ERROR , "Ptrack is disabled" );
788+ if (!is_ptrack_support )
789+ elog (ERROR , "This PostgreSQL instance does not support ptrack" );
790+ else
791+ {
792+ is_ptrack_enable = pg_ptrack_enable ();
793+ if (!is_ptrack_enable )
794+ elog (ERROR , "Ptrack is disabled" );
795+ }
793796 }
794797
795798 /* archiving check */
@@ -1180,12 +1183,34 @@ static bool
11801183pg_ptrack_get_and_clear_db (Oid dbOid , Oid tblspcOid )
11811184{
11821185 char * params [2 ];
1186+ PGresult * res_db ;
11831187 PGresult * res ;
11841188 char * result ;
11851189
11861190 params [0 ] = palloc (64 );
11871191 params [1 ] = palloc (64 );
11881192
1193+ sprintf (params [0 ], "%i" , dbOid );
1194+ res_db = pgut_execute (backup_conn ,
1195+ "SELECT datname FROM pg_database WHERE oid=$1" ,
1196+ 1 , (const char * * ) params );
1197+ /*
1198+ * If database is not found, it's not an error.
1199+ * It could have been deleted since previous backup.
1200+ */
1201+ if (PQntuples (res_db ) != 1 || PQnfields (res_db ) != 1 )
1202+ return false;
1203+
1204+ dbname = pstrdup (PQgetvalue (res_db , 0 , 0 ));
1205+ PQclear (res_db );
1206+
1207+ /* Always backup all files from template0 database */
1208+ if (strcmp (dbname , "template0" ) == 0 )
1209+ {
1210+ pfree (dbname );
1211+ return true;
1212+ }
1213+
11891214 sprintf (params [0 ], "%i" , dbOid );
11901215 sprintf (params [1 ], "%i" , tblspcOid );
11911216 res = pgut_execute (backup_conn , "SELECT pg_ptrack_get_and_clear_db($1, $2)" ,
@@ -2178,7 +2203,6 @@ make_pagemap_from_ptrack(parray *files)
21782203 Oid dbOid_with_ptrack_init = 0 ;
21792204 Oid tblspcOid_with_ptrack_init = 0 ;
21802205 bool ignore_ptrack_for_db = false;
2181- bool is_template = false;
21822206
21832207 for (i = 0 ; i < parray_num (files ); i ++ )
21842208 {
@@ -2195,24 +2219,17 @@ make_pagemap_from_ptrack(parray *files)
21952219 {
21962220 char * filename = strrchr (file -> path , '/' );
21972221 ignore_ptrack_for_db = false;
2198- is_template = false;
21992222
22002223 Assert (filename != NULL );
22012224 filename ++ ;
22022225
2203- /* Always backup all files from template0, template1 databases */
2204- if ((file -> dbOid == 1 ) || //dbOid of template1 daatbase
2205- (file -> dbOid == 12442 )) //dbOid of template0 daatbase
2206- {
2207- is_template = true;
2208- }
22092226 /*
22102227 * The function pg_ptrack_get_and_clear_db returns true
22112228 * if there was a ptrack_init file.
22122229 * Also ignore ptrack files for global tablespace,
22132230 * to avoid any possible specific errors.
22142231 */
2215- else if ((file -> tblspcOid == GLOBALTABLESPACE_OID ) ||
2232+ if ((file -> tblspcOid == GLOBALTABLESPACE_OID ) ||
22162233 pg_ptrack_get_and_clear_db (file -> dbOid , file -> tblspcOid ))
22172234 {
22182235 ignore_ptrack_for_db = true;
@@ -2221,7 +2238,7 @@ make_pagemap_from_ptrack(parray *files)
22212238 }
22222239 }
22232240
2224- if (file -> is_datafile && ! is_template )
2241+ if (file -> is_datafile )
22252242 {
22262243 /* get ptrack bitmap once for all segments of the file */
22272244 if (file -> segno == 0 )
0 commit comments