@@ -572,8 +572,8 @@ makeJsonLexContext(text *json, bool need_escapes)
572
572
* limited in size to NAMEDATALEN and the number of keys is unlikely to
573
573
* be so huge that it has major memory implications.
574
574
*/
575
- Datum
576
- jsonb_object_keys ( PG_FUNCTION_ARGS )
575
+ static Datum
576
+ jsonb_extract_keys_internal ( FunctionCallInfo fcinfo , bool outermost )
577
577
{
578
578
FuncCallContext * funcctx ;
579
579
OkeysState * state ;
@@ -587,18 +587,25 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
587
587
JsonbValue v ;
588
588
JsonbIteratorToken r ;
589
589
590
- if (JB_ROOT_IS_SCALAR (jb ))
591
- ereport (ERROR ,
592
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
593
- errmsg ("cannot call %s on a scalar" ,
594
- JSONB "_object_keys" )));
595
- else if (JB_ROOT_IS_ARRAY (jb ))
596
- ereport (ERROR ,
597
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
598
- errmsg ("cannot call %s on an array" ,
599
- JSONB "_object_keys" )));
590
+ if (outermost )
591
+ {
592
+ if (JB_ROOT_IS_SCALAR (jb ))
593
+ ereport (ERROR ,
594
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
595
+ errmsg ("cannot call %s on a scalar" ,
596
+ JSONB "_object_keys" )));
597
+ else if (JB_ROOT_IS_ARRAY (jb ))
598
+ ereport (ERROR ,
599
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
600
+ errmsg ("cannot call %s on an array" ,
601
+ JSONB "_object_keys" )));
602
+ }
600
603
601
604
funcctx = SRF_FIRSTCALL_INIT ();
605
+
606
+ if (!outermost && JB_ROOT_IS_SCALAR (jb ))
607
+ SRF_RETURN_DONE (funcctx );
608
+
602
609
oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
603
610
604
611
state = palloc (sizeof (OkeysState ));
@@ -614,7 +621,7 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
614
621
615
622
while ((r = JsonbIteratorNext (& it , & v , skipNested )) != WJB_DONE )
616
623
{
617
- skipNested = true ;
624
+ skipNested = outermost ;
618
625
619
626
if (r == WJB_KEY )
620
627
{
@@ -650,6 +657,18 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
650
657
SRF_RETURN_DONE (funcctx );
651
658
}
652
659
660
+ Datum
661
+ jsonb_object_keys (PG_FUNCTION_ARGS )
662
+ {
663
+ return jsonb_extract_keys_internal (fcinfo , true);
664
+ }
665
+
666
+ Datum
667
+ jsonb_extract_keys (PG_FUNCTION_ARGS )
668
+ {
669
+ return jsonb_extract_keys_internal (fcinfo , false);
670
+ }
671
+
653
672
#ifndef JSON_C
654
673
/*
655
674
* Report a JSON error.
0 commit comments