@@ -77,7 +77,7 @@ PG_MODULE_MAGIC;
7777static void rewrite_table_impl (char * relschema_src , char * relname_src ,
7878 char * relname_new , char * relschema_dst ,
7979 char * relname_dst );
80- static Relation get_identity_index (Relation rel_dst , TupleDesc tupdesc_src );
80+ static Relation get_identity_index (Relation rel_dst , Relation rel_src );
8181static int index_cat_info_compare (const void * arg1 , const void * arg2 );
8282
8383/* The WAL segment being decoded. */
@@ -821,8 +821,7 @@ rewrite_table_impl(char *relschema_src, char *relname_src,
821821 LogicalDecodingContext * ctx ;
822822 ReplicationSlot * slot ;
823823 Snapshot snap_hist ;
824- TupleDesc tup_desc_src ,
825- ident_src_tupdesc ;
824+ TupleDesc tup_desc_src ;
826825 CatalogState * cat_state ;
827826 XLogRecPtr end_of_wal ;
828827 XLogRecPtr xlog_insert_ptr ;
@@ -1033,12 +1032,6 @@ rewrite_table_impl(char *relschema_src, char *relname_src,
10331032 RelationGetRelationName (rel_src ))));
10341033 }
10351034
1036- /*
1037- * Store the identity of the source relation, in order to check that of
1038- * the destination table (or its partitions).
1039- */
1040- ident_src_tupdesc = get_index_tuple_desc (ident_idx_src );
1041-
10421035 /* TODO Move this branch into a function. */
10431036 if (part_desc )
10441037 {
@@ -1090,8 +1083,7 @@ rewrite_table_impl(char *relschema_src, char *relname_src,
10901083 RelationGetRelationName (partition ),
10911084 RelationGetRelationName (rel_dst ));
10921085
1093- entry -> ident_index = get_identity_index (partition ,
1094- ident_src_tupdesc );
1086+ entry -> ident_index = get_identity_index (partition , rel_src );
10951087 entry -> ind_slot = table_slot_create (partition , NULL );
10961088 /* Expect many insertions. */
10971089 entry -> bistate = GetBulkInsertState ();
@@ -1110,12 +1102,11 @@ rewrite_table_impl(char *relschema_src, char *relname_src,
11101102 }
11111103 else
11121104 {
1113- ident_index = get_identity_index (rel_dst , ident_src_tupdesc );
1105+ ident_index = get_identity_index (rel_dst , rel_src );
11141106 ident_key = build_identity_key (ident_index , & ident_key_nentries );
11151107 ind_slot = table_slot_create (rel_dst , NULL );
11161108 }
11171109 Assert (ident_key_nentries > 0 );
1118- FreeTupleDesc (ident_src_tupdesc );
11191110
11201111 /*
11211112 * We need to know whether no DDL took place that allows for data
@@ -1391,15 +1382,15 @@ rewrite_table_impl(char *relschema_src, char *relname_src,
13911382}
13921383
13931384/*
1394- * Check if identity index of 'rel_dest' has tuple descriptor that matches
1395- * 'tupdesc_src' and return the index .
1385+ * Check that both relations have matching identity indexes and return the
1386+ * identity index of 'rel_dst' .
13961387 */
13971388static Relation
1398- get_identity_index (Relation rel_dst , TupleDesc tupdesc_src )
1389+ get_identity_index (Relation rel_dst , Relation rel_src )
13991390{
1400- Oid index_dst_oid ;
1401- Relation index_dst ;
1402- TupleDesc tupdesc_dst ;
1391+ Oid index_dst_oid , index_src_oid ;
1392+ Relation index_dst , index_src ;
1393+ TupleDesc tupdesc_dst , tupdesc_src ;
14031394 bool match = true;
14041395
14051396 index_dst_oid = RelationGetReplicaIndex (rel_dst );
@@ -1409,6 +1400,14 @@ get_identity_index(Relation rel_dst, TupleDesc tupdesc_src)
14091400 index_dst = index_open (index_dst_oid , AccessShareLock );
14101401 tupdesc_dst = RelationGetDescr (index_dst );
14111402
1403+ index_src_oid = RelationGetReplicaIndex (rel_src );
1404+ if (!OidIsValid (index_src_oid ))
1405+ elog (ERROR , "Identity index missing on table \"%s\"" ,
1406+ RelationGetRelationName (rel_src ));
1407+ index_src = index_open (index_src_oid , AccessShareLock );
1408+ tupdesc_src = RelationGetDescr (index_src );
1409+
1410+
14121411 /*
14131412 * The tuple descriptors might not be equal, since some attributes of
14141413 * rel_dst can have different types. What should match though is attribute
@@ -1437,8 +1436,10 @@ get_identity_index(Relation rel_dst, TupleDesc tupdesc_src)
14371436
14381437 if (!match )
14391438 elog (ERROR ,
1440- "identity index on table \"%s\" does not match that on the source table" ,
1441- RelationGetRelationName (rel_dst ));
1439+ "identity index on table \"%s\" does not match that on table \"%s\"" ,
1440+ RelationGetRelationName (rel_dst ), RelationGetRelationName (rel_src ));
1441+
1442+ index_close (index_src , AccessShareLock );
14421443
14431444 return index_dst ;
14441445}
0 commit comments