src/backend/replication/logical/reorderbuffer.c
static void
ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
{
	char		path[MAXPGPATH];
	int			fd;
	int			readBytes;
	LogicalRewriteMappingData map;

	sprintf(path, "pg_logical/mappings/%s", fname);
	fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
	if (fd < 0)
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not open file \"%s\": %m", path)));

	while (true)
	{
		ReorderBufferTupleCidKey key;
		ReorderBufferTupleCidEnt *ent;
		ReorderBufferTupleCidEnt *new_ent;
		bool		found;

		/* be careful about padding */
		memset(&key, 0, sizeof(ReorderBufferTupleCidKey));

		/* read all mappings till the end of the file */
		readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));

		if (readBytes < 0)
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not read file \"%s\": %m",
							path)));
		else if (readBytes == 0)	/* EOF */
			break;
		else if (readBytes != sizeof(LogicalRewriteMappingData))
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not read from file \"%s\": read %d instead of %d bytes",
							path, readBytes,
							(int32) sizeof(LogicalRewriteMappingData))));

		key.relnode = map.old_node;
		ItemPointerCopy(&map.old_tid,
						&key.tid);


		ent = (ReorderBufferTupleCidEnt *)
			hash_search(tuplecid_data,
						(void *) &key,
						HASH_FIND,
						NULL);

		/* no existing mapping, no need to update */
		if (!ent)
			continue;

		key.relnode = map.new_node;
		ItemPointerCopy(&map.new_tid,
						&key.tid);

		new_ent = (ReorderBufferTupleCidEnt *)
			hash_search(tuplecid_data,
						(void *) &key,
						HASH_ENTER,
						&found);

		if (found)
		{
			/*
			 * Make sure the existing mapping makes sense. We sometime update
			 * old records that did not yet have a cmax (e.g. pg_class' own
			 * entry while rewriting it) during rewrites, so allow that.
			 */
			Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
			Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
		}
		else
		{
			/* update mapping */
			new_ent->cmin = ent->cmin;
			new_ent->cmax = ent->cmax;
			new_ent->combocid = ent->combocid;
		}
	}
}
/*
 * Apply a single mapping file to tuplecid_data.
 *
 * The mapping file has to have been verified to be a) committed b) for our
 * transaction c) applied in LSN order.
 */
Apply a single mapping file to tuplecid_data.
The mapping file has to have been verified to be a) committed b) for our
transaction c) applied in LSN order.
Apply a single mapping file to tuplecid_data . The mapping file has to have been verified to be a ) committed b ) for our transaction c ) applied in LSN order .
static void
ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
{
	char		path[MAXPGPATH];
	int			fd;
	int			readBytes;
	LogicalRewriteMappingData map;
	sprintf(path, "pg_logical/mappings/%s", fname);
	fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
	if (fd < 0)
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not open file \"%s\": %m", path)));
	while (true)
	{
		ReorderBufferTupleCidKey key;
		ReorderBufferTupleCidEnt *ent;
		ReorderBufferTupleCidEnt *new_ent;
		bool		found;
		memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
		readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));
		if (readBytes < 0)
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not read file \"%s\": %m",
							path)));
		else if (readBytes == 0)	
			break;
		else if (readBytes != sizeof(LogicalRewriteMappingData))
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not read from file \"%s\": read %d instead of %d bytes",
							path, readBytes,
							(int32) sizeof(LogicalRewriteMappingData))));
		key.relnode = map.old_node;
		ItemPointerCopy(&map.old_tid,
						&key.tid);
		ent = (ReorderBufferTupleCidEnt *)
			hash_search(tuplecid_data,
						(void *) &key,
						HASH_FIND,
						NULL);
		if (!ent)
			continue;
		key.relnode = map.new_node;
		ItemPointerCopy(&map.new_tid,
						&key.tid);
		new_ent = (ReorderBufferTupleCidEnt *)
			hash_search(tuplecid_data,
						(void *) &key,
						HASH_ENTER,
						&found);
		if (found)
		{
			Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
			Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
		}
		else
		{
			new_ent->cmin = ent->cmin;
			new_ent->cmax = ent->cmax;
			new_ent->combocid = ent->combocid;
		}
	}
}
static void ApplyLogicalMappingFile ( HTAB * tuplecid_data , Oid relid , const char * fname ) { char path [ MAXPGPATH ] ; int fd ; int readBytes ; LogicalRewriteMappingData map ; sprintf ( path , " " , fname ) ; fd = OpenTransientFile ( path , O_RDONLY | PG_BINARY , 0 ) ; if ( fd < 0 ) ereport ( ERROR , ( errcode_for_file_access ( ) , errmsg ( " \" \" " , path ) ) ) ; while ( true ) { ReorderBufferTupleCidKey key ; ReorderBufferTupleCidEnt * ent ; ReorderBufferTupleCidEnt * new_ent ; bool found ; memset ( & key , 0 , sizeof ( ReorderBufferTupleCidKey ) ) ; readBytes = read ( fd , & map , sizeof ( LogicalRewriteMappingData ) ) ; if ( readBytes < 0 ) ereport ( ERROR , ( errcode_for_file_access ( ) , errmsg ( " \" \" " , path ) ) ) ; else if ( readBytes == 0 ) break ; else if ( readBytes != sizeof ( LogicalRewriteMappingData ) ) ereport ( ERROR , ( errcode_for_file_access ( ) , errmsg ( " \" \" " , path , readBytes , ( int32 ) sizeof ( LogicalRewriteMappingData ) ) ) ) ; key . relnode = map . old_node ; ItemPointerCopy ( & map . old_tid , & key . tid ) ; ent = ( ReorderBufferTupleCidEnt * ) hash_search ( tuplecid_data , ( void * ) & key , HASH_FIND , NULL ) ; if ( ! ent ) continue ; key . relnode = map . new_node ; ItemPointerCopy ( & map . new_tid , & key . tid ) ; new_ent = ( ReorderBufferTupleCidEnt * ) hash_search ( tuplecid_data , ( void * ) & key , HASH_ENTER , & found ) ; if ( found ) { Assert ( ent -> cmin == InvalidCommandId || ent -> cmin == new_ent -> cmin ) ; Assert ( ent -> cmax == InvalidCommandId || ent -> cmax == new_ent -> cmax ) ; } else { new_ent -> cmin = ent -> cmin ; new_ent -> cmax = ent -> cmax ; new_ent -> combocid = ent -> combocid ; } } }
Apply a single mapping file to tuplecid_data.
Apply a single mapping file to tuplecid_data .
/* be careful about padding */ /* read all mappings till the end of the file */ /* EOF */ /* no existing mapping, no need to update */ /*
			 * Make sure the existing mapping makes sense. We sometime update
			 * old records that did not yet have a cmax (e.g. pg_class' own
			 * entry while rewriting it) during rewrites, so allow that.
			 */ /* update mapping */