fix(postgres): correct partition DDL generation

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-19 15:08:54 +08:00
1 parent e9ca697c3b
commit d085c5b4f4
9 files changed
+960 -378

No files matched your search

+36 -4
View File
@@ -170,10 +170,14 @@ fn render_external_table_ddl(ddl: &str, external: &ExternalTableDefinition) -> R
.ok_or_else(|| "Cloudberry fallback DDL has no CREATE TABLE terminator".to_string())?;
// When the fallback DDL already read `CREATE FOREIGN TABLE`, the generic
// renderer also already appended its own ` SERVER ... [OPTIONS (...)]`
// clause (from the same relkind='f' catalog data). Drop that so this
// doesn't emit the clause twice — Cloudberry's own `external` definition
// above is authoritative here.
let body_end = ddl[header_len..insertion].find(" SERVER \"").map(|pos| header_len + pos).unwrap_or(insertion);
// clause (from the same relkind='f' catalog data) right after the column
// list's closing paren. Drop that so this doesn't emit the clause twice —
// Cloudberry's own `external` definition above is authoritative here.
// Locate the column list's own closing paren structurally (tracking
// parenthesis depth and skipping quoted content) instead of searching for
// the literal text `" SERVER \""`, which a column's DEFAULT/CHECK
// expression could contain and match spuriously.
let body_end = super::ddl_scan::find_top_level_paren_close(ddl, header_len).unwrap_or(insertion);
let mut output = String::with_capacity(ddl.len() + external.options.len() * 24 + 48);
output.push_str("CREATE FOREIGN TABLE ");
output.push_str(&ddl[header_len..body_end]);
@@ -364,6 +368,34 @@ mod tests {
assert!(!rendered.contains("stale_server"));
}
#[test]
fn renders_external_table_when_a_column_default_contains_literal_server_text() {
// A naive text search for `" SERVER \""` to find the stale SERVER
// clause's start would match this DEFAULT's embedded text instead of
// the real trailing SERVER clause, truncating the column list.
let ddl = "CREATE FOREIGN TABLE \"public\".\"external_events\" (\n \"id\" integer,\n \"note\" text DEFAULT 'contact SERVER \"admin\"'\n) SERVER \"stale_server\";\n";
let rendered = append_table_modifiers(
ddl,
&TableModifiers {
external: Some(ExternalTableDefinition {
server: "gp_exttable_server".to_string(),
options: vec!["format=csv".to_string()],
}),
..modifiers(None)
},
)
.unwrap();
assert!(rendered.contains("\"note\" text DEFAULT 'contact SERVER \"admin\"'"));
assert_eq!(
rendered.matches("SERVER").count(),
2,
"expected exactly the embedded literal plus the real clause: {rendered}"
);
assert!(rendered.contains("SERVER \"gp_exttable_server\""));
assert!(!rendered.contains("stale_server"));
}
#[test]
fn ddl_queries_avoid_privileged_external_catalog_view() {
assert_eq!(CLOUD_BERRY_TABLE_DDL_SQL, "SELECT pg_get_tabledef($1, $2, true)");
+223
View File
@@ -0,0 +1,223 @@
//! Structural, quote-and-parenthesis-aware scanning helpers for
//! post-processing already-rendered `CREATE TABLE` DDL text.
//!
//! A naive `str::find` for a literal marker (e.g. `" SERVER \""` or a bare
//! `;`) can be fooled by that same text appearing inside a quoted string
//! literal, a quoted identifier, or a nested parenthesized sub-expression —
//! a column's `DEFAULT`/`CHECK` expression can legally contain any of those.
//! These helpers track quote and parenthesis depth so callers only ever
//! match at the top level.
use std::ops::Range;
fn char_at(sql: &str, pos: usize) -> Option<char> {
sql.get(pos..)?.chars().next()
}
/// Skips a `'...'` string literal (`''`-escaped, with backslash-escape
/// tolerance) or a `"..."` quoted identifier (`""`-escaped) starting at
/// `pos`, which must point at the opening `quote` character. Returns the
/// index just past the closing quote (or `sql.len()` if unterminated).
fn skip_quoted(sql: &str, pos: usize, quote: char) -> usize {
let mut i = pos + quote.len_utf8();
while i < sql.len() {
let Some(ch) = char_at(sql, i) else { break };
let next = char_at(sql, i + ch.len_utf8());
if ch == quote {
if next == Some(quote) {
i += ch.len_utf8() + quote.len_utf8();
continue;
}
return i + ch.len_utf8();
}
if quote == '\'' && ch == '\\' {
i += ch.len_utf8();
if let Some(escaped) = char_at(sql, i) {
i += escaped.len_utf8();
}
continue;
}
i += ch.len_utf8();
}
sql.len()
}
/// Skips a PostgreSQL `$tag$...$tag$` dollar-quoted body starting at `pos`
/// (pointing at the first `$`). Returns `None` if `pos` isn't actually the
/// start of a valid dollar-quote tag (e.g. a stray `$` in an expression), in
/// which case the caller should treat `$` as an ordinary character.
fn skip_dollar_quoted(sql: &str, pos: usize) -> Option<usize> {
let tag_end_offset = sql.get(pos + 1..)?.find('$')?;
let tag_end = pos + 1 + tag_end_offset;
let tag = &sql[pos + 1..tag_end];
let valid_tag = tag.is_empty()
|| (tag.chars().next().is_some_and(|ch| ch.is_ascii_alphabetic() || ch == '_')
&& tag.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_'));
if !valid_tag {
return None;
}
let delimiter = &sql[pos..=tag_end];
let content_start = tag_end + 1;
sql.get(content_start..)?.find(delimiter).map(|offset| content_start + offset + delimiter.len())
}
/// Finds the byte index just past the first top-level (parenthesis-depth-0)
/// closing paren at or after `from` — the end of the outermost parenthesized
/// group that starts somewhere at or after `from` — treating quoted or
/// dollar-quoted content as opaque so parens or keywords inside a
/// `DEFAULT`/`CHECK` expression can't confuse the count. Returns `None` if no
/// top-level `(` is ever opened, or it's never closed.
pub(crate) fn find_top_level_paren_close(sql: &str, from: usize) -> Option<usize> {
let mut i = from;
let mut depth: usize = 0;
let mut opened = false;
while i < sql.len() {
let ch = char_at(sql, i)?;
match ch {
'\'' | '"' => {
i = skip_quoted(sql, i, ch);
continue;
}
'$' => {
if let Some(end) = skip_dollar_quoted(sql, i) {
i = end;
continue;
}
}
'(' => {
depth += 1;
opened = true;
}
')' => {
depth = depth.saturating_sub(1);
i += ch.len_utf8();
if opened && depth == 0 {
return Some(i);
}
continue;
}
_ => {}
}
i += ch.len_utf8();
}
None
}
/// Finds the byte index of the next top-level (depth-0, outside any quoted
/// or dollar-quoted content) `;` at or after `from`.
fn find_top_level_semicolon(sql: &str, from: usize) -> Option<usize> {
let mut i = from;
let mut depth: usize = 0;
while i < sql.len() {
let ch = char_at(sql, i)?;
match ch {
'\'' | '"' => {
i = skip_quoted(sql, i, ch);
continue;
}
'$' => {
if let Some(end) = skip_dollar_quoted(sql, i) {
i = end;
continue;
}
}
'(' => depth += 1,
')' => depth = depth.saturating_sub(1),
';' if depth == 0 => return Some(i),
_ => {}
}
i += ch.len_utf8();
}
None
}
/// Splits `sql` into consecutive top-level statement ranges, each ending
/// just past its own top-level `;`. The last statement may lack a trailing
/// `;`, in which case its range runs to the end of the string.
pub(crate) fn top_level_statement_ranges(sql: &str) -> Vec<Range<usize>> {
let mut ranges = Vec::new();
let mut start = 0;
while start < sql.len() {
match find_top_level_semicolon(sql, start) {
Some(semi) => {
ranges.push(start..semi + 1);
start = semi + 1;
}
None => {
ranges.push(start..sql.len());
break;
}
}
}
ranges
}
/// If `stmt` (ignoring leading whitespace) begins with `CREATE TABLE
/// "schema"."table"` or `CREATE FOREIGN TABLE "schema"."table"`, returns the
/// unescaped `(schema, table)` pair.
pub(crate) fn parse_create_table_relation(stmt: &str) -> Option<(String, String)> {
let trimmed = stmt.trim_start();
let rest =
trimmed.strip_prefix("CREATE FOREIGN TABLE ").or_else(|| trimmed.strip_prefix("CREATE TABLE "))?.trim_start();
let (schema, rest) = unquote_ident(rest)?;
let (table, _) = unquote_ident(rest.strip_prefix('.')?)?;
Some((schema, table))
}
fn unquote_ident(s: &str) -> Option<(String, &str)> {
if !s.starts_with('"') {
return None;
}
let end = skip_quoted(s, 0, '"');
if end < 2 {
return None;
}
let raw = &s[1..end - 1];
Some((raw.replace("\"\"", "\""), &s[end..]))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paren_close_skips_nested_parens_and_quotes() {
let ddl =
"\"public\".\"t\" (\n \"a\" text DEFAULT 'x(y)'::text,\n \"b\" text CHECK ((b <> 'z'))\n) SERVER \"s\"";
let close = find_top_level_paren_close(ddl, 0).unwrap();
assert_eq!(
&ddl[..close],
"\"public\".\"t\" (\n \"a\" text DEFAULT 'x(y)'::text,\n \"b\" text CHECK ((b <> 'z'))\n)"
);
}
#[test]
fn paren_close_ignores_literal_server_text_inside_quotes() {
let ddl = "\"public\".\"t\" (\n \"note\" text DEFAULT 'contact SERVER \"admin\"'\n) SERVER \"real_server\"";
let close = find_top_level_paren_close(ddl, 0).unwrap();
assert!(ddl[close..].starts_with(" SERVER \"real_server\""));
}
#[test]
fn top_level_statement_ranges_split_on_depth_zero_semicolons() {
let ddl = "CREATE TABLE \"s\".\"t\" (\n \"a\" text DEFAULT 'x;y'\n);\nALTER TABLE \"s\".\"t\" ADD CONSTRAINT c CHECK (a <> '');";
let ranges = top_level_statement_ranges(ddl);
assert_eq!(ranges.len(), 2);
assert!(ddl[ranges[0].clone()].starts_with("CREATE TABLE"));
assert!(ddl[ranges[0].clone()].ends_with(";"));
assert!(ddl[ranges[1].clone()].trim_start().starts_with("ALTER TABLE"));
}
#[test]
fn parse_create_table_relation_unescapes_doubled_quotes() {
let (schema, table) =
parse_create_table_relation("CREATE TABLE \"pub\"\"lic\".\"t\" (\n \"a\" int\n);").unwrap();
assert_eq!(schema, "pub\"lic");
assert_eq!(table, "t");
}
#[test]
fn parse_create_table_relation_rejects_non_create_table() {
assert!(parse_create_table_relation("ALTER TABLE \"s\".\"t\" ADD CONSTRAINT c CHECK (true);").is_none());
}
}
+1
View File
@@ -3,6 +3,7 @@ pub mod clickhouse_driver;
pub mod cloudberry;
pub mod cloudflare_d1;
pub use cloudflare_d1 as cloudflare_d1_driver;
pub(crate) mod ddl_scan;
pub mod document_result;
pub mod dolt;
pub mod doris;
+120
View File
@@ -1,3 +1,5 @@
use std::collections::HashMap;
use deadpool_postgres::Pool;
use crate::db;
@@ -20,6 +22,24 @@ const TABLE_DISTRIBUTION_SQL: &str = "SELECT c.pclocatortype::text, \
WHERE namespace.nspname = $1 AND relation.relname = $2 \
LIMIT 1";
/// Batched sibling of `TABLE_DISTRIBUTION_SQL`, for appending each
/// partition's own `DISTRIBUTE BY` clause when rendering a whole partition
/// tree instead of just the root relation.
const TABLE_DISTRIBUTION_FOR_RELATIONS_SQL: &str =
"SELECT namespace.nspname, relation.relname, c.pclocatortype::text, \
COALESCE(( \
SELECT string_agg(a.attname, E'\\n' ORDER BY distributed_column.ordinality) \
FROM unnest(c.discolnums::smallint[]) WITH ORDINALITY \
AS distributed_column(attnum, ordinality) \
JOIN pg_catalog.pg_attribute a \
ON a.attrelid = c.pcrelid AND a.attnum = distributed_column.attnum \
WHERE NOT a.attisdropped \
), '')::text \
FROM unnest($1::text[], $2::text[]) AS rel(rel_schema, rel_table) \
JOIN pg_catalog.pg_namespace namespace ON namespace.nspname = rel.rel_schema \
JOIN pg_catalog.pg_class relation ON relation.relnamespace = namespace.oid AND relation.relname = rel.rel_table \
JOIN pg_catalog.pgxc_class c ON c.pcrelid = relation.oid";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DistributionPolicy {
Hash(Vec<String>),
@@ -52,6 +72,43 @@ pub async fn table_distribution(pool: &Pool, schema: &str, table: &str) -> Resul
parse_distribution_policy(&locator_type, columns)
}
/// Batched sibling of `table_distribution`, for a whole partition tree.
/// Relations without a distribution policy (or that don't exist) are simply
/// absent from the result map rather than erroring the whole batch.
pub async fn table_distribution_for_relations(
pool: &Pool,
relations: &[(String, String)],
) -> Result<HashMap<(String, String), DistributionPolicy>, String> {
if relations.is_empty() {
return Ok(HashMap::new());
}
let client = db::postgres::checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let schemas: Vec<&str> = relations.iter().map(|(schema, _)| schema.as_str()).collect();
let tables: Vec<&str> = relations.iter().map(|(_, table)| table.as_str()).collect();
let rows = client
.query(TABLE_DISTRIBUTION_FOR_RELATIONS_SQL, &[&schemas, &tables])
.await
.map_err(|error| error.to_string())?;
let mut result = HashMap::new();
for row in &rows {
let schema = row.try_get::<_, String>(0).map_err(|error| error.to_string())?;
let table = row.try_get::<_, String>(1).map_err(|error| error.to_string())?;
let locator_type = row.try_get::<_, String>(2).map_err(|error| error.to_string())?;
let columns = row
.try_get::<_, String>(3)
.map_err(|error| error.to_string())?
.lines()
.map(str::trim)
.filter(|column| !column.is_empty())
.map(str::to_string)
.collect::<Vec<_>>();
if let Some(distribution) = parse_distribution_policy(&locator_type, columns)? {
result.insert((schema, table), distribution);
}
}
Ok(result)
}
pub fn append_distribution_clause(ddl: &str, distribution: &DistributionPolicy) -> Result<String, String> {
let clause = render_distribution_clause(distribution)?;
let insertion = ddl
@@ -66,6 +123,41 @@ pub fn append_distribution_clause(ddl: &str, distribution: &DistributionPolicy)
Ok(output)
}
/// Tree-aware sibling of `append_distribution_clause`: `ddl` may contain
/// several relations' worth of statements concatenated together (root plus
/// every partition), so each `CREATE [FOREIGN] TABLE "schema"."table"`
/// statement gets its own `DISTRIBUTE BY` clause inserted before its own
/// terminator, looked up by that statement's own `(schema, table)` — instead
/// of inserting a single clause at the first `;` in the whole string, which
/// would only ever land on the root relation's statement.
pub fn append_distribution_clauses(
ddl: &str,
distributions: &HashMap<(String, String), DistributionPolicy>,
) -> Result<String, String> {
if distributions.is_empty() {
return Ok(ddl.to_string());
}
let mut output = String::with_capacity(ddl.len() + distributions.len() * 32);
let mut cursor = 0usize;
for range in db::ddl_scan::top_level_statement_ranges(ddl) {
let statement = &ddl[range.clone()];
let Some(relation) = db::ddl_scan::parse_create_table_relation(statement) else {
continue;
};
let Some(distribution) = distributions.get(&relation) else {
continue;
};
let clause = render_distribution_clause(distribution)?;
let insertion = if statement.ends_with(';') { range.end - 1 } else { range.end };
output.push_str(&ddl[cursor..insertion]);
output.push('\n');
output.push_str(&clause);
cursor = insertion;
}
output.push_str(&ddl[cursor..]);
Ok(output)
}
fn parse_distribution_policy(locator_type: &str, columns: Vec<String>) -> Result<Option<DistributionPolicy>, String> {
let column_distribution = |kind| {
if columns.is_empty() {
@@ -148,4 +240,32 @@ mod tests {
assert!(TABLE_DISTRIBUTION_SQL.contains("WITH ORDINALITY"));
assert!(TABLE_DISTRIBUTION_SQL.contains("NOT a.attisdropped"));
}
#[test]
fn batched_distribution_query_matches_opentenbase_catalog_contract() {
assert!(TABLE_DISTRIBUTION_FOR_RELATIONS_SQL.contains("unnest($1::text[], $2::text[])"));
assert!(TABLE_DISTRIBUTION_FOR_RELATIONS_SQL.contains("pg_catalog.pgxc_class"));
assert!(TABLE_DISTRIBUTION_FOR_RELATIONS_SQL.contains("WITH ORDINALITY"));
}
#[test]
fn appends_each_partition_its_own_distribution_clause_in_a_multi_relation_tree() {
let ddl = "CREATE TABLE \"public\".\"events\" (\n \"id\" integer\n) PARTITION BY RANGE (id);\n\nCREATE TABLE \"public\".\"events_2026\" PARTITION OF \"public\".\"events\" FOR VALUES FROM (1) TO (100);\n\nCREATE TABLE \"public\".\"events_2027\" PARTITION OF \"public\".\"events\" FOR VALUES FROM (100) TO (200);\n";
let mut distributions = HashMap::new();
distributions
.insert(("public".to_string(), "events".to_string()), DistributionPolicy::Hash(vec!["id".to_string()]));
distributions.insert(("public".to_string(), "events_2027".to_string()), DistributionPolicy::Replication);
// "events_2026" deliberately has no entry — it must be left untouched.
let rendered = append_distribution_clauses(ddl, &distributions).unwrap();
assert!(rendered.contains("PARTITION BY RANGE (id)\nDISTRIBUTE BY HASH (\"id\");"));
assert!(rendered.contains(
"CREATE TABLE \"public\".\"events_2027\" PARTITION OF \"public\".\"events\" FOR VALUES FROM (100) TO (200)\nDISTRIBUTE BY REPLICATION;"
));
assert!(rendered.contains(
"CREATE TABLE \"public\".\"events_2026\" PARTITION OF \"public\".\"events\" FOR VALUES FROM (1) TO (100);"
));
assert_eq!(rendered.matches("DISTRIBUTE BY").count(), 2);
}
}
+96 -74
View File
@@ -1018,6 +1018,37 @@ fn pg_error_to_string(err: tokio_postgres::Error) -> String {
err.as_db_error().map(ToString::to_string).unwrap_or_else(|| err.to_string())
}
/// Tries each SQL tier in `tiers` in order (most-capable first), via `run`,
/// returning the first tier that succeeds. Every driver-compat query in this
/// module (a "does this server have the newer catalog column" primary/compat
/// split, occasionally with a further information_schema fallback) used to
/// hand-roll this same try/log/combine-errors shape once per query; this is
/// the shared version.
///
/// If every tier fails, all of their errors are logged together at debug
/// level (so a fallback firing in production is diagnosable) and the last
/// tier's error is returned to the caller, since it's usually the most
/// specific one for whatever the connected server actually is.
async fn query_with_compat_fallback<T, F, Fut>(
log_context: &str,
tiers: &[&'static str],
mut run: F,
) -> Result<T, String>
where
F: FnMut(&'static str) -> Fut,
Fut: std::future::Future<Output = Result<T, tokio_postgres::Error>>,
{
let mut errors: Vec<String> = Vec::new();
for sql in tiers {
match run(sql).await {
Ok(value) => return Ok(value),
Err(error) => errors.push(pg_error_to_string(error)),
}
}
log::debug!("[postgres][{log_context}:compat-failed] {}", errors.join("; "));
Err(errors.into_iter().next_back().unwrap_or_else(|| format!("[postgres][{log_context}] no SQL tiers configured")))
}
fn pg_db_error_to_string(err: &tokio_postgres::error::DbError) -> String {
format!("{err} (SQLSTATE {})", err.code().code())
}
@@ -3243,23 +3274,11 @@ pub async fn get_columns_for_relations(
) -> Result<HashMap<i64, Vec<ColumnInfo>>, String> {
let oids: Vec<i64> = relations.iter().map(|(oid, _, _)| *oid).collect();
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let [primary_sql, compat_sql] = postgres_columns_for_relations_query_tiers();
match get_columns_for_relations_with_sql(&client, primary_sql, &oids).await {
Ok(columns) => Ok(columns),
Err(primary_error) => match get_columns_for_relations_with_sql(&client, compat_sql, &oids).await {
Ok(columns) => Ok(columns),
Err(fallback_error) => {
let primary_message = pg_error_to_string(primary_error);
let fallback_message = pg_error_to_string(fallback_error);
log::debug!(
"[postgres][get_columns_for_relations:compat-failed] primary_error={} fallback_error={}",
primary_message,
fallback_message
);
Err(fallback_message)
}
},
}
let tiers = postgres_columns_for_relations_query_tiers();
query_with_compat_fallback("get_columns_for_relations", &tiers, |sql| {
get_columns_for_relations_with_sql(&client, sql, &oids)
})
.await
}
async fn get_columns_for_relations_with_sql(
@@ -3280,6 +3299,15 @@ fn postgres_columns_for_relations_query_tiers() -> [&'static str; 2] {
[postgres_columns_for_relations_sql(), postgres_columns_for_relations_compat_sql()]
}
// Sibling of `POSTGRES_COLUMNS_SQL`/`POSTGRES_COLUMNS_COMPAT_SQL` below (~line
// 4880): same column list and detection logic (identity/serial inference,
// numeric precision/scale, enum values), batched by oid instead of a single
// (schema, table) pair. Kept as a separate literal rather than sharing a
// fragment — `pg_class` needs the `c` alias here for the oid filter, which
// pushes `information_schema.columns` to `ic` instead of the single-relation
// version's `c`, and every column's position in the SELECT list is relied on
// positionally by `column_info_from_row_offset`. A change to one almost
// certainly needs the same change in the other.
fn postgres_columns_for_relations_sql() -> &'static str {
"SELECT c.oid::bigint AS relid, a.attname AS column_name, \
format_type(a.atttypid, a.atttypmod) AS full_type, \
@@ -3352,6 +3380,8 @@ fn postgres_columns_for_relations_sql() -> &'static str {
ORDER BY c.oid, a.attnum"
}
// Compat-tier sibling of `POSTGRES_COLUMNS_COMPAT_SQL` (~line 4938) — see the
// note on `postgres_columns_for_relations_sql` above.
fn postgres_columns_for_relations_compat_sql() -> &'static str {
"SELECT c.oid::bigint AS relid, a.attname AS column_name, \
format_type(a.atttypid, a.atttypmod) AS full_type, \
@@ -3438,23 +3468,11 @@ pub async fn list_indexes_for_relations(
) -> Result<HashMap<i64, Vec<IndexInfo>>, String> {
let oids: Vec<i64> = relations.iter().map(|(oid, _, _)| *oid).collect();
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let [primary_sql, compat_sql] = postgres_indexes_for_relations_query_tiers();
match list_indexes_for_relations_with_sql(&client, primary_sql, &oids).await {
Ok(indexes) => Ok(indexes),
Err(primary_error) => match list_indexes_for_relations_with_sql(&client, compat_sql, &oids).await {
Ok(indexes) => Ok(indexes),
Err(fallback_error) => {
let primary_message = pg_error_to_string(primary_error);
let fallback_message = pg_error_to_string(fallback_error);
log::debug!(
"[postgres][list_indexes_for_relations:compat-failed] primary_error={} fallback_error={}",
primary_message,
fallback_message
);
Err(fallback_message)
}
},
}
let tiers = postgres_indexes_for_relations_query_tiers();
query_with_compat_fallback("list_indexes_for_relations", &tiers, |sql| {
list_indexes_for_relations_with_sql(&client, sql, &oids)
})
.await
}
async fn list_indexes_for_relations_with_sql(
@@ -3493,6 +3511,11 @@ fn postgres_indexes_for_relations_query_tiers() -> [&'static str; 2] {
[postgres_indexes_for_relations_sql(), postgres_indexes_for_relations_compat_sql()]
}
// Sibling of `POSTGRES_INDEXES_SQL`/`POSTGRES_INDEXES_COMPAT_SQL` (~line
// 6042): same index-detection logic, batched by oid instead of a single
// (schema, table) pair. Not merged into a shared fragment for the same
// reason as the columns queries above — an alias would need renaming to
// line up, and result columns are read positionally.
fn postgres_indexes_for_relations_sql() -> &'static str {
"SELECT t.oid::bigint AS relid, i.relname AS index_name, \
array_agg(COALESCE(a.attname, pg_get_indexdef(ix.indexrelid, k.n::int, true)) ORDER BY k.n) AS columns, \
@@ -3515,6 +3538,8 @@ fn postgres_indexes_for_relations_sql() -> &'static str {
ORDER BY t.oid, i.relname"
}
// Compat-tier sibling of `POSTGRES_INDEXES_COMPAT_SQL` (~line 6063) — see the
// note on `postgres_indexes_for_relations_sql` above.
fn postgres_indexes_for_relations_compat_sql() -> &'static str {
"SELECT t.oid::bigint AS relid, i.relname AS index_name, \
ARRAY( \
@@ -5101,6 +5126,9 @@ pub async fn list_schema_infos_with_system(pool: &Pool, show_system_schemas: boo
.collect())
}
// Sibling of `postgres_columns_for_relations_sql`/`_compat_sql` above (~line
// 3086), for a single (schema, table) instead of a batch of oids — see the
// note there about why these aren't merged into a shared fragment.
const POSTGRES_COLUMNS_SQL: &str = "SELECT a.attname AS column_name, \
format_type(a.atttypid, a.atttypmod) AS full_type, \
COALESCE(c.is_nullable = 'YES', NOT a.attnotnull) AS is_nullable, \
@@ -5169,6 +5197,8 @@ const POSTGRES_COLUMNS_SQL: &str = "SELECT a.attname AS column_name, \
AND a.attnum > 0 AND NOT a.attisdropped \
ORDER BY a.attnum";
// Compat-tier sibling of `postgres_columns_for_relations_compat_sql` (~line
// 3148) — see the note on `POSTGRES_COLUMNS_SQL` above.
const POSTGRES_COLUMNS_COMPAT_SQL: &str = "SELECT a.attname AS column_name, \
format_type(a.atttypid, a.atttypmod) AS full_type, \
COALESCE(c.is_nullable = 'YES', NOT a.attnotnull) AS is_nullable, \
@@ -5348,29 +5378,8 @@ async fn get_columns_with_sql(
pub async fn get_columns(pool: &Pool, schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let schema = if schema.is_empty() { "public" } else { schema };
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
match get_columns_with_sql(&client, POSTGRES_COLUMNS_SQL, schema, table).await {
Ok(columns) => Ok(columns),
Err(primary_error) => match get_columns_with_sql(&client, POSTGRES_COLUMNS_COMPAT_SQL, schema, table).await {
Ok(columns) => Ok(columns),
Err(fallback_error) => {
let primary_message = pg_error_to_string(primary_error);
let fallback_message = pg_error_to_string(fallback_error);
match get_columns_with_sql(&client, POSTGRES_COLUMNS_INFORMATION_SCHEMA_SQL, schema, table).await {
Ok(columns) => Ok(columns),
Err(information_schema_error) => {
let information_schema_message = pg_error_to_string(information_schema_error);
log::debug!(
"[postgres][get_columns:compat-failed] primary_error={} fallback_error={} information_schema_error={}",
primary_message,
fallback_message,
information_schema_message
);
Err(information_schema_message)
}
}
}
},
}
let tiers = [POSTGRES_COLUMNS_SQL, POSTGRES_COLUMNS_COMPAT_SQL, POSTGRES_COLUMNS_INFORMATION_SCHEMA_SQL];
query_with_compat_fallback("get_columns", &tiers, |sql| get_columns_with_sql(&client, sql, schema, table)).await
}
fn pg_quote_literal(value: &str) -> String {
@@ -6372,6 +6381,8 @@ async fn execute_query_with_max_rows_inner(
}
}
// Sibling of `postgres_indexes_for_relations_sql` (~line 3288), for a single
// (schema, table) instead of a batch of oids — see the note there.
const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \
array_agg(COALESCE(a.attname, pg_get_indexdef(ix.indexrelid, k.n::int, true)) ORDER BY k.n) AS columns, \
ix.indisunique AS is_unique, \
@@ -6393,6 +6404,8 @@ const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \
GROUP BY i.relname, i.oid, ix.indisunique, ix.indisprimary, ix.indpred, ix.indrelid, am.amname, ix.indnkeyatts, ix.indkey \
ORDER BY i.relname";
// Compat-tier sibling of `postgres_indexes_for_relations_compat_sql` (~line
// 3312) — see the note on `POSTGRES_INDEXES_SQL` above.
const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \
ARRAY( \
SELECT COALESCE(a.attname, pg_get_indexdef(ix.indexrelid, pos.n, true)) \
@@ -6465,7 +6478,7 @@ const POSTGRES_COLUMN_ACL_PRIVILEGES_SQL: &str =
WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r', 'p') \
ORDER BY 5, 1, 2, 3, 4";
fn postgres_owner_object_type(relkind: &str) -> &str {
pub(crate) fn postgres_owner_object_type(relkind: &str) -> &str {
match relkind {
"r" => "TABLE",
"v" => "VIEW",
@@ -6478,6 +6491,29 @@ fn postgres_owner_object_type(relkind: &str) -> &str {
}
}
/// Looks up a relation's own `relkind` (e.g. `'r'`, `'v'`, `'S'`), regardless
/// of whether it's a kind this driver otherwise treats as a table. Used to
/// give a specific diagnostic ("it's a view, not a table") instead of a bare
/// "not found" when a `(schema, table)` request turns out not to be a
/// table/partition/foreign table.
pub(crate) async fn postgres_relation_relkind(
pool: &Pool,
schema: &str,
table: &str,
) -> Result<Option<String>, String> {
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let row = client
.query_opt(
"SELECT c.relkind::text FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
WHERE n.nspname = $1 AND c.relname = $2 LIMIT 1",
&[&schema, &table],
)
.await
.map_err(pg_error_to_string)?;
row.map(|row| row.try_get::<_, String>(0)).transpose().map_err(pg_error_to_string)
}
async fn list_indexes_with_sql(
client: &deadpool_postgres::Client,
sql: &str,
@@ -6516,22 +6552,8 @@ async fn list_indexes_with_sql(
pub async fn list_indexes(pool: &Pool, schema: &str, table: &str) -> Result<Vec<IndexInfo>, String> {
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
match list_indexes_with_sql(&client, POSTGRES_INDEXES_SQL, schema, table).await {
Ok(indexes) => Ok(indexes),
Err(primary_error) => match list_indexes_with_sql(&client, POSTGRES_INDEXES_COMPAT_SQL, schema, table).await {
Ok(indexes) => Ok(indexes),
Err(fallback_error) => {
let primary_message = pg_error_to_string(primary_error);
let fallback_message = pg_error_to_string(fallback_error);
log::debug!(
"[postgres][list_indexes:compat-failed] primary_error={} fallback_error={}",
primary_message,
fallback_message
);
Err(fallback_message)
}
},
}
let tiers = [POSTGRES_INDEXES_SQL, POSTGRES_INDEXES_COMPAT_SQL];
query_with_compat_fallback("list_indexes", &tiers, |sql| list_indexes_with_sql(&client, sql, schema, table)).await
}
/// Names of same-table indexes whose `pg_index.indisvalid` is `false`.
+131 -9
View File
@@ -9812,9 +9812,17 @@ pub async fn pg_ddl_with_partitions(
let Some(root) =
tree.iter().find(|node| !node.parent_oid.is_some_and(|parent_oid| tree_oids.contains(&parent_oid)))
else {
return Err(format!(
"relation \"{schema}\".\"{table}\" was not found or is not a table/partition/foreign table"
));
// The tree query only matches relkind IN ('r','p','f'), so an empty
// tree could mean the relation doesn't exist at all, or that it
// exists but is a view/sequence/other non-table object. Look up its
// relkind (if any) to tell those two cases apart in the error.
return Err(match db::postgres::postgres_relation_relkind(pool, schema, table).await {
Ok(Some(relkind)) => format!(
"relation \"{schema}\".\"{table}\" is a {}, not a table/partition/foreign table",
db::postgres::postgres_owner_object_type(&relkind)
),
_ => format!("relation \"{schema}\".\"{table}\" was not found or is not a table/partition/foreign table"),
});
};
let oids: Vec<i64> = tree.iter().map(|node| node.oid).collect();
@@ -9841,6 +9849,18 @@ pub async fn pg_ddl_with_partitions(
db::postgres::get_table_partition_local_objects_for_relations(pool, &oids),
)?;
// `list_foreign_keys_for_relations` is keyed by (schema, table) since its
// query is information_schema-based, unlike every other *_by_oid map
// here. Remap it once so the render loop below can look fkeys up by oid
// like everything else, instead of cloning a fresh (schema, table) key
// on every node it renders.
let relation_oid_by_pair: HashMap<(String, String), i64> =
relations.into_iter().map(|(oid, schema, table)| ((schema, table), oid)).collect();
let fkeys_by_oid: HashMap<i64, Vec<db::ForeignKeyInfo>> = fkeys_by_relation
.into_iter()
.filter_map(|(key, fkeys)| relation_oid_by_pair.get(&key).map(|oid| (*oid, fkeys)))
.collect();
// Group children by parent oid, each group ordered by relname to match
// `list_table_partitions`' `ORDER BY c.relname` (today's traversal order).
let mut children_by_parent: HashMap<i64, Vec<&db::postgres::PostgresPartitionTreeNode>> = HashMap::new();
@@ -9859,7 +9879,7 @@ pub async fn pg_ddl_with_partitions(
&children_by_parent,
&columns_by_oid,
&indexes_by_oid,
&fkeys_by_relation,
&fkeys_by_oid,
&comments_by_oid,
&triggers_by_oid,
&checks_by_oid,
@@ -9880,7 +9900,7 @@ fn render_postgres_partition_tree_node(
children_by_parent: &HashMap<i64, Vec<&db::postgres::PostgresPartitionTreeNode>>,
columns_by_oid: &HashMap<i64, Vec<db::ColumnInfo>>,
indexes_by_oid: &HashMap<i64, Vec<db::IndexInfo>>,
fkeys_by_relation: &HashMap<(String, String), Vec<db::ForeignKeyInfo>>,
fkeys_by_oid: &HashMap<i64, Vec<db::ForeignKeyInfo>>,
comments_by_oid: &HashMap<i64, Option<String>>,
triggers_by_oid: &HashMap<i64, Vec<String>>,
checks_by_oid: &HashMap<i64, Vec<(String, String)>>,
@@ -9903,8 +9923,8 @@ fn render_postgres_partition_tree_node(
let columns = columns_by_oid.get(&node.oid).unwrap_or(&empty_columns);
let indexes = indexes_by_oid.get(&node.oid).unwrap_or(&empty_indexes);
let fkeys = fkeys_by_relation.get(&(node.schema.clone(), node.table.clone())).unwrap_or(&empty_fkeys);
let comment = comments_by_oid.get(&node.oid).cloned().flatten();
let fkeys = fkeys_by_oid.get(&node.oid).unwrap_or(&empty_fkeys);
let comment = comments_by_oid.get(&node.oid).and_then(|comment| comment.as_deref());
let triggers = triggers_by_oid.get(&node.oid).unwrap_or(&empty_triggers);
let checks = checks_by_oid.get(&node.oid).unwrap_or(&empty_checks);
let local_objects = local_objects_by_oid.get(&node.oid).unwrap_or(&empty_local_objects);
@@ -9920,7 +9940,7 @@ fn render_postgres_partition_tree_node(
indexes,
fkeys,
checks,
comment.as_deref(),
comment,
&node.partition_info,
local_objects,
),
@@ -10195,13 +10215,82 @@ fn append_opengauss_trigger_definitions(mut ddl: String, trigger_definitions: &[
ddl
}
/// Cloudberry's native `pg_get_tabledef` (a community-maintained PL/pgSQL
/// function most Cloudberry/Greenplum installs have, not a built-in) renders
/// exactly one relation — for a partitioned table it emits that table's own
/// `CREATE TABLE ... PARTITION BY ...`, never its partitions' own `CREATE
/// TABLE ... PARTITION OF ...` statements, regardless of whether the table
/// was created with classic Greenplum or PostgreSQL-style declarative
/// partition syntax. When `include_partitions` is requested, fetch the same
/// partition tree the plain-Postgres path uses and append each descendant's
/// own native DDL, so Cloudberry's more accurate native rendering (storage
/// options, distribution policy, external-table clauses) is still used per
/// relation instead of falling back to the generic renderer for the whole
/// tree.
async fn cloudberry_native_tree_ddl(
pool: &deadpool_postgres::Pool,
schema: &str,
table: &str,
include_partitions: bool,
) -> Result<String, String> {
let root_ddl = db::cloudberry::table_ddl(pool, schema, table).await?;
if !include_partitions {
return Ok(root_ddl);
}
let tree = db::postgres::fetch_postgres_partition_tree(pool, schema, table).await?;
if tree.len() <= 1 {
return Ok(root_ddl);
}
let tree_oids: HashSet<i64> = tree.iter().map(|node| node.oid).collect();
let Some(root) =
tree.iter().find(|node| !node.parent_oid.is_some_and(|parent_oid| tree_oids.contains(&parent_oid)))
else {
return Ok(root_ddl);
};
let mut children_by_parent: HashMap<i64, Vec<&db::postgres::PostgresPartitionTreeNode>> = HashMap::new();
for node in &tree {
if let Some(parent_oid) = node.parent_oid {
children_by_parent.entry(parent_oid).or_default().push(node);
}
}
for children in children_by_parent.values_mut() {
children.sort_by(|a, b| a.table.cmp(&b.table));
}
let mut descendants = Vec::new();
let mut visited: HashSet<i64> = HashSet::new();
let mut stack: Vec<&db::postgres::PostgresPartitionTreeNode> = vec![root];
while let Some(node) = stack.pop() {
if !visited.insert(node.oid) {
continue;
}
if node.oid != root.oid {
descendants.push(node);
}
if let Some(children) = children_by_parent.get(&node.oid) {
for child in children.iter().rev() {
stack.push(child);
}
}
}
let mut ddl = root_ddl;
for node in descendants {
let child_ddl = db::cloudberry::table_ddl(pool, &node.schema, &node.table).await?;
ddl.push('\n');
ddl.push_str(&child_ddl);
}
Ok(ddl)
}
pub async fn cloudberry_ddl(
pool: &deadpool_postgres::Pool,
schema: &str,
table: &str,
include_partitions: bool,
) -> Result<String, String> {
match db::cloudberry::table_ddl(pool, schema, table).await {
match cloudberry_native_tree_ddl(pool, schema, table, include_partitions).await {
Ok(ddl) => Ok(ddl),
Err(native_error) => {
let base_ddl = pg_ddl_for_options(pool, schema, table, include_partitions).await.map_err(|fallback_error| {
@@ -10228,6 +10317,39 @@ pub async fn opentenbase_ddl(
include_partitions: bool,
) -> Result<String, String> {
let ddl = pg_ddl_for_options(pool, schema, table, include_partitions).await?;
if include_partitions {
// The rendered ddl may cover the whole partition tree (root plus
// every partition), each as its own `CREATE [FOREIGN] TABLE`
// statement, so distribution policies need to be looked up and
// applied per relation rather than once for the root.
let relations: Vec<(String, String)> = db::ddl_scan::top_level_statement_ranges(&ddl)
.into_iter()
.filter_map(|range| db::ddl_scan::parse_create_table_relation(&ddl[range]))
.collect();
return match db::opentenbase::table_distribution_for_relations(pool, &relations).await {
Ok(distributions) => match db::opentenbase::append_distribution_clauses(&ddl, &distributions) {
Ok(ddl) => Ok(ddl),
Err(error) => {
log::warn!(
"[schema][opentenbase:table-ddl-distribution-render-fallback] schema={} table={} error={}",
schema,
table,
error
);
Ok(ddl)
}
},
Err(error) => {
log::warn!(
"[schema][opentenbase:table-ddl-distribution-query-fallback] schema={} table={} error={}",
schema,
table,
error
);
Ok(ddl)
}
};
}
match db::opentenbase::table_distribution(pool, schema, table).await {
Ok(Some(distribution)) => match db::opentenbase::append_distribution_clause(&ddl, &distribution) {
Ok(ddl) => Ok(ddl),