0% found this document useful (0 votes)
17 views

ADC Theory

Uploaded by

aaaryaa1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

ADC Theory

Uploaded by

aaaryaa1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Location of cluster.

Ans: /var/lib/postgresql/14/main/base/

2. What is a cluster?
Ans: A database cluster is a collection of databases that is managed by a single instance of
a running database server.

3. What postgresql.conf contains?


Ans: Location of data directory/cluster. Number of clients can be connected to a single
server.

4. location of databases in directory.


Ans: (/var/lib/postgresql/14/main/base/)

5. Schema. Create schema, set default schema.


Ans: Default schema is public.
syntax: create schema schemaName.
Schema is a collection of logical structures of data. In PostgreSQL, schema is a named
collection of tables, views, functions, constraints, indexes, sequences etc. PostgreSQL
supports having multiple schemas in a single database there by letting you namespace
different features into different schemas.

6. pg_class, pg_attribute.
Ans: pg_class is an internal system table that stores information about tables (and similar
objects") available in the database. Every database product has such a collection of system
tables (and views) that it maintains.

The catalogue pg_attribute stores information about table columns. There will be exactly one
pg_attribute row for every column in every table in the database.

7. When we start our machine. How many postgres processes are running?
Ans: 6+1

8. pg_database.
Ans: Catalog table that stores pg_database information about available databases.

9. What is a toast? toast_index.


Ans: PostgreSQL uses a fixed page size (commonly 8 kB), and does not allow tuples to
span multiple pages. Therefore, it is not possible to store very large field values directly. To
overcome this limitation, large field values are compressed and/or broken up into multiple
physical rows. This happens transparently to the user, with only a small impact on most of
the backend code. The technique is affectionately known as TOAST (or “the best thing since
sliced bread”). The TOAST infrastructure is also used to improve handling of large data
values in-memory.

10. What are the uses of default databases in pg_databases such as template1,
template0.
Ans: template1 is the one used by default. You can alter / add / remove objects there to
affect every newly created DB

11. relowner is the id of owner; select relname,relowner from pg_class where relname =
'x'; select *from pg_user;
Ans: in pg_class every relation(table) has relowner which is the id of the user who created
that relation. The relowner id is nothing but usesysid from pg_user.

12. What are some default pg_attributes that come with every table but our default.
Ans: cmax,cmin,xmax,xmin,tableoid

13. What is the difference between peer,trust and password?


ANs: in trust = no password is required.
In password = password is required.
In peer = sometimes password is required. Sometimes not. Depending on the
process.

14. What is a sequence? Create a sequence.


ANs: In PostgreSQL, a sequence is a user-defined schema-bound object which creates a
sequence of integers depending on the particular requirement. CREATE SEQUENCE creates
a new sequence number generator. This involves creating and initializing a new special
single-row table with the name name. The generator will be owned by the user issuing the
command.

15. Alter sequence.


Ans: ALTER SEQUENCE changes the parameters of an existing sequence generator. Any
parameters not specifically set in the ALTER SEQUENCE command retain their prior settings.

16. \df nextval


Ans: Advances the sequence object to its next value and returns that value. This is done
atomically: even if multiple sessions execute nextval concurrently, each will safely receive
a distinct sequence value.

17. \explain
Ans: It returns the execution plan generated by PostgreSQL query planner for a
given statement.

18. analyze explain.


Ans: The ANALYZE option causes the statement to be actually executed, not only planned.
Then actual run time statistics are added to the display, including the total elapsed time
expended within each plan node (in milliseconds) and the total number of rows it actually
returned. This is useful for seeing whether the planner's estimates are close to reality.

19. query planner and query rewriter.


Ans: The task of the planner/optimizer is to create an optimal execution plan. A given SQL
query (and hence, a query tree) can be actually executed in a wide variety of different ways,
each of which will produce the same set of results. If it is computationally feasible, the query
optimizer will examine each of these possible execution plans, ultimately selecting the
execution plan that is expected to run the fastest.

20. \create index


Ans: index is a pointer to data in a table. CREATE INDEX constructs an index on the
specified column(s) of the specified relation, which can be a table or a materialized view.
Indexes are primarily used to enhance database performance (though inappropriate use can
result in slower performance).
The key field(s) for the index are specified as column names, or alternatively as expressions
written in parentheses. Multiple fields can be specified if the index method supports
multicolumn indexes.

21. seq search , index search


Ans:

22. create user abc with nosuperuser;


Ans: createuser creates a new PostgreSQL user (or more precisely, a role). Only superusers
and users with CREATEROLE privilege can create new users, so createuser must be invoked
by someone who can connect as a superuser or a user with CREATEROLE privilege.
If you wish to create a new superuser, you must connect as a superuser, not merely with
CREATEROLE privilege. Being a superuser implies the ability to bypass all access permission
checks within the database, so superuser access should not be granted lightly.

23. Grant , revoke


Ans: You can GRANT and REVOKE privileges on various database objects in
PostgreSQL. We'll look at how to grant and revoke privileges on tables in
PostgreSQL.

Grant Privileges on Table


You can grant users various privileges to tables. These permissions can be any
combination of SELECT, INSERT, UPDATE, DELETE, INDEX, CREATE, ALTER,
DROP, GRANT OPTION or ALL.

Syntax
The syntax for granting privileges on a table in PostgreSQL is:
GRANT privileges ON object TO user;
24. \dS+ tableName
Ans: Displays sequences.

25. create temp table


Ans: A temporary table, as the name implies, is a short-lived table that exists for the duration
of a database session. PostgreSQL automatically drops the temporary tables at the end of a
session or a transaction. Syntax: CREATE TEMPORARY TABLE temp_table( ... );

26. \h set
Ans: set command is used to change run time parameter. For e,g set search_path.

27. show search_path


Ans: Search_path sets which schema to search for searching a table. If in search_path
public is first. It will only search tables in public shema.

28. select *from pg_namespace;


Ans: Displays scheamas.

29. what is pg_toast?


Ans: Toast is a mechanism in PostgreSQL to handle large chunks of
data to fit in page buffer. When the data exceeds
TOAST_TUPLE_THRESHOLD (2KB default), Postgres will compress the
data, trying to fit in 2KB buffer size. If the compressing of the large
column data does not lead to smaller block (<2KB), it will be split
into smaller chunks.
This is enabled by default and all tables will have the toast table
associated with it. You can check the associated toast table in
pg_class using the following query:

30. TOAST : table of oversized attribute storage. That compression mechanism is called
as toast.

31. HashAggregate, Group Key, Hash join, Hash semijoin

32. tushar1=# \h vacuum


Ans: VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL operation,
tuples that are deleted or obsoleted by an update are not physically removed from their
table; they remain present until a VACUUM is done. Therefore it's necessary to do VACUUM
periodically, especially on frequently-updated tables.

33. initdb creates a new PostgreSQL database cluster. A database cluster is a collection
of databases that are managed by a single server instance.
Ans: initdb creates a new PostgreSQL database cluster. A database cluster is a collection
of databases that are managed by a single server instance.

34. relkind from pg_class.


Ans: relkind char
r = ordinary table, i = index, S = sequence, t = TOAST table, v = view, m = materialized
view, c = composite type, f = foreign table, p = partitioned table, I = partitioned index

35. tushar=# set enable_seqscan TO off;


Ans: Set enable helps to off a specific type of plan to turn on or turn off.

36. Processing a Query, postmaster, lex/yacc, join tree, query planner/optimizer.


ANs: A query comes to the backend via data packets arriving through TCP/IP or Unix
Domain
sockets. It is loaded into a string, and passed to the parser, where the lexical scanner,
scan.l, breaks the query up into tokens(words). The parser uses gram.y and the tokens to
identify the query type, and load the proper query-specific structure, like CreateStmt or
SelectStmt.
The statement is then identified as complex (SELECT / INSERT / UPDATE / DELETE) or
a simple, e.g CREATE USER, ANALYZE, , etc. Simple utility commands are processed by
statement-specific functions in backend/commands. Complex statements require more
handling.
The parser takes a complex query, and creates a Query structure that contains all the
elements used by complex queries. Query.qual holds the WHERE clause qualification,
which is filled in by transformWhereClause(). Each table referenced in the query is
represented by a RangeTableEntry, and they are linked together to form the range table
of the query, which is generated by transformFromClause(). Query.rtable holds the
query's range table.
Certain queries, like SELECT, return columns of data. Other queries, like INSERT and
UPDATE, specify the columns modified by the query. These column references are
converted to TargetEntry entries, which are linked together to make up the target list of
the query. The target list is stored in Query.targetList, which is generated by
transformTargetList().
Other query elements, like aggregates(SUM()), GROUP BY, and ORDER BY are also
stored in their own Query fields.
The next step is for the Query to be modified by any VIEWS or RULES that may apply to
the query. This is performed by the rewrite system.
The optimizer takes the Query structure and generates an optimal Plan, which contains
the operations to be performed to execute the query. The path module determines the best
table join order and join type of each table in the RangeTable, using Query.qual(WHERE
clause) to consider optimal index usage.
The Plan is then passed to the executor for execution, and the result returned to the client.
The Plan is actually as set of nodes, arranged in a tree structure with a top-level node, and
various sub-nodes as children.
There are many other modules that support this basic functionality. They can be accessed
by clicking on the flowchart.

37. Arrays in postgresql


https://www.geeksforgeeks.org/postgresql-array-data-type/

38. \h alter group.


Ans: ALTER GROUP changes the attributes of a user group. This is an obsolete command,
though still accepted for backwards compatibility, because groups (and users too) have been
superseded by the more general concept of roles.

39. tushar=# create group noadmin;


CREATE ROLE
CREATE GROUP will create a new group of users. You must be a database superuser to
use this command.

tushar=# alter group noadmin add user abc2;


ALTER ROLE

tushar=# grant select on barcode TO noadmin;


GRANT
40. Begin, commit, rollback.
Ans: https://www.tutorialspoint.com/postgresql/postgresql_transactions.htm

41. AccessSharedLock, AccessExclusiveLock.


Ans: ACCESS SHARE MODE
The ACCESS SHARE MODE lock is acquired automatically by a SELECT statement on
the table or tables it retrieves from. This mode blocks ALTER TABLE, DROP TABLE,
and VACUUM commands on the table on which it is placed.

This mode also blocks concurrent ACCESS EXCLUSIVE MODE locks from being
acquired on the same table.

ROW SHARE MODE

The ROW SHARE MODE lock is acquired automatically by a SELECT statement that
has a FOR UPDATE clause. It blocks ALTER TABLE, DROP TABLE, and VACUUM
commands on the table on which it is acquired.

This mode also blocks concurrent EXCLUSIVE MODE and ACCESS EXCLUSIVE MODE
locks from being acquired on the same table.

ROW EXCLUSIVE MODE

The ROW EXCLUSIVE MODE lock is acquired automatically by an UPDATE, INSERT, or


DELETE command. This mode blocks ALTER TABLE, DROP TABLE, VACUUM, and
CREATE INDEX commands.
This mode also blocks concurrent SHARE MODE, SHARE ROW EXCLUSIVE MODE,
EXCLUSIVE MODE, and ACCESS EXCLUSIVE MODE locks from being acquired on the
same table.

42. who has to wait for what

select
select for update
select for shared
update,delete,insert
vaccum(without full),analyze, create index,alter table(with validation)
with concurrency,w
materialized view
drop table,truncate,reindex,vaccum(full),alter tale(add column),alter table(drop column).

43. Inheritance in postgresql.


Ans: https://www.postgresql.org/docs/current/tutorial-inheritance.html

44. functions in postgresql.


Ans: https://www.tutorialspoint.com/postgresql/postgresql_functions.htm

45. View
Ans: https://www.tutorialspoint.com/postgresql/postgresql_views.htm

You might also like