이 페이지에는 PostgreSQL 디버깅 및 사용에 도움이 되는 스크립트가 포함되어 있습니다.
기본 키가 아닌 테이블의 UPDATE 및 DELETE 작업 마이그레이션
기본 키가 없는 테이블의 경우 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');
위치:
n_tup_ins: 삽입된 행 수
n_tup_upd: 업데이트된 행 수 (HOT 업데이트된 행 포함)
n_tup_del: 삭제된 행 수
이 결과를 별도의 테이블이나 파일에 저장합니다.
이전 설정이 완료되면 쿼리를 다시 실행합니다.
이 결과를 3단계의 결과와 비교합니다.
마이그레이션 중에 소스의 n_tup_upd 또는 n_tup_del 값에 차이가 있으면 소스에 일부 업데이트 또는 삭제가 있을 수 있습니다.
소스에서 Cloud SQL 대상 인스턴스로 데이터를 수동으로 마이그레이션
소스와 Cloud SQL 대상 인스턴스 간에 약간의 불일치가 있는 것으로 감지되면 다음 옵션 중 하나를 사용하여 데이터를 마이그레이션할 수 있습니다.
옵션 1: 소스와 Cloud SQL 대상 간에 데이터를 수동으로 비교하고 적절한 SQL 쿼리를 실행하여 소스와 복제본 간에 다른 데이터만 업데이트합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-01(UTC)"],[[["\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,["# Debugging and other tools\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n[MySQL](/database-migration/docs/mysql/debugging-tools \"View this page for the MySQL version of Database Migration Service.\") \\| PostgreSQL \\| [PostgreSQL to AlloyDB](/database-migration/docs/postgresql-to-alloydb/debugging-tools \"View this page for the PostgreSQL to AlloyDB version of Database Migration Service.\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nOverview\n--------\n\nThis page includes scripts that help with debugging and using PostgreSQL.\n\nMigrate UPDATE and DELETE operations for non-primary key tables\n---------------------------------------------------------------\n\nFor tables that don't have primary keys, Database Migration Service supports migration of the initial snapshot and `INSERT` statements during the change data capture (CDC) phase.\n\nTo update the missing `UPDATE` and `DELETE` processes, refer to the later sections of this document.\n\n### Detect missing data between the source and Cloud SQL destination instances\n\n1. Identify which tables don't have primary keys: \n\n ```\n select tab.table_schema,\n tab.table_name\n from information_schema.tables tab\n left join information_schema.table_constraints tco \n on tab.table_schema = tco.table_schema\n and tab.table_name = tco.table_name \n and tco.constraint_type = 'PRIMARY KEY'\n where tab.table_type = 'BASE TABLE'\n and tab.table_schema not in ('pg_catalog', 'information_schema', 'pglogical')\n and tco.constraint_name is null\n order by table_schema,\n table_name;\n ```\n2. Before starting the migration, for all tables without primary keys, check if there are any updates or deletes using this query: \n\n ```\n SELECT schemaname,\n relname,\n n_tup_ins,\n n_tup_upd,\n n_tup_del\n FROM pg_stat_user_tables\n WHERE schemaname NOT IN\n ('pglogical', 'pg_catalog', 'information_schema');\n ```\n Where:\n - `n_tup_ins`: number of rows inserted\n - `n_tup_upd`: number of rows updated (includes HOT updated rows)\n - `n_tup_del`: number of rows deleted\n3. Save these results either in a separate table or in a file.\n4. After the migration setup is complete, run the query again.\n5. Compare the results with the results from step 3.\n\nIf there are differences in values of `n_tup_upd` or `n_tup_del` on the source during migration, there might be some updates or deletes on the source.\n| **Note:** You can manually reset `pg_stat_user_tables` using the `pg_stat_reset()` function. If this function is run on the source, the statistics might not reflect accurate information.\n\n### Manually migrate data from the source to the Cloud SQL destination instances\n\nIf you detect there are some discrepancies between the source and Cloud SQL destination instance, you can migrate the data using one of the options:\n\n- **Option 1**: Manually compare the data between the source and Cloud SQL destination and execute appropriate SQL queries to update only the data that is different between the source and the replica.\n\n- **Option 2** : Run `pg_dump` and `pg_restore` for the tables without primary keys. See [Export data from an on-premises PostgreSQL server using pg_dump](/sql/docs/postgres/import-export/import-export-dmp#external-server) for more information.\n\n- **Option 3** : Use Postgres `COPY` command to migrate your data. See [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html) for more information. Note that this step requires the replica VM to be able to connect to the source VM.\n\n| For **Option 2** and **Option 3** , if there is any data that is already migrated on to the replica, those records might require cleanup before running the `pg_restore` or `COPY` commands. Please make changes accordingly."]]