@@ -418,7 +418,7 @@ logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
418
418
static void
419
419
logicalrep_partmap_invalidate_cb (Datum arg , Oid reloid )
420
420
{
421
- LogicalRepRelMapEntry * entry ;
421
+ LogicalRepPartMapEntry * entry ;
422
422
423
423
/* Just to be sure. */
424
424
if (LogicalRepPartMap == NULL )
@@ -431,11 +431,11 @@ logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
431
431
hash_seq_init (& status , LogicalRepPartMap );
432
432
433
433
/* TODO, use inverse lookup hashtable? */
434
- while ((entry = (LogicalRepRelMapEntry * ) hash_seq_search (& status )) != NULL )
434
+ while ((entry = (LogicalRepPartMapEntry * ) hash_seq_search (& status )) != NULL )
435
435
{
436
- if (entry -> localreloid == reloid )
436
+ if (entry -> relmapentry . localreloid == reloid )
437
437
{
438
- entry -> localrelvalid = false;
438
+ entry -> relmapentry . localrelvalid = false;
439
439
hash_seq_term (& status );
440
440
break ;
441
441
}
@@ -448,8 +448,8 @@ logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
448
448
449
449
hash_seq_init (& status , LogicalRepPartMap );
450
450
451
- while ((entry = (LogicalRepRelMapEntry * ) hash_seq_search (& status )) != NULL )
452
- entry -> localrelvalid = false;
451
+ while ((entry = (LogicalRepPartMapEntry * ) hash_seq_search (& status )) != NULL )
452
+ entry -> relmapentry . localrelvalid = false;
453
453
}
454
454
}
455
455
@@ -502,7 +502,6 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
502
502
Oid partOid = RelationGetRelid (partrel );
503
503
AttrMap * attrmap = root -> attrmap ;
504
504
bool found ;
505
- int i ;
506
505
MemoryContext oldctx ;
507
506
508
507
if (LogicalRepPartMap == NULL )
@@ -513,31 +512,40 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
513
512
(void * ) & partOid ,
514
513
HASH_ENTER , & found );
515
514
516
- if (found )
517
- return & part_entry -> relmapentry ;
515
+ entry = & part_entry -> relmapentry ;
518
516
519
- memset (part_entry , 0 , sizeof (LogicalRepPartMapEntry ));
517
+ if (found && entry -> localrelvalid )
518
+ return entry ;
520
519
521
520
/* Switch to longer-lived context. */
522
521
oldctx = MemoryContextSwitchTo (LogicalRepPartMapContext );
523
522
524
- part_entry -> partoid = partOid ;
523
+ if (!found )
524
+ {
525
+ memset (part_entry , 0 , sizeof (LogicalRepPartMapEntry ));
526
+ part_entry -> partoid = partOid ;
527
+ }
525
528
526
- /* Remote relation is copied as-is from the root entry. */
527
- entry = & part_entry -> relmapentry ;
528
- entry -> remoterel .remoteid = remoterel -> remoteid ;
529
- entry -> remoterel .nspname = pstrdup (remoterel -> nspname );
530
- entry -> remoterel .relname = pstrdup (remoterel -> relname );
531
- entry -> remoterel .natts = remoterel -> natts ;
532
- entry -> remoterel .attnames = palloc (remoterel -> natts * sizeof (char * ));
533
- entry -> remoterel .atttyps = palloc (remoterel -> natts * sizeof (Oid ));
534
- for (i = 0 ; i < remoterel -> natts ; i ++ )
529
+ if (!entry -> remoterel .remoteid )
535
530
{
536
- entry -> remoterel .attnames [i ] = pstrdup (remoterel -> attnames [i ]);
537
- entry -> remoterel .atttyps [i ] = remoterel -> atttyps [i ];
531
+ int i ;
532
+
533
+ /* Remote relation is copied as-is from the root entry. */
534
+ entry = & part_entry -> relmapentry ;
535
+ entry -> remoterel .remoteid = remoterel -> remoteid ;
536
+ entry -> remoterel .nspname = pstrdup (remoterel -> nspname );
537
+ entry -> remoterel .relname = pstrdup (remoterel -> relname );
538
+ entry -> remoterel .natts = remoterel -> natts ;
539
+ entry -> remoterel .attnames = palloc (remoterel -> natts * sizeof (char * ));
540
+ entry -> remoterel .atttyps = palloc (remoterel -> natts * sizeof (Oid ));
541
+ for (i = 0 ; i < remoterel -> natts ; i ++ )
542
+ {
543
+ entry -> remoterel .attnames [i ] = pstrdup (remoterel -> attnames [i ]);
544
+ entry -> remoterel .atttyps [i ] = remoterel -> atttyps [i ];
545
+ }
546
+ entry -> remoterel .replident = remoterel -> replident ;
547
+ entry -> remoterel .attkeys = bms_copy (remoterel -> attkeys );
538
548
}
539
- entry -> remoterel .replident = remoterel -> replident ;
540
- entry -> remoterel .attkeys = bms_copy (remoterel -> attkeys );
541
549
542
550
entry -> localrel = partrel ;
543
551
entry -> localreloid = partOid ;
@@ -562,7 +570,11 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
562
570
{
563
571
AttrNumber root_attno = map -> attnums [attno ];
564
572
565
- entry -> attrmap -> attnums [attno ] = attrmap -> attnums [root_attno - 1 ];
573
+ /* 0 means it's a dropped attribute. See comments atop AttrMap. */
574
+ if (root_attno == 0 )
575
+ entry -> attrmap -> attnums [attno ] = -1 ;
576
+ else
577
+ entry -> attrmap -> attnums [attno ] = attrmap -> attnums [root_attno - 1 ];
566
578
}
567
579
}
568
580
else
0 commit comments