+PG_FUNCTION_INFO_V1(jsonb_delete_key);
+
+/*
+ * Operator function to delete key from left operand where a match is found in
+ * the right operand.
+ *
+ * jsonb, text -> jsonb
+ *
+ */
+Datum
+jsonb_delete_key(PG_FUNCTION_ARGS)
+{
+ /* pointers to incoming jsonb and text data */
+ Jsonb *input_jsonb = PG_GETARG_JSONB(0);
+ text *input_text = PG_GETARG_TEXT_P(1);
+
+ /* pointers to return jsonb value data and state to be converted to jsonb on return */
+ JsonbParseState *state = NULL;
+ JsonbValue *return_jsonb_value = NULL;
+
+ /* pointer to iterator for input_jsonb value data */
+ JsonbIterator *jsonb_iterator;
+ JsonbValue jsonb_iterator_value;
+ int32 jsonb_iterator_token;
+
+ /* variables used for skip logic */
+ int32 skip_level = 0;
+ int32 nest_level = 0;
+ int32 array_level = 0;
+ bool push = true;
+
+ /*
+ * If we've been supplied with an existing key iterate round json data and rebuild
+ * with key/element excluded.
+ *
+ * skip_key, nest_level and array_level are crude counts to check if the the value
+ * for the key is closed and ensure we don't match on keys within nested objects.
+ * Because we are recursing into nested elements but blindly just pushing them onto
+ * the return value we can get away without deeper knowledge of the json?
+ */
+
+ jsonb_iterator = JsonbIteratorInit(&input_jsonb->root);
+
+ while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, false)) != WJB_DONE)
+ {
+ push = true;
+ switch (jsonb_iterator_token)
+ {
+ case WJB_BEGIN_ARRAY:
+ array_level++;
+ break;
+ case WJB_BEGIN_OBJECT:
+ nest_level++;
+ break;
+ case WJB_ELEM:
+ case WJB_KEY:
+ if (skip_level == 0 && ((jsonb_iterator_token == WJB_KEY && nest_level == 1 && array_level == 0) ||
+ (jsonb_iterator_token == WJB_ELEM && nest_level == 0 && array_level == 1)))
+ {
+ if (jsonb_iterator_value.type == jbvString)
+ {
+ if ((jsonb_iterator_value.val.string.len == VARSIZE_ANY_EXHDR(input_text)) &&
+ (memcmp(jsonb_iterator_value.val.string.val,
+ VARDATA_ANY(input_text),
+ jsonb_iterator_value.val.string.len) == 0))
+ {
+
+ if (jsonb_iterator_token == WJB_ELEM)
+ push = false;
+ else
+ skip_level = nest_level;
+ }
+ }
+ }
+ }
+
+ if (push && (skip_level == 0 || nest_level < skip_level))
+ {
+ return_jsonb_value = pushJsonbValueBlind(&state, jsonb_iterator_token, &jsonb_iterator_value);
+ }
+
+ switch (jsonb_iterator_token)
+ {
+ case WJB_END_OBJECT:
+ nest_level--;
+ if (skip_level == nest_level && array_level == 0)
+ skip_level = 0;
+ break;
+ case WJB_END_ARRAY:
+ array_level--;
+ if (skip_level == nest_level && array_level == 0)
+ skip_level = 0;
+ break;
+ case WJB_VALUE:
+ if (skip_level == nest_level)
+ skip_level = 0;
+ }
+ }
+
+ if (JB_ROOT_IS_SCALAR(input_jsonb) && !return_jsonb_value->val.array.rawScalar && return_jsonb_value->val.array.nElems == 1)
+ return_jsonb_value->val.array.rawScalar = true;
+
+ PG_FREE_IF_COPY(input_jsonb, 0);
+ PG_FREE_IF_COPY(input_text, 1);
+
+ PG_RETURN_JSONB(JsonbValueToJsonb(return_jsonb_value));
+}
+
+Datum jsonb_delete_keys(PG_FUNCTION_ARGS);
+
+PG_FUNCTION_INFO_V1(jsonb_delete_keys);