@@ -504,7 +504,12 @@ list_member_parenthood(List *list, Oid relid)
504504status = (cached_parenthood_status * )lfirst (lc );
505505
506506if (status -> relid == relid )
507+ {
508+ /* This should NEVER happen! */
509+ Assert (status -> parenthood_status != PARENTHOOD_NOT_SET );
510+
507511return status -> parenthood_status ;
512+ }
508513}
509514
510515return PARENTHOOD_NOT_SET ;
@@ -562,23 +567,28 @@ assign_rel_parenthood_status(Index query_level,
562567/* Search for a parenthood status */
563568existing_status = list_member_parenthood (nth_parenthood_list ,relid );
564569
570+ /* Append new status entry if we couldn't find it */
571+ if (existing_status == PARENTHOOD_NOT_SET )
572+ {
573+ /* Append new element (relid, status) */
574+ old_mcxt = MemoryContextSwitchTo (TopTransactionContext );
575+ nth_parenthood_list = lappend_parenthood (nth_parenthood_list ,
576+ relid ,new_status );
577+ MemoryContextSwitchTo (old_mcxt );
578+
579+ /* Update ListCell */
580+ lfirst (nth_parenthood_cell )= nth_parenthood_list ;
581+ }
582+
565583/* Parenthood statuses mismatched, emit an ERROR */
566- if (existing_status != new_status && existing_status != PARENTHOOD_NOT_SET )
584+ else if (existing_status != new_status )
567585{
568586/* Don't forget to clear all lists! */
569587reset_parenthood_statuses ();
570588
571589elog (ERROR ,"It is prohibited to apply ONLY modifier to partitioned "
572590"tables which have already been mentioned without ONLY" );
573591}
574-
575- /* Append new element (relid, status) */
576- old_mcxt = MemoryContextSwitchTo (TopTransactionContext );
577- nth_parenthood_list = lappend_parenthood (nth_parenthood_list ,relid ,new_status );
578- MemoryContextSwitchTo (old_mcxt );
579-
580- /* Update ListCell */
581- lfirst (nth_parenthood_cell )= nth_parenthood_list ;
582592}
583593
584594/* Get parenthood status (per query level) */