Sitemap
Harsha

Harsha

Postgresql vs Oracle

7 min readSep 6, 2017

--

Synopsis

This is an effort to record most important readings I have done/doing going through the PostgreSql Online documentation.

Differences between Oracle and Postgresql

#

PostgreSql

Oracle

1$$ quoted stringNo such feature2Partitioned tables are managed through inheritance, needs triggers on the Master table to make it happen.Partition is a concept in Oracle with so many features around it to make it work better3Various subqueries in a From clause can reference data in each other (using Lateral keyword)No such feature4Does not look like there is anything known has a stored procedure (you have to write a function which will be made use of like a procedure — mostly without returning anything)Stored Procedure is available and different from function5No such featurePackages6Database user seems to be tied with the OS loginOracle is free from such tie up7Referencing data on disk in csv etc format is very easyIn Oracle you have to make changes to listener to have Heterogenous config (if my memory serves better!) and you will then be able to access files through sql8Generally I found that everything Oracle does in terms of SQL is possible through Postgres and on top of it they have certain features which is much more user friendly like for example /d to describe a table /q to exit etcOracle is strong in terms of very well shaped concepts like the Stored Procedures that we saw above or the Partitions, it might not have those minor things like /d in postgresql but that does not hamper the functioning or it does not handicap one from getting work done.9Array feature which can be used in a sqlDoes not seem to be readily available in Oracle (needs some googling to confirm)10Offset clause can be used to retrieve rows in setsNo such feature11Boolean data type of 1 byteSuch data type is not directly available but one can use number(1) or char(1) indirectly12Geometric types, Network address typesI don’t think these data types are available in Oracle (need to find)13Jason Types is supported (There are two JSON data types: json and jsonb)Oracle supports Jason but only in 12c.14Shutdown related:

  • SIGTERM
  • SIGINT
  • SIGQUIT

This one is not any different from Oracle, only the names are different:

  • Shutdown;
  • Shutdown immediate;
  • Shutdown abort;

15During server startup, parameter settings can be passed to the postgres command via the -c command-line parameter. For example:

postgres -c log_connections=yes -c log_destination=’syslog’

No such feature16By default object names can be of length 63 characters, it could be controlled through a init parameter max_identifier_lengthI guess there is no way to influence the length of object names in Oracle and by default I know it to be 30 characters in length.17Short options shown here not very sure if it would be required for a production database day to day operations, but good to know.No such feature18Roles: as per the document roles can be configured either to work as Oracle db user or as Oracle db role

Meaning if a role is created with ‘login’ privilege then it acts as a user, else it is a role.

Roles can be assigned to other roles (which could also be a db user).

Note: DB user/Create user are both virtual, ultimately it is a role in the db with/without login privilege

Unlike Postgresql role and db user are both distinct in Oracle.19Ownership of objects can be transferred one at a time using ALTER commands, for example:

ALTER TABLE bobs_table OWNER TO alice;

No such feature20Alternatively, the REASSIGN OWNED command can be used to reassign ownership of all objects owned by the role-to-be-dropped to a single other role. Because REASSIGN OWNED cannot access objects in other databases, it is necessary to run it in each database that contains objects owned by the role. (Note that the first such REASSIGN OWNED will change the ownership of any shared-across-databases objects, that is databases or tablespaces, that are owned by the role-to-be-dropped.)No such feature21Drop owned by ;

Will drop all the objects of the role/user, leaving the user/role intact.

No such feature22Dropdb on the shell or Drop database inside the psql (client) can be used to drop a particular databaseOne could use DBCA to drop databases (but certain times I have seen it fail).23Tablespace pg_global contains shared system catalogsEquivalent to system tablespace in Oracle24Pg_default tablespace is used to host template0 and template1 databases, (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE)Almost equivalent to Users tablespace in Oracle25Looks like in Pg vaccum of a table happens irrespective of changes happening in that table or not.

Meaning a small look up table which contains static data will get a vaccum done. (even if autovaccum is disabled and/or even if this small lookup table does not go through any DMLs, meaning frozen static data).

As per the online doc, this is done in order to replace the XIDs (transaction IDs) in such tables with frozen XIDs, else there will be data loss.

The main reason for this is that the XIDs revert to 0 if the transactions in a database exceeds 4 billion.

Get Harsha’s stories in your inbox

Join Medium for free to get updates from this writer.

There is no such limitation in Oracle, you could choose to skip and not compact a table (reduce HWM regain space from a table) and still not have any data loss.26It is suggested to reindex b-tree indexes periodically to improve performance (basically defrag and sequentialize data in it)Whereas in Oracle you don’t have to reindex b-tree indexes27Unlike Oracle PG does not dump server log continuously on its own through a init parameter, but you would have to redirect the logging of the process to a file, just like how you would use 1> 2> for log and error redirection for a unix command.Oracle would log automatically into the diag folder that is set in the init parameters.28Pg_dump could be used to extract data and piped to psql to import it into another database (if need be onto a db which resides on a remote host) in one single command, example:

pg_dump -h host1 dbname | psql -h host2 dbname

No such feature29Domain that can be defined in Postgresql, as per the documentation, if there is a particular constraint that you would want to use for multiple tables, you can create a domain and then use the same.

You could find complete info and example here:

No such feature30Postgresql views are not updateable by default, you can do the same using “an unconditional On Update Do Instead”.

In Postgresql 9.1 and above you can create a “view trigger”

From online blogs it also seems like ‘Do Instead’ rules are not applicable for versions below 8.4

But: From Postgresql 9.3 onwards simple views are directly updatable.

Oracle lets updating of views by default, no additional coding required to make it happen.

*Like I have mentioned in the box Postgresql column, from Postgresql 9.3 onwards both Oracle and Postgresql become same feature wise, because Postgresql here on lets one update simple-views (meaning the data would be propagated to the downstream table).

31Postgresql9.5 onwards Row Level Security is supported through security policies. Security policies will be controlled by the DBA not the application.

RLS offers strong security as it is enforced by the database

Oracle has “Oracle Label Security” to implement the same, but I wouldn’t think it would be free, one might have to buy additional license to use it.32PG suggests to reindex as one of the maintenance job on a database (this is basically to address fragmentation)Whereas in Oracle there is no need to maintain btree indexes, they are auto maintained33In a sql query we can directly deduct a date_column value from now, for example:

select usename, now()-backend_start as tot_session_time from pg_stat_activity;

Which would return the username and the who is connected to the database and how long as the connection been there

In Oracle you cannot + — etc between 2 date columns of a table or for that matter even with sysdate.34
PAGE_BREAK: PageBreak

Equivalent commands/views/concepts between Postgresql and Oracle

#

PostgreSql

Oracle

1pg_dump, pg_dumpallexpdmp2pg_hba.confYet to confirm but pg_hba.conf in Postgresql seems like an equivalent of init.ora in Oracle3pg_file_settings (I guess even pg_settings is same as v$parameter)v$parameter4Postgresql.conf (there is also a file postgresql.auto.conf which will store all the settings that has been done through alter system, the doc says not to edit this file manually).init.ora5Committing a transaction and acknowledging to the client can be done with conditions in terms of before or after writing to local disk or remote standby db etc, this can be controlled through the parameter synchronous_commit.

At the same time, this setting could be controlled at individual transaction level!

Just not sure about this detail in Oracle, makes me wonder how less I know in terms of this particular information!6** random_page_cost ** seems to be a very important parameter, need to read better about it…7Looks like the Truncate command works the same in both the DB, but for some reason the online doc has put a note which says: “The disadvantage is that strict MVCC semantics are violated.”

Also, the doc says you have to follow the truncate with a vaccum command.

Truncate in Oracle does all the same as Postgresql.8WAL filesRedo log files9If the destination where WAL log files are to be copied is full the db will start writing to a folder named pg_xlog, in case this folder fills up, db will shutdownSame behaviour when trying to archive oracle archive redo logs.10Use pg_basebackup can be used to perform base backup (in terms of Oracle hot backup)

This puts the database into the backup mode automatically and starts archiving the database in the format that you choose (plain or tar), switches off the backup mode immediately after backup.

There is no guarantee that all WAL files required for the backup are archived at the end of backup. If you are planning to use the backup for an archive recovery and want to ensure that all required files are available at that moment, you need to include them into the backup by using -x option.

Same can be done through scripts in RMAN.11pg_stat_activityv$session

--

--