@ -6272,20 +6272,23 @@ getTables(Archive *fout, int *numTables)
int i_is_identity_sequence ;
int i_is_identity_sequence ;
int i_relacl ;
int i_relacl ;
int i_acldefault ;
int i_acldefault ;
int i_partkeydef ;
int i_ispartition ;
int i_ispartition ;
int i_partbound ;
/*
/*
* Find all the tables and table - like objects .
* Find all the tables and table - like objects .
*
*
* We must fetch all tables in this phase because otherwise we cannot
* correctly identify inherited columns , owned sequences , etc .
*
* We include system catalogs , so that we can work if a user table is
* We include system catalogs , so that we can work if a user table is
* defined to inherit from a system catalog ( pretty weird , but . . . )
* defined to inherit from a system catalog ( pretty weird , but . . . )
*
*
* Note : in this phase we should collect only a minimal amount of
* Note : in this phase we should collect only a minimal amount of
* information about each table , basically just enough to decide if it is
* information about each table , basically just enough to decide if it is
* interesting . We must fetch all tables in this phase because otherwise
* interesting . In particular , since we do not yet have lock on any user
* we cannot correctly identify inherited columns , owned sequences , etc .
* table , we MUST NOT invoke any server - side data collection functions
* ( for instance , pg_get_partkeydef ( ) ) . Those are likely to fail or give
* wrong answers if any concurrent DDL is happening .
*/
*/
appendPQExpBuffer ( query ,
appendPQExpBuffer ( query ,
@ -6379,10 +6382,10 @@ getTables(Archive *fout, int *numTables)
if ( fout - > remoteVersion > = 90000 )
if ( fout - > remoteVersion > = 90000 )
appendPQExpBufferStr ( query ,
appendPQExpBufferStr ( query ,
" CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, " ) ;
" c.reloftype, " ) ;
else
else
appendPQExpBufferStr ( query ,
appendPQExpBufferStr ( query ,
" NULL AS reloftype, " ) ;
" 0 AS reloftype, " ) ;
if ( fout - > remoteVersion > = 90100 )
if ( fout - > remoteVersion > = 90100 )
appendPQExpBufferStr ( query ,
appendPQExpBufferStr ( query ,
@ -6423,14 +6426,10 @@ getTables(Archive *fout, int *numTables)
if ( fout - > remoteVersion > = 100000 )
if ( fout - > remoteVersion > = 100000 )
appendPQExpBufferStr ( query ,
appendPQExpBufferStr ( query ,
" pg_get_partkeydef(c.oid) AS partkeydef, "
" c.relispartition AS ispartition " ) ;
" c.relispartition AS ispartition, "
" pg_get_expr(c.relpartbound, c.oid) AS partbound " ) ;
else
else
appendPQExpBufferStr ( query ,
appendPQExpBufferStr ( query ,
" NULL AS partkeydef, "
" false AS ispartition " ) ;
" false AS ispartition, "
" NULL AS partbound " ) ;
/*
/*
* Left join to pg_depend to pick up dependency info linking sequences to
* Left join to pg_depend to pick up dependency info linking sequences to
@ -6539,9 +6538,7 @@ getTables(Archive *fout, int *numTables)
i_is_identity_sequence = PQfnumber ( res , " is_identity_sequence " ) ;
i_is_identity_sequence = PQfnumber ( res , " is_identity_sequence " ) ;
i_relacl = PQfnumber ( res , " relacl " ) ;
i_relacl = PQfnumber ( res , " relacl " ) ;
i_acldefault = PQfnumber ( res , " acldefault " ) ;
i_acldefault = PQfnumber ( res , " acldefault " ) ;
i_partkeydef = PQfnumber ( res , " partkeydef " ) ;
i_ispartition = PQfnumber ( res , " ispartition " ) ;
i_ispartition = PQfnumber ( res , " ispartition " ) ;
i_partbound = PQfnumber ( res , " partbound " ) ;
if ( dopt - > lockWaitTimeout )
if ( dopt - > lockWaitTimeout )
{
{
@ -6607,19 +6604,14 @@ getTables(Archive *fout, int *numTables)
else
else
tblinfo [ i ] . checkoption = pg_strdup ( PQgetvalue ( res , i , i_checkoption ) ) ;
tblinfo [ i ] . checkoption = pg_strdup ( PQgetvalue ( res , i , i_checkoption ) ) ;
tblinfo [ i ] . toast_reloptions = pg_strdup ( PQgetvalue ( res , i , i_toastreloptions ) ) ;
tblinfo [ i ] . toast_reloptions = pg_strdup ( PQgetvalue ( res , i , i_toastreloptions ) ) ;
if ( PQgetisnull ( res , i , i_reloftype ) )
tblinfo [ i ] . reloftype = atooid ( PQgetvalue ( res , i , i_reloftype ) ) ;
tblinfo [ i ] . reloftype = NULL ;
else
tblinfo [ i ] . reloftype = pg_strdup ( PQgetvalue ( res , i , i_reloftype ) ) ;
tblinfo [ i ] . foreign_server = atooid ( PQgetvalue ( res , i , i_foreignserver ) ) ;
tblinfo [ i ] . foreign_server = atooid ( PQgetvalue ( res , i , i_foreignserver ) ) ;
if ( PQgetisnull ( res , i , i_amname ) )
if ( PQgetisnull ( res , i , i_amname ) )
tblinfo [ i ] . amname = NULL ;
tblinfo [ i ] . amname = NULL ;
else
else
tblinfo [ i ] . amname = pg_strdup ( PQgetvalue ( res , i , i_amname ) ) ;
tblinfo [ i ] . amname = pg_strdup ( PQgetvalue ( res , i , i_amname ) ) ;
tblinfo [ i ] . is_identity_sequence = ( strcmp ( PQgetvalue ( res , i , i_is_identity_sequence ) , " t " ) = = 0 ) ;
tblinfo [ i ] . is_identity_sequence = ( strcmp ( PQgetvalue ( res , i , i_is_identity_sequence ) , " t " ) = = 0 ) ;
tblinfo [ i ] . partkeydef = pg_strdup ( PQgetvalue ( res , i , i_partkeydef ) ) ;
tblinfo [ i ] . ispartition = ( strcmp ( PQgetvalue ( res , i , i_ispartition ) , " t " ) = = 0 ) ;
tblinfo [ i ] . ispartition = ( strcmp ( PQgetvalue ( res , i , i_ispartition ) , " t " ) = = 0 ) ;
tblinfo [ i ] . partbound = pg_strdup ( PQgetvalue ( res , i , i_partbound ) ) ;
/* other fields were zeroed above */
/* other fields were zeroed above */
@ -15651,12 +15643,34 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
}
}
else
else
{
{
char * partkeydef = NULL ;
char * ftoptions = NULL ;
char * ftoptions = NULL ;
char * srvname = NULL ;
char * srvname = NULL ;
char * foreign = " " ;
char * foreign = " " ;
/*
* Set reltypename , and collect any relkind - specific data that we
* didn ' t fetch during getTables ( ) .
*/
switch ( tbinfo - > relkind )
switch ( tbinfo - > relkind )
{
{
case RELKIND_PARTITIONED_TABLE :
{
PQExpBuffer query = createPQExpBuffer ( ) ;
PGresult * res ;
reltypename = " TABLE " ;
/* retrieve partition key definition */
appendPQExpBuffer ( query ,
" SELECT pg_get_partkeydef('%u') " ,
tbinfo - > dobj . catId . oid ) ;
res = ExecuteSqlQueryForSingleRow ( fout , query - > data ) ;
partkeydef = pg_strdup ( PQgetvalue ( res , 0 , 0 ) ) ;
PQclear ( res ) ;
destroyPQExpBuffer ( query ) ;
break ;
}
case RELKIND_FOREIGN_TABLE :
case RELKIND_FOREIGN_TABLE :
{
{
PQExpBuffer query = createPQExpBuffer ( ) ;
PQExpBuffer query = createPQExpBuffer ( ) ;
@ -15696,6 +15710,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
break ;
break ;
default :
default :
reltypename = " TABLE " ;
reltypename = " TABLE " ;
break ;
}
}
numParents = tbinfo - > numParents ;
numParents = tbinfo - > numParents ;
@ -15717,8 +15732,10 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
* Attach to type , if reloftype ; except in case of a binary upgrade ,
* Attach to type , if reloftype ; except in case of a binary upgrade ,
* we dump the table normally and attach it to the type afterward .
* we dump the table normally and attach it to the type afterward .
*/
*/
if ( tbinfo - > reloftype & & ! dopt - > binary_upgrade )
if ( OidIsValid ( tbinfo - > reloftype ) & & ! dopt - > binary_upgrade )
appendPQExpBuffer ( q , " OF %s " , tbinfo - > reloftype ) ;
appendPQExpBuffer ( q , " OF %s " ,
getFormattedTypeName ( fout , tbinfo - > reloftype ,
zeroIsError ) ) ;
if ( tbinfo - > relkind ! = RELKIND_MATVIEW )
if ( tbinfo - > relkind ! = RELKIND_MATVIEW )
{
{
@ -15756,7 +15773,8 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
* Skip column if fully defined by reloftype , except in
* Skip column if fully defined by reloftype , except in
* binary upgrade
* binary upgrade
*/
*/
if ( tbinfo - > reloftype & & ! print_default & & ! print_notnull & &
if ( OidIsValid ( tbinfo - > reloftype ) & &
! print_default & & ! print_notnull & &
! dopt - > binary_upgrade )
! dopt - > binary_upgrade )
continue ;
continue ;
@ -15789,7 +15807,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
* table ( ' OF type_name ' ) , but in binary - upgrade mode ,
* table ( ' OF type_name ' ) , but in binary - upgrade mode ,
* print it in that case too .
* print it in that case too .
*/
*/
if ( dopt - > binary_upgrade | | ! tbinfo - > reloftype )
if ( dopt - > binary_upgrade | | ! OidIsValid ( tbinfo - > reloftype ) )
{
{
appendPQExpBuffer ( q , " %s " ,
appendPQExpBuffer ( q , " %s " ,
tbinfo - > atttypnames [ j ] ) ;
tbinfo - > atttypnames [ j ] ) ;
@ -15852,7 +15870,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
if ( actual_atts )
if ( actual_atts )
appendPQExpBufferStr ( q , " \n ) " ) ;
appendPQExpBufferStr ( q , " \n ) " ) ;
else if ( ! ( tbinfo - > reloftype & & ! dopt - > binary_upgrade ) )
else if ( ! ( OidIsValid ( tbinfo - > reloftype ) & & ! dopt - > binary_upgrade ) )
{
{
/*
/*
* No attributes ? we must have a parenthesized attribute list ,
* No attributes ? we must have a parenthesized attribute list ,
@ -15881,7 +15899,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
}
}
if ( tbinfo - > relkind = = RELKIND_PARTITIONED_TABLE )
if ( tbinfo - > relkind = = RELKIND_PARTITIONED_TABLE )
appendPQExpBuffer ( q , " \n PARTITION BY %s " , tbinfo - > partkeydef ) ;
appendPQExpBuffer ( q , " \n PARTITION BY %s " , partkeydef ) ;
if ( tbinfo - > relkind = = RELKIND_FOREIGN_TABLE )
if ( tbinfo - > relkind = = RELKIND_FOREIGN_TABLE )
appendPQExpBuffer ( q , " \n SERVER %s " , fmtId ( srvname ) ) ;
appendPQExpBuffer ( q , " \n SERVER %s " , fmtId ( srvname ) ) ;
@ -16064,12 +16082,13 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
}
}
}
}
if ( tbinfo - > reloftype )
if ( OidIsValid ( tbinfo - > reloftype ) )
{
{
appendPQExpBufferStr ( q , " \n -- For binary upgrade, set up typed tables this way. \n " ) ;
appendPQExpBufferStr ( q , " \n -- For binary upgrade, set up typed tables this way. \n " ) ;
appendPQExpBuffer ( q , " ALTER TABLE ONLY %s OF %s; \n " ,
appendPQExpBuffer ( q , " ALTER TABLE ONLY %s OF %s; \n " ,
qualrelname ,
qualrelname ,
tbinfo - > reloftype ) ;
getFormattedTypeName ( fout , tbinfo - > reloftype ,
zeroIsError ) ) ;
}
}
}
}
@ -16242,6 +16261,8 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
tbinfo - > attfdwoptions [ j ] ) ;
tbinfo - > attfdwoptions [ j ] ) ;
} /* end loop over columns */
} /* end loop over columns */
if ( partkeydef )
free ( partkeydef ) ;
if ( ftoptions )
if ( ftoptions )
free ( ftoptions ) ;
free ( ftoptions ) ;
if ( srvname )
if ( srvname )
@ -16358,6 +16379,8 @@ dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
{
{
DumpOptions * dopt = fout - > dopt ;
DumpOptions * dopt = fout - > dopt ;
PQExpBuffer q ;
PQExpBuffer q ;
PGresult * res ;
char * partbound ;
/* Do nothing in data-only dump */
/* Do nothing in data-only dump */
if ( dopt - > dataOnly )
if ( dopt - > dataOnly )
@ -16368,14 +16391,23 @@ dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
q = createPQExpBuffer ( ) ;
q = createPQExpBuffer ( ) ;
/* Perform ALTER TABLE on the parent */
/* Fetch the partition's partbound */
appendPQExpBuffer ( q ,
appendPQExpBuffer ( q ,
" SELECT pg_get_expr(c.relpartbound, c.oid) "
" FROM pg_class c "
" WHERE c.oid = '%u' " ,
attachinfo - > partitionTbl - > dobj . catId . oid ) ;
res = ExecuteSqlQueryForSingleRow ( fout , q - > data ) ;
partbound = PQgetvalue ( res , 0 , 0 ) ;
/* Perform ALTER TABLE on the parent */
printfPQExpBuffer ( q ,
" ALTER TABLE ONLY %s " ,
" ALTER TABLE ONLY %s " ,
fmtQualifiedDumpable ( attachinfo - > parentTbl ) ) ;
fmtQualifiedDumpable ( attachinfo - > parentTbl ) ) ;
appendPQExpBuffer ( q ,
appendPQExpBuffer ( q ,
" ATTACH PARTITION %s %s; \n " ,
" ATTACH PARTITION %s %s; \n " ,
fmtQualifiedDumpable ( attachinfo - > partitionTbl ) ,
fmtQualifiedDumpable ( attachinfo - > partitionTbl ) ,
attachinfo - > partitionTbl - > partbound ) ;
partbound ) ;
/*
/*
* There is no point in creating a drop query as the drop is done by table
* There is no point in creating a drop query as the drop is done by table
@ -16392,6 +16424,7 @@ dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
. section = SECTION_PRE_DATA ,
. section = SECTION_PRE_DATA ,
. createStmt = q - > data ) ) ;
. createStmt = q - > data ) ) ;
PQclear ( res ) ;
destroyPQExpBuffer ( q ) ;
destroyPQExpBuffer ( q ) ;
}
}