Support PostgreSQL 19

PG19 changed the signatures of pushJsonbValue(), array_iter_setup() and
array_iter_next(), made the varatt.h accessors take a pointer, made
post_parse_analyze_hook take a const JumbleState, no longer includes
utils/hsearch.h from other headers, and renamed AssertVariableIsOfType()
to StaticAssertVariableIsOfType().

--- a/hstore/hstore_pllua.c
+++ b/hstore/hstore_pllua.c
@@ -13,6 +13,13 @@ PG_MODULE_MAGIC;
 #define PG_GETARG_HSTORE_P(h_) PG_GETARG_HS(h_)
 #endif
 
+/* PG19 renamed AssertVariableIsOfType() to StaticAssertVariableIsOfType() */
+#if PG_VERSION_NUM >= 190000
+#define EXTVARTYPE(n_) StaticAssertVariableIsOfTypeMacro(&n_, EXTFUNCT(n_))
+#else
+#define EXTVARTYPE(n_) AssertVariableIsOfTypeMacro(&n_, EXTFUNCT(n_))
+#endif
+
 extern void _PG_init(void);
 
 /* Linkage to functions in hstore module */
@@ -49,9 +56,9 @@ _PG_init(void)
 #define EXTFUNCT(xp_) xp_##_t
 #define EXTFUNCP(xp_) xp_##_p
 #define EXTFUNC(lib_, n_)									\
-	AssertVariableIsOfType(&n_, EXTFUNCT(n_));				\
-	EXTFUNCP(n_) = (EXTFUNCT(n_))							\
-		load_external_function(lib_, EXTFUNCS(n_), true, NULL);
+	(EXTVARTYPE(n_),										\
+	 EXTFUNCP(n_) = (EXTFUNCT(n_))							\
+		load_external_function(lib_, EXTFUNCS(n_), true, NULL));
 
 	EXTFUNC("$libdir/hstore", hstoreUpgrade);
 	EXTFUNC("$libdir/hstore", hstoreUniquePairs);
--- a/src/datum.c
+++ b/src/datum.c
@@ -34,6 +34,22 @@
 #define DatumGetAnyArrayP(d_) DatumGetAnyArray(d_)
 #endif
 
+/*
+ * In PG19, array_iter_setup() takes the element type's length/byval/align,
+ * and array_iter_next() takes only the element index.
+ */
+#if PG_VERSION_NUM >= 190000
+#define PLLUA_ARRAY_ITER_SETUP(it, arr, len, byval, align) \
+	array_iter_setup((it), (arr), (len), (byval), (align))
+#define PLLUA_ARRAY_ITER_NEXT(it, isnull, i, len, byval, align) \
+	array_iter_next((it), (isnull), (i))
+#else
+#define PLLUA_ARRAY_ITER_SETUP(it, arr, len, byval, align) \
+	array_iter_setup((it), (arr))
+#define PLLUA_ARRAY_ITER_NEXT(it, isnull, i, len, byval, align) \
+	array_iter_next((it), (isnull), (i), (len), (byval), (align))
+#endif
+
 /*
  * Basic plan of attack:
  *
@@ -150,8 +166,9 @@ static Datum pllua_detoast_light(lua_State *L, Datum d)
 {
 	volatile Datum nd;
 
-	if (!VARATT_IS_EXTENDED(d)
-		|| (VARATT_IS_SHORT(d) && !VARATT_IS_EXTERNAL(d)))
+	if (!VARATT_IS_EXTENDED(DatumGetPointer(d))
+		|| (VARATT_IS_SHORT(DatumGetPointer(d))
+			&& !VARATT_IS_EXTERNAL(DatumGetPointer(d))))
 		return d;
 
 	PLLUA_TRY();
@@ -226,7 +243,7 @@ pllua_get_tuple_type(lua_State *L, Datum value, Oid *typeid, int32 *typmod)
 	if (typmod)
 		*typmod = -1;
 
-	if (VARATT_IS_EXTENDED(value))
+	if (VARATT_IS_EXTENDED(DatumGetPointer(value)))
 	{
 		PLLUA_TRY();
 		{
@@ -276,7 +293,8 @@ int pllua_value_from_datum(lua_State *L,
 		case BYTEAOID:
 			{
 				Datum v = pllua_detoast_light(L, value);
-				lua_pushlstring(L, VARDATA_ANY(v), VARSIZE_ANY_EXHDR(v));
+				lua_pushlstring(L, VARDATA_ANY(DatumGetPointer(v)),
+								VARSIZE_ANY_EXHDR(DatumGetPointer(v)));
 			}
 			return LUA_TSTRING;
 
@@ -330,7 +348,8 @@ int pllua_value_from_datum(lua_State *L,
 			lua_pushcfunction(L, pllua_spi_newcursor);
 			{
 				Datum v = pllua_detoast_light(L, value);
-				lua_pushlstring(L, VARDATA_ANY(v), VARSIZE_ANY_EXHDR(v));
+				lua_pushlstring(L, VARDATA_ANY(DatumGetPointer(v)),
+								VARSIZE_ANY_EXHDR(DatumGetPointer(v)));
 			}
 			lua_call(L, 1, 1);
 			return LUA_TUSERDATA;
@@ -2294,7 +2313,8 @@ pllua_datum_array_map(lua_State *L)
 	 * to the right depth.
 	 */
 
-	array_iter_setup(&iter, (AnyArrayType *) arr);
+	PLLUA_ARRAY_ITER_SETUP(&iter, (AnyArrayType *) arr,
+						   et->typlen, et->typbyval, et->typalign);
 
 	for (nstack = 0, index = 0; index < nelems; ++index)
 	{
@@ -2310,8 +2330,8 @@ pllua_datum_array_map(lua_State *L)
 			++nstack;
 		}
 
-		val = array_iter_next(&iter, &isnull, index,
-							  et->typlen, et->typbyval, et->typalign);
+		val = PLLUA_ARRAY_ITER_NEXT(&iter, &isnull, index,
+									et->typlen, et->typbyval, et->typalign);
 
 		pllua_datum_single(L, val, isnull, lua_upvalueindex(2), et);
 
@@ -3582,7 +3602,10 @@ static void pllua_typeinfo_raw_coerce_array(lua_State *L, Datum *val, bool *valn
 		if (OidIsValid(fnoid2) && (!fn2 || !OidIsValid(fn->fn_oid)))
 			fn2 = pllua_pgfunc_init(L, nf2, fnoid2, -1, NULL, InvalidOid);
 
-		array_iter_setup(&iter, arr);
+		PLLUA_ARRAY_ITER_SETUP(&iter, arr,
+							   st->elemtyplen,
+							   st->elemtypbyval,
+							   st->elemtypalign);
 
 		if (elempath == COERCION_PATH_FUNC)
 			InitFunctionCallInfoData(*fcinfo, fn, 3, InvalidOid, NULL, NULL);
@@ -3591,12 +3614,12 @@ static void pllua_typeinfo_raw_coerce_array(lua_State *L, Datum *val, bool *valn
 
 		for (idx = 0; idx < nitems; ++idx)
 		{
-			LFCI_ARG_VALUE(fcinfo,0) = array_iter_next(&iter,
-													   &LFCI_ARGISNULL(fcinfo,0),
-													   idx,
-													   st->elemtyplen,
-													   st->elemtypbyval,
-													   st->elemtypalign);
+			LFCI_ARG_VALUE(fcinfo,0) = PLLUA_ARRAY_ITER_NEXT(&iter,
+															 &LFCI_ARGISNULL(fcinfo,0),
+															 idx,
+															 st->elemtyplen,
+															 st->elemtypbyval,
+															 st->elemtypalign);
 
 			if (elempath == COERCION_PATH_RELABELTYPE)
 			{
--- a/src/init.c
+++ b/src/init.c
@@ -11,6 +11,7 @@
 #include "catalog/pg_proc.h"
 #include "nodes/pg_list.h"
 #include "storage/ipc.h"
+#include "utils/hsearch.h"
 #include "utils/inval.h"
 #include "utils/syscache.h"
 
--- a/src/jsonb.c
+++ b/src/jsonb.c
@@ -19,6 +19,32 @@
 #define DatumGetJsonbP(d_) DatumGetJsonb(d_)
 #endif
 
+/*
+ * PG19 changed pushJsonbValue() to take a JsonbInState and to return void,
+ * with the completed value left in pstate->result.  Wrap both APIs so the
+ * caller doesn't need to care.
+ */
+#if PG_VERSION_NUM >= 190000
+typedef JsonbInState pllua_jsonb_state;
+
+static inline JsonbValue *
+pllua_push_jsonb_value(pllua_jsonb_state *pstate, JsonbIteratorToken tok,
+					   JsonbValue *val)
+{
+	pushJsonbValue(pstate, tok, val);
+	return pstate->result;
+}
+#else
+typedef JsonbParseState *pllua_jsonb_state;
+
+static inline JsonbValue *
+pllua_push_jsonb_value(pllua_jsonb_state *pstate, JsonbIteratorToken tok,
+					   JsonbValue *val)
+{
+	return pushJsonbValue(pstate, tok, val);
+}
+#endif
+
 /*
  * called with the container value on top of the stack
  *
@@ -316,7 +342,7 @@ pllua_jsonb_tosql(lua_State *L)
 	int funcidx = 0;
 	int array_thresh = 1000;
 	int array_frac = 1000;
-	JsonbParseState *pstate = NULL;
+	pllua_jsonb_state pstate = {0};
 	JsonbValue nullval;
 	JsonbValue curval;
 	MemoryContext tmpcxt;
@@ -371,6 +397,9 @@ pllua_jsonb_tosql(lua_State *L)
 
 	tmpcxt = pllua_newmemcontext(L, "pllua jsonb temp context",
 								 ALLOCSET_START_SMALL_SIZES);
+#if PG_VERSION_NUM >= 190000
+	pstate.outcontext = tmpcxt;
+#endif
 
 	if (lua_rawequal(L, 1, nullvalue))
 	{
@@ -412,7 +441,7 @@ pllua_jsonb_tosql(lua_State *L)
 		PLLUA_TRY();
 		{
 			MemoryContext oldcontext = MemoryContextSwitchTo(tmpcxt);
-			pushJsonbValue(&pstate, tok, NULL);
+			pllua_push_jsonb_value(&pstate, tok, NULL);
 			MemoryContextSwitchTo(oldcontext);
 		}
 		PLLUA_CATCH_RETHROW();
@@ -453,7 +482,7 @@ pllua_jsonb_tosql(lua_State *L)
 				PLLUA_TRY();
 				{
 					MemoryContext oldcontext = MemoryContextSwitchTo(tmpcxt);
-					result = pushJsonbValue(&pstate, tok, NULL);
+					result = pllua_push_jsonb_value(&pstate, tok, NULL);
 					MemoryContextSwitchTo(oldcontext);
 				}
 				PLLUA_CATCH_RETHROW();
@@ -478,7 +507,7 @@ pllua_jsonb_tosql(lua_State *L)
 						int prevkey = lua_tointeger(L, -4);
 						while (++prevkey != key)
 						{
-							pushJsonbValue(&pstate, WJB_ELEM, &nullval);
+							pllua_push_jsonb_value(&pstate, WJB_ELEM, &nullval);
 						}
 						lua_pushinteger(L, key);
 						lua_replace(L, -5);
@@ -493,7 +522,7 @@ pllua_jsonb_tosql(lua_State *L)
 						curval.val.string.len = len;
 						memcpy(curval.val.string.val, ptr, len);
 						pg_verifymbstr(curval.val.string.val, len, false);
-						pushJsonbValue(&pstate, WJB_KEY, &curval);
+						pllua_push_jsonb_value(&pstate, WJB_KEY, &curval);
 						tok = WJB_VALUE;
 					}
 
@@ -530,7 +559,7 @@ pllua_jsonb_tosql(lua_State *L)
 				PLLUA_TRY();
 				{
 					MemoryContext oldcontext = MemoryContextSwitchTo(tmpcxt);
-					pushJsonbValue(&pstate, tok, pval);
+					pllua_push_jsonb_value(&pstate, tok, pval);
 					MemoryContextSwitchTo(oldcontext);
 				}
 				PLLUA_CATCH_RETHROW();
--- a/src/spi.c
+++ b/src/spi.c
@@ -248,7 +248,17 @@ static int pllua_cursor_options(lua_State *L, int nd, int *fetch_count)
 static int pllua_spi_prepare_recursion = -1;
 static post_parse_analyze_hook_type pllua_spi_prev_parse_hook = NULL;
 
-#if PG_VERSION_NUM >= 140000
+#if PG_VERSION_NUM >= 190000
+static void pllua_spi_prepare_checkparam_hook(ParseState *pstate,
+											  Query *query,
+											  const JumbleState *jstate)
+{
+	if (pllua_spi_prepare_recursion == 1)
+		check_variable_parameters(pstate, query);
+	if (pllua_spi_prev_parse_hook)
+		pllua_spi_prev_parse_hook(pstate, query, jstate);
+}
+#elif PG_VERSION_NUM >= 140000
 static void pllua_spi_prepare_checkparam_hook(ParseState *pstate,
 											  Query *query,
 											  JumbleState *jstate)
