Store and index YAML with PostgreSQL, with Javascr

2019-04-14 19:13发布

问题:

PostgreSQL 9.2 has native JSON support. I'd like to store human readable config files, however, in YAML. And I think I'd like to index a few (but not all) of the config file values. Therefore I'm wondering:

  1. Is it's somehow possible to include [a third party Javascript library that parses Yaml] in Postgres, for example js-yaml. Then I could have my own YAML Javascript helper, in the same way as there's the built-in JSON helper in PostgreSQL 9.2.

Alternatively:

  1. is it possible to declare individual reusable Javascript functions? If so, then I could add my own YAML parsing functions (based on simple regexps), that are able to parse a subset of YAML, for example the top level key-value pairs here:

    # some "top level key-value paris":
    the_key: 'the value'
    another_key: 'another value'
    # But this however:
    would_be_too_complicated_to_parse_manually_with_regexps: |
      block string
      with newlines
    

Worst case scenario would be that I'd need to duplicate YAML parsing code in each PostgreSQL stored procedure (if I cannot add 3rd party libraries or declare reusable functions).

(Performance wouldn't be terribly important in my case.)

(I've googled a while for "postgresql plv8 reusable function" and "postgresql plv8 library" but found nothing of relevance)

回答1:

The pl/v8 procedural language is probably the way to go. It's a 'trusted' language, which means (among other things) it does not provide any way to do the 'load an external module from this file' thing. But it does have a 'find_function()' method to let you define your own javascript function and call it from another function (js or not). See description of it in this blog post:
http://umitanuki.hatenablog.com/entry/2012/04/10/171935