commit 43cc22a (HEAD, pp-squash)
Author:     Noah Misch <noah@leadboat.com>
AuthorDate: Mon Feb 27 02:50:41 2017 -0500
Commit:     Noah Misch <noah@leadboat.com>
CommitDate: Mon Feb 27 02:50:41 2017 -0500

    Use wrappers of PG_DETOAST_DATUM_PACKED() more.
    
    This makes almost all core code follow the policy introduced in the
    previous commit.  Specific decisions:
    
    - Text search support functions with char* and length arguments, such as
      prsstart and lexize, may receive unaligned strings.  I doubt
      maintainers of non-core text search code will notice.
    
    - Use plain VARDATA() on values detoasted or synthesized earlier in the
      same function.  Use VARDATA_ANY() on varlenas sourced outside the
      function, even if they happen to always have four-byte headers.  As an
      exception, retain the universal practice of using VARDATA() on return
      values of SendFunctionCall().
    
    - Retain PG_GETARG_BYTEA_P() in pageinspect.  (Page images are too large
      for a one-byte header, so this misses no optimization.)  Sites that do
      not call get_page_from_raw() typically need the four-byte alignment.
    
    - For now, do not change btree_gist.  Its use of four-byte headers in
      memory is partly entangled with storage of 4-byte headers inside
      GBT_VARKEY, on disk.
    
    - For now, do not change gtrgm_consistent() or gtrgm_distance().  They
      incorporate the varlena header into a cache, and there are multiple
      credible implementation strategies to consider.

diff --git a/contrib/adminpack/adminpack.c b/contrib/adminpack/adminpack.c
index a63ff36..10338f9 100644
--- a/contrib/adminpack/adminpack.c
+++ b/contrib/adminpack/adminpack.c
@@ -124,8 +124,8 @@ pg_file_write(PG_FUNCTION_ARGS)
 
 	requireSuperuser();
 
-	filename = convert_and_check_filename(PG_GETARG_TEXT_P(0), false);
-	data = PG_GETARG_TEXT_P(1);
+	filename = convert_and_check_filename(PG_GETARG_TEXT_PP(0), false);
+	data = PG_GETARG_TEXT_PP(1);
 
 	if (!PG_GETARG_BOOL(2))
 	{
@@ -147,8 +147,8 @@ pg_file_write(PG_FUNCTION_ARGS)
 				 errmsg("could not open file \"%s\" for writing: %m",
 						filename)));
 
-	count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
-	if (count != VARSIZE(data) - VARHDRSZ || FreeFile(f))
+	count = fwrite(VARDATA_ANY(data), 1, VARSIZE_ANY_EXHDR(data), f);
+	if (count != VARSIZE_ANY_EXHDR(data) || FreeFile(f))
 		ereport(ERROR,
 				(errcode_for_file_access(),
 				 errmsg("could not write file \"%s\": %m", filename)));
@@ -170,12 +170,12 @@ pg_file_rename(PG_FUNCTION_ARGS)
 	if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
 		PG_RETURN_NULL();
 
-	fn1 = convert_and_check_filename(PG_GETARG_TEXT_P(0), false);
-	fn2 = convert_and_check_filename(PG_GETARG_TEXT_P(1), false);
+	fn1 = convert_and_check_filename(PG_GETARG_TEXT_PP(0), false);
+	fn2 = convert_and_check_filename(PG_GETARG_TEXT_PP(1), false);
 	if (PG_ARGISNULL(2))
 		fn3 = 0;
 	else
-		fn3 = convert_and_check_filename(PG_GETARG_TEXT_P(2), false);
+		fn3 = convert_and_check_filename(PG_GETARG_TEXT_PP(2), false);
 
 	if (access(fn1, W_OK) < 0)
 	{
@@ -254,7 +254,7 @@ pg_file_unlink(PG_FUNCTION_ARGS)
 
 	requireSuperuser();
 
-	filename = convert_and_check_filename(PG_GETARG_TEXT_P(0), false);
+	filename = convert_and_check_filename(PG_GETARG_TEXT_PP(0), false);
 
 	if (access(filename, W_OK) < 0)
 	{
diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index ac43c45..116cc58 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -1512,7 +1512,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
 		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
 
 		/* open target relation */
-		rel = get_rel_from_relname(PG_GETARG_TEXT_P(0), AccessShareLock, ACL_SELECT);
+		rel = get_rel_from_relname(PG_GETARG_TEXT_PP(0), AccessShareLock, ACL_SELECT);
 
 		/* get the array of attnums */
 		results = get_pkey_attnames(rel, &numatts);
@@ -1613,7 +1613,7 @@ PG_FUNCTION_INFO_V1(dblink_build_sql_insert);
 Datum
 dblink_build_sql_insert(PG_FUNCTION_ARGS)
 {
-	text	   *relname_text = PG_GETARG_TEXT_P(0);
+	text	   *relname_text = PG_GETARG_TEXT_PP(0);
 	int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
 	int32		pknumatts_arg = PG_GETARG_INT32(2);
 	ArrayType  *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
@@ -1704,7 +1704,7 @@ PG_FUNCTION_INFO_V1(dblink_build_sql_delete);
 Datum
 dblink_build_sql_delete(PG_FUNCTION_ARGS)
 {
-	text	   *relname_text = PG_GETARG_TEXT_P(0);
+	text	   *relname_text = PG_GETARG_TEXT_PP(0);
 	int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
 	int32		pknumatts_arg = PG_GETARG_INT32(2);
 	ArrayType  *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
@@ -1781,7 +1781,7 @@ PG_FUNCTION_INFO_V1(dblink_build_sql_update);
 Datum
 dblink_build_sql_update(PG_FUNCTION_ARGS)
 {
-	text	   *relname_text = PG_GETARG_TEXT_P(0);
+	text	   *relname_text = PG_GETARG_TEXT_PP(0);
 	int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
 	int32		pknumatts_arg = PG_GETARG_INT32(2);
 	ArrayType  *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
@@ -2350,7 +2350,7 @@ quote_ident_cstr(char *rawstr)
 	char	   *result;
 
 	rawstr_text = cstring_to_text(rawstr);
-	result_text = DatumGetTextP(DirectFunctionCall1(quote_ident,
+	result_text = DatumGetTextPP(DirectFunctionCall1(quote_ident,
 											  PointerGetDatum(rawstr_text)));
 	result = text_to_cstring(result_text);
 
diff --git a/contrib/fuzzystrmatch/dmetaphone.c b/contrib/fuzzystrmatch/dmetaphone.c
index f210869..4e89983 100644
--- a/contrib/fuzzystrmatch/dmetaphone.c
+++ b/contrib/fuzzystrmatch/dmetaphone.c
@@ -139,7 +139,7 @@ dmetaphone(PG_FUNCTION_ARGS)
 	if (PG_ARGISNULL(0))
 		PG_RETURN_NULL();
 #endif
-	arg = PG_GETARG_TEXT_P(0);
+	arg = PG_GETARG_TEXT_PP(0);
 	aptr = text_to_cstring(arg);
 
 	DoubleMetaphone(aptr, codes);
@@ -168,7 +168,7 @@ dmetaphone_alt(PG_FUNCTION_ARGS)
 	if (PG_ARGISNULL(0))
 		PG_RETURN_NULL();
 #endif
-	arg = PG_GETARG_TEXT_P(0);
+	arg = PG_GETARG_TEXT_PP(0);
 	aptr = text_to_cstring(arg);
 
 	DoubleMetaphone(aptr, codes);
diff --git a/contrib/fuzzystrmatch/fuzzystrmatch.c b/contrib/fuzzystrmatch/fuzzystrmatch.c
index bd36fc5..f1bb7bc 100644
--- a/contrib/fuzzystrmatch/fuzzystrmatch.c
+++ b/contrib/fuzzystrmatch/fuzzystrmatch.c
@@ -736,7 +736,7 @@ soundex(PG_FUNCTION_ARGS)
 	char		outstr[SOUNDEX_LEN + 1];
 	char	   *arg;
 
-	arg = text_to_cstring(PG_GETARG_TEXT_P(0));
+	arg = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
 	_soundex(arg, outstr);
 
@@ -802,8 +802,8 @@ difference(PG_FUNCTION_ARGS)
 	int			i,
 				result;
 
-	_soundex(text_to_cstring(PG_GETARG_TEXT_P(0)), sndx1);
-	_soundex(text_to_cstring(PG_GETARG_TEXT_P(1)), sndx2);
+	_soundex(text_to_cstring(PG_GETARG_TEXT_PP(0)), sndx1);
+	_soundex(text_to_cstring(PG_GETARG_TEXT_PP(1)), sndx2);
 
 	result = 0;
 	for (i = 0; i < SOUNDEX_LEN; i++)
diff --git a/contrib/intarray/_int_op.c b/contrib/intarray/_int_op.c
index 5371741..c30d3c8 100644
--- a/contrib/intarray/_int_op.c
+++ b/contrib/intarray/_int_op.c
@@ -200,9 +200,9 @@ Datum
 sort(PG_FUNCTION_ARGS)
 {
 	ArrayType  *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
-	text	   *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_P(1) : NULL;
-	int32		dc = (dirstr) ? VARSIZE(dirstr) - VARHDRSZ : 0;
-	char	   *d = (dirstr) ? VARDATA(dirstr) : NULL;
+	text	   *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_PP(1) : NULL;
+	int32		dc = (dirstr) ? VARSIZE_ANY_EXHDR(dirstr) : 0;
+	char	   *d = (dirstr) ? VARDATA_ANY(dirstr) : NULL;
 	int			dir = -1;
 
 	CHECKARRVALID(a);
diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index d50ec3a..6f35e28 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -159,7 +159,7 @@ GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
 Datum
 bt_page_stats(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	uint32		blkno = PG_GETARG_UINT32(1);
 	Buffer		buffer;
 	Relation	rel;
@@ -256,7 +256,7 @@ struct user_args
 Datum
 bt_page_items(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	uint32		blkno = PG_GETARG_UINT32(1);
 	Datum		result;
 	char	   *values[6];
@@ -408,7 +408,7 @@ bt_page_items(PG_FUNCTION_ARGS)
 Datum
 bt_metap(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	Datum		result;
 	Relation	rel;
 	RangeVar   *relrv;
diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index 102f360..c3578b7 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -45,7 +45,7 @@ PG_FUNCTION_INFO_V1(get_raw_page);
 Datum
 get_raw_page(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	uint32		blkno = PG_GETARG_UINT32(1);
 	bytea	   *raw_page;
 
@@ -74,8 +74,8 @@ PG_FUNCTION_INFO_V1(get_raw_page_fork);
 Datum
 get_raw_page_fork(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
-	text	   *forkname = PG_GETARG_TEXT_P(1);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
+	text	   *forkname = PG_GETARG_TEXT_PP(1);
 	uint32		blkno = PG_GETARG_UINT32(2);
 	bytea	   *raw_page;
 	ForkNumber	forknum;
@@ -179,7 +179,7 @@ get_page_from_raw(bytea *raw_page)
 	Page		page;
 	int			raw_page_size;
 
-	raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
+	raw_page_size = VARSIZE_ANY_EXHDR(raw_page);
 
 	if (raw_page_size != BLCKSZ)
 		ereport(ERROR,
@@ -190,7 +190,7 @@ get_page_from_raw(bytea *raw_page)
 
 	page = palloc(raw_page_size);
 
-	memcpy(page, VARDATA(raw_page), raw_page_size);
+	memcpy(page, VARDATA_ANY(raw_page), raw_page_size);
 
 	return page;
 }
diff --git a/contrib/pg_prewarm/pg_prewarm.c b/contrib/pg_prewarm/pg_prewarm.c
index 52ea004..78d71ab 100644
--- a/contrib/pg_prewarm/pg_prewarm.c
+++ b/contrib/pg_prewarm/pg_prewarm.c
@@ -79,7 +79,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 (errmsg("prewarm type cannot be null"))));
-	type = PG_GETARG_TEXT_P(1);
+	type = PG_GETARG_TEXT_PP(1);
 	ttype = text_to_cstring(type);
 	if (strcmp(ttype, "prefetch") == 0)
 		ptype = PREWARM_PREFETCH;
@@ -99,7 +99,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 (errmsg("relation fork cannot be null"))));
-	forkName = PG_GETARG_TEXT_P(2);
+	forkName = PG_GETARG_TEXT_PP(2);
 	forkString = text_to_cstring(forkName);
 	forkNumber = forkname_to_number(forkString);
 
diff --git a/contrib/pg_trgm/trgm_gin.c b/contrib/pg_trgm/trgm_gin.c
index ead33ef..e4b3dae 100644
--- a/contrib/pg_trgm/trgm_gin.c
+++ b/contrib/pg_trgm/trgm_gin.c
@@ -35,7 +35,7 @@ gin_extract_trgm(PG_FUNCTION_ARGS)
 Datum
 gin_extract_value_trgm(PG_FUNCTION_ARGS)
 {
-	text	   *val = (text *) PG_GETARG_TEXT_P(0);
+	text	   *val = (text *) PG_GETARG_TEXT_PP(0);
 	int32	   *nentries = (int32 *) PG_GETARG_POINTER(1);
 	Datum	   *entries = NULL;
 	TRGM	   *trg;
@@ -43,7 +43,7 @@ gin_extract_value_trgm(PG_FUNCTION_ARGS)
 
 	*nentries = 0;
 
-	trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
+	trg = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
 	trglen = ARRNELEM(trg);
 
 	if (trglen > 0)
@@ -70,7 +70,7 @@ gin_extract_value_trgm(PG_FUNCTION_ARGS)
 Datum
 gin_extract_query_trgm(PG_FUNCTION_ARGS)
 {
-	text	   *val = (text *) PG_GETARG_TEXT_P(0);
+	text	   *val = (text *) PG_GETARG_TEXT_PP(0);
 	int32	   *nentries = (int32 *) PG_GETARG_POINTER(1);
 	StrategyNumber strategy = PG_GETARG_UINT16(2);
 
@@ -90,7 +90,7 @@ gin_extract_query_trgm(PG_FUNCTION_ARGS)
 	{
 		case SimilarityStrategyNumber:
 		case WordSimilarityStrategyNumber:
-			trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
+			trg = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
 			break;
 		case ILikeStrategyNumber:
 #ifndef IGNORECASE
@@ -103,7 +103,8 @@ gin_extract_query_trgm(PG_FUNCTION_ARGS)
 			 * For wildcard search we extract all the trigrams that every
 			 * potentially-matching string must include.
 			 */
-			trg = generate_wildcard_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
+			trg = generate_wildcard_trgm(VARDATA_ANY(val),
+										 VARSIZE_ANY_EXHDR(val));
 			break;
 		case RegExpICaseStrategyNumber:
 #ifndef IGNORECASE
@@ -170,7 +171,7 @@ gin_trgm_consistent(PG_FUNCTION_ARGS)
 	bool	   *check = (bool *) PG_GETARG_POINTER(0);
 	StrategyNumber strategy = PG_GETARG_UINT16(1);
 
-	/* text    *query = PG_GETARG_TEXT_P(2); */
+	/* text    *query = PG_GETARG_TEXT_PP(2); */
 	int32		nkeys = PG_GETARG_INT32(3);
 	Pointer    *extra_data = (Pointer *) PG_GETARG_POINTER(4);
 	bool	   *recheck = (bool *) PG_GETARG_POINTER(5);
@@ -268,7 +269,7 @@ gin_trgm_triconsistent(PG_FUNCTION_ARGS)
 	GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0);
 	StrategyNumber strategy = PG_GETARG_UINT16(1);
 
-	/* text    *query = PG_GETARG_TEXT_P(2); */
+	/* text    *query = PG_GETARG_TEXT_PP(2); */
 	int32		nkeys = PG_GETARG_INT32(3);
 	Pointer    *extra_data = (Pointer *) PG_GETARG_POINTER(4);
 	GinTernaryValue res = GIN_MAYBE;
diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c
index f52867d..ed02af8 100644
--- a/contrib/pg_trgm/trgm_gist.c
+++ b/contrib/pg_trgm/trgm_gist.c
@@ -100,9 +100,9 @@ gtrgm_compress(PG_FUNCTION_ARGS)
 	if (entry->leafkey)
 	{							/* trgm */
 		TRGM	   *res;
-		text	   *val = DatumGetTextP(entry->key);
+		text	   *val = DatumGetTextPP(entry->key);
 
-		res = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
+		res = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
 		retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
 		gistentryinit(*retval, PointerGetDatum(res),
 					  entry->rel, entry->page,
@@ -142,7 +142,7 @@ gtrgm_decompress(PG_FUNCTION_ARGS)
 	GISTENTRY  *retval;
 	text	   *key;
 
-	key = DatumGetTextP(entry->key);
+	key = DatumGetTextPP(entry->key);
 
 	if (key != (text *) DatumGetPointer(entry->key))
 	{
@@ -200,11 +200,12 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
 	 * depends on strategy.
 	 *
 	 * The cached structure is a single palloc chunk containing the
-	 * gtrgm_consistent_cache header, then the input query (starting at a
-	 * MAXALIGN boundary), then the TRGM value (also starting at a MAXALIGN
-	 * boundary).  However we don't try to include the regex graph (if any) in
-	 * that struct.  (XXX currently, this approach can leak regex graphs
-	 * across index rescans.  Not clear if that's worth fixing.)
+	 * gtrgm_consistent_cache header, then the input query (4-byte length
+	 * word, uncompressed, starting at a MAXALIGN boundary), then the TRGM
+	 * value (also starting at a MAXALIGN boundary).  However we don't try to
+	 * include the regex graph (if any) in that struct.  (XXX currently, this
+	 * approach can leak regex graphs across index rescans.  Not clear if
+	 * that's worth fixing.)
 	 */
 	cache = (gtrgm_consistent_cache *) fcinfo->flinfo->fn_extra;
 	if (cache == NULL ||
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 368e7c8..e9a7131 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -878,14 +878,14 @@ trgm2int(trgm *ptr)
 Datum
 show_trgm(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(0);
+	text	   *in = PG_GETARG_TEXT_PP(0);
 	TRGM	   *trg;
 	Datum	   *d;
 	ArrayType  *a;
 	trgm	   *ptr;
 	int			i;
 
-	trg = generate_trgm(VARDATA(in), VARSIZE(in) - VARHDRSZ);
+	trg = generate_trgm(VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
 	d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
 
 	for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
@@ -1053,14 +1053,14 @@ trgm_presence_map(TRGM *query, TRGM *key)
 Datum
 similarity(PG_FUNCTION_ARGS)
 {
-	text	   *in1 = PG_GETARG_TEXT_P(0);
-	text	   *in2 = PG_GETARG_TEXT_P(1);
+	text	   *in1 = PG_GETARG_TEXT_PP(0);
+	text	   *in2 = PG_GETARG_TEXT_PP(1);
 	TRGM	   *trg1,
 			   *trg2;
 	float4		res;
 
-	trg1 = generate_trgm(VARDATA(in1), VARSIZE(in1) - VARHDRSZ);
-	trg2 = generate_trgm(VARDATA(in2), VARSIZE(in2) - VARHDRSZ);
+	trg1 = generate_trgm(VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1));
+	trg2 = generate_trgm(VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2));
 
 	res = cnt_sml(trg1, trg2, false);
 
diff --git a/contrib/pgcrypto/pgcrypto.c b/contrib/pgcrypto/pgcrypto.c
index d815de3..ccfdc20 100644
--- a/contrib/pgcrypto/pgcrypto.c
+++ b/contrib/pgcrypto/pgcrypto.c
@@ -62,7 +62,7 @@ pg_digest(PG_FUNCTION_ARGS)
 	PX_MD	   *md;
 	bytea	   *res;
 
-	name = PG_GETARG_TEXT_P(1);
+	name = PG_GETARG_TEXT_PP(1);
 
 	/* will give error if fails */
 	md = find_provider(name, (PFN) px_find_digest, "Digest", 0);
@@ -72,10 +72,10 @@ pg_digest(PG_FUNCTION_ARGS)
 	res = (text *) palloc(hlen + VARHDRSZ);
 	SET_VARSIZE(res, hlen + VARHDRSZ);
 
-	arg = PG_GETARG_BYTEA_P(0);
-	len = VARSIZE(arg) - VARHDRSZ;
+	arg = PG_GETARG_BYTEA_PP(0);
+	len = VARSIZE_ANY_EXHDR(arg);
 
-	px_md_update(md, (uint8 *) VARDATA(arg), len);
+	px_md_update(md, (uint8 *) VARDATA_ANY(arg), len);
 	px_md_finish(md, (uint8 *) VARDATA(res));
 	px_md_free(md);
 
@@ -100,7 +100,7 @@ pg_hmac(PG_FUNCTION_ARGS)
 	PX_HMAC    *h;
 	bytea	   *res;
 
-	name = PG_GETARG_TEXT_P(2);
+	name = PG_GETARG_TEXT_PP(2);
 
 	/* will give error if fails */
 	h = find_provider(name, (PFN) px_find_hmac, "HMAC", 0);
@@ -110,13 +110,13 @@ pg_hmac(PG_FUNCTION_ARGS)
 	res = (text *) palloc(hlen + VARHDRSZ);
 	SET_VARSIZE(res, hlen + VARHDRSZ);
 
-	arg = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
-	len = VARSIZE(arg) - VARHDRSZ;
-	klen = VARSIZE(key) - VARHDRSZ;
+	arg = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
+	len = VARSIZE_ANY_EXHDR(arg);
+	klen = VARSIZE_ANY_EXHDR(key);
 
-	px_hmac_init(h, (uint8 *) VARDATA(key), klen);
-	px_hmac_update(h, (uint8 *) VARDATA(arg), len);
+	px_hmac_init(h, (uint8 *) VARDATA_ANY(key), klen);
+	px_hmac_update(h, (uint8 *) VARDATA_ANY(arg), len);
 	px_hmac_finish(h, (uint8 *) VARDATA(res));
 	px_hmac_free(h);
 
@@ -228,20 +228,20 @@ pg_encrypt(PG_FUNCTION_ARGS)
 				klen,
 				rlen;
 
-	type = PG_GETARG_TEXT_P(2);
+	type = PG_GETARG_TEXT_PP(2);
 	c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
-	dlen = VARSIZE(data) - VARHDRSZ;
-	klen = VARSIZE(key) - VARHDRSZ;
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
+	dlen = VARSIZE_ANY_EXHDR(data);
+	klen = VARSIZE_ANY_EXHDR(key);
 
 	rlen = px_combo_encrypt_len(c, dlen);
 	res = palloc(VARHDRSZ + rlen);
 
-	err = px_combo_init(c, (uint8 *) VARDATA(key), klen, NULL, 0);
+	err = px_combo_init(c, (uint8 *) VARDATA_ANY(key), klen, NULL, 0);
 	if (!err)
-		err = px_combo_encrypt(c, (uint8 *) VARDATA(data), dlen,
+		err = px_combo_encrypt(c, (uint8 *) VARDATA_ANY(data), dlen,
 							   (uint8 *) VARDATA(res), &rlen);
 	px_combo_free(c);
 
@@ -277,20 +277,20 @@ pg_decrypt(PG_FUNCTION_ARGS)
 				klen,
 				rlen;
 
-	type = PG_GETARG_TEXT_P(2);
+	type = PG_GETARG_TEXT_PP(2);
 	c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
-	dlen = VARSIZE(data) - VARHDRSZ;
-	klen = VARSIZE(key) - VARHDRSZ;
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
+	dlen = VARSIZE_ANY_EXHDR(data);
+	klen = VARSIZE_ANY_EXHDR(key);
 
 	rlen = px_combo_decrypt_len(c, dlen);
 	res = palloc(VARHDRSZ + rlen);
 
-	err = px_combo_init(c, (uint8 *) VARDATA(key), klen, NULL, 0);
+	err = px_combo_init(c, (uint8 *) VARDATA_ANY(key), klen, NULL, 0);
 	if (!err)
-		err = px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen,
+		err = px_combo_decrypt(c, (uint8 *) VARDATA_ANY(data), dlen,
 							   (uint8 *) VARDATA(res), &rlen);
 
 	px_combo_free(c);
@@ -327,23 +327,23 @@ pg_encrypt_iv(PG_FUNCTION_ARGS)
 				ivlen,
 				rlen;
 
-	type = PG_GETARG_TEXT_P(3);
+	type = PG_GETARG_TEXT_PP(3);
 	c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
-	iv = PG_GETARG_BYTEA_P(2);
-	dlen = VARSIZE(data) - VARHDRSZ;
-	klen = VARSIZE(key) - VARHDRSZ;
-	ivlen = VARSIZE(iv) - VARHDRSZ;
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
+	iv = PG_GETARG_BYTEA_PP(2);
+	dlen = VARSIZE_ANY_EXHDR(data);
+	klen = VARSIZE_ANY_EXHDR(key);
+	ivlen = VARSIZE_ANY_EXHDR(iv);
 
 	rlen = px_combo_encrypt_len(c, dlen);
 	res = palloc(VARHDRSZ + rlen);
 
-	err = px_combo_init(c, (uint8 *) VARDATA(key), klen,
-						(uint8 *) VARDATA(iv), ivlen);
+	err = px_combo_init(c, (uint8 *) VARDATA_ANY(key), klen,
+						(uint8 *) VARDATA_ANY(iv), ivlen);
 	if (!err)
-		err = px_combo_encrypt(c, (uint8 *) VARDATA(data), dlen,
+		err = px_combo_encrypt(c, (uint8 *) VARDATA_ANY(data), dlen,
 							   (uint8 *) VARDATA(res), &rlen);
 
 	px_combo_free(c);
@@ -381,23 +381,23 @@ pg_decrypt_iv(PG_FUNCTION_ARGS)
 				rlen,
 				ivlen;
 
-	type = PG_GETARG_TEXT_P(3);
+	type = PG_GETARG_TEXT_PP(3);
 	c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
-	iv = PG_GETARG_BYTEA_P(2);
-	dlen = VARSIZE(data) - VARHDRSZ;
-	klen = VARSIZE(key) - VARHDRSZ;
-	ivlen = VARSIZE(iv) - VARHDRSZ;
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
+	iv = PG_GETARG_BYTEA_PP(2);
+	dlen = VARSIZE_ANY_EXHDR(data);
+	klen = VARSIZE_ANY_EXHDR(key);
+	ivlen = VARSIZE_ANY_EXHDR(iv);
 
 	rlen = px_combo_decrypt_len(c, dlen);
 	res = palloc(VARHDRSZ + rlen);
 
-	err = px_combo_init(c, (uint8 *) VARDATA(key), klen,
-						(uint8 *) VARDATA(iv), ivlen);
+	err = px_combo_init(c, (uint8 *) VARDATA_ANY(key), klen,
+						(uint8 *) VARDATA_ANY(iv), ivlen);
 	if (!err)
-		err = px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen,
+		err = px_combo_decrypt(c, (uint8 *) VARDATA_ANY(data), dlen,
 							   (uint8 *) VARDATA(res), &rlen);
 
 	px_combo_free(c);
@@ -480,8 +480,8 @@ find_provider(text *name,
 	char	   *buf;
 	int			err;
 
-	buf = downcase_truncate_identifier(VARDATA(name),
-									   VARSIZE(name) - VARHDRSZ,
+	buf = downcase_truncate_identifier(VARDATA_ANY(name),
+									   VARSIZE_ANY_EXHDR(name),
 									   false);
 
 	err = provider_lookup(buf, &res);
diff --git a/contrib/pgcrypto/pgp-pgsql.c b/contrib/pgcrypto/pgp-pgsql.c
index ce16db7..cc5df14 100644
--- a/contrib/pgcrypto/pgp-pgsql.c
+++ b/contrib/pgcrypto/pgp-pgsql.c
@@ -67,9 +67,9 @@ PG_FUNCTION_INFO_V1(pgp_armor_headers);
 static text *
 convert_charset(text *src, int cset_from, int cset_to)
 {
-	int			src_len = VARSIZE(src) - VARHDRSZ;
+	int			src_len = VARSIZE_ANY_EXHDR(src);
 	unsigned char *dst;
-	unsigned char *csrc = (unsigned char *) VARDATA(src);
+	unsigned char *csrc = (unsigned char *) VARDATA_ANY(src);
 	text	   *res;
 
 	dst = pg_do_encoding_conversion(csrc, src_len, cset_from, cset_to);
@@ -109,7 +109,7 @@ string_is_ascii(const char *str)
 static void
 clear_and_pfree(text *p)
 {
-	px_memset(p, 0, VARSIZE(p));
+	px_memset(p, 0, VARSIZE_ANY(p));
 	pfree(p);
 }
 
@@ -356,8 +356,8 @@ parse_args(PGP_Context *ctx, uint8 *args, int arg_len,
 static MBuf *
 create_mbuf_from_vardata(text *data)
 {
-	return mbuf_create_from_data((uint8 *) VARDATA(data),
-								 VARSIZE(data) - VARHDRSZ);
+	return mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
+								 VARSIZE_ANY_EXHDR(data));
 }
 
 static void
@@ -369,8 +369,8 @@ init_work(PGP_Context **ctx_p, int is_text,
 	fill_expect(ex, is_text);
 
 	if (err == 0 && args != NULL)
-		err = parse_args(*ctx_p, (uint8 *) VARDATA(args),
-						 VARSIZE(args) - VARHDRSZ, ex);
+		err = parse_args(*ctx_p, (uint8 *) VARDATA_ANY(args),
+						 VARSIZE_ANY_EXHDR(args), ex);
 
 	if (err)
 		px_THROW_ERROR(err);
@@ -408,7 +408,7 @@ encrypt_internal(int is_pubenc, int is_text,
 	}
 
 	src = create_mbuf_from_vardata(data);
-	dst = mbuf_create(VARSIZE(data) + 128);
+	dst = mbuf_create(VARSIZE_ANY(data) + 128);
 
 	/*
 	 * reserve room for header
@@ -427,8 +427,8 @@ encrypt_internal(int is_pubenc, int is_text,
 		mbuf_free(kbuf);
 	}
 	else
-		err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key),
-							 VARSIZE(key) - VARHDRSZ);
+		err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
+							 VARSIZE_ANY_EXHDR(key));
 
 	/*
 	 * encrypt
@@ -485,9 +485,9 @@ decrypt_internal(int is_pubenc, int need_text, text *data,
 
 	init_work(&ctx, need_text, args, &ex);
 
-	src = mbuf_create_from_data((uint8 *) VARDATA(data),
-								VARSIZE(data) - VARHDRSZ);
-	dst = mbuf_create(VARSIZE(data) + 2048);
+	src = mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
+								VARSIZE_ANY_EXHDR(data));
+	dst = mbuf_create(VARSIZE_ANY(data) + 2048);
 
 	/*
 	 * reserve room for header
@@ -505,16 +505,16 @@ decrypt_internal(int is_pubenc, int need_text, text *data,
 
 		if (keypsw)
 		{
-			psw = (uint8 *) VARDATA(keypsw);
-			psw_len = VARSIZE(keypsw) - VARHDRSZ;
+			psw = (uint8 *) VARDATA_ANY(keypsw);
+			psw_len = VARSIZE_ANY_EXHDR(keypsw);
 		}
 		kbuf = create_mbuf_from_vardata(key);
 		err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1);
 		mbuf_free(kbuf);
 	}
 	else
-		err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key),
-							 VARSIZE(key) - VARHDRSZ);
+		err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
+							 VARSIZE_ANY_EXHDR(key));
 
 	/* decrypt */
 	if (err >= 0)
@@ -571,10 +571,10 @@ pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS)
 	text	   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		arg = PG_GETARG_BYTEA_P(2);
+		arg = PG_GETARG_BYTEA_PP(2);
 
 	res = encrypt_internal(0, 0, data, key, arg);
 
@@ -593,10 +593,10 @@ pgp_sym_encrypt_text(PG_FUNCTION_ARGS)
 	text	   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		arg = PG_GETARG_BYTEA_P(2);
+		arg = PG_GETARG_BYTEA_PP(2);
 
 	res = encrypt_internal(0, 1, data, key, arg);
 
@@ -616,10 +616,10 @@ pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS)
 	text	   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		arg = PG_GETARG_BYTEA_P(2);
+		arg = PG_GETARG_BYTEA_PP(2);
 
 	res = decrypt_internal(0, 0, data, key, NULL, arg);
 
@@ -638,10 +638,10 @@ pgp_sym_decrypt_text(PG_FUNCTION_ARGS)
 	text	   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		arg = PG_GETARG_BYTEA_P(2);
+		arg = PG_GETARG_BYTEA_PP(2);
 
 	res = decrypt_internal(0, 1, data, key, NULL, arg);
 
@@ -664,10 +664,10 @@ pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS)
 	text	   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		arg = PG_GETARG_BYTEA_P(2);
+		arg = PG_GETARG_BYTEA_PP(2);
 
 	res = encrypt_internal(1, 0, data, key, arg);
 
@@ -686,10 +686,10 @@ pgp_pub_encrypt_text(PG_FUNCTION_ARGS)
 	text	   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		arg = PG_GETARG_BYTEA_P(2);
+		arg = PG_GETARG_BYTEA_PP(2);
 
 	res = encrypt_internal(1, 1, data, key, arg);
 
@@ -710,12 +710,12 @@ pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS)
 			   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		psw = PG_GETARG_BYTEA_P(2);
+		psw = PG_GETARG_BYTEA_PP(2);
 	if (PG_NARGS() > 3)
-		arg = PG_GETARG_BYTEA_P(3);
+		arg = PG_GETARG_BYTEA_PP(3);
 
 	res = decrypt_internal(1, 0, data, key, psw, arg);
 
@@ -737,12 +737,12 @@ pgp_pub_decrypt_text(PG_FUNCTION_ARGS)
 			   *arg = NULL;
 	text	   *res;
 
-	data = PG_GETARG_BYTEA_P(0);
-	key = PG_GETARG_BYTEA_P(1);
+	data = PG_GETARG_BYTEA_PP(0);
+	key = PG_GETARG_BYTEA_PP(1);
 	if (PG_NARGS() > 2)
-		psw = PG_GETARG_BYTEA_P(2);
+		psw = PG_GETARG_BYTEA_PP(2);
 	if (PG_NARGS() > 3)
-		arg = PG_GETARG_BYTEA_P(3);
+		arg = PG_GETARG_BYTEA_PP(3);
 
 	res = decrypt_internal(1, 1, data, key, psw, arg);
 
@@ -865,8 +865,8 @@ pg_armor(PG_FUNCTION_ARGS)
 	char	  **keys = NULL,
 			  **values = NULL;
 
-	data = PG_GETARG_BYTEA_P(0);
-	data_len = VARSIZE(data) - VARHDRSZ;
+	data = PG_GETARG_BYTEA_PP(0);
+	data_len = VARSIZE_ANY_EXHDR(data);
 	if (PG_NARGS() == 3)
 	{
 		num_headers = parse_key_value_arrays(PG_GETARG_ARRAYTYPE_P(1),
@@ -880,7 +880,7 @@ pg_armor(PG_FUNCTION_ARGS)
 
 	initStringInfo(&buf);
 
-	pgp_armor_encode((uint8 *) VARDATA(data), data_len, &buf,
+	pgp_armor_encode((uint8 *) VARDATA_ANY(data), data_len, &buf,
 					 num_headers, keys, values);
 
 	res = palloc(VARHDRSZ + buf.len);
@@ -901,12 +901,12 @@ pg_dearmor(PG_FUNCTION_ARGS)
 	int			ret;
 	StringInfoData buf;
 
-	data = PG_GETARG_TEXT_P(0);
-	data_len = VARSIZE(data) - VARHDRSZ;
+	data = PG_GETARG_TEXT_PP(0);
+	data_len = VARSIZE_ANY_EXHDR(data);
 
 	initStringInfo(&buf);
 
-	ret = pgp_armor_decode((uint8 *) VARDATA(data), data_len, &buf);
+	ret = pgp_armor_decode((uint8 *) VARDATA_ANY(data), data_len, &buf);
 	if (ret < 0)
 		px_THROW_ERROR(ret);
 	res = palloc(VARHDRSZ + buf.len);
@@ -1004,7 +1004,7 @@ pgp_key_id_w(PG_FUNCTION_ARGS)
 	int			res_len;
 	MBuf	   *buf;
 
-	data = PG_GETARG_BYTEA_P(0);
+	data = PG_GETARG_BYTEA_PP(0);
 	buf = create_mbuf_from_vardata(data);
 	res = palloc(VARHDRSZ + 17);
 
diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c
index 4d49610..db9e034 100644
--- a/contrib/pgrowlocks/pgrowlocks.c
+++ b/contrib/pgrowlocks/pgrowlocks.c
@@ -94,7 +94,7 @@ pgrowlocks(PG_FUNCTION_ARGS)
 		attinmeta = TupleDescGetAttInMetadata(tupdesc);
 		funcctx->attinmeta = attinmeta;
 
-		relname = PG_GETARG_TEXT_P(0);
+		relname = PG_GETARG_TEXT_PP(0);
 		relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
 		rel = heap_openrv(relrv, AccessShareLock);
 
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index 17a53e3..3cdcf1c 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -143,7 +143,7 @@ static void GetHashPageStats(Page page, HashIndexStat *stats);
 Datum
 pgstatindex(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	Relation	rel;
 	RangeVar   *relrv;
 
@@ -168,7 +168,7 @@ pgstatindex(PG_FUNCTION_ARGS)
 Datum
 pgstatindex_v1_5(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	Relation	rel;
 	RangeVar   *relrv;
 
@@ -375,7 +375,7 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
 Datum
 pg_relpages(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	int64		relpages;
 	Relation	rel;
 	RangeVar   *relrv;
@@ -401,7 +401,7 @@ pg_relpages(PG_FUNCTION_ARGS)
 Datum
 pg_relpages_v1_5(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	int64		relpages;
 	Relation	rel;
 	RangeVar   *relrv;
diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c
index 06a1992..6176106 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -165,7 +165,7 @@ build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
 Datum
 pgstattuple(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	RangeVar   *relrv;
 	Relation	rel;
 
@@ -191,7 +191,7 @@ pgstattuple(PG_FUNCTION_ARGS)
 Datum
 pgstattuple_v1_5(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	RangeVar   *relrv;
 	Relation	rel;
 
diff --git a/contrib/sepgsql/label.c b/contrib/sepgsql/label.c
index 82deb93..1a8f884 100644
--- a/contrib/sepgsql/label.c
+++ b/contrib/sepgsql/label.c
@@ -590,7 +590,7 @@ PG_FUNCTION_INFO_V1(sepgsql_mcstrans_in);
 Datum
 sepgsql_mcstrans_in(PG_FUNCTION_ARGS)
 {
-	text	   *label = PG_GETARG_TEXT_P(0);
+	text	   *label = PG_GETARG_TEXT_PP(0);
 	char	   *raw_label;
 	char	   *result;
 
@@ -630,7 +630,7 @@ PG_FUNCTION_INFO_V1(sepgsql_mcstrans_out);
 Datum
 sepgsql_mcstrans_out(PG_FUNCTION_ARGS)
 {
-	text	   *label = PG_GETARG_TEXT_P(0);
+	text	   *label = PG_GETARG_TEXT_PP(0);
 	char	   *qual_label;
 	char	   *result;
 
diff --git a/contrib/spi/autoinc.c b/contrib/spi/autoinc.c
index 54f85a3..8bf7422 100644
--- a/contrib/spi/autoinc.c
+++ b/contrib/spi/autoinc.c
@@ -106,7 +106,7 @@ autoinc(PG_FUNCTION_ARGS)
 			newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
 		}
 		newnulls[chnattrs] = false;
-		pfree(DatumGetTextP(seqname));
+		pfree(DatumGetTextPP(seqname));
 		chnattrs++;
 		i++;
 	}
diff --git a/contrib/sslinfo/sslinfo.c b/contrib/sslinfo/sslinfo.c
index a4b0f9b..4284643 100644
--- a/contrib/sslinfo/sslinfo.c
+++ b/contrib/sslinfo/sslinfo.c
@@ -225,7 +225,7 @@ PG_FUNCTION_INFO_V1(ssl_client_dn_field);
 Datum
 ssl_client_dn_field(PG_FUNCTION_ARGS)
 {
-	text	   *fieldname = PG_GETARG_TEXT_P(0);
+	text	   *fieldname = PG_GETARG_TEXT_PP(0);
 	Datum		result;
 
 	if (!(MyProcPort->peer))
@@ -260,7 +260,7 @@ PG_FUNCTION_INFO_V1(ssl_issuer_field);
 Datum
 ssl_issuer_field(PG_FUNCTION_ARGS)
 {
-	text	   *fieldname = PG_GETARG_TEXT_P(0);
+	text	   *fieldname = PG_GETARG_TEXT_PP(0);
 	Datum		result;
 
 	if (!(MyProcPort->peer))
diff --git a/contrib/unaccent/unaccent.c b/contrib/unaccent/unaccent.c
index 542e726..6a34cfd 100644
--- a/contrib/unaccent/unaccent.c
+++ b/contrib/unaccent/unaccent.c
@@ -384,14 +384,14 @@ unaccent_dict(PG_FUNCTION_ARGS)
 		dictOid = PG_GETARG_OID(0);
 		strArg = 1;
 	}
-	str = PG_GETARG_TEXT_P(strArg);
+	str = PG_GETARG_TEXT_PP(strArg);
 
 	dict = lookup_ts_dictionary_cache(dictOid);
 
 	res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(dict->lexize),
 											 PointerGetDatum(dict->dictData),
-											   PointerGetDatum(VARDATA(str)),
-									  Int32GetDatum(VARSIZE(str) - VARHDRSZ),
+										   PointerGetDatum(VARDATA_ANY(str)),
+									   Int32GetDatum(VARSIZE_ANY_EXHDR(str)),
 													 PointerGetDatum(NULL)));
 
 	PG_FREE_IF_COPY(str, strArg);
diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c
index 57bdf4d..1ce0855 100644
--- a/contrib/uuid-ossp/uuid-ossp.c
+++ b/contrib/uuid-ossp/uuid-ossp.c
@@ -503,13 +503,13 @@ Datum
 uuid_generate_v3(PG_FUNCTION_ARGS)
 {
 	pg_uuid_t  *ns = PG_GETARG_UUID_P(0);
-	text	   *name = PG_GETARG_TEXT_P(1);
+	text	   *name = PG_GETARG_TEXT_PP(1);
 
 #ifdef HAVE_UUID_OSSP
 	return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
 #else
 	return uuid_generate_internal(UUID_MAKE_V3, (unsigned char *) ns,
-								  VARDATA(name), VARSIZE(name) - VARHDRSZ);
+								  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
 #endif
 }
 
@@ -525,12 +525,12 @@ Datum
 uuid_generate_v5(PG_FUNCTION_ARGS)
 {
 	pg_uuid_t  *ns = PG_GETARG_UUID_P(0);
-	text	   *name = PG_GETARG_TEXT_P(1);
+	text	   *name = PG_GETARG_TEXT_PP(1);
 
 #ifdef HAVE_UUID_OSSP
 	return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
 #else
 	return uuid_generate_internal(UUID_MAKE_V5, (unsigned char *) ns,
-								  VARDATA(name), VARSIZE(name) - VARHDRSZ);
+								  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
 #endif
 }
diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c
index 73b74c8..034545c 100644
--- a/contrib/xml2/xpath.c
+++ b/contrib/xml2/xpath.c
@@ -95,9 +95,9 @@ PG_FUNCTION_INFO_V1(xml_is_well_formed);
 Datum
 xml_is_well_formed(PG_FUNCTION_ARGS)
 {
-	text	   *t = PG_GETARG_TEXT_P(0);		/* document buffer */
+	text	   *t = PG_GETARG_TEXT_PP(0);		/* document buffer */
 	bool		result = false;
-	int32		docsize = VARSIZE(t) - VARHDRSZ;
+	int32		docsize = VARSIZE_ANY_EXHDR(t);
 	xmlDocPtr	doctree;
 	PgXmlErrorContext *xmlerrcxt;
 
@@ -105,7 +105,7 @@ xml_is_well_formed(PG_FUNCTION_ARGS)
 
 	PG_TRY();
 	{
-		doctree = xmlParseMemory((char *) VARDATA(t), docsize);
+		doctree = xmlParseMemory((char *) VARDATA_ANY(t), docsize);
 
 		result = (doctree != NULL);
 
@@ -133,7 +133,7 @@ PG_FUNCTION_INFO_V1(xml_encode_special_chars);
 Datum
 xml_encode_special_chars(PG_FUNCTION_ARGS)
 {
-	text	   *tin = PG_GETARG_TEXT_P(0);
+	text	   *tin = PG_GETARG_TEXT_PP(0);
 	text	   *tout;
 	xmlChar    *ts,
 			   *tt;
@@ -248,10 +248,10 @@ PG_FUNCTION_INFO_V1(xpath_nodeset);
 Datum
 xpath_nodeset(PG_FUNCTION_ARGS)
 {
-	text	   *document = PG_GETARG_TEXT_P(0);
-	text	   *xpathsupp = PG_GETARG_TEXT_P(1);		/* XPath expression */
-	xmlChar    *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_P(2));
-	xmlChar    *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_P(3));
+	text	   *document = PG_GETARG_TEXT_PP(0);
+	text	   *xpathsupp = PG_GETARG_TEXT_PP(1);		/* XPath expression */
+	xmlChar    *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
+	xmlChar    *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3));
 	xmlChar    *xpath;
 	text	   *xpres;
 	xmlXPathObjectPtr res;
@@ -281,9 +281,9 @@ PG_FUNCTION_INFO_V1(xpath_list);
 Datum
 xpath_list(PG_FUNCTION_ARGS)
 {
-	text	   *document = PG_GETARG_TEXT_P(0);
-	text	   *xpathsupp = PG_GETARG_TEXT_P(1);		/* XPath expression */
-	xmlChar    *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_P(2));
+	text	   *document = PG_GETARG_TEXT_PP(0);
+	text	   *xpathsupp = PG_GETARG_TEXT_PP(1);		/* XPath expression */
+	xmlChar    *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
 	xmlChar    *xpath;
 	text	   *xpres;
 	xmlXPathObjectPtr res;
@@ -310,15 +310,15 @@ PG_FUNCTION_INFO_V1(xpath_string);
 Datum
 xpath_string(PG_FUNCTION_ARGS)
 {
-	text	   *document = PG_GETARG_TEXT_P(0);
-	text	   *xpathsupp = PG_GETARG_TEXT_P(1);		/* XPath expression */
+	text	   *document = PG_GETARG_TEXT_PP(0);
+	text	   *xpathsupp = PG_GETARG_TEXT_PP(1);		/* XPath expression */
 	xmlChar    *xpath;
 	int32		pathsize;
 	text	   *xpres;
 	xmlXPathObjectPtr res;
 	xpath_workspace workspace;
 
-	pathsize = VARSIZE(xpathsupp) - VARHDRSZ;
+	pathsize = VARSIZE_ANY_EXHDR(xpathsupp);
 
 	/*
 	 * We encapsulate the supplied path with "string()" = 8 chars + 1 for NUL
@@ -328,7 +328,7 @@ xpath_string(PG_FUNCTION_ARGS)
 
 	xpath = (xmlChar *) palloc(pathsize + 9);
 	memcpy((char *) xpath, "string(", 7);
-	memcpy((char *) (xpath + 7), VARDATA(xpathsupp), pathsize);
+	memcpy((char *) (xpath + 7), VARDATA_ANY(xpathsupp), pathsize);
 	xpath[pathsize + 7] = ')';
 	xpath[pathsize + 8] = '\0';
 
@@ -351,8 +351,8 @@ PG_FUNCTION_INFO_V1(xpath_number);
 Datum
 xpath_number(PG_FUNCTION_ARGS)
 {
-	text	   *document = PG_GETARG_TEXT_P(0);
-	text	   *xpathsupp = PG_GETARG_TEXT_P(1);		/* XPath expression */
+	text	   *document = PG_GETARG_TEXT_PP(0);
+	text	   *xpathsupp = PG_GETARG_TEXT_PP(1);		/* XPath expression */
 	xmlChar    *xpath;
 	float4		fRes;
 	xmlXPathObjectPtr res;
@@ -383,8 +383,8 @@ PG_FUNCTION_INFO_V1(xpath_bool);
 Datum
 xpath_bool(PG_FUNCTION_ARGS)
 {
-	text	   *document = PG_GETARG_TEXT_P(0);
-	text	   *xpathsupp = PG_GETARG_TEXT_P(1);		/* XPath expression */
+	text	   *document = PG_GETARG_TEXT_PP(0);
+	text	   *xpathsupp = PG_GETARG_TEXT_PP(1);		/* XPath expression */
 	xmlChar    *xpath;
 	int			bRes;
 	xmlXPathObjectPtr res;
@@ -413,7 +413,7 @@ xpath_bool(PG_FUNCTION_ARGS)
 static xmlXPathObjectPtr
 pgxml_xpath(text *document, xmlChar *xpath, xpath_workspace *workspace)
 {
-	int32		docsize = VARSIZE(document) - VARHDRSZ;
+	int32		docsize = VARSIZE_ANY_EXHDR(document);
 	PgXmlErrorContext *xmlerrcxt;
 	xmlXPathCompExprPtr comppath;
 
@@ -425,7 +425,7 @@ pgxml_xpath(text *document, xmlChar *xpath, xpath_workspace *workspace)
 
 	PG_TRY();
 	{
-		workspace->doctree = xmlParseMemory((char *) VARDATA(document),
+		workspace->doctree = xmlParseMemory((char *) VARDATA_ANY(document),
 											docsize);
 		if (workspace->doctree != NULL)
 		{
diff --git a/contrib/xml2/xslt_proc.c b/contrib/xml2/xslt_proc.c
index 343924e..391e6b5 100644
--- a/contrib/xml2/xslt_proc.c
+++ b/contrib/xml2/xslt_proc.c
@@ -49,8 +49,8 @@ xslt_process(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXSLT
 
-	text	   *doct = PG_GETARG_TEXT_P(0);
-	text	   *ssheet = PG_GETARG_TEXT_P(1);
+	text	   *doct = PG_GETARG_TEXT_PP(0);
+	text	   *ssheet = PG_GETARG_TEXT_PP(1);
 	text	   *result;
 	text	   *paramstr;
 	const char **params;
@@ -66,7 +66,7 @@ xslt_process(PG_FUNCTION_ARGS)
 
 	if (fcinfo->nargs == 3)
 	{
-		paramstr = PG_GETARG_TEXT_P(2);
+		paramstr = PG_GETARG_TEXT_PP(2);
 		params = parse_params(paramstr);
 	}
 	else
@@ -85,16 +85,16 @@ xslt_process(PG_FUNCTION_ARGS)
 		bool		xslt_sec_prefs_error;
 
 		/* Parse document */
-		doctree = xmlParseMemory((char *) VARDATA(doct),
-								 VARSIZE(doct) - VARHDRSZ);
+		doctree = xmlParseMemory((char *) VARDATA_ANY(doct),
+								 VARSIZE_ANY_EXHDR(doct));
 
 		if (doctree == NULL)
 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
 						"error parsing XML document");
 
 		/* Same for stylesheet */
-		ssdoc = xmlParseMemory((char *) VARDATA(ssheet),
-							   VARSIZE(ssheet) - VARHDRSZ);
+		ssdoc = xmlParseMemory((char *) VARDATA_ANY(ssheet),
+							   VARSIZE_ANY_EXHDR(ssheet));
 
 		if (ssdoc == NULL)
 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index 8678854..53f298b 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -568,8 +568,9 @@ spg_text_leaf_consistent(PG_FUNCTION_ARGS)
 
 	leafValue = DatumGetTextPP(in->leafDatum);
 
+	/* As above, in->reconstructedValue isn't toasted or short. */
 	if (DatumGetPointer(in->reconstructedValue))
-		reconstrValue = DatumGetTextP(in->reconstructedValue);
+		reconstrValue = (text *) DatumGetPointer(in->reconstructedValue);
 
 	Assert(reconstrValue == NULL ? level == 0 :
 		   VARSIZE_ANY_EXHDR(reconstrValue) == level);
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index 27c0c56..96aa15e 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -72,7 +72,7 @@ nonexclusive_base_backup_cleanup(int code, Datum arg)
 Datum
 pg_start_backup(PG_FUNCTION_ARGS)
 {
-	text	   *backupid = PG_GETARG_TEXT_P(0);
+	text	   *backupid = PG_GETARG_TEXT_PP(0);
 	bool		fast = PG_GETARG_BOOL(1);
 	bool		exclusive = PG_GETARG_BOOL(2);
 	char	   *backupidstr;
@@ -309,7 +309,7 @@ pg_switch_wal(PG_FUNCTION_ARGS)
 Datum
 pg_create_restore_point(PG_FUNCTION_ARGS)
 {
-	text	   *restore_name = PG_GETARG_TEXT_P(0);
+	text	   *restore_name = PG_GETARG_TEXT_PP(0);
 	char	   *restore_name_str;
 	XLogRecPtr	restorepoint;
 
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 325f5b7..8bc006e 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -2317,7 +2317,7 @@ Datum
 pg_extension_config_dump(PG_FUNCTION_ARGS)
 {
 	Oid			tableoid = PG_GETARG_OID(0);
-	text	   *wherecond = PG_GETARG_TEXT_P(1);
+	text	   *wherecond = PG_GETARG_TEXT_PP(1);
 	char	   *tablename;
 	Relation	extRel;
 	ScanKeyData key[1];
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index e0df642..9fc6233 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -538,7 +538,7 @@ DeleteSequenceTuple(Oid relid)
 Datum
 nextval(PG_FUNCTION_ARGS)
 {
-	text	   *seqin = PG_GETARG_TEXT_P(0);
+	text	   *seqin = PG_GETARG_TEXT_PP(0);
 	RangeVar   *sequence;
 	Oid			relid;
 
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index d80bff6..a1bb3e9 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -1673,13 +1673,13 @@ RelationBuildTriggers(Relation relation)
 			bytea	   *val;
 			char	   *p;
 
-			val = DatumGetByteaP(fastgetattr(htup,
-											 Anum_pg_trigger_tgargs,
-											 tgrel->rd_att, &isnull));
+			val = DatumGetByteaPP(fastgetattr(htup,
+											  Anum_pg_trigger_tgargs,
+											  tgrel->rd_att, &isnull));
 			if (isnull)
 				elog(ERROR, "tgargs is null in trigger for relation \"%s\"",
 					 RelationGetRelationName(relation));
-			p = (char *) VARDATA(val);
+			p = (char *) VARDATA_ANY(val);
 			build->tgargs = (char **) palloc(build->tgnargs * sizeof(char *));
 			for (i = 0; i < build->tgnargs; i++)
 			{
diff --git a/src/backend/commands/tsearchcmds.c b/src/backend/commands/tsearchcmds.c
index 49668be..b58d60c 100644
--- a/src/backend/commands/tsearchcmds.c
+++ b/src/backend/commands/tsearchcmds.c
@@ -1561,9 +1561,9 @@ serialize_deflist(List *deflist)
 List *
 deserialize_deflist(Datum txt)
 {
-	text	   *in = DatumGetTextP(txt);		/* in case it's toasted */
+	text	   *in = DatumGetTextPP(txt);		/* in case it's toasted */
 	List	   *result = NIL;
-	int			len = VARSIZE(in) - VARHDRSZ;
+	int			len = VARSIZE_ANY_EXHDR(in);
 	char	   *ptr,
 			   *endptr,
 			   *workspace,
@@ -1583,7 +1583,7 @@ deserialize_deflist(Datum txt)
 	ds_state	state = CS_WAITKEY;
 
 	workspace = (char *) palloc(len + 1);		/* certainly enough room */
-	ptr = VARDATA(in);
+	ptr = VARDATA_ANY(in);
 	endptr = ptr + len;
 	for (; ptr < endptr; ptr++)
 	{
diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c
index 4566219..f0b94fa 100644
--- a/src/backend/executor/execQual.c
+++ b/src/backend/executor/execQual.c
@@ -3439,7 +3439,7 @@ ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
 				value = ExecEvalExpr(e, econtext, &isnull);
 				if (isnull)
 					return (Datum) 0;
-				data = DatumGetTextP(value);
+				data = DatumGetTextPP(value);
 
 				e = (ExprState *) lsecond(xmlExpr->args);
 				value = ExecEvalExpr(e, econtext, &isnull);
@@ -3470,7 +3470,7 @@ ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
 					if (isnull)
 						arg = NULL;
 					else
-						arg = DatumGetTextP(value);
+						arg = DatumGetTextPP(value);
 				}
 				else
 				{
@@ -3503,7 +3503,7 @@ ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
 				if (isnull)
 					version = NULL;
 				else
-					version = DatumGetTextP(value);
+					version = DatumGetTextPP(value);
 
 				e = (ExprState *) lthird(xmlExpr->args);
 				value = ExecEvalExpr(e, econtext, &isnull);
diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c
index f537aff..2cb6039 100644
--- a/src/backend/libpq/be-fsstubs.c
+++ b/src/backend/libpq/be-fsstubs.c
@@ -399,12 +399,12 @@ Datum
 be_lowrite(PG_FUNCTION_ARGS)
 {
 	int32		fd = PG_GETARG_INT32(0);
-	bytea	   *wbuf = PG_GETARG_BYTEA_P(1);
+	bytea	   *wbuf = PG_GETARG_BYTEA_PP(1);
 	int			bytestowrite;
 	int			totalwritten;
 
-	bytestowrite = VARSIZE(wbuf) - VARHDRSZ;
-	totalwritten = lo_write(fd, VARDATA(wbuf), bytestowrite);
+	bytestowrite = VARSIZE_ANY_EXHDR(wbuf);
+	totalwritten = lo_write(fd, VARDATA_ANY(wbuf), bytestowrite);
 	PG_RETURN_INT32(totalwritten);
 }
 
diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c
index bf84c68..5eaf863 100644
--- a/src/backend/replication/logical/origin.c
+++ b/src/backend/replication/logical/origin.c
@@ -1307,7 +1307,7 @@ pg_replication_origin_xact_reset(PG_FUNCTION_ARGS)
 Datum
 pg_replication_origin_advance(PG_FUNCTION_ARGS)
 {
-	text	   *name = PG_GETARG_TEXT_P(0);
+	text	   *name = PG_GETARG_TEXT_PP(0);
 	XLogRecPtr	remote_commit = PG_GETARG_LSN(1);
 	RepOriginId node;
 
diff --git a/src/backend/tsearch/dict.c b/src/backend/tsearch/dict.c
index 87ee8bf..ba8a3d7 100644
--- a/src/backend/tsearch/dict.c
+++ b/src/backend/tsearch/dict.c
@@ -26,7 +26,7 @@ Datum
 ts_lexize(PG_FUNCTION_ARGS)
 {
 	Oid			dictId = PG_GETARG_OID(0);
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	ArrayType  *a;
 	TSDictionaryCacheEntry *dict;
 	TSLexeme   *res,
@@ -38,8 +38,8 @@ ts_lexize(PG_FUNCTION_ARGS)
 
 	res = (TSLexeme *) DatumGetPointer(FunctionCall4(&dict->lexize,
 											 PointerGetDatum(dict->dictData),
-												PointerGetDatum(VARDATA(in)),
-									   Int32GetDatum(VARSIZE(in) - VARHDRSZ),
+											PointerGetDatum(VARDATA_ANY(in)),
+										Int32GetDatum(VARSIZE_ANY_EXHDR(in)),
 												  PointerGetDatum(&dstate)));
 
 	if (dstate.getnext)
@@ -47,8 +47,8 @@ ts_lexize(PG_FUNCTION_ARGS)
 		dstate.isend = true;
 		ptr = (TSLexeme *) DatumGetPointer(FunctionCall4(&dict->lexize,
 											 PointerGetDatum(dict->dictData),
-												PointerGetDatum(VARDATA(in)),
-									   Int32GetDatum(VARSIZE(in) - VARHDRSZ),
+											PointerGetDatum(VARDATA_ANY(in)),
+										Int32GetDatum(VARSIZE_ANY_EXHDR(in)),
 												  PointerGetDatum(&dstate)));
 		if (ptr != NULL)
 			res = ptr;
diff --git a/src/backend/tsearch/to_tsany.c b/src/backend/tsearch/to_tsany.c
index 6e5de8f..398a781 100644
--- a/src/backend/tsearch/to_tsany.c
+++ b/src/backend/tsearch/to_tsany.c
@@ -216,19 +216,19 @@ Datum
 to_tsvector_byid(PG_FUNCTION_ARGS)
 {
 	Oid			cfgId = PG_GETARG_OID(0);
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	ParsedText	prs;
 	TSVector	out;
 
-	prs.lenwords = (VARSIZE(in) - VARHDRSZ) / 6;		/* just estimation of
-														 * word's number */
+	prs.lenwords = VARSIZE_ANY_EXHDR(in) / 6;	/* just estimation of word's
+												 * number */
 	if (prs.lenwords == 0)
 		prs.lenwords = 2;
 	prs.curwords = 0;
 	prs.pos = 0;
 	prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
 
-	parsetext(cfgId, &prs, VARDATA(in), VARSIZE(in) - VARHDRSZ);
+	parsetext(cfgId, &prs, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
 	PG_FREE_IF_COPY(in, 1);
 
 	if (prs.curwords)
@@ -247,7 +247,7 @@ to_tsvector_byid(PG_FUNCTION_ARGS)
 Datum
 to_tsvector(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(0);
+	text	   *in = PG_GETARG_TEXT_PP(0);
 	Oid			cfgId;
 
 	cfgId = getTSCurrentConfig(true);
@@ -362,7 +362,7 @@ pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval,
 Datum
 to_tsquery_byid(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	TSQuery		query;
 	MorphOpaque data;
 
@@ -380,7 +380,7 @@ to_tsquery_byid(PG_FUNCTION_ARGS)
 Datum
 to_tsquery(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(0);
+	text	   *in = PG_GETARG_TEXT_PP(0);
 	Oid			cfgId;
 
 	cfgId = getTSCurrentConfig(true);
@@ -392,7 +392,7 @@ to_tsquery(PG_FUNCTION_ARGS)
 Datum
 plainto_tsquery_byid(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	TSQuery		query;
 	MorphOpaque data;
 
@@ -410,7 +410,7 @@ plainto_tsquery_byid(PG_FUNCTION_ARGS)
 Datum
 plainto_tsquery(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(0);
+	text	   *in = PG_GETARG_TEXT_PP(0);
 	Oid			cfgId;
 
 	cfgId = getTSCurrentConfig(true);
@@ -423,7 +423,7 @@ plainto_tsquery(PG_FUNCTION_ARGS)
 Datum
 phraseto_tsquery_byid(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	TSQuery		query;
 	MorphOpaque data;
 
@@ -441,7 +441,7 @@ phraseto_tsquery_byid(PG_FUNCTION_ARGS)
 Datum
 phraseto_tsquery(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(0);
+	text	   *in = PG_GETARG_TEXT_PP(0);
 	Oid			cfgId;
 
 	cfgId = getTSCurrentConfig(true);
diff --git a/src/backend/tsearch/wparser.c b/src/backend/tsearch/wparser.c
index 8ca1c62..d8f2f65 100644
--- a/src/backend/tsearch/wparser.c
+++ b/src/backend/tsearch/wparser.c
@@ -123,7 +123,7 @@ ts_token_type_byname(PG_FUNCTION_ARGS)
 
 	if (SRF_IS_FIRSTCALL())
 	{
-		text	   *prsname = PG_GETARG_TEXT_P(0);
+		text	   *prsname = PG_GETARG_TEXT_PP(0);
 		Oid			prsId;
 
 		funcctx = SRF_FIRSTCALL_INIT();
@@ -172,8 +172,8 @@ prs_setup_firstcall(FuncCallContext *funcctx, Oid prsid, text *txt)
 	st->list = (LexemeEntry *) palloc(sizeof(LexemeEntry) * st->len);
 
 	prsdata = (void *) DatumGetPointer(FunctionCall2(&prs->prsstart,
-											   PointerGetDatum(VARDATA(txt)),
-									Int32GetDatum(VARSIZE(txt) - VARHDRSZ)));
+										   PointerGetDatum(VARDATA_ANY(txt)),
+									 Int32GetDatum(VARSIZE_ANY_EXHDR(txt))));
 
 	while ((type = DatumGetInt32(FunctionCall3(&prs->prstoken,
 											   PointerGetDatum(prsdata),
@@ -248,7 +248,7 @@ ts_parse_byid(PG_FUNCTION_ARGS)
 
 	if (SRF_IS_FIRSTCALL())
 	{
-		text	   *txt = PG_GETARG_TEXT_P(1);
+		text	   *txt = PG_GETARG_TEXT_PP(1);
 
 		funcctx = SRF_FIRSTCALL_INIT();
 		prs_setup_firstcall(funcctx, PG_GETARG_OID(0), txt);
@@ -270,8 +270,8 @@ ts_parse_byname(PG_FUNCTION_ARGS)
 
 	if (SRF_IS_FIRSTCALL())
 	{
-		text	   *prsname = PG_GETARG_TEXT_P(0);
-		text	   *txt = PG_GETARG_TEXT_P(1);
+		text	   *prsname = PG_GETARG_TEXT_PP(0);
+		text	   *txt = PG_GETARG_TEXT_PP(1);
 		Oid			prsId;
 
 		funcctx = SRF_FIRSTCALL_INIT();
@@ -289,9 +289,9 @@ ts_parse_byname(PG_FUNCTION_ARGS)
 Datum
 ts_headline_byid_opt(PG_FUNCTION_ARGS)
 {
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	TSQuery		query = PG_GETARG_TSQUERY(2);
-	text	   *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
+	text	   *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_PP(3) : NULL;
 	HeadlineParsedText prs;
 	List	   *prsoptions;
 	text	   *out;
@@ -310,7 +310,8 @@ ts_headline_byid_opt(PG_FUNCTION_ARGS)
 	prs.lenwords = 32;
 	prs.words = (HeadlineWordEntry *) palloc(sizeof(HeadlineWordEntry) * prs.lenwords);
 
-	hlparsetext(cfg->cfgId, &prs, query, VARDATA(in), VARSIZE(in) - VARHDRSZ);
+	hlparsetext(cfg->cfgId, &prs, query,
+				VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
 
 	if (opt)
 		prsoptions = deserialize_deflist(PointerGetDatum(opt));
diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c
index 96ac1df..c890899 100644
--- a/src/backend/utils/adt/acl.c
+++ b/src/backend/utils/adt/acl.c
@@ -1577,7 +1577,7 @@ makeaclitem(PG_FUNCTION_ARGS)
 {
 	Oid			grantee = PG_GETARG_OID(0);
 	Oid			grantor = PG_GETARG_OID(1);
-	text	   *privtext = PG_GETARG_TEXT_P(2);
+	text	   *privtext = PG_GETARG_TEXT_PP(2);
 	bool		goption = PG_GETARG_BOOL(3);
 	AclItem    *result;
 	AclMode		priv;
@@ -1853,8 +1853,8 @@ Datum
 has_table_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		rolename = PG_GETARG_NAME(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			tableoid;
 	AclMode		mode;
@@ -1878,8 +1878,8 @@ has_table_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_table_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *tablename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *tablename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			tableoid;
 	AclMode		mode;
@@ -1904,7 +1904,7 @@ has_table_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			tableoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -1930,7 +1930,7 @@ Datum
 has_table_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			tableoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -1955,8 +1955,8 @@ Datum
 has_table_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			tableoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -1979,7 +1979,7 @@ has_table_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			tableoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -2061,8 +2061,8 @@ Datum
 has_sequence_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		rolename = PG_GETARG_NAME(0);
-	text	   *sequencename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *sequencename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			sequenceoid;
 	AclMode		mode;
@@ -2091,8 +2091,8 @@ has_sequence_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_sequence_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *sequencename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *sequencename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			sequenceoid;
 	AclMode		mode;
@@ -2122,7 +2122,7 @@ has_sequence_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			sequenceoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2154,7 +2154,7 @@ Datum
 has_sequence_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			sequenceoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2185,8 +2185,8 @@ Datum
 has_sequence_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *sequencename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *sequencename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			sequenceoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2214,7 +2214,7 @@ has_sequence_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			sequenceoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 	char		relkind;
@@ -2272,8 +2272,8 @@ Datum
 has_any_column_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		rolename = PG_GETARG_NAME(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			tableoid;
 	AclMode		mode;
@@ -2301,8 +2301,8 @@ has_any_column_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_any_column_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *tablename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *tablename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			tableoid;
 	AclMode		mode;
@@ -2331,7 +2331,7 @@ has_any_column_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			tableoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2361,7 +2361,7 @@ Datum
 has_any_column_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			tableoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2390,8 +2390,8 @@ Datum
 has_any_column_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			tableoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2418,7 +2418,7 @@ has_any_column_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			tableoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -2512,9 +2512,9 @@ Datum
 has_column_privilege_name_name_name(PG_FUNCTION_ARGS)
 {
 	Name		rolename = PG_GETARG_NAME(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
-	text	   *column = PG_GETARG_TEXT_P(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
+	text	   *column = PG_GETARG_TEXT_PP(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	Oid			roleid;
 	Oid			tableoid;
 	AttrNumber	colattnum;
@@ -2541,9 +2541,9 @@ Datum
 has_column_privilege_name_name_attnum(PG_FUNCTION_ARGS)
 {
 	Name		rolename = PG_GETARG_NAME(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
 	AttrNumber	colattnum = PG_GETARG_INT16(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	Oid			roleid;
 	Oid			tableoid;
 	AclMode		mode;
@@ -2569,8 +2569,8 @@ has_column_privilege_name_id_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			tableoid = PG_GETARG_OID(1);
-	text	   *column = PG_GETARG_TEXT_P(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *column = PG_GETARG_TEXT_PP(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	Oid			roleid;
 	AttrNumber	colattnum;
 	AclMode		mode;
@@ -2597,7 +2597,7 @@ has_column_privilege_name_id_attnum(PG_FUNCTION_ARGS)
 	Name		username = PG_GETARG_NAME(0);
 	Oid			tableoid = PG_GETARG_OID(1);
 	AttrNumber	colattnum = PG_GETARG_INT16(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	Oid			roleid;
 	AclMode		mode;
 	int			privresult;
@@ -2620,9 +2620,9 @@ Datum
 has_column_privilege_id_name_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
-	text	   *column = PG_GETARG_TEXT_P(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
+	text	   *column = PG_GETARG_TEXT_PP(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	Oid			tableoid;
 	AttrNumber	colattnum;
 	AclMode		mode;
@@ -2647,9 +2647,9 @@ Datum
 has_column_privilege_id_name_attnum(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *tablename = PG_GETARG_TEXT_P(1);
+	text	   *tablename = PG_GETARG_TEXT_PP(1);
 	AttrNumber	colattnum = PG_GETARG_INT16(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	Oid			tableoid;
 	AclMode		mode;
 	int			privresult;
@@ -2673,8 +2673,8 @@ has_column_privilege_id_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			tableoid = PG_GETARG_OID(1);
-	text	   *column = PG_GETARG_TEXT_P(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *column = PG_GETARG_TEXT_PP(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	AttrNumber	colattnum;
 	AclMode		mode;
 	int			privresult;
@@ -2699,7 +2699,7 @@ has_column_privilege_id_id_attnum(PG_FUNCTION_ARGS)
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			tableoid = PG_GETARG_OID(1);
 	AttrNumber	colattnum = PG_GETARG_INT16(2);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(3);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(3);
 	AclMode		mode;
 	int			privresult;
 
@@ -2720,9 +2720,9 @@ has_column_privilege_id_id_attnum(PG_FUNCTION_ARGS)
 Datum
 has_column_privilege_name_name(PG_FUNCTION_ARGS)
 {
-	text	   *tablename = PG_GETARG_TEXT_P(0);
-	text	   *column = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablename = PG_GETARG_TEXT_PP(0);
+	text	   *column = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			tableoid;
 	AttrNumber	colattnum;
@@ -2749,9 +2749,9 @@ has_column_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_column_privilege_name_attnum(PG_FUNCTION_ARGS)
 {
-	text	   *tablename = PG_GETARG_TEXT_P(0);
+	text	   *tablename = PG_GETARG_TEXT_PP(0);
 	AttrNumber	colattnum = PG_GETARG_INT16(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			tableoid;
 	AclMode		mode;
@@ -2777,8 +2777,8 @@ Datum
 has_column_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			tableoid = PG_GETARG_OID(0);
-	text	   *column = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *column = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AttrNumber	colattnum;
 	AclMode		mode;
@@ -2805,7 +2805,7 @@ has_column_privilege_id_attnum(PG_FUNCTION_ARGS)
 {
 	Oid			tableoid = PG_GETARG_OID(0);
 	AttrNumber	colattnum = PG_GETARG_INT16(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	int			privresult;
@@ -2886,8 +2886,8 @@ Datum
 has_database_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *databasename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *databasename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			databaseoid;
 	AclMode		mode;
@@ -2911,8 +2911,8 @@ has_database_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_database_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *databasename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *databasename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			databaseoid;
 	AclMode		mode;
@@ -2937,7 +2937,7 @@ has_database_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			databaseoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2963,7 +2963,7 @@ Datum
 has_database_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			databaseoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -2988,8 +2988,8 @@ Datum
 has_database_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *databasename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *databasename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			databaseoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3012,7 +3012,7 @@ has_database_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			databaseoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -3084,8 +3084,8 @@ Datum
 has_foreign_data_wrapper_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *fdwname = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *fdwname = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			fdwid;
 	AclMode		mode;
@@ -3109,8 +3109,8 @@ has_foreign_data_wrapper_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_foreign_data_wrapper_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *fdwname = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *fdwname = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			fdwid;
 	AclMode		mode;
@@ -3135,7 +3135,7 @@ has_foreign_data_wrapper_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			fdwid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3158,7 +3158,7 @@ Datum
 has_foreign_data_wrapper_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			fdwid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3180,8 +3180,8 @@ Datum
 has_foreign_data_wrapper_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *fdwname = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *fdwname = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			fdwid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3204,7 +3204,7 @@ has_foreign_data_wrapper_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			fdwid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -3266,8 +3266,8 @@ Datum
 has_function_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *functionname = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *functionname = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			functionoid;
 	AclMode		mode;
@@ -3291,8 +3291,8 @@ has_function_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_function_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *functionname = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *functionname = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			functionoid;
 	AclMode		mode;
@@ -3317,7 +3317,7 @@ has_function_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			functionoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3343,7 +3343,7 @@ Datum
 has_function_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			functionoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3368,8 +3368,8 @@ Datum
 has_function_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *functionname = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *functionname = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			functionoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3392,7 +3392,7 @@ has_function_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			functionoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -3466,8 +3466,8 @@ Datum
 has_language_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *languagename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *languagename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			languageoid;
 	AclMode		mode;
@@ -3491,8 +3491,8 @@ has_language_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_language_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *languagename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *languagename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			languageoid;
 	AclMode		mode;
@@ -3517,7 +3517,7 @@ has_language_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			languageoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3543,7 +3543,7 @@ Datum
 has_language_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			languageoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3568,8 +3568,8 @@ Datum
 has_language_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *languagename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *languagename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			languageoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3592,7 +3592,7 @@ has_language_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			languageoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -3657,8 +3657,8 @@ Datum
 has_schema_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *schemaname = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *schemaname = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			schemaoid;
 	AclMode		mode;
@@ -3682,8 +3682,8 @@ has_schema_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_schema_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *schemaname = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *schemaname = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			schemaoid;
 	AclMode		mode;
@@ -3708,7 +3708,7 @@ has_schema_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			schemaoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3734,7 +3734,7 @@ Datum
 has_schema_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			schemaoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3759,8 +3759,8 @@ Datum
 has_schema_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *schemaname = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *schemaname = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			schemaoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3783,7 +3783,7 @@ has_schema_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			schemaoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -3850,8 +3850,8 @@ Datum
 has_server_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *servername = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *servername = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			serverid;
 	AclMode		mode;
@@ -3875,8 +3875,8 @@ has_server_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_server_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *servername = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *servername = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			serverid;
 	AclMode		mode;
@@ -3901,7 +3901,7 @@ has_server_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			serverid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3924,7 +3924,7 @@ Datum
 has_server_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			serverid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3946,8 +3946,8 @@ Datum
 has_server_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *servername = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *servername = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			serverid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -3970,7 +3970,7 @@ has_server_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			serverid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -4032,8 +4032,8 @@ Datum
 has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *tablespacename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablespacename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			tablespaceoid;
 	AclMode		mode;
@@ -4057,8 +4057,8 @@ has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_tablespace_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *tablespacename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *tablespacename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			tablespaceoid;
 	AclMode		mode;
@@ -4083,7 +4083,7 @@ has_tablespace_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			tablespaceoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4106,7 +4106,7 @@ Datum
 has_tablespace_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			tablespaceoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4128,8 +4128,8 @@ Datum
 has_tablespace_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *tablespacename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *tablespacename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			tablespaceoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4152,7 +4152,7 @@ has_tablespace_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			tablespaceoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -4213,8 +4213,8 @@ Datum
 has_type_privilege_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
-	text	   *typename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *typename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			typeoid;
 	AclMode		mode;
@@ -4238,8 +4238,8 @@ has_type_privilege_name_name(PG_FUNCTION_ARGS)
 Datum
 has_type_privilege_name(PG_FUNCTION_ARGS)
 {
-	text	   *typename = PG_GETARG_TEXT_P(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *typename = PG_GETARG_TEXT_PP(0);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			typeoid;
 	AclMode		mode;
@@ -4264,7 +4264,7 @@ has_type_privilege_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			typeoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4290,7 +4290,7 @@ Datum
 has_type_privilege_id(PG_FUNCTION_ARGS)
 {
 	Oid			typeoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4315,8 +4315,8 @@ Datum
 has_type_privilege_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
-	text	   *typename = PG_GETARG_TEXT_P(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *typename = PG_GETARG_TEXT_PP(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			typeoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4339,7 +4339,7 @@ has_type_privilege_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			typeoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
@@ -4414,7 +4414,7 @@ pg_has_role_name_name(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Name		rolename = PG_GETARG_NAME(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	Oid			roleoid;
 	AclMode		mode;
@@ -4439,7 +4439,7 @@ Datum
 pg_has_role_name(PG_FUNCTION_ARGS)
 {
 	Name		rolename = PG_GETARG_NAME(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	Oid			roleoid;
 	AclMode		mode;
@@ -4464,7 +4464,7 @@ pg_has_role_name_id(PG_FUNCTION_ARGS)
 {
 	Name		username = PG_GETARG_NAME(0);
 	Oid			roleoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4487,7 +4487,7 @@ Datum
 pg_has_role_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleoid = PG_GETARG_OID(0);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(1);
 	Oid			roleid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4510,7 +4510,7 @@ pg_has_role_id_name(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Name		rolename = PG_GETARG_NAME(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	Oid			roleoid;
 	AclMode		mode;
 	AclResult	aclresult;
@@ -4533,7 +4533,7 @@ pg_has_role_id_id(PG_FUNCTION_ARGS)
 {
 	Oid			roleid = PG_GETARG_OID(0);
 	Oid			roleoid = PG_GETARG_OID(1);
-	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
+	text	   *priv_type_text = PG_GETARG_TEXT_PP(2);
 	AclMode		mode;
 	AclResult	aclresult;
 
diff --git a/src/backend/utils/adt/char.c b/src/backend/utils/adt/char.c
index f0c4d25..1b849e8 100644
--- a/src/backend/utils/adt/char.c
+++ b/src/backend/utils/adt/char.c
@@ -175,7 +175,7 @@ i4tochar(PG_FUNCTION_ARGS)
 Datum
 text_char(PG_FUNCTION_ARGS)
 {
-	text	   *arg1 = PG_GETARG_TEXT_P(0);
+	text	   *arg1 = PG_GETARG_TEXT_PP(0);
 	char		result;
 
 	/*
@@ -183,8 +183,8 @@ text_char(PG_FUNCTION_ARGS)
 	 * If the input is longer than one character, the excess data is silently
 	 * discarded.
 	 */
-	if (VARSIZE(arg1) > VARHDRSZ)
-		result = *(VARDATA(arg1));
+	if (VARSIZE_ANY_EXHDR(arg1) > 0)
+		result = *(VARDATA_ANY(arg1));
 	else
 		result = '\0';
 
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 8cdd1dc..5892391 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -308,7 +308,7 @@ Datum
 pg_relation_size(PG_FUNCTION_ARGS)
 {
 	Oid			relOid = PG_GETARG_OID(0);
-	text	   *forkName = PG_GETARG_TEXT_P(1);
+	text	   *forkName = PG_GETARG_TEXT_PP(1);
 	Relation	rel;
 	int64		size;
 
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index 027f28e..8fd8ede 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -35,7 +35,7 @@ static const struct pg_encoding *pg_find_encoding(const char *name);
 Datum
 binary_encode(PG_FUNCTION_ARGS)
 {
-	bytea	   *data = PG_GETARG_BYTEA_P(0);
+	bytea	   *data = PG_GETARG_BYTEA_PP(0);
 	Datum		name = PG_GETARG_DATUM(1);
 	text	   *result;
 	char	   *namebuf;
@@ -44,7 +44,7 @@ binary_encode(PG_FUNCTION_ARGS)
 				res;
 	const struct pg_encoding *enc;
 
-	datalen = VARSIZE(data) - VARHDRSZ;
+	datalen = VARSIZE_ANY_EXHDR(data);
 
 	namebuf = TextDatumGetCString(name);
 
@@ -54,10 +54,10 @@ binary_encode(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 errmsg("unrecognized encoding: \"%s\"", namebuf)));
 
-	resultlen = enc->encode_len(VARDATA(data), datalen);
+	resultlen = enc->encode_len(VARDATA_ANY(data), datalen);
 	result = palloc(VARHDRSZ + resultlen);
 
-	res = enc->encode(VARDATA(data), datalen, VARDATA(result));
+	res = enc->encode(VARDATA_ANY(data), datalen, VARDATA(result));
 
 	/* Make this FATAL 'cause we've trodden on memory ... */
 	if (res > resultlen)
@@ -71,7 +71,7 @@ binary_encode(PG_FUNCTION_ARGS)
 Datum
 binary_decode(PG_FUNCTION_ARGS)
 {
-	text	   *data = PG_GETARG_TEXT_P(0);
+	text	   *data = PG_GETARG_TEXT_PP(0);
 	Datum		name = PG_GETARG_DATUM(1);
 	bytea	   *result;
 	char	   *namebuf;
@@ -80,7 +80,7 @@ binary_decode(PG_FUNCTION_ARGS)
 				res;
 	const struct pg_encoding *enc;
 
-	datalen = VARSIZE(data) - VARHDRSZ;
+	datalen = VARSIZE_ANY_EXHDR(data);
 
 	namebuf = TextDatumGetCString(name);
 
@@ -90,10 +90,10 @@ binary_decode(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 errmsg("unrecognized encoding: \"%s\"", namebuf)));
 
-	resultlen = enc->decode_len(VARDATA(data), datalen);
+	resultlen = enc->decode_len(VARDATA_ANY(data), datalen);
 	result = palloc(VARHDRSZ + resultlen);
 
-	res = enc->decode(VARDATA(data), datalen, VARDATA(result));
+	res = enc->decode(VARDATA_ANY(data), datalen, VARDATA(result));
 
 	/* Make this FATAL 'cause we've trodden on memory ... */
 	if (res > resultlen)
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index e552c8d..c16bfbc 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -3380,13 +3380,13 @@ Datum
 timestamp_to_char(PG_FUNCTION_ARGS)
 {
 	Timestamp	dt = PG_GETARG_TIMESTAMP(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1),
+	text	   *fmt = PG_GETARG_TEXT_PP(1),
 			   *res;
 	TmToChar	tmtc;
 	struct pg_tm *tm;
 	int			thisdate;
 
-	if ((VARSIZE(fmt) - VARHDRSZ) <= 0 || TIMESTAMP_NOT_FINITE(dt))
+	if (VARSIZE_ANY_EXHDR(fmt) <= 0 || TIMESTAMP_NOT_FINITE(dt))
 		PG_RETURN_NULL();
 
 	ZERO_tmtc(&tmtc);
@@ -3411,14 +3411,14 @@ Datum
 timestamptz_to_char(PG_FUNCTION_ARGS)
 {
 	TimestampTz dt = PG_GETARG_TIMESTAMP(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1),
+	text	   *fmt = PG_GETARG_TEXT_PP(1),
 			   *res;
 	TmToChar	tmtc;
 	int			tz;
 	struct pg_tm *tm;
 	int			thisdate;
 
-	if ((VARSIZE(fmt) - VARHDRSZ) <= 0 || TIMESTAMP_NOT_FINITE(dt))
+	if (VARSIZE_ANY_EXHDR(fmt) <= 0 || TIMESTAMP_NOT_FINITE(dt))
 		PG_RETURN_NULL();
 
 	ZERO_tmtc(&tmtc);
@@ -3448,12 +3448,12 @@ Datum
 interval_to_char(PG_FUNCTION_ARGS)
 {
 	Interval   *it = PG_GETARG_INTERVAL_P(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1),
+	text	   *fmt = PG_GETARG_TEXT_PP(1),
 			   *res;
 	TmToChar	tmtc;
 	struct pg_tm *tm;
 
-	if ((VARSIZE(fmt) - VARHDRSZ) <= 0)
+	if (VARSIZE_ANY_EXHDR(fmt) <= 0)
 		PG_RETURN_NULL();
 
 	ZERO_tmtc(&tmtc);
@@ -3481,8 +3481,8 @@ interval_to_char(PG_FUNCTION_ARGS)
 Datum
 to_timestamp(PG_FUNCTION_ARGS)
 {
-	text	   *date_txt = PG_GETARG_TEXT_P(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *date_txt = PG_GETARG_TEXT_PP(0);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	Timestamp	result;
 	int			tz;
 	struct pg_tm tm;
@@ -3508,8 +3508,8 @@ to_timestamp(PG_FUNCTION_ARGS)
 Datum
 to_date(PG_FUNCTION_ARGS)
 {
-	text	   *date_txt = PG_GETARG_TEXT_P(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *date_txt = PG_GETARG_TEXT_PP(0);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	DateADT		result;
 	struct pg_tm tm;
 	fsec_t		fsec;
@@ -5038,8 +5038,8 @@ do { \
 Datum
 numeric_to_number(PG_FUNCTION_ARGS)
 {
-	text	   *value = PG_GETARG_TEXT_P(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *value = PG_GETARG_TEXT_PP(0);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	NUMDesc		Num;
 	Datum		result;
 	FormatNode *format;
@@ -5049,7 +5049,7 @@ numeric_to_number(PG_FUNCTION_ARGS)
 	int			scale,
 				precision;
 
-	len = VARSIZE(fmt) - VARHDRSZ;
+	len = VARSIZE_ANY_EXHDR(fmt);
 
 	if (len <= 0 || len >= INT_MAX / NUM_MAX_ITEM_SIZ)
 		PG_RETURN_NULL();
@@ -5058,8 +5058,8 @@ numeric_to_number(PG_FUNCTION_ARGS)
 
 	numstr = (char *) palloc((len * NUM_MAX_ITEM_SIZ) + 1);
 
-	NUM_processor(format, &Num, VARDATA(value), numstr,
-				  VARSIZE(value) - VARHDRSZ, 0, 0, false, PG_GET_COLLATION());
+	NUM_processor(format, &Num, VARDATA_ANY(value), numstr,
+				  VARSIZE_ANY_EXHDR(value), 0, 0, false, PG_GET_COLLATION());
 
 	scale = Num.post;
 	precision = Num.pre + Num.multi + scale;
@@ -5100,7 +5100,7 @@ Datum
 numeric_to_char(PG_FUNCTION_ARGS)
 {
 	Numeric		value = PG_GETARG_NUMERIC(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	NUMDesc		Num;
 	FormatNode *format;
 	text	   *result;
@@ -5224,7 +5224,7 @@ Datum
 int4_to_char(PG_FUNCTION_ARGS)
 {
 	int32		value = PG_GETARG_INT32(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	NUMDesc		Num;
 	FormatNode *format;
 	text	   *result;
@@ -5319,7 +5319,7 @@ Datum
 int8_to_char(PG_FUNCTION_ARGS)
 {
 	int64		value = PG_GETARG_INT64(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	NUMDesc		Num;
 	FormatNode *format;
 	text	   *result;
@@ -5429,7 +5429,7 @@ Datum
 float4_to_char(PG_FUNCTION_ARGS)
 {
 	float4		value = PG_GETARG_FLOAT4(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	NUMDesc		Num;
 	FormatNode *format;
 	text	   *result;
@@ -5535,7 +5535,7 @@ Datum
 float8_to_char(PG_FUNCTION_ARGS)
 {
 	float8		value = PG_GETARG_FLOAT8(0);
-	text	   *fmt = PG_GETARG_TEXT_P(1);
+	text	   *fmt = PG_GETARG_TEXT_PP(1);
 	NUMDesc		Num;
 	FormatNode *format;
 	text	   *result;
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index 1a127d9..2147936 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -187,7 +187,7 @@ read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
 Datum
 pg_read_file(PG_FUNCTION_ARGS)
 {
-	text	   *filename_t = PG_GETARG_TEXT_P(0);
+	text	   *filename_t = PG_GETARG_TEXT_PP(0);
 	int64		seek_offset = 0;
 	int64		bytes_to_read = -1;
 	bool		missing_ok = false;
@@ -228,7 +228,7 @@ pg_read_file(PG_FUNCTION_ARGS)
 Datum
 pg_read_binary_file(PG_FUNCTION_ARGS)
 {
-	text	   *filename_t = PG_GETARG_TEXT_P(0);
+	text	   *filename_t = PG_GETARG_TEXT_PP(0);
 	int64		seek_offset = 0;
 	int64		bytes_to_read = -1;
 	bool		missing_ok = false;
@@ -303,7 +303,7 @@ pg_read_binary_file_all(PG_FUNCTION_ARGS)
 Datum
 pg_stat_file(PG_FUNCTION_ARGS)
 {
-	text	   *filename_t = PG_GETARG_TEXT_P(0);
+	text	   *filename_t = PG_GETARG_TEXT_PP(0);
 	char	   *filename;
 	struct stat fst;
 	Datum		values[6];
@@ -421,7 +421,7 @@ pg_ls_dir(PG_FUNCTION_ARGS)
 		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
 
 		fctx = palloc(sizeof(directory_fctx));
-		fctx->location = convert_and_check_filename(PG_GETARG_TEXT_P(0));
+		fctx->location = convert_and_check_filename(PG_GETARG_TEXT_PP(0));
 
 		fctx->include_dot_dirs = include_dot_dirs;
 		fctx->dirdesc = AllocateDir(fctx->location);
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 0ed6a10..9fb0e48 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -299,8 +299,8 @@ json_recv(PG_FUNCTION_ARGS)
 JsonLexContext *
 makeJsonLexContext(text *json, bool need_escapes)
 {
-	return makeJsonLexContextCstringLen(VARDATA(json),
-										VARSIZE(json) - VARHDRSZ,
+	return makeJsonLexContextCstringLen(VARDATA_ANY(json),
+										VARSIZE_ANY_EXHDR(json),
 										need_escapes);
 }
 
@@ -1570,7 +1570,7 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
 			break;
 		case JSONTYPE_CAST:
 			/* outfuncoid refers to a cast function, not an output function */
-			jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
+			jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
 			outputstr = text_to_cstring(jsontext);
 			appendStringInfoString(result, outputstr);
 			pfree(outputstr);
@@ -2492,7 +2492,7 @@ json_typeof(PG_FUNCTION_ARGS)
 	JsonTokenType tok;
 	char	   *type;
 
-	json = PG_GETARG_TEXT_P(0);
+	json = PG_GETARG_TEXT_PP(0);
 	lex = makeJsonLexContext(json, false);
 
 	/* Lex exactly one token from the input and check its type. */
diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index 5b6178b..164f57e 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -857,7 +857,7 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
 					/* parse the json right into the existing result object */
 					JsonLexContext *lex;
 					JsonSemAction sem;
-					text	   *json = DatumGetTextP(val);
+					text	   *json = DatumGetTextPP(val);
 
 					lex = makeJsonLexContext(json, true);
 
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 9b46f8e..bf2c91f 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -371,7 +371,7 @@ json_object_keys(PG_FUNCTION_ARGS)
 
 	if (SRF_IS_FIRSTCALL())
 	{
-		text	   *json = PG_GETARG_TEXT_P(0);
+		text	   *json = PG_GETARG_TEXT_PP(0);
 		JsonLexContext *lex = makeJsonLexContext(json, true);
 		JsonSemAction *sem;
 		MemoryContext oldcontext;
@@ -482,7 +482,7 @@ okeys_scalar(void *state, char *token, JsonTokenType tokentype)
 Datum
 json_object_field(PG_FUNCTION_ARGS)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	text	   *fname = PG_GETARG_TEXT_PP(1);
 	char	   *fnamestr = text_to_cstring(fname);
 	text	   *result;
@@ -518,7 +518,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
 Datum
 json_object_field_text(PG_FUNCTION_ARGS)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	text	   *fname = PG_GETARG_TEXT_PP(1);
 	char	   *fnamestr = text_to_cstring(fname);
 	text	   *result;
@@ -585,7 +585,7 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
 Datum
 json_array_element(PG_FUNCTION_ARGS)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	int			element = PG_GETARG_INT32(1);
 	text	   *result;
 
@@ -628,7 +628,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
 Datum
 json_array_element_text(PG_FUNCTION_ARGS)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	int			element = PG_GETARG_INT32(1);
 	text	   *result;
 
@@ -717,7 +717,7 @@ json_extract_path_text(PG_FUNCTION_ARGS)
 static Datum
 get_path_all(FunctionCallInfo fcinfo, bool as_text)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	ArrayType  *path = PG_GETARG_ARRAYTYPE_P(1);
 	text	   *result;
 	Datum	   *pathtext;
@@ -1338,7 +1338,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
 Datum
 json_array_length(PG_FUNCTION_ARGS)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	AlenState  *state;
 	JsonLexContext *lex;
 	JsonSemAction *sem;
@@ -1592,7 +1592,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
 static Datum
 each_worker(FunctionCallInfo fcinfo, bool as_text)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	JsonLexContext *lex;
 	JsonSemAction *sem;
 	ReturnSetInfo *rsi;
@@ -1906,7 +1906,7 @@ json_array_elements_text(PG_FUNCTION_ARGS)
 static Datum
 elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 
 	/* elements only needs escaped strings when as_text */
 	JsonLexContext *lex = makeJsonLexContext(json, as_text);
@@ -2177,7 +2177,7 @@ populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
 	if (jtype == JSONOID)
 	{
 		/* just get the text */
-		json = PG_GETARG_TEXT_P(json_arg_num);
+		json = PG_GETARG_TEXT_PP(json_arg_num);
 
 		json_hash = get_json_object_as_hash(json, funcname);
 
@@ -2767,7 +2767,7 @@ populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
 
 	if (jtype == JSONOID)
 	{
-		text	   *json = PG_GETARG_TEXT_P(json_arg_num);
+		text	   *json = PG_GETARG_TEXT_PP(json_arg_num);
 		JsonLexContext *lex;
 		JsonSemAction *sem;
 
@@ -3193,7 +3193,7 @@ sn_scalar(void *state, char *token, JsonTokenType tokentype)
 Datum
 json_strip_nulls(PG_FUNCTION_ARGS)
 {
-	text	   *json = PG_GETARG_TEXT_P(0);
+	text	   *json = PG_GETARG_TEXT_PP(0);
 	StripnullState *state;
 	JsonLexContext *lex;
 	JsonSemAction *sem;
diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c
index 91fe109..8d9d285 100644
--- a/src/backend/utils/adt/like.c
+++ b/src/backend/utils/adt/like.c
@@ -175,14 +175,14 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation)
 	if (pg_database_encoding_max_length() > 1)
 	{
 		/* lower's result is never packed, so OK to use old macros here */
-		pat = DatumGetTextP(DirectFunctionCall1Coll(lower, collation,
-													PointerGetDatum(pat)));
-		p = VARDATA(pat);
-		plen = (VARSIZE(pat) - VARHDRSZ);
-		str = DatumGetTextP(DirectFunctionCall1Coll(lower, collation,
-													PointerGetDatum(str)));
-		s = VARDATA(str);
-		slen = (VARSIZE(str) - VARHDRSZ);
+		pat = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
+													 PointerGetDatum(pat)));
+		p = VARDATA_ANY(pat);
+		plen = VARSIZE_ANY_EXHDR(pat);
+		str = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
+													 PointerGetDatum(str)));
+		s = VARDATA_ANY(str);
+		slen = VARSIZE_ANY_EXHDR(str);
 		if (GetDatabaseEncoding() == PG_UTF8)
 			return UTF8_MatchText(s, slen, p, plen, 0, true);
 		else
@@ -365,8 +365,8 @@ nameiclike(PG_FUNCTION_ARGS)
 	bool		result;
 	text	   *strtext;
 
-	strtext = DatumGetTextP(DirectFunctionCall1(name_text,
-												NameGetDatum(str)));
+	strtext = DatumGetTextPP(DirectFunctionCall1(name_text,
+												 NameGetDatum(str)));
 	result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) == LIKE_TRUE);
 
 	PG_RETURN_BOOL(result);
@@ -380,8 +380,8 @@ nameicnlike(PG_FUNCTION_ARGS)
 	bool		result;
 	text	   *strtext;
 
-	strtext = DatumGetTextP(DirectFunctionCall1(name_text,
-												NameGetDatum(str)));
+	strtext = DatumGetTextPP(DirectFunctionCall1(name_text,
+												 NameGetDatum(str)));
 	result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) != LIKE_TRUE);
 
 	PG_RETURN_BOOL(result);
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 781c713..6cce0f2 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -3601,7 +3601,7 @@ numeric_avg_serialize(PG_FUNCTION_ARGS)
 
 	temp = DirectFunctionCall1(numeric_send,
 							   NumericGetDatum(make_result(&tmp_var)));
-	sumX = DatumGetByteaP(temp);
+	sumX = DatumGetByteaPP(temp);
 	free_var(&tmp_var);
 
 	pq_begintypsend(&buf);
@@ -3610,7 +3610,7 @@ numeric_avg_serialize(PG_FUNCTION_ARGS)
 	pq_sendint64(&buf, state->N);
 
 	/* sumX */
-	pq_sendbytes(&buf, VARDATA(sumX), VARSIZE(sumX) - VARHDRSZ);
+	pq_sendbytes(&buf, VARDATA_ANY(sumX), VARSIZE_ANY_EXHDR(sumX));
 
 	/* maxScale */
 	pq_sendint(&buf, state->maxScale, 4);
@@ -3643,14 +3643,15 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
 	if (!AggCheckCallContext(fcinfo, NULL))
 		elog(ERROR, "aggregate function called in non-aggregate context");
 
-	sstate = PG_GETARG_BYTEA_P(0);
+	sstate = PG_GETARG_BYTEA_PP(0);
 
 	/*
 	 * Copy the bytea into a StringInfo so that we can "receive" it using the
 	 * standard recv-function infrastructure.
 	 */
 	initStringInfo(&buf);
-	appendBinaryStringInfo(&buf, VARDATA(sstate), VARSIZE(sstate) - VARHDRSZ);
+	appendBinaryStringInfo(&buf,
+						   VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
 	result = makeNumericAggStateCurrentContext(false);
 
@@ -3713,12 +3714,12 @@ numeric_serialize(PG_FUNCTION_ARGS)
 	accum_sum_final(&state->sumX, &tmp_var);
 	temp = DirectFunctionCall1(numeric_send,
 							   NumericGetDatum(make_result(&tmp_var)));
-	sumX = DatumGetByteaP(temp);
+	sumX = DatumGetByteaPP(temp);
 
 	accum_sum_final(&state->sumX2, &tmp_var);
 	temp = DirectFunctionCall1(numeric_send,
 							   NumericGetDatum(make_result(&tmp_var)));
-	sumX2 = DatumGetByteaP(temp);
+	sumX2 = DatumGetByteaPP(temp);
 
 	free_var(&tmp_var);
 
@@ -3728,10 +3729,10 @@ numeric_serialize(PG_FUNCTION_ARGS)
 	pq_sendint64(&buf, state->N);
 
 	/* sumX */
-	pq_sendbytes(&buf, VARDATA(sumX), VARSIZE(sumX) - VARHDRSZ);
+	pq_sendbytes(&buf, VARDATA_ANY(sumX), VARSIZE_ANY_EXHDR(sumX));
 
 	/* sumX2 */
-	pq_sendbytes(&buf, VARDATA(sumX2), VARSIZE(sumX2) - VARHDRSZ);
+	pq_sendbytes(&buf, VARDATA_ANY(sumX2), VARSIZE_ANY_EXHDR(sumX2));
 
 	/* maxScale */
 	pq_sendint(&buf, state->maxScale, 4);
@@ -3765,14 +3766,15 @@ numeric_deserialize(PG_FUNCTION_ARGS)
 	if (!AggCheckCallContext(fcinfo, NULL))
 		elog(ERROR, "aggregate function called in non-aggregate context");
 
-	sstate = PG_GETARG_BYTEA_P(0);
+	sstate = PG_GETARG_BYTEA_PP(0);
 
 	/*
 	 * Copy the bytea into a StringInfo so that we can "receive" it using the
 	 * standard recv-function infrastructure.
 	 */
 	initStringInfo(&buf);
-	appendBinaryStringInfo(&buf, VARDATA(sstate), VARSIZE(sstate) - VARHDRSZ);
+	appendBinaryStringInfo(&buf,
+						   VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
 	result = makeNumericAggStateCurrentContext(false);
 
@@ -4114,7 +4116,7 @@ numeric_poly_serialize(PG_FUNCTION_ARGS)
 #endif
 		temp = DirectFunctionCall1(numeric_send,
 								   NumericGetDatum(make_result(&num)));
-		sumX = DatumGetByteaP(temp);
+		sumX = DatumGetByteaPP(temp);
 
 #ifdef HAVE_INT128
 		int128_to_numericvar(state->sumX2, &num);
@@ -4123,7 +4125,7 @@ numeric_poly_serialize(PG_FUNCTION_ARGS)
 #endif
 		temp = DirectFunctionCall1(numeric_send,
 								   NumericGetDatum(make_result(&num)));
-		sumX2 = DatumGetByteaP(temp);
+		sumX2 = DatumGetByteaPP(temp);
 
 		free_var(&num);
 	}
@@ -4134,10 +4136,10 @@ numeric_poly_serialize(PG_FUNCTION_ARGS)
 	pq_sendint64(&buf, state->N);
 
 	/* sumX */
-	pq_sendbytes(&buf, VARDATA(sumX), VARSIZE(sumX) - VARHDRSZ);
+	pq_sendbytes(&buf, VARDATA_ANY(sumX), VARSIZE_ANY_EXHDR(sumX));
 
 	/* sumX2 */
-	pq_sendbytes(&buf, VARDATA(sumX2), VARSIZE(sumX2) - VARHDRSZ);
+	pq_sendbytes(&buf, VARDATA_ANY(sumX2), VARSIZE_ANY_EXHDR(sumX2));
 
 	result = pq_endtypsend(&buf);
 
@@ -4163,14 +4165,15 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
 	if (!AggCheckCallContext(fcinfo, NULL))
 		elog(ERROR, "aggregate function called in non-aggregate context");
 
-	sstate = PG_GETARG_BYTEA_P(0);
+	sstate = PG_GETARG_BYTEA_PP(0);
 
 	/*
 	 * Copy the bytea into a StringInfo so that we can "receive" it using the
 	 * standard recv-function infrastructure.
 	 */
 	initStringInfo(&buf);
-	appendBinaryStringInfo(&buf, VARDATA(sstate), VARSIZE(sstate) - VARHDRSZ);
+	appendBinaryStringInfo(&buf,
+						   VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
 	result = makePolyNumAggStateCurrentContext(false);
 
@@ -4338,7 +4341,7 @@ int8_avg_serialize(PG_FUNCTION_ARGS)
 #endif
 		temp = DirectFunctionCall1(numeric_send,
 								   NumericGetDatum(make_result(&num)));
-		sumX = DatumGetByteaP(temp);
+		sumX = DatumGetByteaPP(temp);
 
 		free_var(&num);
 	}
@@ -4349,7 +4352,7 @@ int8_avg_serialize(PG_FUNCTION_ARGS)
 	pq_sendint64(&buf, state->N);
 
 	/* sumX */
-	pq_sendbytes(&buf, VARDATA(sumX), VARSIZE(sumX) - VARHDRSZ);
+	pq_sendbytes(&buf, VARDATA_ANY(sumX), VARSIZE_ANY_EXHDR(sumX));
 
 	result = pq_endtypsend(&buf);
 
@@ -4372,14 +4375,15 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
 	if (!AggCheckCallContext(fcinfo, NULL))
 		elog(ERROR, "aggregate function called in non-aggregate context");
 
-	sstate = PG_GETARG_BYTEA_P(0);
+	sstate = PG_GETARG_BYTEA_PP(0);
 
 	/*
 	 * Copy the bytea into a StringInfo so that we can "receive" it using the
 	 * standard recv-function infrastructure.
 	 */
 	initStringInfo(&buf);
-	appendBinaryStringInfo(&buf, VARDATA(sstate), VARSIZE(sstate) - VARHDRSZ);
+	appendBinaryStringInfo(&buf,
+						   VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
 	result = makePolyNumAggStateCurrentContext(false);
 
diff --git a/src/backend/utils/adt/quote.c b/src/backend/utils/adt/quote.c
index 549c503..43e5bb7 100644
--- a/src/backend/utils/adt/quote.c
+++ b/src/backend/utils/adt/quote.c
@@ -76,17 +76,17 @@ quote_literal_internal(char *dst, const char *src, size_t len)
 Datum
 quote_literal(PG_FUNCTION_ARGS)
 {
-	text	   *t = PG_GETARG_TEXT_P(0);
+	text	   *t = PG_GETARG_TEXT_PP(0);
 	text	   *result;
 	char	   *cp1;
 	char	   *cp2;
 	int			len;
 
-	len = VARSIZE(t) - VARHDRSZ;
+	len = VARSIZE_ANY_EXHDR(t);
 	/* We make a worst-case result area; wasting a little space is OK */
 	result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
 
-	cp1 = VARDATA(t);
+	cp1 = VARDATA_ANY(t);
 	cp2 = VARDATA(result);
 
 	SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c
index e518523..304345b 100644
--- a/src/backend/utils/adt/rangetypes.c
+++ b/src/backend/utils/adt/rangetypes.c
@@ -404,7 +404,7 @@ range_constructor3(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_DATA_EXCEPTION),
 			   errmsg("range constructor flags argument must not be null")));
 
-	flags = range_parse_flags(text_to_cstring(PG_GETARG_TEXT_P(2)));
+	flags = range_parse_flags(text_to_cstring(PG_GETARG_TEXT_PP(2)));
 
 	lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
 	lower.infinite = PG_ARGISNULL(0);
diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c
index fa920c0..702924a 100644
--- a/src/backend/utils/adt/regproc.c
+++ b/src/backend/utils/adt/regproc.c
@@ -1810,7 +1810,7 @@ regnamespacesend(PG_FUNCTION_ARGS)
 Datum
 text_regclass(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	Oid			result;
 	RangeVar   *rv;
 
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index b27b77d..7b54c1e 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -644,7 +644,7 @@ Datum
 pg_get_viewdef_name(PG_FUNCTION_ARGS)
 {
 	/* By qualified name */
-	text	   *viewname = PG_GETARG_TEXT_P(0);
+	text	   *viewname = PG_GETARG_TEXT_PP(0);
 	int			prettyFlags;
 	RangeVar   *viewrel;
 	Oid			viewoid;
@@ -669,7 +669,7 @@ Datum
 pg_get_viewdef_name_ext(PG_FUNCTION_ARGS)
 {
 	/* By qualified name */
-	text	   *viewname = PG_GETARG_TEXT_P(0);
+	text	   *viewname = PG_GETARG_TEXT_PP(0);
 	bool		pretty = PG_GETARG_BOOL(1);
 	int			prettyFlags;
 	RangeVar   *viewrel;
@@ -1031,7 +1031,7 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
 							tgrel->rd_att, &isnull);
 		if (isnull)
 			elog(ERROR, "tgargs is null for trigger %u", trigid);
-		p = (char *) VARDATA(DatumGetByteaP(value));
+		p = (char *) VARDATA_ANY(DatumGetByteaPP(value));
 		for (i = 0; i < trigrec->tgnargs; i++)
 		{
 			if (i > 0)
@@ -2013,7 +2013,7 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
 Datum
 pg_get_expr(PG_FUNCTION_ARGS)
 {
-	text	   *expr = PG_GETARG_TEXT_P(0);
+	text	   *expr = PG_GETARG_TEXT_PP(0);
 	Oid			relid = PG_GETARG_OID(1);
 	int			prettyFlags;
 	char	   *relname;
@@ -2043,7 +2043,7 @@ pg_get_expr(PG_FUNCTION_ARGS)
 Datum
 pg_get_expr_ext(PG_FUNCTION_ARGS)
 {
-	text	   *expr = PG_GETARG_TEXT_P(0);
+	text	   *expr = PG_GETARG_TEXT_PP(0);
 	Oid			relid = PG_GETARG_OID(1);
 	bool		pretty = PG_GETARG_BOOL(2);
 	int			prettyFlags;
@@ -2141,7 +2141,7 @@ pg_get_userbyid(PG_FUNCTION_ARGS)
 Datum
 pg_get_serial_sequence(PG_FUNCTION_ARGS)
 {
-	text	   *tablename = PG_GETARG_TEXT_P(0);
+	text	   *tablename = PG_GETARG_TEXT_PP(0);
 	text	   *columnname = PG_GETARG_TEXT_PP(1);
 	RangeVar   *tablerv;
 	Oid			tableOid;
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 8b05e8f..9fa3338 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -5341,13 +5341,12 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
 	}
 	else
 	{
-		bytea	   *bstr = DatumGetByteaP(patt_const->constvalue);
+		bytea	   *bstr = DatumGetByteaPP(patt_const->constvalue);
 
-		pattlen = VARSIZE(bstr) - VARHDRSZ;
+		pattlen = VARSIZE_ANY_EXHDR(bstr);
 		patt = (char *) palloc(pattlen);
-		memcpy(patt, VARDATA(bstr), pattlen);
-		if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
-			pfree(bstr);
+		memcpy(patt, VARDATA_ANY(bstr), pattlen);
+		Assert((Pointer) bstr == DatumGetPointer(patt_const->constvalue));
 	}
 
 	match = palloc(pattlen + 1);
@@ -5857,13 +5856,12 @@ make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
 	}
 	else if (datatype == BYTEAOID)
 	{
-		bytea	   *bstr = DatumGetByteaP(str_const->constvalue);
+		bytea	   *bstr = DatumGetByteaPP(str_const->constvalue);
 
-		len = VARSIZE(bstr) - VARHDRSZ;
+		len = VARSIZE_ANY_EXHDR(bstr);
 		workstr = (char *) palloc(len);
-		memcpy(workstr, VARDATA(bstr), len);
-		if ((Pointer) bstr != DatumGetPointer(str_const->constvalue))
-			pfree(bstr);
+		memcpy(workstr, VARDATA_ANY(bstr), len);
+		Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
 		cmpstr = str_const->constvalue;
 	}
 	else
diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c
index a3b372f..49a5a15 100644
--- a/src/backend/utils/adt/tid.c
+++ b/src/backend/utils/adt/tid.c
@@ -368,7 +368,7 @@ currtid_byreloid(PG_FUNCTION_ARGS)
 Datum
 currtid_byrelname(PG_FUNCTION_ARGS)
 {
-	text	   *relname = PG_GETARG_TEXT_P(0);
+	text	   *relname = PG_GETARG_TEXT_PP(0);
 	ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
 	ItemPointer result;
 	RangeVar   *relrv;
diff --git a/src/backend/utils/adt/tsquery_rewrite.c b/src/backend/utils/adt/tsquery_rewrite.c
index 266a82d..1bd3dea 100644
--- a/src/backend/utils/adt/tsquery_rewrite.c
+++ b/src/backend/utils/adt/tsquery_rewrite.c
@@ -280,7 +280,7 @@ Datum
 tsquery_rewrite_query(PG_FUNCTION_ARGS)
 {
 	TSQuery		query = PG_GETARG_TSQUERY_COPY(0);
-	text	   *in = PG_GETARG_TEXT_P(1);
+	text	   *in = PG_GETARG_TEXT_PP(1);
 	TSQuery		rewrited = query;
 	MemoryContext outercontext = CurrentMemoryContext;
 	MemoryContext oldcontext;
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 3dab84a..c694637 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -557,8 +557,8 @@ tsvector_delete_str(PG_FUNCTION_ARGS)
 {
 	TSVector	tsin = PG_GETARG_TSVECTOR(0),
 				tsout;
-	text	   *tlexeme = PG_GETARG_TEXT_P(1);
-	char	   *lexeme = VARDATA(tlexeme);
+	text	   *tlexeme = PG_GETARG_TEXT_PP(1);
+	char	   *lexeme = VARDATA_ANY(tlexeme);
 	int			lexeme_len = VARSIZE_ANY_EXHDR(tlexeme),
 				skip_index;
 
@@ -2320,8 +2320,8 @@ ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
 	{
 		char	   *buf;
 
-		buf = VARDATA(ws);
-		while (buf - VARDATA(ws) < VARSIZE(ws) - VARHDRSZ)
+		buf = VARDATA_ANY(ws);
+		while (buf - VARDATA_ANY(ws) < VARSIZE_ANY_EXHDR(ws))
 		{
 			if (pg_mblen(buf) == 1)
 			{
@@ -2384,7 +2384,7 @@ ts_stat1(PG_FUNCTION_ARGS)
 	if (SRF_IS_FIRSTCALL())
 	{
 		TSVectorStat *stat;
-		text	   *txt = PG_GETARG_TEXT_P(0);
+		text	   *txt = PG_GETARG_TEXT_PP(0);
 
 		funcctx = SRF_FIRSTCALL_INIT();
 		SPI_connect();
@@ -2409,8 +2409,8 @@ ts_stat2(PG_FUNCTION_ARGS)
 	if (SRF_IS_FIRSTCALL())
 	{
 		TSVectorStat *stat;
-		text	   *txt = PG_GETARG_TEXT_P(0);
-		text	   *ws = PG_GETARG_TEXT_P(1);
+		text	   *txt = PG_GETARG_TEXT_PP(0);
+		text	   *ws = PG_GETARG_TEXT_PP(1);
 
 		funcctx = SRF_FIRSTCALL_INIT();
 		SPI_connect();
@@ -2570,9 +2570,9 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
 		if (isnull)
 			continue;
 
-		txt = DatumGetTextP(datum);
+		txt = DatumGetTextPP(datum);
 
-		parsetext(cfgId, &prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ);
+		parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
 
 		if (txt != (text *) DatumGetPointer(datum))
 			pfree(txt);
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 28b5745..cd036af 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -2662,7 +2662,7 @@ bytea_catenate(bytea *t1, bytea *t2)
 }
 
 #define PG_STR_GET_BYTEA(str_) \
-	DatumGetByteaP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
+	DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
 /*
  * bytea_substr()
@@ -2934,13 +2934,12 @@ byteaGetBit(PG_FUNCTION_ARGS)
 Datum
 byteaSetByte(PG_FUNCTION_ARGS)
 {
-	bytea	   *v = PG_GETARG_BYTEA_P(0);
+	bytea	   *res = PG_GETARG_BYTEA_P_COPY(0);
 	int32		n = PG_GETARG_INT32(1);
 	int32		newByte = PG_GETARG_INT32(2);
 	int			len;
-	bytea	   *res;
 
-	len = VARSIZE(v) - VARHDRSZ;
+	len = VARSIZE(res) - VARHDRSZ;
 
 	if (n < 0 || n >= len)
 		ereport(ERROR,
@@ -2949,12 +2948,6 @@ byteaSetByte(PG_FUNCTION_ARGS)
 						n, len - 1)));
 
 	/*
-	 * Make a copy of the original varlena.
-	 */
-	res = (bytea *) palloc(VARSIZE(v));
-	memcpy((char *) res, (char *) v, VARSIZE(v));
-
-	/*
 	 * Now set the byte.
 	 */
 	((unsigned char *) VARDATA(res))[n] = newByte;
@@ -2973,17 +2966,16 @@ byteaSetByte(PG_FUNCTION_ARGS)
 Datum
 byteaSetBit(PG_FUNCTION_ARGS)
 {
-	bytea	   *v = PG_GETARG_BYTEA_P(0);
+	bytea	   *res = PG_GETARG_BYTEA_P_COPY(0);
 	int32		n = PG_GETARG_INT32(1);
 	int32		newBit = PG_GETARG_INT32(2);
-	bytea	   *res;
 	int			len;
 	int			oldByte,
 				newByte;
 	int			byteNo,
 				bitNo;
 
-	len = VARSIZE(v) - VARHDRSZ;
+	len = VARSIZE(res) - VARHDRSZ;
 
 	if (n < 0 || n >= len * 8)
 		ereport(ERROR,
@@ -3003,12 +2995,6 @@ byteaSetBit(PG_FUNCTION_ARGS)
 				 errmsg("new bit must be 0 or 1")));
 
 	/*
-	 * Make a copy of the original varlena.
-	 */
-	res = (bytea *) palloc(VARSIZE(v));
-	memcpy((char *) res, (char *) v, VARSIZE(v));
-
-	/*
 	 * Update the byte.
 	 */
 	oldByte = ((unsigned char *) VARDATA(res))[byteNo];
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index e8bce3b..aae9f6a 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -387,7 +387,7 @@ xml_send(PG_FUNCTION_ARGS)
 static void
 appendStringInfoText(StringInfo str, const text *t)
 {
-	appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
+	appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
 }
 #endif
 
@@ -420,9 +420,9 @@ Datum
 xmlcomment(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *arg = PG_GETARG_TEXT_P(0);
-	char	   *argdata = VARDATA(arg);
-	int			len = VARSIZE(arg) - VARHDRSZ;
+	text	   *arg = PG_GETARG_TEXT_PP(0);
+	char	   *argdata = VARDATA_ANY(arg);
+	int			len = VARSIZE_ANY_EXHDR(arg);
 	StringInfoData buf;
 	int			i;
 
@@ -544,7 +544,7 @@ xmlconcat2(PG_FUNCTION_ARGS)
 Datum
 texttoxml(PG_FUNCTION_ARGS)
 {
-	text	   *data = PG_GETARG_TEXT_P(0);
+	text	   *data = PG_GETARG_TEXT_PP(0);
 
 	PG_RETURN_XML_P(xmlparse(data, xmloption, true));
 }
@@ -1343,7 +1343,7 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
 	volatile xmlParserCtxtPtr ctxt = NULL;
 	volatile xmlDocPtr doc = NULL;
 
-	len = VARSIZE(data) - VARHDRSZ;		/* will be useful later */
+	len = VARSIZE_ANY_EXHDR(data);		/* will be useful later */
 	string = xml_text2xmlChar(data);
 
 	utf8string = pg_do_encoding_conversion(string,
@@ -3805,7 +3805,7 @@ xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
 
 	datastr = VARDATA(data);
 	len = VARSIZE(data) - VARHDRSZ;
-	xpath_len = VARSIZE(xpath_expr_text) - VARHDRSZ;
+	xpath_len = VARSIZE_ANY_EXHDR(xpath_expr_text);
 	if (xpath_len == 0)
 		ereport(ERROR,
 				(errcode(ERRCODE_DATA_EXCEPTION),
@@ -3816,7 +3816,7 @@ xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
 	string[len] = '\0';
 
 	xpath_expr = (xmlChar *) palloc((xpath_len + 1) * sizeof(xmlChar));
-	memcpy(xpath_expr, VARDATA(xpath_expr_text), xpath_len);
+	memcpy(xpath_expr, VARDATA_ANY(xpath_expr_text), xpath_len);
 	xpath_expr[xpath_len] = '\0';
 
 	xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
@@ -3935,7 +3935,7 @@ Datum
 xpath(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *xpath_expr_text = PG_GETARG_TEXT_P(0);
+	text	   *xpath_expr_text = PG_GETARG_TEXT_PP(0);
 	xmltype    *data = PG_GETARG_XML_P(1);
 	ArrayType  *namespaces = PG_GETARG_ARRAYTYPE_P(2);
 	ArrayBuildState *astate;
@@ -3958,7 +3958,7 @@ Datum
 xmlexists(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *xpath_expr_text = PG_GETARG_TEXT_P(0);
+	text	   *xpath_expr_text = PG_GETARG_TEXT_PP(0);
 	xmltype    *data = PG_GETARG_XML_P(1);
 	int			res_nitems;
 
@@ -3981,7 +3981,7 @@ Datum
 xpath_exists(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *xpath_expr_text = PG_GETARG_TEXT_P(0);
+	text	   *xpath_expr_text = PG_GETARG_TEXT_PP(0);
 	xmltype    *data = PG_GETARG_XML_P(1);
 	ArrayType  *namespaces = PG_GETARG_ARRAYTYPE_P(2);
 	int			res_nitems;
@@ -4031,7 +4031,7 @@ Datum
 xml_is_well_formed(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *data = PG_GETARG_TEXT_P(0);
+	text	   *data = PG_GETARG_TEXT_PP(0);
 
 	PG_RETURN_BOOL(wellformed_xml(data, xmloption));
 #else
@@ -4044,7 +4044,7 @@ Datum
 xml_is_well_formed_document(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *data = PG_GETARG_TEXT_P(0);
+	text	   *data = PG_GETARG_TEXT_PP(0);
 
 	PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
 #else
@@ -4057,7 +4057,7 @@ Datum
 xml_is_well_formed_content(PG_FUNCTION_ARGS)
 {
 #ifdef USE_LIBXML
-	text	   *data = PG_GETARG_TEXT_P(0);
+	text	   *data = PG_GETARG_TEXT_PP(0);
 
 	PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
 #else
diff --git a/src/backend/utils/misc/rls.c b/src/backend/utils/misc/rls.c
index faf1599..49c03d3 100644
--- a/src/backend/utils/misc/rls.c
+++ b/src/backend/utils/misc/rls.c
@@ -154,7 +154,7 @@ Datum
 row_security_active_name(PG_FUNCTION_ARGS)
 {
 	/* By qualified name */
-	text	   *tablename = PG_GETARG_TEXT_P(0);
+	text	   *tablename = PG_GETARG_TEXT_PP(0);
 	RangeVar   *tablerel;
 	Oid			tableoid;
 	int			rls_status;
diff --git a/src/pl/plperl/Util.xs b/src/pl/plperl/Util.xs
index 8c3c47f..dbba0d7 100644
--- a/src/pl/plperl/Util.xs
+++ b/src/pl/plperl/Util.xs
@@ -116,7 +116,7 @@ util_quote_literal(sv)
     }
     else {
         text *arg = sv2text(sv);
-		text *quoted = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(arg)));
+		text *quoted = DatumGetTextPP(DirectFunctionCall1(quote_literal, PointerGetDatum(arg)));
 		char *str;
 
 		pfree(arg);
@@ -138,7 +138,7 @@ util_quote_nullable(sv)
     else
 	{
         text *arg = sv2text(sv);
-		text *quoted = DatumGetTextP(DirectFunctionCall1(quote_nullable, PointerGetDatum(arg)));
+		text *quoted = DatumGetTextPP(DirectFunctionCall1(quote_nullable, PointerGetDatum(arg)));
 		char *str;
 
 		pfree(arg);
@@ -158,7 +158,7 @@ util_quote_ident(sv)
 		char *str;
     CODE:
         arg = sv2text(sv);
-		quoted = DatumGetTextP(DirectFunctionCall1(quote_ident, PointerGetDatum(arg)));
+		quoted = DatumGetTextPP(DirectFunctionCall1(quote_ident, PointerGetDatum(arg)));
 
 		pfree(arg);
 		str = text_to_cstring(quoted);
@@ -175,9 +175,9 @@ util_decode_bytea(sv)
         text *ret;
     CODE:
         arg = SvPVbyte_nolen(sv);
-        ret = DatumGetTextP(DirectFunctionCall1(byteain, PointerGetDatum(arg)));
+        ret = DatumGetTextPP(DirectFunctionCall1(byteain, PointerGetDatum(arg)));
         /* not cstr2sv because this is raw bytes not utf8'able */
-        RETVAL = newSVpvn(VARDATA(ret), (VARSIZE(ret) - VARHDRSZ));
+        RETVAL = newSVpvn(VARDATA_ANY(ret), VARSIZE_ANY_EXHDR(ret));
     OUTPUT:
     RETVAL
 
diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c
index 06743e4..2962d5a 100644
--- a/src/pl/plpython/plpy_typeio.c
+++ b/src/pl/plpython/plpy_typeio.c
@@ -603,9 +603,9 @@ PLyLong_FromOid(PLyDatumToOb *arg, Datum d)
 static PyObject *
 PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d)
 {
-	text	   *txt = DatumGetByteaP(d);
-	char	   *str = VARDATA(txt);
-	size_t		size = VARSIZE(txt) - VARHDRSZ;
+	text	   *txt = DatumGetByteaPP(d);
+	char	   *str = VARDATA_ANY(txt);
+	size_t		size = VARSIZE_ANY_EXHDR(txt);
 
 	return PyBytes_FromStringAndSize(str, size);
 }
