Skip to content

[Bug]: monolith import may create unintended default database #7731

Description

@jsilvela

Is there an existing issue already for this bug?

  • I have searched for an existing issue, and could not find anything. I believe this is a new bug.

I have read the troubleshooting guide

  • I have read the troubleshooting guide and I think this is a new bug.

I am running a supported version of CloudNativePG

  • I have read the troubleshooting guide and I think this is a new bug.

Contact Details

[email protected]

Version

1.26 (latest patch)

What version of Kubernetes are you using?

1.33

What is your Kubernetes environment?

Self-managed: kind (evaluation)

How did you install the operator?

YAML manifest

What happened?

Investigating #7447 , the issue opened by @smiyc , I agree with his assessment that it is a bug,
and moreover I find the mis-behavior bigger than just that bug.

In this description I discuss the problem, compare with microservice approach, and propose the fix.
Section headings to hopefully aid navigation.

monolith import may take in N source databases and create N + 1, or alter the "app" database owner

To recap from #7447 , when using monolith import, a (database, owner) of ("app", "xxx") results in an imported database of ("app", "app"), whereas a (database, owner) of ("foo", "xxx") is imported, as expected, as ("foo", "xxx").

Now, in the second case, the monolith import doesn't create only ("foo", "xxx"), but adds the default ("app", "app") too.

Doing a kubectl get -o yaml of the running cluster reveals:

spec:
  <snipped>
  bootstrap:
    initdb:
      database: app
      import:
        databases:
        - foo
        roles:
        - xxx
        source:
          externalCluster: cluster-other
        type: monolith
      owner: app

In the documentation for the monolith import (https://cloudnative-pg.io/documentation/current/database_import/#the-monolith-type), the expectation is that N .initdb.import.databases will result in N databases in the created cluster.
This example shows this is not the case.

The cause is the defaulting webhook, which will create the default (database, user) if there are no initdb.database and initdb.owner specified.

comparison with microservice

The defaulting webhook behavior makes sense for the microservice approach.
There, a single DB from the source cluster is chosen to be imported, and in the target cluster will result in ("app", "app") by default, unless the user specifies a (database, owner).

For monolith this does not make sense. Firstly, monolith is more useful for N databases to import rather than just one.
And in addition, the monolith approach seems tailor made for copying from the source cluster as faithfully as possible.

proposal

The right fix, in my view, is that the defaulting webhook should not pre-populate the database/owner, if there is a monolith import specified.

Alternatives considered:

Force the user of monolith imports to specify explicitly which of the imported databases is "default" via the initdb.database and initdb.owner.

Also, the documentation needs some updating. And ... we don't have a sample file for monolith import, which shows that it's been taken less seriously than microservice.

Cluster resource

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example-new-monolith
spec:
  instances: 1
  
  storage:
    size: 1Gi

  bootstrap:
    initdb:
      import:
        type: monolith
        databases:
          - foo
        roles:
          - xxx
        source:
          externalCluster: cluster-example

  externalClusters:
    - name: cluster-example
      connectionParameters:
        host: cluster-example-rw
        dbname: postgres

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    triage
    on Jun 5, 2025
  2. dosubot commented on Jun 5, 2025

    @dosubot

    Hi @jsilvela! I'm Dosu and I’m helping the cloudnative-pg team.

    You are correct—there are no existing issues or discussions about this bug. The current implementation of the defaulting webhook sets the default database and owner to "app" if they are not specified, regardless of whether a monolith import is being used. This leads to the unintended creation of an extra default database when using monolith imports, which is not the expected behavior according to the documentation and typical use case for monolith imports (source).

    Your proposal to change the defaulting webhook so it does not pre-populate the database/owner fields when a monolith import is specified is reasonable and aligns with the documented intent of the monolith import process (source). The documentation also does not mention any defaulting behavior for these fields in the context of monolith imports, which supports your suggestion for both a code and documentation update.

    Let me know if you want more details on the relevant code paths or want to discuss possible workarounds until this is addressed.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about cloudnative-pg with Dosu Add Dosu to your team

  3. added this to the 1.27.0 milestone on Jun 11, 2025
  4. armru commented on Jun 11, 2025

    @armru
    Member

    Hello Jaime, thanks for the Issue and PR!

    I agree that the monolith approach should do a carbon copy of the database.

    I would love to hear the other mantainers opinion on this, I'm also adding this issue to the 1.27 milestone so we are sure to have this discussion.

  5. modified the milestones: 1.27.0, 1.26.1 on Jul 21, 2025
  6. added
    bug 🐛
    and removed
    triage
    on Jul 21, 2025
  7. moved this to Waiting for First Review in CloudNativePG operator developmenton Jul 21, 2025
  8. added a commit that references this issue on Jul 25, 2025
    25db589
  9. moved this from Waiting for First Review to Done in CloudNativePG operator developmenton Jul 25, 2025
  10. added 2 commits that reference this issue on Jul 25, 2025
    1da3a53
    8f69f3a
  11. added a commit that references this issue on Jul 25, 2025
    e35085a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions