Class PostgresNotificationSchemaGenerator

java.lang.Object
io.micronaut.data.jdbc.notification.postgres.PostgresNotificationSchemaGenerator

@Internal @Singleton @Requires(classes=org.postgresql.PGConnection.class) @Requires(property="micronaut.data.jdbc.notifications.postgres.schema-generate",value="true") public final class PostgresNotificationSchemaGenerator extends Object
Installs PostgreSQL notification functions and triggers during JDBC schema generation when opted in. Supports SchemaGenerate.CREATE and SchemaGenerate.CREATE_DROP.

The configured notification mode selects either row-change notifications with primary keys and invalidation on truncation, or table invalidations for insert, update, delete, and truncate statements. The corresponding bundled SQL resource supplies the function definition.

The shared listener registry supplies channels and generated key mappings. For inherited listeners without generated metadata, key columns are derived from the entity mapping. Neither path instantiates listeners or repository operations during schema generation. Listeners for the same schema, table, and channel share generated triggers. Installation errors fail application startup.

  • Constructor Details

    • PostgresNotificationSchemaGenerator

      public PostgresNotificationSchemaGenerator(ChangeListenerRegistry registry, io.micronaut.context.env.Environment environment, PostgresNotificationConfiguration configuration)
      Creates a generator using the configured notification trigger mode.
      Parameters:
      registry - the discovered listener metadata grouped by datasource
      environment - the mapping placeholder resolver
      configuration - the notification schema-generation settings
  • Method Details

    • generate

      public void generate(Connection connection, DataJdbcConfiguration configuration, PersistentEntity[] entities) throws SQLException
      Installs the selected function and triggers for subscribed PostgreSQL tables included in this schema-generation pass. Does nothing for other dialects or schema-generation modes other than SchemaGenerate.CREATE and SchemaGenerate.CREATE_DROP.

      The function is installed once per subscribed schema in this pass. For each table/channel subscription, all three generated trigger names are dropped before creating the selected mode's triggers. This also replaces existing triggers under CREATE and allows mode changes without leaving the previous mode's triggers active.

      Function and trigger changes in this pass are transactional. An auto-commit connection uses a new transaction; an existing transaction uses a savepoint and remains under the caller's control. Installation failure rolls back these changes. Table creation performed before this pass is outside this transaction boundary.

      Unrelated triggers and triggers for subscriptions absent from this pass are left unchanged. Previously installed functions are not removed when switching modes. Functions and triggers remain installed after application shutdown.

      Parameters:
      connection - the schema-generation connection
      configuration - the datasource configuration
      entities - the entities included in this schema-generation pass
      Throws:
      SQLException - if schema resolution or SQL execution fails
      IllegalArgumentException - if a resolved notification channel is invalid
      IllegalStateException - if listener key mappings are missing or conflicting, or the SQL resource cannot be read