Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * discard.c
4 : * The implementation of the DISCARD command
5 : *
6 : * Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : *
8 : *
9 : * IDENTIFICATION
10 : * src/backend/commands/discard.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #include "postgres.h"
15 :
16 : #include "access/xact.h"
17 : #include "catalog/namespace.h"
18 : #include "commands/async.h"
19 : #include "commands/discard.h"
20 : #include "commands/prepare.h"
21 : #include "commands/sequence.h"
22 : #include "storage/lock.h"
23 : #include "utils/guc.h"
24 : #include "utils/portal.h"
25 :
26 : static void DiscardAll(bool isTopLevel);
27 :
28 : /*
29 : * DISCARD { ALL | SEQUENCES | TEMP | PLANS }
30 : */
31 : void
32 24 : DiscardCommand(DiscardStmt *stmt, bool isTopLevel)
33 : {
34 24 : switch (stmt->target)
35 : {
36 4 : case DISCARD_ALL:
37 4 : DiscardAll(isTopLevel);
38 4 : break;
39 :
40 3 : case DISCARD_PLANS:
41 3 : ResetPlanCache();
42 3 : break;
43 :
44 8 : case DISCARD_SEQUENCES:
45 8 : ResetSequenceCaches();
46 8 : break;
47 :
48 9 : case DISCARD_TEMP:
49 9 : ResetTempTableNamespace();
50 9 : break;
51 :
52 0 : default:
53 0 : elog(ERROR, "unrecognized DISCARD target: %d", stmt->target);
54 : }
55 24 : }
56 :
57 : static void
58 4 : DiscardAll(bool isTopLevel)
59 : {
60 : /*
61 : * Disallow DISCARD ALL in a transaction block. This is arguably
62 : * inconsistent (we don't make a similar check in the command sequence
63 : * that DISCARD ALL is equivalent to), but the idea is to catch mistakes:
64 : * DISCARD ALL inside a transaction block would leave the transaction
65 : * still uncommitted.
66 : */
67 4 : PreventInTransactionBlock(isTopLevel, "DISCARD ALL");
68 :
69 : /* Closing portals might run user-defined code, so do that first. */
70 4 : PortalHashTableDeleteAll();
71 4 : SetPGVariable("session_authorization", NIL, false);
72 4 : ResetAllOptions();
73 4 : DropAllPreparedStatements();
74 4 : Async_UnlistenAll();
75 4 : LockReleaseAll(USER_LOCKMETHOD, true);
76 4 : ResetPlanCache();
77 4 : ResetTempTableNamespace();
78 4 : ResetSequenceCaches();
79 4 : }
|