|
| 1 | +#include"postgres.h" |
| 2 | + |
| 3 | +#include"access/genam.h" |
| 4 | +#include"access/heapam.h" |
| 5 | +#include"miscadmin.h" |
| 6 | +#include"storage/lmgr.h" |
| 7 | +#include"storage/bufmgr.h" |
| 8 | +#include"catalog/namespace.h" |
| 9 | +#include"utils/lsyscache.h" |
| 10 | +#include"utils/builtins.h" |
| 11 | +#include<fmgr.h> |
| 12 | +#include<funcapi.h> |
| 13 | +#include<access/heapam.h> |
| 14 | +#include<catalog/pg_type.h> |
| 15 | +#include<catalog/heap.h> |
| 16 | +#include<commands/vacuum.h> |
| 17 | + |
| 18 | +#ifdefPG_MODULE_MAGIC |
| 19 | +PG_MODULE_MAGIC; |
| 20 | +#endif |
| 21 | + |
| 22 | +PG_FUNCTION_INFO_V1(fasttruncate); |
| 23 | +Datumfasttruncate(PG_FUNCTION_ARGS); |
| 24 | +Datum |
| 25 | +fasttruncate(PG_FUNCTION_ARGS) { |
| 26 | +text*name=PG_GETARG_TEXT_P(0); |
| 27 | +char*relname; |
| 28 | +List*relname_list; |
| 29 | +RangeVar*relvar; |
| 30 | +OidrelOid; |
| 31 | +Relationrel; |
| 32 | +boolmakeanalyze= false; |
| 33 | + |
| 34 | +relname=palloc(VARSIZE(name)+1); |
| 35 | +memcpy(relname,VARDATA(name),VARSIZE(name)-VARHDRSZ); |
| 36 | +relname[VARSIZE(name)-VARHDRSZ ]='\0'; |
| 37 | + |
| 38 | +relname_list=stringToQualifiedNameList(relname); |
| 39 | +relvar=makeRangeVarFromNameList(relname_list); |
| 40 | +relOid=RangeVarGetRelid(relvar,AccessExclusiveLock, false); |
| 41 | + |
| 42 | +if (get_rel_relkind(relOid)!=RELKIND_RELATION ) |
| 43 | +elog(ERROR,"Relation isn't a ordinary table"); |
| 44 | + |
| 45 | +rel=heap_open(relOid,NoLock); |
| 46 | + |
| 47 | +if ( !isTempNamespace(get_rel_namespace(relOid)) ) |
| 48 | +elog(ERROR,"Relation isn't a temporary table"); |
| 49 | + |
| 50 | +heap_truncate(list_make1_oid(relOid)); |
| 51 | + |
| 52 | +if (rel->rd_rel->relpages>0||rel->rd_rel->reltuples>0 ) |
| 53 | +makeanalyze= true; |
| 54 | + |
| 55 | +/* |
| 56 | + * heap_truncate doesn't unlock the table, |
| 57 | + * so we should unlock it. |
| 58 | + */ |
| 59 | + |
| 60 | +heap_close(rel,AccessExclusiveLock); |
| 61 | + |
| 62 | +if (makeanalyze ) { |
| 63 | +VacuumParamsparams; |
| 64 | + |
| 65 | +params.freeze_min_age=-1; |
| 66 | +params.freeze_table_age=-1; |
| 67 | +params.multixact_freeze_min_age=-1; |
| 68 | +params.multixact_freeze_table_age=-1; |
| 69 | +params.is_wraparound= false; |
| 70 | +params.log_min_duration=-1; |
| 71 | + |
| 72 | +vacuum(VACOPT_ANALYZE,NULL,relOid,¶ms,NULL, |
| 73 | +GetAccessStrategy(BAS_VACUUM), false); |
| 74 | +} |
| 75 | + |
| 76 | +PG_RETURN_VOID(); |
| 77 | +} |