PostgreSQL memory granted for a single query plan - postgresql

I'm interested how can I get the information about how much memory/pages did PostgreSQL/OS grant to a query plan for sorting, subqueries, etc.?

Each session is shown as normal unix process and you can monitor those just like you would any other (with top for example). There is also dedicated tool for postgres: pg_top
It won't show detailed data about what parts of query plan take how much resources. I guess you can get some of it using explain analyze, but not much.

Related

How to DoS the Postgres Database

I am doing small research work on how to protect database against DDoS atack.
I am using postgres database in my testings.
I want to perform a DDoS of Database on my local machine. But I don't really know how to do it.
My plan is to create script that runs bunch of queries. But I want these queries took as much time to complete as possible.
I saw this example: select tab1 from (select decode(encode(convert(compress(post) using latin1),concat(post,post,post,post)),sha1(concat(post,post,post,post))) as tab1 from table_1)a;
But I am failing to replicate it in postgres.
I need help in translating this query in postgres or other examples of functions or queries that would take lots of time to complete.
Edit:
sleep functions might not work. They are not loading system enough.
In my understanding, DDoS should be performed with functions that are taking a long time to perform and sucks up tons of compute powers of the system.

Getting most used queries in mongodb

I'd like to analyze our db and create better indices for it.
Because our app is very complex, and we don't know what are the most used parts of our app, I'd like to somehow see what are the most used read queries that we hit our db with.
That would make it very easy for me to analyze and create the right indices for them.
Any ideas on how to do that?
you can enable database profiling for this.
get the details here - https://docs.mongodb.com/v3.2/tutorial/manage-the-database-profiler/
alternatively a simpler way would be to use the mongostat (details here -https://docs.mongodb.com/v3.2/administration/monitoring/) which captures and returns the counts of database operations by type (e.g. insert, query, update, delete, etc.).

Giving access to execute SQL queries on a static database

I am working on a project where i want to give people the possibility to execute SQL queries on an PostgreSQL database. I then only need to prevent people from hacking/attacking my database.
I thought that maybe a way to do that, is by giving only view access to de database connection. And using EXPLAIN ANALYSE to calculating the cost of the SQL query.
Is EXPLAIN ANALYSE trustworthy enough to make sure there are no cheap ways to get the website down?
Do you have suggestions?
EXPLAIN ANALYSE will execute the query, including any side-effects it may have. PostgreSQL also allows running arbitrary Perl and Python code if configured to do so, so be careful. You're likely better off running PostgreSQL instances in per-request VMs or in similar highly isolated environments.

run an execution plan directly in PostgreSQL

is it possible to run an execution plan directly in PostgreSQL?
I did not find anything about it after quite some search in the PostgreSQL document and on the internet.
No, it is not possible to directly execute a query plan in PostgreSQL. You must run actual SQL.
In theory you could customise the PostgreSQL executor to accept plans without the corresponding SQL by feeding in plan trees. This would be a pretty big job and I'm sure there are many things that'd make it harder that I don't even know about.
You really need to just run SQL.
There is no reverse-compiler to turn an execution plan back into SQL.

Is there any system views which show current and history plan information about SQL of PostgreSQL?

Sometims I want to monitor the performace of PostgreSQL DATABASE, I double that the plan of some sql statements were changed in the past. Is there any views which show current and history plan information about SQL of PostgreSQL?
Use the auto_explain extension. It can write the plans of all queries to the server log.
Plan information is dynamic based on the current state of the DB, what the latest ANALYZE shows, statistics, etc. These stats are accessible in the pg_stats view (see http://www.postgresql.org/docs/8.2/static/planner-stats.html) which you could back up for later analysis.