Unquoted SQL identifiers in `spec.streams` publication sync and connection pooler lookup

Open
#3,256 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, kubernetes, postgresql
Domain
backend, databases

Research direction

Start with the publication sync in pkg/cluster/streams.go and SQL templates and pooler lookup in pkg/cluster/database.go; compare their identifier handling with role quoting in pkg/util/users/users.go. Check how pg_publication_tables represents identifiers and review existing tests around publication sync and pooler lookup. Done means mixed-case, hyphenated, and reserved-word names work correctly and unchanged publications are not altered on every sync.

Written by the indexing model from the issue text.

Description

When syncing publications for spec.streams, pkg/cluster/streams.go#L116-L124 formats schemaName and tableName without double quotes (fmt.Sprintf("%s.%s", schemaName, tableName)) and interpolates that list directly into CREATE PUBLICATION and ALTER PUBLICATION at pkg/cluster/database.go#L54-L55:

createPublicationSQL = `CREATE PUBLICATION "%s" FOR TABLE %s WITH (publish = 'insert, update');`
alterPublicationSQL  = `ALTER PUBLICATION "%s" SET TABLE %s;`

Even though the CRD schema (pkg/apis/acid.zalan.do/v1/crds.go#L533) allows uppercase letters and hyphens in spec.streams[].tables keys, PostgreSQL folds unquoted mixed-case identifiers (such as public.Orders) to lowercase public.orders (ERROR: relation "public.orders" does not exist) and rejects hyphenated or reserved-word table names (such as public.order-events or public.user) with SQLSTATE 42601. Putting literal quotes in the manifest key does not work either because pkg/cluster/database.go#L49-L53 reads unquoted pt.schemaname || '.' || pt.tablename from pg_publication_tables, so currentTables != tableList never matches and re-runs ALTER PUBLICATION on every sync.

Similarly, in the same file at pkg/cluster/database.go#L76-L95, connectionPoolerLookup substitutes {{.pooler_schema}} and {{.pooler_user}} without double quotes (whereas pkg/util/users/users.go#L148 and pkg/cluster/database.go#L70-L74 quote role names), causing PostgreSQL to fold mixed-case pooler role/schema names to lowercase (ERROR: role "..." does not exist) or fail with a syntax error on hyphenated names.

Dominant language
Go
Stars
5.3k
Forks
1.1k
Avg merge
3d 17h
Merged PRs (30d)
11

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zalando/postgres-operator

All issues in zalando/postgres-operator

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.