对于没有主键的表,Database Migration Service 支持在变更数据捕获 (CDC) 阶段迁移初始快照和 INSERT 语句。
如需更新缺少的 UPDATE 和 DELETE 进程,请参阅本文档的后续部分。
检测源实例和 Cloud SQL 目标实例之间缺失的数据
确定哪些表没有主键:
select tab.table_schema,
tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco
on tab.table_schema = tco.table_schema
and tab.table_name = tco.table_name
and tco.constraint_type = 'PRIMARY KEY'
where tab.table_type = 'BASE TABLE'
and tab.table_schema not in ('pg_catalog', 'information_schema', 'pglogical')
and tco.constraint_name is null
order by table_schema,
table_name;
在开始迁移之前,请针对没有主键的所有表,使用以下查询检查是否有任何更新或删除操作:
SELECT schemaname,
relname,
n_tup_ins,
n_tup_upd,
n_tup_del
FROM pg_stat_user_tables
WHERE schemaname NOT IN
('pglogical', 'pg_catalog', 'information_schema');
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eThis page provides debugging scripts and guidance for using PostgreSQL within the Database Migration Service.\u003c/p\u003e\n"],["\u003cp\u003eDatabase Migration Service supports the migration of initial snapshots and \u003ccode\u003eINSERT\u003c/code\u003e statements for tables without primary keys, but \u003ccode\u003eUPDATE\u003c/code\u003e and \u003ccode\u003eDELETE\u003c/code\u003e operations may need manual intervention.\u003c/p\u003e\n"],["\u003cp\u003eYou can identify tables lacking primary keys and check for any updates or deletes on the source during migration using the provided SQL queries against the \u003ccode\u003einformation_schema\u003c/code\u003e and \u003ccode\u003epg_stat_user_tables\u003c/code\u003e tables.\u003c/p\u003e\n"],["\u003cp\u003eIf data discrepancies are found between the source and the Cloud SQL destination instance, you can manually resolve them by comparing and updating data, or by utilizing \u003ccode\u003epg_dump\u003c/code\u003e/\u003ccode\u003epg_restore\u003c/code\u003e or the \u003ccode\u003eCOPY\u003c/code\u003e command for the affected tables.\u003c/p\u003e\n"],["\u003cp\u003eIt is important to potentially clean the replica before using \u003ccode\u003epg_restore\u003c/code\u003e or \u003ccode\u003eCOPY\u003c/code\u003e commands if there is data that was previously migrated.\u003c/p\u003e\n"]]],[],null,[]]