diff options
Diffstat (limited to 'src/backend/replication')
| -rw-r--r-- | src/backend/replication/slot.c | 78 | 
1 files changed, 55 insertions, 23 deletions
| diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index ac188bb2f77..a4ca363f20d 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -260,35 +260,72 @@ ReplicationSlotShmemExit(int code, Datum arg)  /*   * Check whether the passed slot name is valid and report errors at elevel.   * + * See comments for ReplicationSlotValidateNameInternal(). + */ +bool +ReplicationSlotValidateName(const char *name, bool allow_reserved_name, +							int elevel) +{ +	int			err_code; +	char	   *err_msg = NULL; +	char	   *err_hint = NULL; + +	if (!ReplicationSlotValidateNameInternal(name, allow_reserved_name, +											 &err_code, &err_msg, &err_hint)) +	{ +		/* +		 * Use errmsg_internal() and errhint_internal() instead of errmsg() +		 * and errhint(), since the messages from +		 * ReplicationSlotValidateNameInternal() are already translated. This +		 * avoids double translation. +		 */ +		ereport(elevel, +				errcode(err_code), +				errmsg_internal("%s", err_msg), +				(err_hint != NULL) ? errhint_internal("%s", err_hint) : 0); + +		pfree(err_msg); +		if (err_hint != NULL) +			pfree(err_hint); +		return false; +	} + +	return true; +} + +/* + * Check whether the passed slot name is valid. + *   * An error will be reported for a reserved replication slot name if   * allow_reserved_name is set to false.   *   * Slot names may consist out of [a-z0-9_]{1,NAMEDATALEN-1} which should allow   * the name to be used as a directory name on every supported OS.   * - * Returns whether the directory name is valid or not if elevel < ERROR. + * Returns true if the slot name is valid. Otherwise, returns false and stores + * the error code, error message, and optional hint in err_code, err_msg, and + * err_hint, respectively. The caller is responsible for freeing err_msg and + * err_hint, which are palloc'd.   */  bool -ReplicationSlotValidateName(const char *name, bool allow_reserved_name, -							int elevel) +ReplicationSlotValidateNameInternal(const char *name, bool allow_reserved_name, +									int *err_code, char **err_msg, char **err_hint)  {  	const char *cp;  	if (strlen(name) == 0)  	{ -		ereport(elevel, -				(errcode(ERRCODE_INVALID_NAME), -				 errmsg("replication slot name \"%s\" is too short", -						name))); +		*err_code = ERRCODE_INVALID_NAME; +		*err_msg = psprintf(_("replication slot name \"%s\" is too short"), name); +		*err_hint = NULL;  		return false;  	}  	if (strlen(name) >= NAMEDATALEN)  	{ -		ereport(elevel, -				(errcode(ERRCODE_NAME_TOO_LONG), -				 errmsg("replication slot name \"%s\" is too long", -						name))); +		*err_code = ERRCODE_NAME_TOO_LONG; +		*err_msg = psprintf(_("replication slot name \"%s\" is too long"), name); +		*err_hint = NULL;  		return false;  	} @@ -298,24 +335,19 @@ ReplicationSlotValidateName(const char *name, bool allow_reserved_name,  			  || (*cp >= '0' && *cp <= '9')  			  || (*cp == '_')))  		{ -			ereport(elevel, -					(errcode(ERRCODE_INVALID_NAME), -					 errmsg("replication slot name \"%s\" contains invalid character", -							name), -					 errhint("Replication slot names may only contain lower case letters, numbers, and the underscore character."))); +			*err_code = ERRCODE_INVALID_NAME; +			*err_msg = psprintf(_("replication slot name \"%s\" contains invalid character"), name); +			*err_hint = psprintf(_("Replication slot names may only contain lower case letters, numbers, and the underscore character."));  			return false;  		}  	}  	if (!allow_reserved_name && IsSlotForConflictCheck(name))  	{ -		ereport(elevel, -				errcode(ERRCODE_RESERVED_NAME), -				errmsg("replication slot name \"%s\" is reserved", -					   name), -				errdetail("The name \"%s\" is reserved for the conflict detection slot.", -						  CONFLICT_DETECTION_SLOT)); - +		*err_code = ERRCODE_RESERVED_NAME; +		*err_msg = psprintf(_("replication slot name \"%s\" is reserved"), name); +		*err_hint = psprintf(_("The name \"%s\" is reserved for the conflict detection slot."), +							 CONFLICT_DETECTION_SLOT);  		return false;  	} | 
