audit-trigger icon indicating copy to clipboard operation
audit-trigger copied to clipboard

Just converted from hstore to jsonb

Open michelmilezzi opened this issue 6 years ago • 12 comments

  • Removed hstore dependency;
  • Using jsonb builtin type;
  • Postgres 10+.

michelmilezzi avatar Nov 24 '17 13:11 michelmilezzi

Is actively managing this project?

jackhamburger avatar Sep 06 '18 13:09 jackhamburger

@michelmilezzi Did you run into an issue with PG10 that hstore broke? Or is this removing a unneeded dependency made obsolete by jsonb?

jackhamburger avatar Sep 06 '18 13:09 jackhamburger

@jackhamburger Just removing an unneeded dependency. There was a long time since last activity here, I guess the folks from 2ndQuadrant discontinued this project...

michelmilezzi avatar Sep 14 '18 19:09 michelmilezzi

Moving to jsonb is wise. I'd like to find the time to test and merge this.

It'd be a huge help if you could prepare a test script we could replay to test the extension. One that:

  • creates tables with sensible and "sill 'y' " names in the public schema
  • creates a schema with sensible and "difficult "" names" and puts some tables in there
  • creates the triggers
  • runs some DML
  • queries the results
  • diffs the output vs an expected file

A simple way to do this would be to add a PGXS Makefile and a sql/ and expected/ directory so you could make check, treating it as a PostgreSQL extension. Bonus points for a control file etc.

I will not have time to do this. I can't really merge the jsonb change without some kind of test script but I can't find the time to write one either, so I'm going to have to ask if anyone else @ here is willing.

As for maintenance. This is not and never was a maintained product. It was my personal script I published for others, and re-hosted onto 2ndQ's account later. Like anything else you can contact [email protected] if you're interested in special arrangements, consulting etc. But really this is here as a service to the community because it's better here than on a wiki.

ringerc avatar Jan 03 '19 02:01 ringerc

Ok @ringerc, I'll do that. Thanks for the feedback.

michelmilezzi avatar Jan 03 '19 17:01 michelmilezzi

Hey @michelmilezzi, are you still on this? Would be great to see this merged :)

phryneas avatar Jul 08 '19 13:07 phryneas

@phryneas yes, I do. I'll finish it asap, thanks for the interest :)

michelmilezzi avatar Jul 17 '19 12:07 michelmilezzi

  • Postgres 10+.

@michelmilezzi hi. You said postgres 10+, why?

I can see jsonb is also in Postgres 9.6 -- which I am using (cannot upgrade). I wanted to switch to jsonb to improve my performance.

Is there anything I need to know about it? Thanks

sta-szek avatar Oct 11 '19 08:10 sta-szek

I tried that on my local 9.6 (hstore works) and it throws exception:

Error inserting mock data: SQL Error [42883]: Batch entry 0 INSERT INTO ... VALUES ... was aborted: ERROR: operator does not exist: jsonb - text[]
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Where: PL/pgSQL function cdc.change_data_capture() line 54 at assignment  Call getNextException to see other errors in the batch.

i am not 100% sure, but i guess it complains about the line cdc_row.row_data = row_to_json(NEW)::JSONB - excluded_cols;

sta-szek avatar Oct 11 '19 14:10 sta-szek

I made it working by removing - excluded_cols; from all the places as I don't need this feature.

After testing it turned out that JSONB (at least in 9.6) is ~ 2 times slower than hstore.

10,000 records in table with 69 fields + 7 indices: query: update activity set "timestamp"=CURRENT_TIMESTAMP;

  1. no triggers: ~800ms
  2. hstore: ~2,5s
  3. jsonb: ~4,5s

Just FYI

sta-szek avatar Oct 11 '19 15:10 sta-szek

I also noticed the performance hit when switching from hstore to jsonb. I was able to recover most of that performance by using hstore to calculated changed_fields, then converting that result to jsonb. You can find that modification at https://github.com/michelmilezzi/audit-trigger/pull/1 or https://github.com/2ndQuadrant/audit-trigger/compare/master...DavidBoone:master

DavidBoone avatar Aug 28 '20 22:08 DavidBoone

@sta-szek, @DavidBoone, @ringerc, are there any chance it will be merged?

trnl avatar Oct 16 '20 10:10 trnl

@2ndquadrant-ci any chance we could get this merged?

iloveitaly avatar Aug 20 '23 01:08 iloveitaly

No, I won't merge this PR, as it incompatibly changes the existing definition. Also, last I checked hstore remained considerably more efficient than jsonb, but that was a while ago, maybe it has improved.

I would be happy to accept a PR that adds a json-flavoured extension with the same functionality, but you might as well just host your own and ask me to link to that instead of rewriting this one in that case.

This was really meant to be a demo / proof of concept for people to adopt and adapt to their needs more than it was a canned ready-to-go project for all needs.

ringerc avatar Aug 27 '23 22:08 ringerc