<?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
        <channel>
            <title>pgEdge Posts from Asif Naeem</title>
            <link>https://www.pgedge.com/blog</link>
            <description>The latest pgEdge Posts from Asif Naeem</description>
            <atom:link href="https://www.pgedge.com/feeds/rss/user/asif-naeem/all.xml" rel="self" type="application/rss+xml" />
            <language>en-us</language>         
            
            <item>
            <category>postgres,pgEdge,PostgreSQL</category>
            <title><![CDATA[Simplifying Cluster-Wide SQL Execution in pgEdge with exec_node()]]></title>
            <link>https://www.pgedge.com/blog/simplifying-cluster-wide-sql-execution-in-pgedge-with-exec_node</link>
            <pubDate>Fri, 14 Nov 2025 05:02:00 GMT</pubDate>
            <description><![CDATA[ <p><a href="https://www.pgedge.com/products/what-is-distributed-postgres"><u>pgEdge Distributed Postgres</u></a> is a database system built on top of standard open‑source Postgres, extended to support global, distributed, and <a href="https://www.pgedge.com/solutions/benefit/multi-master"><u>multi‑master</u></a> (active‑active) deployments.In the evolving landscape of distributed databases, efficient query execution across nodes is essential to leverage the full power of a distributed architecture. Specifically for distributed Postgres environments, managing a multi-node cluster often requires executing SQL commands that don’t automatically replicate. This includes critical operations like executing DDL statements, performing administrative tasks, and altering cluster configuration - actions that must be applied only on specific nodes.To solve this operational challenge, I created the function: a utility designed to make remote SQL execution across pgEdge nodes simple, consistent, and scriptable directly from within the database.<h3>Why I Created exec_node()</h3>As part of general administration tasks, there is commonly a need to execute SQL commands on a specific node or all nodes within a pgEdge distributed cluster. pgEdge leverages Spock for logical replication, but many important SQL commands—particularly the following DDL and Spock-specific cluster management functions—do not replicate by design. This includes operations like:<ul></ul><ul></ul><ul><li>-</li><li>VACUUM</li></ul><ul><li>Spock-specific commands like </li><li>spock.repset_add_table</li><li>, </li><li>spock.node_add_interface</li></ul>In a traditional setup, executing these commands safely and consistently across all or specific nodes requires manual logins, scripts, or orchestration tools. This is time-consuming and requires additional steps.<h2>Using exec_node()</h2>With , you can issue commands directly from the database—through SQL—and target the exact node you want. The function signature is:<ul></ul><ul></ul>With you can:Run SQL on Any or All Nodes — Remotely and NativelyWhether you're running a maintenance command, executing DDL statements, or configuring Spock, you can use to do it all from a single SQL interface.Example: Running a data maintenance command statement only on node1:;Example: Applying a maintenance operation across all nodes:;Execute Non-Replicating Commands Where They BelongSome SQL commands are intentionally not replicated in pgEdge; this is either to avoid conflicts or is because they are inherently local. allows these commands to be sent only to the relevant node(s), avoiding misconfiguration or inconsistencies.Common non-replicating commands include:<ul></ul><ul></ul><ul></ul>Example: Setting a GUC value on node 3 (only):Example: Changing a GUC on all the cluster nodes:Execute Spock Cluster Management FunctionspgEdge clusters are built on top of the <a href="https://docs.pgedge.com/spock-v5/"><u>Spock </u></a>extension for logical replication, but Spock management commands must be run on a specific node—and they don’t replicate.  makes this easy to automate and manage.Example: Adding a table to a replication set:Example: Creating a table on all the pgEdge nodes without adding them to the replication set:Without exec_node(), these operations would require logging into each node or writing external scripts; now they can be run as SQL from any connected client or script.Supports Targeted DDL DeploymentSometimes not every function or schema change is required to happen on every node. Instead,  you might want:Example: Targeted function deployment:You now have precise control over where that function lives.<h4>Improves Automation and Operational Safety </h4>Because  works like any SQL function, it integrates seamlessly into:It removes the need for external scripting or SSH automation and reduces the risk of human error by centralising command execution in a controlled and auditable way.<h2>Use Cases</h2>Use to help with:<ul><li>deploying a non-replicated data maintenance command,  </li><li>DDL</li><li> or </li><li>functions</li><li> to a specific node.</li></ul><ul><li>running Spock configuration commands (</li><li>repset_add_table, node_add_interface</li><li>, etc.).</li></ul><ul><li>executing maintenance commands (</li><li>VACUUM, REINDEX, ANALYZE</li><li>) cluster-wide.</li></ul><ul><li>setting or altering system parameters per node (</li><li>ALTER SYSTEM</li><li>).</li></ul><ul><li>Creating or dropping databases on individual nodes.</li></ul><ul><li>Controlled rollout of feature flags or logic to subset of nodes.</li></ul>Best Practices <ul><li>Use with awareness</li><li>: While powerful, be careful when executing write operations across all nodes. Ensure commands are safe / secure and do not pose risks.</li></ul><ul><li>Log executions</li><li>: In automation scripts, consider logging the use of </li><li>exec_node()</li><li> for auditability.</li></ul><ul><li>Validate SQL</li><li>: Especially when executing SQL, validate the structure and scope to avoid unintended changes.</li></ul><ul><li>Test on dev/staging</li><li>: For complex cluster operations, test </li><li>exec_node()</li><li> in non-production environments before rollout.</li></ul></p> ]]></description>
            <guid>https://www.pgedge.com/blog/simplifying-cluster-wide-sql-execution-in-pgedge-with-exec_node</guid>
            <author><name>Asif Naeem</name></author>
            </item>
            <item>
            <category>PostgreSQL</category>
            <title><![CDATA[Using Snowflake Sequences with the Hibernate IDENTITY column]]></title>
            <link>https://www.pgedge.com/blog/using-snowflake-sequences-with-the-hibernate-identity-column</link>
            <pubDate>Tue, 27 May 2025 05:28:00 GMT</pubDate>
            <description><![CDATA[ <p>A PostgreSQL native sequence is a great way to generate unique IDs, but there is risk of duplicate ID generation if you use it in a distributed cluster environment. The <a href="https://docs.pgedge.com/platform/advanced/snowflake"><u>pgEdge Snowflake extension</u></a> is designed for use in a distributed environment, helping you generate truly unique identifiers with ease.Snowflake functions create unique identifiers that are designed to support parallel execution across multiple nodes. Using a Snowflake sequence ensures that each generated number is consistent and addresses the need for uniqueness across all of the pgEdge cluster nodes.This is the first in a series of blogs in which I will share different ways to adapt sequences in the pgEdge cluster environment.<h2>Snowflake Sequence Overview</h2>The Snowflake extension creates a replication-friendly identifier that you can use to replace problematic PostgreSQL sequence definitions in your tables that reside in a distributed replication environment. pgEdge PostgreSQL automatically installs and creates the extension in each pgEdge cluster.Snowflake sequences let you:<ul><li>Add or modify data in different regions while ensuring a unique transaction sequence.</li></ul><ul><li>Preserve unique transaction identifiers without manual/administrative management of a numbering scheme.</li></ul><ul><li>Accurately identify the order in which globally distributed transactions are performed.</li></ul>Each Snowflake ID is a 64-bit value that is composed of the multiple parts:<ul><li>Timestamp: 41 bits that contains the number milliseconds since 2023-01-01</li></ul><ul><li>Node number: 10 bits identifying a unique node number (set as a PostgreSQL GUC)</li></ul><ul><li>Counter Number: 12 bits that increment the value for handling multiple IDs that might be generated in the same millisecond</li></ul><a href="https://hibernate.org/"><u>Hibernate</u></a> software is an object-relational mapping (ORM) tool for Java developers that helps map Java objects (classes) to database tables, and automatically handles converting data between the two. It’s one of the most popular ORM frameworks in the Java ecosystem and is part of the Java Persistence API (JPA) standard. In our examples, we'll use a Snowflake sequence in a Hibernate environment.If you're working in a traditional replication environment (without the benefits of multi-master replication), using a Hibernate identity column as an auto-generated primary key implies that the database will automatically generate IDs. However, in distributed systems like pgEdge multi-master replication, Hibernate IDs can lead to collision issues if multiple nodes try to generate the same ID concurrently.The following code snippets implement Snowflake sequences in a Hibernate environment hosted on a pgEdge cluster: relies on PostgreSQL's SERIAL or BIGSERIAL type. Hibernate will create the  table with a 64 bit ID; in SQL, the table would look like:In a distributed environment, adding data to this table would have the potential to create conflicting identifiers (which in turn result in replication interruption and maintenance overhead). The collision issue can be avoided with minimal changes by adopting Snowflake sequences. As shown in the following code snippets, it's simple to replace the PostgreSQL style sequences with Snowflake sequences; simply:<ol></ol><h2>Automating Snowflake Sequence Implementation</h2>You can use a<a href="https://www.postgresql.org/docs/17/event-trigger-definition.html"> </a><a href="https://www.postgresql.org/docs/17/event-trigger-definition.html"><u>PostgreSQL event trigger</u></a> to ensure the use of Snowflake sequences when you create a table. The following example defines a PostgreSQL event trigger that automatically replaces column sequence definitions, replacing them with a Snowflake sequence.After creating this trigger in your PostgreSQL database, any time you run a  command, this trigger fires, looking for any  syntax that is part of the CREATE TABLE command; if it finds any, it replaces the column's default value (usually ) with .</p> ]]></description>
            <guid>https://www.pgedge.com/blog/using-snowflake-sequences-with-the-hibernate-identity-column</guid>
            <author><name>Asif Naeem</name></author>
            </item>    
    
        </channel>
    </rss>