<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhi-Kmr2046</title>
    <description>The latest articles on DEV Community by Abhi-Kmr2046 (@abhikmr2046).</description>
    <link>https://dev.to/abhikmr2046</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1043474%2F8fb4067c-3f79-4072-a565-ddf1d4060208.jpeg</url>
      <title>DEV Community: Abhi-Kmr2046</title>
      <link>https://dev.to/abhikmr2046</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhikmr2046"/>
    <language>en</language>
    <item>
      <title>WAL buffer and WAL segment file</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Thu, 31 Aug 2023 16:50:03 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/wal-buffer-and-wal-segment-file-3d6b</link>
      <guid>https://dev.to/abhikmr2046/wal-buffer-and-wal-segment-file-3d6b</guid>
      <description>&lt;p&gt;WAL stands for Write-Ahead Logging. It is a standard method for ensuring data integrity. WAL data is a description of changes made to the actual data, and is also known as metadata.&lt;/p&gt;

&lt;p&gt;WAL’s central concept is that changes to data files (where tables and indexes reside) must be written only after those changes have been logged, that is, after log records describing the changes have been flushed to persistent storage. This ensures that in the event of any error, we will be able to recover the database using the log. Any changes that have not been applied to the data pages can be redone from the log records. This is roll-forward recovery, also known as REDO.&lt;br&gt;
WAL buffers and WAL segment files are two components of the WAL system in PostgreSQL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WAL buffers&lt;/strong&gt; are in-memory buffers that temporarily hold transactions until they are written to disk. They are used to speed up write operations by allowing transactions to be written to disk in batches rather than one at a time. If the WAL buffer becomes full, the contents of the buffer are dumped into a WAL segment file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WAL segment files&lt;/strong&gt; are on-disk files that store the contents of the WAL buffer when it becomes full. They are used to ensure that all transactions are written to disk before being acknowledged as complete. When a new transaction is started, it is written to a new WAL segment file.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>apacheage</category>
    </item>
    <item>
      <title>How to write PostgreSQL extensions in C++</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Fri, 30 Jun 2023 16:41:14 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/how-to-write-postgresql-extensions-in-c-55l3</link>
      <guid>https://dev.to/abhikmr2046/how-to-write-postgresql-extensions-in-c-55l3</guid>
      <description>&lt;p&gt;Creating a completely new extension for PostgreSQL involves several steps.&lt;br&gt;
Here we will create a basic extension with that adds SQL function to calculate factorial of a given integer. We will write the extension code in c++.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the Factorial Extension
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Setp 1:&lt;/strong&gt;&lt;br&gt;
Create factorial_extension folder in &lt;code&gt;share&lt;/code&gt; directory in the postgres installation folder.&lt;br&gt;
&lt;strong&gt;Set up the folder structure.&lt;/strong&gt; This folder contains file &lt;code&gt;factorial_extension.control&lt;/code&gt;, &lt;code&gt;factorial_extension--1.0.sql&lt;/code&gt;, &lt;code&gt;factorial_extension.cpp&lt;/code&gt; and &lt;code&gt;MakeFile&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;factorial_extension/
    |
    +-- factorial_extension.control
    |
    +-- factorial_extension.cpp
    |
    +-- factorial_extension--1.0.sql
    |
    +-- Makefile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setp 2:&lt;/strong&gt;&lt;br&gt;
Write code for factorial in C++&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;postgres.h&amp;gt;
#include &amp;lt;fmgr.h&amp;gt;
#include &amp;lt;utils/numeric.h&amp;gt;

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

extern "C" {
    PG_FUNCTION_INFO_V1(factorial);

    Datum factorial(PG_FUNCTION_ARGS) {
        int32 arg = PG_GETARG_INT32(0);

        if (arg &amp;lt; 0)
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("Factorial input must be a non-negative integer")));

        int64 result = 1;
        for (int i = 2; i &amp;lt;= arg; ++i)
            result *= i;

        PG_RETURN_INT64(result);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setp 3:&lt;/strong&gt;&lt;br&gt;
Write the control file. The control file for an extension, also known as the .control file, is a metadata file that provides information about the extension. It contains details such as the name, version, author, module path, dependencies, and other important attributes of the extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# factorial_extension.control
comment = 'Extension to calculate factorial'
default_version = '1.0'
module_pathname = '$libdir/factorial_extension'
relocatable = false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;br&gt;
Write the SQL script (factorial_extension--1.0.sql):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- factorial_extension--1.0.sql

-- Create the extension schema
CREATE SCHEMA factorial_extension;

-- Create the SQL function
CREATE OR REPLACE FUNCTION factorial_extension.factorial(integer)
RETURNS bigint AS 'factorial_extension', 'factorial'
LANGUAGE C STRICT;

-- Grant execute permission to public (change to appropriate roles if needed)
GRANT EXECUTE ON FUNCTION factorial_extension.factorial(integer) TO PUBLIC;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;br&gt;
Write the MakeFile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MODULES = factorial_extension
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the Extension
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Start or restart the PostgreSQL server&lt;br&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Connect to the database where you want to install the extension&lt;br&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; To install the extension run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE EXTENSION factorial_extension;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use the Extension
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT factorial_extension.factorial(5);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>postgres</category>
      <category>apacheag</category>
    </item>
    <item>
      <title>How to create extensions in PostgreSQL?</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Fri, 30 Jun 2023 16:28:11 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/how-to-create-extensions-in-postgresql-285f</link>
      <guid>https://dev.to/abhikmr2046/how-to-create-extensions-in-postgresql-285f</guid>
      <description>&lt;p&gt;Creating a completely new extension for PostgreSQL involves several steps.&lt;br&gt;
Here we will create a basic extension with that adds SQL function to calculate factorial of a given integer. This example is written using the PL/pgSQL procedural language.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the Factorial Extension
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Setp 1:&lt;/strong&gt;&lt;br&gt;
Create factorial_extension folder in &lt;code&gt;share&lt;/code&gt; directory in the postgres installation folder.&lt;br&gt;
Set up the folder structure. This folder contains file &lt;code&gt;factorial_extension.control&lt;/code&gt; and &lt;code&gt;factorial_extension--1.0.sql&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;factorial_extension/
    |
    +-- factorial_extension.control
    |
    +-- factorial_extension--1.0.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setp 2:&lt;/strong&gt;&lt;br&gt;
Write the control file. The control file for an extension, also known as the .control file, is a metadata file that provides information about the extension. It contains details such as the name, version, author, module path, dependencies, and other important attributes of the extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# factorial_extension.control
comment = 'Extension to calculate factorial'
default_version = '1.0'
module_pathname = '$libdir/factorial_extension'
relocatable = false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&lt;br&gt;
Write the SQL script (factorial_extension--1.0.sql):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- factorial_extension--1.0.sql

-- Create a new schema for the extension
CREATE SCHEMA factorial_extension;

-- Create the factorial function
CREATE OR REPLACE FUNCTION factorial_extension.factorial(n INTEGER)
RETURNS BIGINT AS $$
DECLARE
    result BIGINT := 1;
BEGIN
    IF n &amp;lt; 0 THEN
        RAISE EXCEPTION 'Factorial is not defined for negative numbers';
    ELSIF n &amp;gt; 1 THEN
        FOR i IN 2..n LOOP
            result := result * i;
        END LOOP;
    END IF;
    RETURN result;
END;
$$ LANGUAGE plpgsql;

-- Grant execute permission to public (change to appropriate roles if needed)
GRANT EXECUTE ON FUNCTION factorial_extension.factorial(INTEGER) TO PUBLIC;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the Extension
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Start or restart the PostgreSQL server&lt;br&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Connect to the database where you want to install the extension&lt;br&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; TO install the extension run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE EXTENSION factorial_extension;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use the Extension
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT factorial_extension.factorial(5);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>What are Extension in PostgreSQL???</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Fri, 30 Jun 2023 15:47:02 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/what-are-extension-in-postgresql-jdd</link>
      <guid>https://dev.to/abhikmr2046/what-are-extension-in-postgresql-jdd</guid>
      <description>&lt;p&gt;Extensions in PostgreSQL are modular add-ons that enhance the functionality of the core database system. They provide a way to extend the capabilities of PostgreSQL by adding new features, data types, functions, operators, and procedural languages.&lt;/p&gt;

&lt;p&gt;The functionalities can range from simple utilities to complex features like full-text search, geographic information systems (GIS), data compression, auditing, and more. By installing the relevant extensions, users can customize their PostgreSQL installation to suit their specific needs.&lt;/p&gt;

&lt;p&gt;Extensions can introduce new data types to PostgreSQL. For example, the hstore extension adds support for a key-value store data type, which can be useful for storing dynamic properties associated with database records. Similarly, the uuid-ossp extension provides a data type for universally unique identifiers (UUIDs). These additional data types enhance the flexibility and expressiveness of the database.&lt;/p&gt;

&lt;p&gt;PostgreSQL supports multiple procedural languages, such as PL/pgSQL, PL/Python, PL/Perl, PL/Java, and more. Extensions can introduce new procedural languages, allowing developers to write database functions, triggers, and stored procedures in their language of choice. This flexibility enables developers to leverage their existing skills and integrate custom logic seamlessly within the database.&lt;/p&gt;

&lt;p&gt;There are many such extensions like PostGIS for spatial and geographic data, pgcrypto provides cryptographic functions, citext extension provides a case-insensitive text data type in PostgreSQL. &lt;/p&gt;

&lt;p&gt;Apache AGE is such an extension for PostgreSQL that enables graph database functionality within the PostgreSQL. It allows users to store, query, and manipulate graph data using familiar SQL syntax. With this extension, users can model complex relationships, traverse graphs efficiently, and perform advanced graph queries using standard SQL.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>apacheage</category>
    </item>
    <item>
      <title>Creating New Function in Apache AGE</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Sun, 28 May 2023 18:49:50 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/creating-new-function-in-apache-age-ioj</link>
      <guid>https://dev.to/abhikmr2046/creating-new-function-in-apache-age-ioj</guid>
      <description>&lt;p&gt;We can add new functions in Apache age. To do this we will have to modify a few files and compile the Apache AGE code.&lt;br&gt;
To demonstrate this we'll create a gcd function which is not available in apache age.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;age--1.1.0.sql&lt;/code&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE FUNCTION ag_catalog.age_gcd(agtype, agtype)
RETURNS agtype
LANGUAGE c
PARALLEL SAFE
AS 'MODULE_PATHNAME';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This declares the function we are creating. Here we have two arguments  of type &lt;code&gt;agetype&lt;/code&gt; because apache age only supports &lt;code&gt;agetype&lt;/code&gt; as arguments. &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;src/backend/utils/adt/agtype.c&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We'll add our function in the above file. Just add the given code anywhere in the file. This is syntax of adding a function to apache age.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
PG_FUNCTION_INFO_V1(age_gcd);

Datum age_gcd(PG_FUNCTION_ARGS)
{
    agtype *agt_arg0 = AG_GET_ARG_AGTYPE_P(0);
    agtype *agt_arg1 = AG_GET_ARG_AGTYPE_P(1);
    agtype_value *arg0;
    agtype_value *arg1;
    agtype_value agtv_result;

    /* gcd supports agtype integer as input */
    arg0 = get_ith_agtype_value_from_container(&amp;amp;agt_arg0-&amp;gt;root, 0);
    arg1 = get_ith_agtype_value_from_container(&amp;amp;agt_arg1-&amp;gt;root, 0);

    /* only integers are allowed */
    if (arg0-&amp;gt;type != AGTV_INTEGER || arg1-&amp;gt;type != AGTV_INTEGER )
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                        errmsg("arguments must resolve to an integer")));

    /* check for agtype null */
    if (arg0-&amp;gt;type == AGTV_NULL)
        PG_RETURN_POINTER(agt_arg1);
    if (arg1-&amp;gt;type == AGTV_NULL)
        PG_RETURN_POINTER(agt_arg0);

    agtv_result.val.numeric = DatumGetNumeric(
                DirectFunctionCall2(numeric_gcd,
                                    DirectFunctionCall1(int8_numeric,
                                                        Int64GetDatum(arg0-&amp;gt;val.int_value)),
                                    DirectFunctionCall1(int8_numeric,
                                                        Int64GetDatum(arg1-&amp;gt;val.int_value))));
    agtv_result.type = AGTV_NUMERIC;

    PG_RETURN_POINTER(agtype_value_to_agtype(&amp;amp;agtv_result));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here to calculate gcd, &lt;code&gt;numeric_gcd&lt;/code&gt; is passed in the &lt;code&gt;DirectFunctionCall2&lt;/code&gt;. It is a postgress function used to calculate gcd. Here we can directly utilize that to make our own gcd function of Apache AGE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compilation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make uninstall
make clean
make PG_CONFIG=/home/abhis/age_installation/postgresql-14.7/bin/pg_config install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Loading age extension
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Start Server:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bin/pg_ctl -D demo -l logfile start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Run psql&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bin/psql &amp;lt;dbname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the code is successfully compiled and psql command line is started we need to load the age extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sometimes might not work. In that case, run the command given below and rerun the command to load extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP EXTENSION age CASCADE;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Query
&lt;/h2&gt;

&lt;p&gt;We can use any of the below syntax to run the query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM age_gcd('12', '9');

SELECT * FROM cypher('expr', $$
    RETURN gcd(15, 25)
$$) as (result agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>apacheage</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Graph Types in NetworkX</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Sun, 28 May 2023 17:35:58 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/graph-types-in-networkx-3e5j</link>
      <guid>https://dev.to/abhikmr2046/graph-types-in-networkx-3e5j</guid>
      <description>&lt;p&gt;NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It provides data structures and methods for storing graphs. &lt;br&gt;
Graphs are basically of four types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Graph: This a &lt;strong&gt;undirected graph&lt;/strong&gt;, with possible self-loops but no parallel edges.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;G = nx.Graph()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;DiGraph: This a &lt;strong&gt;directed graph&lt;/strong&gt;, with possible self-loops but no parallel edges.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;G = nx.DiGraph()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;MultiGraph: This a &lt;strong&gt;undirected graph&lt;/strong&gt;, with possible self-loops and also parallel edges.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;G = nx.MultiGraph()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;MultiDiGraph: This a &lt;strong&gt;directed graph&lt;/strong&gt;, with possible self-loops and also parallel edges.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;G = nx.MultiDiGraph()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Graph Views
&lt;/h2&gt;

&lt;p&gt;For some algorithms it is convenient to temporarily morph a graph to exclude some nodes or edges. NetworkX provides a easier way to accomplish this using &lt;strong&gt;Graph Views&lt;/strong&gt;. This is a view of a graph as SubGraph, Reverse, Directed or Undirected. The graph created using view is a read only graph object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;view = nx.reverse_view(G)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a view of the original graph but edges reversed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def filter_node(node):
    return node != 5

view = nx.subgraph_view(G, filter_node=filter_node)
view.nodes()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a view of the original graph but it will not contain nodes which do satisfy the condition in filter_node.&lt;/p&gt;

</description>
      <category>networkx</category>
      <category>apacheag</category>
    </item>
    <item>
      <title>How to Work with NetworkX</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Sat, 27 May 2023 19:33:22 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/how-to-work-with-networkx-4pk0</link>
      <guid>https://dev.to/abhikmr2046/how-to-work-with-networkx-4pk0</guid>
      <description>&lt;p&gt;NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It provides software for complex networks, data structures for graphs, digraphs, and multigraphs, many standard graph algorithms, network structure and analysis measures, generators for classic graphs, random graphs, and synthetic networks. &lt;/p&gt;

&lt;p&gt;To install networkX library run the command &lt;code&gt;pip install networkx&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Graph Creation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import networkx as nx
G = nx.Graph()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a empty graph. A graph in networkX is collection of nodes (vertices) along with identified pairs of nodes. A node can be any hashable object e.g., string, image, or another Graph etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nodes
&lt;/h2&gt;

&lt;p&gt;To add nodes we can use one of the following functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;G.add_node(1)&lt;/code&gt; creates a single node&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;G.add_nodes_from([2, 3]) creates nodes 2, 3. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Edges
&lt;/h2&gt;

&lt;p&gt;Similar to edges we also have two function to add edges to the graph.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;G.add_edge(1,2)&lt;/code&gt; creates a edge between nodes 1 and 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;G.add_nodes_from([(1, 2), (1, 3)]) creates edges between 1,2 and 1,3. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Visualize Graphs
&lt;/h2&gt;

&lt;p&gt;NetworkX supports basic graph drawing using the matplotlib library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import networkx as nx
import matplotlib.pyplot as plt
G = nx.complete_graph(6)
nx.draw(G)
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will first create a complete graph with 6 nodes. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OjfCfpio--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sigm575816me6ljmmovl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OjfCfpio--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sigm575816me6ljmmovl.png" alt="Image description" width="601" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;plt.show() is important to show the graph image on screen. &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>apacheag</category>
      <category>networkx</category>
    </item>
    <item>
      <title>Pattern Matching In Apache AGE</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Wed, 26 Apr 2023 16:25:49 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/pattern-matching-in-apache-age-4k9e</link>
      <guid>https://dev.to/abhikmr2046/pattern-matching-in-apache-age-4k9e</guid>
      <description>&lt;p&gt;Pattern matching is one of the most fundamental operation in a database system. It has numerous use cases like validating form submissions, performing search and replace operations, cleaning and standardizing data, and searching for specific patterns in a database.&lt;br&gt;
Functions provided by Apache AGE for this are as follows:&lt;/p&gt;
&lt;h2&gt;
  
  
  Starts With
&lt;/h2&gt;

&lt;p&gt;Performs case-sensitive prefix searching on strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name STARTS WITH "J"
    RETURN v.name
$$) AS (names agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Contains
&lt;/h2&gt;

&lt;p&gt;Performs case-sensitive inclusion searching in strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name CONTAINS "o"
    RETURN v.name
$$) AS (names agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ends With
&lt;/h2&gt;

&lt;p&gt;Performs case-sensitive suffix searching on strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name ENDS WITH "n"
    RETURN v.name
$$) AS (names agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Regular Expressions
&lt;/h2&gt;

&lt;p&gt;Regex or regular expressions are special sequences used to find or match patterns in strings. AGE supports the use of POSIX regular expressions using the =~ operator.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name =~ '(?i)JoHn'
    RETURN v.name
$$) AS (names agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can provide any regular expression after ~= and it will search for the strings that matches that expression.&lt;/p&gt;

&lt;p&gt;The special characters used in regex include the backslash , the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called “metacharacters”.&lt;/p&gt;

&lt;p&gt;The backslash \ is used to escape metacharacters so they can be treated as literal characters. The caret ^ is used to match the start of a string. The dollar sign $ is used to match the end of a string. The period or dot . is used to match any character except newline. The vertical bar or pipe symbol | is used to match either/or. The question mark ? is used to match zero or one of the preceding character. The asterisk or star * is used to match zero or more of the preceding character. The plus sign + is used to match one or more of the preceding character. The opening parenthesis ( and closing parenthesis ) are used for grouping. The opening square bracket [ and closing square bracket ] are used for character classes. The opening curly brace { and closing curly brace } are used for quantifiers.&lt;/p&gt;

</description>
      <category>apacheage</category>
    </item>
    <item>
      <title>Entities in Apache AGE</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Wed, 26 Apr 2023 15:10:26 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/entities-in-apache-age-2hb6</link>
      <guid>https://dev.to/abhikmr2046/entities-in-apache-age-2hb6</guid>
      <description>&lt;p&gt;An entity has a unique, comparable identity which defines whether or not two entities are equal.&lt;/p&gt;

&lt;p&gt;An entity is assigned a set of properties, each of which are uniquely identified in the set by the irrespective property keys.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Entities
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GraphId: Simple entities are assigned a unique graphid. A graphid is a unique composition of the entity’s label id and a unique sequence assigned to each label. Note that there will be overlap in ids when comparing entities from different graphs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Labels: A label is an identifier that classifies vertices and edges into certain categories. Edges are required to have a label, but vertices do not. The names of labels between vertices and edges cannot overlap.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Properties: Both vertices and edges may have properties. Properties are attribute values, and each attribute name should be defined only as a string type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vertex: A vertex is the basic entity of the graph, with the unique attribute of being able to exist in and of itself.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# typecasting a map to vertex
SELECT *
FROM cypher('graph', $$
    WITH {id: 0, label: "label_name", properties: {i: 0}}::vertex as v
    RETURN v
$$) AS (v agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Edge: An edge is an entity that encodes a directed connection between exactly two nodes, the source node and the target node. An edge is assigned exactly one edge type.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# typecasting a map to vertex
SELECT *
FROM cypher('graph', $$
    WITH {id: 2, start_id: 0, end_id: 1, label: "label_name", properties: {i: 0}}::edge as e
    RETURN e
$$) AS (e agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Composite Entities
&lt;/h2&gt;

&lt;p&gt;It is combination of multiple simple entities.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Path: A path is a series of alternating vertices and edges. A path must start with a vertex, and have at least one edge.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# typecasting a list to path
SELECT *
FROM cypher('graph', $$
    WITH [{id: 0, label: "label_name_1", properties: {i: 0}}::vertex,
            {id: 2, start_id: 0, end_id: 1, label: "edge_label", properties: {i: 0}}::edge,
           {id: 1, label: "label_name_2", properties: {}}::vertex
           ]::path as p
    RETURN p
$$) AS (p agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>apacheage</category>
    </item>
    <item>
      <title>Composite Datatypes in Apache AGE</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Wed, 26 Apr 2023 14:55:08 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/composite-datatypes-in-apache-age-2cak</link>
      <guid>https://dev.to/abhikmr2046/composite-datatypes-in-apache-age-2cak</guid>
      <description>&lt;p&gt;Composite Data Types is used to store multiple logically related items of Simple Data Types in Apache AGE. This includes Lists and Maps.&lt;/p&gt;

&lt;h2&gt;
  
  
  List
&lt;/h2&gt;

&lt;p&gt;It is a collection of ordered items. This is similar to list in python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# query to create a list
SELECT *
FROM cypher('graph', $$
    WITH [0, 1, 2, 3, 4, 5] as lis
    RETURN lis
$$) AS (lis agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access an individual element in the list we can use square brackets&lt;br&gt;
 like all most other programming languages like C/C++, Python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# query to create a list
SELECT *
FROM cypher('graph', $$
    WITH [0, 1, 2, 3, 4, 5] as lis
    RETURN lis[2]
$$) AS (val agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A list can store elements of all Simple Data Types as well as Composite type like list and map.&lt;br&gt;
It can use negative index, which will start counting from the end and index ranges which returns the elements in the given range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    WITH [0, 1, 2, 3, 4, 5] as lis
    RETURN lis[0..3]
$$) AS (val agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;A map is a collection of elements where each element is stored as a key-value pair. It is like maps in python. &lt;br&gt;
It can use any data type (Simple or Composite) as key or as value, as shown in the example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    WITH {int_key: 1, float_key: 1.0, numeric_key: 1::numeric, bool_key: true, string_key: 'Value'} as m
    RETURN m
$$) AS (m agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    WITH {int_key: 1, float_key: 1.0, numeric_key: 1::numeric, bool_key: true, string_key: 'Value'} as m
    RETURN m
$$) AS (m agtype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access the value for a key we can use the dot (.) convention as in the example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    WITH {int_key: 1, float_key: 1.0, numeric_key: 1::numeric, bool_key: true, string_key: 'Value'} as m
    RETURN m.int_key
$$) AS (int_key agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>apacheage</category>
    </item>
    <item>
      <title>Apache AGE Datatypes</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Wed, 26 Apr 2023 14:32:45 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/apache-age-datatypes-gf2</link>
      <guid>https://dev.to/abhikmr2046/apache-age-datatypes-gf2</guid>
      <description>&lt;p&gt;Apache AGE uses its own custom data type called agetype, which is a super set of Json and a custom implementation of JsonB. It is the only data type returned by AGE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Data Types
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Null: It represents missing or undefined values. Most expressions that take null as input will produce null. Null is not equal to null. Not knowing two values does not imply that they are the same value. So the expression null = null yields null and not true.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integer: It stores whole numbers and it is a 64 bit field that stores values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The smallint type is generally used only if disk space is at a premium. The bigint type is designed to be used when the range of the integer type is insufficient.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    RETURN 1
$$) AS (result agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Float: The data type float is an inexact, variable-precision numeric type, conforming to the IEEE-754 Standard. Values that are too large or too small will cause an error. Rounding might take place if the precision of an input number is too high. Numbers too close to zero that are not representable as distinct from zero will cause an underflow error. It also supports values Infinity, -Infinity and Nan.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    RETURN 1.0
$$) AS (result agtype);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Numeric: The type numeric can store numbers with a very large number of digits. It is especially recommended for storing monetary amounts and other quantities where exactness is required. Calculations with numeric values yield exact results where possible, e.g., addition, subtraction, multiplication. However, calculations on numeric values are very slow compared to the integer types, or to the floating-point type.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    RETURN 1.0::numeric
$$) AS (result agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Bool: AGE provides the standard Cypher type boolean. The boolean type can have several states: “true”, “false”, and a third state, “unknown”, which is represented by the Agtype null value. Boolean constants can be represented in Cypher queries by the keywords TRUE, FALSE, and NULL.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    RETURN TRUE
$$) AS (result agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;String: Single (') quotes are used to identify a string. The output will uses double (") quotes. AGE type strings can also contain escape sequences, for example, \t, \b, \n.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM cypher('graph', $$
    RETURN 'Example String'
$$) AS (result agtype);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the Simple Data Types used in Apache AGE. There are also Composite Data Types which includes List and Maps.&lt;/p&gt;

</description>
      <category>apacheage</category>
    </item>
    <item>
      <title>Cost Estimation In PostgreSQL</title>
      <dc:creator>Abhi-Kmr2046</dc:creator>
      <pubDate>Fri, 31 Mar 2023 13:22:31 +0000</pubDate>
      <link>https://dev.to/abhikmr2046/cost-estimation-in-postgresql-57ol</link>
      <guid>https://dev.to/abhikmr2046/cost-estimation-in-postgresql-57ol</guid>
      <description>&lt;p&gt;Cost estimation in PostgreSQL refers to the process by which the query &lt;strong&gt;planner&lt;/strong&gt; estimates how much time a query will take to execute. The planner uses both a startup cost and a total cost for each operation to determine the cheapest option for executing a query. The costs are calculated using both constants and metadata about the contents of the database, often referred to as "statistics". This is not an absolute performance indicator of the final execution but gives a relative idea of execution of queries in different ways. &lt;br&gt;
The costs of a query are estimated by functions defined in &lt;em&gt;costsize.c&lt;/em&gt; and all the operations of executor have their corresponding cost function. &lt;br&gt;
There are three types of cost in PostgreSQL which are as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Start-up Cost:&lt;/strong&gt; It is cost expended before the first tuple is fetched.&lt;br&gt;
&lt;strong&gt;2. Run Cost:&lt;/strong&gt; It is cost to fetch all the tuples.&lt;br&gt;
&lt;strong&gt;3. Total Cost:&lt;/strong&gt; It is sum of both start-up cost and run cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explain&lt;/strong&gt; command shows both the start-up cost and total cost in the operation. The command can be used like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;testdb=# EXPLAIN SELECT * FROM tbl;
                       QUERY PLAN                        
---------------------------------------------------------
 Seq Scan on tbl  (cost=0.00..145.00 rows=10000 width=8)
(1 row)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cost Estimation in Sequential Scan
&lt;/h2&gt;

&lt;p&gt;The cost of the sequential scan is estimated by &lt;code&gt;cost_seqscan()&lt;/code&gt; function. For this, startup cost is 0 and the run cost is defined using the following formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'run cost' = 'cpu run cost' + 'dis run cost'
           = ('cpu tuple cost' + 'cpu operator cost')xNtupule +'seq page cost'xNpage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here 'seq page cost', 'cpu tuple cost' and 'cpu operator cost' are set in the &lt;em&gt;postgresql.conf&lt;/em&gt; file, and the default values are 1.0, 0.01, and 0.0025, respectively.&lt;br&gt;
For &lt;em&gt;Ntuple&lt;/em&gt; = 10000 and &lt;em&gt;Npage&lt;/em&gt; = 45 the &lt;em&gt;run cost&lt;/em&gt; comes out to be 170 which is same as &lt;em&gt;total cost&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cost Estimation in Sort
&lt;/h2&gt;

&lt;p&gt;The sort path is used for a variety of sorting tasks, including ORDER BY, preprocessing merge join procedures, and more. The &lt;em&gt;cost sort()&lt;/em&gt; method calculates the cost of sorting.&lt;br&gt;
If all of the tuples that need to be sorted can be stored in work mem, the quicksort algorithm is used otherwise tuples are broken into multiple parts in temporary files and merge sort algorithm is used.&lt;br&gt;
The start-up cost of the sort path is the cost of sorting the target tuples. The run cost of the sort path is the cost of reading the sorted tuples.&lt;br&gt;
The following formula is used for the cost estimation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'start up cost' = C + 'comparison cost' x Nsort x log2(Nsort)
'run cost' = 'cpu operator cost' x Nsort
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where C is the total cost of index scan, 'comparison cost'  = 2 x 'cpu operator cost'. &lt;/p&gt;

</description>
      <category>postgres</category>
      <category>apacheage</category>
    </item>
  </channel>
</rss>
