Skip to content

jarulraj/postgresql-cpp

Repository files navigation

PostgreSQL-CPP

PostgreSQL license Version

This is a port of the PostgreSQL Database Management System to the C++ language (C++11 standard).

Certain features of the C++ language and its library should help simplify coding, improve code reuse, and avoid bugs. Here's a nice article that provides more context: Moving to C++.

Dependencies

  • g++ >= 4.8 (C++11 support)

The following packages are needed for building Postgres, including ssl support:

Redhat

$ sudo yum install -y bison-devel readline-devel zlib-devel openssl-devel wget
$ sudo yum groupinstall -y 'Development Tools'

Ubuntu

$ sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev

More information is available in the PostgreSQL manual and the PostgreSQL wiki.

Building the DBMS

$  mkdir build
$  cd build
$  ../configure --prefix=/path/to/build
$  make CC=g++ CPPFLAGS+="-std=c++11 -fpermissive -w"
$  make install

Using the DBMS

Create a data directory

$ cd build
$ ./bin/initdb ./data

We use initdb to create a new database cluster which is a collection of databases that are managed by a single server instance. Here, ./data is the location of the data directory for this new database cluster.

Start the server

$ ./bin/pg_ctl -D ./data start

pg_ctl is a program that can be used to start, stop, and control a Peloton server. It is installed along with Peloton, by default in usr/local/peloton/bin. Here, we use it to start the server.

Create a default user

$ ./bin/createuser -s -r postgres

Let's create a default user using createuser utility.

Connect to the server

$ ./bin/psql postgres

Finally, we can connect to the Peloton server using the psql terminal utility. Here, we connect to the postgres database. We can also connect to any other database in the cluster by altering this command. To get a list of psql shortcuts, just type help and then press enter.

Stop the server

$ ./bin/pg_ctl -D ./data stop

Now, we can use pg_ctl to stop the server.

More information on using the terminal

PSQL manual
PSQL common usage

Testing the DBMS

$  cd build
$  cp ../src/test/regress/expected/security_label.out ./src/test/regress/results/
$  make check

Porting Notes

Here's a list of the key changes:

  1. Refactored identifiers that conflict with C++ reserved keywords. Appended the identifiers with "__" to resolve this problem. Here's a list of the keywords that we refactored:
  • new, this, namespace, friend, public, private
  • typename, typeid, constexpr, operator, class, template
  1. Defined the assignment operator for structures with volatile instances.

    • RelFileNode at include/storage/relfilnode.h
    • QueuePosition at backend/commands/async.cpp
    • BufferTag at include/storage/buf_internals.h
  2. Defined the constructor for unions that contain a non-POD member.

    • SharedInvalidationMessage ar include/storage/sinval.h
  3. Refactored the missing increment operator. Changed forkNum++ to forkNum = forkNum + 1.

  4. Explicity declared that a enum value belongs to the particular enumeration type.

    • JsonbValue
  5. Refactored forward declaration of static arrays using an anonymous namespace.

    • pg_crc32c_table at port/pg_crc32c_sb8.cpp
  6. Handled identifiers with both extern and const qualifiers.

    • sync_method_options at backend/access/transam/xlog.cpp
    • wal_level_options at backend/access/rmgrdesc/xlogdesc.cpp
    • dynamic_shared_memory_options at backend/access/transam/xlog.cpp
    • archive_mode_options at backend/access/transam/xlog.cpp
  7. Declared functions with varying number of arguments by explicity defining function pointer types.

    • func_ptr0 at backend/utils/fmgr/fmgr.c
    • func_ptr1 at backend/utils/fmgr/fmgr.c
    • ...
    • func_ptr16 at backend/utils/fmgr/fmgr.c
    • expression_tree_walker at include/nodes/nodeFunc.h
    • expression_tree_mutator at include/nodes/nodeFunc.h
    • query_tree_walker at include/nodes/nodeFunc.h
    • query_tree_mutator at include/nodes/nodeFunc.h
    • range_table_walker at include/nodes/nodeFunc.h
    • range_table_mutator at include/nodes/nodeFunc.h
    • query_or_expression_tree_walker at include/nodes/nodeFunc.h
    • query_or_expression_tree_mutator at include/nodes/nodeFunc.h
    • raw_expression_tree_walker at include/nodes/nodeFunc.h

Credits

Ming Fang

About

C++11 port of the PostgreSQL DBMS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published