pgsync icon indicating copy to clipboard operation
pgsync copied to clipboard

Index Children only with based Condition

Open kundankumarcasio opened this issue 2 years ago • 9 comments

PGSync version: latest

Postgres version: 15.2

Elasticsearch/OpenSearch version: 7.17.13

Redis version: 7.2

Python version: 3.8

Problem Description: I want to index children with given condition [ { "database": "databaseName", "index": "indexName", "nodes": { "table": "parentTable", "columns": [], "children": [ { "table": "childTable", "columns": [], "relationship": { "variant": "object", "type": "one_to_many", "foreign_key": { "child": [ "parent_id" ], "parent": [ "id" ], "condition": { "child_column1": "some Value" //need help here } } } } ] } } ]

Equivalent SQL Query: select * from parentTable join childTable where parentTable.id=childTable.parent_id where child_column1="some Value"

Error Message (if any):



kundankumarcasio avatar Dec 26 '23 11:12 kundankumarcasio

I wonder if this could also be achieved within PGSync's plugin API? That way, conditions can be more expressive. Something like:

from pgsync import plugin


class FooPlugin(plugin.Plugin):
    name = 'FooPlugin'

    def transform(self, doc, **kwargs):
       ...

    def should_index(self, doc, **kwargs):
        return doc['childTable']['parent_id'] == 'someValue'

jvanderen1 avatar Jan 05 '24 17:01 jvanderen1

thank you very much. i will try this.

kundankumarcasio avatar Jan 08 '24 07:01 kundankumarcasio

I don't think an API exists today for something like this. However, it could be an enhancement.

jvanderen1 avatar Jan 08 '24 16:01 jvanderen1

yes. Any work around for this ?

kundankumarcasio avatar Jan 08 '24 17:01 kundankumarcasio

You can probably write a plugin like the following:

from pgsync import plugin


class FooPlugin(plugin.Plugin):
    name = 'FooPlugin'

    def transform(self, doc, **kwargs):
      return doc if doc['childTable']['parent_id'] == 'someValue' else None

But I am concerned that depending on the condition, this won't necessarily insert/delete documents as expected. It may work for your use case though.

jvanderen1 avatar Jan 09 '24 05:01 jvanderen1

I tried this and it worked, but this creates some sync inconsistencies.

kundankumarcasio avatar Jan 09 '24 05:01 kundankumarcasio

This would be a great enhancement to add for things like soft-deleted or archived records.

jvanderen1 avatar Feb 27 '24 15:02 jvanderen1