CREATE OR REPLACE FUNCTION check_code_page(IN src_str TEXT, IN code_page TEXT) RETURNS BOOLEAN
AS $$
from codecs import getencoder

try:
	codec = getencoder(code_page)
	result_str = codec(src_str, r'strict')
	# If the conversion raises no exception, we succeeded.
	result = True
except LookupError:
	# No encoder: fail.
	result = False
except UnicodeError:
	# Bad character: fail.
	result = False

return result
$$ LANGUAGE plpythonu;

