@@ -1167,6 +1167,14 @@ replorigin_session_setup(RepOriginId node, int acquired_by)
11671167 curstate -> roident , curstate -> acquired_by )));
11681168 }
11691169
1170+ else if (curstate -> acquired_by != acquired_by )
1171+ {
1172+ ereport (ERROR ,
1173+ (errcode (ERRCODE_OBJECT_IN_USE ),
1174+ errmsg ("could not find replication state slot for replication origin with OID %u which was acquired by %d" ,
1175+ node , acquired_by )));
1176+ }
1177+
11701178 /* ok, found slot */
11711179 session_replication_state = curstate ;
11721180 break ;
@@ -1181,6 +1189,13 @@ replorigin_session_setup(RepOriginId node, int acquired_by)
11811189 errhint ("Increase \"max_active_replication_origins\" and try again." )));
11821190 else if (session_replication_state == NULL )
11831191 {
1192+ /* The origin is not used but PID is specified */
1193+ if (acquired_by )
1194+ ereport (ERROR ,
1195+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1196+ errmsg ("replication origin with ID %d is inactive but PID %d was specified" ,
1197+ node , acquired_by )));
1198+
11841199 /* initialize new slot */
11851200 session_replication_state = & replication_states [free_slot ];
11861201 Assert (session_replication_state -> remote_lsn == InvalidXLogRecPtr );
@@ -1193,9 +1208,8 @@ replorigin_session_setup(RepOriginId node, int acquired_by)
11931208
11941209 if (acquired_by == 0 )
11951210 session_replication_state -> acquired_by = MyProcPid ;
1196- else if (session_replication_state -> acquired_by != acquired_by )
1197- elog (ERROR , "could not find replication state slot for replication origin with OID %u which was acquired by %d" ,
1198- node , acquired_by );
1211+ else
1212+ Assert (session_replication_state -> acquired_by == acquired_by );
11991213
12001214 LWLockRelease (ReplicationOriginLock );
12011215
@@ -1374,12 +1388,14 @@ pg_replication_origin_session_setup(PG_FUNCTION_ARGS)
13741388{
13751389 char * name ;
13761390 RepOriginId origin ;
1391+ int pid ;
13771392
13781393 replorigin_check_prerequisites (true, false);
13791394
13801395 name = text_to_cstring ((text * ) DatumGetPointer (PG_GETARG_DATUM (0 )));
13811396 origin = replorigin_by_name (name , false);
1382- replorigin_session_setup (origin , 0 );
1397+ pid = PG_GETARG_INT32 (1 );
1398+ replorigin_session_setup (origin , pid );
13831399
13841400 replorigin_session_origin = origin ;
13851401
0 commit comments