@ -104,6 +104,24 @@ typedef struct
RelFileNumber toast_index_relfilenumber ; /* toast table index filenode */
} BinaryUpgradeClassOidItem ;
/* sequence types */
typedef enum SeqType
{
SEQTYPE_SMALLINT ,
SEQTYPE_INTEGER ,
SEQTYPE_BIGINT ,
} SeqType ;
const char * const SeqTypeNames [ ] =
{
[ SEQTYPE_SMALLINT ] = " smallint " ,
[ SEQTYPE_INTEGER ] = " integer " ,
[ SEQTYPE_BIGINT ] = " bigint " ,
} ;
StaticAssertDecl ( lengthof ( SeqTypeNames ) = = ( SEQTYPE_BIGINT + 1 ) ,
" array length mismatch " ) ;
typedef enum OidOptions
{
zeroIsError = 1 ,
@ -17251,6 +17269,19 @@ dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
free ( qtabname ) ;
}
static inline SeqType
parse_sequence_type ( const char * name )
{
for ( int i = 0 ; i < lengthof ( SeqTypeNames ) ; i + + )
{
if ( strcmp ( SeqTypeNames [ i ] , name ) = = 0 )
return ( SeqType ) i ;
}
pg_fatal ( " unrecognized sequence type: %s " , name ) ;
return ( SeqType ) 0 ; /* keep compiler quiet */
}
/*
* dumpSequence
* write the declaration ( not data ) of one user - defined sequence
@ -17260,18 +17291,16 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
{
DumpOptions * dopt = fout - > dopt ;
PGresult * res ;
char * startv ,
* incby ,
* maxv ,
* minv ,
* cache ,
* seqtype ;
SeqType seqtype ;
bool cycled ;
bool is_ascending ;
int64 default_minv ,
default_maxv ;
char bufm [ 32 ] ,
bufx [ 32 ] ;
default_maxv ,
minv ,
maxv ,
startv ,
incby ,
cache ;
PQExpBuffer query = createPQExpBuffer ( ) ;
PQExpBuffer delqry = createPQExpBuffer ( ) ;
char * qseqname ;
@ -17313,50 +17342,39 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
PQntuples ( res ) ) ,
tbinfo - > dobj . name , PQntuples ( res ) ) ;
seqtype = PQgetvalue ( res , 0 , 0 ) ;
startv = PQgetvalue ( res , 0 , 1 ) ;
incby = PQgetvalue ( res , 0 , 2 ) ;
maxv = PQgetvalue ( res , 0 , 3 ) ;
minv = PQgetvalue ( res , 0 , 4 ) ;
cache = PQgetvalue ( res , 0 , 5 ) ;
seqtype = parse_sequence_type ( PQgetvalue ( res , 0 , 0 ) ) ;
startv = strtoi64 ( PQgetvalue ( res , 0 , 1 ) , NULL , 10 ) ;
incby = strtoi64 ( PQgetvalue ( res , 0 , 2 ) , NULL , 10 ) ;
maxv = strtoi64 ( PQgetvalue ( res , 0 , 3 ) , NULL , 10 ) ;
minv = strtoi64 ( PQgetvalue ( res , 0 , 4 ) , NULL , 10 ) ;
cache = strtoi64 ( PQgetvalue ( res , 0 , 5 ) , NULL , 10 ) ;
cycled = ( strcmp ( PQgetvalue ( res , 0 , 6 ) , " t " ) = = 0 ) ;
PQclear ( res ) ;
/* Calculate default limits for a sequence of this type */
is_ascending = ( incby [ 0 ] ! = ' - ' ) ;
if ( strcmp ( s eqtype , " smallint " ) = = 0 )
is_ascending = ( incby > = 0 ) ;
if ( seqtype = = SEQTYPE_SMALLINT )
{
default_minv = is_ascending ? 1 : PG_INT16_MIN ;
default_maxv = is_ascending ? PG_INT16_MAX : - 1 ;
}
else if ( strcmp ( s eqtype , " integer " ) = = 0 )
else if ( seqtype = = SEQTYPE_INTEGER )
{
default_minv = is_ascending ? 1 : PG_INT32_MIN ;
default_maxv = is_ascending ? PG_INT32_MAX : - 1 ;
}
else if ( strcmp ( s eqtype , " bigint " ) = = 0 )
else if ( seqtype = = SEQTYPE_BIGINT )
{
default_minv = is_ascending ? 1 : PG_INT64_MIN ;
default_maxv = is_ascending ? PG_INT64_MAX : - 1 ;
}
else
{
pg_fatal ( " unrecognized sequence type: %s " , seqtype ) ;
pg_fatal ( " unrecognized sequence type: %d " , seqtype ) ;
default_minv = default_maxv = 0 ; /* keep compiler quiet */
}
/*
* 64 - bit strtol ( ) isn ' t very portable , so convert the limits to strings
* and compare that way .
*/
snprintf ( bufm , sizeof ( bufm ) , INT64_FORMAT , default_minv ) ;
snprintf ( bufx , sizeof ( bufx ) , INT64_FORMAT , default_maxv ) ;
/* Don't print minv/maxv if they match the respective default limit */
if ( strcmp ( minv , bufm ) = = 0 )
minv = NULL ;
if ( strcmp ( maxv , bufx ) = = 0 )
maxv = NULL ;
/*
* Identity sequences are not to be dropped separately .
*/
@ -17404,26 +17422,26 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
" UNLOGGED " : " " ,
fmtQualifiedDumpable ( tbinfo ) ) ;
if ( strcmp ( s eqtype , " bigint " ) ! = 0 )
appendPQExpBuffer ( query , " AS %s \n " , seqtype ) ;
if ( seqtype ! = SEQTYPE_BIGINT )
appendPQExpBuffer ( query , " AS %s \n " , SeqTypeNames [ seqtype ] ) ;
}
appendPQExpBuffer ( query , " START WITH %s \n " , startv ) ;
appendPQExpBuffer ( query , " START WITH " INT64_FORMAT " \n " , startv ) ;
appendPQExpBuffer ( query , " INCREMENT BY %s \n " , incby ) ;
appendPQExpBuffer ( query , " INCREMENT BY " INT64_FORMAT " \n " , incby ) ;
if ( minv )
appendPQExpBuffer ( query , " MINVALUE %s \n " , minv ) ;
if ( minv ! = default_minv )
appendPQExpBuffer ( query , " MINVALUE " INT64_FORMAT " \n " , minv ) ;
else
appendPQExpBufferStr ( query , " NO MINVALUE \n " ) ;
if ( maxv )
appendPQExpBuffer ( query , " MAXVALUE %s \n " , maxv ) ;
if ( maxv ! = default_maxv )
appendPQExpBuffer ( query , " MAXVALUE " INT64_FORMAT " \n " , maxv ) ;
else
appendPQExpBufferStr ( query , " NO MAXVALUE \n " ) ;
appendPQExpBuffer ( query ,
" CACHE %s %s " ,
" CACHE " INT64_FORMAT " %s " ,
cache , ( cycled ? " \n CYCLE " : " " ) ) ;
if ( tbinfo - > is_identity_sequence )
@ -17510,8 +17528,6 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
tbinfo - > dobj . namespace - > dobj . name , tbinfo - > rolname ,
tbinfo - > dobj . catId , 0 , tbinfo - > dobj . dumpId ) ;
PQclear ( res ) ;
destroyPQExpBuffer ( query ) ;
destroyPQExpBuffer ( delqry ) ;
free ( qseqname ) ;