+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 and lookup value data */
+ JsonbValue jsonb_lookup_value;
+ JsonbValue *jsonb_value = NULL;
+ JsonbIterator *jsonb_iterator;
+ JsonbValue jsonb_iterator_value;
+ int32 jsonb_iterator_token;
+
+ /* variables used for skip logic */
+ int32 skip_key = 0;
+ int32 nest_level = 0;
+ int32 array_level = 0;
+
+ /* if we are not deaing with an array first check to make sure the key exists - this is potentially just extra unwanted work */
+ if (!JB_ROOT_IS_ARRAY(input_jsonb))
+ {
+ jsonb_lookup_value.type = jbvString;
+ jsonb_lookup_value.val.string.val = VARDATA_ANY(input_text);
+ jsonb_lookup_value.val.string.len = VARSIZE_ANY_EXHDR(input_text);
+
+ jsonb_value = findJsonbValueFromContainer(&input_jsonb->root,
+ JB_FOBJECT | JB_FARRAY, &jsonb_lookup_value);
+
+ if (jsonb_value == NULL)
+ PG_RETURN_JSONB(input_jsonb);
+ }
+
+ /*
+ * If we've been supplied with an existing key iterate round json data and rebuild with key/value 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)
+ {
+ switch (jsonb_iterator_token)
+ {
+ case WJB_BEGIN_ARRAY:
+ array_level++;
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_BEGIN_ARRAY, &jsonb_iterator_value);
+ break;
+ case WJB_BEGIN_OBJECT:
+ nest_level++;
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_BEGIN_OBJECT, &jsonb_iterator_value);
+ break;
+ case WJB_ELEM:
+ /* only match array elements if they are text */
+ if (skip_key == 0 && nest_level == 0 && array_level > 0)
+ {
+ if (jsonb_iterator_value.type == jbvString)
+ {
+ if (strncmp(pnstrdup(jsonb_iterator_value.val.string.val,jsonb_iterator_value.val.string.len),
+ VARDATA_ANY(input_text), VARSIZE_ANY_EXHDR(input_text)) == 0)
+ break;
+ }
+ }
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_ELEM, &jsonb_iterator_value);
+ break;
+ case WJB_KEY:
+ /* Check each key against our array of keys */
+ if (skip_key > 0)
+ {
+ skip_key++;
+ }
+ else if (nest_level == 1)
+ {
+ if (strncmp(pnstrdup(jsonb_iterator_value.val.string.val,jsonb_iterator_value.val.string.len),
+ VARDATA_ANY(input_text), VARSIZE_ANY_EXHDR(input_text)) == 0) {
+ skip_key++;
+ break;
+ }
+ }
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_KEY, &jsonb_iterator_value);
+ break;
+ case WJB_VALUE:
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_VALUE, &jsonb_iterator_value);
+ else if (skip_key > 0)
+ skip_key--;
+ break;
+ case WJB_END_ARRAY:
+ array_level--;
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_END_ARRAY, NULL);
+ else if (skip_key > 0 && array_level == 0)
+ skip_key--;
+ break;
+ case WJB_END_OBJECT:
+ nest_level--;
+ if (skip_key == 0)
+ return_jsonb_value = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
+ else if (skip_key > 0)
+ skip_key--;
+ break;
+ default:
+ elog(ERROR, "invalid JsonbIteratorNext rc: %d", jsonb_iterator_token);
+ }
+ }
+ 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);