fix(postgres): restore 9.x table DDL and sequence export
This commit is contained in:
2 files changed
+256
-32
No files matched your search
@@ -1648,6 +1648,101 @@ fn is_postgres_extension_member_routine(object: &crate::types::ObjectInfo, membe
|
||||
members.function_keys.contains(&(object.name.clone(), object.signature.clone().unwrap_or_default()))
|
||||
}
|
||||
|
||||
const POSTGRES_EXPORT_SEQUENCES_SQL: &str = "SELECT c.relname, \
|
||||
COALESCE(format_type(s.seqtypid, NULL), 'bigint'), \
|
||||
COALESCE(s.seqstart::text, '1'), \
|
||||
COALESCE(s.seqmin::text, '1'), \
|
||||
COALESCE(s.seqmax::text, '9223372036854775807'), \
|
||||
COALESCE(s.seqincrement::text, '1'), \
|
||||
COALESCE(s.seqcycle, false), \
|
||||
COALESCE(s.seqcache::text, '1'), \
|
||||
t.relname, \
|
||||
a.attname \
|
||||
FROM pg_class c \
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace \
|
||||
LEFT JOIN pg_sequence s ON s.seqrelid = c.oid \
|
||||
LEFT JOIN pg_depend d ON d.classid = 'pg_class'::regclass \
|
||||
AND d.objid = c.oid \
|
||||
AND d.refclassid = 'pg_class'::regclass \
|
||||
AND d.deptype IN ('a', 'i') \
|
||||
LEFT JOIN pg_class t ON t.oid = d.refobjid \
|
||||
LEFT JOIN pg_namespace tn ON tn.oid = t.relnamespace AND tn.nspname = n.nspname \
|
||||
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = d.refobjsubid \
|
||||
WHERE c.relkind = 'S' AND n.nspname = $1 \
|
||||
ORDER BY c.relname";
|
||||
|
||||
/// PostgreSQL 9.x sibling of [`POSTGRES_EXPORT_SEQUENCES_SQL`]: the `pg_sequence`
|
||||
/// catalog only exists from PostgreSQL 10 on, so that query aborts the whole
|
||||
/// structure export with `db error` on older servers (#10079). Sequence
|
||||
/// parameters are filled in per sequence by
|
||||
/// [`postgres_export_sequence_parameters_legacy`] instead.
|
||||
const POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL: &str = "SELECT c.relname, \
|
||||
'bigint', \
|
||||
'1', \
|
||||
'1', \
|
||||
'9223372036854775807', \
|
||||
'1', \
|
||||
false, \
|
||||
'1', \
|
||||
t.relname, \
|
||||
a.attname \
|
||||
FROM pg_class c \
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace \
|
||||
LEFT JOIN pg_depend d ON d.classid = 'pg_class'::regclass \
|
||||
AND d.objid = c.oid \
|
||||
AND d.refclassid = 'pg_class'::regclass \
|
||||
AND d.deptype IN ('a', 'i') \
|
||||
LEFT JOIN pg_class t ON t.oid = d.refobjid \
|
||||
LEFT JOIN pg_namespace tn ON tn.oid = t.relnamespace AND tn.nspname = n.nspname \
|
||||
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = d.refobjsubid \
|
||||
WHERE c.relkind = 'S' AND n.nspname = $1 \
|
||||
ORDER BY c.relname";
|
||||
|
||||
#[derive(Debug)]
|
||||
struct PostgresExportSequenceParameters {
|
||||
start_value: String,
|
||||
min_value: String,
|
||||
max_value: String,
|
||||
increment: String,
|
||||
cycle: bool,
|
||||
cache_value: String,
|
||||
last_value: Option<String>,
|
||||
}
|
||||
|
||||
/// Pre-10 servers store sequence parameters in the sequence relation itself
|
||||
/// (`SELECT ... FROM <sequence>`), which is also the only way to read a
|
||||
/// sequence's current value there. `last_value` is reported only once the
|
||||
/// sequence has been called, matching `pg_sequence_last_value()`'s NULL on an
|
||||
/// untouched sequence.
|
||||
async fn postgres_export_sequence_parameters_legacy(
|
||||
client: &deadpool_postgres::Client,
|
||||
schema: &str,
|
||||
sequence: &str,
|
||||
) -> Result<Option<PostgresExportSequenceParameters>, tokio_postgres::Error> {
|
||||
let qualified = format!(
|
||||
"{}.{}",
|
||||
quote_identifier(schema, &DatabaseType::Postgres),
|
||||
quote_identifier(sequence, &DatabaseType::Postgres)
|
||||
);
|
||||
let sql = format!(
|
||||
"SELECT start_value::text, min_value::text, max_value::text, increment_by::text, is_cycled, \
|
||||
cache_value::text, CASE WHEN is_called THEN last_value::text END FROM {qualified}"
|
||||
);
|
||||
let rows = client.query(sql.as_str(), &[]).await?;
|
||||
let Some(row) = rows.first() else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(PostgresExportSequenceParameters {
|
||||
start_value: row.get(0),
|
||||
min_value: row.get(1),
|
||||
max_value: row.get(2),
|
||||
increment: row.get(3),
|
||||
cycle: row.get(4),
|
||||
cache_value: row.get(5),
|
||||
last_value: row.get(6),
|
||||
}))
|
||||
}
|
||||
|
||||
async fn list_postgres_export_sequences(
|
||||
state: &crate::connection::AppState,
|
||||
pool_key: &str,
|
||||
@@ -1665,34 +1760,23 @@ async fn list_postgres_export_sequences(
|
||||
}
|
||||
};
|
||||
let client = pool.get().await.map_err(|e| e.to_string())?;
|
||||
let rows = client
|
||||
.query(
|
||||
"SELECT c.relname, \
|
||||
COALESCE(format_type(s.seqtypid, NULL), 'bigint'), \
|
||||
COALESCE(s.seqstart::text, '1'), \
|
||||
COALESCE(s.seqmin::text, '1'), \
|
||||
COALESCE(s.seqmax::text, '9223372036854775807'), \
|
||||
COALESCE(s.seqincrement::text, '1'), \
|
||||
COALESCE(s.seqcycle, false), \
|
||||
COALESCE(s.seqcache::text, '1'), \
|
||||
t.relname, \
|
||||
a.attname \
|
||||
FROM pg_class c \
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace \
|
||||
LEFT JOIN pg_sequence s ON s.seqrelid = c.oid \
|
||||
LEFT JOIN pg_depend d ON d.classid = 'pg_class'::regclass \
|
||||
AND d.objid = c.oid \
|
||||
AND d.refclassid = 'pg_class'::regclass \
|
||||
AND d.deptype IN ('a', 'i') \
|
||||
LEFT JOIN pg_class t ON t.oid = d.refobjid \
|
||||
LEFT JOIN pg_namespace tn ON tn.oid = t.relnamespace AND tn.nspname = n.nspname \
|
||||
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = d.refobjsubid \
|
||||
WHERE c.relkind = 'S' AND n.nspname = $1 \
|
||||
ORDER BY c.relname",
|
||||
&[&schema],
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
// PostgreSQL 10+ keeps sequence parameters in `pg_sequence`; older servers
|
||||
// have neither that catalog nor `pg_sequence_last_value()`, so the compat
|
||||
// tier lists sequences (and owners) without it and reads each sequence
|
||||
// relation directly afterwards.
|
||||
let (rows, legacy_parameters) = match client.query(POSTGRES_EXPORT_SEQUENCES_SQL, &[&schema]).await {
|
||||
Ok(rows) => (rows, false),
|
||||
Err(primary_error) => match client.query(POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL, &[&schema]).await {
|
||||
Ok(rows) => {
|
||||
log::debug!(
|
||||
"[postgres][database-export:sequences-compat-used] pg_sequence catalog unavailable ({}); reading sequence parameters from each sequence relation",
|
||||
primary_error
|
||||
);
|
||||
(rows, true)
|
||||
}
|
||||
Err(_) => return Err(primary_error.to_string()),
|
||||
},
|
||||
};
|
||||
|
||||
let selected: HashSet<&str> = selected_tables.iter().map(String::as_str).collect();
|
||||
let excluded: HashSet<&str> = excluded_tables.iter().map(String::as_str).collect();
|
||||
@@ -1723,6 +1807,30 @@ async fn list_postgres_export_sequences(
|
||||
return Ok(sequences);
|
||||
}
|
||||
|
||||
if legacy_parameters {
|
||||
for sequence in sequences.iter_mut() {
|
||||
match postgres_export_sequence_parameters_legacy(&client, schema, &sequence.name).await {
|
||||
Ok(Some(parameters)) => {
|
||||
sequence.start_value = parameters.start_value;
|
||||
sequence.min_value = parameters.min_value;
|
||||
sequence.max_value = parameters.max_value;
|
||||
sequence.increment = parameters.increment;
|
||||
sequence.cycle = parameters.cycle;
|
||||
sequence.cache_value = parameters.cache_value;
|
||||
sequence.last_value = parameters.last_value;
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(error) => log::debug!(
|
||||
"[postgres][database-export:sequences-compat-read-failed] sequence={} schema={} error={}",
|
||||
sequence.name,
|
||||
schema,
|
||||
error
|
||||
),
|
||||
}
|
||||
}
|
||||
return Ok(sequences);
|
||||
}
|
||||
|
||||
let last_values = client
|
||||
.query(
|
||||
"SELECT c.relname, pg_sequence_last_value(c.oid)::text \
|
||||
@@ -4088,7 +4196,7 @@ mod tests {
|
||||
BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions, BuildExportSqlInsertOptions,
|
||||
DatabaseExportObjectCounts, DatabaseExportRequest, DatabaseExportWriter, DdlNormalizeOptions, ExportedTableSql,
|
||||
PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers, DATABASE_EXPORT_INSERT_BATCH_SIZE,
|
||||
DATABASE_EXPORT_ROW_LIMIT,
|
||||
DATABASE_EXPORT_ROW_LIMIT, POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL, POSTGRES_EXPORT_SEQUENCES_SQL,
|
||||
};
|
||||
use super::{ExportProgress, LenientExportErrors};
|
||||
use crate::connection::AppState;
|
||||
@@ -6400,6 +6508,34 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_export_sequence_sql_tiers_skip_pg10_only_catalogs() {
|
||||
// PostgreSQL 10 added `pg_sequence`; PostgreSQL 9.x rejects the whole
|
||||
// query, which used to abort the structure export (#10079).
|
||||
assert!(POSTGRES_EXPORT_SEQUENCES_SQL.contains("pg_sequence"));
|
||||
assert!(!POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL.contains("pg_sequence"));
|
||||
assert!(!POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL.contains("pg_sequence_last_value"));
|
||||
|
||||
// Both tiers must describe the same ten columns in the same order so
|
||||
// the row decoding below stays identical.
|
||||
for sql in [POSTGRES_EXPORT_SEQUENCES_SQL, POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL] {
|
||||
for fragment in [
|
||||
"SELECT c.relname",
|
||||
"'bigint'",
|
||||
"t.relname",
|
||||
"a.attname",
|
||||
"WHERE c.relkind = 'S' AND n.nspname = $1",
|
||||
"ORDER BY c.relname",
|
||||
] {
|
||||
assert!(sql.contains(fragment), "missing {fragment} in {sql}");
|
||||
}
|
||||
}
|
||||
// The compat tier substitutes literals for the pg_sequence columns.
|
||||
for fallback in ["'1'", "'9223372036854775807'", "false"] {
|
||||
assert!(POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL.contains(fallback));
|
||||
}
|
||||
}
|
||||
|
||||
fn postgres_sequence(name: &str) -> PostgresExportSequence {
|
||||
PostgresExportSequence {
|
||||
name: name.to_string(),
|
||||
|
||||
Reference in new issue
Block a user